blob: 9adabe8482e209f8649f364cec679614253e1d7c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070027#include <GLES/glext.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>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
74
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#define DISPLAY_COUNT 1
76
Mathias Agopianfee2b462013-07-03 12:34:01 -070077/*
78 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
79 * black pixels.
80 */
81#define DEBUG_SCREENSHOTS false
82
Mathias Agopianca088332013-03-28 17:44:13 -070083EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086// ---------------------------------------------------------------------------
87
Mathias Agopian99b49842011-06-27 16:05:52 -070088const String16 sHardwareTest("android.permission.HARDWARE_TEST");
89const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
90const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
91const String16 sDump("android.permission.DUMP");
92
93// ---------------------------------------------------------------------------
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095SurfaceFlinger::SurfaceFlinger()
96 : BnSurfaceComposer(), Thread(false),
97 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070098 mTransactionPending(false),
99 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700101 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700102 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800106 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700109 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700110 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700111 mDebugInSwapBuffers(0),
112 mLastSwapBufferTime(0),
113 mDebugInTransaction(0),
114 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700115 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 // debugging stuff...
120 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Mathias Agopianb4b17302013-03-20 18:36:41 -0700122 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700123 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 property_get("debug.sf.showupdates", value, "0");
126 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700127
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 property_get("debug.sf.ddms", value, "0");
129 mDebugDDMS = atoi(value);
130 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700131 if (!startDdmConnection()) {
132 // start failed, and DDMS debugging not enabled
133 mDebugDDMS = 0;
134 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700135 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700136 ALOGI_IF(mDebugRegion, "showupdates enabled");
137 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::onFirstRef()
141{
142 mEventQueue.init(this);
143
144 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
145
146 // Wait for the main thread to be done with its initialization
147 mReadyToRunBarrier.wait();
148}
149
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151SurfaceFlinger::~SurfaceFlinger()
152{
Mathias Agopiana4912602012-07-12 14:25:33 -0700153 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
154 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
155 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800158void SurfaceFlinger::binderDied(const wp<IBinder>& who)
159{
160 // the window manager died on us. prepare its eulogy.
161
Andy McFadden13a082e2012-08-24 10:16:42 -0700162 // restore initial conditions (default device unblank, etc)
163 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800164
165 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700166 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800167}
168
Mathias Agopian7e27f052010-05-28 14:22:23 -0700169sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170{
Mathias Agopian96f08192010-06-02 23:28:45 -0700171 sp<ISurfaceComposerClient> bclient;
172 sp<Client> client(new Client(this));
173 status_t err = client->initCheck();
174 if (err == NO_ERROR) {
175 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 return bclient;
178}
179
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700180sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
181 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700182{
183 class DisplayToken : public BBinder {
184 sp<SurfaceFlinger> flinger;
185 virtual ~DisplayToken() {
186 // no more references, this display must be terminated
187 Mutex::Autolock _l(flinger->mStateLock);
188 flinger->mCurrentState.displays.removeItem(this);
189 flinger->setTransactionFlags(eDisplayTransactionNeeded);
190 }
191 public:
192 DisplayToken(const sp<SurfaceFlinger>& flinger)
193 : flinger(flinger) {
194 }
195 };
196
197 sp<BBinder> token = new DisplayToken(this);
198
199 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700200 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700201 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700202 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700203 mCurrentState.displays.add(token, info);
204
205 return token;
206}
207
Jesse Hall692c7232012-11-08 15:41:56 -0800208void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
209 ALOGW_IF(mBuiltinDisplays[type],
210 "Overwriting display token for display type %d", type);
211 mBuiltinDisplays[type] = new BBinder();
212 DisplayDeviceState info(type);
213 // All non-virtual displays are currently considered secure.
214 info.isSecure = true;
215 mCurrentState.displays.add(mBuiltinDisplays[type], info);
216}
217
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700219 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700220 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
221 return NULL;
222 }
Jesse Hall692c7232012-11-08 15:41:56 -0800223 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700224}
225
Jamie Gennis9a78c902011-01-12 18:30:40 -0800226sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
227{
228 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
229 return gba;
230}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232void SurfaceFlinger::bootFinished()
233{
234 const nsecs_t now = systemTime();
235 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000236 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700237 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700238
239 // wait patiently for the window manager death
240 const String16 name("window");
241 sp<IBinder> window(defaultServiceManager()->getService(name));
242 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700243 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700244 }
245
246 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700247 // formerly we would just kill the process, but we now ask it to exit so it
248 // can choose where to stop the animation.
249 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700252void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
253 class MessageDestroyGLTexture : public MessageBase {
254 GLuint texture;
255 public:
256 MessageDestroyGLTexture(GLuint texture)
257 : texture(texture) {
258 }
259 virtual bool handler() {
260 glDeleteTextures(1, &texture);
261 return true;
262 }
263 };
264 postMessageAsync(new MessageDestroyGLTexture(texture));
265}
266
Mathias Agopian722b98f2012-09-25 18:24:31 -0700267status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700268 EGLDisplay dpy,
269 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700270 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700271 EGLConfig* outConfig)
272{
273 EGLConfig config = NULL;
274 EGLint numConfigs = -1, n=0;
275 eglGetConfigs(dpy, NULL, 0, &numConfigs);
276 EGLConfig* const configs = new EGLConfig[numConfigs];
277 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700278
Mathias Agopian722b98f2012-09-25 18:24:31 -0700279 if (n) {
280 if (attribute != EGL_NONE) {
281 for (int i=0 ; i<n ; i++) {
282 EGLint value = 0;
283 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
284 if (wanted == value) {
285 *outConfig = configs[i];
286 delete [] configs;
287 return NO_ERROR;
288 }
289 }
290 } else {
291 // just pick the first one
292 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 delete [] configs;
294 return NO_ERROR;
295 }
296 }
297 delete [] configs;
298 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian722b98f2012-09-25 18:24:31 -0700301class EGLAttributeVector {
302 struct Attribute;
303 class Adder;
304 friend class Adder;
305 KeyedVector<Attribute, EGLint> mList;
306 struct Attribute {
307 Attribute() {};
308 Attribute(EGLint v) : v(v) { }
309 EGLint v;
310 bool operator < (const Attribute& other) const {
311 // this places EGL_NONE at the end
312 EGLint lhs(v);
313 EGLint rhs(other.v);
314 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
315 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
316 return lhs < rhs;
317 }
318 };
319 class Adder {
320 friend class EGLAttributeVector;
321 EGLAttributeVector& v;
322 EGLint attribute;
323 Adder(EGLAttributeVector& v, EGLint attribute)
324 : v(v), attribute(attribute) {
325 }
326 public:
327 void operator = (EGLint value) {
328 if (attribute != EGL_NONE) {
329 v.mList.add(attribute, value);
330 }
331 }
332 operator EGLint () const { return v.mList[attribute]; }
333 };
334public:
335 EGLAttributeVector() {
336 mList.add(EGL_NONE, EGL_NONE);
337 }
338 void remove(EGLint attribute) {
339 if (attribute != EGL_NONE) {
340 mList.removeItem(attribute);
341 }
342 }
343 Adder operator [] (EGLint attribute) {
344 return Adder(*this, attribute);
345 }
346 EGLint operator [] (EGLint attribute) const {
347 return mList[attribute];
348 }
349 // cast-operator to (EGLint const*)
350 operator EGLint const* () const { return &mList.keyAt(0).v; }
351};
352
Mathias Agopiana4912602012-07-12 14:25:33 -0700353EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
354 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
355 // it is to be used with WIFI displays
356 EGLConfig config;
357 EGLint dummy;
358 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700359
Mathias Agopian722b98f2012-09-25 18:24:31 -0700360 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700361 // TODO: enable ES2
362 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
363 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700364 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
365 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
366 attribs[EGL_RED_SIZE] = 8;
367 attribs[EGL_GREEN_SIZE] = 8;
368 attribs[EGL_BLUE_SIZE] = 8;
369
370 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
371 if (!err)
372 goto success;
373
Mathias Agopian875d8e12013-06-07 15:35:48 -0700374 // this didn't work, probably because we're on the emulator...
375 // try a simplified query
376 ALOGW("no suitable EGLConfig found, trying a simpler query");
377 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700378 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700380 attribs.remove(EGL_RED_SIZE);
381 attribs.remove(EGL_GREEN_SIZE);
382 attribs.remove(EGL_BLUE_SIZE);
383 err = selectConfigForAttribute(display, attribs,
384 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700385 if (!err)
386 goto success;
387
388 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700389 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700390 return 0;
391
392success:
393 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700394 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700395 return config;
396}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397
Mathias Agopiana4912602012-07-12 14:25:33 -0700398
Mathias Agopiana4912602012-07-12 14:25:33 -0700399status_t SurfaceFlinger::readyToRun()
400{
401 ALOGI( "SurfaceFlinger's main thread ready to run. "
402 "Initializing graphics H/W...");
403
Jesse Hall692c7232012-11-08 15:41:56 -0800404 Mutex::Autolock _l(mStateLock);
405
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700406 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700407 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
408 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700409
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700410 // Initialize the H/W composer object. There may or may not be an
411 // actual hardware composer underneath.
412 mHwc = new HWComposer(this,
413 *static_cast<HWComposer::EventHandler *>(this));
414
Mathias Agopian875d8e12013-06-07 15:35:48 -0700415 // initialize the config and context (can't fail)
416 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
417
418 // print some debugging info
419 EGLint r,g,b,a;
420 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
421 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
422 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
423 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
424 ALOGI("EGL informations:");
425 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
426 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
427 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
428 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
429 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
430
431 // get a RenderEngine for the given display / config (can't fail)
432 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
433
434 // retrieve the EGL context that was selected/created
435 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700436
Mathias Agopianed985572013-03-22 00:24:39 -0700437 // figure out which format we got
438 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
439 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
440
Mathias Agopianda27af92012-09-13 18:17:13 -0700441 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
442 "couldn't create EGLContext");
443
Mathias Agopiancde87a32012-09-13 14:09:01 -0700444 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700445 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700446 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700447 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700448 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700449 // All non-virtual displays are currently considered secure.
450 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800451 createBuiltinDisplayLocked(type);
452 wp<IBinder> token = mBuiltinDisplays[i];
453
Mathias Agopianf5a33922012-09-19 18:16:22 -0700454 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700455 type, allocateHwcDisplayId(type), isSecure, token,
456 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700457 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700458 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700459 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700460 // for displays other than the main display, so we always
461 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700462 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700463 hw->acquireScreen();
464 }
465 mDisplays.add(token, hw);
466 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700467 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700468
Mathias Agopian028508c2012-07-25 21:12:12 -0700469 // start the EventThread
470 mEventThread = new EventThread(this);
471 mEventQueue.setEventThread(mEventThread);
472
Mathias Agopian92a979a2012-08-02 18:32:23 -0700473 // initialize our drawing state
474 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700475
Mathias Agopiana4912602012-07-12 14:25:33 -0700476 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800477 mReadyToRunBarrier.open();
478
Andy McFadden13a082e2012-08-24 10:16:42 -0700479 // set initial conditions (e.g. unblank default device)
480 initializeDisplays();
481
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700482 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700483 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 return NO_ERROR;
486}
487
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700488int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
489 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
490 type : mHwc->allocateDisplayId();
491}
492
Mathias Agopiana67e4182012-06-19 17:26:12 -0700493void SurfaceFlinger::startBootAnim() {
494 // start boot animation
495 property_set("service.bootanim.exit", "0");
496 property_set("ctl.start", "bootanim");
497}
498
Mathias Agopian875d8e12013-06-07 15:35:48 -0700499size_t SurfaceFlinger::getMaxTextureSize() const {
500 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700501}
502
Mathias Agopian875d8e12013-06-07 15:35:48 -0700503size_t SurfaceFlinger::getMaxViewportDims() const {
504 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700505}
506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800508
Jamie Gennis582270d2011-08-17 18:19:00 -0700509bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800510 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800511 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800512 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700513 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800514}
515
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700516status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800517 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700518 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800519 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700520 type = i;
521 break;
522 }
523 }
524
525 if (type < 0) {
526 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700527 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700528
529 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700530 float xdpi = hwc.getDpiX(type);
531 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700532
533 // TODO: Not sure if display density should handled by SF any longer
534 class Density {
535 static int getDensityFromProperty(char const* propName) {
536 char property[PROPERTY_VALUE_MAX];
537 int density = 0;
538 if (property_get(propName, property, NULL) > 0) {
539 density = atoi(property);
540 }
541 return density;
542 }
543 public:
544 static int getEmuDensity() {
545 return getDensityFromProperty("qemu.sf.lcd_density"); }
546 static int getBuildDensity() {
547 return getDensityFromProperty("ro.sf.lcd_density"); }
548 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700549
550 if (type == DisplayDevice::DISPLAY_PRIMARY) {
551 // The density of the device is provided by a build property
552 float density = Density::getBuildDensity() / 160.0f;
553 if (density == 0) {
554 // the build doesn't provide a density -- this is wrong!
555 // use xdpi instead
556 ALOGE("ro.sf.lcd_density must be defined as a build property");
557 density = xdpi / 160.0f;
558 }
559 if (Density::getEmuDensity()) {
560 // if "qemu.sf.lcd_density" is specified, it overrides everything
561 xdpi = ydpi = density = Density::getEmuDensity();
562 density /= 160.0f;
563 }
564 info->density = density;
565
566 // TODO: this needs to go away (currently needed only by webkit)
567 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
568 info->orientation = hw->getOrientation();
569 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
570 } else {
571 // TODO: where should this value come from?
572 static const int TV_DENSITY = 213;
573 info->density = TV_DENSITY / 160.0f;
574 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700575 }
576
Mathias Agopian1604f772012-09-18 21:54:42 -0700577 info->w = hwc.getWidth(type);
578 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700579 info->xdpi = xdpi;
580 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700581 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700582
583 // All non-virtual displays are currently considered secure.
584 info->secure = true;
585
Mathias Agopian888c8222012-08-04 21:10:38 -0700586 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700587}
588
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800589// ----------------------------------------------------------------------------
590
591sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800592 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800596
597void SurfaceFlinger::waitForEvent() {
598 mEventQueue.waitMessage();
599}
600
601void SurfaceFlinger::signalTransaction() {
602 mEventQueue.invalidate();
603}
604
605void SurfaceFlinger::signalLayerUpdate() {
606 mEventQueue.invalidate();
607}
608
609void SurfaceFlinger::signalRefresh() {
610 mEventQueue.refresh();
611}
612
613status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
614 nsecs_t reltime, uint32_t flags) {
615 return mEventQueue.postMessage(msg, reltime);
616}
617
618status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
619 nsecs_t reltime, uint32_t flags) {
620 status_t res = mEventQueue.postMessage(msg, reltime);
621 if (res == NO_ERROR) {
622 msg->wait();
623 }
624 return res;
625}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 return true;
630}
631
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700632void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700633 if (mEventThread == NULL) {
634 // This is a temporary workaround for b/7145521. A non-null pointer
635 // does not mean EventThread has finished initializing, so this
636 // is not a correct fix.
637 ALOGW("WARNING: EventThread not started, ignoring vsync");
638 return;
639 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700640 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
641 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700642 mEventThread->onVSyncReceived(type, timestamp);
643 }
644}
645
646void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
647 if (mEventThread == NULL) {
648 // This is a temporary workaround for b/7145521. A non-null pointer
649 // does not mean EventThread has finished initializing, so this
650 // is not a correct fix.
651 ALOGW("WARNING: EventThread not started, ignoring hotplug");
652 return;
653 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700654
Mathias Agopian148994e2012-09-19 17:31:36 -0700655 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700656 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800657 if (connected) {
658 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700659 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800660 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
661 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700662 }
663 setTransactionFlags(eDisplayTransactionNeeded);
664
Andy McFadden9e9689c2012-10-10 18:17:51 -0700665 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700666 }
Mathias Agopian86303202012-07-24 22:46:10 -0700667}
668
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700669void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
670 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700671}
672
Mathias Agopian4fec8732012-06-29 14:12:52 -0700673void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800674 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800675 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700676 case MessageQueue::TRANSACTION:
677 handleMessageTransaction();
678 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679 case MessageQueue::INVALIDATE:
680 handleMessageTransaction();
681 handleMessageInvalidate();
682 signalRefresh();
683 break;
684 case MessageQueue::REFRESH:
685 handleMessageRefresh();
686 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800687 }
688}
689
Mathias Agopian4fec8732012-06-29 14:12:52 -0700690void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700691 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700692 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700693 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700694 }
695}
696
697void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700698 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700699 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700}
701
702void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700703 ATRACE_CALL();
704 preComposition();
705 rebuildLayerStacks();
706 setUpHWComposer();
707 doDebugFlashRegions();
708 doComposition();
709 postComposition();
710}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711
Mathias Agopiancd60f992012-08-16 16:28:27 -0700712void SurfaceFlinger::doDebugFlashRegions()
713{
714 // is debugging enabled
715 if (CC_LIKELY(!mDebugRegion))
716 return;
717
718 const bool repaintEverything = mRepaintEverything;
719 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
720 const sp<DisplayDevice>& hw(mDisplays[dpy]);
721 if (hw->canDraw()) {
722 // transform the dirty region into this screen's coordinate space
723 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
724 if (!dirtyRegion.isEmpty()) {
725 // redraw the whole screen
726 doComposeSurfaces(hw, Region(hw->bounds()));
727
728 // and draw the dirty region
729 glDisable(GL_TEXTURE_EXTERNAL_OES);
730 glDisable(GL_TEXTURE_2D);
731 glDisable(GL_BLEND);
732 glColor4f(1, 0, 1, 1);
733 const int32_t height = hw->getHeight();
734 Region::const_iterator it = dirtyRegion.begin();
735 Region::const_iterator const end = dirtyRegion.end();
736 while (it != end) {
737 const Rect& r = *it++;
738 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800739 { (GLfloat) r.left, (GLfloat) (height - r.top) },
740 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
741 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
742 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700743 };
744 glVertexPointer(2, GL_FLOAT, 0, vertices);
745 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
746 }
747 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700748 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700749 }
750 }
751 }
752
753 postFramebuffer();
754
755 if (mDebugRegion > 1) {
756 usleep(mDebugRegion * 1000);
757 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700758
759 HWComposer& hwc(getHwComposer());
760 if (hwc.initCheck() == NO_ERROR) {
761 status_t err = hwc.prepare();
762 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
763 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764}
765
766void SurfaceFlinger::preComposition()
767{
768 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700769 const LayerVector& layers(mDrawingState.layersSortedByZ);
770 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700772 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773 needExtraInvalidate = true;
774 }
775 }
776 if (needExtraInvalidate) {
777 signalLayerUpdate();
778 }
779}
780
781void SurfaceFlinger::postComposition()
782{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700783 const LayerVector& layers(mDrawingState.layersSortedByZ);
784 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700786 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800788
789 if (mAnimCompositionPending) {
790 mAnimCompositionPending = false;
791
792 const HWComposer& hwc = getHwComposer();
793 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800794 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800795 mAnimFrameTracker.setActualPresentFence(presentFence);
796 } else {
797 // The HWC doesn't support present fences, so use the refresh
798 // timestamp instead.
799 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
800 mAnimFrameTracker.setActualPresentTime(presentTime);
801 }
802 mAnimFrameTracker.advanceFrame();
803 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804}
805
806void SurfaceFlinger::rebuildLayerStacks() {
807 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700810 mVisibleRegionsDirty = false;
811 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700812
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700813 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700814 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700815 Region opaqueRegion;
816 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800817 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700818 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700819 const Transform& tr(hw->getTransform());
820 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700821 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700822 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700823 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700824
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700825 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700826 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const sp<Layer>& layer(layers[i]);
828 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700829 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700830 Region drawRegion(tr.transform(
831 layer->visibleNonTransparentRegion));
832 drawRegion.andSelf(bounds);
833 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700834 layersSortedByZ.add(layer);
835 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700836 }
837 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700838 }
Mathias Agopian42977342012-08-05 00:40:46 -0700839 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700840 hw->undefinedRegion.set(bounds);
841 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
842 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700843 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700844 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700846
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 HWComposer& hwc(getHwComposer());
849 if (hwc.initCheck() == NO_ERROR) {
850 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700851 if (CC_UNLIKELY(mHwWorkListDirty)) {
852 mHwWorkListDirty = false;
853 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
854 sp<const DisplayDevice> hw(mDisplays[dpy]);
855 const int32_t id = hw->getHwcDisplayId();
856 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700858 hw->getVisibleLayersSortedByZ());
859 const size_t count = currentLayers.size();
860 if (hwc.createWorkList(id, count) == NO_ERROR) {
861 HWComposer::LayerListIterator cur = hwc.begin(id);
862 const HWComposer::LayerListIterator end = hwc.end(id);
863 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800864 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700865 layer->setGeometry(hw, *cur);
866 if (mDebugDisableHWC || mDebugRegion) {
867 cur->setSkip(true);
868 }
869 }
870 }
871 }
872 }
873 }
874
875 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700876 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700877 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700878 const int32_t id = hw->getHwcDisplayId();
879 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700882 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700883 HWComposer::LayerListIterator cur = hwc.begin(id);
884 const HWComposer::LayerListIterator end = hwc.end(id);
885 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
886 /*
887 * update the per-frame h/w composer data for each layer
888 * and build the transparent region of the FB
889 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700891 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700892 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700893 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700894 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700895
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700896 status_t err = hwc.prepare();
897 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700898
899 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
900 sp<const DisplayDevice> hw(mDisplays[dpy]);
901 hw->prepareFrame(hwc);
902 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700903 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700904}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700905
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906void SurfaceFlinger::doComposition() {
907 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700908 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700909 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700910 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911 if (hw->canDraw()) {
912 // transform the dirty region into this screen's coordinate space
913 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800914
915 // repaint the framebuffer (if needed)
916 doDisplayComposition(hw, dirtyRegion);
917
Mathias Agopiancd60f992012-08-16 16:28:27 -0700918 hw->dirtyRegion.clear();
919 hw->flip(hw->swapRegion);
920 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700921 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700922 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700923 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700924 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700925 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700926}
927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928void SurfaceFlinger::postFramebuffer()
929{
Mathias Agopian841cde52012-03-01 15:44:37 -0800930 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800931
Mathias Agopiana44b0412011-10-16 18:46:35 -0700932 const nsecs_t now = systemTime();
933 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700934
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700935 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700936 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700937 if (!hwc.supportsFramebufferTarget()) {
938 // EGL spec says:
939 // "surface must be bound to the calling thread's current context,
940 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700941 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700942 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700943 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700944 }
945
Mathias Agopian92a979a2012-08-02 18:32:23 -0700946 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700947 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800948 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700949 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700950 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700951 int32_t id = hw->getHwcDisplayId();
952 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700953 HWComposer::LayerListIterator cur = hwc.begin(id);
954 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700955 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700956 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700957 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700960 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 }
Jesse Hallef194142012-06-14 14:45:17 -0700962 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800963 }
964
Mathias Agopiana44b0412011-10-16 18:46:35 -0700965 mLastSwapBufferTime = systemTime() - now;
966 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967}
968
Mathias Agopian87baae12012-07-31 12:38:26 -0700969void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970{
Mathias Agopian841cde52012-03-01 15:44:37 -0800971 ATRACE_CALL();
972
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700973 // here we keep a copy of the drawing state (that is the state that's
974 // going to be overwritten by handleTransactionLocked()) outside of
975 // mStateLock so that the side-effects of the State assignment
976 // don't happen with mStateLock held (which can cause deadlocks).
977 State drawingState(mDrawingState);
978
Mathias Agopianca4d3602011-05-19 15:38:14 -0700979 Mutex::Autolock _l(mStateLock);
980 const nsecs_t now = systemTime();
981 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982
Mathias Agopianca4d3602011-05-19 15:38:14 -0700983 // Here we're guaranteed that some transaction flags are set
984 // so we can call handleTransactionLocked() unconditionally.
985 // We call getTransactionFlags(), which will also clear the flags,
986 // with mStateLock held to guarantee that mCurrentState won't change
987 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700988
Mathias Agopiane57f2922012-08-09 16:29:12 -0700989 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700990 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700991
Mathias Agopianca4d3602011-05-19 15:38:14 -0700992 mLastTransactionTime = systemTime() - now;
993 mDebugInTransaction = 0;
994 invalidateHwcGeometry();
995 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700996}
997
Mathias Agopian87baae12012-07-31 12:38:26 -0700998void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700999{
1000 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 const size_t count = currentLayers.size();
1002
1003 /*
1004 * Traversal of the children
1005 * (perform the transaction for each of them if needed)
1006 */
1007
Mathias Agopian3559b072012-08-15 13:46:03 -07001008 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001010 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1012 if (!trFlags) continue;
1013
1014 const uint32_t flags = layer->doTransaction(0);
1015 if (flags & Layer::eVisibleRegion)
1016 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 }
1018 }
1019
1020 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001021 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 */
1023
Mathias Agopiane57f2922012-08-09 16:29:12 -07001024 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001025 // here we take advantage of Vector's copy-on-write semantics to
1026 // improve performance by skipping the transaction entirely when
1027 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001028 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1029 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001030 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001032 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001033 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001034
1035 // find the displays that were removed
1036 // (ie: in drawing state but not in current state)
1037 // also handle displays that changed
1038 // (ie: displays that are in both lists)
1039 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001040 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1041 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001042 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001043 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001044 // Call makeCurrent() on the primary display so we can
1045 // be sure that nothing associated with this display
1046 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001047 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001048 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001049 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1050 if (hw != NULL)
1051 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001052 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1053 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001054 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001055 } else {
1056 ALOGW("trying to remove the main display");
1057 }
1058 } else {
1059 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001060 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001061 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001062 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001063 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001064 // recreating the DisplayDevice, so we just remove it
1065 // from the drawing state, so that it get re-added
1066 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001067 sp<DisplayDevice> hw(getDisplayDevice(display));
1068 if (hw != NULL)
1069 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001070 mDisplays.removeItem(display);
1071 mDrawingState.displays.removeItemsAt(i);
1072 dc--; i--;
1073 // at this point we must loop to the next item
1074 continue;
1075 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001076
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001077 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001078 if (disp != NULL) {
1079 if (state.layerStack != draw[i].layerStack) {
1080 disp->setLayerStack(state.layerStack);
1081 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001082 if ((state.orientation != draw[i].orientation)
1083 || (state.viewport != draw[i].viewport)
1084 || (state.frame != draw[i].frame))
1085 {
1086 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001087 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001088 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001089 }
1090 }
1091 }
1092
1093 // find displays that were added
1094 // (ie: in current state but not in drawing state)
1095 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001096 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001097 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001098
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001099 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001100 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001101 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001102 // Virtual displays without a surface are dormant:
1103 // they have external state (layer stack, projection,
1104 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001105 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001106 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001107 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001108 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001109 state.displayName);
1110 }
1111 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001112 ALOGE_IF(state.surface!=NULL,
1113 "adding a supported display, but rendering "
1114 "surface is provided (%p), ignoring it",
1115 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001116 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001117 // for supported (by hwc) displays we provide our
1118 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001119 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001120 }
1121
1122 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001123 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001124 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001125 state.type, hwcDisplayId, state.isSecure,
1126 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001127 hw->setLayerStack(state.layerStack);
1128 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001129 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001130 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001131 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001132 if (state.isVirtualDisplay()) {
1133 if (hwcDisplayId >= 0) {
1134 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1135 hw->getWidth(), hw->getHeight(),
1136 hw->getFormat());
1137 }
1138 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001139 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001140 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001141 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001142 }
1143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146
Mathias Agopian84300952012-11-21 16:02:13 -08001147 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1148 // The transform hint might have changed for some layers
1149 // (either because a display has changed, or because a layer
1150 // as changed).
1151 //
1152 // Walk through all the layers in currentLayers,
1153 // and update their transform hint.
1154 //
1155 // If a layer is visible only on a single display, then that
1156 // display is used to calculate the hint, otherwise we use the
1157 // default display.
1158 //
1159 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1160 // the hint is set before we acquire a buffer from the surface texture.
1161 //
1162 // NOTE: layer transactions have taken place already, so we use their
1163 // drawing state. However, SurfaceFlinger's own transaction has not
1164 // happened yet, so we must use the current state layer list
1165 // (soon to become the drawing state list).
1166 //
1167 sp<const DisplayDevice> disp;
1168 uint32_t currentlayerStack = 0;
1169 for (size_t i=0; i<count; i++) {
1170 // NOTE: we rely on the fact that layers are sorted by
1171 // layerStack first (so we don't have to traverse the list
1172 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001173 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001174 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001175 if (i==0 || currentlayerStack != layerStack) {
1176 currentlayerStack = layerStack;
1177 // figure out if this layerstack is mirrored
1178 // (more than one display) if so, pick the default display,
1179 // if not, pick the only display it's on.
1180 disp.clear();
1181 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1182 sp<const DisplayDevice> hw(mDisplays[dpy]);
1183 if (hw->getLayerStack() == currentlayerStack) {
1184 if (disp == NULL) {
1185 disp = hw;
1186 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001187 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001188 break;
1189 }
1190 }
1191 }
1192 }
Chet Haase91d25932013-04-11 15:24:55 -07001193 if (disp == NULL) {
1194 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1195 // redraw after transform hint changes. See bug 8508397.
1196
1197 // could be null when this layer is using a layerStack
1198 // that is not visible on any display. Also can occur at
1199 // screen off/on times.
1200 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001201 }
Chet Haase91d25932013-04-11 15:24:55 -07001202 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001203 }
1204 }
1205
1206
Mathias Agopian3559b072012-08-15 13:46:03 -07001207 /*
1208 * Perform our own transaction if needed
1209 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001210
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001211 const LayerVector& layers(mDrawingState.layersSortedByZ);
1212 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001213 // layers have been added
1214 mVisibleRegionsDirty = true;
1215 }
1216
1217 // some layers might have been removed, so
1218 // we need to update the regions they're exposing.
1219 if (mLayersRemoved) {
1220 mLayersRemoved = false;
1221 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001222 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001223 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001224 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001225 if (currentLayers.indexOf(layer) < 0) {
1226 // this layer is not visible anymore
1227 // TODO: we could traverse the tree from front to back and
1228 // compute the actual visible region
1229 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001230 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001231 Region visibleReg = s.transform.transform(
1232 Region(Rect(s.active.w, s.active.h)));
1233 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001234 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 }
1237
1238 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001239}
1240
1241void SurfaceFlinger::commitTransaction()
1242{
1243 if (!mLayersPendingRemoval.isEmpty()) {
1244 // Notify removed layers now that they can't be drawn from
1245 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1246 mLayersPendingRemoval[i]->onRemoved();
1247 }
1248 mLayersPendingRemoval.clear();
1249 }
1250
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001251 // If this transaction is part of a window animation then the next frame
1252 // we composite should be considered an animation as well.
1253 mAnimCompositionPending = mAnimTransactionPending;
1254
Mathias Agopian4fec8732012-06-29 14:12:52 -07001255 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001256 mTransactionPending = false;
1257 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001258 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259}
1260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001262 const LayerVector& currentLayers, uint32_t layerStack,
1263 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264{
Mathias Agopian841cde52012-03-01 15:44:37 -08001265 ATRACE_CALL();
1266
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 Region aboveOpaqueLayers;
1268 Region aboveCoveredLayers;
1269 Region dirty;
1270
Mathias Agopian87baae12012-07-31 12:38:26 -07001271 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272
1273 size_t i = currentLayers.size();
1274 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276
1277 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001278 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279
Jesse Hall01e29052013-02-19 16:13:35 -08001280 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001281 if (s.layerStack != layerStack)
1282 continue;
1283
Mathias Agopianab028732010-03-16 16:41:46 -07001284 /*
1285 * opaqueRegion: area of a surface that is fully opaque.
1286 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001288
1289 /*
1290 * visibleRegion: area of a surface that is visible on screen
1291 * and not fully transparent. This is essentially the layer's
1292 * footprint minus the opaque regions above it.
1293 * Areas covered by a translucent surface are considered visible.
1294 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001296
1297 /*
1298 * coveredRegion: area of a surface that is covered by all
1299 * visible regions above it (which includes the translucent areas).
1300 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001302
Jesse Halla8026d22012-09-25 13:25:04 -07001303 /*
1304 * transparentRegion: area of a surface that is hinted to be completely
1305 * transparent. This is only used to tell when the layer has no visible
1306 * non-transparent regions and can be removed from the layer list. It
1307 * does not affect the visibleRegion of this layer or any layers
1308 * beneath it. The hint may not be correct if apps don't respect the
1309 * SurfaceView restrictions (which, sadly, some don't).
1310 */
1311 Region transparentRegion;
1312
Mathias Agopianab028732010-03-16 16:41:46 -07001313
1314 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001315 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001316 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001317 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001319 if (!visibleRegion.isEmpty()) {
1320 // Remove the transparent area from the visible region
1321 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 const Transform tr(s.transform);
1323 if (tr.transformed()) {
1324 if (tr.preserveRects()) {
1325 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001326 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 } else {
1328 // transformation too complex, can't do the
1329 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001330 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001331 }
1332 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001333 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001334 }
Mathias Agopianab028732010-03-16 16:41:46 -07001335 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336
Mathias Agopianab028732010-03-16 16:41:46 -07001337 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001338 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001339 if (s.alpha==255 && !translucent &&
1340 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1341 // the opaque region is the layer's footprint
1342 opaqueRegion = visibleRegion;
1343 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 }
1345 }
1346
Mathias Agopianab028732010-03-16 16:41:46 -07001347 // Clip the covered region to the visible region
1348 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1349
1350 // Update aboveCoveredLayers for next (lower) layer
1351 aboveCoveredLayers.orSelf(visibleRegion);
1352
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 // subtract the opaque region covered by the layers above us
1354 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355
1356 // compute this layer's dirty region
1357 if (layer->contentDirty) {
1358 // we need to invalidate the whole region
1359 dirty = visibleRegion;
1360 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001361 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 layer->contentDirty = false;
1363 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001364 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001365 * the exposed region consists of two components:
1366 * 1) what's VISIBLE now and was COVERED before
1367 * 2) what's EXPOSED now less what was EXPOSED before
1368 *
1369 * note that (1) is conservative, we start with the whole
1370 * visible region but only keep what used to be covered by
1371 * something -- which mean it may have been exposed.
1372 *
1373 * (2) handles areas that were not covered by anything but got
1374 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001375 */
Mathias Agopianab028732010-03-16 16:41:46 -07001376 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001377 const Region oldVisibleRegion = layer->visibleRegion;
1378 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001379 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1380 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 }
1382 dirty.subtractSelf(aboveOpaqueLayers);
1383
1384 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001385 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386
Mathias Agopianab028732010-03-16 16:41:46 -07001387 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001389
Jesse Halla8026d22012-09-25 13:25:04 -07001390 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391 layer->setVisibleRegion(visibleRegion);
1392 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001393 layer->setVisibleNonTransparentRegion(
1394 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 }
1396
Mathias Agopian87baae12012-07-31 12:38:26 -07001397 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398}
1399
Mathias Agopian87baae12012-07-31 12:38:26 -07001400void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1401 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001402 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001403 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1404 if (hw->getLayerStack() == layerStack) {
1405 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001406 }
1407 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001408}
1409
1410void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001412 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413
Mathias Agopian4fec8732012-06-29 14:12:52 -07001414 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001415 const LayerVector& layers(mDrawingState.layersSortedByZ);
1416 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001417 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001418 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001419 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001420 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001421 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001423
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001424 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425}
1426
Mathias Agopianad456f92011-01-13 17:53:01 -08001427void SurfaceFlinger::invalidateHwcGeometry()
1428{
1429 mHwWorkListDirty = true;
1430}
1431
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001432
Mathias Agopiancd60f992012-08-16 16:28:27 -07001433void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001434 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435{
Mathias Agopian87baae12012-07-31 12:38:26 -07001436 Region dirtyRegion(inDirtyRegion);
1437
Mathias Agopianb8a55602009-06-26 19:06:36 -07001438 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001439 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440
Mathias Agopian42977342012-08-05 00:40:46 -07001441 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001442 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001443 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1444 // takes a rectangle, we must make sure to update that whole
1445 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001446 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001447 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001448 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001449 // We need to redraw the rectangle that will be updated
1450 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001451 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001452 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001453 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001454 } else {
1455 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001456 dirtyRegion.set(hw->bounds());
1457 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
1459 }
1460
Mathias Agopiancd60f992012-08-16 16:28:27 -07001461 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001463 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001464 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001465
1466 // swap buffers (presentation)
1467 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopiancd60f992012-08-16 16:28:27 -07001470void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001471{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001472 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001473 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001474 HWComposer::LayerListIterator cur = hwc.begin(id);
1475 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001476
Mathias Agopian85d751c2012-08-29 16:59:24 -07001477 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1478 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001479 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001480 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1481 hw->getDisplayName().string());
1482 return;
1483 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001484
1485 // set the frame buffer
1486 glMatrixMode(GL_MODELVIEW);
1487 glLoadIdentity();
1488
1489 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001490 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001491 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001492 // when using overlays, we assume a fully transparent framebuffer
1493 // NOTE: we could reduce how much we need to clear, for instance
1494 // remove where there are opaque FB layers. however, on some
1495 // GPUs doing a "clean slate" glClear might be more efficient.
1496 // We'll revisit later if needed.
1497 glClearColor(0, 0, 0, 0);
1498 glClear(GL_COLOR_BUFFER_BIT);
1499 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001500 // we start with the whole screen area
1501 const Region bounds(hw->getBounds());
1502
1503 // we remove the scissor part
1504 // we're left with the letterbox region
1505 // (common case is that letterbox ends-up being empty)
1506 const Region letterbox(bounds.subtract(hw->getScissor()));
1507
1508 // compute the area to clear
1509 Region region(hw->undefinedRegion.merge(letterbox));
1510
1511 // but limit it to the dirty region
1512 region.andSelf(dirty);
1513
Mathias Agopianb9494d52012-04-18 02:28:45 -07001514 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001515 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001516 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001517 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001518 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001519 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001520
Mathias Agopian766dc492012-10-30 18:08:06 -07001521 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1522 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001523 // scissor on the main display. It should never be needed
1524 // anyways (though in theory it could since the API allows it).
1525 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001526 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001527 if (scissor != bounds) {
1528 // scissor doesn't match the screen's dimensions, so we
1529 // need to clear everything outside of it and enable
1530 // the GL scissor so we don't draw anything where we shouldn't
1531 const GLint height = hw->getHeight();
1532 glScissor(scissor.left, height - scissor.bottom,
1533 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001534 // enable scissor for this frame
1535 glEnable(GL_SCISSOR_TEST);
1536 }
1537 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001538 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001539
Mathias Agopian85d751c2012-08-29 16:59:24 -07001540 /*
1541 * and then, render the layers targeted at the framebuffer
1542 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001543
Mathias Agopian13127d82013-03-05 17:47:11 -08001544 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001545 const size_t count = layers.size();
1546 const Transform& tr = hw->getTransform();
1547 if (cur != end) {
1548 // we're using h/w composer
1549 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001550 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001551 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001552 if (!clip.isEmpty()) {
1553 switch (cur->getCompositionType()) {
1554 case HWC_OVERLAY: {
1555 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1556 && i
1557 && layer->isOpaque()
1558 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001559 // never clear the very first layer since we're
1560 // guaranteed the FB is already cleared
1561 layer->clearWithOpenGL(hw, clip);
1562 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001563 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001564 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 case HWC_FRAMEBUFFER: {
1566 layer->draw(hw, clip);
1567 break;
1568 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001569 case HWC_FRAMEBUFFER_TARGET: {
1570 // this should not happen as the iterator shouldn't
1571 // let us get there.
1572 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1573 break;
1574 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001575 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001576 }
1577 layer->setAcquireFence(hw, *cur);
1578 }
1579 } else {
1580 // we're not using h/w composer
1581 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001582 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001583 const Region clip(dirty.intersect(
1584 tr.transform(layer->visibleRegion)));
1585 if (!clip.isEmpty()) {
1586 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001587 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001588 }
1589 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001590
1591 // disable scissor at the end of the frame
1592 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593}
1594
Mathias Agopian55801e42012-08-27 18:54:24 -07001595void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1596 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001598 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001599 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001600 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001601 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001602
Mathias Agopian55801e42012-08-27 18:54:24 -07001603 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001604 Region::const_iterator it = region.begin();
1605 Region::const_iterator const end = region.end();
1606 while (it != end) {
1607 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001608 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001609 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1610 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1611 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1612 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001613 };
1614 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001615 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1616 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}
1618
Mathias Agopianac9fa422013-02-11 16:40:36 -08001619void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1620 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001621 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001622 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001623{
Mathias Agopian96f08192010-06-02 23:28:45 -07001624 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001625 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001626
Mathias Agopian96f08192010-06-02 23:28:45 -07001627 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001628 Mutex::Autolock _l(mStateLock);
1629 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001630 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001631}
1632
Mathias Agopian13127d82013-03-05 17:47:11 -08001633status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634{
1635 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001636 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001638 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001639 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001640 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 return NO_ERROR;
1642 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001643 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644}
1645
Mathias Agopiandea20b12011-05-03 17:04:02 -07001646uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1647{
1648 return android_atomic_release_load(&mTransactionFlags);
1649}
1650
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1652{
1653 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1654}
1655
Mathias Agopianbb641242010-05-18 17:06:55 -07001656uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657{
1658 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1659 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001660 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 }
1662 return old;
1663}
1664
Mathias Agopian8b33f032012-07-24 20:43:54 -07001665void SurfaceFlinger::setTransactionState(
1666 const Vector<ComposerState>& state,
1667 const Vector<DisplayState>& displays,
1668 uint32_t flags)
1669{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001670 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001671 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001672 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001673
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001674 if (flags & eAnimation) {
1675 // For window updates that are part of an animation we must wait for
1676 // previous animation "frames" to be handled.
1677 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001678 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001679 if (CC_UNLIKELY(err != NO_ERROR)) {
1680 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001681 // caller after a few seconds.
1682 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1683 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001684 mAnimTransactionPending = false;
1685 break;
1686 }
1687 }
1688 }
1689
Mathias Agopiane57f2922012-08-09 16:29:12 -07001690 size_t count = displays.size();
1691 for (size_t i=0 ; i<count ; i++) {
1692 const DisplayState& s(displays[i]);
1693 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001694 }
1695
Mathias Agopiane57f2922012-08-09 16:29:12 -07001696 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001697 for (size_t i=0 ; i<count ; i++) {
1698 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001699 // Here we need to check that the interface we're given is indeed
1700 // one of our own. A malicious client could give us a NULL
1701 // IInterface, or one of its own or even one of our own but a
1702 // different type. All these situations would cause us to crash.
1703 //
1704 // NOTE: it would be better to use RTTI as we could directly check
1705 // that we have a Client*. however, RTTI is disabled in Android.
1706 if (s.client != NULL) {
1707 sp<IBinder> binder = s.client->asBinder();
1708 if (binder != NULL) {
1709 String16 desc(binder->getInterfaceDescriptor());
1710 if (desc == ISurfaceComposerClient::descriptor) {
1711 sp<Client> client( static_cast<Client *>(s.client.get()) );
1712 transactionFlags |= setClientStateLocked(client, s.state);
1713 }
1714 }
1715 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001716 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001717
Mathias Agopian386aa982011-11-07 21:58:03 -08001718 if (transactionFlags) {
1719 // this triggers the transaction
1720 setTransactionFlags(transactionFlags);
1721
1722 // if this is a synchronous transaction, wait for it to take effect
1723 // before returning.
1724 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001725 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001726 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001727 if (flags & eAnimation) {
1728 mAnimTransactionPending = true;
1729 }
1730 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001731 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1732 if (CC_UNLIKELY(err != NO_ERROR)) {
1733 // just in case something goes wrong in SF, return to the
1734 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001735 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1736 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001737 break;
1738 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001739 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 }
1741}
1742
Mathias Agopiane57f2922012-08-09 16:29:12 -07001743uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1744{
Jesse Hall9a143922012-10-04 16:29:19 -07001745 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1746 if (dpyIdx < 0)
1747 return 0;
1748
Mathias Agopiane57f2922012-08-09 16:29:12 -07001749 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001750 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001751 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001752 const uint32_t what = s.what;
1753 if (what & DisplayState::eSurfaceChanged) {
1754 if (disp.surface->asBinder() != s.surface->asBinder()) {
1755 disp.surface = s.surface;
1756 flags |= eDisplayTransactionNeeded;
1757 }
1758 }
1759 if (what & DisplayState::eLayerStackChanged) {
1760 if (disp.layerStack != s.layerStack) {
1761 disp.layerStack = s.layerStack;
1762 flags |= eDisplayTransactionNeeded;
1763 }
1764 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001765 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001766 if (disp.orientation != s.orientation) {
1767 disp.orientation = s.orientation;
1768 flags |= eDisplayTransactionNeeded;
1769 }
1770 if (disp.frame != s.frame) {
1771 disp.frame = s.frame;
1772 flags |= eDisplayTransactionNeeded;
1773 }
1774 if (disp.viewport != s.viewport) {
1775 disp.viewport = s.viewport;
1776 flags |= eDisplayTransactionNeeded;
1777 }
1778 }
1779 }
1780 return flags;
1781}
1782
1783uint32_t SurfaceFlinger::setClientStateLocked(
1784 const sp<Client>& client,
1785 const layer_state_t& s)
1786{
1787 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001788 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001789 if (layer != 0) {
1790 const uint32_t what = s.what;
1791 if (what & layer_state_t::ePositionChanged) {
1792 if (layer->setPosition(s.x, s.y))
1793 flags |= eTraversalNeeded;
1794 }
1795 if (what & layer_state_t::eLayerChanged) {
1796 // NOTE: index needs to be calculated before we update the state
1797 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1798 if (layer->setLayer(s.z)) {
1799 mCurrentState.layersSortedByZ.removeAt(idx);
1800 mCurrentState.layersSortedByZ.add(layer);
1801 // we need traversal (state changed)
1802 // AND transaction (list changed)
1803 flags |= eTransactionNeeded|eTraversalNeeded;
1804 }
1805 }
1806 if (what & layer_state_t::eSizeChanged) {
1807 if (layer->setSize(s.w, s.h)) {
1808 flags |= eTraversalNeeded;
1809 }
1810 }
1811 if (what & layer_state_t::eAlphaChanged) {
1812 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1813 flags |= eTraversalNeeded;
1814 }
1815 if (what & layer_state_t::eMatrixChanged) {
1816 if (layer->setMatrix(s.matrix))
1817 flags |= eTraversalNeeded;
1818 }
1819 if (what & layer_state_t::eTransparentRegionChanged) {
1820 if (layer->setTransparentRegionHint(s.transparentRegion))
1821 flags |= eTraversalNeeded;
1822 }
1823 if (what & layer_state_t::eVisibilityChanged) {
1824 if (layer->setFlags(s.flags, s.mask))
1825 flags |= eTraversalNeeded;
1826 }
1827 if (what & layer_state_t::eCropChanged) {
1828 if (layer->setCrop(s.crop))
1829 flags |= eTraversalNeeded;
1830 }
1831 if (what & layer_state_t::eLayerStackChanged) {
1832 // NOTE: index needs to be calculated before we update the state
1833 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1834 if (layer->setLayerStack(s.layerStack)) {
1835 mCurrentState.layersSortedByZ.removeAt(idx);
1836 mCurrentState.layersSortedByZ.add(layer);
1837 // we need traversal (state changed)
1838 // AND transaction (list changed)
1839 flags |= eTransactionNeeded|eTraversalNeeded;
1840 }
1841 }
1842 }
1843 return flags;
1844}
1845
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001846status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001847 const String8& name,
1848 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001849 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1850 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001852 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001853 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001854 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001855 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001856 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001857 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001858
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001859 status_t result = NO_ERROR;
1860
1861 sp<Layer> layer;
1862
Mathias Agopian3165cc22012-08-08 19:42:09 -07001863 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1864 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001865 result = createNormalLayer(client,
1866 name, w, h, flags, format,
1867 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001868 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001869 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001870 result = createDimLayer(client,
1871 name, w, h, flags,
1872 handle, gbp, &layer);
1873 break;
1874 default:
1875 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001876 break;
1877 }
1878
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001879 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001880 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001881 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001883 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884}
1885
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001886status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1887 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1888 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889{
1890 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001891 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001892 case PIXEL_FORMAT_TRANSPARENT:
1893 case PIXEL_FORMAT_TRANSLUCENT:
1894 format = PIXEL_FORMAT_RGBA_8888;
1895 break;
1896 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001897#ifdef NO_RGBX_8888
1898 format = PIXEL_FORMAT_RGB_565;
1899#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001900 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001901#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 break;
1903 }
1904
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001905#ifdef NO_RGBX_8888
1906 if (format == PIXEL_FORMAT_RGBX_8888)
1907 format = PIXEL_FORMAT_RGBA_8888;
1908#endif
1909
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001910 *outLayer = new Layer(this, client, name, w, h, flags);
1911 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1912 if (err == NO_ERROR) {
1913 *handle = (*outLayer)->getHandle();
1914 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001916
1917 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1918 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919}
1920
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001921status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1922 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1923 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001925 *outLayer = new LayerDim(this, client, name, w, h, flags);
1926 *handle = (*outLayer)->getHandle();
1927 *gbp = (*outLayer)->getBufferQueue();
1928 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001929}
1930
Mathias Agopianac9fa422013-02-11 16:40:36 -08001931status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001932{
Mathias Agopian67106042013-03-14 19:18:13 -07001933 // called by the window manager when it wants to remove a Layer
1934 status_t err = NO_ERROR;
1935 sp<Layer> l(client->getLayerUser(handle));
1936 if (l != NULL) {
1937 err = removeLayer(l);
1938 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1939 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001940 }
1941 return err;
1942}
1943
Mathias Agopian13127d82013-03-05 17:47:11 -08001944status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001945{
Mathias Agopian67106042013-03-14 19:18:13 -07001946 // called by ~LayerCleaner() when all references to the IBinder (handle)
1947 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001948 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001949 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001950 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001951 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001952 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001953 "error removing layer=%p (%s)", l.get(), strerror(-err));
1954 }
1955 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956}
1957
Mathias Agopianb60314a2012-04-10 22:09:54 -07001958// ---------------------------------------------------------------------------
1959
Andy McFadden13a082e2012-08-24 10:16:42 -07001960void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001961 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001962 Vector<ComposerState> state;
1963 Vector<DisplayState> displays;
1964 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001965 d.what = DisplayState::eDisplayProjectionChanged |
1966 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001967 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001968 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001969 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001970 d.frame.makeInvalid();
1971 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001972 displays.add(d);
1973 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001974 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001975}
1976
1977void SurfaceFlinger::initializeDisplays() {
1978 class MessageScreenInitialized : public MessageBase {
1979 SurfaceFlinger* flinger;
1980 public:
1981 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1982 virtual bool handler() {
1983 flinger->onInitializeDisplays();
1984 return true;
1985 }
1986 };
1987 sp<MessageBase> msg = new MessageScreenInitialized(this);
1988 postMessageAsync(msg); // we may be called from main thread, use async message
1989}
1990
1991
Mathias Agopiancde87a32012-09-13 14:09:01 -07001992void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001993 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
1994 if (hw->isScreenAcquired()) {
1995 // this is expected, e.g. when power manager wakes up during boot
1996 ALOGD(" screen was previously acquired");
1997 return;
1998 }
1999
Mathias Agopian42977342012-08-05 00:40:46 -07002000 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002001 int32_t type = hw->getDisplayType();
2002 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2003 // built-in display, tell the HWC
2004 getHwComposer().acquire(type);
2005
2006 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2007 // FIXME: eventthread only knows about the main display right now
2008 mEventThread->onScreenAcquired();
2009 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002010 }
Mathias Agopian20128302012-08-13 18:32:13 -07002011 mVisibleRegionsDirty = true;
2012 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013}
2014
Mathias Agopiancde87a32012-09-13 14:09:01 -07002015void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002016 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2017 if (!hw->isScreenAcquired()) {
2018 ALOGD(" screen was previously released");
2019 return;
2020 }
2021
2022 hw->releaseScreen();
2023 int32_t type = hw->getDisplayType();
2024 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2025 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002026 // FIXME: eventthread only knows about the main display right now
2027 mEventThread->onScreenReleased();
2028 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002029
2030 // built-in display, tell the HWC
2031 getHwComposer().release(type);
2032 }
2033 mVisibleRegionsDirty = true;
2034 // from this point on, SF will stop drawing on this display
2035}
2036
2037void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2038 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002039 SurfaceFlinger& mFlinger;
2040 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002041 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002042 MessageScreenAcquired(SurfaceFlinger& flinger,
2043 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002044 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002045 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2046 if (hw == NULL) {
2047 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2048 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2049 ALOGW("Attempt to unblank virtual display");
2050 } else {
2051 mFlinger.onScreenAcquired(hw);
2052 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002053 return true;
2054 }
2055 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002056 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2057 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058}
2059
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002060void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002061 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002062 SurfaceFlinger& mFlinger;
2063 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002064 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002065 MessageScreenReleased(SurfaceFlinger& flinger,
2066 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002067 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002068 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2069 if (hw == NULL) {
2070 ALOGE("Attempt to blank null display %p", mDisplay.get());
2071 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2072 ALOGW("Attempt to blank virtual display");
2073 } else {
2074 mFlinger.onScreenReleased(hw);
2075 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002076 return true;
2077 }
2078 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002079 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2080 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002081}
2082
2083// ---------------------------------------------------------------------------
2084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2086{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002088
Mathias Agopianbd115332013-04-18 16:41:04 -07002089 IPCThreadState* ipc = IPCThreadState::self();
2090 const int pid = ipc->getCallingPid();
2091 const int uid = ipc->getCallingUid();
2092 if ((uid != AID_SHELL) &&
2093 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002094 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002095 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002096 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002097 // Try to get the main lock, but don't insist if we can't
2098 // (this would indicate SF is stuck, but we want to be able to
2099 // print something in dumpsys).
2100 int retry = 3;
2101 while (mStateLock.tryLock()<0 && --retry>=0) {
2102 usleep(1000000);
2103 }
2104 const bool locked(retry >= 0);
2105 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002106 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002107 "SurfaceFlinger appears to be unresponsive, "
2108 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002109 }
2110
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002111 bool dumpAll = true;
2112 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002113 size_t numArgs = args.size();
2114 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002115 if ((index < numArgs) &&
2116 (args[index] == String16("--list"))) {
2117 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002118 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002119 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002120 }
2121
2122 if ((index < numArgs) &&
2123 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002124 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002125 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002126 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002127 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002128
2129 if ((index < numArgs) &&
2130 (args[index] == String16("--latency-clear"))) {
2131 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002132 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002133 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002134 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002135 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002136
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002137 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002138 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002139 }
2140
Mathias Agopian9795c422009-08-26 16:36:26 -07002141 if (locked) {
2142 mStateLock.unlock();
2143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 }
2145 write(fd, result.string(), result.size());
2146 return NO_ERROR;
2147}
2148
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002149void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002150 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002151{
2152 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2153 const size_t count = currentLayers.size();
2154 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002155 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002156 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002157 }
2158}
2159
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002160void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002161 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002162{
2163 String8 name;
2164 if (index < args.size()) {
2165 name = String8(args[index]);
2166 index++;
2167 }
2168
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002169 const nsecs_t period =
2170 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2171 result.appendFormat("%lld\n", period);
2172
2173 if (name.isEmpty()) {
2174 mAnimFrameTracker.dump(result);
2175 } else {
2176 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2177 const size_t count = currentLayers.size();
2178 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002179 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002180 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002181 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002182 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002183 }
2184 }
2185}
2186
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002187void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002188 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002189{
2190 String8 name;
2191 if (index < args.size()) {
2192 name = String8(args[index]);
2193 index++;
2194 }
2195
2196 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2197 const size_t count = currentLayers.size();
2198 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002199 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002200 if (name.isEmpty() || (name == layer->getName())) {
2201 layer->clearStats();
2202 }
2203 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002204
2205 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002206}
2207
Andy McFadden4803b742012-09-24 19:07:20 -07002208/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2209{
2210 static const char* config =
2211 " [sf"
2212#ifdef NO_RGBX_8888
2213 " NO_RGBX_8888"
2214#endif
2215#ifdef HAS_CONTEXT_PRIORITY
2216 " HAS_CONTEXT_PRIORITY"
2217#endif
2218#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2219 " NEVER_DEFAULT_TO_ASYNC_MODE"
2220#endif
2221#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2222 " TARGET_DISABLE_TRIPLE_BUFFERING"
2223#endif
2224 "]";
2225 result.append(config);
2226}
2227
Mathias Agopian74d211a2013-04-22 16:55:35 +02002228void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2229 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002230{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002231 bool colorize = false;
2232 if (index < args.size()
2233 && (args[index] == String16("--color"))) {
2234 colorize = true;
2235 index++;
2236 }
2237
2238 Colorizer colorizer(colorize);
2239
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002240 // figure out if we're stuck somewhere
2241 const nsecs_t now = systemTime();
2242 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2243 const nsecs_t inTransaction(mDebugInTransaction);
2244 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2245 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2246
2247 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002248 * Dump library configuration.
2249 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002250
2251 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002252 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002253 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002254 appendSfConfigString(result);
2255 appendUiConfigString(result);
2256 appendGuiConfigString(result);
2257 result.append("\n");
2258
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002259 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002260 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002261 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002262 result.append(SyncFeatures::getInstance().toString());
2263 result.append("\n");
2264
Andy McFadden4803b742012-09-24 19:07:20 -07002265 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002266 * Dump the visible layer list
2267 */
2268 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2269 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002270 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002271 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002272 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002273 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002274 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002275 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002276 }
2277
2278 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002279 * Dump Display state
2280 */
2281
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002282 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002283 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002284 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002285 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2286 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002287 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002288 }
2289
2290 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002291 * Dump SurfaceFlinger global state
2292 */
2293
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002294 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002295 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002297
Mathias Agopian888c8222012-08-04 21:10:38 -07002298 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002299 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002300
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002301 colorizer.bold(result);
2302 result.appendFormat("EGL implementation : %s\n",
2303 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2304 colorizer.reset(result);
2305 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002306 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002307
Mathias Agopian875d8e12013-06-07 15:35:48 -07002308 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309
Mathias Agopian42977342012-08-05 00:40:46 -07002310 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002311 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002312 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002313 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002314 " last eglSwapBuffers() time: %f us\n"
2315 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002316 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002317 " refresh-rate : %f fps\n"
2318 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002319 " y-dpi : %f\n"
2320 " EGL_NATIVE_VISUAL_ID : %d\n"
2321 " gpu_to_cpu_unsupported : %d\n"
2322 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002323 mLastSwapBufferTime/1000.0,
2324 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002325 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002326 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2327 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002328 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2329 mEGLNativeVisualId,
2330 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002331
Mathias Agopian74d211a2013-04-22 16:55:35 +02002332 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002334
Mathias Agopian74d211a2013-04-22 16:55:35 +02002335 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002336 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002337
2338 /*
2339 * VSYNC state
2340 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002341 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002342
2343 /*
2344 * Dump HWComposer state
2345 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002346 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002348 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002349 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002350 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2351 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002352 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002353
2354 /*
2355 * Dump gralloc state
2356 */
2357 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2358 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002359}
2360
Mathias Agopian13127d82013-03-05 17:47:11 -08002361const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002362SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002363 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002364 wp<IBinder> dpy;
2365 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2366 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2367 dpy = mDisplays.keyAt(i);
2368 break;
2369 }
2370 }
2371 if (dpy == NULL) {
2372 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2373 // Just use the primary display so we have something to return
2374 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2375 }
2376 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002377}
2378
Keun young Park63f165f2012-08-31 10:53:36 -07002379bool SurfaceFlinger::startDdmConnection()
2380{
2381 void* libddmconnection_dso =
2382 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2383 if (!libddmconnection_dso) {
2384 return false;
2385 }
2386 void (*DdmConnection_start)(const char* name);
2387 DdmConnection_start =
2388 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2389 if (!DdmConnection_start) {
2390 dlclose(libddmconnection_dso);
2391 return false;
2392 }
2393 (*DdmConnection_start)(getServiceName());
2394 return true;
2395}
2396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002397status_t SurfaceFlinger::onTransact(
2398 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2399{
2400 switch (code) {
2401 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002402 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002403 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002404 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002405 case BLANK:
2406 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002407 {
2408 // codes that require permission check
2409 IPCThreadState* ipc = IPCThreadState::self();
2410 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002411 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002412 if ((uid != AID_GRAPHICS) &&
2413 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002414 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002415 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2416 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002417 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002418 break;
2419 }
2420 case CAPTURE_SCREEN:
2421 {
2422 // codes that require permission check
2423 IPCThreadState* ipc = IPCThreadState::self();
2424 const int pid = ipc->getCallingPid();
2425 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002426 if ((uid != AID_GRAPHICS) &&
2427 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002428 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002429 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2430 return PERMISSION_DENIED;
2431 }
2432 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002433 }
2434 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002436 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2437 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002438 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002439 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002440 IPCThreadState* ipc = IPCThreadState::self();
2441 const int pid = ipc->getCallingPid();
2442 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002443 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002444 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002445 return PERMISSION_DENIED;
2446 }
2447 int n;
2448 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002449 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002450 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002451 return NO_ERROR;
2452 case 1002: // SHOW_UPDATES
2453 n = data.readInt32();
2454 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002455 invalidateHwcGeometry();
2456 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002458 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002459 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002460 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002461 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002462 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002463 setTransactionFlags(
2464 eTransactionNeeded|
2465 eDisplayTransactionNeeded|
2466 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002467 return NO_ERROR;
2468 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002469 case 1006:{ // send empty update
2470 signalRefresh();
2471 return NO_ERROR;
2472 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002473 case 1008: // toggle use of hw composer
2474 n = data.readInt32();
2475 mDebugDisableHWC = n ? 1 : 0;
2476 invalidateHwcGeometry();
2477 repaintEverything();
2478 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002479 case 1009: // toggle use of transform hint
2480 n = data.readInt32();
2481 mDebugDisableTransformHint = n ? 1 : 0;
2482 invalidateHwcGeometry();
2483 repaintEverything();
2484 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002486 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002487 reply->writeInt32(0);
2488 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002489 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002490 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 return NO_ERROR;
2492 case 1013: {
2493 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002494 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2495 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496 }
2497 return NO_ERROR;
2498 }
2499 }
2500 return err;
2501}
2502
Mathias Agopian53331da2011-08-22 21:44:41 -07002503void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002504 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002505 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002506}
2507
Mathias Agopian59119e62010-10-11 12:37:43 -07002508// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002509// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002510// ---------------------------------------------------------------------------
2511
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002512/* The code below is here to handle b/8734824
2513 *
2514 * We create a IGraphicBufferProducer wrapper that forwards all calls
2515 * to the calling binder thread, where they are executed. This allows
2516 * the calling thread to be reused (on the other side) and not
2517 * depend on having "enough" binder threads to handle the requests.
2518 *
2519 */
2520
2521class GraphicProducerWrapper : public BBinder, public MessageHandler {
2522 sp<IGraphicBufferProducer> impl;
2523 sp<Looper> looper;
2524 status_t result;
2525 bool exitPending;
2526 bool exitRequested;
2527 mutable Barrier barrier;
2528 volatile int32_t memoryBarrier;
2529 uint32_t code;
2530 Parcel const* data;
2531 Parcel* reply;
2532
2533 enum {
2534 MSG_API_CALL,
2535 MSG_EXIT
2536 };
2537
2538 /*
2539 * this is called by our "fake" BpGraphicBufferProducer. We package the
2540 * data and reply Parcel and forward them to the calling thread.
2541 */
2542 virtual status_t transact(uint32_t code,
2543 const Parcel& data, Parcel* reply, uint32_t flags) {
2544 this->code = code;
2545 this->data = &data;
2546 this->reply = reply;
2547 android_atomic_acquire_store(0, &memoryBarrier);
2548 if (exitPending) {
2549 // if we've exited, we run the message synchronously right here
2550 handleMessage(Message(MSG_API_CALL));
2551 } else {
2552 barrier.close();
2553 looper->sendMessage(this, Message(MSG_API_CALL));
2554 barrier.wait();
2555 }
2556 return NO_ERROR;
2557 }
2558
2559 /*
2560 * here we run on the binder calling thread. All we've got to do is
2561 * call the real BpGraphicBufferProducer.
2562 */
2563 virtual void handleMessage(const Message& message) {
2564 android_atomic_release_load(&memoryBarrier);
2565 if (message.what == MSG_API_CALL) {
2566 impl->asBinder()->transact(code, data[0], reply);
2567 barrier.open();
2568 } else if (message.what == MSG_EXIT) {
2569 exitRequested = true;
2570 }
2571 }
2572
2573public:
2574 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2575 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2576 exitPending(false), exitRequested(false) {
2577 }
2578
2579 status_t waitForResponse() {
2580 do {
2581 looper->pollOnce(-1);
2582 } while (!exitRequested);
2583 return result;
2584 }
2585
2586 void exit(status_t result) {
2587 exitPending = true;
2588 looper->sendMessage(this, Message(MSG_EXIT));
2589 }
2590};
2591
2592
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002593status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2594 const sp<IGraphicBufferProducer>& producer,
2595 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002596 uint32_t minLayerZ, uint32_t maxLayerZ,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002597 bool useReadPixels) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002598
2599 if (CC_UNLIKELY(display == 0))
2600 return BAD_VALUE;
2601
2602 if (CC_UNLIKELY(producer == 0))
2603 return BAD_VALUE;
2604
2605 class MessageCaptureScreen : public MessageBase {
2606 SurfaceFlinger* flinger;
2607 sp<IBinder> display;
2608 sp<IGraphicBufferProducer> producer;
2609 uint32_t reqWidth, reqHeight;
2610 uint32_t minLayerZ,maxLayerZ;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002611 bool useReadPixels;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002612 status_t result;
2613 public:
2614 MessageCaptureScreen(SurfaceFlinger* flinger,
2615 const sp<IBinder>& display,
2616 const sp<IGraphicBufferProducer>& producer,
2617 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002618 uint32_t minLayerZ, uint32_t maxLayerZ, bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002619 : flinger(flinger), display(display), producer(producer),
2620 reqWidth(reqWidth), reqHeight(reqHeight),
2621 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002622 useReadPixels(useReadPixels),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002623 result(PERMISSION_DENIED)
2624 {
2625 }
2626 status_t getResult() const {
2627 return result;
2628 }
2629 virtual bool handler() {
2630 Mutex::Autolock _l(flinger->mStateLock);
2631 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002632 bool useReadPixels = this->useReadPixels && !flinger->mGpuToCpuSupported;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002633 result = flinger->captureScreenImplLocked(hw,
2634 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2635 useReadPixels);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002636 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002637 return true;
2638 }
2639 };
2640
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002641 // make sure to process transactions before screenshots -- a transaction
2642 // might already be pending but scheduled for VSYNC; this guarantees we
2643 // will handle it before the screenshot. When VSYNC finally arrives
2644 // the scheduled transaction will be a no-op. If no transactions are
2645 // scheduled at this time, this will end-up being a no-op as well.
2646 mEventQueue.invalidateTransactionNow();
2647
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002648 // this creates a "fake" BBinder which will serve as a "fake" remote
2649 // binder to receive the marshaled calls and forward them to the
2650 // real remote (a BpGraphicBufferProducer)
2651 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2652
2653 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2654 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002655 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002656 display, IGraphicBufferProducer::asInterface( wrapper ),
2657 reqWidth, reqHeight, minLayerZ, maxLayerZ,
2658 useReadPixels);
2659
2660 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002661 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002662 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002663 }
2664 return res;
2665}
2666
Mathias Agopian180f10d2013-04-10 22:55:41 -07002667
2668void SurfaceFlinger::renderScreenImplLocked(
2669 const sp<const DisplayDevice>& hw,
2670 uint32_t reqWidth, uint32_t reqHeight,
2671 uint32_t minLayerZ, uint32_t maxLayerZ,
2672 bool yswap)
2673{
2674 ATRACE_CALL();
2675
2676 // get screen geometry
2677 const uint32_t hw_w = hw->getWidth();
2678 const uint32_t hw_h = hw->getHeight();
2679
2680 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2681
2682 // make sure to clear all GL error flags
2683 while ( glGetError() != GL_NO_ERROR ) ;
2684
2685 // set-up our viewport
2686 glViewport(0, 0, reqWidth, reqHeight);
2687 glMatrixMode(GL_PROJECTION);
2688 glLoadIdentity();
2689 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2690 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2691 glMatrixMode(GL_MODELVIEW);
2692 glLoadIdentity();
2693
2694 // redraw the screen entirely...
2695 glDisable(GL_SCISSOR_TEST);
2696 glClearColor(0,0,0,1);
2697 glClear(GL_COLOR_BUFFER_BIT);
2698 glDisable(GL_TEXTURE_EXTERNAL_OES);
2699 glDisable(GL_TEXTURE_2D);
2700
2701 const LayerVector& layers( mDrawingState.layersSortedByZ );
2702 const size_t count = layers.size();
2703 for (size_t i=0 ; i<count ; ++i) {
2704 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002705 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002706 if (state.layerStack == hw->getLayerStack()) {
2707 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2708 if (layer->isVisible()) {
2709 if (filtering) layer->setFiltering(true);
2710 layer->draw(hw);
2711 if (filtering) layer->setFiltering(false);
2712 }
2713 }
2714 }
2715 }
2716
2717 // compositionComplete is needed for older driver
2718 hw->compositionComplete();
2719}
2720
2721
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002722status_t SurfaceFlinger::captureScreenImplLocked(
2723 const sp<const DisplayDevice>& hw,
2724 const sp<IGraphicBufferProducer>& producer,
2725 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002726 uint32_t minLayerZ, uint32_t maxLayerZ,
2727 bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002728{
2729 ATRACE_CALL();
2730
Mathias Agopian180f10d2013-04-10 22:55:41 -07002731 // get screen geometry
2732 const uint32_t hw_w = hw->getWidth();
2733 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002734
Mathias Agopian180f10d2013-04-10 22:55:41 -07002735 // if we have secure windows on this display, never allow the screen capture
2736 if (hw->getSecureLayerVisible()) {
2737 ALOGW("FB is protected: PERMISSION_DENIED");
2738 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002739 }
2740
Mathias Agopian180f10d2013-04-10 22:55:41 -07002741 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2742 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2743 reqWidth, reqHeight, hw_w, hw_h);
2744 return BAD_VALUE;
2745 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002746
Mathias Agopian180f10d2013-04-10 22:55:41 -07002747 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2748 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2749
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002750 // create a surface (because we're a producer, and we need to
2751 // dequeue/queue a buffer)
2752 sp<Surface> sur = new Surface(producer);
2753 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002754
2755 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002756 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
2757 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
2758 if (!useReadPixels) {
2759 usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002760 }
2761
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002762 int err = 0;
2763 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2764 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2765 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002766
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002767 if (err == NO_ERROR) {
2768 ANativeWindowBuffer* buffer;
2769 /* TODO: Once we have the sync framework everywhere this can use
2770 * server-side waits on the fence that dequeueBuffer returns.
2771 */
2772 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2773 if (result == NO_ERROR) {
2774 // create an EGLImage from the buffer so we can later
2775 // turn it into a texture
2776 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2777 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2778 if (image != EGL_NO_IMAGE_KHR) {
2779 GLuint tname, name;
2780 if (!useReadPixels) {
2781 // turn our EGLImage into a texture
2782 glGenTextures(1, &tname);
2783 glBindTexture(GL_TEXTURE_2D, tname);
2784 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2785 // create a Framebuffer Object to render into
2786 glGenFramebuffersOES(1, &name);
2787 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2788 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
Mathias Agopian875d8e12013-06-07 15:35:48 -07002789 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002790 } else {
2791 // since we're going to use glReadPixels() anyways,
2792 // use an intermediate renderbuffer instead
2793 glGenRenderbuffersOES(1, &tname);
2794 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2795 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
2796 // create a FBO to render into
2797 glGenFramebuffersOES(1, &name);
2798 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2799 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2800 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2801 }
2802
2803 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2804 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2805 // this will in fact render into our dequeued buffer
2806 // via an FBO, which means we didn't have to create
2807 // an EGLSurface and therefore we're not
2808 // dependent on the context's EGLConfig.
2809 renderScreenImplLocked(hw, reqWidth, reqHeight,
2810 minLayerZ, maxLayerZ, true);
2811
2812 if (useReadPixels) {
2813 sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
2814 void* vaddr;
2815 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
2816 glReadPixels(0, 0, buffer->stride, reqHeight,
2817 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
Mathias Agopianfee2b462013-07-03 12:34:01 -07002818 checkScreenshot(buf, vaddr, hw, minLayerZ, maxLayerZ);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002819 buf->unlock();
2820 }
2821 }
2822 } else {
2823 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2824 result = INVALID_OPERATION;
2825 }
2826
2827 // back to main framebuffer
2828 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2829 glDeleteFramebuffersOES(1, &name);
2830 if (!useReadPixels) {
2831 glDeleteTextures(1, &tname);
2832 } else {
2833 glDeleteRenderbuffersOES(1, &tname);
2834 }
2835 // destroy our image
2836 eglDestroyImageKHR(mEGLDisplay, image);
2837 } else {
2838 result = BAD_VALUE;
2839 }
2840 window->queueBuffer(window, buffer, -1);
2841 }
2842 } else {
2843 result = BAD_VALUE;
2844 }
2845 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2846 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002847
Mathias Agopian875d8e12013-06-07 15:35:48 -07002848 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002849
Mathias Agopian74c40c02010-09-29 13:02:36 -07002850 return result;
2851}
2852
Mathias Agopianfee2b462013-07-03 12:34:01 -07002853void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2854 const sp<const DisplayDevice>& hw,
2855 uint32_t minLayerZ, uint32_t maxLayerZ) {
2856 if (DEBUG_SCREENSHOTS) {
2857 for (ssize_t y=0 ; y<buf->height ; y++) {
2858 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2859 for (ssize_t x=0 ; x<buf->width ; x++) {
2860 if (p[x] != 0xFF000000) return;
2861 }
2862 }
2863 ALOGE("*** we just took a black screenshot ***\n"
2864 "requested minz=%d, maxz=%d, layerStack=%d",
2865 minLayerZ, maxLayerZ, hw->getLayerStack());
2866 const LayerVector& layers( mDrawingState.layersSortedByZ );
2867 const size_t count = layers.size();
2868 for (size_t i=0 ; i<count ; ++i) {
2869 const sp<Layer>& layer(layers[i]);
2870 const Layer::State& state(layer->getDrawingState());
2871 const bool visible = (state.layerStack == hw->getLayerStack())
2872 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2873 && (layer->isVisible());
2874 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2875 visible ? '+' : '-',
2876 i, layer->getName().string(), state.layerStack, state.z,
2877 layer->isVisible(), state.flags, state.alpha);
2878 }
2879 }
2880}
2881
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002882// ---------------------------------------------------------------------------
2883
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002884SurfaceFlinger::LayerVector::LayerVector() {
2885}
2886
2887SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002888 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002889}
2890
2891int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2892 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002893{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002894 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002895 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2896 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002897
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002898 uint32_t ls = l->getCurrentState().layerStack;
2899 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002900 if (ls != rs)
2901 return ls - rs;
2902
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002903 uint32_t lz = l->getCurrentState().z;
2904 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002905 if (lz != rz)
2906 return lz - rz;
2907
2908 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002909}
2910
2911// ---------------------------------------------------------------------------
2912
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002913SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2914 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002915}
2916
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002917SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002918 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002919 viewport.makeInvalid();
2920 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002921}
2922
2923// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002925}; // namespace android