blob: cbdcd1270f3856dae1a6bba912ad87cd79951ad6 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070027#include <GLES/glext.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
74
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#define DISPLAY_COUNT 1
76
Mathias Agopianfee2b462013-07-03 12:34:01 -070077/*
78 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
79 * black pixels.
80 */
81#define DEBUG_SCREENSHOTS false
82
Mathias Agopianca088332013-03-28 17:44:13 -070083EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086// ---------------------------------------------------------------------------
87
Mathias Agopian99b49842011-06-27 16:05:52 -070088const String16 sHardwareTest("android.permission.HARDWARE_TEST");
89const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
90const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
91const String16 sDump("android.permission.DUMP");
92
93// ---------------------------------------------------------------------------
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095SurfaceFlinger::SurfaceFlinger()
96 : BnSurfaceComposer(), Thread(false),
97 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070098 mTransactionPending(false),
99 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700101 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700102 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800106 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700109 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700110 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700111 mDebugInSwapBuffers(0),
112 mLastSwapBufferTime(0),
113 mDebugInTransaction(0),
114 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700115 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 // debugging stuff...
120 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Mathias Agopianb4b17302013-03-20 18:36:41 -0700122 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700123 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 property_get("debug.sf.showupdates", value, "0");
126 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700127
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 property_get("debug.sf.ddms", value, "0");
129 mDebugDDMS = atoi(value);
130 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700131 if (!startDdmConnection()) {
132 // start failed, and DDMS debugging not enabled
133 mDebugDDMS = 0;
134 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700135 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700136 ALOGI_IF(mDebugRegion, "showupdates enabled");
137 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::onFirstRef()
141{
142 mEventQueue.init(this);
143
144 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
145
146 // Wait for the main thread to be done with its initialization
147 mReadyToRunBarrier.wait();
148}
149
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151SurfaceFlinger::~SurfaceFlinger()
152{
Mathias Agopiana4912602012-07-12 14:25:33 -0700153 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
154 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
155 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800158void SurfaceFlinger::binderDied(const wp<IBinder>& who)
159{
160 // the window manager died on us. prepare its eulogy.
161
Andy McFadden13a082e2012-08-24 10:16:42 -0700162 // restore initial conditions (default device unblank, etc)
163 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800164
165 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700166 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800167}
168
Mathias Agopian7e27f052010-05-28 14:22:23 -0700169sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170{
Mathias Agopian96f08192010-06-02 23:28:45 -0700171 sp<ISurfaceComposerClient> bclient;
172 sp<Client> client(new Client(this));
173 status_t err = client->initCheck();
174 if (err == NO_ERROR) {
175 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 return bclient;
178}
179
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700180sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
181 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700182{
183 class DisplayToken : public BBinder {
184 sp<SurfaceFlinger> flinger;
185 virtual ~DisplayToken() {
186 // no more references, this display must be terminated
187 Mutex::Autolock _l(flinger->mStateLock);
188 flinger->mCurrentState.displays.removeItem(this);
189 flinger->setTransactionFlags(eDisplayTransactionNeeded);
190 }
191 public:
192 DisplayToken(const sp<SurfaceFlinger>& flinger)
193 : flinger(flinger) {
194 }
195 };
196
197 sp<BBinder> token = new DisplayToken(this);
198
199 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700200 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700201 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700202 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700203 mCurrentState.displays.add(token, info);
204
205 return token;
206}
207
Jesse Hall692c7232012-11-08 15:41:56 -0800208void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
209 ALOGW_IF(mBuiltinDisplays[type],
210 "Overwriting display token for display type %d", type);
211 mBuiltinDisplays[type] = new BBinder();
212 DisplayDeviceState info(type);
213 // All non-virtual displays are currently considered secure.
214 info.isSecure = true;
215 mCurrentState.displays.add(mBuiltinDisplays[type], info);
216}
217
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700219 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700220 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
221 return NULL;
222 }
Jesse Hall692c7232012-11-08 15:41:56 -0800223 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700224}
225
Jamie Gennis9a78c902011-01-12 18:30:40 -0800226sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
227{
228 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
229 return gba;
230}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232void SurfaceFlinger::bootFinished()
233{
234 const nsecs_t now = systemTime();
235 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000236 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700237 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700238
239 // wait patiently for the window manager death
240 const String16 name("window");
241 sp<IBinder> window(defaultServiceManager()->getService(name));
242 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700243 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700244 }
245
246 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700247 // formerly we would just kill the process, but we now ask it to exit so it
248 // can choose where to stop the animation.
249 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700252void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
253 class MessageDestroyGLTexture : public MessageBase {
254 GLuint texture;
255 public:
256 MessageDestroyGLTexture(GLuint texture)
257 : texture(texture) {
258 }
259 virtual bool handler() {
260 glDeleteTextures(1, &texture);
261 return true;
262 }
263 };
264 postMessageAsync(new MessageDestroyGLTexture(texture));
265}
266
Mathias Agopian722b98f2012-09-25 18:24:31 -0700267status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700268 EGLDisplay dpy,
269 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700270 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700271 EGLConfig* outConfig)
272{
273 EGLConfig config = NULL;
274 EGLint numConfigs = -1, n=0;
275 eglGetConfigs(dpy, NULL, 0, &numConfigs);
276 EGLConfig* const configs = new EGLConfig[numConfigs];
277 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700278
Mathias Agopian722b98f2012-09-25 18:24:31 -0700279 if (n) {
280 if (attribute != EGL_NONE) {
281 for (int i=0 ; i<n ; i++) {
282 EGLint value = 0;
283 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
284 if (wanted == value) {
285 *outConfig = configs[i];
286 delete [] configs;
287 return NO_ERROR;
288 }
289 }
290 } else {
291 // just pick the first one
292 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 delete [] configs;
294 return NO_ERROR;
295 }
296 }
297 delete [] configs;
298 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian722b98f2012-09-25 18:24:31 -0700301class EGLAttributeVector {
302 struct Attribute;
303 class Adder;
304 friend class Adder;
305 KeyedVector<Attribute, EGLint> mList;
306 struct Attribute {
307 Attribute() {};
308 Attribute(EGLint v) : v(v) { }
309 EGLint v;
310 bool operator < (const Attribute& other) const {
311 // this places EGL_NONE at the end
312 EGLint lhs(v);
313 EGLint rhs(other.v);
314 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
315 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
316 return lhs < rhs;
317 }
318 };
319 class Adder {
320 friend class EGLAttributeVector;
321 EGLAttributeVector& v;
322 EGLint attribute;
323 Adder(EGLAttributeVector& v, EGLint attribute)
324 : v(v), attribute(attribute) {
325 }
326 public:
327 void operator = (EGLint value) {
328 if (attribute != EGL_NONE) {
329 v.mList.add(attribute, value);
330 }
331 }
332 operator EGLint () const { return v.mList[attribute]; }
333 };
334public:
335 EGLAttributeVector() {
336 mList.add(EGL_NONE, EGL_NONE);
337 }
338 void remove(EGLint attribute) {
339 if (attribute != EGL_NONE) {
340 mList.removeItem(attribute);
341 }
342 }
343 Adder operator [] (EGLint attribute) {
344 return Adder(*this, attribute);
345 }
346 EGLint operator [] (EGLint attribute) const {
347 return mList[attribute];
348 }
349 // cast-operator to (EGLint const*)
350 operator EGLint const* () const { return &mList.keyAt(0).v; }
351};
352
Mathias Agopiana4912602012-07-12 14:25:33 -0700353EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
354 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
355 // it is to be used with WIFI displays
356 EGLConfig config;
357 EGLint dummy;
358 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700359
Mathias Agopian722b98f2012-09-25 18:24:31 -0700360 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700361 // TODO: enable ES2
362 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
363 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700364 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
365 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
366 attribs[EGL_RED_SIZE] = 8;
367 attribs[EGL_GREEN_SIZE] = 8;
368 attribs[EGL_BLUE_SIZE] = 8;
369
370 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
371 if (!err)
372 goto success;
373
Mathias Agopian875d8e12013-06-07 15:35:48 -0700374 // this didn't work, probably because we're on the emulator...
375 // try a simplified query
376 ALOGW("no suitable EGLConfig found, trying a simpler query");
377 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700378 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700380 attribs.remove(EGL_RED_SIZE);
381 attribs.remove(EGL_GREEN_SIZE);
382 attribs.remove(EGL_BLUE_SIZE);
383 err = selectConfigForAttribute(display, attribs,
384 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700385 if (!err)
386 goto success;
387
388 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700389 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700390 return 0;
391
392success:
393 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700394 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700395 return config;
396}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397
Mathias Agopiana4912602012-07-12 14:25:33 -0700398
Mathias Agopiana4912602012-07-12 14:25:33 -0700399status_t SurfaceFlinger::readyToRun()
400{
401 ALOGI( "SurfaceFlinger's main thread ready to run. "
402 "Initializing graphics H/W...");
403
Jesse Hall692c7232012-11-08 15:41:56 -0800404 Mutex::Autolock _l(mStateLock);
405
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700406 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700407 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
408 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700409
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700410 // Initialize the H/W composer object. There may or may not be an
411 // actual hardware composer underneath.
412 mHwc = new HWComposer(this,
413 *static_cast<HWComposer::EventHandler *>(this));
414
Mathias Agopian875d8e12013-06-07 15:35:48 -0700415 // initialize the config and context (can't fail)
416 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
417
418 // print some debugging info
419 EGLint r,g,b,a;
420 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
421 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
422 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
423 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
424 ALOGI("EGL informations:");
425 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
426 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
427 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
428 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
429 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
430
431 // get a RenderEngine for the given display / config (can't fail)
432 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
433
434 // retrieve the EGL context that was selected/created
435 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700436
Mathias Agopianed985572013-03-22 00:24:39 -0700437 // figure out which format we got
438 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
439 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
440
Mathias Agopianda27af92012-09-13 18:17:13 -0700441 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
442 "couldn't create EGLContext");
443
Mathias Agopiancde87a32012-09-13 14:09:01 -0700444 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700445 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700446 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700447 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700448 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700449 // All non-virtual displays are currently considered secure.
450 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800451 createBuiltinDisplayLocked(type);
452 wp<IBinder> token = mBuiltinDisplays[i];
453
Mathias Agopianf5a33922012-09-19 18:16:22 -0700454 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700455 type, allocateHwcDisplayId(type), isSecure, token,
456 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700457 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700458 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700459 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700460 // for displays other than the main display, so we always
461 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700462 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700463 hw->acquireScreen();
464 }
465 mDisplays.add(token, hw);
466 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700467 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700468
Mathias Agopian028508c2012-07-25 21:12:12 -0700469 // start the EventThread
470 mEventThread = new EventThread(this);
471 mEventQueue.setEventThread(mEventThread);
472
Mathias Agopian92a979a2012-08-02 18:32:23 -0700473 // initialize our drawing state
474 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700475
Mathias Agopiana4912602012-07-12 14:25:33 -0700476 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800477 mReadyToRunBarrier.open();
478
Andy McFadden13a082e2012-08-24 10:16:42 -0700479 // set initial conditions (e.g. unblank default device)
480 initializeDisplays();
481
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700482 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700483 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 return NO_ERROR;
486}
487
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700488int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
489 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
490 type : mHwc->allocateDisplayId();
491}
492
Mathias Agopiana67e4182012-06-19 17:26:12 -0700493void SurfaceFlinger::startBootAnim() {
494 // start boot animation
495 property_set("service.bootanim.exit", "0");
496 property_set("ctl.start", "bootanim");
497}
498
Mathias Agopian875d8e12013-06-07 15:35:48 -0700499size_t SurfaceFlinger::getMaxTextureSize() const {
500 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700501}
502
Mathias Agopian875d8e12013-06-07 15:35:48 -0700503size_t SurfaceFlinger::getMaxViewportDims() const {
504 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700505}
506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800508
Jamie Gennis582270d2011-08-17 18:19:00 -0700509bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800510 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800511 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800512 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700513 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800514}
515
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700516status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800517 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700518 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800519 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700520 type = i;
521 break;
522 }
523 }
524
525 if (type < 0) {
526 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700527 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700528
529 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700530 float xdpi = hwc.getDpiX(type);
531 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700532
533 // TODO: Not sure if display density should handled by SF any longer
534 class Density {
535 static int getDensityFromProperty(char const* propName) {
536 char property[PROPERTY_VALUE_MAX];
537 int density = 0;
538 if (property_get(propName, property, NULL) > 0) {
539 density = atoi(property);
540 }
541 return density;
542 }
543 public:
544 static int getEmuDensity() {
545 return getDensityFromProperty("qemu.sf.lcd_density"); }
546 static int getBuildDensity() {
547 return getDensityFromProperty("ro.sf.lcd_density"); }
548 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700549
550 if (type == DisplayDevice::DISPLAY_PRIMARY) {
551 // The density of the device is provided by a build property
552 float density = Density::getBuildDensity() / 160.0f;
553 if (density == 0) {
554 // the build doesn't provide a density -- this is wrong!
555 // use xdpi instead
556 ALOGE("ro.sf.lcd_density must be defined as a build property");
557 density = xdpi / 160.0f;
558 }
559 if (Density::getEmuDensity()) {
560 // if "qemu.sf.lcd_density" is specified, it overrides everything
561 xdpi = ydpi = density = Density::getEmuDensity();
562 density /= 160.0f;
563 }
564 info->density = density;
565
566 // TODO: this needs to go away (currently needed only by webkit)
567 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
568 info->orientation = hw->getOrientation();
569 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
570 } else {
571 // TODO: where should this value come from?
572 static const int TV_DENSITY = 213;
573 info->density = TV_DENSITY / 160.0f;
574 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700575 }
576
Mathias Agopian1604f772012-09-18 21:54:42 -0700577 info->w = hwc.getWidth(type);
578 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700579 info->xdpi = xdpi;
580 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700581 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700582
583 // All non-virtual displays are currently considered secure.
584 info->secure = true;
585
Mathias Agopian888c8222012-08-04 21:10:38 -0700586 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700587}
588
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800589// ----------------------------------------------------------------------------
590
591sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800592 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800596
597void SurfaceFlinger::waitForEvent() {
598 mEventQueue.waitMessage();
599}
600
601void SurfaceFlinger::signalTransaction() {
602 mEventQueue.invalidate();
603}
604
605void SurfaceFlinger::signalLayerUpdate() {
606 mEventQueue.invalidate();
607}
608
609void SurfaceFlinger::signalRefresh() {
610 mEventQueue.refresh();
611}
612
613status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
614 nsecs_t reltime, uint32_t flags) {
615 return mEventQueue.postMessage(msg, reltime);
616}
617
618status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
619 nsecs_t reltime, uint32_t flags) {
620 status_t res = mEventQueue.postMessage(msg, reltime);
621 if (res == NO_ERROR) {
622 msg->wait();
623 }
624 return res;
625}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 return true;
630}
631
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700632void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700633 if (mEventThread == NULL) {
634 // This is a temporary workaround for b/7145521. A non-null pointer
635 // does not mean EventThread has finished initializing, so this
636 // is not a correct fix.
637 ALOGW("WARNING: EventThread not started, ignoring vsync");
638 return;
639 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700640 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
641 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700642 mEventThread->onVSyncReceived(type, timestamp);
643 }
644}
645
646void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
647 if (mEventThread == NULL) {
648 // This is a temporary workaround for b/7145521. A non-null pointer
649 // does not mean EventThread has finished initializing, so this
650 // is not a correct fix.
651 ALOGW("WARNING: EventThread not started, ignoring hotplug");
652 return;
653 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700654
Mathias Agopian148994e2012-09-19 17:31:36 -0700655 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700656 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800657 if (connected) {
658 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700659 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800660 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
661 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700662 }
663 setTransactionFlags(eDisplayTransactionNeeded);
664
Andy McFadden9e9689c2012-10-10 18:17:51 -0700665 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700666 }
Mathias Agopian86303202012-07-24 22:46:10 -0700667}
668
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700669void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
670 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700671}
672
Mathias Agopian4fec8732012-06-29 14:12:52 -0700673void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800674 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800675 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700676 case MessageQueue::TRANSACTION:
677 handleMessageTransaction();
678 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679 case MessageQueue::INVALIDATE:
680 handleMessageTransaction();
681 handleMessageInvalidate();
682 signalRefresh();
683 break;
684 case MessageQueue::REFRESH:
685 handleMessageRefresh();
686 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800687 }
688}
689
Mathias Agopian4fec8732012-06-29 14:12:52 -0700690void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700691 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700692 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700693 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700694 }
695}
696
697void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700698 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700699 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700}
701
702void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700703 ATRACE_CALL();
704 preComposition();
705 rebuildLayerStacks();
706 setUpHWComposer();
707 doDebugFlashRegions();
708 doComposition();
709 postComposition();
710}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711
Mathias Agopiancd60f992012-08-16 16:28:27 -0700712void SurfaceFlinger::doDebugFlashRegions()
713{
714 // is debugging enabled
715 if (CC_LIKELY(!mDebugRegion))
716 return;
717
718 const bool repaintEverything = mRepaintEverything;
719 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
720 const sp<DisplayDevice>& hw(mDisplays[dpy]);
721 if (hw->canDraw()) {
722 // transform the dirty region into this screen's coordinate space
723 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
724 if (!dirtyRegion.isEmpty()) {
725 // redraw the whole screen
726 doComposeSurfaces(hw, Region(hw->bounds()));
727
728 // and draw the dirty region
729 glDisable(GL_TEXTURE_EXTERNAL_OES);
730 glDisable(GL_TEXTURE_2D);
731 glDisable(GL_BLEND);
732 glColor4f(1, 0, 1, 1);
733 const int32_t height = hw->getHeight();
734 Region::const_iterator it = dirtyRegion.begin();
735 Region::const_iterator const end = dirtyRegion.end();
736 while (it != end) {
737 const Rect& r = *it++;
738 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800739 { (GLfloat) r.left, (GLfloat) (height - r.top) },
740 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
741 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
742 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700743 };
744 glVertexPointer(2, GL_FLOAT, 0, vertices);
745 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
746 }
747 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700748 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700749 }
750 }
751 }
752
753 postFramebuffer();
754
755 if (mDebugRegion > 1) {
756 usleep(mDebugRegion * 1000);
757 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700758
759 HWComposer& hwc(getHwComposer());
760 if (hwc.initCheck() == NO_ERROR) {
761 status_t err = hwc.prepare();
762 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
763 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764}
765
766void SurfaceFlinger::preComposition()
767{
768 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700769 const LayerVector& layers(mDrawingState.layersSortedByZ);
770 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700772 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773 needExtraInvalidate = true;
774 }
775 }
776 if (needExtraInvalidate) {
777 signalLayerUpdate();
778 }
779}
780
781void SurfaceFlinger::postComposition()
782{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700783 const LayerVector& layers(mDrawingState.layersSortedByZ);
784 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700786 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800788
789 if (mAnimCompositionPending) {
790 mAnimCompositionPending = false;
791
792 const HWComposer& hwc = getHwComposer();
793 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800794 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800795 mAnimFrameTracker.setActualPresentFence(presentFence);
796 } else {
797 // The HWC doesn't support present fences, so use the refresh
798 // timestamp instead.
799 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
800 mAnimFrameTracker.setActualPresentTime(presentTime);
801 }
802 mAnimFrameTracker.advanceFrame();
803 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804}
805
806void SurfaceFlinger::rebuildLayerStacks() {
807 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700810 mVisibleRegionsDirty = false;
811 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700812
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700813 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700814 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700815 Region opaqueRegion;
816 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800817 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700818 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700819 const Transform& tr(hw->getTransform());
820 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700821 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700822 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700823 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700824
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700825 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700826 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const sp<Layer>& layer(layers[i]);
828 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700829 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700830 Region drawRegion(tr.transform(
831 layer->visibleNonTransparentRegion));
832 drawRegion.andSelf(bounds);
833 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700834 layersSortedByZ.add(layer);
835 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700836 }
837 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700838 }
Mathias Agopian42977342012-08-05 00:40:46 -0700839 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700840 hw->undefinedRegion.set(bounds);
841 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
842 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700843 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700844 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700846
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 HWComposer& hwc(getHwComposer());
849 if (hwc.initCheck() == NO_ERROR) {
850 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700851 if (CC_UNLIKELY(mHwWorkListDirty)) {
852 mHwWorkListDirty = false;
853 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
854 sp<const DisplayDevice> hw(mDisplays[dpy]);
855 const int32_t id = hw->getHwcDisplayId();
856 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700858 hw->getVisibleLayersSortedByZ());
859 const size_t count = currentLayers.size();
860 if (hwc.createWorkList(id, count) == NO_ERROR) {
861 HWComposer::LayerListIterator cur = hwc.begin(id);
862 const HWComposer::LayerListIterator end = hwc.end(id);
863 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800864 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700865 layer->setGeometry(hw, *cur);
866 if (mDebugDisableHWC || mDebugRegion) {
867 cur->setSkip(true);
868 }
869 }
870 }
871 }
872 }
873 }
874
875 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700876 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700877 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700878 const int32_t id = hw->getHwcDisplayId();
879 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700882 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700883 HWComposer::LayerListIterator cur = hwc.begin(id);
884 const HWComposer::LayerListIterator end = hwc.end(id);
885 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
886 /*
887 * update the per-frame h/w composer data for each layer
888 * and build the transparent region of the FB
889 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700891 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700892 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700893 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700894 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700895
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700896 status_t err = hwc.prepare();
897 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700898
899 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
900 sp<const DisplayDevice> hw(mDisplays[dpy]);
901 hw->prepareFrame(hwc);
902 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700903 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700904}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700905
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906void SurfaceFlinger::doComposition() {
907 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700908 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700909 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700910 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911 if (hw->canDraw()) {
912 // transform the dirty region into this screen's coordinate space
913 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800914
915 // repaint the framebuffer (if needed)
916 doDisplayComposition(hw, dirtyRegion);
917
Mathias Agopiancd60f992012-08-16 16:28:27 -0700918 hw->dirtyRegion.clear();
919 hw->flip(hw->swapRegion);
920 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700921 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700922 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700923 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700924 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700925 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700926}
927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928void SurfaceFlinger::postFramebuffer()
929{
Mathias Agopian841cde52012-03-01 15:44:37 -0800930 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800931
Mathias Agopiana44b0412011-10-16 18:46:35 -0700932 const nsecs_t now = systemTime();
933 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700934
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700935 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700936 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700937 if (!hwc.supportsFramebufferTarget()) {
938 // EGL spec says:
939 // "surface must be bound to the calling thread's current context,
940 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700941 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700942 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700943 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700944 }
945
Mathias Agopian92a979a2012-08-02 18:32:23 -0700946 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700947 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800948 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700949 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700950 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700951 int32_t id = hw->getHwcDisplayId();
952 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700953 HWComposer::LayerListIterator cur = hwc.begin(id);
954 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700955 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700956 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700957 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700960 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 }
Jesse Hallef194142012-06-14 14:45:17 -0700962 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800963 }
964
Mathias Agopiana44b0412011-10-16 18:46:35 -0700965 mLastSwapBufferTime = systemTime() - now;
966 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700967
968 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
969 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
970 logFrameStats();
971 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972}
973
Mathias Agopian87baae12012-07-31 12:38:26 -0700974void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975{
Mathias Agopian841cde52012-03-01 15:44:37 -0800976 ATRACE_CALL();
977
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700978 // here we keep a copy of the drawing state (that is the state that's
979 // going to be overwritten by handleTransactionLocked()) outside of
980 // mStateLock so that the side-effects of the State assignment
981 // don't happen with mStateLock held (which can cause deadlocks).
982 State drawingState(mDrawingState);
983
Mathias Agopianca4d3602011-05-19 15:38:14 -0700984 Mutex::Autolock _l(mStateLock);
985 const nsecs_t now = systemTime();
986 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987
Mathias Agopianca4d3602011-05-19 15:38:14 -0700988 // Here we're guaranteed that some transaction flags are set
989 // so we can call handleTransactionLocked() unconditionally.
990 // We call getTransactionFlags(), which will also clear the flags,
991 // with mStateLock held to guarantee that mCurrentState won't change
992 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700993
Mathias Agopiane57f2922012-08-09 16:29:12 -0700994 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700995 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700996
Mathias Agopianca4d3602011-05-19 15:38:14 -0700997 mLastTransactionTime = systemTime() - now;
998 mDebugInTransaction = 0;
999 invalidateHwcGeometry();
1000 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001001}
1002
Mathias Agopian87baae12012-07-31 12:38:26 -07001003void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001004{
1005 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 const size_t count = currentLayers.size();
1007
1008 /*
1009 * Traversal of the children
1010 * (perform the transaction for each of them if needed)
1011 */
1012
Mathias Agopian3559b072012-08-15 13:46:03 -07001013 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001015 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1017 if (!trFlags) continue;
1018
1019 const uint32_t flags = layer->doTransaction(0);
1020 if (flags & Layer::eVisibleRegion)
1021 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 }
1023 }
1024
1025 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001026 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 */
1028
Mathias Agopiane57f2922012-08-09 16:29:12 -07001029 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001030 // here we take advantage of Vector's copy-on-write semantics to
1031 // improve performance by skipping the transaction entirely when
1032 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001033 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1034 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001035 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001037 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001038 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001039
1040 // find the displays that were removed
1041 // (ie: in drawing state but not in current state)
1042 // also handle displays that changed
1043 // (ie: displays that are in both lists)
1044 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001045 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1046 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001047 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001048 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001049 // Call makeCurrent() on the primary display so we can
1050 // be sure that nothing associated with this display
1051 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001052 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001053 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001054 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1055 if (hw != NULL)
1056 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001057 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1058 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001059 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001060 } else {
1061 ALOGW("trying to remove the main display");
1062 }
1063 } else {
1064 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001065 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001066 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001067 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001068 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001069 // recreating the DisplayDevice, so we just remove it
1070 // from the drawing state, so that it get re-added
1071 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001072 sp<DisplayDevice> hw(getDisplayDevice(display));
1073 if (hw != NULL)
1074 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001075 mDisplays.removeItem(display);
1076 mDrawingState.displays.removeItemsAt(i);
1077 dc--; i--;
1078 // at this point we must loop to the next item
1079 continue;
1080 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001081
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001082 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001083 if (disp != NULL) {
1084 if (state.layerStack != draw[i].layerStack) {
1085 disp->setLayerStack(state.layerStack);
1086 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001087 if ((state.orientation != draw[i].orientation)
1088 || (state.viewport != draw[i].viewport)
1089 || (state.frame != draw[i].frame))
1090 {
1091 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001092 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001093 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001094 }
1095 }
1096 }
1097
1098 // find displays that were added
1099 // (ie: in current state but not in drawing state)
1100 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001101 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001102 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001103
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001104 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001105 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001106 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001107 // Virtual displays without a surface are dormant:
1108 // they have external state (layer stack, projection,
1109 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001110 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001111 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001112 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001113 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001114 state.displayName);
1115 }
1116 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001117 ALOGE_IF(state.surface!=NULL,
1118 "adding a supported display, but rendering "
1119 "surface is provided (%p), ignoring it",
1120 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001121 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001122 // for supported (by hwc) displays we provide our
1123 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001124 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001125 }
1126
1127 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001128 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001129 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001130 state.type, hwcDisplayId, state.isSecure,
1131 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001132 hw->setLayerStack(state.layerStack);
1133 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001134 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001135 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001136 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001137 if (state.isVirtualDisplay()) {
1138 if (hwcDisplayId >= 0) {
1139 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1140 hw->getWidth(), hw->getHeight(),
1141 hw->getFormat());
1142 }
1143 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001144 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001145 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001146 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001147 }
1148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001150 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151
Mathias Agopian84300952012-11-21 16:02:13 -08001152 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1153 // The transform hint might have changed for some layers
1154 // (either because a display has changed, or because a layer
1155 // as changed).
1156 //
1157 // Walk through all the layers in currentLayers,
1158 // and update their transform hint.
1159 //
1160 // If a layer is visible only on a single display, then that
1161 // display is used to calculate the hint, otherwise we use the
1162 // default display.
1163 //
1164 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1165 // the hint is set before we acquire a buffer from the surface texture.
1166 //
1167 // NOTE: layer transactions have taken place already, so we use their
1168 // drawing state. However, SurfaceFlinger's own transaction has not
1169 // happened yet, so we must use the current state layer list
1170 // (soon to become the drawing state list).
1171 //
1172 sp<const DisplayDevice> disp;
1173 uint32_t currentlayerStack = 0;
1174 for (size_t i=0; i<count; i++) {
1175 // NOTE: we rely on the fact that layers are sorted by
1176 // layerStack first (so we don't have to traverse the list
1177 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001178 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001179 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001180 if (i==0 || currentlayerStack != layerStack) {
1181 currentlayerStack = layerStack;
1182 // figure out if this layerstack is mirrored
1183 // (more than one display) if so, pick the default display,
1184 // if not, pick the only display it's on.
1185 disp.clear();
1186 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1187 sp<const DisplayDevice> hw(mDisplays[dpy]);
1188 if (hw->getLayerStack() == currentlayerStack) {
1189 if (disp == NULL) {
1190 disp = hw;
1191 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001192 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001193 break;
1194 }
1195 }
1196 }
1197 }
Chet Haase91d25932013-04-11 15:24:55 -07001198 if (disp == NULL) {
1199 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1200 // redraw after transform hint changes. See bug 8508397.
1201
1202 // could be null when this layer is using a layerStack
1203 // that is not visible on any display. Also can occur at
1204 // screen off/on times.
1205 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001206 }
Chet Haase91d25932013-04-11 15:24:55 -07001207 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001208 }
1209 }
1210
1211
Mathias Agopian3559b072012-08-15 13:46:03 -07001212 /*
1213 * Perform our own transaction if needed
1214 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001215
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001216 const LayerVector& layers(mDrawingState.layersSortedByZ);
1217 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001218 // layers have been added
1219 mVisibleRegionsDirty = true;
1220 }
1221
1222 // some layers might have been removed, so
1223 // we need to update the regions they're exposing.
1224 if (mLayersRemoved) {
1225 mLayersRemoved = false;
1226 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001227 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001228 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001229 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001230 if (currentLayers.indexOf(layer) < 0) {
1231 // this layer is not visible anymore
1232 // TODO: we could traverse the tree from front to back and
1233 // compute the actual visible region
1234 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001235 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001236 Region visibleReg = s.transform.transform(
1237 Region(Rect(s.active.w, s.active.h)));
1238 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001239 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 }
1242
1243 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001244}
1245
1246void SurfaceFlinger::commitTransaction()
1247{
1248 if (!mLayersPendingRemoval.isEmpty()) {
1249 // Notify removed layers now that they can't be drawn from
1250 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1251 mLayersPendingRemoval[i]->onRemoved();
1252 }
1253 mLayersPendingRemoval.clear();
1254 }
1255
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001256 // If this transaction is part of a window animation then the next frame
1257 // we composite should be considered an animation as well.
1258 mAnimCompositionPending = mAnimTransactionPending;
1259
Mathias Agopian4fec8732012-06-29 14:12:52 -07001260 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001261 mTransactionPending = false;
1262 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001263 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264}
1265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001267 const LayerVector& currentLayers, uint32_t layerStack,
1268 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269{
Mathias Agopian841cde52012-03-01 15:44:37 -08001270 ATRACE_CALL();
1271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 Region aboveOpaqueLayers;
1273 Region aboveCoveredLayers;
1274 Region dirty;
1275
Mathias Agopian87baae12012-07-31 12:38:26 -07001276 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277
1278 size_t i = currentLayers.size();
1279 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001280 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281
1282 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001283 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284
Jesse Hall01e29052013-02-19 16:13:35 -08001285 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001286 if (s.layerStack != layerStack)
1287 continue;
1288
Mathias Agopianab028732010-03-16 16:41:46 -07001289 /*
1290 * opaqueRegion: area of a surface that is fully opaque.
1291 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001293
1294 /*
1295 * visibleRegion: area of a surface that is visible on screen
1296 * and not fully transparent. This is essentially the layer's
1297 * footprint minus the opaque regions above it.
1298 * Areas covered by a translucent surface are considered visible.
1299 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001301
1302 /*
1303 * coveredRegion: area of a surface that is covered by all
1304 * visible regions above it (which includes the translucent areas).
1305 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001307
Jesse Halla8026d22012-09-25 13:25:04 -07001308 /*
1309 * transparentRegion: area of a surface that is hinted to be completely
1310 * transparent. This is only used to tell when the layer has no visible
1311 * non-transparent regions and can be removed from the layer list. It
1312 * does not affect the visibleRegion of this layer or any layers
1313 * beneath it. The hint may not be correct if apps don't respect the
1314 * SurfaceView restrictions (which, sadly, some don't).
1315 */
1316 Region transparentRegion;
1317
Mathias Agopianab028732010-03-16 16:41:46 -07001318
1319 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001320 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001321 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001322 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001324 if (!visibleRegion.isEmpty()) {
1325 // Remove the transparent area from the visible region
1326 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 const Transform tr(s.transform);
1328 if (tr.transformed()) {
1329 if (tr.preserveRects()) {
1330 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001331 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001332 } else {
1333 // transformation too complex, can't do the
1334 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001335 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001336 }
1337 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001338 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001339 }
Mathias Agopianab028732010-03-16 16:41:46 -07001340 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341
Mathias Agopianab028732010-03-16 16:41:46 -07001342 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001343 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001344 if (s.alpha==255 && !translucent &&
1345 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1346 // the opaque region is the layer's footprint
1347 opaqueRegion = visibleRegion;
1348 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 }
1350 }
1351
Mathias Agopianab028732010-03-16 16:41:46 -07001352 // Clip the covered region to the visible region
1353 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1354
1355 // Update aboveCoveredLayers for next (lower) layer
1356 aboveCoveredLayers.orSelf(visibleRegion);
1357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 // subtract the opaque region covered by the layers above us
1359 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360
1361 // compute this layer's dirty region
1362 if (layer->contentDirty) {
1363 // we need to invalidate the whole region
1364 dirty = visibleRegion;
1365 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 layer->contentDirty = false;
1368 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001369 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001370 * the exposed region consists of two components:
1371 * 1) what's VISIBLE now and was COVERED before
1372 * 2) what's EXPOSED now less what was EXPOSED before
1373 *
1374 * note that (1) is conservative, we start with the whole
1375 * visible region but only keep what used to be covered by
1376 * something -- which mean it may have been exposed.
1377 *
1378 * (2) handles areas that were not covered by anything but got
1379 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001380 */
Mathias Agopianab028732010-03-16 16:41:46 -07001381 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 const Region oldVisibleRegion = layer->visibleRegion;
1383 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001384 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1385 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 }
1387 dirty.subtractSelf(aboveOpaqueLayers);
1388
1389 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001390 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391
Mathias Agopianab028732010-03-16 16:41:46 -07001392 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001394
Jesse Halla8026d22012-09-25 13:25:04 -07001395 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 layer->setVisibleRegion(visibleRegion);
1397 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001398 layer->setVisibleNonTransparentRegion(
1399 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 }
1401
Mathias Agopian87baae12012-07-31 12:38:26 -07001402 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403}
1404
Mathias Agopian87baae12012-07-31 12:38:26 -07001405void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1406 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001407 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001408 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1409 if (hw->getLayerStack() == layerStack) {
1410 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001411 }
1412 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001413}
1414
1415void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001417 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418
Mathias Agopian4fec8732012-06-29 14:12:52 -07001419 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001420 const LayerVector& layers(mDrawingState.layersSortedByZ);
1421 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001423 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001424 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001425 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001426 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001427 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001428
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001429 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430}
1431
Mathias Agopianad456f92011-01-13 17:53:01 -08001432void SurfaceFlinger::invalidateHwcGeometry()
1433{
1434 mHwWorkListDirty = true;
1435}
1436
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001437
Mathias Agopiancd60f992012-08-16 16:28:27 -07001438void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001439 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440{
Mathias Agopian87baae12012-07-31 12:38:26 -07001441 Region dirtyRegion(inDirtyRegion);
1442
Mathias Agopianb8a55602009-06-26 19:06:36 -07001443 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001444 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445
Mathias Agopian42977342012-08-05 00:40:46 -07001446 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001447 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001448 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1449 // takes a rectangle, we must make sure to update that whole
1450 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001451 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001452 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001453 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001454 // We need to redraw the rectangle that will be updated
1455 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001456 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001457 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001458 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001459 } else {
1460 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001461 dirtyRegion.set(hw->bounds());
1462 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 }
1464 }
1465
Mathias Agopiancd60f992012-08-16 16:28:27 -07001466 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001468 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001469 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001470
1471 // swap buffers (presentation)
1472 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473}
1474
Mathias Agopiancd60f992012-08-16 16:28:27 -07001475void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001476{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001477 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001478 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001479 HWComposer::LayerListIterator cur = hwc.begin(id);
1480 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001481
Mathias Agopian85d751c2012-08-29 16:59:24 -07001482 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1483 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001484 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001485 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1486 hw->getDisplayName().string());
1487 return;
1488 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001489
1490 // set the frame buffer
1491 glMatrixMode(GL_MODELVIEW);
1492 glLoadIdentity();
1493
1494 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001495 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001496 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001497 // when using overlays, we assume a fully transparent framebuffer
1498 // NOTE: we could reduce how much we need to clear, for instance
1499 // remove where there are opaque FB layers. however, on some
1500 // GPUs doing a "clean slate" glClear might be more efficient.
1501 // We'll revisit later if needed.
1502 glClearColor(0, 0, 0, 0);
1503 glClear(GL_COLOR_BUFFER_BIT);
1504 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001505 // we start with the whole screen area
1506 const Region bounds(hw->getBounds());
1507
1508 // we remove the scissor part
1509 // we're left with the letterbox region
1510 // (common case is that letterbox ends-up being empty)
1511 const Region letterbox(bounds.subtract(hw->getScissor()));
1512
1513 // compute the area to clear
1514 Region region(hw->undefinedRegion.merge(letterbox));
1515
1516 // but limit it to the dirty region
1517 region.andSelf(dirty);
1518
Mathias Agopianb9494d52012-04-18 02:28:45 -07001519 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001520 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001521 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001522 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001523 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001524 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001525
Mathias Agopian766dc492012-10-30 18:08:06 -07001526 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1527 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001528 // scissor on the main display. It should never be needed
1529 // anyways (though in theory it could since the API allows it).
1530 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001531 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001532 if (scissor != bounds) {
1533 // scissor doesn't match the screen's dimensions, so we
1534 // need to clear everything outside of it and enable
1535 // the GL scissor so we don't draw anything where we shouldn't
1536 const GLint height = hw->getHeight();
1537 glScissor(scissor.left, height - scissor.bottom,
1538 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001539 // enable scissor for this frame
1540 glEnable(GL_SCISSOR_TEST);
1541 }
1542 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001543 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001544
Mathias Agopian85d751c2012-08-29 16:59:24 -07001545 /*
1546 * and then, render the layers targeted at the framebuffer
1547 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001548
Mathias Agopian13127d82013-03-05 17:47:11 -08001549 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001550 const size_t count = layers.size();
1551 const Transform& tr = hw->getTransform();
1552 if (cur != end) {
1553 // we're using h/w composer
1554 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001555 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001556 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001557 if (!clip.isEmpty()) {
1558 switch (cur->getCompositionType()) {
1559 case HWC_OVERLAY: {
1560 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1561 && i
1562 && layer->isOpaque()
1563 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001564 // never clear the very first layer since we're
1565 // guaranteed the FB is already cleared
1566 layer->clearWithOpenGL(hw, clip);
1567 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001568 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001569 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001570 case HWC_FRAMEBUFFER: {
1571 layer->draw(hw, clip);
1572 break;
1573 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001574 case HWC_FRAMEBUFFER_TARGET: {
1575 // this should not happen as the iterator shouldn't
1576 // let us get there.
1577 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1578 break;
1579 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001580 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001581 }
1582 layer->setAcquireFence(hw, *cur);
1583 }
1584 } else {
1585 // we're not using h/w composer
1586 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001587 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001588 const Region clip(dirty.intersect(
1589 tr.transform(layer->visibleRegion)));
1590 if (!clip.isEmpty()) {
1591 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001592 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001593 }
1594 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001595
1596 // disable scissor at the end of the frame
1597 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598}
1599
Mathias Agopian55801e42012-08-27 18:54:24 -07001600void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1601 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001603 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001604 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001605 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001606 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001607
Mathias Agopian55801e42012-08-27 18:54:24 -07001608 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001609 Region::const_iterator it = region.begin();
1610 Region::const_iterator const end = region.end();
1611 while (it != end) {
1612 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001613 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001614 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1615 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1616 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1617 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001618 };
1619 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001620 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1621 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622}
1623
Mathias Agopianac9fa422013-02-11 16:40:36 -08001624void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1625 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001626 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001627 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001628{
Mathias Agopian96f08192010-06-02 23:28:45 -07001629 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001630 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001631
Mathias Agopian96f08192010-06-02 23:28:45 -07001632 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001633 Mutex::Autolock _l(mStateLock);
1634 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001635 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001636}
1637
Mathias Agopian13127d82013-03-05 17:47:11 -08001638status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639{
1640 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001641 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001643 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001645 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 return NO_ERROR;
1647 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001648 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649}
1650
Mathias Agopiandea20b12011-05-03 17:04:02 -07001651uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1652{
1653 return android_atomic_release_load(&mTransactionFlags);
1654}
1655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1657{
1658 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1659}
1660
Mathias Agopianbb641242010-05-18 17:06:55 -07001661uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662{
1663 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1664 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001665 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 }
1667 return old;
1668}
1669
Mathias Agopian8b33f032012-07-24 20:43:54 -07001670void SurfaceFlinger::setTransactionState(
1671 const Vector<ComposerState>& state,
1672 const Vector<DisplayState>& displays,
1673 uint32_t flags)
1674{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001675 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001676 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001677 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001678
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001679 if (flags & eAnimation) {
1680 // For window updates that are part of an animation we must wait for
1681 // previous animation "frames" to be handled.
1682 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001683 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001684 if (CC_UNLIKELY(err != NO_ERROR)) {
1685 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001686 // caller after a few seconds.
1687 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1688 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001689 mAnimTransactionPending = false;
1690 break;
1691 }
1692 }
1693 }
1694
Mathias Agopiane57f2922012-08-09 16:29:12 -07001695 size_t count = displays.size();
1696 for (size_t i=0 ; i<count ; i++) {
1697 const DisplayState& s(displays[i]);
1698 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001699 }
1700
Mathias Agopiane57f2922012-08-09 16:29:12 -07001701 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001702 for (size_t i=0 ; i<count ; i++) {
1703 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001704 // Here we need to check that the interface we're given is indeed
1705 // one of our own. A malicious client could give us a NULL
1706 // IInterface, or one of its own or even one of our own but a
1707 // different type. All these situations would cause us to crash.
1708 //
1709 // NOTE: it would be better to use RTTI as we could directly check
1710 // that we have a Client*. however, RTTI is disabled in Android.
1711 if (s.client != NULL) {
1712 sp<IBinder> binder = s.client->asBinder();
1713 if (binder != NULL) {
1714 String16 desc(binder->getInterfaceDescriptor());
1715 if (desc == ISurfaceComposerClient::descriptor) {
1716 sp<Client> client( static_cast<Client *>(s.client.get()) );
1717 transactionFlags |= setClientStateLocked(client, s.state);
1718 }
1719 }
1720 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001721 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001722
Mathias Agopian386aa982011-11-07 21:58:03 -08001723 if (transactionFlags) {
1724 // this triggers the transaction
1725 setTransactionFlags(transactionFlags);
1726
1727 // if this is a synchronous transaction, wait for it to take effect
1728 // before returning.
1729 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001730 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001731 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001732 if (flags & eAnimation) {
1733 mAnimTransactionPending = true;
1734 }
1735 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001736 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1737 if (CC_UNLIKELY(err != NO_ERROR)) {
1738 // just in case something goes wrong in SF, return to the
1739 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001740 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1741 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001742 break;
1743 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001744 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 }
1746}
1747
Mathias Agopiane57f2922012-08-09 16:29:12 -07001748uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1749{
Jesse Hall9a143922012-10-04 16:29:19 -07001750 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1751 if (dpyIdx < 0)
1752 return 0;
1753
Mathias Agopiane57f2922012-08-09 16:29:12 -07001754 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001755 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001756 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001757 const uint32_t what = s.what;
1758 if (what & DisplayState::eSurfaceChanged) {
1759 if (disp.surface->asBinder() != s.surface->asBinder()) {
1760 disp.surface = s.surface;
1761 flags |= eDisplayTransactionNeeded;
1762 }
1763 }
1764 if (what & DisplayState::eLayerStackChanged) {
1765 if (disp.layerStack != s.layerStack) {
1766 disp.layerStack = s.layerStack;
1767 flags |= eDisplayTransactionNeeded;
1768 }
1769 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001770 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001771 if (disp.orientation != s.orientation) {
1772 disp.orientation = s.orientation;
1773 flags |= eDisplayTransactionNeeded;
1774 }
1775 if (disp.frame != s.frame) {
1776 disp.frame = s.frame;
1777 flags |= eDisplayTransactionNeeded;
1778 }
1779 if (disp.viewport != s.viewport) {
1780 disp.viewport = s.viewport;
1781 flags |= eDisplayTransactionNeeded;
1782 }
1783 }
1784 }
1785 return flags;
1786}
1787
1788uint32_t SurfaceFlinger::setClientStateLocked(
1789 const sp<Client>& client,
1790 const layer_state_t& s)
1791{
1792 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001793 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001794 if (layer != 0) {
1795 const uint32_t what = s.what;
1796 if (what & layer_state_t::ePositionChanged) {
1797 if (layer->setPosition(s.x, s.y))
1798 flags |= eTraversalNeeded;
1799 }
1800 if (what & layer_state_t::eLayerChanged) {
1801 // NOTE: index needs to be calculated before we update the state
1802 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1803 if (layer->setLayer(s.z)) {
1804 mCurrentState.layersSortedByZ.removeAt(idx);
1805 mCurrentState.layersSortedByZ.add(layer);
1806 // we need traversal (state changed)
1807 // AND transaction (list changed)
1808 flags |= eTransactionNeeded|eTraversalNeeded;
1809 }
1810 }
1811 if (what & layer_state_t::eSizeChanged) {
1812 if (layer->setSize(s.w, s.h)) {
1813 flags |= eTraversalNeeded;
1814 }
1815 }
1816 if (what & layer_state_t::eAlphaChanged) {
1817 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1818 flags |= eTraversalNeeded;
1819 }
1820 if (what & layer_state_t::eMatrixChanged) {
1821 if (layer->setMatrix(s.matrix))
1822 flags |= eTraversalNeeded;
1823 }
1824 if (what & layer_state_t::eTransparentRegionChanged) {
1825 if (layer->setTransparentRegionHint(s.transparentRegion))
1826 flags |= eTraversalNeeded;
1827 }
1828 if (what & layer_state_t::eVisibilityChanged) {
1829 if (layer->setFlags(s.flags, s.mask))
1830 flags |= eTraversalNeeded;
1831 }
1832 if (what & layer_state_t::eCropChanged) {
1833 if (layer->setCrop(s.crop))
1834 flags |= eTraversalNeeded;
1835 }
1836 if (what & layer_state_t::eLayerStackChanged) {
1837 // NOTE: index needs to be calculated before we update the state
1838 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1839 if (layer->setLayerStack(s.layerStack)) {
1840 mCurrentState.layersSortedByZ.removeAt(idx);
1841 mCurrentState.layersSortedByZ.add(layer);
1842 // we need traversal (state changed)
1843 // AND transaction (list changed)
1844 flags |= eTransactionNeeded|eTraversalNeeded;
1845 }
1846 }
1847 }
1848 return flags;
1849}
1850
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001851status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001852 const String8& name,
1853 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001854 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1855 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001856{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001857 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001858 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001859 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001860 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001861 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001862 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001863
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001864 status_t result = NO_ERROR;
1865
1866 sp<Layer> layer;
1867
Mathias Agopian3165cc22012-08-08 19:42:09 -07001868 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1869 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001870 result = createNormalLayer(client,
1871 name, w, h, flags, format,
1872 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001874 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001875 result = createDimLayer(client,
1876 name, w, h, flags,
1877 handle, gbp, &layer);
1878 break;
1879 default:
1880 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 break;
1882 }
1883
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001884 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001885 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001886 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001888 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889}
1890
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001891status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1892 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1893 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894{
1895 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001896 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 case PIXEL_FORMAT_TRANSPARENT:
1898 case PIXEL_FORMAT_TRANSLUCENT:
1899 format = PIXEL_FORMAT_RGBA_8888;
1900 break;
1901 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001902#ifdef NO_RGBX_8888
1903 format = PIXEL_FORMAT_RGB_565;
1904#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001905 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001906#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 break;
1908 }
1909
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001910#ifdef NO_RGBX_8888
1911 if (format == PIXEL_FORMAT_RGBX_8888)
1912 format = PIXEL_FORMAT_RGBA_8888;
1913#endif
1914
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001915 *outLayer = new Layer(this, client, name, w, h, flags);
1916 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1917 if (err == NO_ERROR) {
1918 *handle = (*outLayer)->getHandle();
1919 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001921
1922 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1923 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924}
1925
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001926status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1927 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1928 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001930 *outLayer = new LayerDim(this, client, name, w, h, flags);
1931 *handle = (*outLayer)->getHandle();
1932 *gbp = (*outLayer)->getBufferQueue();
1933 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001934}
1935
Mathias Agopianac9fa422013-02-11 16:40:36 -08001936status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937{
Mathias Agopian67106042013-03-14 19:18:13 -07001938 // called by the window manager when it wants to remove a Layer
1939 status_t err = NO_ERROR;
1940 sp<Layer> l(client->getLayerUser(handle));
1941 if (l != NULL) {
1942 err = removeLayer(l);
1943 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1944 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001945 }
1946 return err;
1947}
1948
Mathias Agopian13127d82013-03-05 17:47:11 -08001949status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001950{
Mathias Agopian67106042013-03-14 19:18:13 -07001951 // called by ~LayerCleaner() when all references to the IBinder (handle)
1952 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001953 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001954 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001955 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001956 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001957 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001958 "error removing layer=%p (%s)", l.get(), strerror(-err));
1959 }
1960 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961}
1962
Mathias Agopianb60314a2012-04-10 22:09:54 -07001963// ---------------------------------------------------------------------------
1964
Andy McFadden13a082e2012-08-24 10:16:42 -07001965void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001966 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001967 Vector<ComposerState> state;
1968 Vector<DisplayState> displays;
1969 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001970 d.what = DisplayState::eDisplayProjectionChanged |
1971 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001972 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001973 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001974 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001975 d.frame.makeInvalid();
1976 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001977 displays.add(d);
1978 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001979 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001980
1981 const nsecs_t period =
1982 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1983 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001984}
1985
1986void SurfaceFlinger::initializeDisplays() {
1987 class MessageScreenInitialized : public MessageBase {
1988 SurfaceFlinger* flinger;
1989 public:
1990 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1991 virtual bool handler() {
1992 flinger->onInitializeDisplays();
1993 return true;
1994 }
1995 };
1996 sp<MessageBase> msg = new MessageScreenInitialized(this);
1997 postMessageAsync(msg); // we may be called from main thread, use async message
1998}
1999
2000
Mathias Agopiancde87a32012-09-13 14:09:01 -07002001void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002002 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2003 if (hw->isScreenAcquired()) {
2004 // this is expected, e.g. when power manager wakes up during boot
2005 ALOGD(" screen was previously acquired");
2006 return;
2007 }
2008
Mathias Agopian42977342012-08-05 00:40:46 -07002009 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002010 int32_t type = hw->getDisplayType();
2011 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2012 // built-in display, tell the HWC
2013 getHwComposer().acquire(type);
2014
2015 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2016 // FIXME: eventthread only knows about the main display right now
2017 mEventThread->onScreenAcquired();
2018 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002019 }
Mathias Agopian20128302012-08-13 18:32:13 -07002020 mVisibleRegionsDirty = true;
2021 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002022}
2023
Mathias Agopiancde87a32012-09-13 14:09:01 -07002024void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002025 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2026 if (!hw->isScreenAcquired()) {
2027 ALOGD(" screen was previously released");
2028 return;
2029 }
2030
2031 hw->releaseScreen();
2032 int32_t type = hw->getDisplayType();
2033 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2034 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002035 // FIXME: eventthread only knows about the main display right now
2036 mEventThread->onScreenReleased();
2037 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002038
2039 // built-in display, tell the HWC
2040 getHwComposer().release(type);
2041 }
2042 mVisibleRegionsDirty = true;
2043 // from this point on, SF will stop drawing on this display
2044}
2045
2046void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2047 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002048 SurfaceFlinger& mFlinger;
2049 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002050 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002051 MessageScreenAcquired(SurfaceFlinger& flinger,
2052 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002053 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002054 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2055 if (hw == NULL) {
2056 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2057 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2058 ALOGW("Attempt to unblank virtual display");
2059 } else {
2060 mFlinger.onScreenAcquired(hw);
2061 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002062 return true;
2063 }
2064 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002065 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2066 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067}
2068
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002069void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002070 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002071 SurfaceFlinger& mFlinger;
2072 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002073 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002074 MessageScreenReleased(SurfaceFlinger& flinger,
2075 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002076 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002077 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2078 if (hw == NULL) {
2079 ALOGE("Attempt to blank null display %p", mDisplay.get());
2080 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2081 ALOGW("Attempt to blank virtual display");
2082 } else {
2083 mFlinger.onScreenReleased(hw);
2084 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002085 return true;
2086 }
2087 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002088 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2089 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002090}
2091
2092// ---------------------------------------------------------------------------
2093
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002094status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2095{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002096 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002097
Mathias Agopianbd115332013-04-18 16:41:04 -07002098 IPCThreadState* ipc = IPCThreadState::self();
2099 const int pid = ipc->getCallingPid();
2100 const int uid = ipc->getCallingUid();
2101 if ((uid != AID_SHELL) &&
2102 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002103 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002104 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002105 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002106 // Try to get the main lock, but don't insist if we can't
2107 // (this would indicate SF is stuck, but we want to be able to
2108 // print something in dumpsys).
2109 int retry = 3;
2110 while (mStateLock.tryLock()<0 && --retry>=0) {
2111 usleep(1000000);
2112 }
2113 const bool locked(retry >= 0);
2114 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002115 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002116 "SurfaceFlinger appears to be unresponsive, "
2117 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002118 }
2119
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002120 bool dumpAll = true;
2121 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002122 size_t numArgs = args.size();
2123 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002124 if ((index < numArgs) &&
2125 (args[index] == String16("--list"))) {
2126 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002127 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002128 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002129 }
2130
2131 if ((index < numArgs) &&
2132 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002133 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002134 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002135 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002136 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002137
2138 if ((index < numArgs) &&
2139 (args[index] == String16("--latency-clear"))) {
2140 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002141 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002142 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002145
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002146 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002147 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002148 }
2149
Mathias Agopian9795c422009-08-26 16:36:26 -07002150 if (locked) {
2151 mStateLock.unlock();
2152 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002153 }
2154 write(fd, result.string(), result.size());
2155 return NO_ERROR;
2156}
2157
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002158void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002159 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002160{
2161 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2162 const size_t count = currentLayers.size();
2163 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002164 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002165 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002166 }
2167}
2168
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002169void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002170 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002171{
2172 String8 name;
2173 if (index < args.size()) {
2174 name = String8(args[index]);
2175 index++;
2176 }
2177
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002178 const nsecs_t period =
2179 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2180 result.appendFormat("%lld\n", period);
2181
2182 if (name.isEmpty()) {
2183 mAnimFrameTracker.dump(result);
2184 } else {
2185 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2186 const size_t count = currentLayers.size();
2187 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002188 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002189 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002190 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002191 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002192 }
2193 }
2194}
2195
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002196void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002197 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002198{
2199 String8 name;
2200 if (index < args.size()) {
2201 name = String8(args[index]);
2202 index++;
2203 }
2204
2205 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2206 const size_t count = currentLayers.size();
2207 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002208 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002209 if (name.isEmpty() || (name == layer->getName())) {
2210 layer->clearStats();
2211 }
2212 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002213
2214 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002215}
2216
Jamie Gennis6547ff42013-07-16 20:12:42 -07002217// This should only be called from the main thread. Otherwise it would need
2218// the lock and should use mCurrentState rather than mDrawingState.
2219void SurfaceFlinger::logFrameStats() {
2220 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2221 const size_t count = drawingLayers.size();
2222 for (size_t i=0 ; i<count ; i++) {
2223 const sp<Layer>& layer(drawingLayers[i]);
2224 layer->logFrameStats();
2225 }
2226
2227 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2228}
2229
Andy McFadden4803b742012-09-24 19:07:20 -07002230/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2231{
2232 static const char* config =
2233 " [sf"
2234#ifdef NO_RGBX_8888
2235 " NO_RGBX_8888"
2236#endif
2237#ifdef HAS_CONTEXT_PRIORITY
2238 " HAS_CONTEXT_PRIORITY"
2239#endif
2240#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2241 " NEVER_DEFAULT_TO_ASYNC_MODE"
2242#endif
2243#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2244 " TARGET_DISABLE_TRIPLE_BUFFERING"
2245#endif
2246 "]";
2247 result.append(config);
2248}
2249
Mathias Agopian74d211a2013-04-22 16:55:35 +02002250void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2251 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002252{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002253 bool colorize = false;
2254 if (index < args.size()
2255 && (args[index] == String16("--color"))) {
2256 colorize = true;
2257 index++;
2258 }
2259
2260 Colorizer colorizer(colorize);
2261
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002262 // figure out if we're stuck somewhere
2263 const nsecs_t now = systemTime();
2264 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2265 const nsecs_t inTransaction(mDebugInTransaction);
2266 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2267 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2268
2269 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002270 * Dump library configuration.
2271 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002272
2273 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002274 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002275 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002276 appendSfConfigString(result);
2277 appendUiConfigString(result);
2278 appendGuiConfigString(result);
2279 result.append("\n");
2280
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002281 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002282 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002283 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002284 result.append(SyncFeatures::getInstance().toString());
2285 result.append("\n");
2286
Andy McFadden4803b742012-09-24 19:07:20 -07002287 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002288 * Dump the visible layer list
2289 */
2290 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2291 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002292 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002293 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002294 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002295 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002296 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002297 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002298 }
2299
2300 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002301 * Dump Display state
2302 */
2303
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002304 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002305 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002306 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002307 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2308 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002309 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002310 }
2311
2312 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002313 * Dump SurfaceFlinger global state
2314 */
2315
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002316 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002317 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002318 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002319
Mathias Agopian888c8222012-08-04 21:10:38 -07002320 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002321 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002322
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002323 colorizer.bold(result);
2324 result.appendFormat("EGL implementation : %s\n",
2325 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2326 colorizer.reset(result);
2327 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002328 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002329
Mathias Agopian875d8e12013-06-07 15:35:48 -07002330 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002331
Mathias Agopian42977342012-08-05 00:40:46 -07002332 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002333 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002334 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002335 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002336 " last eglSwapBuffers() time: %f us\n"
2337 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002338 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002339 " refresh-rate : %f fps\n"
2340 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002341 " y-dpi : %f\n"
2342 " EGL_NATIVE_VISUAL_ID : %d\n"
2343 " gpu_to_cpu_unsupported : %d\n"
2344 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345 mLastSwapBufferTime/1000.0,
2346 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002347 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002348 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2349 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002350 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2351 mEGLNativeVisualId,
2352 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002353
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002355 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356
Mathias Agopian74d211a2013-04-22 16:55:35 +02002357 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002359
2360 /*
2361 * VSYNC state
2362 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002363 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002364
2365 /*
2366 * Dump HWComposer state
2367 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002368 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002369 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002370 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002371 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002372 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2373 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002374 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002375
2376 /*
2377 * Dump gralloc state
2378 */
2379 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2380 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002381}
2382
Mathias Agopian13127d82013-03-05 17:47:11 -08002383const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002384SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002385 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002386 wp<IBinder> dpy;
2387 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2388 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2389 dpy = mDisplays.keyAt(i);
2390 break;
2391 }
2392 }
2393 if (dpy == NULL) {
2394 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2395 // Just use the primary display so we have something to return
2396 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2397 }
2398 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002399}
2400
Keun young Park63f165f2012-08-31 10:53:36 -07002401bool SurfaceFlinger::startDdmConnection()
2402{
2403 void* libddmconnection_dso =
2404 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2405 if (!libddmconnection_dso) {
2406 return false;
2407 }
2408 void (*DdmConnection_start)(const char* name);
2409 DdmConnection_start =
2410 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2411 if (!DdmConnection_start) {
2412 dlclose(libddmconnection_dso);
2413 return false;
2414 }
2415 (*DdmConnection_start)(getServiceName());
2416 return true;
2417}
2418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419status_t SurfaceFlinger::onTransact(
2420 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2421{
2422 switch (code) {
2423 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002424 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002425 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002426 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002427 case BLANK:
2428 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002429 {
2430 // codes that require permission check
2431 IPCThreadState* ipc = IPCThreadState::self();
2432 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002433 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002434 if ((uid != AID_GRAPHICS) &&
2435 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002436 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002437 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2438 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002439 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440 break;
2441 }
2442 case CAPTURE_SCREEN:
2443 {
2444 // codes that require permission check
2445 IPCThreadState* ipc = IPCThreadState::self();
2446 const int pid = ipc->getCallingPid();
2447 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002448 if ((uid != AID_GRAPHICS) &&
2449 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002450 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002451 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2452 return PERMISSION_DENIED;
2453 }
2454 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002455 }
2456 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002458 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2459 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002460 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002461 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002462 IPCThreadState* ipc = IPCThreadState::self();
2463 const int pid = ipc->getCallingPid();
2464 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002465 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002466 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002467 return PERMISSION_DENIED;
2468 }
2469 int n;
2470 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002471 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002472 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 return NO_ERROR;
2474 case 1002: // SHOW_UPDATES
2475 n = data.readInt32();
2476 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002477 invalidateHwcGeometry();
2478 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002481 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002482 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002484 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002485 setTransactionFlags(
2486 eTransactionNeeded|
2487 eDisplayTransactionNeeded|
2488 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002489 return NO_ERROR;
2490 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002491 case 1006:{ // send empty update
2492 signalRefresh();
2493 return NO_ERROR;
2494 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002495 case 1008: // toggle use of hw composer
2496 n = data.readInt32();
2497 mDebugDisableHWC = n ? 1 : 0;
2498 invalidateHwcGeometry();
2499 repaintEverything();
2500 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002501 case 1009: // toggle use of transform hint
2502 n = data.readInt32();
2503 mDebugDisableTransformHint = n ? 1 : 0;
2504 invalidateHwcGeometry();
2505 repaintEverything();
2506 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002508 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002509 reply->writeInt32(0);
2510 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002511 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002512 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002513 return NO_ERROR;
2514 case 1013: {
2515 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002516 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2517 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518 }
2519 return NO_ERROR;
2520 }
2521 }
2522 return err;
2523}
2524
Mathias Agopian53331da2011-08-22 21:44:41 -07002525void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002526 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002527 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002528}
2529
Mathias Agopian59119e62010-10-11 12:37:43 -07002530// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002531// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002532// ---------------------------------------------------------------------------
2533
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002534/* The code below is here to handle b/8734824
2535 *
2536 * We create a IGraphicBufferProducer wrapper that forwards all calls
2537 * to the calling binder thread, where they are executed. This allows
2538 * the calling thread to be reused (on the other side) and not
2539 * depend on having "enough" binder threads to handle the requests.
2540 *
2541 */
2542
2543class GraphicProducerWrapper : public BBinder, public MessageHandler {
2544 sp<IGraphicBufferProducer> impl;
2545 sp<Looper> looper;
2546 status_t result;
2547 bool exitPending;
2548 bool exitRequested;
2549 mutable Barrier barrier;
2550 volatile int32_t memoryBarrier;
2551 uint32_t code;
2552 Parcel const* data;
2553 Parcel* reply;
2554
2555 enum {
2556 MSG_API_CALL,
2557 MSG_EXIT
2558 };
2559
2560 /*
2561 * this is called by our "fake" BpGraphicBufferProducer. We package the
2562 * data and reply Parcel and forward them to the calling thread.
2563 */
2564 virtual status_t transact(uint32_t code,
2565 const Parcel& data, Parcel* reply, uint32_t flags) {
2566 this->code = code;
2567 this->data = &data;
2568 this->reply = reply;
2569 android_atomic_acquire_store(0, &memoryBarrier);
2570 if (exitPending) {
2571 // if we've exited, we run the message synchronously right here
2572 handleMessage(Message(MSG_API_CALL));
2573 } else {
2574 barrier.close();
2575 looper->sendMessage(this, Message(MSG_API_CALL));
2576 barrier.wait();
2577 }
2578 return NO_ERROR;
2579 }
2580
2581 /*
2582 * here we run on the binder calling thread. All we've got to do is
2583 * call the real BpGraphicBufferProducer.
2584 */
2585 virtual void handleMessage(const Message& message) {
2586 android_atomic_release_load(&memoryBarrier);
2587 if (message.what == MSG_API_CALL) {
2588 impl->asBinder()->transact(code, data[0], reply);
2589 barrier.open();
2590 } else if (message.what == MSG_EXIT) {
2591 exitRequested = true;
2592 }
2593 }
2594
2595public:
2596 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2597 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2598 exitPending(false), exitRequested(false) {
2599 }
2600
2601 status_t waitForResponse() {
2602 do {
2603 looper->pollOnce(-1);
2604 } while (!exitRequested);
2605 return result;
2606 }
2607
2608 void exit(status_t result) {
2609 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