blob: 4085a3aba777eb69a71c53f1d49d402fc4f96e5d [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>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Raj Kamal366d9b42014-06-11 13:49:32 +053080#ifdef QCOM_BSP
81#include <gralloc_priv.h>
82#endif
Mathias Agopian875d8e12013-06-07 15:35:48 -070083
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -070084#ifdef QCOM_BSP
85#include <display_config.h>
86#endif
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088#define DISPLAY_COUNT 1
89
Mathias Agopianfee2b462013-07-03 12:34:01 -070090/*
91 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
92 * black pixels.
93 */
94#define DEBUG_SCREENSHOTS false
95
Mathias Agopianca088332013-03-28 17:44:13 -070096EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
97
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700100// This is the phase offset in nanoseconds of the software vsync event
101// relative to the vsync event reported by HWComposer. The software vsync
102// event is when SurfaceFlinger and Choreographer-based applications run each
103// frame.
104//
105// This phase offset allows adjustment of the minimum latency from application
106// wake-up (by Choregographer) time to the time at which the resulting window
107// image is displayed. This value may be either positive (after the HW vsync)
108// or negative (before the HW vsync). Setting it to 0 will result in a
109// minimum latency of two vsync periods because the app and SurfaceFlinger
110// will run just after the HW vsync. Setting it to a positive number will
111// result in the minimum latency being:
112//
113// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
114//
115// Note that reducing this latency makes it more likely for the applications
116// to not have their window content image ready in time. When this happens
117// the latency will end up being an additional vsync period, and animations
118// will hiccup. Therefore, this latency should be tuned somewhat
119// conservatively (or at least with awareness of the trade-off being made).
120static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
121
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700122// This is the phase offset at which SurfaceFlinger's composition runs.
123static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125// ---------------------------------------------------------------------------
126
Mathias Agopian99b49842011-06-27 16:05:52 -0700127const String16 sHardwareTest("android.permission.HARDWARE_TEST");
128const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
129const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
130const String16 sDump("android.permission.DUMP");
131
132// ---------------------------------------------------------------------------
Arun Kumar K.R766d5f62013-06-21 19:36:02 -0700133// Initialize extendedMode to false
134#ifdef QCOM_BSP
135bool SurfaceFlinger::sExtendedMode = false;
136#endif
Mathias Agopian99b49842011-06-27 16:05:52 -0700137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700139 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700141 mTransactionPending(false),
142 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700144 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700145 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700148 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800149 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700151 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700152 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700153 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700154 mDebugInSwapBuffers(0),
155 mLastSwapBufferTime(0),
156 mDebugInTransaction(0),
157 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700158 mBootFinished(false),
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530159 mGpuTileRenderEnable(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700160 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700161 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700162 mDaltonize(false),
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530163 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Steve Blocka19954a2012-01-04 20:05:49 +0000165 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
167 // debugging stuff...
168 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169
Mathias Agopianb4b17302013-03-20 18:36:41 -0700170 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700171 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 property_get("debug.sf.showupdates", value, "0");
174 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700175
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700176 property_get("debug.sf.ddms", value, "0");
177 mDebugDDMS = atoi(value);
178 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700179 if (!startDdmConnection()) {
180 // start failed, and DDMS debugging not enabled
181 mDebugDDMS = 0;
182 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700183 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530184#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530185 mCanUseGpuTileRender = false;
186 property_get("debug.sf.gpu_comp_tiling", value, "0");
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530187 mGpuTileRenderEnable = atoi(value) ? true : false;
188 if(mGpuTileRenderEnable)
189 ALOGV("DirtyRect optimization enabled for FULL GPU Composition");
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530190 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530191#endif
192
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700193 ALOGI_IF(mDebugRegion, "showupdates enabled");
194 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800197void SurfaceFlinger::onFirstRef()
198{
199 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800200}
201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202SurfaceFlinger::~SurfaceFlinger()
203{
Mathias Agopiana4912602012-07-12 14:25:33 -0700204 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
205 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
206 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
Dan Stozac7014012014-02-14 15:03:43 -0800209void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800210{
211 // the window manager died on us. prepare its eulogy.
212
Andy McFadden13a082e2012-08-24 10:16:42 -0700213 // restore initial conditions (default device unblank, etc)
214 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800215
216 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700217 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800218}
219
Mathias Agopian7e27f052010-05-28 14:22:23 -0700220sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221{
Mathias Agopian96f08192010-06-02 23:28:45 -0700222 sp<ISurfaceComposerClient> bclient;
223 sp<Client> client(new Client(this));
224 status_t err = client->initCheck();
225 if (err == NO_ERROR) {
226 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 return bclient;
229}
230
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700231sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
232 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233{
234 class DisplayToken : public BBinder {
235 sp<SurfaceFlinger> flinger;
236 virtual ~DisplayToken() {
237 // no more references, this display must be terminated
238 Mutex::Autolock _l(flinger->mStateLock);
239 flinger->mCurrentState.displays.removeItem(this);
240 flinger->setTransactionFlags(eDisplayTransactionNeeded);
241 }
242 public:
243 DisplayToken(const sp<SurfaceFlinger>& flinger)
244 : flinger(flinger) {
245 }
246 };
247
248 sp<BBinder> token = new DisplayToken(this);
249
250 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700251 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700252 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700253 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700254 mCurrentState.displays.add(token, info);
255
256 return token;
257}
258
Jesse Hall6c913be2013-08-08 12:15:49 -0700259void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
260 Mutex::Autolock _l(mStateLock);
261
262 ssize_t idx = mCurrentState.displays.indexOfKey(display);
263 if (idx < 0) {
264 ALOGW("destroyDisplay: invalid display token");
265 return;
266 }
267
268 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
269 if (!info.isVirtualDisplay()) {
270 ALOGE("destroyDisplay called for non-virtual display");
271 return;
272 }
273
274 mCurrentState.displays.removeItemsAt(idx);
275 setTransactionFlags(eDisplayTransactionNeeded);
276}
277
Jesse Hall692c7232012-11-08 15:41:56 -0800278void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
279 ALOGW_IF(mBuiltinDisplays[type],
280 "Overwriting display token for display type %d", type);
281 mBuiltinDisplays[type] = new BBinder();
282 DisplayDeviceState info(type);
283 // All non-virtual displays are currently considered secure.
284 info.isSecure = true;
285 mCurrentState.displays.add(mBuiltinDisplays[type], info);
286}
287
Mathias Agopiane57f2922012-08-09 16:29:12 -0700288sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700289 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700290 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
291 return NULL;
292 }
Jesse Hall692c7232012-11-08 15:41:56 -0800293 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700294}
295
Jamie Gennis9a78c902011-01-12 18:30:40 -0800296sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
297{
298 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
299 return gba;
300}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302void SurfaceFlinger::bootFinished()
303{
304 const nsecs_t now = systemTime();
305 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000306 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700307 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700308
309 // wait patiently for the window manager death
310 const String16 name("window");
311 sp<IBinder> window(defaultServiceManager()->getService(name));
312 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700314 }
315
316 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700317 // formerly we would just kill the process, but we now ask it to exit so it
318 // can choose where to stop the animation.
319 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320}
321
Mathias Agopian3f844832013-08-07 21:24:32 -0700322void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700323 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700324 RenderEngine& engine;
325 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700327 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
328 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700329 }
330 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700331 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700332 return true;
333 }
334 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700335 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700336}
337
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700338class DispSyncSource : public VSyncSource, private DispSync::Callback {
339public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700340 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
341 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700342 mValue(0),
343 mPhaseOffset(phaseOffset),
344 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700345 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
346 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700347 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700348
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 virtual ~DispSyncSource() {}
350
351 virtual void setVSyncEnabled(bool enable) {
352 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
353 // with locking it in the onDispSyncEvent callback.
354 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700355 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700356 static_cast<DispSync::Callback*>(this));
357 if (err != NO_ERROR) {
358 ALOGE("error registering vsync callback: %s (%d)",
359 strerror(-err), err);
360 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700361 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700362 } else {
363 status_t err = mDispSync->removeEventListener(
364 static_cast<DispSync::Callback*>(this));
365 if (err != NO_ERROR) {
366 ALOGE("error unregistering vsync callback: %s (%d)",
367 strerror(-err), err);
368 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700369 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370 }
371 }
372
373 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
374 Mutex::Autolock lock(mMutex);
375 mCallback = callback;
376 }
377
378private:
379 virtual void onDispSyncEvent(nsecs_t when) {
380 sp<VSyncSource::Callback> callback;
381 {
382 Mutex::Autolock lock(mMutex);
383 callback = mCallback;
384
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700385 if (mTraceVsync) {
386 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700387 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700388 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700389 }
390
391 if (callback != NULL) {
392 callback->onVSyncEvent(when);
393 }
394 }
395
396 int mValue;
397
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700398 const nsecs_t mPhaseOffset;
399 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700400 const String8 mVsyncOnLabel;
401 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700402
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700403 DispSync* mDispSync;
404 sp<VSyncSource::Callback> mCallback;
405 Mutex mMutex;
406};
407
408void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700409 ALOGI( "SurfaceFlinger's main thread ready to run. "
410 "Initializing graphics H/W...");
411
Jesse Hallb65f32e2013-09-27 22:10:47 -0700412 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800413 Mutex::Autolock _l(mStateLock);
414
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700415 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700416 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
417 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700418
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700419 // Initialize the H/W composer object. There may or may not be an
420 // actual hardware composer underneath.
421 mHwc = new HWComposer(this,
422 *static_cast<HWComposer::EventHandler *>(this));
423
Mathias Agopian875d8e12013-06-07 15:35:48 -0700424 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800425 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700426
427 // retrieve the EGL context that was selected/created
428 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700429
Mathias Agopianda27af92012-09-13 18:17:13 -0700430 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
431 "couldn't create EGLContext");
432
Mathias Agopiancde87a32012-09-13 14:09:01 -0700433 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700434 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700435 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700436 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700437 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700438 // All non-virtual displays are currently considered secure.
439 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800440 createBuiltinDisplayLocked(type);
441 wp<IBinder> token = mBuiltinDisplays[i];
442
Dan Stozab9b08832014-03-13 11:55:57 -0700443 sp<IGraphicBufferProducer> producer;
444 sp<IGraphicBufferConsumer> consumer;
445 BufferQueue::createBufferQueue(&producer, &consumer,
446 new GraphicBufferAlloc());
447
448 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
449 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800450 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700451 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800452 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700453 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800454 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700455 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700456 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700457 // for displays other than the main display, so we always
458 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800459 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700460 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700461 }
462 mDisplays.add(token, hw);
463 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700464 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700465
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700466 // make the GLContext current so that we can create textures when creating Layers
467 // (which may happens before we render something)
468 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
469
Mathias Agopian028508c2012-07-25 21:12:12 -0700470 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700471 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700472 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700473 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700474 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700475 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700476 mSFEventThread = new EventThread(sfVsyncSrc);
477 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700478
Jamie Gennisd1700752013-10-14 12:22:52 -0700479 mEventControlThread = new EventControlThread(this);
480 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
481
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700482 // set a fake vsync period if there is no HWComposer
483 if (mHwc->initCheck() != NO_ERROR) {
484 mPrimaryDispSync.setPeriod(16666667);
485 }
486
Mathias Agopian92a979a2012-08-02 18:32:23 -0700487 // initialize our drawing state
488 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700489
Andy McFadden13a082e2012-08-24 10:16:42 -0700490 // set initial conditions (e.g. unblank default device)
491 initializeDisplays();
492
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700493 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700494 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495}
496
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700497int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700498 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700499 type : mHwc->allocateDisplayId();
500}
501
Mathias Agopiana67e4182012-06-19 17:26:12 -0700502void SurfaceFlinger::startBootAnim() {
503 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700504 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700505 property_set("service.bootanim.exit", "0");
506 property_set("ctl.start", "bootanim");
507}
508
Mathias Agopian875d8e12013-06-07 15:35:48 -0700509size_t SurfaceFlinger::getMaxTextureSize() const {
510 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700511}
512
Mathias Agopian875d8e12013-06-07 15:35:48 -0700513size_t SurfaceFlinger::getMaxViewportDims() const {
514 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700515}
516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800518
Jamie Gennis582270d2011-08-17 18:19:00 -0700519bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800520 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800521 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800522 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700523 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800524}
525
Dan Stoza7f7da322014-05-02 15:26:25 -0700526status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
527 Vector<DisplayInfo>* configs) {
528 if (configs == NULL) {
529 return BAD_VALUE;
530 }
531
Jesse Hall692c7232012-11-08 15:41:56 -0800532 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700533 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800534 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700535 type = i;
536 break;
537 }
538 }
539
540 if (type < 0) {
541 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700542 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700543
Mathias Agopian8b736f12012-08-13 17:54:26 -0700544 // TODO: Not sure if display density should handled by SF any longer
545 class Density {
546 static int getDensityFromProperty(char const* propName) {
547 char property[PROPERTY_VALUE_MAX];
548 int density = 0;
549 if (property_get(propName, property, NULL) > 0) {
550 density = atoi(property);
551 }
552 return density;
553 }
554 public:
555 static int getEmuDensity() {
556 return getDensityFromProperty("qemu.sf.lcd_density"); }
557 static int getBuildDensity() {
558 return getDensityFromProperty("ro.sf.lcd_density"); }
559 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700560
Dan Stoza7f7da322014-05-02 15:26:25 -0700561 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700562
Dan Stoza7f7da322014-05-02 15:26:25 -0700563 const Vector<HWComposer::DisplayConfig>& hwConfigs =
564 getHwComposer().getConfigs(type);
565 for (size_t c = 0; c < hwConfigs.size(); ++c) {
566 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
567 DisplayInfo info = DisplayInfo();
568
569 float xdpi = hwConfig.xdpi;
570 float ydpi = hwConfig.ydpi;
571
572 if (type == DisplayDevice::DISPLAY_PRIMARY) {
573 // The density of the device is provided by a build property
574 float density = Density::getBuildDensity() / 160.0f;
575 if (density == 0) {
576 // the build doesn't provide a density -- this is wrong!
577 // use xdpi instead
578 ALOGE("ro.sf.lcd_density must be defined as a build property");
579 density = xdpi / 160.0f;
580 }
581 if (Density::getEmuDensity()) {
582 // if "qemu.sf.lcd_density" is specified, it overrides everything
583 xdpi = ydpi = density = Density::getEmuDensity();
584 density /= 160.0f;
585 }
586 info.density = density;
587
588 // TODO: this needs to go away (currently needed only by webkit)
589 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
590 info.orientation = hw->getOrientation();
591 } else {
592 // TODO: where should this value come from?
593 static const int TV_DENSITY = 213;
594 info.density = TV_DENSITY / 160.0f;
595 info.orientation = 0;
596 }
597
598 info.w = hwConfig.width;
599 info.h = hwConfig.height;
600 info.xdpi = xdpi;
601 info.ydpi = ydpi;
602 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700603 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
604
605 // This is how far in advance a buffer must be queued for
606 // presentation at a given time. If you want a buffer to appear
607 // on the screen at time N, you must submit the buffer before
608 // (N - presentationDeadline).
609 //
610 // Normally it's one full refresh period (to give SF a chance to
611 // latch the buffer), but this can be reduced by configuring a
612 // DispSync offset. Any additional delays introduced by the hardware
613 // composer or panel must be accounted for here.
614 //
615 // We add an additional 1ms to allow for processing time and
616 // differences between the ideal and actual refresh rate.
617 info.presentationDeadline =
618 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700619
620 // All non-virtual displays are currently considered secure.
621 info.secure = true;
622
623 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700624 }
625
Dan Stoza7f7da322014-05-02 15:26:25 -0700626 return NO_ERROR;
627}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700628
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700629status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
630 DisplayStatInfo* stats) {
631 if (stats == NULL) {
632 return BAD_VALUE;
633 }
634
635 // FIXME for now we always return stats for the primary display
636 memset(stats, 0, sizeof(*stats));
637 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
638 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
639 return NO_ERROR;
640}
641
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700642int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
643 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700644}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700645
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700646void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
647 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
648 this);
649 int32_t type = hw->getDisplayType();
650 int currentMode = hw->getActiveConfig();
651
652 if (mode == currentMode) {
653 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
654 return;
655 }
656
657 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
658 ALOGW("Trying to set config for virtual display");
659 return;
660 }
661
662 hw->setActiveConfig(mode);
663 getHwComposer().setActiveConfig(type, mode);
664}
665
666status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
667 class MessageSetActiveConfig: public MessageBase {
668 SurfaceFlinger& mFlinger;
669 sp<IBinder> mDisplay;
670 int mMode;
671 public:
672 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
673 int mode) :
674 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
675 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700676 Vector<DisplayInfo> configs;
677 mFlinger.getDisplayConfigs(mDisplay, &configs);
678 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700679 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700680 mMode, configs.size());
681 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700682 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
683 if (hw == NULL) {
684 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700685 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700686 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
687 ALOGW("Attempt to set active config = %d for virtual display",
688 mMode);
689 } else {
690 mFlinger.setActiveConfigInternal(hw, mMode);
691 }
692 return true;
693 }
694 };
695 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
696 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700697 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700698}
699
Svetoslavd85084b2014-03-20 10:28:31 -0700700status_t SurfaceFlinger::clearAnimationFrameStats() {
701 Mutex::Autolock _l(mStateLock);
702 mAnimFrameTracker.clearStats();
703 return NO_ERROR;
704}
705
706status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
707 Mutex::Autolock _l(mStateLock);
708 mAnimFrameTracker.getStats(outStats);
709 return NO_ERROR;
710}
711
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800712// ----------------------------------------------------------------------------
713
714sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800715 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700716}
717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800719
720void SurfaceFlinger::waitForEvent() {
721 mEventQueue.waitMessage();
722}
723
724void SurfaceFlinger::signalTransaction() {
725 mEventQueue.invalidate();
726}
727
728void SurfaceFlinger::signalLayerUpdate() {
729 mEventQueue.invalidate();
730}
731
732void SurfaceFlinger::signalRefresh() {
733 mEventQueue.refresh();
734}
735
736status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800737 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800738 return mEventQueue.postMessage(msg, reltime);
739}
740
741status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800742 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800743 status_t res = mEventQueue.postMessage(msg, reltime);
744 if (res == NO_ERROR) {
745 msg->wait();
746 }
747 return res;
748}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700750void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700751 do {
752 waitForEvent();
753 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754}
755
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700756void SurfaceFlinger::enableHardwareVsync() {
757 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700758 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700759 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700760 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
761 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700762 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700763 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700764}
765
Jesse Hall948fe0c2013-10-14 12:56:09 -0700766void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700767 Mutex::Autolock _l(mHWVsyncLock);
768
Jesse Hall948fe0c2013-10-14 12:56:09 -0700769 if (makeAvailable) {
770 mHWVsyncAvailable = true;
771 } else if (!mHWVsyncAvailable) {
772 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
773 return;
774 }
775
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700776 const nsecs_t period =
777 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
778
779 mPrimaryDispSync.reset();
780 mPrimaryDispSync.setPeriod(period);
781
782 if (!mPrimaryHWVsyncEnabled) {
783 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700784 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
785 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700786 mPrimaryHWVsyncEnabled = true;
787 }
788}
789
Jesse Hall948fe0c2013-10-14 12:56:09 -0700790void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791 Mutex::Autolock _l(mHWVsyncLock);
792 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700793 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
794 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700795 mPrimaryDispSync.endResync();
796 mPrimaryHWVsyncEnabled = false;
797 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700798 if (makeUnavailable) {
799 mHWVsyncAvailable = false;
800 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700801}
802
803void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700804 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700805
Jamie Gennisd1700752013-10-14 12:22:52 -0700806 { // Scope for the lock
807 Mutex::Autolock _l(mHWVsyncLock);
808 if (type == 0 && mPrimaryHWVsyncEnabled) {
809 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700810 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700811 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700812
813 if (needsHwVsync) {
814 enableHardwareVsync();
815 } else {
816 disableHardwareVsync(false);
817 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700818}
819
820void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
821 if (mEventThread == NULL) {
822 // This is a temporary workaround for b/7145521. A non-null pointer
823 // does not mean EventThread has finished initializing, so this
824 // is not a correct fix.
825 ALOGW("WARNING: EventThread not started, ignoring hotplug");
826 return;
827 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700828
Jesse Hall9e663de2013-08-16 14:28:37 -0700829 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700830 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800831 if (connected) {
832 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700833 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800834 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
835 mBuiltinDisplays[type].clear();
Arun Kumar K.R766d5f62013-06-21 19:36:02 -0700836#ifdef QCOM_BSP
837 // if extended_mode is set, and set mVisibleRegionsDirty
838 // as we need to rebuildLayerStack
839 if(isExtendedMode()) {
840 mVisibleRegionsDirty = true;
841 }
842#endif
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700843 }
844 setTransactionFlags(eDisplayTransactionNeeded);
845
Andy McFadden9e9689c2012-10-10 18:17:51 -0700846 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700847 }
Mathias Agopian86303202012-07-24 22:46:10 -0700848}
849
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700850void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700851 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700852 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700853}
854
Mathias Agopian4fec8732012-06-29 14:12:52 -0700855void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800856 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800857 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700858 case MessageQueue::TRANSACTION:
859 handleMessageTransaction();
860 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700861 case MessageQueue::INVALIDATE:
862 handleMessageTransaction();
863 handleMessageInvalidate();
864 signalRefresh();
865 break;
866 case MessageQueue::REFRESH:
867 handleMessageRefresh();
868 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800869 }
870}
871
Mathias Agopian4fec8732012-06-29 14:12:52 -0700872void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700873 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700874 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700875 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700876 }
877}
878
879void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700880 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700881 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700882}
883
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530884#ifdef QCOM_BSP
885/* Compute DirtyRegion, if DR optimization for GPU comp optimization
886 * is ON & and no external device is connected.*/
887void SurfaceFlinger::setUpTiledDr() {
888 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
889 const sp<DisplayDevice>& hw(mDisplays[HWC_DISPLAY_PRIMARY]);
890 mCanUseGpuTileRender = computeTiledDr(hw);
891 }
892}
893#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700894void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700895 ATRACE_CALL();
896 preComposition();
897 rebuildLayerStacks();
898 setUpHWComposer();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530899#ifdef QCOM_BSP
900 setUpTiledDr();
901#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -0700902 doDebugFlashRegions();
903 doComposition();
904 postComposition();
905}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700906
Mathias Agopiancd60f992012-08-16 16:28:27 -0700907void SurfaceFlinger::doDebugFlashRegions()
908{
909 // is debugging enabled
910 if (CC_LIKELY(!mDebugRegion))
911 return;
912
913 const bool repaintEverything = mRepaintEverything;
914 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
915 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530916
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700917 if (hw->isDisplayOn()) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530918 const int32_t height = hw->getHeight();
919 RenderEngine& engine(getRenderEngine());
920#ifdef QCOM_BSP
921 // Use Union DR, if it is valid & GPU Tiled DR optimization is ON
922 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 // redraw the whole screen
924 doComposeSurfaces(hw, Region(hw->bounds()));
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530925 Region dirtyRegion(mUnionDirtyRect);
926 Rect dr = mUnionDirtyRect;
927 hw->eglSwapPreserved(true);
928 engine.startTileComposition(dr.left, (height-dr.bottom),
929 (dr.right-dr.left),
930 (dr.bottom-dr.top), 1);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700931 // and draw the dirty region
Mathias Agopian3f844832013-08-07 21:24:32 -0700932 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530933 engine.endTileComposition(GL_PRESERVE);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700934 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700935 hw->swapBuffers(getHwComposer());
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530936 } else
937#endif
938 {
939 // transform the dirty region into this screen's coordinate
940 // space
941 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
942 if (!dirtyRegion.isEmpty()) {
943 // redraw the whole screen
944 doComposeSurfaces(hw, Region(hw->bounds()));
945 // and draw the dirty region
946#ifdef QCOM_BSP
947 if(mGpuTileRenderEnable)
948 hw->eglSwapPreserved(false);
949#endif
950
951 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
952 hw->compositionComplete();
953 hw->swapBuffers(getHwComposer());
954 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700955 }
956 }
957 }
958
959 postFramebuffer();
960
961 if (mDebugRegion > 1) {
962 usleep(mDebugRegion * 1000);
963 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700964
965 HWComposer& hwc(getHwComposer());
966 if (hwc.initCheck() == NO_ERROR) {
967 status_t err = hwc.prepare();
968 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
969 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700970}
971
972void SurfaceFlinger::preComposition()
973{
974 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700975 const LayerVector& layers(mDrawingState.layersSortedByZ);
976 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700977 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700978 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700979 needExtraInvalidate = true;
980 }
981 }
982 if (needExtraInvalidate) {
983 signalLayerUpdate();
984 }
985}
986
987void SurfaceFlinger::postComposition()
988{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700989 const LayerVector& layers(mDrawingState.layersSortedByZ);
990 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700991 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700993 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800994
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700995 const HWComposer& hwc = getHwComposer();
996 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
997
998 if (presentFence->isValid()) {
999 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1000 enableHardwareVsync();
1001 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001002 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001003 }
1004 }
1005
Andy McFadden5167ec62014-05-22 13:08:43 -07001006 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001007 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001008 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001009 enableHardwareVsync();
1010 }
1011 }
1012
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001013 if (mAnimCompositionPending) {
1014 mAnimCompositionPending = false;
1015
Jesse Halla9a1b002013-02-27 16:39:25 -08001016 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001017 mAnimFrameTracker.setActualPresentFence(presentFence);
1018 } else {
1019 // The HWC doesn't support present fences, so use the refresh
1020 // timestamp instead.
1021 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1022 mAnimFrameTracker.setActualPresentTime(presentTime);
1023 }
1024 mAnimFrameTracker.advanceFrame();
1025 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001026}
1027
1028void SurfaceFlinger::rebuildLayerStacks() {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001029#ifdef QCOM_BSP
1030 char prop[PROPERTY_VALUE_MAX];
1031 property_get("sys.extended_mode", prop, "0");
1032 sExtendedMode = atoi(prop) ? true : false;
1033#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -07001034 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001035 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001036 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001037 mVisibleRegionsDirty = false;
1038 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001039
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001040 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001041 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001042 Region opaqueRegion;
1043 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001045 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001046 const Transform& tr(hw->getTransform());
1047 const Rect bounds(hw->getBounds());
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001048 int dpyId = hw->getHwcDisplayId();
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001049 if (hw->isDisplayOn()) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001050 SurfaceFlinger::computeVisibleRegions(dpyId, layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001051 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001052
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001053 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001054 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001055 const sp<Layer>& layer(layers[i]);
1056 const Layer::State& s(layer->getDrawingState());
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001057 Region drawRegion(tr.transform(
1058 layer->visibleNonTransparentRegion));
1059 drawRegion.andSelf(bounds);
1060 if (!drawRegion.isEmpty()) {
1061 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -07001062 }
1063 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001064 }
Mathias Agopian42977342012-08-05 00:40:46 -07001065 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001066 hw->undefinedRegion.set(bounds);
1067 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1068 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001069 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001070 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001071}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001072
Mathias Agopiancd60f992012-08-16 16:28:27 -07001073void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001074 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001075 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1076 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1077 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1078
1079 // If nothing has changed (!dirty), don't recompose.
1080 // If something changed, but we don't currently have any visible layers,
1081 // and didn't when we last did a composition, then skip it this time.
1082 // The second rule does two things:
1083 // - When all layers are removed from a display, we'll emit one black
1084 // frame, then nothing more until we get new layers.
1085 // - When a display is created with a private layer stack, we won't
1086 // emit any black frames until a layer is added to the layer stack.
1087 bool mustRecompose = dirty && !(empty && wasEmpty);
1088
1089 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1090 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1091 mustRecompose ? "doing" : "skipping",
1092 dirty ? "+" : "-",
1093 empty ? "+" : "-",
1094 wasEmpty ? "+" : "-");
1095
Dan Stoza71433162014-02-04 16:22:36 -08001096 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001097
1098 if (mustRecompose) {
1099 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1100 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001101 }
1102
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001103 HWComposer& hwc(getHwComposer());
1104 if (hwc.initCheck() == NO_ERROR) {
1105 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001106 if (CC_UNLIKELY(mHwWorkListDirty)) {
1107 mHwWorkListDirty = false;
1108 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1109 sp<const DisplayDevice> hw(mDisplays[dpy]);
1110 const int32_t id = hw->getHwcDisplayId();
1111 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001113 hw->getVisibleLayersSortedByZ());
1114 const size_t count = currentLayers.size();
1115 if (hwc.createWorkList(id, count) == NO_ERROR) {
1116 HWComposer::LayerListIterator cur = hwc.begin(id);
1117 const HWComposer::LayerListIterator end = hwc.end(id);
1118 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001119 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001120 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001121 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001122 cur->setSkip(true);
1123 }
1124 }
1125 }
1126 }
1127 }
1128 }
1129
1130 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001131 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001132 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001133 const int32_t id = hw->getHwcDisplayId();
1134 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001135 // Get the layers in the current drawying state
1136 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001137#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001138 bool freezeSurfacePresent = false;
1139 const size_t layerCount = layers.size();
1140 char value[PROPERTY_VALUE_MAX];
1141 property_get("sys.disable_ext_animation", value, "0");
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001142 if(atoi(value) && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001143 for (size_t i = 0 ; i < layerCount ; ++i) {
1144 static int screenShotLen = strlen("ScreenshotSurface");
1145 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001146 const Layer::State& s(layer->getDrawingState());
1147 // check the layers associated with external display
1148 if(s.layerStack == hw->getLayerStack()) {
1149 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001150 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001151 // Screenshot layer is present, and animation in
1152 // progress
1153 freezeSurfacePresent = true;
1154 break;
1155 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001156 }
1157 }
1158 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001159#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001160 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001161 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001162 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001163 HWComposer::LayerListIterator cur = hwc.begin(id);
1164 const HWComposer::LayerListIterator end = hwc.end(id);
1165 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1166 /*
1167 * update the per-frame h/w composer data for each layer
1168 * and build the transparent region of the FB
1169 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001170 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001171 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001172#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001173 if(freezeSurfacePresent) {
1174 // if freezeSurfacePresent, set ANIMATING flag
1175 cur->setAnimating(true);
1176 } else {
1177 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1178 draw(mDrawingState.displays);
1179 size_t dc = draw.size();
1180 for (size_t i=0 ; i<dc ; i++) {
1181 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001182 // Pass the current orientation to HWC
1183 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1184 SurfaceFlinger::EVENT_ORIENTATION,
1185 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001186 }
1187 }
1188 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001189#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001190 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001191 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001192 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001193
Riley Andrews03414a12014-07-01 14:22:59 -07001194 // If possible, attempt to use the cursor overlay on each display.
1195 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1196 sp<const DisplayDevice> hw(mDisplays[dpy]);
1197 const int32_t id = hw->getHwcDisplayId();
1198 if (id >= 0) {
1199 const Vector< sp<Layer> >& currentLayers(
1200 hw->getVisibleLayersSortedByZ());
1201 const size_t count = currentLayers.size();
1202 HWComposer::LayerListIterator cur = hwc.begin(id);
1203 const HWComposer::LayerListIterator end = hwc.end(id);
1204 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1205 const sp<Layer>& layer(currentLayers[i]);
1206 if (layer->isPotentialCursor()) {
1207 cur->setIsCursorLayerHint();
1208 break;
1209 }
1210 }
1211 }
1212 }
1213
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001214 status_t err = hwc.prepare();
1215 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001216
1217 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1218 sp<const DisplayDevice> hw(mDisplays[dpy]);
1219 hw->prepareFrame(hwc);
1220 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001221 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001222}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001223
Mathias Agopiancd60f992012-08-16 16:28:27 -07001224void SurfaceFlinger::doComposition() {
1225 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001226 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001227 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001228 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001229 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001230 // transform the dirty region into this screen's coordinate space
1231 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001232
1233 // repaint the framebuffer (if needed)
1234 doDisplayComposition(hw, dirtyRegion);
1235
Mathias Agopiancd60f992012-08-16 16:28:27 -07001236 hw->dirtyRegion.clear();
1237 hw->flip(hw->swapRegion);
1238 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001239 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001240 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001241 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001242 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001243 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001244}
1245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246void SurfaceFlinger::postFramebuffer()
1247{
Mathias Agopian841cde52012-03-01 15:44:37 -08001248 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001249
Mathias Agopiana44b0412011-10-16 18:46:35 -07001250 const nsecs_t now = systemTime();
1251 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001252
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001253 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001254 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001255 if (!hwc.supportsFramebufferTarget()) {
1256 // EGL spec says:
1257 // "surface must be bound to the calling thread's current context,
1258 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001259 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001260 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001261 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001262 }
1263
Mathias Agopian6da15f42013-09-25 20:40:07 -07001264 // make the default display current because the VirtualDisplayDevice code cannot
1265 // deal with dequeueBuffer() being called outside of the composition loop; however
1266 // the code below can call glFlush() which is allowed (and does in some case) call
1267 // dequeueBuffer().
1268 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1269
Mathias Agopian92a979a2012-08-02 18:32:23 -07001270 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001271 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001273 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001274 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001275 int32_t id = hw->getHwcDisplayId();
1276 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001277 HWComposer::LayerListIterator cur = hwc.begin(id);
1278 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001279 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001280 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001281 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001282 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001283 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001284 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001285 }
Jesse Hallef194142012-06-14 14:45:17 -07001286 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001287 }
1288
Mathias Agopiana44b0412011-10-16 18:46:35 -07001289 mLastSwapBufferTime = systemTime() - now;
1290 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001291
1292 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1293 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1294 logFrameStats();
1295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296}
1297
Mathias Agopian87baae12012-07-31 12:38:26 -07001298void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299{
Mathias Agopian841cde52012-03-01 15:44:37 -08001300 ATRACE_CALL();
1301
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001302 // here we keep a copy of the drawing state (that is the state that's
1303 // going to be overwritten by handleTransactionLocked()) outside of
1304 // mStateLock so that the side-effects of the State assignment
1305 // don't happen with mStateLock held (which can cause deadlocks).
1306 State drawingState(mDrawingState);
1307
Mathias Agopianca4d3602011-05-19 15:38:14 -07001308 Mutex::Autolock _l(mStateLock);
1309 const nsecs_t now = systemTime();
1310 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311
Mathias Agopianca4d3602011-05-19 15:38:14 -07001312 // Here we're guaranteed that some transaction flags are set
1313 // so we can call handleTransactionLocked() unconditionally.
1314 // We call getTransactionFlags(), which will also clear the flags,
1315 // with mStateLock held to guarantee that mCurrentState won't change
1316 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001317
Mathias Agopiane57f2922012-08-09 16:29:12 -07001318 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001319 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001320
Mathias Agopianca4d3602011-05-19 15:38:14 -07001321 mLastTransactionTime = systemTime() - now;
1322 mDebugInTransaction = 0;
1323 invalidateHwcGeometry();
1324 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001325}
1326
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301327void SurfaceFlinger::setVirtualDisplayData(
1328 int32_t hwcDisplayId,
1329 const sp<IGraphicBufferProducer>& sink)
1330{
1331 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1332 ANativeWindow* const window = mNativeWindow.get();
1333
1334 int format;
1335 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1336
1337 EGLSurface surface;
1338 EGLint w, h;
1339 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1340 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1341 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1342 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1343
1344 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1345}
1346
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001347void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1348 sp<DisplaySurface> &dispSurface,
1349 sp<IGraphicBufferProducer> &producer,
1350 const DisplayDeviceState state,
1351 sp<IGraphicBufferProducer> bqProducer,
1352 sp<IGraphicBufferConsumer> bqConsumer)
1353{
1354 bool vdsEnabled = mHwc->isVDSEnabled();
1355
1356 //for V4L2 based virtual display implementation
1357 if(!vdsEnabled) {
1358 // persist.sys.wfd.virtual will be set if WFD is launched via
1359 // settings app. This is currently being done in
1360 // ExtendedRemoteDisplay-WFD stack.
1361 // This flag will be reset at the time of disconnection of virtual WFD
1362 // display.
1363 // This flag is set to zero if WFD is launched via QCOM WFD
1364 // proprietary APIs which use HDMI piggyback approach.
1365 char value[PROPERTY_VALUE_MAX];
1366 property_get("persist.sys.wfd.virtual", value, "0");
1367 int wfdVirtual = atoi(value);
1368 if(!wfdVirtual) {
1369 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1370 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001371 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1372 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001373 dispSurface = vds;
1374 // There won't be any interaction with HWC for this virtual display.
1375 // so the GLES driver can pass buffers directly to the sink.
1376 producer = state.surface;
1377 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301378 int sinkUsage = -1;
1379 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1380#ifdef QCOM_BSP
1381 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1382#endif
1383 hwcDisplayId = allocateHwcDisplayId(state.type);
1384
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001385 if (hwcDisplayId >= 0) {
1386 // This is for WFD virtual display scenario.
1387 // Read virtual display properties and create a
1388 // rendering surface for it inorder to be handled by hwc.
1389 setVirtualDisplayData(hwcDisplayId, state.surface);
1390 dispSurface = new FramebufferSurface(*mHwc, state.type,
1391 bqConsumer);
1392 producer = bqProducer;
1393 } else {
1394 // in case of WFD Virtual + SSD/SR concurrency scenario,
1395 // WFD virtual display instance gets valid hwcDisplayId and
1396 // SSD/SR will get invalid hwcDisplayId
1397 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001398 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1399 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001400 dispSurface = vds;
1401 // There won't be any interaction with HWC for this virtual
1402 // display, so the GLES driver can pass buffers directly to the
1403 // sink.
1404 producer = state.surface;
1405 }
1406 }
1407 } else {
1408 // VDS solution is enabled
1409 // HWC is allocated for first virtual display.
1410 // Subsequent virtual display sessions will be composed by GLES driver.
1411 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1412 // mForceHwcCopy (which is based on Usage Flags)
1413
1414 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001415 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1416 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001417 dispSurface = vds;
1418 if (hwcDisplayId >= 0) {
1419 producer = vds;
1420 } else {
1421 // There won't be any interaction with HWC for this virtual display,
1422 // so the GLES driver can pass buffers directly to the sink.
1423 producer = state.surface;
1424 }
1425 }
1426}
1427
Mathias Agopian87baae12012-07-31 12:38:26 -07001428void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001429{
1430 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 const size_t count = currentLayers.size();
1432
1433 /*
1434 * Traversal of the children
1435 * (perform the transaction for each of them if needed)
1436 */
1437
Mathias Agopian3559b072012-08-15 13:46:03 -07001438 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001440 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1442 if (!trFlags) continue;
1443
1444 const uint32_t flags = layer->doTransaction(0);
1445 if (flags & Layer::eVisibleRegion)
1446 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 }
1448 }
1449
1450 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001451 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 */
1453
Mathias Agopiane57f2922012-08-09 16:29:12 -07001454 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001455 // here we take advantage of Vector's copy-on-write semantics to
1456 // improve performance by skipping the transaction entirely when
1457 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001458 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1459 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001460 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001462 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001463 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001464
1465 // find the displays that were removed
1466 // (ie: in drawing state but not in current state)
1467 // also handle displays that changed
1468 // (ie: displays that are in both lists)
1469 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001470 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1471 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001472 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001473 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001474 // Call makeCurrent() on the primary display so we can
1475 // be sure that nothing associated with this display
1476 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001477 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001478 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001479 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1480 if (hw != NULL)
1481 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001482 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001483 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001484 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001485 } else {
1486 ALOGW("trying to remove the main display");
1487 }
1488 } else {
1489 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001490 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001491 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001492 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001493 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001494 // recreating the DisplayDevice, so we just remove it
1495 // from the drawing state, so that it get re-added
1496 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001497 sp<DisplayDevice> hw(getDisplayDevice(display));
1498 if (hw != NULL)
1499 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001500 mDisplays.removeItem(display);
1501 mDrawingState.displays.removeItemsAt(i);
1502 dc--; i--;
1503 // at this point we must loop to the next item
1504 continue;
1505 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001506
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001507 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001508 if (disp != NULL) {
1509 if (state.layerStack != draw[i].layerStack) {
1510 disp->setLayerStack(state.layerStack);
1511 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001512 if ((state.orientation != draw[i].orientation)
1513 || (state.viewport != draw[i].viewport)
1514 || (state.frame != draw[i].frame))
1515 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001516#ifdef QCOM_BSP
1517 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001518 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301519 // animation completes and make sure boot
1520 // animation is shown in panel orientation always.
1521 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001522 disp->setProjection(state.orientation,
1523 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001524 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001525 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301526 else{
1527 char property[PROPERTY_VALUE_MAX];
1528 int panelOrientation =
1529 DisplayState::eOrientationDefault;
1530 if(property_get("persist.panel.orientation",
1531 property, "0") > 0){
1532 panelOrientation = atoi(property) / 90;
1533 }
1534 disp->setProjection(panelOrientation,
1535 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001536 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301537 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001538 // Set the view frame of each display only of its
1539 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001540 if(orient == DisplayState::eOrientationDefault and
1541 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001542 qdutils::setViewFrame(disp->getHwcDisplayId(),
1543 state.frame.left, state.frame.top,
1544 state.frame.right, state.frame.bottom);
1545 }
1546#else
1547 disp->setProjection(state.orientation,
1548 state.viewport, state.frame);
1549#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001550 }
Michael Lentine47e45402014-07-18 15:34:25 -07001551 if (state.width != draw[i].width || state.height != draw[i].height) {
1552 disp->setDisplaySize(state.width, state.height);
1553 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001554 }
1555 }
1556 }
1557
1558 // find displays that were added
1559 // (ie: in current state but not in drawing state)
1560 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001561 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001562 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001563
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001564 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001565 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001566 sp<IGraphicBufferProducer> bqProducer;
1567 sp<IGraphicBufferConsumer> bqConsumer;
1568 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1569 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001570
Jesse Hall02d86562013-03-25 14:43:23 -07001571 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001572 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001573 // Virtual displays without a surface are dormant:
1574 // they have external state (layer stack, projection,
1575 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001576 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001577 configureVirtualDisplay(hwcDisplayId,
1578 dispSurface, producer, state, bqProducer,
1579 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001580 }
1581 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001582 ALOGE_IF(state.surface!=NULL,
1583 "adding a supported display, but rendering "
1584 "surface is provided (%p), ignoring it",
1585 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001586 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001587 // for supported (by hwc) displays we provide our
1588 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001589 dispSurface = new FramebufferSurface(*mHwc, state.type,
1590 bqConsumer);
1591 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001592 }
1593
1594 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301595 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001596 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001597 state.type, hwcDisplayId,
1598 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001599 display, dispSurface, producer,
1600 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001601 hw->setLayerStack(state.layerStack);
1602 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001603 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001604 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001605 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001606 if (state.isVirtualDisplay()) {
1607 if (hwcDisplayId >= 0) {
1608 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1609 hw->getWidth(), hw->getHeight(),
1610 hw->getFormat());
1611 }
1612 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001613 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001614 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001615 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001616 }
1617 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001619 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620
Mathias Agopian84300952012-11-21 16:02:13 -08001621 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1622 // The transform hint might have changed for some layers
1623 // (either because a display has changed, or because a layer
1624 // as changed).
1625 //
1626 // Walk through all the layers in currentLayers,
1627 // and update their transform hint.
1628 //
1629 // If a layer is visible only on a single display, then that
1630 // display is used to calculate the hint, otherwise we use the
1631 // default display.
1632 //
1633 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1634 // the hint is set before we acquire a buffer from the surface texture.
1635 //
1636 // NOTE: layer transactions have taken place already, so we use their
1637 // drawing state. However, SurfaceFlinger's own transaction has not
1638 // happened yet, so we must use the current state layer list
1639 // (soon to become the drawing state list).
1640 //
1641 sp<const DisplayDevice> disp;
1642 uint32_t currentlayerStack = 0;
1643 for (size_t i=0; i<count; i++) {
1644 // NOTE: we rely on the fact that layers are sorted by
1645 // layerStack first (so we don't have to traverse the list
1646 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001647 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001648 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001649 if (i==0 || currentlayerStack != layerStack) {
1650 currentlayerStack = layerStack;
1651 // figure out if this layerstack is mirrored
1652 // (more than one display) if so, pick the default display,
1653 // if not, pick the only display it's on.
1654 disp.clear();
1655 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1656 sp<const DisplayDevice> hw(mDisplays[dpy]);
1657 if (hw->getLayerStack() == currentlayerStack) {
1658 if (disp == NULL) {
1659 disp = hw;
1660 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001661 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001662 break;
1663 }
1664 }
1665 }
1666 }
Chet Haase91d25932013-04-11 15:24:55 -07001667 if (disp == NULL) {
1668 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1669 // redraw after transform hint changes. See bug 8508397.
1670
1671 // could be null when this layer is using a layerStack
1672 // that is not visible on any display. Also can occur at
1673 // screen off/on times.
1674 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001675 }
Chet Haase91d25932013-04-11 15:24:55 -07001676 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001677 }
1678 }
1679
1680
Mathias Agopian3559b072012-08-15 13:46:03 -07001681 /*
1682 * Perform our own transaction if needed
1683 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001684
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001685 const LayerVector& layers(mDrawingState.layersSortedByZ);
1686 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001687 // layers have been added
1688 mVisibleRegionsDirty = true;
1689 }
1690
1691 // some layers might have been removed, so
1692 // we need to update the regions they're exposing.
1693 if (mLayersRemoved) {
1694 mLayersRemoved = false;
1695 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001696 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001697 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001698 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001699 if (currentLayers.indexOf(layer) < 0) {
1700 // this layer is not visible anymore
1701 // TODO: we could traverse the tree from front to back and
1702 // compute the actual visible region
1703 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001704 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001705 Region visibleReg = s.transform.transform(
1706 Region(Rect(s.active.w, s.active.h)));
1707 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001708 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 }
1711
1712 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001713
1714 updateCursorAsync();
1715}
1716
1717void SurfaceFlinger::updateCursorAsync()
1718{
1719 HWComposer& hwc(getHwComposer());
1720 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1721 sp<const DisplayDevice> hw(mDisplays[dpy]);
1722 const int32_t id = hw->getHwcDisplayId();
1723 if (id < 0) {
1724 continue;
1725 }
1726 const Vector< sp<Layer> >& currentLayers(
1727 hw->getVisibleLayersSortedByZ());
1728 const size_t count = currentLayers.size();
1729 HWComposer::LayerListIterator cur = hwc.begin(id);
1730 const HWComposer::LayerListIterator end = hwc.end(id);
1731 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1732 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1733 continue;
1734 }
1735 const sp<Layer>& layer(currentLayers[i]);
1736 Rect cursorPos = layer->getPosition(hw);
1737 hwc.setCursorPositionAsync(id, cursorPos);
1738 break;
1739 }
1740 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001741}
1742
1743void SurfaceFlinger::commitTransaction()
1744{
1745 if (!mLayersPendingRemoval.isEmpty()) {
1746 // Notify removed layers now that they can't be drawn from
1747 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1748 mLayersPendingRemoval[i]->onRemoved();
1749 }
1750 mLayersPendingRemoval.clear();
1751 }
1752
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001753 // If this transaction is part of a window animation then the next frame
1754 // we composite should be considered an animation as well.
1755 mAnimCompositionPending = mAnimTransactionPending;
1756
Mathias Agopian4fec8732012-06-29 14:12:52 -07001757 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001758 mTransactionPending = false;
1759 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001760 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761}
1762
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001763void SurfaceFlinger::computeVisibleRegions(size_t dpy,
Mathias Agopian87baae12012-07-31 12:38:26 -07001764 const LayerVector& currentLayers, uint32_t layerStack,
1765 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766{
Mathias Agopian841cde52012-03-01 15:44:37 -08001767 ATRACE_CALL();
1768
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 Region aboveOpaqueLayers;
1770 Region aboveCoveredLayers;
1771 Region dirty;
1772
Mathias Agopian87baae12012-07-31 12:38:26 -07001773 outDirtyRegion.clear();
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001774 bool bIgnoreLayers = false;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001775 int indexLOI = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776 size_t i = currentLayers.size();
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001777#ifdef QCOM_BSP
1778 while (i--) {
1779 const sp<Layer>& layer = currentLayers[i];
1780 // iterate through the layer list to find ext_only layers and store
1781 // the index
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001782 if (layer->isSecureDisplay()) {
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001783 bIgnoreLayers = true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001784 indexLOI = -1;
1785 if(!dpy)
1786 indexLOI = i;
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001787 break;
1788 }
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001789 // iterate through the layer list to find ext_only layers or yuv
1790 // layer(extended_mode) and store the index
1791 if ((dpy && (layer->isExtOnly() ||
1792 (isExtendedMode() && layer->isYuvLayer())))) {
1793 bIgnoreLayers= true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001794 indexLOI = i;
1795 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001796 }
1797 i = currentLayers.size();
1798#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001800 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801
1802 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001803 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001804
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001805#ifdef QCOM_BSP
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001806 // Only add the layer marked as "external_only" or yuvLayer
1807 // (extended_mode) to external list and
1808 // only remove the layer marked as "external_only" or yuvLayer in
1809 // extended_mode from primary list
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001810 // and do not add the layer marked as "internal_only" to external list
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001811 // Add secure UI layers to primary and remove other layers from internal
1812 //and external list
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001813 if(((bIgnoreLayers && indexLOI != (int)i) ||
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001814 (!dpy && layer->isExtOnly()) ||
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001815 (!dpy && isExtendedMode() && layer->isYuvLayer()))||
1816 (dpy && layer->isIntOnly())) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001817 // Ignore all other layers except the layers marked as ext_only
1818 // by setting visible non transparent region empty.
1819 Region visibleNonTransRegion;
1820 visibleNonTransRegion.set(Rect(0,0));
1821 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1822 continue;
1823 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001824#endif
1825 // only consider the layers on the given later stack
1826 // Override layers created using presentation class by the layers having
1827 // ext_only flag enabled
1828 if(s.layerStack != layerStack && !bIgnoreLayers) {
1829 // set the visible region as empty since we have removed the
1830 // layerstack check in rebuildLayerStack() function.
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001831 Region visibleNonTransRegion;
1832 visibleNonTransRegion.set(Rect(0,0));
1833 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1834 continue;
1835 }
Mathias Agopianab028732010-03-16 16:41:46 -07001836 /*
1837 * opaqueRegion: area of a surface that is fully opaque.
1838 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001840
1841 /*
1842 * visibleRegion: area of a surface that is visible on screen
1843 * and not fully transparent. This is essentially the layer's
1844 * footprint minus the opaque regions above it.
1845 * Areas covered by a translucent surface are considered visible.
1846 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001847 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001848
1849 /*
1850 * coveredRegion: area of a surface that is covered by all
1851 * visible regions above it (which includes the translucent areas).
1852 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001854
Jesse Halla8026d22012-09-25 13:25:04 -07001855 /*
1856 * transparentRegion: area of a surface that is hinted to be completely
1857 * transparent. This is only used to tell when the layer has no visible
1858 * non-transparent regions and can be removed from the layer list. It
1859 * does not affect the visibleRegion of this layer or any layers
1860 * beneath it. The hint may not be correct if apps don't respect the
1861 * SurfaceView restrictions (which, sadly, some don't).
1862 */
1863 Region transparentRegion;
1864
Mathias Agopianab028732010-03-16 16:41:46 -07001865
1866 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001867 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001868 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001869 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001871 if (!visibleRegion.isEmpty()) {
1872 // Remove the transparent area from the visible region
1873 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001874 const Transform tr(s.transform);
1875 if (tr.transformed()) {
1876 if (tr.preserveRects()) {
1877 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001878 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001879 } else {
1880 // transformation too complex, can't do the
1881 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001882 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001883 }
1884 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001885 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001886 }
Mathias Agopianab028732010-03-16 16:41:46 -07001887 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888
Mathias Agopianab028732010-03-16 16:41:46 -07001889 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001890 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001891 if (s.alpha==255 && !translucent &&
1892 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1893 // the opaque region is the layer's footprint
1894 opaqueRegion = visibleRegion;
1895 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896 }
1897 }
1898
Mathias Agopianab028732010-03-16 16:41:46 -07001899 // Clip the covered region to the visible region
1900 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1901
1902 // Update aboveCoveredLayers for next (lower) layer
1903 aboveCoveredLayers.orSelf(visibleRegion);
1904
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 // subtract the opaque region covered by the layers above us
1906 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907
1908 // compute this layer's dirty region
1909 if (layer->contentDirty) {
1910 // we need to invalidate the whole region
1911 dirty = visibleRegion;
1912 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001913 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 layer->contentDirty = false;
1915 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001916 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001917 * the exposed region consists of two components:
1918 * 1) what's VISIBLE now and was COVERED before
1919 * 2) what's EXPOSED now less what was EXPOSED before
1920 *
1921 * note that (1) is conservative, we start with the whole
1922 * visible region but only keep what used to be covered by
1923 * something -- which mean it may have been exposed.
1924 *
1925 * (2) handles areas that were not covered by anything but got
1926 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001927 */
Mathias Agopianab028732010-03-16 16:41:46 -07001928 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001929 const Region oldVisibleRegion = layer->visibleRegion;
1930 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001931 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1932 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933 }
1934 dirty.subtractSelf(aboveOpaqueLayers);
1935
1936 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001937 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938
Mathias Agopianab028732010-03-16 16:41:46 -07001939 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001941
Jesse Halla8026d22012-09-25 13:25:04 -07001942 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 layer->setVisibleRegion(visibleRegion);
1944 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001945 layer->setVisibleNonTransparentRegion(
1946 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001947 }
1948
Mathias Agopian87baae12012-07-31 12:38:26 -07001949 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950}
1951
Mathias Agopian87baae12012-07-31 12:38:26 -07001952void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1953 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001954 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001955 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1956 if (hw->getLayerStack() == layerStack) {
1957 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001958 }
1959 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001960}
1961
1962void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001964 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965
Mathias Agopian4fec8732012-06-29 14:12:52 -07001966 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001967 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001968
1969 // Store the set of layers that need updates. This set must not change as
1970 // buffers are being latched, as this could result in a deadlock.
1971 // Example: Two producers share the same command stream and:
1972 // 1.) Layer 0 is latched
1973 // 2.) Layer 0 gets a new frame
1974 // 2.) Layer 1 gets a new frame
1975 // 3.) Layer 1 is latched.
1976 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1977 // second frame. But layer 0's second frame could be waiting on display.
1978 Vector<Layer*> layersWithQueuedFrames;
1979 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001980 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001981 if (layer->hasQueuedFrame())
1982 layersWithQueuedFrames.push_back(layer.get());
1983 }
1984 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1985 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001986 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001987 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001988 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001989 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001990
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001991 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992}
1993
Mathias Agopianad456f92011-01-13 17:53:01 -08001994void SurfaceFlinger::invalidateHwcGeometry()
1995{
1996 mHwWorkListDirty = true;
1997}
1998
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001999
Mathias Agopiancd60f992012-08-16 16:28:27 -07002000void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07002001 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002002{
Dan Stoza71433162014-02-04 16:22:36 -08002003 // We only need to actually compose the display if:
2004 // 1) It is being handled by hardware composer, which may need this to
2005 // keep its virtual display state machine in sync, or
2006 // 2) There is work to be done (the dirty region isn't empty)
2007 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
2008 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
2009 return;
2010 }
2011
Mathias Agopian87baae12012-07-31 12:38:26 -07002012 Region dirtyRegion(inDirtyRegion);
2013
Mathias Agopianb8a55602009-06-26 19:06:36 -07002014 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002015 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002016
Mathias Agopian42977342012-08-05 00:40:46 -07002017 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002018 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002019 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2020 // takes a rectangle, we must make sure to update that whole
2021 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002022 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002023 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002024 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002025 // We need to redraw the rectangle that will be updated
2026 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002027 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002028 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002029 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002030 } else {
2031 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002032 dirtyRegion.set(hw->bounds());
2033 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002034 }
2035 }
2036
Alan Viverette9c5a3332013-09-12 20:04:35 -07002037 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07002038 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07002039 } else {
2040 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07002041 mat4 colorMatrix = mColorMatrix;
2042 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07002043 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002044 }
2045 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07002046 doComposeSurfaces(hw, dirtyRegion);
2047 engine.endGroup();
2048 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002049
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002050 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002051 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002052
2053 // swap buffers (presentation)
2054 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002055}
2056
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302057#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302058bool SurfaceFlinger::computeTiledDr(const sp<const DisplayDevice>& hw) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302059 int fbWidth= hw->getWidth();
2060 int fbHeight= hw->getHeight();
2061 Rect fullScreenRect = Rect(0,0,fbWidth, fbHeight);
2062 const int32_t id = hw->getHwcDisplayId();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302063 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302064 HWComposer& hwc(getHwComposer());
2065
2066 /* Compute and return the Union of Dirty Rects.
2067 * Return false if the unionDR is fullscreen, as there is no benefit from
2068 * preserving full screen.*/
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302069 return (hwc.canUseTiledDR(id, mUnionDirtyRect) &&
2070 (mUnionDirtyRect != fullScreenRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302071
2072}
2073#endif
2074
Michael Lentine3f121fc2014-10-01 11:17:28 -07002075bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002076{
Mathias Agopian3f844832013-08-07 21:24:32 -07002077 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002078 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07002079 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07002080 HWComposer::LayerListIterator cur = hwc.begin(id);
2081 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002082
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302083 Region clearRegion;
Jesse Halld05a17f2013-09-30 16:49:30 -07002084 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002085 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07002086 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002087 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
2088 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002089 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2090 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2091 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2092 }
2093 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002094 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002095
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002096 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07002097 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07002098 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002099 // when using overlays, we assume a fully transparent framebuffer
2100 // NOTE: we could reduce how much we need to clear, for instance
2101 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002102 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002103 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07002104 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002105 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002106 // we start with the whole screen area
2107 const Region bounds(hw->getBounds());
2108
2109 // we remove the scissor part
2110 // we're left with the letterbox region
2111 // (common case is that letterbox ends-up being empty)
2112 const Region letterbox(bounds.subtract(hw->getScissor()));
2113
2114 // compute the area to clear
2115 Region region(hw->undefinedRegion.merge(letterbox));
2116
2117 // but limit it to the dirty region
2118 region.andSelf(dirty);
2119
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302120
Mathias Agopianb9494d52012-04-18 02:28:45 -07002121 // screen is already cleared here
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302122#ifdef QCOM_BSP
2123 clearRegion.clear();
2124 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2125 clearRegion = region;
2126 if (cur == end) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05302127 drawWormhole(hw, region);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302128 } else if(mCanUseGpuTileRender) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302129 /* If GPUTileRect DR optimization on clear only the UnionDR
2130 * (computed by computeTiledDr) which is the actual region
2131 * that will be drawn on FB in this cycle.. */
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302132 clearRegion = clearRegion.andSelf(Region(mUnionDirtyRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302133 }
2134 } else
2135#endif
2136 {
2137 if (!region.isEmpty()) {
2138 if (cur != end) {
2139 if (cur->getCompositionType() != HWC_BLIT)
2140 // can happen with SurfaceView
2141 drawWormhole(hw, region);
2142 } else
2143 drawWormhole(hw, region);
2144 }
Mathias Agopianb9494d52012-04-18 02:28:45 -07002145 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002146 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002147
Mathias Agopian766dc492012-10-30 18:08:06 -07002148 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2149 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002150 // scissor on the main display. It should never be needed
2151 // anyways (though in theory it could since the API allows it).
2152 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002153 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002154 if (scissor != bounds) {
2155 // scissor doesn't match the screen's dimensions, so we
2156 // need to clear everything outside of it and enable
2157 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002158
Mathias Agopianf45c5102012-10-24 16:29:17 -07002159 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002160 const uint32_t height = hw->getHeight();
2161 engine.setScissor(scissor.left, height - scissor.bottom,
2162 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002163 }
2164 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002165 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002166
Mathias Agopian85d751c2012-08-29 16:59:24 -07002167 /*
2168 * and then, render the layers targeted at the framebuffer
2169 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002170
Mathias Agopian13127d82013-03-05 17:47:11 -08002171 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002172 const size_t count = layers.size();
2173 const Transform& tr = hw->getTransform();
2174 if (cur != end) {
2175 // we're using h/w composer
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302176#ifdef QCOM_BSP
2177 int fbWidth= hw->getWidth();
2178 int fbHeight= hw->getHeight();
2179 /* if GPUTileRender optimization property is on & can be used
2180 * i) Enable EGL_SWAP_PRESERVED flag
2181 * ii) do startTile with union DirtyRect
2182 * else , Disable EGL_SWAP_PRESERVED */
2183 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302184 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302185 hw->eglSwapPreserved(true);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302186 Rect dr = mUnionDirtyRect;
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302187 engine.startTileComposition(dr.left, (fbHeight-dr.bottom),
2188 (dr.right-dr.left),
2189 (dr.bottom-dr.top), 0);
2190 } else {
2191 // Un Set EGL_SWAP_PRESERVED flag, if no tiling required.
2192 hw->eglSwapPreserved(false);
2193 }
2194 // DrawWormHole/Any Draw has to be within startTile & EndTile
2195 if (cur->getCompositionType() != HWC_BLIT &&
2196 !clearRegion.isEmpty()){
2197 drawWormhole(hw, clearRegion);
2198 }
2199 }
2200#endif
2201
Mathias Agopian85d751c2012-08-29 16:59:24 -07002202 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002203 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002204 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002205 if (!clip.isEmpty()) {
2206 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002207 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002208 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002209 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002210 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2211 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002212 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002213 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002214 // never clear the very first layer since we're
2215 // guaranteed the FB is already cleared
2216 layer->clearWithOpenGL(hw, clip);
2217 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002218 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002219 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002220 case HWC_FRAMEBUFFER: {
2221 layer->draw(hw, clip);
2222 break;
2223 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002224 case HWC_BLIT:
2225 //Do nothing
2226 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002227 case HWC_FRAMEBUFFER_TARGET: {
2228 // this should not happen as the iterator shouldn't
2229 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002230 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002231 break;
2232 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002233 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002234 }
2235 layer->setAcquireFence(hw, *cur);
2236 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302237
2238#ifdef QCOM_BSP
2239 // call EndTile, if starTile has been called in this cycle.
2240 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302241 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302242 engine.endTileComposition(GL_PRESERVE);
2243 }
2244 }
2245#endif
Mathias Agopian85d751c2012-08-29 16:59:24 -07002246 } else {
2247 // we're not using h/w composer
2248 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002249 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002250 const Region clip(dirty.intersect(
2251 tr.transform(layer->visibleRegion)));
2252 if (!clip.isEmpty()) {
2253 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002254 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002255 }
2256 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002257
2258 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002259 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002260 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002261}
2262
Mathias Agopian3f844832013-08-07 21:24:32 -07002263void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002264 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002265 RenderEngine& engine(getRenderEngine());
2266 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002267}
2268
Mathias Agopianac9fa422013-02-11 16:40:36 -08002269void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2270 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002271 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002272 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002273{
Mathias Agopian96f08192010-06-02 23:28:45 -07002274 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002275 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002276
Mathias Agopian96f08192010-06-02 23:28:45 -07002277 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002278 Mutex::Autolock _l(mStateLock);
2279 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002280 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002281}
2282
Mathias Agopian3f844832013-08-07 21:24:32 -07002283status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002284 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002285 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002287 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002288 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002289 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002290 return NO_ERROR;
2291 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002292 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002293}
2294
Dan Stozac7014012014-02-14 15:03:43 -08002295uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002296 return android_atomic_release_load(&mTransactionFlags);
2297}
2298
Mathias Agopian3f844832013-08-07 21:24:32 -07002299uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002300 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2301}
2302
Mathias Agopian3f844832013-08-07 21:24:32 -07002303uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002304 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2305 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002306 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002307 }
2308 return old;
2309}
2310
Mathias Agopian8b33f032012-07-24 20:43:54 -07002311void SurfaceFlinger::setTransactionState(
2312 const Vector<ComposerState>& state,
2313 const Vector<DisplayState>& displays,
2314 uint32_t flags)
2315{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002316 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002317 size_t count = displays.size();
2318#ifdef QCOM_BSP
2319 for (size_t i=0 ; i<count ; i++) {
2320 const DisplayState& s(displays[i]);
2321 if(s.token != mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]) {
2322 const uint32_t what = s.what;
2323 // Invalidate and Delay the binder thread by 50 ms on
2324 // eDisplayProjectionChanged to trigger a draw cycle so that
2325 // it can fix one incorrect frame on the External, when we disable
2326 // external animation
2327 if (what & DisplayState::eDisplayProjectionChanged) {
2328 invalidateHwcGeometry();
2329 repaintEverything();
2330 usleep(50000);
2331 }
2332 }
2333 }
2334#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002335 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002336 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002337
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002338 if (flags & eAnimation) {
2339 // For window updates that are part of an animation we must wait for
2340 // previous animation "frames" to be handled.
2341 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002342 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002343 if (CC_UNLIKELY(err != NO_ERROR)) {
2344 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002345 // caller after a few seconds.
2346 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2347 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002348 mAnimTransactionPending = false;
2349 break;
2350 }
2351 }
2352 }
2353
Mathias Agopiane57f2922012-08-09 16:29:12 -07002354 for (size_t i=0 ; i<count ; i++) {
2355 const DisplayState& s(displays[i]);
2356 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002357 }
2358
Mathias Agopiane57f2922012-08-09 16:29:12 -07002359 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002360 for (size_t i=0 ; i<count ; i++) {
2361 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002362 // Here we need to check that the interface we're given is indeed
2363 // one of our own. A malicious client could give us a NULL
2364 // IInterface, or one of its own or even one of our own but a
2365 // different type. All these situations would cause us to crash.
2366 //
2367 // NOTE: it would be better to use RTTI as we could directly check
2368 // that we have a Client*. however, RTTI is disabled in Android.
2369 if (s.client != NULL) {
2370 sp<IBinder> binder = s.client->asBinder();
2371 if (binder != NULL) {
2372 String16 desc(binder->getInterfaceDescriptor());
2373 if (desc == ISurfaceComposerClient::descriptor) {
2374 sp<Client> client( static_cast<Client *>(s.client.get()) );
2375 transactionFlags |= setClientStateLocked(client, s.state);
2376 }
2377 }
2378 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002379 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002380
Mathias Agopian386aa982011-11-07 21:58:03 -08002381 if (transactionFlags) {
2382 // this triggers the transaction
2383 setTransactionFlags(transactionFlags);
2384
2385 // if this is a synchronous transaction, wait for it to take effect
2386 // before returning.
2387 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002388 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002389 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002390 if (flags & eAnimation) {
2391 mAnimTransactionPending = true;
2392 }
2393 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002394 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2395 if (CC_UNLIKELY(err != NO_ERROR)) {
2396 // just in case something goes wrong in SF, return to the
2397 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002398 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2399 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002400 break;
2401 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002402 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002403 }
2404}
2405
Mathias Agopiane57f2922012-08-09 16:29:12 -07002406uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2407{
Jesse Hall9a143922012-10-04 16:29:19 -07002408 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2409 if (dpyIdx < 0)
2410 return 0;
2411
Mathias Agopiane57f2922012-08-09 16:29:12 -07002412 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002413 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002414 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002415 const uint32_t what = s.what;
2416 if (what & DisplayState::eSurfaceChanged) {
2417 if (disp.surface->asBinder() != s.surface->asBinder()) {
2418 disp.surface = s.surface;
2419 flags |= eDisplayTransactionNeeded;
2420 }
2421 }
2422 if (what & DisplayState::eLayerStackChanged) {
2423 if (disp.layerStack != s.layerStack) {
2424 disp.layerStack = s.layerStack;
2425 flags |= eDisplayTransactionNeeded;
2426 }
2427 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002428 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002429 if (disp.orientation != s.orientation) {
2430 disp.orientation = s.orientation;
2431 flags |= eDisplayTransactionNeeded;
2432 }
2433 if (disp.frame != s.frame) {
2434 disp.frame = s.frame;
2435 flags |= eDisplayTransactionNeeded;
2436 }
2437 if (disp.viewport != s.viewport) {
2438 disp.viewport = s.viewport;
2439 flags |= eDisplayTransactionNeeded;
2440 }
2441 }
Michael Lentine47e45402014-07-18 15:34:25 -07002442 if (what & DisplayState::eDisplaySizeChanged) {
2443 if (disp.width != s.width) {
2444 disp.width = s.width;
2445 flags |= eDisplayTransactionNeeded;
2446 }
2447 if (disp.height != s.height) {
2448 disp.height = s.height;
2449 flags |= eDisplayTransactionNeeded;
2450 }
2451 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002452 }
2453 return flags;
2454}
2455
2456uint32_t SurfaceFlinger::setClientStateLocked(
2457 const sp<Client>& client,
2458 const layer_state_t& s)
2459{
2460 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002461 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002462 if (layer != 0) {
2463 const uint32_t what = s.what;
2464 if (what & layer_state_t::ePositionChanged) {
2465 if (layer->setPosition(s.x, s.y))
2466 flags |= eTraversalNeeded;
2467 }
2468 if (what & layer_state_t::eLayerChanged) {
2469 // NOTE: index needs to be calculated before we update the state
2470 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2471 if (layer->setLayer(s.z)) {
2472 mCurrentState.layersSortedByZ.removeAt(idx);
2473 mCurrentState.layersSortedByZ.add(layer);
2474 // we need traversal (state changed)
2475 // AND transaction (list changed)
2476 flags |= eTransactionNeeded|eTraversalNeeded;
2477 }
2478 }
2479 if (what & layer_state_t::eSizeChanged) {
2480 if (layer->setSize(s.w, s.h)) {
2481 flags |= eTraversalNeeded;
2482 }
2483 }
2484 if (what & layer_state_t::eAlphaChanged) {
2485 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2486 flags |= eTraversalNeeded;
2487 }
2488 if (what & layer_state_t::eMatrixChanged) {
2489 if (layer->setMatrix(s.matrix))
2490 flags |= eTraversalNeeded;
2491 }
2492 if (what & layer_state_t::eTransparentRegionChanged) {
2493 if (layer->setTransparentRegionHint(s.transparentRegion))
2494 flags |= eTraversalNeeded;
2495 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002496 if ((what & layer_state_t::eVisibilityChanged) ||
2497 (what & layer_state_t::eOpacityChanged)) {
2498 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002499 if (layer->setFlags(s.flags, s.mask))
2500 flags |= eTraversalNeeded;
2501 }
2502 if (what & layer_state_t::eCropChanged) {
2503 if (layer->setCrop(s.crop))
2504 flags |= eTraversalNeeded;
2505 }
2506 if (what & layer_state_t::eLayerStackChanged) {
2507 // NOTE: index needs to be calculated before we update the state
2508 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2509 if (layer->setLayerStack(s.layerStack)) {
2510 mCurrentState.layersSortedByZ.removeAt(idx);
2511 mCurrentState.layersSortedByZ.add(layer);
2512 // we need traversal (state changed)
2513 // AND transaction (list changed)
2514 flags |= eTransactionNeeded|eTraversalNeeded;
2515 }
2516 }
2517 }
2518 return flags;
2519}
2520
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002521status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002522 const String8& name,
2523 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002524 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2525 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002527 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002528 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002529 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002530 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002531 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002532 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002533
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002534 status_t result = NO_ERROR;
2535
2536 sp<Layer> layer;
2537
Mathias Agopian3165cc22012-08-08 19:42:09 -07002538 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2539 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002540 result = createNormalLayer(client,
2541 name, w, h, flags, format,
2542 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002544 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002545 result = createDimLayer(client,
2546 name, w, h, flags,
2547 handle, gbp, &layer);
2548 break;
2549 default:
2550 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551 break;
2552 }
2553
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002554 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002555 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002556 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002557 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002558 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559}
2560
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002561status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2562 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2563 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002564{
2565 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002566 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567 case PIXEL_FORMAT_TRANSPARENT:
2568 case PIXEL_FORMAT_TRANSLUCENT:
2569 format = PIXEL_FORMAT_RGBA_8888;
2570 break;
2571 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002572 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002573 break;
2574 }
2575
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002576 *outLayer = new Layer(this, client, name, w, h, flags);
2577 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2578 if (err == NO_ERROR) {
2579 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002580 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002582
2583 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2584 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002585}
2586
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002587status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2588 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2589 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002591 *outLayer = new LayerDim(this, client, name, w, h, flags);
2592 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002593 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002594 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002595}
2596
Mathias Agopianac9fa422013-02-11 16:40:36 -08002597status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598{
Mathias Agopian67106042013-03-14 19:18:13 -07002599 // called by the window manager when it wants to remove a Layer
2600 status_t err = NO_ERROR;
2601 sp<Layer> l(client->getLayerUser(handle));
2602 if (l != NULL) {
2603 err = removeLayer(l);
2604 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2605 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002606 }
2607 return err;
2608}
2609
Mathias Agopian13127d82013-03-05 17:47:11 -08002610status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002611{
Mathias Agopian67106042013-03-14 19:18:13 -07002612 // called by ~LayerCleaner() when all references to the IBinder (handle)
2613 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002614 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002615 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002616 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002617 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002618 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002619 "error removing layer=%p (%s)", l.get(), strerror(-err));
2620 }
2621 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002622}
2623
Mathias Agopianb60314a2012-04-10 22:09:54 -07002624// ---------------------------------------------------------------------------
2625
Andy McFadden13a082e2012-08-24 10:16:42 -07002626void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002627 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002628 Vector<ComposerState> state;
2629 Vector<DisplayState> displays;
2630 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002631 d.what = DisplayState::eDisplayProjectionChanged |
2632 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002633 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002634 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002635 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002636 d.frame.makeInvalid();
2637 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002638 d.width = 0;
2639 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002640 displays.add(d);
2641 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002642 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002643
2644 const nsecs_t period =
2645 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2646 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002647}
2648
2649void SurfaceFlinger::initializeDisplays() {
2650 class MessageScreenInitialized : public MessageBase {
2651 SurfaceFlinger* flinger;
2652 public:
2653 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2654 virtual bool handler() {
2655 flinger->onInitializeDisplays();
2656 return true;
2657 }
2658 };
2659 sp<MessageBase> msg = new MessageScreenInitialized(this);
2660 postMessageAsync(msg); // we may be called from main thread, use async message
2661}
2662
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002663void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2664 int mode) {
2665 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2666 this);
2667 int32_t type = hw->getDisplayType();
2668 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002669
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002670 if (mode == currentMode) {
2671 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002672 return;
2673 }
2674
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002675 hw->setPowerMode(mode);
2676 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2677 ALOGW("Trying to set power mode for virtual display");
2678 return;
2679 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002680
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002681 if (currentMode == HWC_POWER_MODE_OFF) {
2682 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002683 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2684 // FIXME: eventthread only knows about the main display right now
2685 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002686 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002687 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002689 mVisibleRegionsDirty = true;
2690 repaintEverything();
2691 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002692 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002693 disableHardwareVsync(true); // also cancels any in-progress resync
2694
Mathias Agopiancde87a32012-09-13 14:09:01 -07002695 // FIXME: eventthread only knows about the main display right now
2696 mEventThread->onScreenReleased();
2697 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002698
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002699 getHwComposer().setPowerMode(type, mode);
2700 mVisibleRegionsDirty = true;
2701 // from this point on, SF will stop drawing on this display
2702 } else {
2703 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002704 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002705}
2706
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002707void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2708 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002709 SurfaceFlinger& mFlinger;
2710 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002711 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002712 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002713 MessageSetPowerMode(SurfaceFlinger& flinger,
2714 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2715 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002716 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002717 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002718 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002719 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002720 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002721 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002722 ALOGW("Attempt to set power mode = %d for virtual display",
2723 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002724 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002725 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002726 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002727 return true;
2728 }
2729 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002730 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002731 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002732}
2733
2734// ---------------------------------------------------------------------------
2735
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002736status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2737{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002738 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002739
Mathias Agopianbd115332013-04-18 16:41:04 -07002740 IPCThreadState* ipc = IPCThreadState::self();
2741 const int pid = ipc->getCallingPid();
2742 const int uid = ipc->getCallingUid();
2743 if ((uid != AID_SHELL) &&
2744 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002745 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002746 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002747 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002748 // Try to get the main lock, but don't insist if we can't
2749 // (this would indicate SF is stuck, but we want to be able to
2750 // print something in dumpsys).
2751 int retry = 3;
2752 while (mStateLock.tryLock()<0 && --retry>=0) {
2753 usleep(1000000);
2754 }
2755 const bool locked(retry >= 0);
2756 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002757 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002758 "SurfaceFlinger appears to be unresponsive, "
2759 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002760 }
2761
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002762 bool dumpAll = true;
2763 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002764 size_t numArgs = args.size();
2765 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002766 if ((index < numArgs) &&
2767 (args[index] == String16("--list"))) {
2768 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002769 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002770 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002771 }
2772
2773 if ((index < numArgs) &&
2774 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002775 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002776 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002777 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002778 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002779
2780 if ((index < numArgs) &&
2781 (args[index] == String16("--latency-clear"))) {
2782 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002783 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002784 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002785 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002786
2787 if ((index < numArgs) &&
2788 (args[index] == String16("--dispsync"))) {
2789 index++;
2790 mPrimaryDispSync.dump(result);
2791 dumpAll = false;
2792 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002793 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002794
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002795 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002796 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002797 }
2798
Mathias Agopian9795c422009-08-26 16:36:26 -07002799 if (locked) {
2800 mStateLock.unlock();
2801 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002802 }
2803 write(fd, result.string(), result.size());
2804 return NO_ERROR;
2805}
2806
Dan Stozac7014012014-02-14 15:03:43 -08002807void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2808 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002809{
2810 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2811 const size_t count = currentLayers.size();
2812 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002813 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002814 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002815 }
2816}
2817
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002818void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002819 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002820{
2821 String8 name;
2822 if (index < args.size()) {
2823 name = String8(args[index]);
2824 index++;
2825 }
2826
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002827 const nsecs_t period =
2828 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002829 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002830
2831 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002832 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002833 } else {
2834 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2835 const size_t count = currentLayers.size();
2836 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002837 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002838 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002839 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002840 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002841 }
2842 }
2843}
2844
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002845void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002846 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002847{
2848 String8 name;
2849 if (index < args.size()) {
2850 name = String8(args[index]);
2851 index++;
2852 }
2853
2854 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2855 const size_t count = currentLayers.size();
2856 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002857 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002858 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002859 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002860 }
2861 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002862
Svetoslavd85084b2014-03-20 10:28:31 -07002863 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002864}
2865
Jamie Gennis6547ff42013-07-16 20:12:42 -07002866// This should only be called from the main thread. Otherwise it would need
2867// the lock and should use mCurrentState rather than mDrawingState.
2868void SurfaceFlinger::logFrameStats() {
2869 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2870 const size_t count = drawingLayers.size();
2871 for (size_t i=0 ; i<count ; i++) {
2872 const sp<Layer>& layer(drawingLayers[i]);
2873 layer->logFrameStats();
2874 }
2875
2876 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2877}
2878
Andy McFadden4803b742012-09-24 19:07:20 -07002879/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2880{
2881 static const char* config =
2882 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002883#ifdef HAS_CONTEXT_PRIORITY
2884 " HAS_CONTEXT_PRIORITY"
2885#endif
2886#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2887 " NEVER_DEFAULT_TO_ASYNC_MODE"
2888#endif
2889#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2890 " TARGET_DISABLE_TRIPLE_BUFFERING"
2891#endif
2892 "]";
2893 result.append(config);
2894}
2895
Mathias Agopian74d211a2013-04-22 16:55:35 +02002896void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2897 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002898{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002899 bool colorize = false;
2900 if (index < args.size()
2901 && (args[index] == String16("--color"))) {
2902 colorize = true;
2903 index++;
2904 }
2905
2906 Colorizer colorizer(colorize);
2907
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002908 // figure out if we're stuck somewhere
2909 const nsecs_t now = systemTime();
2910 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2911 const nsecs_t inTransaction(mDebugInTransaction);
2912 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2913 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2914
2915 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002916 * Dump library configuration.
2917 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002918
2919 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002920 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002921 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002922 appendSfConfigString(result);
2923 appendUiConfigString(result);
2924 appendGuiConfigString(result);
2925 result.append("\n");
2926
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002927 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002928 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002929 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002930 result.append(SyncFeatures::getInstance().toString());
2931 result.append("\n");
2932
Andy McFadden41d67d72014-04-25 16:58:34 -07002933 colorizer.bold(result);
2934 result.append("DispSync configuration: ");
2935 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002936 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2937 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002938 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2939 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2940 result.append("\n");
2941
Andy McFadden4803b742012-09-24 19:07:20 -07002942 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002943 * Dump the visible layer list
2944 */
2945 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2946 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002947 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002948 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002949 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002950 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002951 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002952 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002953 }
2954
2955 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002956 * Dump Display state
2957 */
2958
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002959 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002960 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002961 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002962 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2963 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002964 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002965 }
2966
2967 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002968 * Dump SurfaceFlinger global state
2969 */
2970
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002971 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002972 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002973 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002974
Mathias Agopian888c8222012-08-04 21:10:38 -07002975 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002976 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002977
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002978 colorizer.bold(result);
2979 result.appendFormat("EGL implementation : %s\n",
2980 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2981 colorizer.reset(result);
2982 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002983 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002984
Mathias Agopian875d8e12013-06-07 15:35:48 -07002985 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002986
Mathias Agopian42977342012-08-05 00:40:46 -07002987 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002988 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2989 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002990 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002991 " last eglSwapBuffers() time: %f us\n"
2992 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002993 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002994 " refresh-rate : %f fps\n"
2995 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002996 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002997 " gpu_to_cpu_unsupported : %d\n"
2998 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002999 mLastSwapBufferTime/1000.0,
3000 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003001 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07003002 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
3003 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003004 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003005 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003006
Mathias Agopian74d211a2013-04-22 16:55:35 +02003007 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003008 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003009
Mathias Agopian74d211a2013-04-22 16:55:35 +02003010 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003011 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003012
3013 /*
3014 * VSYNC state
3015 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003016 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003017
3018 /*
3019 * Dump HWComposer state
3020 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003021 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003022 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003023 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003024 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003025 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07003026 (mDebugDisableHWC || mDebugRegion || mDaltonize
3027 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003028 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003029
3030 /*
3031 * Dump gralloc state
3032 */
3033 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3034 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003035}
3036
Mathias Agopian13127d82013-03-05 17:47:11 -08003037const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003038SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003039 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003040 wp<IBinder> dpy;
3041 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3042 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3043 dpy = mDisplays.keyAt(i);
3044 break;
3045 }
3046 }
3047 if (dpy == NULL) {
3048 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3049 // Just use the primary display so we have something to return
3050 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3051 }
3052 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003053}
3054
Keun young Park63f165f2012-08-31 10:53:36 -07003055bool SurfaceFlinger::startDdmConnection()
3056{
3057 void* libddmconnection_dso =
3058 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3059 if (!libddmconnection_dso) {
3060 return false;
3061 }
3062 void (*DdmConnection_start)(const char* name);
3063 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003064 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003065 if (!DdmConnection_start) {
3066 dlclose(libddmconnection_dso);
3067 return false;
3068 }
3069 (*DdmConnection_start)(getServiceName());
3070 return true;
3071}
3072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003073status_t SurfaceFlinger::onTransact(
3074 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3075{
3076 switch (code) {
3077 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003078 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003079 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003080 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003081 case CLEAR_ANIMATION_FRAME_STATS:
3082 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003083 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003084 {
3085 // codes that require permission check
3086 IPCThreadState* ipc = IPCThreadState::self();
3087 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003088 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003089 if ((uid != AID_GRAPHICS) &&
3090 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003091 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003092 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3093 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003094 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003095 break;
3096 }
3097 case CAPTURE_SCREEN:
3098 {
3099 // codes that require permission check
3100 IPCThreadState* ipc = IPCThreadState::self();
3101 const int pid = ipc->getCallingPid();
3102 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003103 if ((uid != AID_GRAPHICS) &&
3104 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003105 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003106 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3107 return PERMISSION_DENIED;
3108 }
3109 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003110 }
3111 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003113 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3114 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003115 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003116 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003117 IPCThreadState* ipc = IPCThreadState::self();
3118 const int pid = ipc->getCallingPid();
3119 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003120 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003121 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003122 return PERMISSION_DENIED;
3123 }
3124 int n;
3125 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003126 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003127 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003128 return NO_ERROR;
3129 case 1002: // SHOW_UPDATES
3130 n = data.readInt32();
3131 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003132 invalidateHwcGeometry();
3133 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003134 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003135 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003136 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003137 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003138 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003139 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003140 setTransactionFlags(
3141 eTransactionNeeded|
3142 eDisplayTransactionNeeded|
3143 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003144 return NO_ERROR;
3145 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003146 case 1006:{ // send empty update
3147 signalRefresh();
3148 return NO_ERROR;
3149 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003150 case 1008: // toggle use of hw composer
3151 n = data.readInt32();
3152 mDebugDisableHWC = n ? 1 : 0;
3153 invalidateHwcGeometry();
3154 repaintEverything();
3155 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003156 case 1009: // toggle use of transform hint
3157 n = data.readInt32();
3158 mDebugDisableTransformHint = n ? 1 : 0;
3159 invalidateHwcGeometry();
3160 repaintEverything();
3161 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003162 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003163 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003164 reply->writeInt32(0);
3165 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003166 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003167 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003168 return NO_ERROR;
3169 case 1013: {
3170 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003171 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3172 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003173 return NO_ERROR;
3174 }
3175 case 1014: {
3176 // daltonize
3177 n = data.readInt32();
3178 switch (n % 10) {
3179 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
3180 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
3181 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
3182 }
3183 if (n >= 10) {
3184 mDaltonizer.setMode(Daltonizer::correction);
3185 } else {
3186 mDaltonizer.setMode(Daltonizer::simulation);
3187 }
3188 mDaltonize = n > 0;
3189 invalidateHwcGeometry();
3190 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003191 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003192 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003193 case 1015: {
3194 // apply a color matrix
3195 n = data.readInt32();
3196 mHasColorMatrix = n ? 1 : 0;
3197 if (n) {
3198 // color matrix is sent as mat3 matrix followed by vec3
3199 // offset, then packed into a mat4 where the last row is
3200 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003201 for (size_t i = 0 ; i < 4; i++) {
3202 for (size_t j = 0; j < 4; j++) {
3203 mColorMatrix[i][j] = data.readFloat();
3204 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003205 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003206 } else {
3207 mColorMatrix = mat4();
3208 }
3209 invalidateHwcGeometry();
3210 repaintEverything();
3211 return NO_ERROR;
3212 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003213 // This is an experimental interface
3214 // Needs to be shifted to proper binder interface when we productize
3215 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003216 n = data.readInt32();
3217 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003218 return NO_ERROR;
3219 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003220 }
3221 }
3222 return err;
3223}
3224
Mathias Agopian53331da2011-08-22 21:44:41 -07003225void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003226 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003227 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003228}
3229
Mathias Agopian59119e62010-10-11 12:37:43 -07003230// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003231// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003232// ---------------------------------------------------------------------------
3233
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003234/* The code below is here to handle b/8734824
3235 *
3236 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003237 * from the surfaceflinger thread to the calling binder thread, where they
3238 * are executed. This allows the calling thread in the calling process to be
3239 * reused and not depend on having "enough" binder threads to handle the
3240 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003241 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003242class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003243 /* Parts of GraphicProducerWrapper are run on two different threads,
3244 * communicating by sending messages via Looper but also by shared member
3245 * data. Coherence maintenance is subtle and in places implicit (ugh).
3246 *
3247 * Don't rely on Looper's sendMessage/handleMessage providing
3248 * release/acquire semantics for any data not actually in the Message.
3249 * Data going from surfaceflinger to binder threads needs to be
3250 * synchronized explicitly.
3251 *
3252 * Barrier open/wait do provide release/acquire semantics. This provides
3253 * implicit synchronization for data coming back from binder to
3254 * surfaceflinger threads.
3255 */
3256
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003257 sp<IGraphicBufferProducer> impl;
3258 sp<Looper> looper;
3259 status_t result;
3260 bool exitPending;
3261 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003262 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003263 uint32_t code;
3264 Parcel const* data;
3265 Parcel* reply;
3266
3267 enum {
3268 MSG_API_CALL,
3269 MSG_EXIT
3270 };
3271
3272 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003273 * Called on surfaceflinger thread. This is called by our "fake"
3274 * BpGraphicBufferProducer. We package the data and reply Parcel and
3275 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003276 */
3277 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003278 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003279 this->code = code;
3280 this->data = &data;
3281 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003282 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003283 // if we've exited, we run the message synchronously right here.
3284 // note (JH): as far as I can tell from looking at the code, this
3285 // never actually happens. if it does, i'm not sure if it happens
3286 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003287 handleMessage(Message(MSG_API_CALL));
3288 } else {
3289 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003290 // Prevent stores to this->{code, data, reply} from being
3291 // reordered later than the construction of Message.
3292 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003293 looper->sendMessage(this, Message(MSG_API_CALL));
3294 barrier.wait();
3295 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003296 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003297 }
3298
3299 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003300 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003301 * call the real BpGraphicBufferProducer.
3302 */
3303 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003304 int what = message.what;
3305 // Prevent reads below from happening before the read from Message
3306 atomic_thread_fence(memory_order_acquire);
3307 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003308 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003309 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003310 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003311 exitRequested = true;
3312 }
3313 }
3314
3315public:
Jesse Hallb154c422014-07-13 12:47:02 -07003316 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3317 : impl(impl),
3318 looper(new Looper(true)),
3319 exitPending(false),
3320 exitRequested(false)
3321 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003322
Jesse Hallb154c422014-07-13 12:47:02 -07003323 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003324 status_t waitForResponse() {
3325 do {
3326 looper->pollOnce(-1);
3327 } while (!exitRequested);
3328 return result;
3329 }
3330
Jesse Hallb154c422014-07-13 12:47:02 -07003331 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003332 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003333 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003334 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003335 // Ensure this->result is visible to the binder thread before it
3336 // handles the message.
3337 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003338 looper->sendMessage(this, Message(MSG_EXIT));
3339 }
3340};
3341
3342
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003343status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3344 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003345 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003346 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003347 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003348
3349 if (CC_UNLIKELY(display == 0))
3350 return BAD_VALUE;
3351
3352 if (CC_UNLIKELY(producer == 0))
3353 return BAD_VALUE;
3354
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003355 // if we have secure windows on this display, never allow the screen capture
3356 // unless the producer interface is local (i.e.: we can take a screenshot for
3357 // ourselves).
3358 if (!producer->asBinder()->localBinder()) {
3359 Mutex::Autolock _l(mStateLock);
3360 sp<const DisplayDevice> hw(getDisplayDevice(display));
3361 if (hw->getSecureLayerVisible()) {
3362 ALOGW("FB is protected: PERMISSION_DENIED");
3363 return PERMISSION_DENIED;
3364 }
3365 }
3366
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003367 // Convert to surfaceflinger's internal rotation type.
3368 Transform::orientation_flags rotationFlags;
3369 switch (rotation) {
3370 case ISurfaceComposer::eRotateNone:
3371 rotationFlags = Transform::ROT_0;
3372 break;
3373 case ISurfaceComposer::eRotate90:
3374 rotationFlags = Transform::ROT_90;
3375 break;
3376 case ISurfaceComposer::eRotate180:
3377 rotationFlags = Transform::ROT_180;
3378 break;
3379 case ISurfaceComposer::eRotate270:
3380 rotationFlags = Transform::ROT_270;
3381 break;
3382 default:
3383 rotationFlags = Transform::ROT_0;
3384 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3385 break;
3386 }
3387
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003388 class MessageCaptureScreen : public MessageBase {
3389 SurfaceFlinger* flinger;
3390 sp<IBinder> display;
3391 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003392 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003393 uint32_t reqWidth, reqHeight;
3394 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003395 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003396 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003397 status_t result;
3398 public:
3399 MessageCaptureScreen(SurfaceFlinger* flinger,
3400 const sp<IBinder>& display,
3401 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003402 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003403 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003404 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003405 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003406 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003407 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003408 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003409 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003410 result(PERMISSION_DENIED)
3411 {
3412 }
3413 status_t getResult() const {
3414 return result;
3415 }
3416 virtual bool handler() {
3417 Mutex::Autolock _l(flinger->mStateLock);
3418 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003419 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003420 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003421 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003422 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003423 return true;
3424 }
3425 };
3426
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003427 // make sure to process transactions before screenshots -- a transaction
3428 // might already be pending but scheduled for VSYNC; this guarantees we
3429 // will handle it before the screenshot. When VSYNC finally arrives
3430 // the scheduled transaction will be a no-op. If no transactions are
3431 // scheduled at this time, this will end-up being a no-op as well.
3432 mEventQueue.invalidateTransactionNow();
3433
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003434 // this creates a "fake" BBinder which will serve as a "fake" remote
3435 // binder to receive the marshaled calls and forward them to the
3436 // real remote (a BpGraphicBufferProducer)
3437 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3438
3439 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3440 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003441 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003442 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003443 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003444 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003445
3446 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003447 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003448 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003449 }
3450 return res;
3451}
3452
Mathias Agopian180f10d2013-04-10 22:55:41 -07003453
3454void SurfaceFlinger::renderScreenImplLocked(
3455 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003456 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003457 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003458 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003459{
3460 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003461 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003462
3463 // get screen geometry
3464 const uint32_t hw_w = hw->getWidth();
3465 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003466 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3467
Dan Stozac1879002014-05-22 15:59:05 -07003468 // if a default or invalid sourceCrop is passed in, set reasonable values
3469 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3470 !sourceCrop.isValid()) {
3471 sourceCrop.setLeftTop(Point(0, 0));
3472 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3473 }
3474
3475 // ensure that sourceCrop is inside screen
3476 if (sourceCrop.left < 0) {
3477 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3478 }
Dan Stozabe31f442014-06-11 11:20:54 -07003479 if (sourceCrop.right > hw_w) {
3480 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003481 }
3482 if (sourceCrop.top < 0) {
3483 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3484 }
Dan Stozabe31f442014-06-11 11:20:54 -07003485 if (sourceCrop.bottom > hw_h) {
3486 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003487 }
3488
Mathias Agopian180f10d2013-04-10 22:55:41 -07003489 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003490 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003491
3492 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003493 engine.setViewportAndProjection(
3494 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003495 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003496
3497 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003498 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003499
3500 const LayerVector& layers( mDrawingState.layersSortedByZ );
3501 const size_t count = layers.size();
3502 for (size_t i=0 ; i<count ; ++i) {
3503 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003504 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003505 if (state.layerStack == hw->getLayerStack()) {
3506 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07003507#ifdef QCOM_BSP
3508 // dont render the secure Display Layer
3509 if(layer->isSecureDisplay()) {
3510 continue;
3511 }
3512#endif
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003513#ifdef QCOM_BSP
3514 int dispType = hw->getDisplayType();
3515 // Dont let ext_only and extended_mode to be captured
3516 // If not, we would see incorrect image during rotatoin
3517 // on primary
3518 if (layer->isVisible() &&
3519 not (!dispType && (layer->isExtOnly() ||
3520 (isExtendedMode() && layer->isYuvLayer())))) {
3521#else
Mathias Agopian180f10d2013-04-10 22:55:41 -07003522 if (layer->isVisible()) {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003523#endif
Mathias Agopian180f10d2013-04-10 22:55:41 -07003524 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303525 if(!layer->isProtected())
3526 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003527 if (filtering) layer->setFiltering(false);
3528 }
3529 }
3530 }
3531 }
3532
3533 // compositionComplete is needed for older driver
3534 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003535 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003536}
3537
3538
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003539status_t SurfaceFlinger::captureScreenImplLocked(
3540 const sp<const DisplayDevice>& hw,
3541 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003542 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003543 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003544 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003545{
3546 ATRACE_CALL();
3547
Mathias Agopian180f10d2013-04-10 22:55:41 -07003548 // get screen geometry
3549 const uint32_t hw_w = hw->getWidth();
3550 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003551
Mathias Agopian180f10d2013-04-10 22:55:41 -07003552 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3553 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3554 reqWidth, reqHeight, hw_w, hw_h);
3555 return BAD_VALUE;
3556 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003557
Mathias Agopian180f10d2013-04-10 22:55:41 -07003558 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3559 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3560
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003561 // create a surface (because we're a producer, and we need to
3562 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003563 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003564 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003565
3566 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003567 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003568 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3569 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003570
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003571 int err = 0;
3572 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003573 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003574 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3575 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003576
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003577 if (err == NO_ERROR) {
3578 ANativeWindowBuffer* buffer;
3579 /* TODO: Once we have the sync framework everywhere this can use
3580 * server-side waits on the fence that dequeueBuffer returns.
3581 */
3582 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3583 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003584 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003585 // create an EGLImage from the buffer so we can later
3586 // turn it into a texture
3587 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3588 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3589 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003590 // this binds the given EGLImage as a framebuffer for the
3591 // duration of this scope.
3592 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3593 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003594 // this will in fact render into our dequeued buffer
3595 // via an FBO, which means we didn't have to create
3596 // an EGLSurface and therefore we're not
3597 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003598 renderScreenImplLocked(
3599 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3600 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003601
Riley Andrews86639902014-08-15 12:27:24 -07003602 // Attempt to create a sync khr object that can produce a sync point. If that
3603 // isn't available, create a non-dupable sync object in the fallback path and
3604 // wait on it directly.
3605 EGLSyncKHR sync;
3606 if (!DEBUG_SCREENSHOTS) {
3607 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003608 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003609 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003610 }
Riley Andrews86639902014-08-15 12:27:24 -07003611 if (sync != EGL_NO_SYNC_KHR) {
3612 // get the sync fd
3613 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3614 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3615 ALOGW("captureScreen: failed to dup sync khr object");
3616 syncFd = -1;
3617 }
3618 eglDestroySyncKHR(mEGLDisplay, sync);
3619 } else {
3620 // fallback path
3621 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3622 if (sync != EGL_NO_SYNC_KHR) {
3623 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3624 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3625 EGLint eglErr = eglGetError();
3626 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3627 ALOGW("captureScreen: fence wait timed out");
3628 } else {
3629 ALOGW_IF(eglErr != EGL_SUCCESS,
3630 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3631 }
3632 eglDestroySyncKHR(mEGLDisplay, sync);
3633 } else {
3634 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3635 }
3636 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003637 if (DEBUG_SCREENSHOTS) {
3638 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3639 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3640 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3641 hw, minLayerZ, maxLayerZ);
3642 delete [] pixels;
3643 }
3644
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003645 } else {
3646 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3647 result = INVALID_OPERATION;
3648 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003649 // destroy our image
3650 eglDestroyImageKHR(mEGLDisplay, image);
3651 } else {
3652 result = BAD_VALUE;
3653 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003654 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003655 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003656 }
3657 } else {
3658 result = BAD_VALUE;
3659 }
3660 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3661 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003662
Mathias Agopian74c40c02010-09-29 13:02:36 -07003663 return result;
3664}
3665
Mathias Agopiand5556842013-09-19 17:08:37 -07003666void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3667 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003668 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003669 for (size_t y=0 ; y<h ; y++) {
3670 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3671 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003672 if (p[x] != 0xFF000000) return;
3673 }
3674 }
3675 ALOGE("*** we just took a black screenshot ***\n"
3676 "requested minz=%d, maxz=%d, layerStack=%d",
3677 minLayerZ, maxLayerZ, hw->getLayerStack());
3678 const LayerVector& layers( mDrawingState.layersSortedByZ );
3679 const size_t count = layers.size();
3680 for (size_t i=0 ; i<count ; ++i) {
3681 const sp<Layer>& layer(layers[i]);
3682 const Layer::State& state(layer->getDrawingState());
3683 const bool visible = (state.layerStack == hw->getLayerStack())
3684 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3685 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003686 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003687 visible ? '+' : '-',
3688 i, layer->getName().string(), state.layerStack, state.z,
3689 layer->isVisible(), state.flags, state.alpha);
3690 }
3691 }
3692}
3693
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003694// ---------------------------------------------------------------------------
3695
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003696SurfaceFlinger::LayerVector::LayerVector() {
3697}
3698
3699SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003700 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003701}
3702
3703int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3704 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003705{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003706 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003707 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3708 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003709
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003710 uint32_t ls = l->getCurrentState().layerStack;
3711 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003712 if (ls != rs)
3713 return ls - rs;
3714
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003715 uint32_t lz = l->getCurrentState().z;
3716 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003717 if (lz != rz)
3718 return lz - rz;
3719
3720 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003721}
3722
3723// ---------------------------------------------------------------------------
3724
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003725SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003726 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003727}
3728
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003729SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003730 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003731 viewport.makeInvalid();
3732 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003733}
3734
3735// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003737}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003738
3739
3740#if defined(__gl_h_)
3741#error "don't include gl/gl.h in this file"
3742#endif
3743
3744#if defined(__gl2_h_)
3745#error "don't include gl2/gl2.h in this file"
3746#endif