blob: b0e400257945987a92010a23a8711e8c0dd21e9d [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();
Mathias Agopian1604f772012-09-18 21:54:42 -0700569 } else {
570 // TODO: where should this value come from?
571 static const int TV_DENSITY = 213;
572 info->density = TV_DENSITY / 160.0f;
573 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700574 }
575
Mathias Agopian1604f772012-09-18 21:54:42 -0700576 info->w = hwc.getWidth(type);
577 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700578 info->xdpi = xdpi;
579 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700580 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700581
582 // All non-virtual displays are currently considered secure.
583 info->secure = true;
584
Mathias Agopian888c8222012-08-04 21:10:38 -0700585 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700586}
587
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800588// ----------------------------------------------------------------------------
589
590sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800591 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700592}
593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800595
596void SurfaceFlinger::waitForEvent() {
597 mEventQueue.waitMessage();
598}
599
600void SurfaceFlinger::signalTransaction() {
601 mEventQueue.invalidate();
602}
603
604void SurfaceFlinger::signalLayerUpdate() {
605 mEventQueue.invalidate();
606}
607
608void SurfaceFlinger::signalRefresh() {
609 mEventQueue.refresh();
610}
611
612status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
613 nsecs_t reltime, uint32_t flags) {
614 return mEventQueue.postMessage(msg, reltime);
615}
616
617status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
618 nsecs_t reltime, uint32_t flags) {
619 status_t res = mEventQueue.postMessage(msg, reltime);
620 if (res == NO_ERROR) {
621 msg->wait();
622 }
623 return res;
624}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625
Mathias Agopian4fec8732012-06-29 14:12:52 -0700626bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 return true;
629}
630
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700631void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700632 if (mEventThread == NULL) {
633 // This is a temporary workaround for b/7145521. A non-null pointer
634 // does not mean EventThread has finished initializing, so this
635 // is not a correct fix.
636 ALOGW("WARNING: EventThread not started, ignoring vsync");
637 return;
638 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700639 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
640 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700641 mEventThread->onVSyncReceived(type, timestamp);
642 }
643}
644
645void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
646 if (mEventThread == NULL) {
647 // This is a temporary workaround for b/7145521. A non-null pointer
648 // does not mean EventThread has finished initializing, so this
649 // is not a correct fix.
650 ALOGW("WARNING: EventThread not started, ignoring hotplug");
651 return;
652 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700653
Mathias Agopian148994e2012-09-19 17:31:36 -0700654 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700655 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800656 if (connected) {
657 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700658 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800659 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
660 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700661 }
662 setTransactionFlags(eDisplayTransactionNeeded);
663
Andy McFadden9e9689c2012-10-10 18:17:51 -0700664 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700665 }
Mathias Agopian86303202012-07-24 22:46:10 -0700666}
667
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700668void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
669 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700670}
671
Mathias Agopian4fec8732012-06-29 14:12:52 -0700672void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800673 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800674 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700675 case MessageQueue::TRANSACTION:
676 handleMessageTransaction();
677 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700678 case MessageQueue::INVALIDATE:
679 handleMessageTransaction();
680 handleMessageInvalidate();
681 signalRefresh();
682 break;
683 case MessageQueue::REFRESH:
684 handleMessageRefresh();
685 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800686 }
687}
688
Mathias Agopian4fec8732012-06-29 14:12:52 -0700689void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700690 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700691 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700692 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700693 }
694}
695
696void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700697 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700698 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700699}
700
701void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700702 ATRACE_CALL();
703 preComposition();
704 rebuildLayerStacks();
705 setUpHWComposer();
706 doDebugFlashRegions();
707 doComposition();
708 postComposition();
709}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700710
Mathias Agopiancd60f992012-08-16 16:28:27 -0700711void SurfaceFlinger::doDebugFlashRegions()
712{
713 // is debugging enabled
714 if (CC_LIKELY(!mDebugRegion))
715 return;
716
717 const bool repaintEverything = mRepaintEverything;
718 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
719 const sp<DisplayDevice>& hw(mDisplays[dpy]);
720 if (hw->canDraw()) {
721 // transform the dirty region into this screen's coordinate space
722 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
723 if (!dirtyRegion.isEmpty()) {
724 // redraw the whole screen
725 doComposeSurfaces(hw, Region(hw->bounds()));
726
727 // and draw the dirty region
728 glDisable(GL_TEXTURE_EXTERNAL_OES);
729 glDisable(GL_TEXTURE_2D);
730 glDisable(GL_BLEND);
731 glColor4f(1, 0, 1, 1);
732 const int32_t height = hw->getHeight();
733 Region::const_iterator it = dirtyRegion.begin();
734 Region::const_iterator const end = dirtyRegion.end();
735 while (it != end) {
736 const Rect& r = *it++;
737 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800738 { (GLfloat) r.left, (GLfloat) (height - r.top) },
739 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
740 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
741 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700742 };
743 glVertexPointer(2, GL_FLOAT, 0, vertices);
744 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
745 }
746 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700747 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700748 }
749 }
750 }
751
752 postFramebuffer();
753
754 if (mDebugRegion > 1) {
755 usleep(mDebugRegion * 1000);
756 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700757
758 HWComposer& hwc(getHwComposer());
759 if (hwc.initCheck() == NO_ERROR) {
760 status_t err = hwc.prepare();
761 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
762 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700763}
764
765void SurfaceFlinger::preComposition()
766{
767 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700768 const LayerVector& layers(mDrawingState.layersSortedByZ);
769 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700771 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700772 needExtraInvalidate = true;
773 }
774 }
775 if (needExtraInvalidate) {
776 signalLayerUpdate();
777 }
778}
779
780void SurfaceFlinger::postComposition()
781{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700782 const LayerVector& layers(mDrawingState.layersSortedByZ);
783 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700784 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700785 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800787
788 if (mAnimCompositionPending) {
789 mAnimCompositionPending = false;
790
791 const HWComposer& hwc = getHwComposer();
792 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800793 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800794 mAnimFrameTracker.setActualPresentFence(presentFence);
795 } else {
796 // The HWC doesn't support present fences, so use the refresh
797 // timestamp instead.
798 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
799 mAnimFrameTracker.setActualPresentTime(presentTime);
800 }
801 mAnimFrameTracker.advanceFrame();
802 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700803}
804
805void SurfaceFlinger::rebuildLayerStacks() {
806 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700807 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700808 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700809 mVisibleRegionsDirty = false;
810 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700811
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700812 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700813 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700814 Region opaqueRegion;
815 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800816 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700817 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700818 const Transform& tr(hw->getTransform());
819 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700820 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700821 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700822 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700823
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700824 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700825 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700826 const sp<Layer>& layer(layers[i]);
827 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700829 Region drawRegion(tr.transform(
830 layer->visibleNonTransparentRegion));
831 drawRegion.andSelf(bounds);
832 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700833 layersSortedByZ.add(layer);
834 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700835 }
836 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700837 }
Mathias Agopian42977342012-08-05 00:40:46 -0700838 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700839 hw->undefinedRegion.set(bounds);
840 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
841 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700842 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700843 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700845
Mathias Agopiancd60f992012-08-16 16:28:27 -0700846void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700847 HWComposer& hwc(getHwComposer());
848 if (hwc.initCheck() == NO_ERROR) {
849 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700850 if (CC_UNLIKELY(mHwWorkListDirty)) {
851 mHwWorkListDirty = false;
852 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
853 sp<const DisplayDevice> hw(mDisplays[dpy]);
854 const int32_t id = hw->getHwcDisplayId();
855 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800856 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700857 hw->getVisibleLayersSortedByZ());
858 const size_t count = currentLayers.size();
859 if (hwc.createWorkList(id, count) == NO_ERROR) {
860 HWComposer::LayerListIterator cur = hwc.begin(id);
861 const HWComposer::LayerListIterator end = hwc.end(id);
862 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700864 layer->setGeometry(hw, *cur);
865 if (mDebugDisableHWC || mDebugRegion) {
866 cur->setSkip(true);
867 }
868 }
869 }
870 }
871 }
872 }
873
874 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700875 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700876 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700877 const int32_t id = hw->getHwcDisplayId();
878 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800879 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700880 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700881 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700882 HWComposer::LayerListIterator cur = hwc.begin(id);
883 const HWComposer::LayerListIterator end = hwc.end(id);
884 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
885 /*
886 * update the per-frame h/w composer data for each layer
887 * and build the transparent region of the FB
888 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700890 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700891 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700892 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700893 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700894
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700895 status_t err = hwc.prepare();
896 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700897
898 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
899 sp<const DisplayDevice> hw(mDisplays[dpy]);
900 hw->prepareFrame(hwc);
901 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700902 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700903}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700904
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905void SurfaceFlinger::doComposition() {
906 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700907 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700908 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700909 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910 if (hw->canDraw()) {
911 // transform the dirty region into this screen's coordinate space
912 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800913
914 // repaint the framebuffer (if needed)
915 doDisplayComposition(hw, dirtyRegion);
916
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917 hw->dirtyRegion.clear();
918 hw->flip(hw->swapRegion);
919 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700920 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700921 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700922 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700923 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700924 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700925}
926
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927void SurfaceFlinger::postFramebuffer()
928{
Mathias Agopian841cde52012-03-01 15:44:37 -0800929 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800930
Mathias Agopiana44b0412011-10-16 18:46:35 -0700931 const nsecs_t now = systemTime();
932 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700933
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700934 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700935 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700936 if (!hwc.supportsFramebufferTarget()) {
937 // EGL spec says:
938 // "surface must be bound to the calling thread's current context,
939 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700940 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700941 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700942 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700943 }
944
Mathias Agopian92a979a2012-08-02 18:32:23 -0700945 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700946 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800947 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700948 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700950 int32_t id = hw->getHwcDisplayId();
951 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700952 HWComposer::LayerListIterator cur = hwc.begin(id);
953 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700954 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700955 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700956 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700957 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700958 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700959 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700960 }
Jesse Hallef194142012-06-14 14:45:17 -0700961 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800962 }
963
Mathias Agopiana44b0412011-10-16 18:46:35 -0700964 mLastSwapBufferTime = systemTime() - now;
965 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700966
967 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
968 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
969 logFrameStats();
970 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971}
972
Mathias Agopian87baae12012-07-31 12:38:26 -0700973void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974{
Mathias Agopian841cde52012-03-01 15:44:37 -0800975 ATRACE_CALL();
976
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700977 // here we keep a copy of the drawing state (that is the state that's
978 // going to be overwritten by handleTransactionLocked()) outside of
979 // mStateLock so that the side-effects of the State assignment
980 // don't happen with mStateLock held (which can cause deadlocks).
981 State drawingState(mDrawingState);
982
Mathias Agopianca4d3602011-05-19 15:38:14 -0700983 Mutex::Autolock _l(mStateLock);
984 const nsecs_t now = systemTime();
985 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986
Mathias Agopianca4d3602011-05-19 15:38:14 -0700987 // Here we're guaranteed that some transaction flags are set
988 // so we can call handleTransactionLocked() unconditionally.
989 // We call getTransactionFlags(), which will also clear the flags,
990 // with mStateLock held to guarantee that mCurrentState won't change
991 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700992
Mathias Agopiane57f2922012-08-09 16:29:12 -0700993 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700994 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700995
Mathias Agopianca4d3602011-05-19 15:38:14 -0700996 mLastTransactionTime = systemTime() - now;
997 mDebugInTransaction = 0;
998 invalidateHwcGeometry();
999 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001000}
1001
Mathias Agopian87baae12012-07-31 12:38:26 -07001002void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001003{
1004 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 const size_t count = currentLayers.size();
1006
1007 /*
1008 * Traversal of the children
1009 * (perform the transaction for each of them if needed)
1010 */
1011
Mathias Agopian3559b072012-08-15 13:46:03 -07001012 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001014 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1016 if (!trFlags) continue;
1017
1018 const uint32_t flags = layer->doTransaction(0);
1019 if (flags & Layer::eVisibleRegion)
1020 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 }
1022 }
1023
1024 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001025 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 */
1027
Mathias Agopiane57f2922012-08-09 16:29:12 -07001028 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001029 // here we take advantage of Vector's copy-on-write semantics to
1030 // improve performance by skipping the transaction entirely when
1031 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001032 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1033 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001034 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001036 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001037 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001038
1039 // find the displays that were removed
1040 // (ie: in drawing state but not in current state)
1041 // also handle displays that changed
1042 // (ie: displays that are in both lists)
1043 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001044 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1045 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001046 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001047 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001048 // Call makeCurrent() on the primary display so we can
1049 // be sure that nothing associated with this display
1050 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001051 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001052 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001053 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1054 if (hw != NULL)
1055 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001056 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1057 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001058 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 } else {
1060 ALOGW("trying to remove the main display");
1061 }
1062 } else {
1063 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001064 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001065 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001066 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001067 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001068 // recreating the DisplayDevice, so we just remove it
1069 // from the drawing state, so that it get re-added
1070 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001071 sp<DisplayDevice> hw(getDisplayDevice(display));
1072 if (hw != NULL)
1073 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001074 mDisplays.removeItem(display);
1075 mDrawingState.displays.removeItemsAt(i);
1076 dc--; i--;
1077 // at this point we must loop to the next item
1078 continue;
1079 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001080
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001081 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001082 if (disp != NULL) {
1083 if (state.layerStack != draw[i].layerStack) {
1084 disp->setLayerStack(state.layerStack);
1085 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001086 if ((state.orientation != draw[i].orientation)
1087 || (state.viewport != draw[i].viewport)
1088 || (state.frame != draw[i].frame))
1089 {
1090 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001091 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001092 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001093 }
1094 }
1095 }
1096
1097 // find displays that were added
1098 // (ie: in current state but not in drawing state)
1099 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001100 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001101 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001102
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001103 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001104 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001105 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001106 // Virtual displays without a surface are dormant:
1107 // they have external state (layer stack, projection,
1108 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001109 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001110 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001111 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001112 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001113 state.displayName);
1114 }
1115 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001116 ALOGE_IF(state.surface!=NULL,
1117 "adding a supported display, but rendering "
1118 "surface is provided (%p), ignoring it",
1119 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001120 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001121 // for supported (by hwc) displays we provide our
1122 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001123 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001124 }
1125
1126 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001127 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001128 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001129 state.type, hwcDisplayId, state.isSecure,
1130 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001131 hw->setLayerStack(state.layerStack);
1132 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001133 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001134 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001135 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001136 if (state.isVirtualDisplay()) {
1137 if (hwcDisplayId >= 0) {
1138 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1139 hw->getWidth(), hw->getHeight(),
1140 hw->getFormat());
1141 }
1142 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001143 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001144 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001145 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001146 }
1147 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150
Mathias Agopian84300952012-11-21 16:02:13 -08001151 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1152 // The transform hint might have changed for some layers
1153 // (either because a display has changed, or because a layer
1154 // as changed).
1155 //
1156 // Walk through all the layers in currentLayers,
1157 // and update their transform hint.
1158 //
1159 // If a layer is visible only on a single display, then that
1160 // display is used to calculate the hint, otherwise we use the
1161 // default display.
1162 //
1163 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1164 // the hint is set before we acquire a buffer from the surface texture.
1165 //
1166 // NOTE: layer transactions have taken place already, so we use their
1167 // drawing state. However, SurfaceFlinger's own transaction has not
1168 // happened yet, so we must use the current state layer list
1169 // (soon to become the drawing state list).
1170 //
1171 sp<const DisplayDevice> disp;
1172 uint32_t currentlayerStack = 0;
1173 for (size_t i=0; i<count; i++) {
1174 // NOTE: we rely on the fact that layers are sorted by
1175 // layerStack first (so we don't have to traverse the list
1176 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001178 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001179 if (i==0 || currentlayerStack != layerStack) {
1180 currentlayerStack = layerStack;
1181 // figure out if this layerstack is mirrored
1182 // (more than one display) if so, pick the default display,
1183 // if not, pick the only display it's on.
1184 disp.clear();
1185 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1186 sp<const DisplayDevice> hw(mDisplays[dpy]);
1187 if (hw->getLayerStack() == currentlayerStack) {
1188 if (disp == NULL) {
1189 disp = hw;
1190 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001191 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001192 break;
1193 }
1194 }
1195 }
1196 }
Chet Haase91d25932013-04-11 15:24:55 -07001197 if (disp == NULL) {
1198 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1199 // redraw after transform hint changes. See bug 8508397.
1200
1201 // could be null when this layer is using a layerStack
1202 // that is not visible on any display. Also can occur at
1203 // screen off/on times.
1204 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001205 }
Chet Haase91d25932013-04-11 15:24:55 -07001206 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001207 }
1208 }
1209
1210
Mathias Agopian3559b072012-08-15 13:46:03 -07001211 /*
1212 * Perform our own transaction if needed
1213 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001214
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001215 const LayerVector& layers(mDrawingState.layersSortedByZ);
1216 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001217 // layers have been added
1218 mVisibleRegionsDirty = true;
1219 }
1220
1221 // some layers might have been removed, so
1222 // we need to update the regions they're exposing.
1223 if (mLayersRemoved) {
1224 mLayersRemoved = false;
1225 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001226 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001227 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001228 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001229 if (currentLayers.indexOf(layer) < 0) {
1230 // this layer is not visible anymore
1231 // TODO: we could traverse the tree from front to back and
1232 // compute the actual visible region
1233 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001234 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001235 Region visibleReg = s.transform.transform(
1236 Region(Rect(s.active.w, s.active.h)));
1237 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001238 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 }
1241
1242 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001243}
1244
1245void SurfaceFlinger::commitTransaction()
1246{
1247 if (!mLayersPendingRemoval.isEmpty()) {
1248 // Notify removed layers now that they can't be drawn from
1249 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1250 mLayersPendingRemoval[i]->onRemoved();
1251 }
1252 mLayersPendingRemoval.clear();
1253 }
1254
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001255 // If this transaction is part of a window animation then the next frame
1256 // we composite should be considered an animation as well.
1257 mAnimCompositionPending = mAnimTransactionPending;
1258
Mathias Agopian4fec8732012-06-29 14:12:52 -07001259 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001260 mTransactionPending = false;
1261 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001262 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263}
1264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001266 const LayerVector& currentLayers, uint32_t layerStack,
1267 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268{
Mathias Agopian841cde52012-03-01 15:44:37 -08001269 ATRACE_CALL();
1270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 Region aboveOpaqueLayers;
1272 Region aboveCoveredLayers;
1273 Region dirty;
1274
Mathias Agopian87baae12012-07-31 12:38:26 -07001275 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276
1277 size_t i = currentLayers.size();
1278 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001279 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280
1281 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001282 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283
Jesse Hall01e29052013-02-19 16:13:35 -08001284 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001285 if (s.layerStack != layerStack)
1286 continue;
1287
Mathias Agopianab028732010-03-16 16:41:46 -07001288 /*
1289 * opaqueRegion: area of a surface that is fully opaque.
1290 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001292
1293 /*
1294 * visibleRegion: area of a surface that is visible on screen
1295 * and not fully transparent. This is essentially the layer's
1296 * footprint minus the opaque regions above it.
1297 * Areas covered by a translucent surface are considered visible.
1298 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001300
1301 /*
1302 * coveredRegion: area of a surface that is covered by all
1303 * visible regions above it (which includes the translucent areas).
1304 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001306
Jesse Halla8026d22012-09-25 13:25:04 -07001307 /*
1308 * transparentRegion: area of a surface that is hinted to be completely
1309 * transparent. This is only used to tell when the layer has no visible
1310 * non-transparent regions and can be removed from the layer list. It
1311 * does not affect the visibleRegion of this layer or any layers
1312 * beneath it. The hint may not be correct if apps don't respect the
1313 * SurfaceView restrictions (which, sadly, some don't).
1314 */
1315 Region transparentRegion;
1316
Mathias Agopianab028732010-03-16 16:41:46 -07001317
1318 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001319 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001320 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001321 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001323 if (!visibleRegion.isEmpty()) {
1324 // Remove the transparent area from the visible region
1325 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001326 const Transform tr(s.transform);
1327 if (tr.transformed()) {
1328 if (tr.preserveRects()) {
1329 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001330 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001331 } else {
1332 // transformation too complex, can't do the
1333 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001334 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001335 }
1336 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001337 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001338 }
Mathias Agopianab028732010-03-16 16:41:46 -07001339 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340
Mathias Agopianab028732010-03-16 16:41:46 -07001341 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001342 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001343 if (s.alpha==255 && !translucent &&
1344 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1345 // the opaque region is the layer's footprint
1346 opaqueRegion = visibleRegion;
1347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 }
1349 }
1350
Mathias Agopianab028732010-03-16 16:41:46 -07001351 // Clip the covered region to the visible region
1352 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1353
1354 // Update aboveCoveredLayers for next (lower) layer
1355 aboveCoveredLayers.orSelf(visibleRegion);
1356
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 // subtract the opaque region covered by the layers above us
1358 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359
1360 // compute this layer's dirty region
1361 if (layer->contentDirty) {
1362 // we need to invalidate the whole region
1363 dirty = visibleRegion;
1364 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001365 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 layer->contentDirty = false;
1367 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001368 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001369 * the exposed region consists of two components:
1370 * 1) what's VISIBLE now and was COVERED before
1371 * 2) what's EXPOSED now less what was EXPOSED before
1372 *
1373 * note that (1) is conservative, we start with the whole
1374 * visible region but only keep what used to be covered by
1375 * something -- which mean it may have been exposed.
1376 *
1377 * (2) handles areas that were not covered by anything but got
1378 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001379 */
Mathias Agopianab028732010-03-16 16:41:46 -07001380 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001381 const Region oldVisibleRegion = layer->visibleRegion;
1382 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001383 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1384 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 }
1386 dirty.subtractSelf(aboveOpaqueLayers);
1387
1388 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001389 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390
Mathias Agopianab028732010-03-16 16:41:46 -07001391 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001393
Jesse Halla8026d22012-09-25 13:25:04 -07001394 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 layer->setVisibleRegion(visibleRegion);
1396 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001397 layer->setVisibleNonTransparentRegion(
1398 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 }
1400
Mathias Agopian87baae12012-07-31 12:38:26 -07001401 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402}
1403
Mathias Agopian87baae12012-07-31 12:38:26 -07001404void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1405 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001406 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001407 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1408 if (hw->getLayerStack() == layerStack) {
1409 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001410 }
1411 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001412}
1413
1414void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001416 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417
Mathias Agopian4fec8732012-06-29 14:12:52 -07001418 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 const LayerVector& layers(mDrawingState.layersSortedByZ);
1420 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001421 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001422 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001423 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001424 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001425 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001426 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001427
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001428 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429}
1430
Mathias Agopianad456f92011-01-13 17:53:01 -08001431void SurfaceFlinger::invalidateHwcGeometry()
1432{
1433 mHwWorkListDirty = true;
1434}
1435
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001436
Mathias Agopiancd60f992012-08-16 16:28:27 -07001437void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001438 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
Mathias Agopian87baae12012-07-31 12:38:26 -07001440 Region dirtyRegion(inDirtyRegion);
1441
Mathias Agopianb8a55602009-06-26 19:06:36 -07001442 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001443 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444
Mathias Agopian42977342012-08-05 00:40:46 -07001445 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001446 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001447 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1448 // takes a rectangle, we must make sure to update that whole
1449 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001450 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001451 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001452 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001453 // We need to redraw the rectangle that will be updated
1454 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001455 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001456 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001457 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001458 } else {
1459 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001460 dirtyRegion.set(hw->bounds());
1461 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 }
1463 }
1464
Mathias Agopiancd60f992012-08-16 16:28:27 -07001465 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001467 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001468 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001469
1470 // swap buffers (presentation)
1471 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472}
1473
Mathias Agopiancd60f992012-08-16 16:28:27 -07001474void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001475{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001476 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001477 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001478 HWComposer::LayerListIterator cur = hwc.begin(id);
1479 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001480
Mathias Agopian85d751c2012-08-29 16:59:24 -07001481 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1482 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001483 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001484 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1485 hw->getDisplayName().string());
1486 return;
1487 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001488
1489 // set the frame buffer
1490 glMatrixMode(GL_MODELVIEW);
1491 glLoadIdentity();
1492
1493 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001494 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001495 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001496 // when using overlays, we assume a fully transparent framebuffer
1497 // NOTE: we could reduce how much we need to clear, for instance
1498 // remove where there are opaque FB layers. however, on some
1499 // GPUs doing a "clean slate" glClear might be more efficient.
1500 // We'll revisit later if needed.
1501 glClearColor(0, 0, 0, 0);
1502 glClear(GL_COLOR_BUFFER_BIT);
1503 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001504 // we start with the whole screen area
1505 const Region bounds(hw->getBounds());
1506
1507 // we remove the scissor part
1508 // we're left with the letterbox region
1509 // (common case is that letterbox ends-up being empty)
1510 const Region letterbox(bounds.subtract(hw->getScissor()));
1511
1512 // compute the area to clear
1513 Region region(hw->undefinedRegion.merge(letterbox));
1514
1515 // but limit it to the dirty region
1516 region.andSelf(dirty);
1517
Mathias Agopianb9494d52012-04-18 02:28:45 -07001518 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001519 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001520 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001521 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001522 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001523 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001524
Mathias Agopian766dc492012-10-30 18:08:06 -07001525 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1526 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001527 // scissor on the main display. It should never be needed
1528 // anyways (though in theory it could since the API allows it).
1529 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001530 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001531 if (scissor != bounds) {
1532 // scissor doesn't match the screen's dimensions, so we
1533 // need to clear everything outside of it and enable
1534 // the GL scissor so we don't draw anything where we shouldn't
1535 const GLint height = hw->getHeight();
1536 glScissor(scissor.left, height - scissor.bottom,
1537 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001538 // enable scissor for this frame
1539 glEnable(GL_SCISSOR_TEST);
1540 }
1541 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001542 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001543
Mathias Agopian85d751c2012-08-29 16:59:24 -07001544 /*
1545 * and then, render the layers targeted at the framebuffer
1546 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001547
Mathias Agopian13127d82013-03-05 17:47:11 -08001548 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001549 const size_t count = layers.size();
1550 const Transform& tr = hw->getTransform();
1551 if (cur != end) {
1552 // we're using h/w composer
1553 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001554 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001555 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001556 if (!clip.isEmpty()) {
1557 switch (cur->getCompositionType()) {
1558 case HWC_OVERLAY: {
1559 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1560 && i
1561 && layer->isOpaque()
1562 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001563 // never clear the very first layer since we're
1564 // guaranteed the FB is already cleared
1565 layer->clearWithOpenGL(hw, clip);
1566 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001567 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001568 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 case HWC_FRAMEBUFFER: {
1570 layer->draw(hw, clip);
1571 break;
1572 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001573 case HWC_FRAMEBUFFER_TARGET: {
1574 // this should not happen as the iterator shouldn't
1575 // let us get there.
1576 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1577 break;
1578 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001579 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001580 }
1581 layer->setAcquireFence(hw, *cur);
1582 }
1583 } else {
1584 // we're not using h/w composer
1585 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001586 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001587 const Region clip(dirty.intersect(
1588 tr.transform(layer->visibleRegion)));
1589 if (!clip.isEmpty()) {
1590 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001591 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001592 }
1593 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001594
1595 // disable scissor at the end of the frame
1596 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597}
1598
Mathias Agopian55801e42012-08-27 18:54:24 -07001599void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1600 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001602 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001603 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001604 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001605 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001606
Mathias Agopian55801e42012-08-27 18:54:24 -07001607 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001608 Region::const_iterator it = region.begin();
1609 Region::const_iterator const end = region.end();
1610 while (it != end) {
1611 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001612 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001613 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1614 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1615 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1616 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001617 };
1618 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001619 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1620 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
1622
Mathias Agopianac9fa422013-02-11 16:40:36 -08001623void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1624 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001625 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001626 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001627{
Mathias Agopian96f08192010-06-02 23:28:45 -07001628 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001629 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001630
Mathias Agopian96f08192010-06-02 23:28:45 -07001631 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001632 Mutex::Autolock _l(mStateLock);
1633 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001634 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001635}
1636
Mathias Agopian13127d82013-03-05 17:47:11 -08001637status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638{
1639 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001640 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001642 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001643 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001644 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 return NO_ERROR;
1646 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001647 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648}
1649
Mathias Agopiandea20b12011-05-03 17:04:02 -07001650uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1651{
1652 return android_atomic_release_load(&mTransactionFlags);
1653}
1654
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1656{
1657 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1658}
1659
Mathias Agopianbb641242010-05-18 17:06:55 -07001660uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661{
1662 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1663 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001664 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665 }
1666 return old;
1667}
1668
Mathias Agopian8b33f032012-07-24 20:43:54 -07001669void SurfaceFlinger::setTransactionState(
1670 const Vector<ComposerState>& state,
1671 const Vector<DisplayState>& displays,
1672 uint32_t flags)
1673{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001674 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001675 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001676 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001677
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001678 if (flags & eAnimation) {
1679 // For window updates that are part of an animation we must wait for
1680 // previous animation "frames" to be handled.
1681 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001682 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001683 if (CC_UNLIKELY(err != NO_ERROR)) {
1684 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001685 // caller after a few seconds.
1686 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1687 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001688 mAnimTransactionPending = false;
1689 break;
1690 }
1691 }
1692 }
1693
Mathias Agopiane57f2922012-08-09 16:29:12 -07001694 size_t count = displays.size();
1695 for (size_t i=0 ; i<count ; i++) {
1696 const DisplayState& s(displays[i]);
1697 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001698 }
1699
Mathias Agopiane57f2922012-08-09 16:29:12 -07001700 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001701 for (size_t i=0 ; i<count ; i++) {
1702 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001703 // Here we need to check that the interface we're given is indeed
1704 // one of our own. A malicious client could give us a NULL
1705 // IInterface, or one of its own or even one of our own but a
1706 // different type. All these situations would cause us to crash.
1707 //
1708 // NOTE: it would be better to use RTTI as we could directly check
1709 // that we have a Client*. however, RTTI is disabled in Android.
1710 if (s.client != NULL) {
1711 sp<IBinder> binder = s.client->asBinder();
1712 if (binder != NULL) {
1713 String16 desc(binder->getInterfaceDescriptor());
1714 if (desc == ISurfaceComposerClient::descriptor) {
1715 sp<Client> client( static_cast<Client *>(s.client.get()) );
1716 transactionFlags |= setClientStateLocked(client, s.state);
1717 }
1718 }
1719 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001720 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001721
Mathias Agopian386aa982011-11-07 21:58:03 -08001722 if (transactionFlags) {
1723 // this triggers the transaction
1724 setTransactionFlags(transactionFlags);
1725
1726 // if this is a synchronous transaction, wait for it to take effect
1727 // before returning.
1728 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001729 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001730 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001731 if (flags & eAnimation) {
1732 mAnimTransactionPending = true;
1733 }
1734 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001735 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1736 if (CC_UNLIKELY(err != NO_ERROR)) {
1737 // just in case something goes wrong in SF, return to the
1738 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001739 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1740 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001741 break;
1742 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001743 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 }
1745}
1746
Mathias Agopiane57f2922012-08-09 16:29:12 -07001747uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1748{
Jesse Hall9a143922012-10-04 16:29:19 -07001749 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1750 if (dpyIdx < 0)
1751 return 0;
1752
Mathias Agopiane57f2922012-08-09 16:29:12 -07001753 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001754 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001755 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001756 const uint32_t what = s.what;
1757 if (what & DisplayState::eSurfaceChanged) {
1758 if (disp.surface->asBinder() != s.surface->asBinder()) {
1759 disp.surface = s.surface;
1760 flags |= eDisplayTransactionNeeded;
1761 }
1762 }
1763 if (what & DisplayState::eLayerStackChanged) {
1764 if (disp.layerStack != s.layerStack) {
1765 disp.layerStack = s.layerStack;
1766 flags |= eDisplayTransactionNeeded;
1767 }
1768 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001769 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001770 if (disp.orientation != s.orientation) {
1771 disp.orientation = s.orientation;
1772 flags |= eDisplayTransactionNeeded;
1773 }
1774 if (disp.frame != s.frame) {
1775 disp.frame = s.frame;
1776 flags |= eDisplayTransactionNeeded;
1777 }
1778 if (disp.viewport != s.viewport) {
1779 disp.viewport = s.viewport;
1780 flags |= eDisplayTransactionNeeded;
1781 }
1782 }
1783 }
1784 return flags;
1785}
1786
1787uint32_t SurfaceFlinger::setClientStateLocked(
1788 const sp<Client>& client,
1789 const layer_state_t& s)
1790{
1791 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001792 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001793 if (layer != 0) {
1794 const uint32_t what = s.what;
1795 if (what & layer_state_t::ePositionChanged) {
1796 if (layer->setPosition(s.x, s.y))
1797 flags |= eTraversalNeeded;
1798 }
1799 if (what & layer_state_t::eLayerChanged) {
1800 // NOTE: index needs to be calculated before we update the state
1801 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1802 if (layer->setLayer(s.z)) {
1803 mCurrentState.layersSortedByZ.removeAt(idx);
1804 mCurrentState.layersSortedByZ.add(layer);
1805 // we need traversal (state changed)
1806 // AND transaction (list changed)
1807 flags |= eTransactionNeeded|eTraversalNeeded;
1808 }
1809 }
1810 if (what & layer_state_t::eSizeChanged) {
1811 if (layer->setSize(s.w, s.h)) {
1812 flags |= eTraversalNeeded;
1813 }
1814 }
1815 if (what & layer_state_t::eAlphaChanged) {
1816 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1817 flags |= eTraversalNeeded;
1818 }
1819 if (what & layer_state_t::eMatrixChanged) {
1820 if (layer->setMatrix(s.matrix))
1821 flags |= eTraversalNeeded;
1822 }
1823 if (what & layer_state_t::eTransparentRegionChanged) {
1824 if (layer->setTransparentRegionHint(s.transparentRegion))
1825 flags |= eTraversalNeeded;
1826 }
1827 if (what & layer_state_t::eVisibilityChanged) {
1828 if (layer->setFlags(s.flags, s.mask))
1829 flags |= eTraversalNeeded;
1830 }
1831 if (what & layer_state_t::eCropChanged) {
1832 if (layer->setCrop(s.crop))
1833 flags |= eTraversalNeeded;
1834 }
1835 if (what & layer_state_t::eLayerStackChanged) {
1836 // NOTE: index needs to be calculated before we update the state
1837 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1838 if (layer->setLayerStack(s.layerStack)) {
1839 mCurrentState.layersSortedByZ.removeAt(idx);
1840 mCurrentState.layersSortedByZ.add(layer);
1841 // we need traversal (state changed)
1842 // AND transaction (list changed)
1843 flags |= eTransactionNeeded|eTraversalNeeded;
1844 }
1845 }
1846 }
1847 return flags;
1848}
1849
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001850status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001851 const String8& name,
1852 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001853 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1854 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001856 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001857 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001858 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001859 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001860 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001861 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001862
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001863 status_t result = NO_ERROR;
1864
1865 sp<Layer> layer;
1866
Mathias Agopian3165cc22012-08-08 19:42:09 -07001867 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1868 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001869 result = createNormalLayer(client,
1870 name, w, h, flags, format,
1871 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001873 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001874 result = createDimLayer(client,
1875 name, w, h, flags,
1876 handle, gbp, &layer);
1877 break;
1878 default:
1879 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 break;
1881 }
1882
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001883 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001884 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001885 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001886 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001887 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888}
1889
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001890status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1891 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1892 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893{
1894 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001895 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896 case PIXEL_FORMAT_TRANSPARENT:
1897 case PIXEL_FORMAT_TRANSLUCENT:
1898 format = PIXEL_FORMAT_RGBA_8888;
1899 break;
1900 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001901#ifdef NO_RGBX_8888
1902 format = PIXEL_FORMAT_RGB_565;
1903#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001904 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001905#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001906 break;
1907 }
1908
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001909#ifdef NO_RGBX_8888
1910 if (format == PIXEL_FORMAT_RGBX_8888)
1911 format = PIXEL_FORMAT_RGBA_8888;
1912#endif
1913
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 *outLayer = new Layer(this, client, name, w, h, flags);
1915 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1916 if (err == NO_ERROR) {
1917 *handle = (*outLayer)->getHandle();
1918 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001920
1921 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1922 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001923}
1924
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001925status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1926 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1927 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001929 *outLayer = new LayerDim(this, client, name, w, h, flags);
1930 *handle = (*outLayer)->getHandle();
1931 *gbp = (*outLayer)->getBufferQueue();
1932 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001933}
1934
Mathias Agopianac9fa422013-02-11 16:40:36 -08001935status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936{
Mathias Agopian67106042013-03-14 19:18:13 -07001937 // called by the window manager when it wants to remove a Layer
1938 status_t err = NO_ERROR;
1939 sp<Layer> l(client->getLayerUser(handle));
1940 if (l != NULL) {
1941 err = removeLayer(l);
1942 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1943 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001944 }
1945 return err;
1946}
1947
Mathias Agopian13127d82013-03-05 17:47:11 -08001948status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001949{
Mathias Agopian67106042013-03-14 19:18:13 -07001950 // called by ~LayerCleaner() when all references to the IBinder (handle)
1951 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001952 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001953 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001954 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001955 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001956 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001957 "error removing layer=%p (%s)", l.get(), strerror(-err));
1958 }
1959 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960}
1961
Mathias Agopianb60314a2012-04-10 22:09:54 -07001962// ---------------------------------------------------------------------------
1963
Andy McFadden13a082e2012-08-24 10:16:42 -07001964void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001965 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001966 Vector<ComposerState> state;
1967 Vector<DisplayState> displays;
1968 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001969 d.what = DisplayState::eDisplayProjectionChanged |
1970 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001971 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001972 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001973 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001974 d.frame.makeInvalid();
1975 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001976 displays.add(d);
1977 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001978 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001979
1980 const nsecs_t period =
1981 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1982 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001983}
1984
1985void SurfaceFlinger::initializeDisplays() {
1986 class MessageScreenInitialized : public MessageBase {
1987 SurfaceFlinger* flinger;
1988 public:
1989 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1990 virtual bool handler() {
1991 flinger->onInitializeDisplays();
1992 return true;
1993 }
1994 };
1995 sp<MessageBase> msg = new MessageScreenInitialized(this);
1996 postMessageAsync(msg); // we may be called from main thread, use async message
1997}
1998
1999
Mathias Agopiancde87a32012-09-13 14:09:01 -07002000void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002001 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2002 if (hw->isScreenAcquired()) {
2003 // this is expected, e.g. when power manager wakes up during boot
2004 ALOGD(" screen was previously acquired");
2005 return;
2006 }
2007
Mathias Agopian42977342012-08-05 00:40:46 -07002008 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002009 int32_t type = hw->getDisplayType();
2010 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2011 // built-in display, tell the HWC
2012 getHwComposer().acquire(type);
2013
2014 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2015 // FIXME: eventthread only knows about the main display right now
2016 mEventThread->onScreenAcquired();
2017 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002018 }
Mathias Agopian20128302012-08-13 18:32:13 -07002019 mVisibleRegionsDirty = true;
2020 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002021}
2022
Mathias Agopiancde87a32012-09-13 14:09:01 -07002023void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002024 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2025 if (!hw->isScreenAcquired()) {
2026 ALOGD(" screen was previously released");
2027 return;
2028 }
2029
2030 hw->releaseScreen();
2031 int32_t type = hw->getDisplayType();
2032 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2033 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002034 // FIXME: eventthread only knows about the main display right now
2035 mEventThread->onScreenReleased();
2036 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002037
2038 // built-in display, tell the HWC
2039 getHwComposer().release(type);
2040 }
2041 mVisibleRegionsDirty = true;
2042 // from this point on, SF will stop drawing on this display
2043}
2044
2045void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2046 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002047 SurfaceFlinger& mFlinger;
2048 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002049 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002050 MessageScreenAcquired(SurfaceFlinger& flinger,
2051 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002052 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002053 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2054 if (hw == NULL) {
2055 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2056 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2057 ALOGW("Attempt to unblank virtual display");
2058 } else {
2059 mFlinger.onScreenAcquired(hw);
2060 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002061 return true;
2062 }
2063 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002064 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2065 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066}
2067
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002068void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002069 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002070 SurfaceFlinger& mFlinger;
2071 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002072 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002073 MessageScreenReleased(SurfaceFlinger& flinger,
2074 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002075 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002076 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2077 if (hw == NULL) {
2078 ALOGE("Attempt to blank null display %p", mDisplay.get());
2079 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2080 ALOGW("Attempt to blank virtual display");
2081 } else {
2082 mFlinger.onScreenReleased(hw);
2083 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002084 return true;
2085 }
2086 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002087 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2088 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002089}
2090
2091// ---------------------------------------------------------------------------
2092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2094{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002096
Mathias Agopianbd115332013-04-18 16:41:04 -07002097 IPCThreadState* ipc = IPCThreadState::self();
2098 const int pid = ipc->getCallingPid();
2099 const int uid = ipc->getCallingUid();
2100 if ((uid != AID_SHELL) &&
2101 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002102 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002103 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002104 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002105 // Try to get the main lock, but don't insist if we can't
2106 // (this would indicate SF is stuck, but we want to be able to
2107 // print something in dumpsys).
2108 int retry = 3;
2109 while (mStateLock.tryLock()<0 && --retry>=0) {
2110 usleep(1000000);
2111 }
2112 const bool locked(retry >= 0);
2113 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002114 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002115 "SurfaceFlinger appears to be unresponsive, "
2116 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002117 }
2118
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002119 bool dumpAll = true;
2120 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002121 size_t numArgs = args.size();
2122 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002123 if ((index < numArgs) &&
2124 (args[index] == String16("--list"))) {
2125 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002126 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002127 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002128 }
2129
2130 if ((index < numArgs) &&
2131 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002132 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002133 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002134 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002135 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002136
2137 if ((index < numArgs) &&
2138 (args[index] == String16("--latency-clear"))) {
2139 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002140 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002141 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002142 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002143 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002144
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002145 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002146 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002147 }
2148
Mathias Agopian9795c422009-08-26 16:36:26 -07002149 if (locked) {
2150 mStateLock.unlock();
2151 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002152 }
2153 write(fd, result.string(), result.size());
2154 return NO_ERROR;
2155}
2156
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002157void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002158 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002159{
2160 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2161 const size_t count = currentLayers.size();
2162 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002163 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002164 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002165 }
2166}
2167
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002168void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002169 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002170{
2171 String8 name;
2172 if (index < args.size()) {
2173 name = String8(args[index]);
2174 index++;
2175 }
2176
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002177 const nsecs_t period =
2178 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2179 result.appendFormat("%lld\n", period);
2180
2181 if (name.isEmpty()) {
2182 mAnimFrameTracker.dump(result);
2183 } else {
2184 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2185 const size_t count = currentLayers.size();
2186 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002187 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002188 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002189 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002190 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002191 }
2192 }
2193}
2194
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002196 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002197{
2198 String8 name;
2199 if (index < args.size()) {
2200 name = String8(args[index]);
2201 index++;
2202 }
2203
2204 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2205 const size_t count = currentLayers.size();
2206 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002207 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002208 if (name.isEmpty() || (name == layer->getName())) {
2209 layer->clearStats();
2210 }
2211 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002212
2213 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002214}
2215
Jamie Gennis6547ff42013-07-16 20:12:42 -07002216// This should only be called from the main thread. Otherwise it would need
2217// the lock and should use mCurrentState rather than mDrawingState.
2218void SurfaceFlinger::logFrameStats() {
2219 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2220 const size_t count = drawingLayers.size();
2221 for (size_t i=0 ; i<count ; i++) {
2222 const sp<Layer>& layer(drawingLayers[i]);
2223 layer->logFrameStats();
2224 }
2225
2226 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2227}
2228
Andy McFadden4803b742012-09-24 19:07:20 -07002229/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2230{
2231 static const char* config =
2232 " [sf"
2233#ifdef NO_RGBX_8888
2234 " NO_RGBX_8888"
2235#endif
2236#ifdef HAS_CONTEXT_PRIORITY
2237 " HAS_CONTEXT_PRIORITY"
2238#endif
2239#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2240 " NEVER_DEFAULT_TO_ASYNC_MODE"
2241#endif
2242#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2243 " TARGET_DISABLE_TRIPLE_BUFFERING"
2244#endif
2245 "]";
2246 result.append(config);
2247}
2248
Mathias Agopian74d211a2013-04-22 16:55:35 +02002249void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2250 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002251{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002252 bool colorize = false;
2253 if (index < args.size()
2254 && (args[index] == String16("--color"))) {
2255 colorize = true;
2256 index++;
2257 }
2258
2259 Colorizer colorizer(colorize);
2260
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002261 // figure out if we're stuck somewhere
2262 const nsecs_t now = systemTime();
2263 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2264 const nsecs_t inTransaction(mDebugInTransaction);
2265 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2266 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2267
2268 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002269 * Dump library configuration.
2270 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002271
2272 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002273 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002274 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002275 appendSfConfigString(result);
2276 appendUiConfigString(result);
2277 appendGuiConfigString(result);
2278 result.append("\n");
2279
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002280 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002281 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002282 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002283 result.append(SyncFeatures::getInstance().toString());
2284 result.append("\n");
2285
Andy McFadden4803b742012-09-24 19:07:20 -07002286 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002287 * Dump the visible layer list
2288 */
2289 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2290 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002291 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002292 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002293 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002294 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002295 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002297 }
2298
2299 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002300 * Dump Display state
2301 */
2302
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002303 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002304 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002305 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002306 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2307 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002308 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002309 }
2310
2311 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312 * Dump SurfaceFlinger global state
2313 */
2314
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002315 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002316 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002317 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318
Mathias Agopian888c8222012-08-04 21:10:38 -07002319 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002320 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002321
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002322 colorizer.bold(result);
2323 result.appendFormat("EGL implementation : %s\n",
2324 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2325 colorizer.reset(result);
2326 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002327 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002328
Mathias Agopian875d8e12013-06-07 15:35:48 -07002329 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002330
Mathias Agopian42977342012-08-05 00:40:46 -07002331 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002332 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002333 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002334 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335 " last eglSwapBuffers() time: %f us\n"
2336 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002337 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002338 " refresh-rate : %f fps\n"
2339 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002340 " y-dpi : %f\n"
2341 " EGL_NATIVE_VISUAL_ID : %d\n"
2342 " gpu_to_cpu_unsupported : %d\n"
2343 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002344 mLastSwapBufferTime/1000.0,
2345 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002346 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002347 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2348 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002349 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2350 mEGLNativeVisualId,
2351 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352
Mathias Agopian74d211a2013-04-22 16:55:35 +02002353 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002355
Mathias Agopian74d211a2013-04-22 16:55:35 +02002356 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358
2359 /*
2360 * VSYNC state
2361 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002362 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002363
2364 /*
2365 * Dump HWComposer state
2366 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002367 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002368 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002369 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002370 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002371 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2372 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002373 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374
2375 /*
2376 * Dump gralloc state
2377 */
2378 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2379 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002380}
2381
Mathias Agopian13127d82013-03-05 17:47:11 -08002382const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002383SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002384 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002385 wp<IBinder> dpy;
2386 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2387 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2388 dpy = mDisplays.keyAt(i);
2389 break;
2390 }
2391 }
2392 if (dpy == NULL) {
2393 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2394 // Just use the primary display so we have something to return
2395 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2396 }
2397 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002398}
2399
Keun young Park63f165f2012-08-31 10:53:36 -07002400bool SurfaceFlinger::startDdmConnection()
2401{
2402 void* libddmconnection_dso =
2403 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2404 if (!libddmconnection_dso) {
2405 return false;
2406 }
2407 void (*DdmConnection_start)(const char* name);
2408 DdmConnection_start =
2409 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2410 if (!DdmConnection_start) {
2411 dlclose(libddmconnection_dso);
2412 return false;
2413 }
2414 (*DdmConnection_start)(getServiceName());
2415 return true;
2416}
2417
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002418status_t SurfaceFlinger::onTransact(
2419 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2420{
2421 switch (code) {
2422 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002423 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002424 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002425 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002426 case BLANK:
2427 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002428 {
2429 // codes that require permission check
2430 IPCThreadState* ipc = IPCThreadState::self();
2431 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002432 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002433 if ((uid != AID_GRAPHICS) &&
2434 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002435 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002436 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2437 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002438 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002439 break;
2440 }
2441 case CAPTURE_SCREEN:
2442 {
2443 // codes that require permission check
2444 IPCThreadState* ipc = IPCThreadState::self();
2445 const int pid = ipc->getCallingPid();
2446 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002447 if ((uid != AID_GRAPHICS) &&
2448 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002449 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002450 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2451 return PERMISSION_DENIED;
2452 }
2453 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454 }
2455 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2458 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002459 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002460 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002461 IPCThreadState* ipc = IPCThreadState::self();
2462 const int pid = ipc->getCallingPid();
2463 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002464 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002465 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002466 return PERMISSION_DENIED;
2467 }
2468 int n;
2469 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002470 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002471 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472 return NO_ERROR;
2473 case 1002: // SHOW_UPDATES
2474 n = data.readInt32();
2475 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002476 invalidateHwcGeometry();
2477 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002478 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002480 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002481 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002483 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002484 setTransactionFlags(
2485 eTransactionNeeded|
2486 eDisplayTransactionNeeded|
2487 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002488 return NO_ERROR;
2489 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002490 case 1006:{ // send empty update
2491 signalRefresh();
2492 return NO_ERROR;
2493 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002494 case 1008: // toggle use of hw composer
2495 n = data.readInt32();
2496 mDebugDisableHWC = n ? 1 : 0;
2497 invalidateHwcGeometry();
2498 repaintEverything();
2499 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002500 case 1009: // toggle use of transform hint
2501 n = data.readInt32();
2502 mDebugDisableTransformHint = n ? 1 : 0;
2503 invalidateHwcGeometry();
2504 repaintEverything();
2505 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002507 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 reply->writeInt32(0);
2509 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002510 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002511 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512 return NO_ERROR;
2513 case 1013: {
2514 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002515 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2516 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517 }
2518 return NO_ERROR;
2519 }
2520 }
2521 return err;
2522}
2523
Mathias Agopian53331da2011-08-22 21:44:41 -07002524void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002525 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002526 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002527}
2528
Mathias Agopian59119e62010-10-11 12:37:43 -07002529// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002530// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002531// ---------------------------------------------------------------------------
2532
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002533/* The code below is here to handle b/8734824
2534 *
2535 * We create a IGraphicBufferProducer wrapper that forwards all calls
2536 * to the calling binder thread, where they are executed. This allows
2537 * the calling thread to be reused (on the other side) and not
2538 * depend on having "enough" binder threads to handle the requests.
2539 *
2540 */
2541
2542class GraphicProducerWrapper : public BBinder, public MessageHandler {
2543 sp<IGraphicBufferProducer> impl;
2544 sp<Looper> looper;
2545 status_t result;
2546 bool exitPending;
2547 bool exitRequested;
2548 mutable Barrier barrier;
2549 volatile int32_t memoryBarrier;
2550 uint32_t code;
2551 Parcel const* data;
2552 Parcel* reply;
2553
2554 enum {
2555 MSG_API_CALL,
2556 MSG_EXIT
2557 };
2558
2559 /*
2560 * this is called by our "fake" BpGraphicBufferProducer. We package the
2561 * data and reply Parcel and forward them to the calling thread.
2562 */
2563 virtual status_t transact(uint32_t code,
2564 const Parcel& data, Parcel* reply, uint32_t flags) {
2565 this->code = code;
2566 this->data = &data;
2567 this->reply = reply;
2568 android_atomic_acquire_store(0, &memoryBarrier);
2569 if (exitPending) {
2570 // if we've exited, we run the message synchronously right here
2571 handleMessage(Message(MSG_API_CALL));
2572 } else {
2573 barrier.close();
2574 looper->sendMessage(this, Message(MSG_API_CALL));
2575 barrier.wait();
2576 }
2577 return NO_ERROR;
2578 }
2579
2580 /*
2581 * here we run on the binder calling thread. All we've got to do is
2582 * call the real BpGraphicBufferProducer.
2583 */
2584 virtual void handleMessage(const Message& message) {
2585 android_atomic_release_load(&memoryBarrier);
2586 if (message.what == MSG_API_CALL) {
2587 impl->asBinder()->transact(code, data[0], reply);
2588 barrier.open();
2589 } else if (message.what == MSG_EXIT) {
2590 exitRequested = true;
2591 }
2592 }
2593
2594public:
2595 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2596 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2597 exitPending(false), exitRequested(false) {
2598 }
2599
2600 status_t waitForResponse() {
2601 do {
2602 looper->pollOnce(-1);
2603 } while (!exitRequested);
2604 return result;
2605 }
2606
2607 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002608 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002609 exitPending = true;
2610 looper->sendMessage(this, Message(MSG_EXIT));
2611 }
2612};
2613
2614
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002615status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2616 const sp<IGraphicBufferProducer>& producer,
2617 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002618 uint32_t minLayerZ, uint32_t maxLayerZ,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002619 bool useReadPixels) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002620
2621 if (CC_UNLIKELY(display == 0))
2622 return BAD_VALUE;
2623
2624 if (CC_UNLIKELY(producer == 0))
2625 return BAD_VALUE;
2626
2627 class MessageCaptureScreen : public MessageBase {
2628 SurfaceFlinger* flinger;
2629 sp<IBinder> display;
2630 sp<IGraphicBufferProducer> producer;
2631 uint32_t reqWidth, reqHeight;
2632 uint32_t minLayerZ,maxLayerZ;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002633 bool useReadPixels;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002634 status_t result;
2635 public:
2636 MessageCaptureScreen(SurfaceFlinger* flinger,
2637 const sp<IBinder>& display,
2638 const sp<IGraphicBufferProducer>& producer,
2639 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002640 uint32_t minLayerZ, uint32_t maxLayerZ, bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002641 : flinger(flinger), display(display), producer(producer),
2642 reqWidth(reqWidth), reqHeight(reqHeight),
2643 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002644 useReadPixels(useReadPixels),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002645 result(PERMISSION_DENIED)
2646 {
2647 }
2648 status_t getResult() const {
2649 return result;
2650 }
2651 virtual bool handler() {
2652 Mutex::Autolock _l(flinger->mStateLock);
2653 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002654 bool useReadPixels = this->useReadPixels && !flinger->mGpuToCpuSupported;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002655 result = flinger->captureScreenImplLocked(hw,
2656 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2657 useReadPixels);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002658 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002659 return true;
2660 }
2661 };
2662
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002663 // make sure to process transactions before screenshots -- a transaction
2664 // might already be pending but scheduled for VSYNC; this guarantees we
2665 // will handle it before the screenshot. When VSYNC finally arrives
2666 // the scheduled transaction will be a no-op. If no transactions are
2667 // scheduled at this time, this will end-up being a no-op as well.
2668 mEventQueue.invalidateTransactionNow();
2669
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002670 // this creates a "fake" BBinder which will serve as a "fake" remote
2671 // binder to receive the marshaled calls and forward them to the
2672 // real remote (a BpGraphicBufferProducer)
2673 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2674
2675 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2676 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002677 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002678 display, IGraphicBufferProducer::asInterface( wrapper ),
2679 reqWidth, reqHeight, minLayerZ, maxLayerZ,
2680 useReadPixels);
2681
2682 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002683 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002684 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002685 }
2686 return res;
2687}
2688
Mathias Agopian180f10d2013-04-10 22:55:41 -07002689
2690void SurfaceFlinger::renderScreenImplLocked(
2691 const sp<const DisplayDevice>& hw,
2692 uint32_t reqWidth, uint32_t reqHeight,
2693 uint32_t minLayerZ, uint32_t maxLayerZ,
2694 bool yswap)
2695{
2696 ATRACE_CALL();
2697
2698 // get screen geometry
2699 const uint32_t hw_w = hw->getWidth();
2700 const uint32_t hw_h = hw->getHeight();
2701
2702 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2703
2704 // make sure to clear all GL error flags
2705 while ( glGetError() != GL_NO_ERROR ) ;
2706
2707 // set-up our viewport
2708 glViewport(0, 0, reqWidth, reqHeight);
2709 glMatrixMode(GL_PROJECTION);
2710 glLoadIdentity();
2711 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2712 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2713 glMatrixMode(GL_MODELVIEW);
2714 glLoadIdentity();
2715
2716 // redraw the screen entirely...
2717 glDisable(GL_SCISSOR_TEST);
2718 glClearColor(0,0,0,1);
2719 glClear(GL_COLOR_BUFFER_BIT);
2720 glDisable(GL_TEXTURE_EXTERNAL_OES);
2721 glDisable(GL_TEXTURE_2D);
2722
2723 const LayerVector& layers( mDrawingState.layersSortedByZ );
2724 const size_t count = layers.size();
2725 for (size_t i=0 ; i<count ; ++i) {
2726 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002727 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002728 if (state.layerStack == hw->getLayerStack()) {
2729 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2730 if (layer->isVisible()) {
2731 if (filtering) layer->setFiltering(true);
2732 layer->draw(hw);
2733 if (filtering) layer->setFiltering(false);
2734 }
2735 }
2736 }
2737 }
2738
2739 // compositionComplete is needed for older driver
2740 hw->compositionComplete();
2741}
2742
2743
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002744status_t SurfaceFlinger::captureScreenImplLocked(
2745 const sp<const DisplayDevice>& hw,
2746 const sp<IGraphicBufferProducer>& producer,
2747 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002748 uint32_t minLayerZ, uint32_t maxLayerZ,
2749 bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002750{
2751 ATRACE_CALL();
2752
Mathias Agopian180f10d2013-04-10 22:55:41 -07002753 // get screen geometry
2754 const uint32_t hw_w = hw->getWidth();
2755 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002756
Mathias Agopian180f10d2013-04-10 22:55:41 -07002757 // if we have secure windows on this display, never allow the screen capture
2758 if (hw->getSecureLayerVisible()) {
2759 ALOGW("FB is protected: PERMISSION_DENIED");
2760 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002761 }
2762
Mathias Agopian180f10d2013-04-10 22:55:41 -07002763 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2764 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2765 reqWidth, reqHeight, hw_w, hw_h);
2766 return BAD_VALUE;
2767 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002768
Mathias Agopian180f10d2013-04-10 22:55:41 -07002769 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2770 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2771
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002772 // create a surface (because we're a producer, and we need to
2773 // dequeue/queue a buffer)
2774 sp<Surface> sur = new Surface(producer);
2775 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002776
2777 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002778 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
2779 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
2780 if (!useReadPixels) {
2781 usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002782 }
2783
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002784 int err = 0;
2785 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2786 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2787 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002788
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002789 if (err == NO_ERROR) {
2790 ANativeWindowBuffer* buffer;
2791 /* TODO: Once we have the sync framework everywhere this can use
2792 * server-side waits on the fence that dequeueBuffer returns.
2793 */
2794 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2795 if (result == NO_ERROR) {
2796 // create an EGLImage from the buffer so we can later
2797 // turn it into a texture
2798 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2799 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2800 if (image != EGL_NO_IMAGE_KHR) {
2801 GLuint tname, name;
2802 if (!useReadPixels) {
2803 // turn our EGLImage into a texture
2804 glGenTextures(1, &tname);
2805 glBindTexture(GL_TEXTURE_2D, tname);
2806 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2807 // create a Framebuffer Object to render into
2808 glGenFramebuffersOES(1, &name);
2809 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2810 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
Mathias Agopian875d8e12013-06-07 15:35:48 -07002811 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002812 } else {
2813 // since we're going to use glReadPixels() anyways,
2814 // use an intermediate renderbuffer instead
2815 glGenRenderbuffersOES(1, &tname);
2816 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2817 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
2818 // create a FBO to render into
2819 glGenFramebuffersOES(1, &name);
2820 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2821 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2822 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2823 }
2824
2825 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2826 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2827 // this will in fact render into our dequeued buffer
2828 // via an FBO, which means we didn't have to create
2829 // an EGLSurface and therefore we're not
2830 // dependent on the context's EGLConfig.
2831 renderScreenImplLocked(hw, reqWidth, reqHeight,
2832 minLayerZ, maxLayerZ, true);
2833
2834 if (useReadPixels) {
2835 sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
2836 void* vaddr;
2837 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
2838 glReadPixels(0, 0, buffer->stride, reqHeight,
2839 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
Mathias Agopianfee2b462013-07-03 12:34:01 -07002840 checkScreenshot(buf, vaddr, hw, minLayerZ, maxLayerZ);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002841 buf->unlock();
2842 }
2843 }
2844 } else {
2845 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2846 result = INVALID_OPERATION;
2847 }
2848
2849 // back to main framebuffer
2850 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2851 glDeleteFramebuffersOES(1, &name);
2852 if (!useReadPixels) {
2853 glDeleteTextures(1, &tname);
2854 } else {
2855 glDeleteRenderbuffersOES(1, &tname);
2856 }
2857 // destroy our image
2858 eglDestroyImageKHR(mEGLDisplay, image);
2859 } else {
2860 result = BAD_VALUE;
2861 }
2862 window->queueBuffer(window, buffer, -1);
2863 }
2864 } else {
2865 result = BAD_VALUE;
2866 }
2867 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2868 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002869
Mathias Agopian875d8e12013-06-07 15:35:48 -07002870 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002871
Mathias Agopian74c40c02010-09-29 13:02:36 -07002872 return result;
2873}
2874
Mathias Agopianfee2b462013-07-03 12:34:01 -07002875void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2876 const sp<const DisplayDevice>& hw,
2877 uint32_t minLayerZ, uint32_t maxLayerZ) {
2878 if (DEBUG_SCREENSHOTS) {
2879 for (ssize_t y=0 ; y<buf->height ; y++) {
2880 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2881 for (ssize_t x=0 ; x<buf->width ; x++) {
2882 if (p[x] != 0xFF000000) return;
2883 }
2884 }
2885 ALOGE("*** we just took a black screenshot ***\n"
2886 "requested minz=%d, maxz=%d, layerStack=%d",
2887 minLayerZ, maxLayerZ, hw->getLayerStack());
2888 const LayerVector& layers( mDrawingState.layersSortedByZ );
2889 const size_t count = layers.size();
2890 for (size_t i=0 ; i<count ; ++i) {
2891 const sp<Layer>& layer(layers[i]);
2892 const Layer::State& state(layer->getDrawingState());
2893 const bool visible = (state.layerStack == hw->getLayerStack())
2894 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2895 && (layer->isVisible());
2896 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2897 visible ? '+' : '-',
2898 i, layer->getName().string(), state.layerStack, state.z,
2899 layer->isVisible(), state.flags, state.alpha);
2900 }
2901 }
2902}
2903
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002904// ---------------------------------------------------------------------------
2905
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002906SurfaceFlinger::LayerVector::LayerVector() {
2907}
2908
2909SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002910 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002911}
2912
2913int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2914 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002915{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002916 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002917 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2918 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002919
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002920 uint32_t ls = l->getCurrentState().layerStack;
2921 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002922 if (ls != rs)
2923 return ls - rs;
2924
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002925 uint32_t lz = l->getCurrentState().z;
2926 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002927 if (lz != rz)
2928 return lz - rz;
2929
2930 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002931}
2932
2933// ---------------------------------------------------------------------------
2934
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002935SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2936 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002937}
2938
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002939SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002940 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002941 viewport.makeInvalid();
2942 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002943}
2944
2945// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002946
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002947}; // namespace android