blob: c6feee16f9acb2699105748f1fbc9acf19fc9f87 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070027#include <GLES/glext.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
74
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#define DISPLAY_COUNT 1
76
Mathias Agopianfee2b462013-07-03 12:34:01 -070077/*
78 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
79 * black pixels.
80 */
81#define DEBUG_SCREENSHOTS false
82
Mathias Agopianca088332013-03-28 17:44:13 -070083EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086// ---------------------------------------------------------------------------
87
Mathias Agopian99b49842011-06-27 16:05:52 -070088const String16 sHardwareTest("android.permission.HARDWARE_TEST");
89const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
90const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
91const String16 sDump("android.permission.DUMP");
92
93// ---------------------------------------------------------------------------
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095SurfaceFlinger::SurfaceFlinger()
96 : BnSurfaceComposer(), Thread(false),
97 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070098 mTransactionPending(false),
99 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700101 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700102 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800106 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700109 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700110 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700111 mDebugInSwapBuffers(0),
112 mLastSwapBufferTime(0),
113 mDebugInTransaction(0),
114 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700115 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 // debugging stuff...
120 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Mathias Agopianb4b17302013-03-20 18:36:41 -0700122 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700123 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 property_get("debug.sf.showupdates", value, "0");
126 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700127
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 property_get("debug.sf.ddms", value, "0");
129 mDebugDDMS = atoi(value);
130 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700131 if (!startDdmConnection()) {
132 // start failed, and DDMS debugging not enabled
133 mDebugDDMS = 0;
134 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700135 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700136 ALOGI_IF(mDebugRegion, "showupdates enabled");
137 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::onFirstRef()
141{
142 mEventQueue.init(this);
143
144 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
145
146 // Wait for the main thread to be done with its initialization
147 mReadyToRunBarrier.wait();
148}
149
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151SurfaceFlinger::~SurfaceFlinger()
152{
Mathias Agopiana4912602012-07-12 14:25:33 -0700153 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
154 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
155 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800158void SurfaceFlinger::binderDied(const wp<IBinder>& who)
159{
160 // the window manager died on us. prepare its eulogy.
161
Andy McFadden13a082e2012-08-24 10:16:42 -0700162 // restore initial conditions (default device unblank, etc)
163 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800164
165 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700166 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800167}
168
Mathias Agopian7e27f052010-05-28 14:22:23 -0700169sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170{
Mathias Agopian96f08192010-06-02 23:28:45 -0700171 sp<ISurfaceComposerClient> bclient;
172 sp<Client> client(new Client(this));
173 status_t err = client->initCheck();
174 if (err == NO_ERROR) {
175 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 return bclient;
178}
179
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700180sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
181 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700182{
183 class DisplayToken : public BBinder {
184 sp<SurfaceFlinger> flinger;
185 virtual ~DisplayToken() {
186 // no more references, this display must be terminated
187 Mutex::Autolock _l(flinger->mStateLock);
188 flinger->mCurrentState.displays.removeItem(this);
189 flinger->setTransactionFlags(eDisplayTransactionNeeded);
190 }
191 public:
192 DisplayToken(const sp<SurfaceFlinger>& flinger)
193 : flinger(flinger) {
194 }
195 };
196
197 sp<BBinder> token = new DisplayToken(this);
198
199 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700200 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700201 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700202 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700203 mCurrentState.displays.add(token, info);
204
205 return token;
206}
207
Jesse Hall692c7232012-11-08 15:41:56 -0800208void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
209 ALOGW_IF(mBuiltinDisplays[type],
210 "Overwriting display token for display type %d", type);
211 mBuiltinDisplays[type] = new BBinder();
212 DisplayDeviceState info(type);
213 // All non-virtual displays are currently considered secure.
214 info.isSecure = true;
215 mCurrentState.displays.add(mBuiltinDisplays[type], info);
216}
217
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700219 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700220 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
221 return NULL;
222 }
Jesse Hall692c7232012-11-08 15:41:56 -0800223 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700224}
225
Jamie Gennis9a78c902011-01-12 18:30:40 -0800226sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
227{
228 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
229 return gba;
230}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232void SurfaceFlinger::bootFinished()
233{
234 const nsecs_t now = systemTime();
235 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000236 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700237 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700238
239 // wait patiently for the window manager death
240 const String16 name("window");
241 sp<IBinder> window(defaultServiceManager()->getService(name));
242 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700243 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700244 }
245
246 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700247 // formerly we would just kill the process, but we now ask it to exit so it
248 // can choose where to stop the animation.
249 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700252void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
253 class MessageDestroyGLTexture : public MessageBase {
254 GLuint texture;
255 public:
256 MessageDestroyGLTexture(GLuint texture)
257 : texture(texture) {
258 }
259 virtual bool handler() {
260 glDeleteTextures(1, &texture);
261 return true;
262 }
263 };
264 postMessageAsync(new MessageDestroyGLTexture(texture));
265}
266
Mathias Agopian722b98f2012-09-25 18:24:31 -0700267status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700268 EGLDisplay dpy,
269 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700270 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700271 EGLConfig* outConfig)
272{
273 EGLConfig config = NULL;
274 EGLint numConfigs = -1, n=0;
275 eglGetConfigs(dpy, NULL, 0, &numConfigs);
276 EGLConfig* const configs = new EGLConfig[numConfigs];
277 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700278
Mathias Agopian722b98f2012-09-25 18:24:31 -0700279 if (n) {
280 if (attribute != EGL_NONE) {
281 for (int i=0 ; i<n ; i++) {
282 EGLint value = 0;
283 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
284 if (wanted == value) {
285 *outConfig = configs[i];
286 delete [] configs;
287 return NO_ERROR;
288 }
289 }
290 } else {
291 // just pick the first one
292 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700293 delete [] configs;
294 return NO_ERROR;
295 }
296 }
297 delete [] configs;
298 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian722b98f2012-09-25 18:24:31 -0700301class EGLAttributeVector {
302 struct Attribute;
303 class Adder;
304 friend class Adder;
305 KeyedVector<Attribute, EGLint> mList;
306 struct Attribute {
307 Attribute() {};
308 Attribute(EGLint v) : v(v) { }
309 EGLint v;
310 bool operator < (const Attribute& other) const {
311 // this places EGL_NONE at the end
312 EGLint lhs(v);
313 EGLint rhs(other.v);
314 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
315 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
316 return lhs < rhs;
317 }
318 };
319 class Adder {
320 friend class EGLAttributeVector;
321 EGLAttributeVector& v;
322 EGLint attribute;
323 Adder(EGLAttributeVector& v, EGLint attribute)
324 : v(v), attribute(attribute) {
325 }
326 public:
327 void operator = (EGLint value) {
328 if (attribute != EGL_NONE) {
329 v.mList.add(attribute, value);
330 }
331 }
332 operator EGLint () const { return v.mList[attribute]; }
333 };
334public:
335 EGLAttributeVector() {
336 mList.add(EGL_NONE, EGL_NONE);
337 }
338 void remove(EGLint attribute) {
339 if (attribute != EGL_NONE) {
340 mList.removeItem(attribute);
341 }
342 }
343 Adder operator [] (EGLint attribute) {
344 return Adder(*this, attribute);
345 }
346 EGLint operator [] (EGLint attribute) const {
347 return mList[attribute];
348 }
349 // cast-operator to (EGLint const*)
350 operator EGLint const* () const { return &mList.keyAt(0).v; }
351};
352
Mathias Agopiana4912602012-07-12 14:25:33 -0700353EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
354 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
355 // it is to be used with WIFI displays
356 EGLConfig config;
357 EGLint dummy;
358 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700359
Mathias Agopian722b98f2012-09-25 18:24:31 -0700360 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700361 // TODO: enable ES2
362 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
363 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700364 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
365 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
366 attribs[EGL_RED_SIZE] = 8;
367 attribs[EGL_GREEN_SIZE] = 8;
368 attribs[EGL_BLUE_SIZE] = 8;
369
370 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
371 if (!err)
372 goto success;
373
Mathias Agopian875d8e12013-06-07 15:35:48 -0700374 // this didn't work, probably because we're on the emulator...
375 // try a simplified query
376 ALOGW("no suitable EGLConfig found, trying a simpler query");
377 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700378 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700380 attribs.remove(EGL_RED_SIZE);
381 attribs.remove(EGL_GREEN_SIZE);
382 attribs.remove(EGL_BLUE_SIZE);
383 err = selectConfigForAttribute(display, attribs,
384 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700385 if (!err)
386 goto success;
387
388 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700389 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700390 return 0;
391
392success:
393 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700394 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700395 return config;
396}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397
Mathias Agopiana4912602012-07-12 14:25:33 -0700398
Mathias Agopiana4912602012-07-12 14:25:33 -0700399status_t SurfaceFlinger::readyToRun()
400{
401 ALOGI( "SurfaceFlinger's main thread ready to run. "
402 "Initializing graphics H/W...");
403
Jesse Hall692c7232012-11-08 15:41:56 -0800404 Mutex::Autolock _l(mStateLock);
405
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700406 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700407 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
408 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700409
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700410 // Initialize the H/W composer object. There may or may not be an
411 // actual hardware composer underneath.
412 mHwc = new HWComposer(this,
413 *static_cast<HWComposer::EventHandler *>(this));
414
Mathias Agopian875d8e12013-06-07 15:35:48 -0700415 // initialize the config and context (can't fail)
416 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
417
418 // print some debugging info
419 EGLint r,g,b,a;
420 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
421 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
422 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
423 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
424 ALOGI("EGL informations:");
425 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
426 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
427 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
428 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
429 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
430
431 // get a RenderEngine for the given display / config (can't fail)
432 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
433
434 // retrieve the EGL context that was selected/created
435 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700436
Mathias Agopianed985572013-03-22 00:24:39 -0700437 // figure out which format we got
438 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
439 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
440
Mathias Agopianda27af92012-09-13 18:17:13 -0700441 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
442 "couldn't create EGLContext");
443
Mathias Agopiancde87a32012-09-13 14:09:01 -0700444 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700445 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700446 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700447 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700448 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700449 // All non-virtual displays are currently considered secure.
450 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800451 createBuiltinDisplayLocked(type);
452 wp<IBinder> token = mBuiltinDisplays[i];
453
Mathias Agopianf5a33922012-09-19 18:16:22 -0700454 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700455 type, allocateHwcDisplayId(type), isSecure, token,
456 new FramebufferSurface(*mHwc, i),
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700457 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700458 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700459 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700460 // for displays other than the main display, so we always
461 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700462 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700463 hw->acquireScreen();
464 }
465 mDisplays.add(token, hw);
466 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700467 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700468
Mathias Agopian028508c2012-07-25 21:12:12 -0700469 // start the EventThread
470 mEventThread = new EventThread(this);
471 mEventQueue.setEventThread(mEventThread);
472
Mathias Agopian92a979a2012-08-02 18:32:23 -0700473 // initialize our drawing state
474 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700475
Mathias Agopiana4912602012-07-12 14:25:33 -0700476 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800477 mReadyToRunBarrier.open();
478
Andy McFadden13a082e2012-08-24 10:16:42 -0700479 // set initial conditions (e.g. unblank default device)
480 initializeDisplays();
481
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700482 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700483 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 return NO_ERROR;
486}
487
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700488int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
489 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
490 type : mHwc->allocateDisplayId();
491}
492
Mathias Agopiana67e4182012-06-19 17:26:12 -0700493void SurfaceFlinger::startBootAnim() {
494 // start boot animation
495 property_set("service.bootanim.exit", "0");
496 property_set("ctl.start", "bootanim");
497}
498
Mathias Agopian875d8e12013-06-07 15:35:48 -0700499size_t SurfaceFlinger::getMaxTextureSize() const {
500 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700501}
502
Mathias Agopian875d8e12013-06-07 15:35:48 -0700503size_t SurfaceFlinger::getMaxViewportDims() const {
504 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700505}
506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800508
Jamie Gennis582270d2011-08-17 18:19:00 -0700509bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800510 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800511 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800512 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700513 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800514}
515
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700516status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800517 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700518 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800519 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700520 type = i;
521 break;
522 }
523 }
524
525 if (type < 0) {
526 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700527 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700528
529 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700530 float xdpi = hwc.getDpiX(type);
531 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700532
533 // TODO: Not sure if display density should handled by SF any longer
534 class Density {
535 static int getDensityFromProperty(char const* propName) {
536 char property[PROPERTY_VALUE_MAX];
537 int density = 0;
538 if (property_get(propName, property, NULL) > 0) {
539 density = atoi(property);
540 }
541 return density;
542 }
543 public:
544 static int getEmuDensity() {
545 return getDensityFromProperty("qemu.sf.lcd_density"); }
546 static int getBuildDensity() {
547 return getDensityFromProperty("ro.sf.lcd_density"); }
548 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700549
550 if (type == DisplayDevice::DISPLAY_PRIMARY) {
551 // The density of the device is provided by a build property
552 float density = Density::getBuildDensity() / 160.0f;
553 if (density == 0) {
554 // the build doesn't provide a density -- this is wrong!
555 // use xdpi instead
556 ALOGE("ro.sf.lcd_density must be defined as a build property");
557 density = xdpi / 160.0f;
558 }
559 if (Density::getEmuDensity()) {
560 // if "qemu.sf.lcd_density" is specified, it overrides everything
561 xdpi = ydpi = density = Density::getEmuDensity();
562 density /= 160.0f;
563 }
564 info->density = density;
565
566 // TODO: this needs to go away (currently needed only by webkit)
567 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
568 info->orientation = hw->getOrientation();
569 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
570 } else {
571 // TODO: where should this value come from?
572 static const int TV_DENSITY = 213;
573 info->density = TV_DENSITY / 160.0f;
574 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700575 }
576
Mathias Agopian1604f772012-09-18 21:54:42 -0700577 info->w = hwc.getWidth(type);
578 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700579 info->xdpi = xdpi;
580 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700581 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700582
583 // All non-virtual displays are currently considered secure.
584 info->secure = true;
585
Mathias Agopian888c8222012-08-04 21:10:38 -0700586 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700587}
588
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800589// ----------------------------------------------------------------------------
590
591sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800592 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800596
597void SurfaceFlinger::waitForEvent() {
598 mEventQueue.waitMessage();
599}
600
601void SurfaceFlinger::signalTransaction() {
602 mEventQueue.invalidate();
603}
604
605void SurfaceFlinger::signalLayerUpdate() {
606 mEventQueue.invalidate();
607}
608
609void SurfaceFlinger::signalRefresh() {
610 mEventQueue.refresh();
611}
612
613status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
614 nsecs_t reltime, uint32_t flags) {
615 return mEventQueue.postMessage(msg, reltime);
616}
617
618status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
619 nsecs_t reltime, uint32_t flags) {
620 status_t res = mEventQueue.postMessage(msg, reltime);
621 if (res == NO_ERROR) {
622 msg->wait();
623 }
624 return res;
625}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 return true;
630}
631
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700632void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700633 if (mEventThread == NULL) {
634 // This is a temporary workaround for b/7145521. A non-null pointer
635 // does not mean EventThread has finished initializing, so this
636 // is not a correct fix.
637 ALOGW("WARNING: EventThread not started, ignoring vsync");
638 return;
639 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700640 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
641 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700642 mEventThread->onVSyncReceived(type, timestamp);
643 }
644}
645
646void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
647 if (mEventThread == NULL) {
648 // This is a temporary workaround for b/7145521. A non-null pointer
649 // does not mean EventThread has finished initializing, so this
650 // is not a correct fix.
651 ALOGW("WARNING: EventThread not started, ignoring hotplug");
652 return;
653 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700654
Mathias Agopian148994e2012-09-19 17:31:36 -0700655 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700656 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800657 if (connected) {
658 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700659 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800660 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
661 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700662 }
663 setTransactionFlags(eDisplayTransactionNeeded);
664
Andy McFadden9e9689c2012-10-10 18:17:51 -0700665 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700666 }
Mathias Agopian86303202012-07-24 22:46:10 -0700667}
668
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700669void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
670 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700671}
672
Mathias Agopian4fec8732012-06-29 14:12:52 -0700673void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800674 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800675 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700676 case MessageQueue::TRANSACTION:
677 handleMessageTransaction();
678 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679 case MessageQueue::INVALIDATE:
680 handleMessageTransaction();
681 handleMessageInvalidate();
682 signalRefresh();
683 break;
684 case MessageQueue::REFRESH:
685 handleMessageRefresh();
686 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800687 }
688}
689
Mathias Agopian4fec8732012-06-29 14:12:52 -0700690void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700691 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700692 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700693 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700694 }
695}
696
697void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700698 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700699 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700}
701
702void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700703 ATRACE_CALL();
704 preComposition();
705 rebuildLayerStacks();
706 setUpHWComposer();
707 doDebugFlashRegions();
708 doComposition();
709 postComposition();
710}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711
Mathias Agopiancd60f992012-08-16 16:28:27 -0700712void SurfaceFlinger::doDebugFlashRegions()
713{
714 // is debugging enabled
715 if (CC_LIKELY(!mDebugRegion))
716 return;
717
718 const bool repaintEverything = mRepaintEverything;
719 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
720 const sp<DisplayDevice>& hw(mDisplays[dpy]);
721 if (hw->canDraw()) {
722 // transform the dirty region into this screen's coordinate space
723 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
724 if (!dirtyRegion.isEmpty()) {
725 // redraw the whole screen
726 doComposeSurfaces(hw, Region(hw->bounds()));
727
728 // and draw the dirty region
729 glDisable(GL_TEXTURE_EXTERNAL_OES);
730 glDisable(GL_TEXTURE_2D);
731 glDisable(GL_BLEND);
732 glColor4f(1, 0, 1, 1);
733 const int32_t height = hw->getHeight();
734 Region::const_iterator it = dirtyRegion.begin();
735 Region::const_iterator const end = dirtyRegion.end();
736 while (it != end) {
737 const Rect& r = *it++;
738 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800739 { (GLfloat) r.left, (GLfloat) (height - r.top) },
740 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
741 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
742 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700743 };
744 glVertexPointer(2, GL_FLOAT, 0, vertices);
745 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
746 }
747 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700748 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700749 }
750 }
751 }
752
753 postFramebuffer();
754
755 if (mDebugRegion > 1) {
756 usleep(mDebugRegion * 1000);
757 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700758
759 HWComposer& hwc(getHwComposer());
760 if (hwc.initCheck() == NO_ERROR) {
761 status_t err = hwc.prepare();
762 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
763 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764}
765
766void SurfaceFlinger::preComposition()
767{
768 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700769 const LayerVector& layers(mDrawingState.layersSortedByZ);
770 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700772 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773 needExtraInvalidate = true;
774 }
775 }
776 if (needExtraInvalidate) {
777 signalLayerUpdate();
778 }
779}
780
781void SurfaceFlinger::postComposition()
782{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700783 const LayerVector& layers(mDrawingState.layersSortedByZ);
784 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700786 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800788
789 if (mAnimCompositionPending) {
790 mAnimCompositionPending = false;
791
792 const HWComposer& hwc = getHwComposer();
793 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800794 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800795 mAnimFrameTracker.setActualPresentFence(presentFence);
796 } else {
797 // The HWC doesn't support present fences, so use the refresh
798 // timestamp instead.
799 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
800 mAnimFrameTracker.setActualPresentTime(presentTime);
801 }
802 mAnimFrameTracker.advanceFrame();
803 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804}
805
806void SurfaceFlinger::rebuildLayerStacks() {
807 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700810 mVisibleRegionsDirty = false;
811 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700812
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700813 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700814 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700815 Region opaqueRegion;
816 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800817 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700818 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700819 const Transform& tr(hw->getTransform());
820 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700821 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700822 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700823 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700824
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700825 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700826 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const sp<Layer>& layer(layers[i]);
828 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700829 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700830 Region drawRegion(tr.transform(
831 layer->visibleNonTransparentRegion));
832 drawRegion.andSelf(bounds);
833 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700834 layersSortedByZ.add(layer);
835 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700836 }
837 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700838 }
Mathias Agopian42977342012-08-05 00:40:46 -0700839 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700840 hw->undefinedRegion.set(bounds);
841 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
842 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700843 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700844 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700846
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 HWComposer& hwc(getHwComposer());
849 if (hwc.initCheck() == NO_ERROR) {
850 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700851 if (CC_UNLIKELY(mHwWorkListDirty)) {
852 mHwWorkListDirty = false;
853 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
854 sp<const DisplayDevice> hw(mDisplays[dpy]);
855 const int32_t id = hw->getHwcDisplayId();
856 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700858 hw->getVisibleLayersSortedByZ());
859 const size_t count = currentLayers.size();
860 if (hwc.createWorkList(id, count) == NO_ERROR) {
861 HWComposer::LayerListIterator cur = hwc.begin(id);
862 const HWComposer::LayerListIterator end = hwc.end(id);
863 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800864 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700865 layer->setGeometry(hw, *cur);
866 if (mDebugDisableHWC || mDebugRegion) {
867 cur->setSkip(true);
868 }
869 }
870 }
871 }
872 }
873 }
874
875 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700876 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700877 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700878 const int32_t id = hw->getHwcDisplayId();
879 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700882 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700883 HWComposer::LayerListIterator cur = hwc.begin(id);
884 const HWComposer::LayerListIterator end = hwc.end(id);
885 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
886 /*
887 * update the per-frame h/w composer data for each layer
888 * and build the transparent region of the FB
889 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700891 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700892 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700893 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700894 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700895
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700896 status_t err = hwc.prepare();
897 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
898 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700899}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700900
Mathias Agopiancd60f992012-08-16 16:28:27 -0700901void SurfaceFlinger::doComposition() {
902 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700903 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700904 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700905 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906 if (hw->canDraw()) {
907 // transform the dirty region into this screen's coordinate space
908 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800909
910 // repaint the framebuffer (if needed)
911 doDisplayComposition(hw, dirtyRegion);
912
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913 hw->dirtyRegion.clear();
914 hw->flip(hw->swapRegion);
915 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700916 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700917 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700918 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700919 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700920 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700921}
922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923void SurfaceFlinger::postFramebuffer()
924{
Mathias Agopian841cde52012-03-01 15:44:37 -0800925 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800926
Mathias Agopiana44b0412011-10-16 18:46:35 -0700927 const nsecs_t now = systemTime();
928 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700929
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700930 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700931 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700932 if (!hwc.supportsFramebufferTarget()) {
933 // EGL spec says:
934 // "surface must be bound to the calling thread's current context,
935 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700936 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700937 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700938 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700939 }
940
Mathias Agopian92a979a2012-08-02 18:32:23 -0700941 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700942 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800943 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700944 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700946 int32_t id = hw->getHwcDisplayId();
947 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700948 HWComposer::LayerListIterator cur = hwc.begin(id);
949 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700950 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700951 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700952 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700953 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700954 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700955 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700956 }
Jesse Hallef194142012-06-14 14:45:17 -0700957 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800958 }
959
Mathias Agopiana44b0412011-10-16 18:46:35 -0700960 mLastSwapBufferTime = systemTime() - now;
961 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962}
963
Mathias Agopian87baae12012-07-31 12:38:26 -0700964void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965{
Mathias Agopian841cde52012-03-01 15:44:37 -0800966 ATRACE_CALL();
967
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700968 // here we keep a copy of the drawing state (that is the state that's
969 // going to be overwritten by handleTransactionLocked()) outside of
970 // mStateLock so that the side-effects of the State assignment
971 // don't happen with mStateLock held (which can cause deadlocks).
972 State drawingState(mDrawingState);
973
Mathias Agopianca4d3602011-05-19 15:38:14 -0700974 Mutex::Autolock _l(mStateLock);
975 const nsecs_t now = systemTime();
976 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977
Mathias Agopianca4d3602011-05-19 15:38:14 -0700978 // Here we're guaranteed that some transaction flags are set
979 // so we can call handleTransactionLocked() unconditionally.
980 // We call getTransactionFlags(), which will also clear the flags,
981 // with mStateLock held to guarantee that mCurrentState won't change
982 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700983
Mathias Agopiane57f2922012-08-09 16:29:12 -0700984 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700985 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700986
Mathias Agopianca4d3602011-05-19 15:38:14 -0700987 mLastTransactionTime = systemTime() - now;
988 mDebugInTransaction = 0;
989 invalidateHwcGeometry();
990 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700991}
992
Mathias Agopian87baae12012-07-31 12:38:26 -0700993void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700994{
995 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 const size_t count = currentLayers.size();
997
998 /*
999 * Traversal of the children
1000 * (perform the transaction for each of them if needed)
1001 */
1002
Mathias Agopian3559b072012-08-15 13:46:03 -07001003 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001005 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1007 if (!trFlags) continue;
1008
1009 const uint32_t flags = layer->doTransaction(0);
1010 if (flags & Layer::eVisibleRegion)
1011 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 }
1013 }
1014
1015 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001016 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 */
1018
Mathias Agopiane57f2922012-08-09 16:29:12 -07001019 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001020 // here we take advantage of Vector's copy-on-write semantics to
1021 // improve performance by skipping the transaction entirely when
1022 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001023 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1024 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001025 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001027 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001028 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001029
1030 // find the displays that were removed
1031 // (ie: in drawing state but not in current state)
1032 // also handle displays that changed
1033 // (ie: displays that are in both lists)
1034 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001035 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1036 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001037 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001038 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001039 // Call makeCurrent() on the primary display so we can
1040 // be sure that nothing associated with this display
1041 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001042 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001043 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001044 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1045 if (hw != NULL)
1046 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001047 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1048 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001049 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001050 } else {
1051 ALOGW("trying to remove the main display");
1052 }
1053 } else {
1054 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001055 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001056 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001057 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001058 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001059 // recreating the DisplayDevice, so we just remove it
1060 // from the drawing state, so that it get re-added
1061 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001062 sp<DisplayDevice> hw(getDisplayDevice(display));
1063 if (hw != NULL)
1064 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001065 mDisplays.removeItem(display);
1066 mDrawingState.displays.removeItemsAt(i);
1067 dc--; i--;
1068 // at this point we must loop to the next item
1069 continue;
1070 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001071
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001072 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001073 if (disp != NULL) {
1074 if (state.layerStack != draw[i].layerStack) {
1075 disp->setLayerStack(state.layerStack);
1076 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001077 if ((state.orientation != draw[i].orientation)
1078 || (state.viewport != draw[i].viewport)
1079 || (state.frame != draw[i].frame))
1080 {
1081 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001082 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001083 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001084 }
1085 }
1086 }
1087
1088 // find displays that were added
1089 // (ie: in current state but not in drawing state)
1090 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001092 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001093
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001094 sp<DisplaySurface> dispSurface;
Jesse Hall02d86562013-03-25 14:43:23 -07001095 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001096 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001097 // Virtual displays without a surface are dormant:
1098 // they have external state (layer stack, projection,
1099 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001100 if (state.surface != NULL) {
Jesse Hall02d86562013-03-25 14:43:23 -07001101 hwcDisplayId = allocateHwcDisplayId(state.type);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001102 dispSurface = new VirtualDisplaySurface(
Jesse Hallffe1f192013-03-22 15:13:48 -07001103 *mHwc, hwcDisplayId, state.surface,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001104 state.displayName);
1105 }
1106 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001107 ALOGE_IF(state.surface!=NULL,
1108 "adding a supported display, but rendering "
1109 "surface is provided (%p), ignoring it",
1110 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001111 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001112 // for supported (by hwc) displays we provide our
1113 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001114 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001115 }
1116
1117 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001118 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001119 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001120 state.type, hwcDisplayId, state.isSecure,
1121 display, dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001122 hw->setLayerStack(state.layerStack);
1123 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001124 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001125 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001126 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001127 if (state.isVirtualDisplay()) {
1128 if (hwcDisplayId >= 0) {
1129 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1130 hw->getWidth(), hw->getHeight(),
1131 hw->getFormat());
1132 }
1133 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001134 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001135 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001136 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001137 }
1138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001140 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopian84300952012-11-21 16:02:13 -08001142 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1143 // The transform hint might have changed for some layers
1144 // (either because a display has changed, or because a layer
1145 // as changed).
1146 //
1147 // Walk through all the layers in currentLayers,
1148 // and update their transform hint.
1149 //
1150 // If a layer is visible only on a single display, then that
1151 // display is used to calculate the hint, otherwise we use the
1152 // default display.
1153 //
1154 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1155 // the hint is set before we acquire a buffer from the surface texture.
1156 //
1157 // NOTE: layer transactions have taken place already, so we use their
1158 // drawing state. However, SurfaceFlinger's own transaction has not
1159 // happened yet, so we must use the current state layer list
1160 // (soon to become the drawing state list).
1161 //
1162 sp<const DisplayDevice> disp;
1163 uint32_t currentlayerStack = 0;
1164 for (size_t i=0; i<count; i++) {
1165 // NOTE: we rely on the fact that layers are sorted by
1166 // layerStack first (so we don't have to traverse the list
1167 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001168 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001169 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001170 if (i==0 || currentlayerStack != layerStack) {
1171 currentlayerStack = layerStack;
1172 // figure out if this layerstack is mirrored
1173 // (more than one display) if so, pick the default display,
1174 // if not, pick the only display it's on.
1175 disp.clear();
1176 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1177 sp<const DisplayDevice> hw(mDisplays[dpy]);
1178 if (hw->getLayerStack() == currentlayerStack) {
1179 if (disp == NULL) {
1180 disp = hw;
1181 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001182 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001183 break;
1184 }
1185 }
1186 }
1187 }
Chet Haase91d25932013-04-11 15:24:55 -07001188 if (disp == NULL) {
1189 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1190 // redraw after transform hint changes. See bug 8508397.
1191
1192 // could be null when this layer is using a layerStack
1193 // that is not visible on any display. Also can occur at
1194 // screen off/on times.
1195 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001196 }
Chet Haase91d25932013-04-11 15:24:55 -07001197 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001198 }
1199 }
1200
1201
Mathias Agopian3559b072012-08-15 13:46:03 -07001202 /*
1203 * Perform our own transaction if needed
1204 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001205
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001206 const LayerVector& layers(mDrawingState.layersSortedByZ);
1207 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001208 // layers have been added
1209 mVisibleRegionsDirty = true;
1210 }
1211
1212 // some layers might have been removed, so
1213 // we need to update the regions they're exposing.
1214 if (mLayersRemoved) {
1215 mLayersRemoved = false;
1216 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001217 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001218 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001219 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001220 if (currentLayers.indexOf(layer) < 0) {
1221 // this layer is not visible anymore
1222 // TODO: we could traverse the tree from front to back and
1223 // compute the actual visible region
1224 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001225 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001226 Region visibleReg = s.transform.transform(
1227 Region(Rect(s.active.w, s.active.h)));
1228 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001229 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 }
1232
1233 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001234}
1235
1236void SurfaceFlinger::commitTransaction()
1237{
1238 if (!mLayersPendingRemoval.isEmpty()) {
1239 // Notify removed layers now that they can't be drawn from
1240 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1241 mLayersPendingRemoval[i]->onRemoved();
1242 }
1243 mLayersPendingRemoval.clear();
1244 }
1245
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001246 // If this transaction is part of a window animation then the next frame
1247 // we composite should be considered an animation as well.
1248 mAnimCompositionPending = mAnimTransactionPending;
1249
Mathias Agopian4fec8732012-06-29 14:12:52 -07001250 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001251 mTransactionPending = false;
1252 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001253 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254}
1255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001257 const LayerVector& currentLayers, uint32_t layerStack,
1258 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259{
Mathias Agopian841cde52012-03-01 15:44:37 -08001260 ATRACE_CALL();
1261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 Region aboveOpaqueLayers;
1263 Region aboveCoveredLayers;
1264 Region dirty;
1265
Mathias Agopian87baae12012-07-31 12:38:26 -07001266 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267
1268 size_t i = currentLayers.size();
1269 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001270 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271
1272 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001273 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274
Jesse Hall01e29052013-02-19 16:13:35 -08001275 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001276 if (s.layerStack != layerStack)
1277 continue;
1278
Mathias Agopianab028732010-03-16 16:41:46 -07001279 /*
1280 * opaqueRegion: area of a surface that is fully opaque.
1281 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001283
1284 /*
1285 * visibleRegion: area of a surface that is visible on screen
1286 * and not fully transparent. This is essentially the layer's
1287 * footprint minus the opaque regions above it.
1288 * Areas covered by a translucent surface are considered visible.
1289 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001291
1292 /*
1293 * coveredRegion: area of a surface that is covered by all
1294 * visible regions above it (which includes the translucent areas).
1295 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001297
Jesse Halla8026d22012-09-25 13:25:04 -07001298 /*
1299 * transparentRegion: area of a surface that is hinted to be completely
1300 * transparent. This is only used to tell when the layer has no visible
1301 * non-transparent regions and can be removed from the layer list. It
1302 * does not affect the visibleRegion of this layer or any layers
1303 * beneath it. The hint may not be correct if apps don't respect the
1304 * SurfaceView restrictions (which, sadly, some don't).
1305 */
1306 Region transparentRegion;
1307
Mathias Agopianab028732010-03-16 16:41:46 -07001308
1309 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001310 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001311 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001312 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001314 if (!visibleRegion.isEmpty()) {
1315 // Remove the transparent area from the visible region
1316 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001317 const Transform tr(s.transform);
1318 if (tr.transformed()) {
1319 if (tr.preserveRects()) {
1320 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001321 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 } else {
1323 // transformation too complex, can't do the
1324 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001325 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001326 }
1327 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001328 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001329 }
Mathias Agopianab028732010-03-16 16:41:46 -07001330 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331
Mathias Agopianab028732010-03-16 16:41:46 -07001332 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001333 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001334 if (s.alpha==255 && !translucent &&
1335 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1336 // the opaque region is the layer's footprint
1337 opaqueRegion = visibleRegion;
1338 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 }
1340 }
1341
Mathias Agopianab028732010-03-16 16:41:46 -07001342 // Clip the covered region to the visible region
1343 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1344
1345 // Update aboveCoveredLayers for next (lower) layer
1346 aboveCoveredLayers.orSelf(visibleRegion);
1347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 // subtract the opaque region covered by the layers above us
1349 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350
1351 // compute this layer's dirty region
1352 if (layer->contentDirty) {
1353 // we need to invalidate the whole region
1354 dirty = visibleRegion;
1355 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001356 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 layer->contentDirty = false;
1358 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001359 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001360 * the exposed region consists of two components:
1361 * 1) what's VISIBLE now and was COVERED before
1362 * 2) what's EXPOSED now less what was EXPOSED before
1363 *
1364 * note that (1) is conservative, we start with the whole
1365 * visible region but only keep what used to be covered by
1366 * something -- which mean it may have been exposed.
1367 *
1368 * (2) handles areas that were not covered by anything but got
1369 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001370 */
Mathias Agopianab028732010-03-16 16:41:46 -07001371 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001372 const Region oldVisibleRegion = layer->visibleRegion;
1373 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001374 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1375 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 }
1377 dirty.subtractSelf(aboveOpaqueLayers);
1378
1379 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001380 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381
Mathias Agopianab028732010-03-16 16:41:46 -07001382 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001384
Jesse Halla8026d22012-09-25 13:25:04 -07001385 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 layer->setVisibleRegion(visibleRegion);
1387 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001388 layer->setVisibleNonTransparentRegion(
1389 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 }
1391
Mathias Agopian87baae12012-07-31 12:38:26 -07001392 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393}
1394
Mathias Agopian87baae12012-07-31 12:38:26 -07001395void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1396 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001397 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001398 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1399 if (hw->getLayerStack() == layerStack) {
1400 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001401 }
1402 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001403}
1404
1405void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001407 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408
Mathias Agopian4fec8732012-06-29 14:12:52 -07001409 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001410 const LayerVector& layers(mDrawingState.layersSortedByZ);
1411 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001412 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001413 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001414 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001415 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001416 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001417 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001418
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001419 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420}
1421
Mathias Agopianad456f92011-01-13 17:53:01 -08001422void SurfaceFlinger::invalidateHwcGeometry()
1423{
1424 mHwWorkListDirty = true;
1425}
1426
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001427
Mathias Agopiancd60f992012-08-16 16:28:27 -07001428void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001429 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430{
Mathias Agopian87baae12012-07-31 12:38:26 -07001431 Region dirtyRegion(inDirtyRegion);
1432
Mathias Agopianb8a55602009-06-26 19:06:36 -07001433 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001434 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435
Mathias Agopian42977342012-08-05 00:40:46 -07001436 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001437 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001438 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1439 // takes a rectangle, we must make sure to update that whole
1440 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001441 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001442 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001443 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001444 // We need to redraw the rectangle that will be updated
1445 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001446 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001447 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001448 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001449 } else {
1450 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001451 dirtyRegion.set(hw->bounds());
1452 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 }
1454 }
1455
Mathias Agopiancd60f992012-08-16 16:28:27 -07001456 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001458 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001459 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001460
1461 // swap buffers (presentation)
1462 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463}
1464
Mathias Agopiancd60f992012-08-16 16:28:27 -07001465void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001466{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001467 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001468 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001469 HWComposer::LayerListIterator cur = hwc.begin(id);
1470 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001471
Mathias Agopian85d751c2012-08-29 16:59:24 -07001472 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1473 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001474 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001475 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1476 hw->getDisplayName().string());
1477 return;
1478 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001479
1480 // set the frame buffer
1481 glMatrixMode(GL_MODELVIEW);
1482 glLoadIdentity();
1483
1484 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001485 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001486 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001487 // when using overlays, we assume a fully transparent framebuffer
1488 // NOTE: we could reduce how much we need to clear, for instance
1489 // remove where there are opaque FB layers. however, on some
1490 // GPUs doing a "clean slate" glClear might be more efficient.
1491 // We'll revisit later if needed.
1492 glClearColor(0, 0, 0, 0);
1493 glClear(GL_COLOR_BUFFER_BIT);
1494 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001495 // we start with the whole screen area
1496 const Region bounds(hw->getBounds());
1497
1498 // we remove the scissor part
1499 // we're left with the letterbox region
1500 // (common case is that letterbox ends-up being empty)
1501 const Region letterbox(bounds.subtract(hw->getScissor()));
1502
1503 // compute the area to clear
1504 Region region(hw->undefinedRegion.merge(letterbox));
1505
1506 // but limit it to the dirty region
1507 region.andSelf(dirty);
1508
Mathias Agopianb9494d52012-04-18 02:28:45 -07001509 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001510 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001511 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001512 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001513 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001514 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001515
Mathias Agopian766dc492012-10-30 18:08:06 -07001516 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1517 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001518 // scissor on the main display. It should never be needed
1519 // anyways (though in theory it could since the API allows it).
1520 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001521 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001522 if (scissor != bounds) {
1523 // scissor doesn't match the screen's dimensions, so we
1524 // need to clear everything outside of it and enable
1525 // the GL scissor so we don't draw anything where we shouldn't
1526 const GLint height = hw->getHeight();
1527 glScissor(scissor.left, height - scissor.bottom,
1528 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001529 // enable scissor for this frame
1530 glEnable(GL_SCISSOR_TEST);
1531 }
1532 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001533 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001534
Mathias Agopian85d751c2012-08-29 16:59:24 -07001535 /*
1536 * and then, render the layers targeted at the framebuffer
1537 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001538
Mathias Agopian13127d82013-03-05 17:47:11 -08001539 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001540 const size_t count = layers.size();
1541 const Transform& tr = hw->getTransform();
1542 if (cur != end) {
1543 // we're using h/w composer
1544 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001545 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001546 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001547 if (!clip.isEmpty()) {
1548 switch (cur->getCompositionType()) {
1549 case HWC_OVERLAY: {
1550 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1551 && i
1552 && layer->isOpaque()
1553 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001554 // never clear the very first layer since we're
1555 // guaranteed the FB is already cleared
1556 layer->clearWithOpenGL(hw, clip);
1557 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001558 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001559 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001560 case HWC_FRAMEBUFFER: {
1561 layer->draw(hw, clip);
1562 break;
1563 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001564 case HWC_FRAMEBUFFER_TARGET: {
1565 // this should not happen as the iterator shouldn't
1566 // let us get there.
1567 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1568 break;
1569 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001570 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001571 }
1572 layer->setAcquireFence(hw, *cur);
1573 }
1574 } else {
1575 // we're not using h/w composer
1576 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001577 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001578 const Region clip(dirty.intersect(
1579 tr.transform(layer->visibleRegion)));
1580 if (!clip.isEmpty()) {
1581 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001582 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001583 }
1584 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001585
1586 // disable scissor at the end of the frame
1587 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588}
1589
Mathias Agopian55801e42012-08-27 18:54:24 -07001590void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1591 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001593 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001594 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001595 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001596 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001597
Mathias Agopian55801e42012-08-27 18:54:24 -07001598 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001599 Region::const_iterator it = region.begin();
1600 Region::const_iterator const end = region.end();
1601 while (it != end) {
1602 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001603 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001604 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1605 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1606 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1607 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001608 };
1609 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001610 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1611 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612}
1613
Mathias Agopianac9fa422013-02-11 16:40:36 -08001614void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1615 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001616 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001617 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001618{
Mathias Agopian96f08192010-06-02 23:28:45 -07001619 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001620 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001621
Mathias Agopian96f08192010-06-02 23:28:45 -07001622 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001623 Mutex::Autolock _l(mStateLock);
1624 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001625 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001626}
1627
Mathias Agopian13127d82013-03-05 17:47:11 -08001628status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629{
1630 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001631 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001633 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001634 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001635 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 return NO_ERROR;
1637 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001638 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639}
1640
Mathias Agopiandea20b12011-05-03 17:04:02 -07001641uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1642{
1643 return android_atomic_release_load(&mTransactionFlags);
1644}
1645
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1647{
1648 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1649}
1650
Mathias Agopianbb641242010-05-18 17:06:55 -07001651uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652{
1653 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1654 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001655 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 }
1657 return old;
1658}
1659
Mathias Agopian8b33f032012-07-24 20:43:54 -07001660void SurfaceFlinger::setTransactionState(
1661 const Vector<ComposerState>& state,
1662 const Vector<DisplayState>& displays,
1663 uint32_t flags)
1664{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001665 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001666 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001667 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001668
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001669 if (flags & eAnimation) {
1670 // For window updates that are part of an animation we must wait for
1671 // previous animation "frames" to be handled.
1672 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001673 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001674 if (CC_UNLIKELY(err != NO_ERROR)) {
1675 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001676 // caller after a few seconds.
1677 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1678 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001679 mAnimTransactionPending = false;
1680 break;
1681 }
1682 }
1683 }
1684
Mathias Agopiane57f2922012-08-09 16:29:12 -07001685 size_t count = displays.size();
1686 for (size_t i=0 ; i<count ; i++) {
1687 const DisplayState& s(displays[i]);
1688 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001689 }
1690
Mathias Agopiane57f2922012-08-09 16:29:12 -07001691 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001692 for (size_t i=0 ; i<count ; i++) {
1693 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001694 // Here we need to check that the interface we're given is indeed
1695 // one of our own. A malicious client could give us a NULL
1696 // IInterface, or one of its own or even one of our own but a
1697 // different type. All these situations would cause us to crash.
1698 //
1699 // NOTE: it would be better to use RTTI as we could directly check
1700 // that we have a Client*. however, RTTI is disabled in Android.
1701 if (s.client != NULL) {
1702 sp<IBinder> binder = s.client->asBinder();
1703 if (binder != NULL) {
1704 String16 desc(binder->getInterfaceDescriptor());
1705 if (desc == ISurfaceComposerClient::descriptor) {
1706 sp<Client> client( static_cast<Client *>(s.client.get()) );
1707 transactionFlags |= setClientStateLocked(client, s.state);
1708 }
1709 }
1710 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001711 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001712
Mathias Agopian386aa982011-11-07 21:58:03 -08001713 if (transactionFlags) {
1714 // this triggers the transaction
1715 setTransactionFlags(transactionFlags);
1716
1717 // if this is a synchronous transaction, wait for it to take effect
1718 // before returning.
1719 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001720 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001721 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001722 if (flags & eAnimation) {
1723 mAnimTransactionPending = true;
1724 }
1725 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001726 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1727 if (CC_UNLIKELY(err != NO_ERROR)) {
1728 // just in case something goes wrong in SF, return to the
1729 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001730 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1731 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001732 break;
1733 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001734 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 }
1736}
1737
Mathias Agopiane57f2922012-08-09 16:29:12 -07001738uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1739{
Jesse Hall9a143922012-10-04 16:29:19 -07001740 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1741 if (dpyIdx < 0)
1742 return 0;
1743
Mathias Agopiane57f2922012-08-09 16:29:12 -07001744 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001745 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001746 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001747 const uint32_t what = s.what;
1748 if (what & DisplayState::eSurfaceChanged) {
1749 if (disp.surface->asBinder() != s.surface->asBinder()) {
1750 disp.surface = s.surface;
1751 flags |= eDisplayTransactionNeeded;
1752 }
1753 }
1754 if (what & DisplayState::eLayerStackChanged) {
1755 if (disp.layerStack != s.layerStack) {
1756 disp.layerStack = s.layerStack;
1757 flags |= eDisplayTransactionNeeded;
1758 }
1759 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001760 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001761 if (disp.orientation != s.orientation) {
1762 disp.orientation = s.orientation;
1763 flags |= eDisplayTransactionNeeded;
1764 }
1765 if (disp.frame != s.frame) {
1766 disp.frame = s.frame;
1767 flags |= eDisplayTransactionNeeded;
1768 }
1769 if (disp.viewport != s.viewport) {
1770 disp.viewport = s.viewport;
1771 flags |= eDisplayTransactionNeeded;
1772 }
1773 }
1774 }
1775 return flags;
1776}
1777
1778uint32_t SurfaceFlinger::setClientStateLocked(
1779 const sp<Client>& client,
1780 const layer_state_t& s)
1781{
1782 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001783 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001784 if (layer != 0) {
1785 const uint32_t what = s.what;
1786 if (what & layer_state_t::ePositionChanged) {
1787 if (layer->setPosition(s.x, s.y))
1788 flags |= eTraversalNeeded;
1789 }
1790 if (what & layer_state_t::eLayerChanged) {
1791 // NOTE: index needs to be calculated before we update the state
1792 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1793 if (layer->setLayer(s.z)) {
1794 mCurrentState.layersSortedByZ.removeAt(idx);
1795 mCurrentState.layersSortedByZ.add(layer);
1796 // we need traversal (state changed)
1797 // AND transaction (list changed)
1798 flags |= eTransactionNeeded|eTraversalNeeded;
1799 }
1800 }
1801 if (what & layer_state_t::eSizeChanged) {
1802 if (layer->setSize(s.w, s.h)) {
1803 flags |= eTraversalNeeded;
1804 }
1805 }
1806 if (what & layer_state_t::eAlphaChanged) {
1807 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1808 flags |= eTraversalNeeded;
1809 }
1810 if (what & layer_state_t::eMatrixChanged) {
1811 if (layer->setMatrix(s.matrix))
1812 flags |= eTraversalNeeded;
1813 }
1814 if (what & layer_state_t::eTransparentRegionChanged) {
1815 if (layer->setTransparentRegionHint(s.transparentRegion))
1816 flags |= eTraversalNeeded;
1817 }
1818 if (what & layer_state_t::eVisibilityChanged) {
1819 if (layer->setFlags(s.flags, s.mask))
1820 flags |= eTraversalNeeded;
1821 }
1822 if (what & layer_state_t::eCropChanged) {
1823 if (layer->setCrop(s.crop))
1824 flags |= eTraversalNeeded;
1825 }
1826 if (what & layer_state_t::eLayerStackChanged) {
1827 // NOTE: index needs to be calculated before we update the state
1828 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1829 if (layer->setLayerStack(s.layerStack)) {
1830 mCurrentState.layersSortedByZ.removeAt(idx);
1831 mCurrentState.layersSortedByZ.add(layer);
1832 // we need traversal (state changed)
1833 // AND transaction (list changed)
1834 flags |= eTransactionNeeded|eTraversalNeeded;
1835 }
1836 }
1837 }
1838 return flags;
1839}
1840
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001841status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001842 const String8& name,
1843 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001844 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1845 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001847 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001848 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001849 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001850 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001851 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001852 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001853
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001854 status_t result = NO_ERROR;
1855
1856 sp<Layer> layer;
1857
Mathias Agopian3165cc22012-08-08 19:42:09 -07001858 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1859 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001860 result = createNormalLayer(client,
1861 name, w, h, flags, format,
1862 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001864 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001865 result = createDimLayer(client,
1866 name, w, h, flags,
1867 handle, gbp, &layer);
1868 break;
1869 default:
1870 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 break;
1872 }
1873
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001874 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001875 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001876 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001878 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879}
1880
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001881status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1882 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1883 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884{
1885 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001886 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 case PIXEL_FORMAT_TRANSPARENT:
1888 case PIXEL_FORMAT_TRANSLUCENT:
1889 format = PIXEL_FORMAT_RGBA_8888;
1890 break;
1891 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001892#ifdef NO_RGBX_8888
1893 format = PIXEL_FORMAT_RGB_565;
1894#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001895 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001896#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 break;
1898 }
1899
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001900#ifdef NO_RGBX_8888
1901 if (format == PIXEL_FORMAT_RGBX_8888)
1902 format = PIXEL_FORMAT_RGBA_8888;
1903#endif
1904
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905 *outLayer = new Layer(this, client, name, w, h, flags);
1906 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1907 if (err == NO_ERROR) {
1908 *handle = (*outLayer)->getHandle();
1909 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001911
1912 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1913 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914}
1915
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001916status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1917 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1918 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001920 *outLayer = new LayerDim(this, client, name, w, h, flags);
1921 *handle = (*outLayer)->getHandle();
1922 *gbp = (*outLayer)->getBufferQueue();
1923 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001924}
1925
Mathias Agopianac9fa422013-02-11 16:40:36 -08001926status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927{
Mathias Agopian67106042013-03-14 19:18:13 -07001928 // called by the window manager when it wants to remove a Layer
1929 status_t err = NO_ERROR;
1930 sp<Layer> l(client->getLayerUser(handle));
1931 if (l != NULL) {
1932 err = removeLayer(l);
1933 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1934 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001935 }
1936 return err;
1937}
1938
Mathias Agopian13127d82013-03-05 17:47:11 -08001939status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001940{
Mathias Agopian67106042013-03-14 19:18:13 -07001941 // called by ~LayerCleaner() when all references to the IBinder (handle)
1942 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001943 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001944 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001945 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001946 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001947 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001948 "error removing layer=%p (%s)", l.get(), strerror(-err));
1949 }
1950 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951}
1952
Mathias Agopianb60314a2012-04-10 22:09:54 -07001953// ---------------------------------------------------------------------------
1954
Andy McFadden13a082e2012-08-24 10:16:42 -07001955void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001956 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001957 Vector<ComposerState> state;
1958 Vector<DisplayState> displays;
1959 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001960 d.what = DisplayState::eDisplayProjectionChanged |
1961 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001962 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001963 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001964 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001965 d.frame.makeInvalid();
1966 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001967 displays.add(d);
1968 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001969 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001970}
1971
1972void SurfaceFlinger::initializeDisplays() {
1973 class MessageScreenInitialized : public MessageBase {
1974 SurfaceFlinger* flinger;
1975 public:
1976 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1977 virtual bool handler() {
1978 flinger->onInitializeDisplays();
1979 return true;
1980 }
1981 };
1982 sp<MessageBase> msg = new MessageScreenInitialized(this);
1983 postMessageAsync(msg); // we may be called from main thread, use async message
1984}
1985
1986
Mathias Agopiancde87a32012-09-13 14:09:01 -07001987void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001988 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
1989 if (hw->isScreenAcquired()) {
1990 // this is expected, e.g. when power manager wakes up during boot
1991 ALOGD(" screen was previously acquired");
1992 return;
1993 }
1994
Mathias Agopian42977342012-08-05 00:40:46 -07001995 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001996 int32_t type = hw->getDisplayType();
1997 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
1998 // built-in display, tell the HWC
1999 getHwComposer().acquire(type);
2000
2001 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2002 // FIXME: eventthread only knows about the main display right now
2003 mEventThread->onScreenAcquired();
2004 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002005 }
Mathias Agopian20128302012-08-13 18:32:13 -07002006 mVisibleRegionsDirty = true;
2007 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002008}
2009
Mathias Agopiancde87a32012-09-13 14:09:01 -07002010void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002011 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2012 if (!hw->isScreenAcquired()) {
2013 ALOGD(" screen was previously released");
2014 return;
2015 }
2016
2017 hw->releaseScreen();
2018 int32_t type = hw->getDisplayType();
2019 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2020 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002021 // FIXME: eventthread only knows about the main display right now
2022 mEventThread->onScreenReleased();
2023 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002024
2025 // built-in display, tell the HWC
2026 getHwComposer().release(type);
2027 }
2028 mVisibleRegionsDirty = true;
2029 // from this point on, SF will stop drawing on this display
2030}
2031
2032void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2033 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002034 SurfaceFlinger& mFlinger;
2035 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002036 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002037 MessageScreenAcquired(SurfaceFlinger& flinger,
2038 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002039 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002040 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2041 if (hw == NULL) {
2042 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2043 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2044 ALOGW("Attempt to unblank virtual display");
2045 } else {
2046 mFlinger.onScreenAcquired(hw);
2047 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002048 return true;
2049 }
2050 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002051 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2052 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002053}
2054
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002055void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002056 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002057 SurfaceFlinger& mFlinger;
2058 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002059 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002060 MessageScreenReleased(SurfaceFlinger& flinger,
2061 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002062 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002063 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2064 if (hw == NULL) {
2065 ALOGE("Attempt to blank null display %p", mDisplay.get());
2066 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2067 ALOGW("Attempt to blank virtual display");
2068 } else {
2069 mFlinger.onScreenReleased(hw);
2070 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002071 return true;
2072 }
2073 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002074 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2075 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002076}
2077
2078// ---------------------------------------------------------------------------
2079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2081{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002083
Mathias Agopianbd115332013-04-18 16:41:04 -07002084 IPCThreadState* ipc = IPCThreadState::self();
2085 const int pid = ipc->getCallingPid();
2086 const int uid = ipc->getCallingUid();
2087 if ((uid != AID_SHELL) &&
2088 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002089 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002090 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002092 // Try to get the main lock, but don't insist if we can't
2093 // (this would indicate SF is stuck, but we want to be able to
2094 // print something in dumpsys).
2095 int retry = 3;
2096 while (mStateLock.tryLock()<0 && --retry>=0) {
2097 usleep(1000000);
2098 }
2099 const bool locked(retry >= 0);
2100 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002101 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002102 "SurfaceFlinger appears to be unresponsive, "
2103 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002104 }
2105
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002106 bool dumpAll = true;
2107 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002108 size_t numArgs = args.size();
2109 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002110 if ((index < numArgs) &&
2111 (args[index] == String16("--list"))) {
2112 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002113 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002114 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002115 }
2116
2117 if ((index < numArgs) &&
2118 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002119 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002120 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002121 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002122 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002123
2124 if ((index < numArgs) &&
2125 (args[index] == String16("--latency-clear"))) {
2126 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002127 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002128 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002129 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002130 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002131
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002132 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002133 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002134 }
2135
Mathias Agopian9795c422009-08-26 16:36:26 -07002136 if (locked) {
2137 mStateLock.unlock();
2138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002139 }
2140 write(fd, result.string(), result.size());
2141 return NO_ERROR;
2142}
2143
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002144void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002145 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002146{
2147 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2148 const size_t count = currentLayers.size();
2149 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002150 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002151 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002152 }
2153}
2154
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002155void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002156 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002157{
2158 String8 name;
2159 if (index < args.size()) {
2160 name = String8(args[index]);
2161 index++;
2162 }
2163
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002164 const nsecs_t period =
2165 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2166 result.appendFormat("%lld\n", period);
2167
2168 if (name.isEmpty()) {
2169 mAnimFrameTracker.dump(result);
2170 } else {
2171 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2172 const size_t count = currentLayers.size();
2173 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002174 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002175 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002176 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002177 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002178 }
2179 }
2180}
2181
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002182void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002183 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002184{
2185 String8 name;
2186 if (index < args.size()) {
2187 name = String8(args[index]);
2188 index++;
2189 }
2190
2191 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2192 const size_t count = currentLayers.size();
2193 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002194 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195 if (name.isEmpty() || (name == layer->getName())) {
2196 layer->clearStats();
2197 }
2198 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002199
2200 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002201}
2202
Andy McFadden4803b742012-09-24 19:07:20 -07002203/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2204{
2205 static const char* config =
2206 " [sf"
2207#ifdef NO_RGBX_8888
2208 " NO_RGBX_8888"
2209#endif
2210#ifdef HAS_CONTEXT_PRIORITY
2211 " HAS_CONTEXT_PRIORITY"
2212#endif
2213#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2214 " NEVER_DEFAULT_TO_ASYNC_MODE"
2215#endif
2216#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2217 " TARGET_DISABLE_TRIPLE_BUFFERING"
2218#endif
2219 "]";
2220 result.append(config);
2221}
2222
Mathias Agopian74d211a2013-04-22 16:55:35 +02002223void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2224 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002225{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002226 bool colorize = false;
2227 if (index < args.size()
2228 && (args[index] == String16("--color"))) {
2229 colorize = true;
2230 index++;
2231 }
2232
2233 Colorizer colorizer(colorize);
2234
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002235 // figure out if we're stuck somewhere
2236 const nsecs_t now = systemTime();
2237 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2238 const nsecs_t inTransaction(mDebugInTransaction);
2239 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2240 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2241
2242 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002243 * Dump library configuration.
2244 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002245
2246 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002247 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002248 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002249 appendSfConfigString(result);
2250 appendUiConfigString(result);
2251 appendGuiConfigString(result);
2252 result.append("\n");
2253
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002254 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002255 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002256 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002257 result.append(SyncFeatures::getInstance().toString());
2258 result.append("\n");
2259
Andy McFadden4803b742012-09-24 19:07:20 -07002260 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002261 * Dump the visible layer list
2262 */
2263 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2264 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002265 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002266 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002267 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002268 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002269 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002270 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002271 }
2272
2273 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002274 * Dump Display state
2275 */
2276
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002277 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002278 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002279 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002280 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2281 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002282 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002283 }
2284
2285 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002286 * Dump SurfaceFlinger global state
2287 */
2288
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002289 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002290 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002291 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002292
Mathias Agopian888c8222012-08-04 21:10:38 -07002293 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002294 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002295
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 colorizer.bold(result);
2297 result.appendFormat("EGL implementation : %s\n",
2298 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2299 colorizer.reset(result);
2300 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002301 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002302
Mathias Agopian875d8e12013-06-07 15:35:48 -07002303 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002304
Mathias Agopian42977342012-08-05 00:40:46 -07002305 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002306 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002307 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002308 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309 " last eglSwapBuffers() time: %f us\n"
2310 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002311 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312 " refresh-rate : %f fps\n"
2313 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002314 " y-dpi : %f\n"
2315 " EGL_NATIVE_VISUAL_ID : %d\n"
2316 " gpu_to_cpu_unsupported : %d\n"
2317 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318 mLastSwapBufferTime/1000.0,
2319 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002320 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002321 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2322 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002323 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2324 mEGLNativeVisualId,
2325 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326
Mathias Agopian74d211a2013-04-22 16:55:35 +02002327 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002329
Mathias Agopian74d211a2013-04-22 16:55:35 +02002330 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002331 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332
2333 /*
2334 * VSYNC state
2335 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002336 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002337
2338 /*
2339 * Dump HWComposer state
2340 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002341 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002342 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002343 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002344 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2346 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002348
2349 /*
2350 * Dump gralloc state
2351 */
2352 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2353 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354}
2355
Mathias Agopian13127d82013-03-05 17:47:11 -08002356const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002357SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002358 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002359 wp<IBinder> dpy;
2360 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2361 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2362 dpy = mDisplays.keyAt(i);
2363 break;
2364 }
2365 }
2366 if (dpy == NULL) {
2367 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2368 // Just use the primary display so we have something to return
2369 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2370 }
2371 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002372}
2373
Keun young Park63f165f2012-08-31 10:53:36 -07002374bool SurfaceFlinger::startDdmConnection()
2375{
2376 void* libddmconnection_dso =
2377 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2378 if (!libddmconnection_dso) {
2379 return false;
2380 }
2381 void (*DdmConnection_start)(const char* name);
2382 DdmConnection_start =
2383 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2384 if (!DdmConnection_start) {
2385 dlclose(libddmconnection_dso);
2386 return false;
2387 }
2388 (*DdmConnection_start)(getServiceName());
2389 return true;
2390}
2391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002392status_t SurfaceFlinger::onTransact(
2393 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2394{
2395 switch (code) {
2396 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002397 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002398 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002399 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002400 case BLANK:
2401 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002402 {
2403 // codes that require permission check
2404 IPCThreadState* ipc = IPCThreadState::self();
2405 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002406 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002407 if ((uid != AID_GRAPHICS) &&
2408 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002409 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002410 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2411 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002412 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002413 break;
2414 }
2415 case CAPTURE_SCREEN:
2416 {
2417 // codes that require permission check
2418 IPCThreadState* ipc = IPCThreadState::self();
2419 const int pid = ipc->getCallingPid();
2420 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002421 if ((uid != AID_GRAPHICS) &&
2422 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002423 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002424 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2425 return PERMISSION_DENIED;
2426 }
2427 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002428 }
2429 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002430
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2432 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002433 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002434 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002435 IPCThreadState* ipc = IPCThreadState::self();
2436 const int pid = ipc->getCallingPid();
2437 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002438 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002439 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440 return PERMISSION_DENIED;
2441 }
2442 int n;
2443 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002444 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002445 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002446 return NO_ERROR;
2447 case 1002: // SHOW_UPDATES
2448 n = data.readInt32();
2449 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002450 invalidateHwcGeometry();
2451 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002452 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002453 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002454 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002455 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002457 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002458 setTransactionFlags(
2459 eTransactionNeeded|
2460 eDisplayTransactionNeeded|
2461 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002462 return NO_ERROR;
2463 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002464 case 1006:{ // send empty update
2465 signalRefresh();
2466 return NO_ERROR;
2467 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002468 case 1008: // toggle use of hw composer
2469 n = data.readInt32();
2470 mDebugDisableHWC = n ? 1 : 0;
2471 invalidateHwcGeometry();
2472 repaintEverything();
2473 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002474 case 1009: // toggle use of transform hint
2475 n = data.readInt32();
2476 mDebugDisableTransformHint = n ? 1 : 0;
2477 invalidateHwcGeometry();
2478 repaintEverything();
2479 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002481 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 reply->writeInt32(0);
2483 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002484 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002485 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486 return NO_ERROR;
2487 case 1013: {
2488 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002489 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2490 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 }
2492 return NO_ERROR;
2493 }
2494 }
2495 return err;
2496}
2497
Mathias Agopian53331da2011-08-22 21:44:41 -07002498void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002499 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002500 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002501}
2502
Mathias Agopian59119e62010-10-11 12:37:43 -07002503// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002504// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002505// ---------------------------------------------------------------------------
2506
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002507status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2508 const sp<IGraphicBufferProducer>& producer,
2509 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002510 uint32_t minLayerZ, uint32_t maxLayerZ,
2511 bool isCpuConsumer) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002512
2513 if (CC_UNLIKELY(display == 0))
2514 return BAD_VALUE;
2515
2516 if (CC_UNLIKELY(producer == 0))
2517 return BAD_VALUE;
2518
2519 class MessageCaptureScreen : public MessageBase {
2520 SurfaceFlinger* flinger;
2521 sp<IBinder> display;
2522 sp<IGraphicBufferProducer> producer;
2523 uint32_t reqWidth, reqHeight;
2524 uint32_t minLayerZ,maxLayerZ;
Mathias Agopianabe815d2013-03-19 22:22:21 -07002525 bool isCpuConsumer;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002526 status_t result;
2527 public:
2528 MessageCaptureScreen(SurfaceFlinger* flinger,
2529 const sp<IBinder>& display,
2530 const sp<IGraphicBufferProducer>& producer,
2531 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002532 uint32_t minLayerZ, uint32_t maxLayerZ, bool isCpuConsumer)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002533 : flinger(flinger), display(display), producer(producer),
2534 reqWidth(reqWidth), reqHeight(reqHeight),
2535 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Mathias Agopianabe815d2013-03-19 22:22:21 -07002536 isCpuConsumer(isCpuConsumer),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002537 result(PERMISSION_DENIED)
2538 {
2539 }
2540 status_t getResult() const {
2541 return result;
2542 }
2543 virtual bool handler() {
2544 Mutex::Autolock _l(flinger->mStateLock);
2545 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002546 bool useReadPixels = isCpuConsumer && !flinger->mGpuToCpuSupported;
2547 result = flinger->captureScreenImplLocked(hw,
2548 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2549 useReadPixels);
Mathias Agopianb4b17302013-03-20 18:36:41 -07002550
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002551 return true;
2552 }
2553 };
2554
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002555 // make sure to process transactions before screenshots -- a transaction
2556 // might already be pending but scheduled for VSYNC; this guarantees we
2557 // will handle it before the screenshot. When VSYNC finally arrives
2558 // the scheduled transaction will be a no-op. If no transactions are
2559 // scheduled at this time, this will end-up being a no-op as well.
2560 mEventQueue.invalidateTransactionNow();
2561
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002562 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianabe815d2013-03-19 22:22:21 -07002563 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2564 isCpuConsumer);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002565 status_t res = postMessageSync(msg);
2566 if (res == NO_ERROR) {
2567 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2568 }
2569 return res;
2570}
2571
Mathias Agopian180f10d2013-04-10 22:55:41 -07002572
2573void SurfaceFlinger::renderScreenImplLocked(
2574 const sp<const DisplayDevice>& hw,
2575 uint32_t reqWidth, uint32_t reqHeight,
2576 uint32_t minLayerZ, uint32_t maxLayerZ,
2577 bool yswap)
2578{
2579 ATRACE_CALL();
2580
2581 // get screen geometry
2582 const uint32_t hw_w = hw->getWidth();
2583 const uint32_t hw_h = hw->getHeight();
2584
2585 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2586
2587 // make sure to clear all GL error flags
2588 while ( glGetError() != GL_NO_ERROR ) ;
2589
2590 // set-up our viewport
2591 glViewport(0, 0, reqWidth, reqHeight);
2592 glMatrixMode(GL_PROJECTION);
2593 glLoadIdentity();
2594 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2595 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2596 glMatrixMode(GL_MODELVIEW);
2597 glLoadIdentity();
2598
2599 // redraw the screen entirely...
2600 glDisable(GL_SCISSOR_TEST);
2601 glClearColor(0,0,0,1);
2602 glClear(GL_COLOR_BUFFER_BIT);
2603 glDisable(GL_TEXTURE_EXTERNAL_OES);
2604 glDisable(GL_TEXTURE_2D);
2605
2606 const LayerVector& layers( mDrawingState.layersSortedByZ );
2607 const size_t count = layers.size();
2608 for (size_t i=0 ; i<count ; ++i) {
2609 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002610 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002611 if (state.layerStack == hw->getLayerStack()) {
2612 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2613 if (layer->isVisible()) {
2614 if (filtering) layer->setFiltering(true);
2615 layer->draw(hw);
2616 if (filtering) layer->setFiltering(false);
2617 }
2618 }
2619 }
2620 }
2621
2622 // compositionComplete is needed for older driver
2623 hw->compositionComplete();
2624}
2625
2626
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002627status_t SurfaceFlinger::captureScreenImplLocked(
2628 const sp<const DisplayDevice>& hw,
2629 const sp<IGraphicBufferProducer>& producer,
2630 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002631 uint32_t minLayerZ, uint32_t maxLayerZ,
2632 bool useReadPixels)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002633{
2634 ATRACE_CALL();
2635
Mathias Agopian180f10d2013-04-10 22:55:41 -07002636 // get screen geometry
2637 const uint32_t hw_w = hw->getWidth();
2638 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002639
Mathias Agopian180f10d2013-04-10 22:55:41 -07002640 // if we have secure windows on this display, never allow the screen capture
2641 if (hw->getSecureLayerVisible()) {
2642 ALOGW("FB is protected: PERMISSION_DENIED");
2643 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002644 }
2645
Mathias Agopian180f10d2013-04-10 22:55:41 -07002646 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2647 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2648 reqWidth, reqHeight, hw_w, hw_h);
2649 return BAD_VALUE;
2650 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002651
Mathias Agopian180f10d2013-04-10 22:55:41 -07002652 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2653 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2654
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002655 // create a surface (because we're a producer, and we need to
2656 // dequeue/queue a buffer)
2657 sp<Surface> sur = new Surface(producer);
2658 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002659
2660 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002661 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
2662 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
2663 if (!useReadPixels) {
2664 usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002665 }
2666
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002667 int err = 0;
2668 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2669 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2670 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002671
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002672 if (err == NO_ERROR) {
2673 ANativeWindowBuffer* buffer;
2674 /* TODO: Once we have the sync framework everywhere this can use
2675 * server-side waits on the fence that dequeueBuffer returns.
2676 */
2677 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2678 if (result == NO_ERROR) {
2679 // create an EGLImage from the buffer so we can later
2680 // turn it into a texture
2681 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2682 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2683 if (image != EGL_NO_IMAGE_KHR) {
2684 GLuint tname, name;
2685 if (!useReadPixels) {
2686 // turn our EGLImage into a texture
2687 glGenTextures(1, &tname);
2688 glBindTexture(GL_TEXTURE_2D, tname);
2689 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2690 // create a Framebuffer Object to render into
2691 glGenFramebuffersOES(1, &name);
2692 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2693 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
Mathias Agopian875d8e12013-06-07 15:35:48 -07002694 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002695 } else {
2696 // since we're going to use glReadPixels() anyways,
2697 // use an intermediate renderbuffer instead
2698 glGenRenderbuffersOES(1, &tname);
2699 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2700 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, reqWidth, reqHeight);
2701 // create a FBO to render into
2702 glGenFramebuffersOES(1, &name);
2703 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2704 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2705 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2706 }
2707
2708 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2709 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2710 // this will in fact render into our dequeued buffer
2711 // via an FBO, which means we didn't have to create
2712 // an EGLSurface and therefore we're not
2713 // dependent on the context's EGLConfig.
2714 renderScreenImplLocked(hw, reqWidth, reqHeight,
2715 minLayerZ, maxLayerZ, true);
2716
2717 if (useReadPixels) {
2718 sp<GraphicBuffer> buf = static_cast<GraphicBuffer*>(buffer);
2719 void* vaddr;
2720 if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
2721 glReadPixels(0, 0, buffer->stride, reqHeight,
2722 GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
Mathias Agopianfee2b462013-07-03 12:34:01 -07002723 checkScreenshot(buf, vaddr, hw, minLayerZ, maxLayerZ);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002724 buf->unlock();
2725 }
2726 }
2727 } else {
2728 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2729 result = INVALID_OPERATION;
2730 }
2731
2732 // back to main framebuffer
2733 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2734 glDeleteFramebuffersOES(1, &name);
2735 if (!useReadPixels) {
2736 glDeleteTextures(1, &tname);
2737 } else {
2738 glDeleteRenderbuffersOES(1, &tname);
2739 }
2740 // destroy our image
2741 eglDestroyImageKHR(mEGLDisplay, image);
2742 } else {
2743 result = BAD_VALUE;
2744 }
2745 window->queueBuffer(window, buffer, -1);
2746 }
2747 } else {
2748 result = BAD_VALUE;
2749 }
2750 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2751 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002752
Mathias Agopian875d8e12013-06-07 15:35:48 -07002753 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002754
Mathias Agopian74c40c02010-09-29 13:02:36 -07002755 return result;
2756}
2757
Mathias Agopianfee2b462013-07-03 12:34:01 -07002758void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2759 const sp<const DisplayDevice>& hw,
2760 uint32_t minLayerZ, uint32_t maxLayerZ) {
2761 if (DEBUG_SCREENSHOTS) {
2762 for (ssize_t y=0 ; y<buf->height ; y++) {
2763 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2764 for (ssize_t x=0 ; x<buf->width ; x++) {
2765 if (p[x] != 0xFF000000) return;
2766 }
2767 }
2768 ALOGE("*** we just took a black screenshot ***\n"
2769 "requested minz=%d, maxz=%d, layerStack=%d",
2770 minLayerZ, maxLayerZ, hw->getLayerStack());
2771 const LayerVector& layers( mDrawingState.layersSortedByZ );
2772 const size_t count = layers.size();
2773 for (size_t i=0 ; i<count ; ++i) {
2774 const sp<Layer>& layer(layers[i]);
2775 const Layer::State& state(layer->getDrawingState());
2776 const bool visible = (state.layerStack == hw->getLayerStack())
2777 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2778 && (layer->isVisible());
2779 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2780 visible ? '+' : '-',
2781 i, layer->getName().string(), state.layerStack, state.z,
2782 layer->isVisible(), state.flags, state.alpha);
2783 }
2784 }
2785}
2786
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002787// ---------------------------------------------------------------------------
2788
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002789SurfaceFlinger::LayerVector::LayerVector() {
2790}
2791
2792SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002793 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002794}
2795
2796int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2797 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002798{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002799 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002800 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2801 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002802
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002803 uint32_t ls = l->getCurrentState().layerStack;
2804 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002805 if (ls != rs)
2806 return ls - rs;
2807
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002808 uint32_t lz = l->getCurrentState().z;
2809 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002810 if (lz != rz)
2811 return lz - rz;
2812
2813 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002814}
2815
2816// ---------------------------------------------------------------------------
2817
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002818SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2819 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002820}
2821
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002822SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002823 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002824 viewport.makeInvalid();
2825 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002826}
2827
2828// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002830}; // namespace android