blob: b38ad104789545983fcc71eac767d3a4cc238dde [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 Agopiandb89edc2013-08-02 01:40:18 -0700454 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
455 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700456 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700457 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700458 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700459 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700460 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700461 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700462 // for displays other than the main display, so we always
463 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700464 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700465 hw->acquireScreen();
466 }
467 mDisplays.add(token, hw);
468 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700469 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700470
Mathias Agopian028508c2012-07-25 21:12:12 -0700471 // start the EventThread
472 mEventThread = new EventThread(this);
473 mEventQueue.setEventThread(mEventThread);
474
Mathias Agopian92a979a2012-08-02 18:32:23 -0700475 // initialize our drawing state
476 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700477
Mathias Agopiana4912602012-07-12 14:25:33 -0700478 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800479 mReadyToRunBarrier.open();
480
Andy McFadden13a082e2012-08-24 10:16:42 -0700481 // set initial conditions (e.g. unblank default device)
482 initializeDisplays();
483
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700484 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700485 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700486
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 return NO_ERROR;
488}
489
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700490int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
491 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
492 type : mHwc->allocateDisplayId();
493}
494
Mathias Agopiana67e4182012-06-19 17:26:12 -0700495void SurfaceFlinger::startBootAnim() {
496 // start boot animation
497 property_set("service.bootanim.exit", "0");
498 property_set("ctl.start", "bootanim");
499}
500
Mathias Agopian875d8e12013-06-07 15:35:48 -0700501size_t SurfaceFlinger::getMaxTextureSize() const {
502 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700503}
504
Mathias Agopian875d8e12013-06-07 15:35:48 -0700505size_t SurfaceFlinger::getMaxViewportDims() const {
506 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700507}
508
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800510
Jamie Gennis582270d2011-08-17 18:19:00 -0700511bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800512 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800513 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800514 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700515 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800516}
517
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700518status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800519 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700520 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800521 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700522 type = i;
523 break;
524 }
525 }
526
527 if (type < 0) {
528 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700529 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700530
531 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700532 float xdpi = hwc.getDpiX(type);
533 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700534
535 // TODO: Not sure if display density should handled by SF any longer
536 class Density {
537 static int getDensityFromProperty(char const* propName) {
538 char property[PROPERTY_VALUE_MAX];
539 int density = 0;
540 if (property_get(propName, property, NULL) > 0) {
541 density = atoi(property);
542 }
543 return density;
544 }
545 public:
546 static int getEmuDensity() {
547 return getDensityFromProperty("qemu.sf.lcd_density"); }
548 static int getBuildDensity() {
549 return getDensityFromProperty("ro.sf.lcd_density"); }
550 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700551
552 if (type == DisplayDevice::DISPLAY_PRIMARY) {
553 // The density of the device is provided by a build property
554 float density = Density::getBuildDensity() / 160.0f;
555 if (density == 0) {
556 // the build doesn't provide a density -- this is wrong!
557 // use xdpi instead
558 ALOGE("ro.sf.lcd_density must be defined as a build property");
559 density = xdpi / 160.0f;
560 }
561 if (Density::getEmuDensity()) {
562 // if "qemu.sf.lcd_density" is specified, it overrides everything
563 xdpi = ydpi = density = Density::getEmuDensity();
564 density /= 160.0f;
565 }
566 info->density = density;
567
568 // TODO: this needs to go away (currently needed only by webkit)
569 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
570 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700571 } else {
572 // TODO: where should this value come from?
573 static const int TV_DENSITY = 213;
574 info->density = TV_DENSITY / 160.0f;
575 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700576 }
577
Mathias Agopian1604f772012-09-18 21:54:42 -0700578 info->w = hwc.getWidth(type);
579 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700580 info->xdpi = xdpi;
581 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700582 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700583
584 // All non-virtual displays are currently considered secure.
585 info->secure = true;
586
Mathias Agopian888c8222012-08-04 21:10:38 -0700587 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700588}
589
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800590// ----------------------------------------------------------------------------
591
592sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800593 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700594}
595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800597
598void SurfaceFlinger::waitForEvent() {
599 mEventQueue.waitMessage();
600}
601
602void SurfaceFlinger::signalTransaction() {
603 mEventQueue.invalidate();
604}
605
606void SurfaceFlinger::signalLayerUpdate() {
607 mEventQueue.invalidate();
608}
609
610void SurfaceFlinger::signalRefresh() {
611 mEventQueue.refresh();
612}
613
614status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
615 nsecs_t reltime, uint32_t flags) {
616 return mEventQueue.postMessage(msg, reltime);
617}
618
619status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
620 nsecs_t reltime, uint32_t flags) {
621 status_t res = mEventQueue.postMessage(msg, reltime);
622 if (res == NO_ERROR) {
623 msg->wait();
624 }
625 return res;
626}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopian4fec8732012-06-29 14:12:52 -0700628bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 return true;
631}
632
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700633void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700634 if (mEventThread == NULL) {
635 // This is a temporary workaround for b/7145521. A non-null pointer
636 // does not mean EventThread has finished initializing, so this
637 // is not a correct fix.
638 ALOGW("WARNING: EventThread not started, ignoring vsync");
639 return;
640 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700641 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
642 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700643 mEventThread->onVSyncReceived(type, timestamp);
644 }
645}
646
647void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
648 if (mEventThread == NULL) {
649 // This is a temporary workaround for b/7145521. A non-null pointer
650 // does not mean EventThread has finished initializing, so this
651 // is not a correct fix.
652 ALOGW("WARNING: EventThread not started, ignoring hotplug");
653 return;
654 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700655
Mathias Agopian148994e2012-09-19 17:31:36 -0700656 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700657 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800658 if (connected) {
659 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700660 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800661 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
662 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700663 }
664 setTransactionFlags(eDisplayTransactionNeeded);
665
Andy McFadden9e9689c2012-10-10 18:17:51 -0700666 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700667 }
Mathias Agopian86303202012-07-24 22:46:10 -0700668}
669
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700670void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
671 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700672}
673
Mathias Agopian4fec8732012-06-29 14:12:52 -0700674void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800675 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800676 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700677 case MessageQueue::TRANSACTION:
678 handleMessageTransaction();
679 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700680 case MessageQueue::INVALIDATE:
681 handleMessageTransaction();
682 handleMessageInvalidate();
683 signalRefresh();
684 break;
685 case MessageQueue::REFRESH:
686 handleMessageRefresh();
687 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800688 }
689}
690
Mathias Agopian4fec8732012-06-29 14:12:52 -0700691void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700692 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700693 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700694 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700695 }
696}
697
698void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700699 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700700 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700701}
702
703void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700704 ATRACE_CALL();
705 preComposition();
706 rebuildLayerStacks();
707 setUpHWComposer();
708 doDebugFlashRegions();
709 doComposition();
710 postComposition();
711}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700712
Mathias Agopiancd60f992012-08-16 16:28:27 -0700713void SurfaceFlinger::doDebugFlashRegions()
714{
715 // is debugging enabled
716 if (CC_LIKELY(!mDebugRegion))
717 return;
718
719 const bool repaintEverything = mRepaintEverything;
720 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
721 const sp<DisplayDevice>& hw(mDisplays[dpy]);
722 if (hw->canDraw()) {
723 // transform the dirty region into this screen's coordinate space
724 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
725 if (!dirtyRegion.isEmpty()) {
726 // redraw the whole screen
727 doComposeSurfaces(hw, Region(hw->bounds()));
728
729 // and draw the dirty region
730 glDisable(GL_TEXTURE_EXTERNAL_OES);
731 glDisable(GL_TEXTURE_2D);
732 glDisable(GL_BLEND);
733 glColor4f(1, 0, 1, 1);
734 const int32_t height = hw->getHeight();
735 Region::const_iterator it = dirtyRegion.begin();
736 Region::const_iterator const end = dirtyRegion.end();
737 while (it != end) {
738 const Rect& r = *it++;
739 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800740 { (GLfloat) r.left, (GLfloat) (height - r.top) },
741 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
742 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
743 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700744 };
745 glVertexPointer(2, GL_FLOAT, 0, vertices);
746 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
747 }
748 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700749 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700750 }
751 }
752 }
753
754 postFramebuffer();
755
756 if (mDebugRegion > 1) {
757 usleep(mDebugRegion * 1000);
758 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700759
760 HWComposer& hwc(getHwComposer());
761 if (hwc.initCheck() == NO_ERROR) {
762 status_t err = hwc.prepare();
763 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
764 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700765}
766
767void SurfaceFlinger::preComposition()
768{
769 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700770 const LayerVector& layers(mDrawingState.layersSortedByZ);
771 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700772 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700773 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700774 needExtraInvalidate = true;
775 }
776 }
777 if (needExtraInvalidate) {
778 signalLayerUpdate();
779 }
780}
781
782void SurfaceFlinger::postComposition()
783{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700784 const LayerVector& layers(mDrawingState.layersSortedByZ);
785 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700787 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700788 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800789
790 if (mAnimCompositionPending) {
791 mAnimCompositionPending = false;
792
793 const HWComposer& hwc = getHwComposer();
794 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800795 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800796 mAnimFrameTracker.setActualPresentFence(presentFence);
797 } else {
798 // The HWC doesn't support present fences, so use the refresh
799 // timestamp instead.
800 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
801 mAnimFrameTracker.setActualPresentTime(presentTime);
802 }
803 mAnimFrameTracker.advanceFrame();
804 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805}
806
807void SurfaceFlinger::rebuildLayerStacks() {
808 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700809 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700810 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700811 mVisibleRegionsDirty = false;
812 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700813
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700814 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700815 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700816 Region opaqueRegion;
817 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800818 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700819 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700820 const Transform& tr(hw->getTransform());
821 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700822 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700823 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700824 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700825
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700826 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700827 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700828 const sp<Layer>& layer(layers[i]);
829 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700830 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700831 Region drawRegion(tr.transform(
832 layer->visibleNonTransparentRegion));
833 drawRegion.andSelf(bounds);
834 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700835 layersSortedByZ.add(layer);
836 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700837 }
838 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700839 }
Mathias Agopian42977342012-08-05 00:40:46 -0700840 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700841 hw->undefinedRegion.set(bounds);
842 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
843 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700844 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700845 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700846}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700847
Mathias Agopiancd60f992012-08-16 16:28:27 -0700848void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 HWComposer& hwc(getHwComposer());
850 if (hwc.initCheck() == NO_ERROR) {
851 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700852 if (CC_UNLIKELY(mHwWorkListDirty)) {
853 mHwWorkListDirty = false;
854 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
855 sp<const DisplayDevice> hw(mDisplays[dpy]);
856 const int32_t id = hw->getHwcDisplayId();
857 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700859 hw->getVisibleLayersSortedByZ());
860 const size_t count = currentLayers.size();
861 if (hwc.createWorkList(id, count) == NO_ERROR) {
862 HWComposer::LayerListIterator cur = hwc.begin(id);
863 const HWComposer::LayerListIterator end = hwc.end(id);
864 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800865 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700866 layer->setGeometry(hw, *cur);
867 if (mDebugDisableHWC || mDebugRegion) {
868 cur->setSkip(true);
869 }
870 }
871 }
872 }
873 }
874 }
875
876 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700877 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700878 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700879 const int32_t id = hw->getHwcDisplayId();
880 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700882 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700883 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700884 HWComposer::LayerListIterator cur = hwc.begin(id);
885 const HWComposer::LayerListIterator end = hwc.end(id);
886 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
887 /*
888 * update the per-frame h/w composer data for each layer
889 * and build the transparent region of the FB
890 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800891 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700892 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700893 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700894 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700895 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700896
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700897 status_t err = hwc.prepare();
898 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700899
900 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
901 sp<const DisplayDevice> hw(mDisplays[dpy]);
902 hw->prepareFrame(hwc);
903 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700904 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700906
Mathias Agopiancd60f992012-08-16 16:28:27 -0700907void SurfaceFlinger::doComposition() {
908 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700909 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700910 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700911 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 if (hw->canDraw()) {
913 // transform the dirty region into this screen's coordinate space
914 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800915
916 // repaint the framebuffer (if needed)
917 doDisplayComposition(hw, dirtyRegion);
918
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919 hw->dirtyRegion.clear();
920 hw->flip(hw->swapRegion);
921 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700922 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700923 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700924 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700925 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700926 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700927}
928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929void SurfaceFlinger::postFramebuffer()
930{
Mathias Agopian841cde52012-03-01 15:44:37 -0800931 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800932
Mathias Agopiana44b0412011-10-16 18:46:35 -0700933 const nsecs_t now = systemTime();
934 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700935
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700936 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700937 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700938 if (!hwc.supportsFramebufferTarget()) {
939 // EGL spec says:
940 // "surface must be bound to the calling thread's current context,
941 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700942 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700943 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700944 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945 }
946
Mathias Agopian92a979a2012-08-02 18:32:23 -0700947 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700948 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800949 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700950 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700951 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700952 int32_t id = hw->getHwcDisplayId();
953 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700954 HWComposer::LayerListIterator cur = hwc.begin(id);
955 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700956 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700957 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700958 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700959 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700960 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700961 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700962 }
Jesse Hallef194142012-06-14 14:45:17 -0700963 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800964 }
965
Mathias Agopiana44b0412011-10-16 18:46:35 -0700966 mLastSwapBufferTime = systemTime() - now;
967 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700968
969 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
970 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
971 logFrameStats();
972 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973}
974
Mathias Agopian87baae12012-07-31 12:38:26 -0700975void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976{
Mathias Agopian841cde52012-03-01 15:44:37 -0800977 ATRACE_CALL();
978
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700979 // here we keep a copy of the drawing state (that is the state that's
980 // going to be overwritten by handleTransactionLocked()) outside of
981 // mStateLock so that the side-effects of the State assignment
982 // don't happen with mStateLock held (which can cause deadlocks).
983 State drawingState(mDrawingState);
984
Mathias Agopianca4d3602011-05-19 15:38:14 -0700985 Mutex::Autolock _l(mStateLock);
986 const nsecs_t now = systemTime();
987 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988
Mathias Agopianca4d3602011-05-19 15:38:14 -0700989 // Here we're guaranteed that some transaction flags are set
990 // so we can call handleTransactionLocked() unconditionally.
991 // We call getTransactionFlags(), which will also clear the flags,
992 // with mStateLock held to guarantee that mCurrentState won't change
993 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700994
Mathias Agopiane57f2922012-08-09 16:29:12 -0700995 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700996 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700997
Mathias Agopianca4d3602011-05-19 15:38:14 -0700998 mLastTransactionTime = systemTime() - now;
999 mDebugInTransaction = 0;
1000 invalidateHwcGeometry();
1001 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001002}
1003
Mathias Agopian87baae12012-07-31 12:38:26 -07001004void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001005{
1006 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 const size_t count = currentLayers.size();
1008
1009 /*
1010 * Traversal of the children
1011 * (perform the transaction for each of them if needed)
1012 */
1013
Mathias Agopian3559b072012-08-15 13:46:03 -07001014 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001016 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1018 if (!trFlags) continue;
1019
1020 const uint32_t flags = layer->doTransaction(0);
1021 if (flags & Layer::eVisibleRegion)
1022 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 }
1024 }
1025
1026 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001027 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 */
1029
Mathias Agopiane57f2922012-08-09 16:29:12 -07001030 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001031 // here we take advantage of Vector's copy-on-write semantics to
1032 // improve performance by skipping the transaction entirely when
1033 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001034 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1035 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001036 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001038 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001039 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001040
1041 // find the displays that were removed
1042 // (ie: in drawing state but not in current state)
1043 // also handle displays that changed
1044 // (ie: displays that are in both lists)
1045 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001046 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1047 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001048 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001049 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001050 // Call makeCurrent() on the primary display so we can
1051 // be sure that nothing associated with this display
1052 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001053 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001054 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001055 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1056 if (hw != NULL)
1057 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001058 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1059 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001060 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 } else {
1062 ALOGW("trying to remove the main display");
1063 }
1064 } else {
1065 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001066 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001067 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001068 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001069 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001070 // recreating the DisplayDevice, so we just remove it
1071 // from the drawing state, so that it get re-added
1072 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001073 sp<DisplayDevice> hw(getDisplayDevice(display));
1074 if (hw != NULL)
1075 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001076 mDisplays.removeItem(display);
1077 mDrawingState.displays.removeItemsAt(i);
1078 dc--; i--;
1079 // at this point we must loop to the next item
1080 continue;
1081 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001082
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001083 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001084 if (disp != NULL) {
1085 if (state.layerStack != draw[i].layerStack) {
1086 disp->setLayerStack(state.layerStack);
1087 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001088 if ((state.orientation != draw[i].orientation)
1089 || (state.viewport != draw[i].viewport)
1090 || (state.frame != draw[i].frame))
1091 {
1092 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001093 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001094 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001095 }
1096 }
1097 }
1098
1099 // find displays that were added
1100 // (ie: in current state but not in drawing state)
1101 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001102 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001103 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001104
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001105 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001106 sp<IGraphicBufferProducer> producer;
1107 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1108
Jesse Hall02d86562013-03-25 14:43:23 -07001109 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001110 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001111 // Virtual displays without a surface are dormant:
1112 // they have external state (layer stack, projection,
1113 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001114 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001115
Jesse Hall02d86562013-03-25 14:43:23 -07001116 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001117 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1118 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001119 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001120
1121 dispSurface = vds;
1122 if (hwcDisplayId >= 0) {
1123 producer = vds;
1124 } else {
1125 // There won't be any interaction with HWC for this virtual display,
1126 // so the GLES driver can pass buffers directly to the sink.
1127 producer = state.surface;
1128 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001129 }
1130 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001131 ALOGE_IF(state.surface!=NULL,
1132 "adding a supported display, but rendering "
1133 "surface is provided (%p), ignoring it",
1134 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001135 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001136 // for supported (by hwc) displays we provide our
1137 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001138 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1139 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001140 }
1141
1142 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001143 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001144 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001145 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001146 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001147 hw->setLayerStack(state.layerStack);
1148 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001149 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001150 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001151 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001152 if (state.isVirtualDisplay()) {
1153 if (hwcDisplayId >= 0) {
1154 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1155 hw->getWidth(), hw->getHeight(),
1156 hw->getFormat());
1157 }
1158 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001159 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001160 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001161 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001162 }
1163 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001165 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166
Mathias Agopian84300952012-11-21 16:02:13 -08001167 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1168 // The transform hint might have changed for some layers
1169 // (either because a display has changed, or because a layer
1170 // as changed).
1171 //
1172 // Walk through all the layers in currentLayers,
1173 // and update their transform hint.
1174 //
1175 // If a layer is visible only on a single display, then that
1176 // display is used to calculate the hint, otherwise we use the
1177 // default display.
1178 //
1179 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1180 // the hint is set before we acquire a buffer from the surface texture.
1181 //
1182 // NOTE: layer transactions have taken place already, so we use their
1183 // drawing state. However, SurfaceFlinger's own transaction has not
1184 // happened yet, so we must use the current state layer list
1185 // (soon to become the drawing state list).
1186 //
1187 sp<const DisplayDevice> disp;
1188 uint32_t currentlayerStack = 0;
1189 for (size_t i=0; i<count; i++) {
1190 // NOTE: we rely on the fact that layers are sorted by
1191 // layerStack first (so we don't have to traverse the list
1192 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001193 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001194 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001195 if (i==0 || currentlayerStack != layerStack) {
1196 currentlayerStack = layerStack;
1197 // figure out if this layerstack is mirrored
1198 // (more than one display) if so, pick the default display,
1199 // if not, pick the only display it's on.
1200 disp.clear();
1201 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1202 sp<const DisplayDevice> hw(mDisplays[dpy]);
1203 if (hw->getLayerStack() == currentlayerStack) {
1204 if (disp == NULL) {
1205 disp = hw;
1206 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001207 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001208 break;
1209 }
1210 }
1211 }
1212 }
Chet Haase91d25932013-04-11 15:24:55 -07001213 if (disp == NULL) {
1214 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1215 // redraw after transform hint changes. See bug 8508397.
1216
1217 // could be null when this layer is using a layerStack
1218 // that is not visible on any display. Also can occur at
1219 // screen off/on times.
1220 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001221 }
Chet Haase91d25932013-04-11 15:24:55 -07001222 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001223 }
1224 }
1225
1226
Mathias Agopian3559b072012-08-15 13:46:03 -07001227 /*
1228 * Perform our own transaction if needed
1229 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001230
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001231 const LayerVector& layers(mDrawingState.layersSortedByZ);
1232 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001233 // layers have been added
1234 mVisibleRegionsDirty = true;
1235 }
1236
1237 // some layers might have been removed, so
1238 // we need to update the regions they're exposing.
1239 if (mLayersRemoved) {
1240 mLayersRemoved = false;
1241 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001242 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001243 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001244 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001245 if (currentLayers.indexOf(layer) < 0) {
1246 // this layer is not visible anymore
1247 // TODO: we could traverse the tree from front to back and
1248 // compute the actual visible region
1249 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001250 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001251 Region visibleReg = s.transform.transform(
1252 Region(Rect(s.active.w, s.active.h)));
1253 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001254 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 }
1257
1258 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001259}
1260
1261void SurfaceFlinger::commitTransaction()
1262{
1263 if (!mLayersPendingRemoval.isEmpty()) {
1264 // Notify removed layers now that they can't be drawn from
1265 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1266 mLayersPendingRemoval[i]->onRemoved();
1267 }
1268 mLayersPendingRemoval.clear();
1269 }
1270
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001271 // If this transaction is part of a window animation then the next frame
1272 // we composite should be considered an animation as well.
1273 mAnimCompositionPending = mAnimTransactionPending;
1274
Mathias Agopian4fec8732012-06-29 14:12:52 -07001275 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001276 mTransactionPending = false;
1277 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001278 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279}
1280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001282 const LayerVector& currentLayers, uint32_t layerStack,
1283 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284{
Mathias Agopian841cde52012-03-01 15:44:37 -08001285 ATRACE_CALL();
1286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 Region aboveOpaqueLayers;
1288 Region aboveCoveredLayers;
1289 Region dirty;
1290
Mathias Agopian87baae12012-07-31 12:38:26 -07001291 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292
1293 size_t i = currentLayers.size();
1294 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001295 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296
1297 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001298 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299
Jesse Hall01e29052013-02-19 16:13:35 -08001300 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001301 if (s.layerStack != layerStack)
1302 continue;
1303
Mathias Agopianab028732010-03-16 16:41:46 -07001304 /*
1305 * opaqueRegion: area of a surface that is fully opaque.
1306 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001308
1309 /*
1310 * visibleRegion: area of a surface that is visible on screen
1311 * and not fully transparent. This is essentially the layer's
1312 * footprint minus the opaque regions above it.
1313 * Areas covered by a translucent surface are considered visible.
1314 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001316
1317 /*
1318 * coveredRegion: area of a surface that is covered by all
1319 * visible regions above it (which includes the translucent areas).
1320 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001322
Jesse Halla8026d22012-09-25 13:25:04 -07001323 /*
1324 * transparentRegion: area of a surface that is hinted to be completely
1325 * transparent. This is only used to tell when the layer has no visible
1326 * non-transparent regions and can be removed from the layer list. It
1327 * does not affect the visibleRegion of this layer or any layers
1328 * beneath it. The hint may not be correct if apps don't respect the
1329 * SurfaceView restrictions (which, sadly, some don't).
1330 */
1331 Region transparentRegion;
1332
Mathias Agopianab028732010-03-16 16:41:46 -07001333
1334 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001335 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001336 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001337 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001339 if (!visibleRegion.isEmpty()) {
1340 // Remove the transparent area from the visible region
1341 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001342 const Transform tr(s.transform);
1343 if (tr.transformed()) {
1344 if (tr.preserveRects()) {
1345 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001346 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001347 } else {
1348 // transformation too complex, can't do the
1349 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001350 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001351 }
1352 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001353 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001354 }
Mathias Agopianab028732010-03-16 16:41:46 -07001355 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356
Mathias Agopianab028732010-03-16 16:41:46 -07001357 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001358 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001359 if (s.alpha==255 && !translucent &&
1360 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1361 // the opaque region is the layer's footprint
1362 opaqueRegion = visibleRegion;
1363 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 }
1365 }
1366
Mathias Agopianab028732010-03-16 16:41:46 -07001367 // Clip the covered region to the visible region
1368 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1369
1370 // Update aboveCoveredLayers for next (lower) layer
1371 aboveCoveredLayers.orSelf(visibleRegion);
1372
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 // subtract the opaque region covered by the layers above us
1374 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375
1376 // compute this layer's dirty region
1377 if (layer->contentDirty) {
1378 // we need to invalidate the whole region
1379 dirty = visibleRegion;
1380 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001381 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 layer->contentDirty = false;
1383 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001384 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001385 * the exposed region consists of two components:
1386 * 1) what's VISIBLE now and was COVERED before
1387 * 2) what's EXPOSED now less what was EXPOSED before
1388 *
1389 * note that (1) is conservative, we start with the whole
1390 * visible region but only keep what used to be covered by
1391 * something -- which mean it may have been exposed.
1392 *
1393 * (2) handles areas that were not covered by anything but got
1394 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001395 */
Mathias Agopianab028732010-03-16 16:41:46 -07001396 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001397 const Region oldVisibleRegion = layer->visibleRegion;
1398 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001399 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1400 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 }
1402 dirty.subtractSelf(aboveOpaqueLayers);
1403
1404 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001405 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406
Mathias Agopianab028732010-03-16 16:41:46 -07001407 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001409
Jesse Halla8026d22012-09-25 13:25:04 -07001410 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 layer->setVisibleRegion(visibleRegion);
1412 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001413 layer->setVisibleNonTransparentRegion(
1414 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 }
1416
Mathias Agopian87baae12012-07-31 12:38:26 -07001417 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418}
1419
Mathias Agopian87baae12012-07-31 12:38:26 -07001420void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1421 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001422 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001423 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1424 if (hw->getLayerStack() == layerStack) {
1425 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001426 }
1427 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001428}
1429
1430void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001432 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433
Mathias Agopian4fec8732012-06-29 14:12:52 -07001434 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001435 const LayerVector& layers(mDrawingState.layersSortedByZ);
1436 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001437 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001438 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001439 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001440 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001441 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001443
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001444 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445}
1446
Mathias Agopianad456f92011-01-13 17:53:01 -08001447void SurfaceFlinger::invalidateHwcGeometry()
1448{
1449 mHwWorkListDirty = true;
1450}
1451
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001452
Mathias Agopiancd60f992012-08-16 16:28:27 -07001453void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001454 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455{
Mathias Agopian87baae12012-07-31 12:38:26 -07001456 Region dirtyRegion(inDirtyRegion);
1457
Mathias Agopianb8a55602009-06-26 19:06:36 -07001458 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001459 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460
Mathias Agopian42977342012-08-05 00:40:46 -07001461 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001462 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001463 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1464 // takes a rectangle, we must make sure to update that whole
1465 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001466 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001467 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001468 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001469 // We need to redraw the rectangle that will be updated
1470 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001471 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001472 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001473 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001474 } else {
1475 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001476 dirtyRegion.set(hw->bounds());
1477 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 }
1479 }
1480
Mathias Agopiancd60f992012-08-16 16:28:27 -07001481 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001483 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001484 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001485
1486 // swap buffers (presentation)
1487 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488}
1489
Mathias Agopiancd60f992012-08-16 16:28:27 -07001490void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001491{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001492 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001493 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001494 HWComposer::LayerListIterator cur = hwc.begin(id);
1495 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001496
Mathias Agopian85d751c2012-08-29 16:59:24 -07001497 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1498 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001499 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001500 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1501 hw->getDisplayName().string());
1502 return;
1503 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001504
1505 // set the frame buffer
1506 glMatrixMode(GL_MODELVIEW);
1507 glLoadIdentity();
1508
1509 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001510 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001511 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001512 // when using overlays, we assume a fully transparent framebuffer
1513 // NOTE: we could reduce how much we need to clear, for instance
1514 // remove where there are opaque FB layers. however, on some
1515 // GPUs doing a "clean slate" glClear might be more efficient.
1516 // We'll revisit later if needed.
1517 glClearColor(0, 0, 0, 0);
1518 glClear(GL_COLOR_BUFFER_BIT);
1519 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001520 // we start with the whole screen area
1521 const Region bounds(hw->getBounds());
1522
1523 // we remove the scissor part
1524 // we're left with the letterbox region
1525 // (common case is that letterbox ends-up being empty)
1526 const Region letterbox(bounds.subtract(hw->getScissor()));
1527
1528 // compute the area to clear
1529 Region region(hw->undefinedRegion.merge(letterbox));
1530
1531 // but limit it to the dirty region
1532 region.andSelf(dirty);
1533
Mathias Agopianb9494d52012-04-18 02:28:45 -07001534 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001535 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001536 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001537 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001538 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001539 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001540
Mathias Agopian766dc492012-10-30 18:08:06 -07001541 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1542 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001543 // scissor on the main display. It should never be needed
1544 // anyways (though in theory it could since the API allows it).
1545 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001546 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001547 if (scissor != bounds) {
1548 // scissor doesn't match the screen's dimensions, so we
1549 // need to clear everything outside of it and enable
1550 // the GL scissor so we don't draw anything where we shouldn't
1551 const GLint height = hw->getHeight();
1552 glScissor(scissor.left, height - scissor.bottom,
1553 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001554 // enable scissor for this frame
1555 glEnable(GL_SCISSOR_TEST);
1556 }
1557 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001558 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001559
Mathias Agopian85d751c2012-08-29 16:59:24 -07001560 /*
1561 * and then, render the layers targeted at the framebuffer
1562 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001563
Mathias Agopian13127d82013-03-05 17:47:11 -08001564 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 const size_t count = layers.size();
1566 const Transform& tr = hw->getTransform();
1567 if (cur != end) {
1568 // we're using h/w composer
1569 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001570 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001571 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001572 if (!clip.isEmpty()) {
1573 switch (cur->getCompositionType()) {
1574 case HWC_OVERLAY: {
1575 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1576 && i
1577 && layer->isOpaque()
1578 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001579 // never clear the very first layer since we're
1580 // guaranteed the FB is already cleared
1581 layer->clearWithOpenGL(hw, clip);
1582 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001583 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001584 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001585 case HWC_FRAMEBUFFER: {
1586 layer->draw(hw, clip);
1587 break;
1588 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001589 case HWC_FRAMEBUFFER_TARGET: {
1590 // this should not happen as the iterator shouldn't
1591 // let us get there.
1592 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1593 break;
1594 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001595 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001596 }
1597 layer->setAcquireFence(hw, *cur);
1598 }
1599 } else {
1600 // we're not using h/w composer
1601 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001602 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001603 const Region clip(dirty.intersect(
1604 tr.transform(layer->visibleRegion)));
1605 if (!clip.isEmpty()) {
1606 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001607 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001608 }
1609 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001610
1611 // disable scissor at the end of the frame
1612 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613}
1614
Mathias Agopian55801e42012-08-27 18:54:24 -07001615void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1616 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001618 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001619 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001620 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001621 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001622
Mathias Agopian55801e42012-08-27 18:54:24 -07001623 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001624 Region::const_iterator it = region.begin();
1625 Region::const_iterator const end = region.end();
1626 while (it != end) {
1627 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001628 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001629 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1630 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1631 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1632 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001633 };
1634 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001635 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1636 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637}
1638
Mathias Agopianac9fa422013-02-11 16:40:36 -08001639void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1640 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001641 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001642 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001643{
Mathias Agopian96f08192010-06-02 23:28:45 -07001644 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001645 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001646
Mathias Agopian96f08192010-06-02 23:28:45 -07001647 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001648 Mutex::Autolock _l(mStateLock);
1649 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001650 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001651}
1652
Mathias Agopian13127d82013-03-05 17:47:11 -08001653status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654{
1655 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001656 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001658 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001659 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001660 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 return NO_ERROR;
1662 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001663 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664}
1665
Mathias Agopiandea20b12011-05-03 17:04:02 -07001666uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1667{
1668 return android_atomic_release_load(&mTransactionFlags);
1669}
1670
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1672{
1673 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1674}
1675
Mathias Agopianbb641242010-05-18 17:06:55 -07001676uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677{
1678 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1679 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001680 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 }
1682 return old;
1683}
1684
Mathias Agopian8b33f032012-07-24 20:43:54 -07001685void SurfaceFlinger::setTransactionState(
1686 const Vector<ComposerState>& state,
1687 const Vector<DisplayState>& displays,
1688 uint32_t flags)
1689{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001690 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001691 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001692 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001693
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001694 if (flags & eAnimation) {
1695 // For window updates that are part of an animation we must wait for
1696 // previous animation "frames" to be handled.
1697 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001698 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001699 if (CC_UNLIKELY(err != NO_ERROR)) {
1700 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001701 // caller after a few seconds.
1702 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1703 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001704 mAnimTransactionPending = false;
1705 break;
1706 }
1707 }
1708 }
1709
Mathias Agopiane57f2922012-08-09 16:29:12 -07001710 size_t count = displays.size();
1711 for (size_t i=0 ; i<count ; i++) {
1712 const DisplayState& s(displays[i]);
1713 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001714 }
1715
Mathias Agopiane57f2922012-08-09 16:29:12 -07001716 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001717 for (size_t i=0 ; i<count ; i++) {
1718 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001719 // Here we need to check that the interface we're given is indeed
1720 // one of our own. A malicious client could give us a NULL
1721 // IInterface, or one of its own or even one of our own but a
1722 // different type. All these situations would cause us to crash.
1723 //
1724 // NOTE: it would be better to use RTTI as we could directly check
1725 // that we have a Client*. however, RTTI is disabled in Android.
1726 if (s.client != NULL) {
1727 sp<IBinder> binder = s.client->asBinder();
1728 if (binder != NULL) {
1729 String16 desc(binder->getInterfaceDescriptor());
1730 if (desc == ISurfaceComposerClient::descriptor) {
1731 sp<Client> client( static_cast<Client *>(s.client.get()) );
1732 transactionFlags |= setClientStateLocked(client, s.state);
1733 }
1734 }
1735 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001736 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001737
Mathias Agopian386aa982011-11-07 21:58:03 -08001738 if (transactionFlags) {
1739 // this triggers the transaction
1740 setTransactionFlags(transactionFlags);
1741
1742 // if this is a synchronous transaction, wait for it to take effect
1743 // before returning.
1744 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001745 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001746 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001747 if (flags & eAnimation) {
1748 mAnimTransactionPending = true;
1749 }
1750 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001751 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1752 if (CC_UNLIKELY(err != NO_ERROR)) {
1753 // just in case something goes wrong in SF, return to the
1754 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001755 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1756 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001757 break;
1758 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001759 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 }
1761}
1762
Mathias Agopiane57f2922012-08-09 16:29:12 -07001763uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1764{
Jesse Hall9a143922012-10-04 16:29:19 -07001765 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1766 if (dpyIdx < 0)
1767 return 0;
1768
Mathias Agopiane57f2922012-08-09 16:29:12 -07001769 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001770 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001771 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001772 const uint32_t what = s.what;
1773 if (what & DisplayState::eSurfaceChanged) {
1774 if (disp.surface->asBinder() != s.surface->asBinder()) {
1775 disp.surface = s.surface;
1776 flags |= eDisplayTransactionNeeded;
1777 }
1778 }
1779 if (what & DisplayState::eLayerStackChanged) {
1780 if (disp.layerStack != s.layerStack) {
1781 disp.layerStack = s.layerStack;
1782 flags |= eDisplayTransactionNeeded;
1783 }
1784 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001785 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001786 if (disp.orientation != s.orientation) {
1787 disp.orientation = s.orientation;
1788 flags |= eDisplayTransactionNeeded;
1789 }
1790 if (disp.frame != s.frame) {
1791 disp.frame = s.frame;
1792 flags |= eDisplayTransactionNeeded;
1793 }
1794 if (disp.viewport != s.viewport) {
1795 disp.viewport = s.viewport;
1796 flags |= eDisplayTransactionNeeded;
1797 }
1798 }
1799 }
1800 return flags;
1801}
1802
1803uint32_t SurfaceFlinger::setClientStateLocked(
1804 const sp<Client>& client,
1805 const layer_state_t& s)
1806{
1807 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001808 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001809 if (layer != 0) {
1810 const uint32_t what = s.what;
1811 if (what & layer_state_t::ePositionChanged) {
1812 if (layer->setPosition(s.x, s.y))
1813 flags |= eTraversalNeeded;
1814 }
1815 if (what & layer_state_t::eLayerChanged) {
1816 // NOTE: index needs to be calculated before we update the state
1817 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1818 if (layer->setLayer(s.z)) {
1819 mCurrentState.layersSortedByZ.removeAt(idx);
1820 mCurrentState.layersSortedByZ.add(layer);
1821 // we need traversal (state changed)
1822 // AND transaction (list changed)
1823 flags |= eTransactionNeeded|eTraversalNeeded;
1824 }
1825 }
1826 if (what & layer_state_t::eSizeChanged) {
1827 if (layer->setSize(s.w, s.h)) {
1828 flags |= eTraversalNeeded;
1829 }
1830 }
1831 if (what & layer_state_t::eAlphaChanged) {
1832 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1833 flags |= eTraversalNeeded;
1834 }
1835 if (what & layer_state_t::eMatrixChanged) {
1836 if (layer->setMatrix(s.matrix))
1837 flags |= eTraversalNeeded;
1838 }
1839 if (what & layer_state_t::eTransparentRegionChanged) {
1840 if (layer->setTransparentRegionHint(s.transparentRegion))
1841 flags |= eTraversalNeeded;
1842 }
1843 if (what & layer_state_t::eVisibilityChanged) {
1844 if (layer->setFlags(s.flags, s.mask))
1845 flags |= eTraversalNeeded;
1846 }
1847 if (what & layer_state_t::eCropChanged) {
1848 if (layer->setCrop(s.crop))
1849 flags |= eTraversalNeeded;
1850 }
1851 if (what & layer_state_t::eLayerStackChanged) {
1852 // NOTE: index needs to be calculated before we update the state
1853 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1854 if (layer->setLayerStack(s.layerStack)) {
1855 mCurrentState.layersSortedByZ.removeAt(idx);
1856 mCurrentState.layersSortedByZ.add(layer);
1857 // we need traversal (state changed)
1858 // AND transaction (list changed)
1859 flags |= eTransactionNeeded|eTraversalNeeded;
1860 }
1861 }
1862 }
1863 return flags;
1864}
1865
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001866status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001867 const String8& name,
1868 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001869 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1870 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001873 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001874 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001875 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001876 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001877 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001878
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001879 status_t result = NO_ERROR;
1880
1881 sp<Layer> layer;
1882
Mathias Agopian3165cc22012-08-08 19:42:09 -07001883 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1884 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001885 result = createNormalLayer(client,
1886 name, w, h, flags, format,
1887 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001889 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001890 result = createDimLayer(client,
1891 name, w, h, flags,
1892 handle, gbp, &layer);
1893 break;
1894 default:
1895 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896 break;
1897 }
1898
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001899 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001900 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001901 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001903 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904}
1905
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001906status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1907 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1908 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909{
1910 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001911 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912 case PIXEL_FORMAT_TRANSPARENT:
1913 case PIXEL_FORMAT_TRANSLUCENT:
1914 format = PIXEL_FORMAT_RGBA_8888;
1915 break;
1916 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001917#ifdef NO_RGBX_8888
1918 format = PIXEL_FORMAT_RGB_565;
1919#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001920 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001921#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 break;
1923 }
1924
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001925#ifdef NO_RGBX_8888
1926 if (format == PIXEL_FORMAT_RGBX_8888)
1927 format = PIXEL_FORMAT_RGBA_8888;
1928#endif
1929
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001930 *outLayer = new Layer(this, client, name, w, h, flags);
1931 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1932 if (err == NO_ERROR) {
1933 *handle = (*outLayer)->getHandle();
1934 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001936
1937 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1938 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939}
1940
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1942 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1943 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001945 *outLayer = new LayerDim(this, client, name, w, h, flags);
1946 *handle = (*outLayer)->getHandle();
1947 *gbp = (*outLayer)->getBufferQueue();
1948 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001949}
1950
Mathias Agopianac9fa422013-02-11 16:40:36 -08001951status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952{
Mathias Agopian67106042013-03-14 19:18:13 -07001953 // called by the window manager when it wants to remove a Layer
1954 status_t err = NO_ERROR;
1955 sp<Layer> l(client->getLayerUser(handle));
1956 if (l != NULL) {
1957 err = removeLayer(l);
1958 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1959 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001960 }
1961 return err;
1962}
1963
Mathias Agopian13127d82013-03-05 17:47:11 -08001964status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001965{
Mathias Agopian67106042013-03-14 19:18:13 -07001966 // called by ~LayerCleaner() when all references to the IBinder (handle)
1967 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001968 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001969 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001970 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001971 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001972 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001973 "error removing layer=%p (%s)", l.get(), strerror(-err));
1974 }
1975 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001976}
1977
Mathias Agopianb60314a2012-04-10 22:09:54 -07001978// ---------------------------------------------------------------------------
1979
Andy McFadden13a082e2012-08-24 10:16:42 -07001980void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001981 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001982 Vector<ComposerState> state;
1983 Vector<DisplayState> displays;
1984 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001985 d.what = DisplayState::eDisplayProjectionChanged |
1986 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001987 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001988 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001989 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001990 d.frame.makeInvalid();
1991 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001992 displays.add(d);
1993 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001994 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001995
1996 const nsecs_t period =
1997 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1998 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001999}
2000
2001void SurfaceFlinger::initializeDisplays() {
2002 class MessageScreenInitialized : public MessageBase {
2003 SurfaceFlinger* flinger;
2004 public:
2005 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2006 virtual bool handler() {
2007 flinger->onInitializeDisplays();
2008 return true;
2009 }
2010 };
2011 sp<MessageBase> msg = new MessageScreenInitialized(this);
2012 postMessageAsync(msg); // we may be called from main thread, use async message
2013}
2014
2015
Mathias Agopiancde87a32012-09-13 14:09:01 -07002016void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002017 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2018 if (hw->isScreenAcquired()) {
2019 // this is expected, e.g. when power manager wakes up during boot
2020 ALOGD(" screen was previously acquired");
2021 return;
2022 }
2023
Mathias Agopian42977342012-08-05 00:40:46 -07002024 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002025 int32_t type = hw->getDisplayType();
2026 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2027 // built-in display, tell the HWC
2028 getHwComposer().acquire(type);
2029
2030 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2031 // FIXME: eventthread only knows about the main display right now
2032 mEventThread->onScreenAcquired();
2033 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002034 }
Mathias Agopian20128302012-08-13 18:32:13 -07002035 mVisibleRegionsDirty = true;
2036 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002037}
2038
Mathias Agopiancde87a32012-09-13 14:09:01 -07002039void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002040 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2041 if (!hw->isScreenAcquired()) {
2042 ALOGD(" screen was previously released");
2043 return;
2044 }
2045
2046 hw->releaseScreen();
2047 int32_t type = hw->getDisplayType();
2048 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2049 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002050 // FIXME: eventthread only knows about the main display right now
2051 mEventThread->onScreenReleased();
2052 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002053
2054 // built-in display, tell the HWC
2055 getHwComposer().release(type);
2056 }
2057 mVisibleRegionsDirty = true;
2058 // from this point on, SF will stop drawing on this display
2059}
2060
2061void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2062 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002063 SurfaceFlinger& mFlinger;
2064 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002065 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002066 MessageScreenAcquired(SurfaceFlinger& flinger,
2067 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002068 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002069 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2070 if (hw == NULL) {
2071 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2072 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2073 ALOGW("Attempt to unblank virtual display");
2074 } else {
2075 mFlinger.onScreenAcquired(hw);
2076 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002077 return true;
2078 }
2079 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002080 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2081 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082}
2083
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002084void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002085 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002086 SurfaceFlinger& mFlinger;
2087 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002088 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002089 MessageScreenReleased(SurfaceFlinger& flinger,
2090 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002091 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002092 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2093 if (hw == NULL) {
2094 ALOGE("Attempt to blank null display %p", mDisplay.get());
2095 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2096 ALOGW("Attempt to blank virtual display");
2097 } else {
2098 mFlinger.onScreenReleased(hw);
2099 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002100 return true;
2101 }
2102 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002103 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2104 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002105}
2106
2107// ---------------------------------------------------------------------------
2108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002109status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2110{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002112
Mathias Agopianbd115332013-04-18 16:41:04 -07002113 IPCThreadState* ipc = IPCThreadState::self();
2114 const int pid = ipc->getCallingPid();
2115 const int uid = ipc->getCallingUid();
2116 if ((uid != AID_SHELL) &&
2117 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002118 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002119 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002121 // Try to get the main lock, but don't insist if we can't
2122 // (this would indicate SF is stuck, but we want to be able to
2123 // print something in dumpsys).
2124 int retry = 3;
2125 while (mStateLock.tryLock()<0 && --retry>=0) {
2126 usleep(1000000);
2127 }
2128 const bool locked(retry >= 0);
2129 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002130 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002131 "SurfaceFlinger appears to be unresponsive, "
2132 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002133 }
2134
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002135 bool dumpAll = true;
2136 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002137 size_t numArgs = args.size();
2138 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002139 if ((index < numArgs) &&
2140 (args[index] == String16("--list"))) {
2141 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002142 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002143 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002144 }
2145
2146 if ((index < numArgs) &&
2147 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002148 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002149 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002150 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002151 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002152
2153 if ((index < numArgs) &&
2154 (args[index] == String16("--latency-clear"))) {
2155 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002156 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002157 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002158 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002159 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002160
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002161 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002162 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002163 }
2164
Mathias Agopian9795c422009-08-26 16:36:26 -07002165 if (locked) {
2166 mStateLock.unlock();
2167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002168 }
2169 write(fd, result.string(), result.size());
2170 return NO_ERROR;
2171}
2172
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002173void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002174 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002175{
2176 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2177 const size_t count = currentLayers.size();
2178 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002179 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002180 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002181 }
2182}
2183
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002184void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002185 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002186{
2187 String8 name;
2188 if (index < args.size()) {
2189 name = String8(args[index]);
2190 index++;
2191 }
2192
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002193 const nsecs_t period =
2194 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2195 result.appendFormat("%lld\n", period);
2196
2197 if (name.isEmpty()) {
2198 mAnimFrameTracker.dump(result);
2199 } else {
2200 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2201 const size_t count = currentLayers.size();
2202 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002203 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002204 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002205 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002206 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002207 }
2208 }
2209}
2210
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002212 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002213{
2214 String8 name;
2215 if (index < args.size()) {
2216 name = String8(args[index]);
2217 index++;
2218 }
2219
2220 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2221 const size_t count = currentLayers.size();
2222 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002223 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002224 if (name.isEmpty() || (name == layer->getName())) {
2225 layer->clearStats();
2226 }
2227 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002228
2229 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002230}
2231
Jamie Gennis6547ff42013-07-16 20:12:42 -07002232// This should only be called from the main thread. Otherwise it would need
2233// the lock and should use mCurrentState rather than mDrawingState.
2234void SurfaceFlinger::logFrameStats() {
2235 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2236 const size_t count = drawingLayers.size();
2237 for (size_t i=0 ; i<count ; i++) {
2238 const sp<Layer>& layer(drawingLayers[i]);
2239 layer->logFrameStats();
2240 }
2241
2242 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2243}
2244
Andy McFadden4803b742012-09-24 19:07:20 -07002245/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2246{
2247 static const char* config =
2248 " [sf"
2249#ifdef NO_RGBX_8888
2250 " NO_RGBX_8888"
2251#endif
2252#ifdef HAS_CONTEXT_PRIORITY
2253 " HAS_CONTEXT_PRIORITY"
2254#endif
2255#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2256 " NEVER_DEFAULT_TO_ASYNC_MODE"
2257#endif
2258#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2259 " TARGET_DISABLE_TRIPLE_BUFFERING"
2260#endif
2261 "]";
2262 result.append(config);
2263}
2264
Mathias Agopian74d211a2013-04-22 16:55:35 +02002265void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2266 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002267{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002268 bool colorize = false;
2269 if (index < args.size()
2270 && (args[index] == String16("--color"))) {
2271 colorize = true;
2272 index++;
2273 }
2274
2275 Colorizer colorizer(colorize);
2276
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002277 // figure out if we're stuck somewhere
2278 const nsecs_t now = systemTime();
2279 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2280 const nsecs_t inTransaction(mDebugInTransaction);
2281 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2282 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2283
2284 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002285 * Dump library configuration.
2286 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002287
2288 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002289 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002290 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002291 appendSfConfigString(result);
2292 appendUiConfigString(result);
2293 appendGuiConfigString(result);
2294 result.append("\n");
2295
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002297 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002298 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002299 result.append(SyncFeatures::getInstance().toString());
2300 result.append("\n");
2301
Andy McFadden4803b742012-09-24 19:07:20 -07002302 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002303 * Dump the visible layer list
2304 */
2305 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2306 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002307 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002308 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002309 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002310 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002311 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002312 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002313 }
2314
2315 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002316 * Dump Display state
2317 */
2318
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002319 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002320 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002321 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002322 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2323 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002324 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002325 }
2326
2327 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328 * Dump SurfaceFlinger global state
2329 */
2330
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002331 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002332 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002333 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002334
Mathias Agopian888c8222012-08-04 21:10:38 -07002335 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002336 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002337
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002338 colorizer.bold(result);
2339 result.appendFormat("EGL implementation : %s\n",
2340 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2341 colorizer.reset(result);
2342 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002343 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002344
Mathias Agopian875d8e12013-06-07 15:35:48 -07002345 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346
Mathias Agopian42977342012-08-05 00:40:46 -07002347 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002348 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002349 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002350 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002351 " last eglSwapBuffers() time: %f us\n"
2352 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002353 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354 " refresh-rate : %f fps\n"
2355 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002356 " y-dpi : %f\n"
2357 " EGL_NATIVE_VISUAL_ID : %d\n"
2358 " gpu_to_cpu_unsupported : %d\n"
2359 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360 mLastSwapBufferTime/1000.0,
2361 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002362 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002363 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2364 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002365 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2366 mEGLNativeVisualId,
2367 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002368
Mathias Agopian74d211a2013-04-22 16:55:35 +02002369 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002370 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002371
Mathias Agopian74d211a2013-04-22 16:55:35 +02002372 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002373 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374
2375 /*
2376 * VSYNC state
2377 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002378 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379
2380 /*
2381 * Dump HWComposer state
2382 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002383 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002384 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002385 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002386 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002387 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2388 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002389 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002390
2391 /*
2392 * Dump gralloc state
2393 */
2394 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2395 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002396}
2397
Mathias Agopian13127d82013-03-05 17:47:11 -08002398const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002399SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002400 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002401 wp<IBinder> dpy;
2402 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2403 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2404 dpy = mDisplays.keyAt(i);
2405 break;
2406 }
2407 }
2408 if (dpy == NULL) {
2409 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2410 // Just use the primary display so we have something to return
2411 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2412 }
2413 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002414}
2415
Keun young Park63f165f2012-08-31 10:53:36 -07002416bool SurfaceFlinger::startDdmConnection()
2417{
2418 void* libddmconnection_dso =
2419 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2420 if (!libddmconnection_dso) {
2421 return false;
2422 }
2423 void (*DdmConnection_start)(const char* name);
2424 DdmConnection_start =
2425 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2426 if (!DdmConnection_start) {
2427 dlclose(libddmconnection_dso);
2428 return false;
2429 }
2430 (*DdmConnection_start)(getServiceName());
2431 return true;
2432}
2433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002434status_t SurfaceFlinger::onTransact(
2435 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2436{
2437 switch (code) {
2438 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002439 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002440 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002441 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002442 case BLANK:
2443 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444 {
2445 // codes that require permission check
2446 IPCThreadState* ipc = IPCThreadState::self();
2447 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002448 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002449 if ((uid != AID_GRAPHICS) &&
2450 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002451 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002452 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2453 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002455 break;
2456 }
2457 case CAPTURE_SCREEN:
2458 {
2459 // codes that require permission check
2460 IPCThreadState* ipc = IPCThreadState::self();
2461 const int pid = ipc->getCallingPid();
2462 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002463 if ((uid != AID_GRAPHICS) &&
2464 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002465 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002466 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2467 return PERMISSION_DENIED;
2468 }
2469 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470 }
2471 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2474 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002475 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002476 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002477 IPCThreadState* ipc = IPCThreadState::self();
2478 const int pid = ipc->getCallingPid();
2479 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002480 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002481 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 return PERMISSION_DENIED;
2483 }
2484 int n;
2485 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002486 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002487 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488 return NO_ERROR;
2489 case 1002: // SHOW_UPDATES
2490 n = data.readInt32();
2491 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002492 invalidateHwcGeometry();
2493 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002496 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002497 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002499 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002500 setTransactionFlags(
2501 eTransactionNeeded|
2502 eDisplayTransactionNeeded|
2503 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002504 return NO_ERROR;
2505 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002506 case 1006:{ // send empty update
2507 signalRefresh();
2508 return NO_ERROR;
2509 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002510 case 1008: // toggle use of hw composer
2511 n = data.readInt32();
2512 mDebugDisableHWC = n ? 1 : 0;
2513 invalidateHwcGeometry();
2514 repaintEverything();
2515 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002516 case 1009: // toggle use of transform hint
2517 n = data.readInt32();
2518 mDebugDisableTransformHint = n ? 1 : 0;
2519 invalidateHwcGeometry();
2520 repaintEverything();
2521 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002523 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524 reply->writeInt32(0);
2525 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002526 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002527 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002528 return NO_ERROR;
2529 case 1013: {
2530 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002531 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2532 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 }
2534 return NO_ERROR;
2535 }
2536 }
2537 return err;
2538}
2539
Mathias Agopian53331da2011-08-22 21:44:41 -07002540void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002541 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002542 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002543}
2544
Mathias Agopian59119e62010-10-11 12:37:43 -07002545// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002546// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002547// ---------------------------------------------------------------------------
2548
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002549/* The code below is here to handle b/8734824
2550 *
2551 * We create a IGraphicBufferProducer wrapper that forwards all calls
2552 * to the calling binder thread, where they are executed. This allows
2553 * the calling thread to be reused (on the other side) and not
2554 * depend on having "enough" binder threads to handle the requests.
2555 *
2556 */
2557
2558class GraphicProducerWrapper : public BBinder, public MessageHandler {
2559 sp<IGraphicBufferProducer> impl;
2560 sp<Looper> looper;
2561 status_t result;
2562 bool exitPending;
2563 bool exitRequested;
2564 mutable Barrier barrier;
2565 volatile int32_t memoryBarrier;
2566 uint32_t code;
2567 Parcel const* data;
2568 Parcel* reply;
2569
2570 enum {
2571 MSG_API_CALL,
2572 MSG_EXIT
2573 };
2574
2575 /*
2576 * this is called by our "fake" BpGraphicBufferProducer. We package the
2577 * data and reply Parcel and forward them to the calling thread.
2578 */
2579 virtual status_t transact(uint32_t code,
2580 const Parcel& data, Parcel* reply, uint32_t flags) {
2581 this->code = code;
2582 this->data = &data;
2583 this->reply = reply;
2584 android_atomic_acquire_store(0, &memoryBarrier);
2585 if (exitPending) {
2586 // if we've exited, we run the message synchronously right here
2587 handleMessage(Message(MSG_API_CALL));
2588 } else {
2589 barrier.close();
2590 looper->sendMessage(this, Message(MSG_API_CALL));
2591 barrier.wait();
2592 }
2593 return NO_ERROR;
2594 }
2595
2596 /*
2597 * here we run on the binder calling thread. All we've got to do is
2598 * call the real BpGraphicBufferProducer.
2599 */
2600 virtual void handleMessage(const Message& message) {
2601 android_atomic_release_load(&memoryBarrier);
2602 if (message.what == MSG_API_CALL) {
2603 impl->asBinder()->transact(code, data[0], reply);
2604 barrier.open();
2605 } else if (message.what == MSG_EXIT) {
2606 exitRequested = true;
2607 }
2608 }
2609
2610public:
2611 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2612 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2613 exitPending(false), exitRequested(false) {
2614 }
2615
2616 status_t waitForResponse() {
2617 do {
2618 looper->pollOnce(-1);
2619 } while (!exitRequested);
2620 return result;
2621 }
2622
2623 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002624 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002625 exitPending = true;
2626 looper->sendMessage(this, Message(MSG_EXIT));
2627 }
2628};
2629
2630
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002631status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2632 const sp<IGraphicBufferProducer>& producer,
2633 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002634 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002635
2636 if (CC_UNLIKELY(display == 0))
2637 return BAD_VALUE;
2638
2639 if (CC_UNLIKELY(producer == 0))
2640 return BAD_VALUE;
2641
2642 class MessageCaptureScreen : public MessageBase {
2643 SurfaceFlinger* flinger;
2644 sp<IBinder> display;
2645 sp<IGraphicBufferProducer> producer;
2646 uint32_t reqWidth, reqHeight;
2647 uint32_t minLayerZ,maxLayerZ;
2648 status_t result;
2649 public:
2650 MessageCaptureScreen(SurfaceFlinger* flinger,
2651 const sp<IBinder>& display,
2652 const sp<IGraphicBufferProducer>& producer,
2653 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002654 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002655 : flinger(flinger), display(display), producer(producer),
2656 reqWidth(reqWidth), reqHeight(reqHeight),
2657 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2658 result(PERMISSION_DENIED)
2659 {
2660 }
2661 status_t getResult() const {
2662 return result;
2663 }
2664 virtual bool handler() {
2665 Mutex::Autolock _l(flinger->mStateLock);
2666 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002667 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002668 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002669 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002670 return true;
2671 }
2672 };
2673
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002674 // make sure to process transactions before screenshots -- a transaction
2675 // might already be pending but scheduled for VSYNC; this guarantees we
2676 // will handle it before the screenshot. When VSYNC finally arrives
2677 // the scheduled transaction will be a no-op. If no transactions are
2678 // scheduled at this time, this will end-up being a no-op as well.
2679 mEventQueue.invalidateTransactionNow();
2680
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002681 // this creates a "fake" BBinder which will serve as a "fake" remote
2682 // binder to receive the marshaled calls and forward them to the
2683 // real remote (a BpGraphicBufferProducer)
2684 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2685
2686 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2687 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002688 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002689 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002690 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002691
2692 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002693 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002694 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002695 }
2696 return res;
2697}
2698
Mathias Agopian180f10d2013-04-10 22:55:41 -07002699
2700void SurfaceFlinger::renderScreenImplLocked(
2701 const sp<const DisplayDevice>& hw,
2702 uint32_t reqWidth, uint32_t reqHeight,
2703 uint32_t minLayerZ, uint32_t maxLayerZ,
2704 bool yswap)
2705{
2706 ATRACE_CALL();
2707
2708 // get screen geometry
2709 const uint32_t hw_w = hw->getWidth();
2710 const uint32_t hw_h = hw->getHeight();
2711
2712 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2713
2714 // make sure to clear all GL error flags
2715 while ( glGetError() != GL_NO_ERROR ) ;
2716
2717 // set-up our viewport
2718 glViewport(0, 0, reqWidth, reqHeight);
2719 glMatrixMode(GL_PROJECTION);
2720 glLoadIdentity();
2721 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2722 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2723 glMatrixMode(GL_MODELVIEW);
2724 glLoadIdentity();
2725
2726 // redraw the screen entirely...
2727 glDisable(GL_SCISSOR_TEST);
2728 glClearColor(0,0,0,1);
2729 glClear(GL_COLOR_BUFFER_BIT);
2730 glDisable(GL_TEXTURE_EXTERNAL_OES);
2731 glDisable(GL_TEXTURE_2D);
2732
2733 const LayerVector& layers( mDrawingState.layersSortedByZ );
2734 const size_t count = layers.size();
2735 for (size_t i=0 ; i<count ; ++i) {
2736 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002737 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002738 if (state.layerStack == hw->getLayerStack()) {
2739 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2740 if (layer->isVisible()) {
2741 if (filtering) layer->setFiltering(true);
2742 layer->draw(hw);
2743 if (filtering) layer->setFiltering(false);
2744 }
2745 }
2746 }
2747 }
2748
2749 // compositionComplete is needed for older driver
2750 hw->compositionComplete();
2751}
2752
2753
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002754status_t SurfaceFlinger::captureScreenImplLocked(
2755 const sp<const DisplayDevice>& hw,
2756 const sp<IGraphicBufferProducer>& producer,
2757 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002758 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002759{
2760 ATRACE_CALL();
2761
Mathias Agopian180f10d2013-04-10 22:55:41 -07002762 // get screen geometry
2763 const uint32_t hw_w = hw->getWidth();
2764 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002765
Mathias Agopian180f10d2013-04-10 22:55:41 -07002766 // if we have secure windows on this display, never allow the screen capture
2767 if (hw->getSecureLayerVisible()) {
2768 ALOGW("FB is protected: PERMISSION_DENIED");
2769 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002770 }
2771
Mathias Agopian180f10d2013-04-10 22:55:41 -07002772 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2773 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2774 reqWidth, reqHeight, hw_w, hw_h);
2775 return BAD_VALUE;
2776 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002777
Mathias Agopian180f10d2013-04-10 22:55:41 -07002778 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2779 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2780
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002781 // create a surface (because we're a producer, and we need to
2782 // dequeue/queue a buffer)
2783 sp<Surface> sur = new Surface(producer);
2784 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002785
2786 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002787 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002788 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2789 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002790
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002791 int err = 0;
2792 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2793 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2794 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002795
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002796 if (err == NO_ERROR) {
2797 ANativeWindowBuffer* buffer;
2798 /* TODO: Once we have the sync framework everywhere this can use
2799 * server-side waits on the fence that dequeueBuffer returns.
2800 */
2801 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2802 if (result == NO_ERROR) {
2803 // create an EGLImage from the buffer so we can later
2804 // turn it into a texture
2805 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2806 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2807 if (image != EGL_NO_IMAGE_KHR) {
2808 GLuint tname, name;
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002809
2810 // turn our EGLImage into a texture
2811 glGenTextures(1, &tname);
2812 glBindTexture(GL_TEXTURE_2D, tname);
2813 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2814 // create a Framebuffer Object to render into
2815 glGenFramebuffersOES(1, &name);
2816 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2817 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2818 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002819
2820 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2821 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2822 // this will in fact render into our dequeued buffer
2823 // via an FBO, which means we didn't have to create
2824 // an EGLSurface and therefore we're not
2825 // dependent on the context's EGLConfig.
2826 renderScreenImplLocked(hw, reqWidth, reqHeight,
2827 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002828 } else {
2829 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2830 result = INVALID_OPERATION;
2831 }
2832
2833 // back to main framebuffer
2834 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2835 glDeleteFramebuffersOES(1, &name);
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002836 glDeleteTextures(1, &tname);
2837
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002838 // destroy our image
2839 eglDestroyImageKHR(mEGLDisplay, image);
2840 } else {
2841 result = BAD_VALUE;
2842 }
2843 window->queueBuffer(window, buffer, -1);
2844 }
2845 } else {
2846 result = BAD_VALUE;
2847 }
2848 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2849 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002850
Mathias Agopian875d8e12013-06-07 15:35:48 -07002851 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002852
Mathias Agopian74c40c02010-09-29 13:02:36 -07002853 return result;
2854}
2855
Mathias Agopianfee2b462013-07-03 12:34:01 -07002856void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2857 const sp<const DisplayDevice>& hw,
2858 uint32_t minLayerZ, uint32_t maxLayerZ) {
2859 if (DEBUG_SCREENSHOTS) {
2860 for (ssize_t y=0 ; y<buf->height ; y++) {
2861 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2862 for (ssize_t x=0 ; x<buf->width ; x++) {
2863 if (p[x] != 0xFF000000) return;
2864 }
2865 }
2866 ALOGE("*** we just took a black screenshot ***\n"
2867 "requested minz=%d, maxz=%d, layerStack=%d",
2868 minLayerZ, maxLayerZ, hw->getLayerStack());
2869 const LayerVector& layers( mDrawingState.layersSortedByZ );
2870 const size_t count = layers.size();
2871 for (size_t i=0 ; i<count ; ++i) {
2872 const sp<Layer>& layer(layers[i]);
2873 const Layer::State& state(layer->getDrawingState());
2874 const bool visible = (state.layerStack == hw->getLayerStack())
2875 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2876 && (layer->isVisible());
2877 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2878 visible ? '+' : '-',
2879 i, layer->getName().string(), state.layerStack, state.z,
2880 layer->isVisible(), state.flags, state.alpha);
2881 }
2882 }
2883}
2884
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002885// ---------------------------------------------------------------------------
2886
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002887SurfaceFlinger::LayerVector::LayerVector() {
2888}
2889
2890SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002891 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002892}
2893
2894int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2895 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002896{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002897 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002898 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2899 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002900
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002901 uint32_t ls = l->getCurrentState().layerStack;
2902 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002903 if (ls != rs)
2904 return ls - rs;
2905
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002906 uint32_t lz = l->getCurrentState().z;
2907 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002908 if (lz != rz)
2909 return lz - rz;
2910
2911 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002912}
2913
2914// ---------------------------------------------------------------------------
2915
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002916SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2917 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002918}
2919
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002920SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002921 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002922 viewport.makeInvalid();
2923 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002924}
2925
2926// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002928}; // namespace android