blob: 68b9950c8b4e82b32aaaee29415c48c418aac793 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070068#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopian875d8e12013-06-07 15:35:48 -070071#include "RenderEngine/RenderEngine.h"
72
Mathias Agopian3f844832013-08-07 21:24:32 -070073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
Mathias Agopianfee2b462013-07-03 12:34:01 -070076/*
77 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
78 * black pixels.
79 */
80#define DEBUG_SCREENSHOTS false
81
Mathias Agopianca088332013-03-28 17:44:13 -070082EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085// ---------------------------------------------------------------------------
86
Mathias Agopian99b49842011-06-27 16:05:52 -070087const String16 sHardwareTest("android.permission.HARDWARE_TEST");
88const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
89const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
90const String16 sDump("android.permission.DUMP");
91
92// ---------------------------------------------------------------------------
93
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094SurfaceFlinger::SurfaceFlinger()
95 : BnSurfaceComposer(), Thread(false),
96 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070097 mTransactionPending(false),
98 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700100 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700101 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700104 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800105 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700108 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700109 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700110 mDebugInSwapBuffers(0),
111 mLastSwapBufferTime(0),
112 mDebugInTransaction(0),
113 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700114 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
Steve Blocka19954a2012-01-04 20:05:49 +0000116 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
118 // debugging stuff...
119 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120
Mathias Agopianb4b17302013-03-20 18:36:41 -0700121 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700122 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 property_get("debug.sf.showupdates", value, "0");
125 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700127 property_get("debug.sf.ddms", value, "0");
128 mDebugDDMS = atoi(value);
129 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700130 if (!startDdmConnection()) {
131 // start failed, and DDMS debugging not enabled
132 mDebugDDMS = 0;
133 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700134 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700135 ALOGI_IF(mDebugRegion, "showupdates enabled");
136 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800139void SurfaceFlinger::onFirstRef()
140{
141 mEventQueue.init(this);
142
143 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
144
145 // Wait for the main thread to be done with its initialization
146 mReadyToRunBarrier.wait();
147}
148
149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150SurfaceFlinger::~SurfaceFlinger()
151{
Mathias Agopiana4912602012-07-12 14:25:33 -0700152 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
153 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
154 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155}
156
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800157void SurfaceFlinger::binderDied(const wp<IBinder>& who)
158{
159 // the window manager died on us. prepare its eulogy.
160
Andy McFadden13a082e2012-08-24 10:16:42 -0700161 // restore initial conditions (default device unblank, etc)
162 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800163
164 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700165 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800166}
167
Mathias Agopian7e27f052010-05-28 14:22:23 -0700168sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169{
Mathias Agopian96f08192010-06-02 23:28:45 -0700170 sp<ISurfaceComposerClient> bclient;
171 sp<Client> client(new Client(this));
172 status_t err = client->initCheck();
173 if (err == NO_ERROR) {
174 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 return bclient;
177}
178
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700179sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
180 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700181{
182 class DisplayToken : public BBinder {
183 sp<SurfaceFlinger> flinger;
184 virtual ~DisplayToken() {
185 // no more references, this display must be terminated
186 Mutex::Autolock _l(flinger->mStateLock);
187 flinger->mCurrentState.displays.removeItem(this);
188 flinger->setTransactionFlags(eDisplayTransactionNeeded);
189 }
190 public:
191 DisplayToken(const sp<SurfaceFlinger>& flinger)
192 : flinger(flinger) {
193 }
194 };
195
196 sp<BBinder> token = new DisplayToken(this);
197
198 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700199 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700200 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700201 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700202 mCurrentState.displays.add(token, info);
203
204 return token;
205}
206
Jesse Hall6c913be2013-08-08 12:15:49 -0700207void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
208 Mutex::Autolock _l(mStateLock);
209
210 ssize_t idx = mCurrentState.displays.indexOfKey(display);
211 if (idx < 0) {
212 ALOGW("destroyDisplay: invalid display token");
213 return;
214 }
215
216 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
217 if (!info.isVirtualDisplay()) {
218 ALOGE("destroyDisplay called for non-virtual display");
219 return;
220 }
221
222 mCurrentState.displays.removeItemsAt(idx);
223 setTransactionFlags(eDisplayTransactionNeeded);
224}
225
Jesse Hall692c7232012-11-08 15:41:56 -0800226void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
227 ALOGW_IF(mBuiltinDisplays[type],
228 "Overwriting display token for display type %d", type);
229 mBuiltinDisplays[type] = new BBinder();
230 DisplayDeviceState info(type);
231 // All non-virtual displays are currently considered secure.
232 info.isSecure = true;
233 mCurrentState.displays.add(mBuiltinDisplays[type], info);
234}
235
Mathias Agopiane57f2922012-08-09 16:29:12 -0700236sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700237 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700238 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
239 return NULL;
240 }
Jesse Hall692c7232012-11-08 15:41:56 -0800241 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700242}
243
Jamie Gennis9a78c902011-01-12 18:30:40 -0800244sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
245{
246 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
247 return gba;
248}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250void SurfaceFlinger::bootFinished()
251{
252 const nsecs_t now = systemTime();
253 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000254 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700255 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700256
257 // wait patiently for the window manager death
258 const String16 name("window");
259 sp<IBinder> window(defaultServiceManager()->getService(name));
260 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700261 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700262 }
263
264 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700265 // formerly we would just kill the process, but we now ask it to exit so it
266 // can choose where to stop the animation.
267 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268}
269
Mathias Agopian3f844832013-08-07 21:24:32 -0700270void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700271 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700272 RenderEngine& engine;
273 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700274 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700275 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
276 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700277 }
278 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700279 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700280 return true;
281 }
282 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700283 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700284}
285
Mathias Agopian722b98f2012-09-25 18:24:31 -0700286status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 EGLDisplay dpy,
288 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700289 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 EGLConfig* outConfig)
291{
292 EGLConfig config = NULL;
293 EGLint numConfigs = -1, n=0;
294 eglGetConfigs(dpy, NULL, 0, &numConfigs);
295 EGLConfig* const configs = new EGLConfig[numConfigs];
296 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700297
Mathias Agopian722b98f2012-09-25 18:24:31 -0700298 if (n) {
299 if (attribute != EGL_NONE) {
300 for (int i=0 ; i<n ; i++) {
301 EGLint value = 0;
302 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
303 if (wanted == value) {
304 *outConfig = configs[i];
305 delete [] configs;
306 return NO_ERROR;
307 }
308 }
309 } else {
310 // just pick the first one
311 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700312 delete [] configs;
313 return NO_ERROR;
314 }
315 }
316 delete [] configs;
317 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318}
319
Mathias Agopian722b98f2012-09-25 18:24:31 -0700320class EGLAttributeVector {
321 struct Attribute;
322 class Adder;
323 friend class Adder;
324 KeyedVector<Attribute, EGLint> mList;
325 struct Attribute {
326 Attribute() {};
327 Attribute(EGLint v) : v(v) { }
328 EGLint v;
329 bool operator < (const Attribute& other) const {
330 // this places EGL_NONE at the end
331 EGLint lhs(v);
332 EGLint rhs(other.v);
333 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
334 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
335 return lhs < rhs;
336 }
337 };
338 class Adder {
339 friend class EGLAttributeVector;
340 EGLAttributeVector& v;
341 EGLint attribute;
342 Adder(EGLAttributeVector& v, EGLint attribute)
343 : v(v), attribute(attribute) {
344 }
345 public:
346 void operator = (EGLint value) {
347 if (attribute != EGL_NONE) {
348 v.mList.add(attribute, value);
349 }
350 }
351 operator EGLint () const { return v.mList[attribute]; }
352 };
353public:
354 EGLAttributeVector() {
355 mList.add(EGL_NONE, EGL_NONE);
356 }
357 void remove(EGLint attribute) {
358 if (attribute != EGL_NONE) {
359 mList.removeItem(attribute);
360 }
361 }
362 Adder operator [] (EGLint attribute) {
363 return Adder(*this, attribute);
364 }
365 EGLint operator [] (EGLint attribute) const {
366 return mList[attribute];
367 }
368 // cast-operator to (EGLint const*)
369 operator EGLint const* () const { return &mList.keyAt(0).v; }
370};
371
Mathias Agopiana4912602012-07-12 14:25:33 -0700372EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
373 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
374 // it is to be used with WIFI displays
375 EGLConfig config;
376 EGLint dummy;
377 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700378
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700380 // TODO: enable ES2
381 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
382 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700383 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
384 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
385 attribs[EGL_RED_SIZE] = 8;
386 attribs[EGL_GREEN_SIZE] = 8;
387 attribs[EGL_BLUE_SIZE] = 8;
388
389 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
390 if (!err)
391 goto success;
392
Mathias Agopian875d8e12013-06-07 15:35:48 -0700393 // this didn't work, probably because we're on the emulator...
394 // try a simplified query
395 ALOGW("no suitable EGLConfig found, trying a simpler query");
396 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700397 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700398 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700399 attribs.remove(EGL_RED_SIZE);
400 attribs.remove(EGL_GREEN_SIZE);
401 attribs.remove(EGL_BLUE_SIZE);
402 err = selectConfigForAttribute(display, attribs,
403 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700404 if (!err)
405 goto success;
406
407 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700408 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700409 return 0;
410
411success:
412 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700414 return config;
415}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416
Mathias Agopiana4912602012-07-12 14:25:33 -0700417
Mathias Agopiana4912602012-07-12 14:25:33 -0700418status_t SurfaceFlinger::readyToRun()
419{
420 ALOGI( "SurfaceFlinger's main thread ready to run. "
421 "Initializing graphics H/W...");
422
Jesse Hall692c7232012-11-08 15:41:56 -0800423 Mutex::Autolock _l(mStateLock);
424
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700425 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700426 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
427 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700428
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700429 // Initialize the H/W composer object. There may or may not be an
430 // actual hardware composer underneath.
431 mHwc = new HWComposer(this,
432 *static_cast<HWComposer::EventHandler *>(this));
433
Mathias Agopian875d8e12013-06-07 15:35:48 -0700434 // initialize the config and context (can't fail)
435 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
436
437 // print some debugging info
438 EGLint r,g,b,a;
439 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
440 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
441 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
442 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
443 ALOGI("EGL informations:");
444 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
445 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
446 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
447 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
448 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
449
450 // get a RenderEngine for the given display / config (can't fail)
451 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
452
453 // retrieve the EGL context that was selected/created
454 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700455
Mathias Agopianed985572013-03-22 00:24:39 -0700456 // figure out which format we got
457 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
458 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
459
Mathias Agopianda27af92012-09-13 18:17:13 -0700460 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
461 "couldn't create EGLContext");
462
Mathias Agopiancde87a32012-09-13 14:09:01 -0700463 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700464 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700465 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700466 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700467 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700468 // All non-virtual displays are currently considered secure.
469 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800470 createBuiltinDisplayLocked(type);
471 wp<IBinder> token = mBuiltinDisplays[i];
472
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700473 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
474 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700475 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700476 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700477 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700478 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700479 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700480 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700481 // for displays other than the main display, so we always
482 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700483 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700484 hw->acquireScreen();
485 }
486 mDisplays.add(token, hw);
487 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700488 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700489
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700490 // make the GLContext current so that we can create textures when creating Layers
491 // (which may happens before we render something)
492 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
493
Mathias Agopian028508c2012-07-25 21:12:12 -0700494 // start the EventThread
495 mEventThread = new EventThread(this);
496 mEventQueue.setEventThread(mEventThread);
497
Mathias Agopian92a979a2012-08-02 18:32:23 -0700498 // initialize our drawing state
499 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700500
Mathias Agopiana4912602012-07-12 14:25:33 -0700501 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800502 mReadyToRunBarrier.open();
503
Andy McFadden13a082e2012-08-24 10:16:42 -0700504 // set initial conditions (e.g. unblank default device)
505 initializeDisplays();
506
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700507 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700508 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 return NO_ERROR;
511}
512
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700513int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
514 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
515 type : mHwc->allocateDisplayId();
516}
517
Mathias Agopiana67e4182012-06-19 17:26:12 -0700518void SurfaceFlinger::startBootAnim() {
519 // start boot animation
520 property_set("service.bootanim.exit", "0");
521 property_set("ctl.start", "bootanim");
522}
523
Mathias Agopian875d8e12013-06-07 15:35:48 -0700524size_t SurfaceFlinger::getMaxTextureSize() const {
525 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700526}
527
Mathias Agopian875d8e12013-06-07 15:35:48 -0700528size_t SurfaceFlinger::getMaxViewportDims() const {
529 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700530}
531
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800533
Jamie Gennis582270d2011-08-17 18:19:00 -0700534bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800535 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800536 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800537 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700538 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800539}
540
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700541status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800542 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700543 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800544 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700545 type = i;
546 break;
547 }
548 }
549
550 if (type < 0) {
551 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700552 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700553
554 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700555 float xdpi = hwc.getDpiX(type);
556 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700557
558 // TODO: Not sure if display density should handled by SF any longer
559 class Density {
560 static int getDensityFromProperty(char const* propName) {
561 char property[PROPERTY_VALUE_MAX];
562 int density = 0;
563 if (property_get(propName, property, NULL) > 0) {
564 density = atoi(property);
565 }
566 return density;
567 }
568 public:
569 static int getEmuDensity() {
570 return getDensityFromProperty("qemu.sf.lcd_density"); }
571 static int getBuildDensity() {
572 return getDensityFromProperty("ro.sf.lcd_density"); }
573 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700574
575 if (type == DisplayDevice::DISPLAY_PRIMARY) {
576 // The density of the device is provided by a build property
577 float density = Density::getBuildDensity() / 160.0f;
578 if (density == 0) {
579 // the build doesn't provide a density -- this is wrong!
580 // use xdpi instead
581 ALOGE("ro.sf.lcd_density must be defined as a build property");
582 density = xdpi / 160.0f;
583 }
584 if (Density::getEmuDensity()) {
585 // if "qemu.sf.lcd_density" is specified, it overrides everything
586 xdpi = ydpi = density = Density::getEmuDensity();
587 density /= 160.0f;
588 }
589 info->density = density;
590
591 // TODO: this needs to go away (currently needed only by webkit)
592 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
593 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700594 } else {
595 // TODO: where should this value come from?
596 static const int TV_DENSITY = 213;
597 info->density = TV_DENSITY / 160.0f;
598 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700599 }
600
Mathias Agopian1604f772012-09-18 21:54:42 -0700601 info->w = hwc.getWidth(type);
602 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700603 info->xdpi = xdpi;
604 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700605 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700606
607 // All non-virtual displays are currently considered secure.
608 info->secure = true;
609
Mathias Agopian888c8222012-08-04 21:10:38 -0700610 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700611}
612
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800613// ----------------------------------------------------------------------------
614
615sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800616 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700617}
618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800620
621void SurfaceFlinger::waitForEvent() {
622 mEventQueue.waitMessage();
623}
624
625void SurfaceFlinger::signalTransaction() {
626 mEventQueue.invalidate();
627}
628
629void SurfaceFlinger::signalLayerUpdate() {
630 mEventQueue.invalidate();
631}
632
633void SurfaceFlinger::signalRefresh() {
634 mEventQueue.refresh();
635}
636
637status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
638 nsecs_t reltime, uint32_t flags) {
639 return mEventQueue.postMessage(msg, reltime);
640}
641
642status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
643 nsecs_t reltime, uint32_t flags) {
644 status_t res = mEventQueue.postMessage(msg, reltime);
645 if (res == NO_ERROR) {
646 msg->wait();
647 }
648 return res;
649}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 return true;
654}
655
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700656void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700657 if (mEventThread == NULL) {
658 // This is a temporary workaround for b/7145521. A non-null pointer
659 // does not mean EventThread has finished initializing, so this
660 // is not a correct fix.
661 ALOGW("WARNING: EventThread not started, ignoring vsync");
662 return;
663 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700664 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
665 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700666 mEventThread->onVSyncReceived(type, timestamp);
667 }
668}
669
670void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
671 if (mEventThread == NULL) {
672 // This is a temporary workaround for b/7145521. A non-null pointer
673 // does not mean EventThread has finished initializing, so this
674 // is not a correct fix.
675 ALOGW("WARNING: EventThread not started, ignoring hotplug");
676 return;
677 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700678
Mathias Agopian148994e2012-09-19 17:31:36 -0700679 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700680 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800681 if (connected) {
682 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700683 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800684 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
685 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700686 }
687 setTransactionFlags(eDisplayTransactionNeeded);
688
Andy McFadden9e9689c2012-10-10 18:17:51 -0700689 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700690 }
Mathias Agopian86303202012-07-24 22:46:10 -0700691}
692
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700693void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
694 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700695}
696
Mathias Agopian4fec8732012-06-29 14:12:52 -0700697void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800698 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800699 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700700 case MessageQueue::TRANSACTION:
701 handleMessageTransaction();
702 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700703 case MessageQueue::INVALIDATE:
704 handleMessageTransaction();
705 handleMessageInvalidate();
706 signalRefresh();
707 break;
708 case MessageQueue::REFRESH:
709 handleMessageRefresh();
710 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800711 }
712}
713
Mathias Agopian4fec8732012-06-29 14:12:52 -0700714void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700715 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700716 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700717 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700718 }
719}
720
721void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700722 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700723 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724}
725
726void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700727 ATRACE_CALL();
728 preComposition();
729 rebuildLayerStacks();
730 setUpHWComposer();
731 doDebugFlashRegions();
732 doComposition();
733 postComposition();
734}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700735
Mathias Agopiancd60f992012-08-16 16:28:27 -0700736void SurfaceFlinger::doDebugFlashRegions()
737{
738 // is debugging enabled
739 if (CC_LIKELY(!mDebugRegion))
740 return;
741
742 const bool repaintEverything = mRepaintEverything;
743 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
744 const sp<DisplayDevice>& hw(mDisplays[dpy]);
745 if (hw->canDraw()) {
746 // transform the dirty region into this screen's coordinate space
747 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
748 if (!dirtyRegion.isEmpty()) {
749 // redraw the whole screen
750 doComposeSurfaces(hw, Region(hw->bounds()));
751
752 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700753 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700754 RenderEngine& engine(getRenderEngine());
755 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
756
Mathias Agopiancd60f992012-08-16 16:28:27 -0700757 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700758 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700759 }
760 }
761 }
762
763 postFramebuffer();
764
765 if (mDebugRegion > 1) {
766 usleep(mDebugRegion * 1000);
767 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700768
769 HWComposer& hwc(getHwComposer());
770 if (hwc.initCheck() == NO_ERROR) {
771 status_t err = hwc.prepare();
772 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
773 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700774}
775
776void SurfaceFlinger::preComposition()
777{
778 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700779 const LayerVector& layers(mDrawingState.layersSortedByZ);
780 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700781 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700782 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700783 needExtraInvalidate = true;
784 }
785 }
786 if (needExtraInvalidate) {
787 signalLayerUpdate();
788 }
789}
790
791void SurfaceFlinger::postComposition()
792{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700793 const LayerVector& layers(mDrawingState.layersSortedByZ);
794 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700796 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700797 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800798
799 if (mAnimCompositionPending) {
800 mAnimCompositionPending = false;
801
802 const HWComposer& hwc = getHwComposer();
803 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800804 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800805 mAnimFrameTracker.setActualPresentFence(presentFence);
806 } else {
807 // The HWC doesn't support present fences, so use the refresh
808 // timestamp instead.
809 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
810 mAnimFrameTracker.setActualPresentTime(presentTime);
811 }
812 mAnimFrameTracker.advanceFrame();
813 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700814}
815
816void SurfaceFlinger::rebuildLayerStacks() {
817 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700818 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700819 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700820 mVisibleRegionsDirty = false;
821 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700822
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700823 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700824 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700825 Region opaqueRegion;
826 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700828 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700829 const Transform& tr(hw->getTransform());
830 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700831 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700832 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700833 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700834
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700835 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700837 const sp<Layer>& layer(layers[i]);
838 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700840 Region drawRegion(tr.transform(
841 layer->visibleNonTransparentRegion));
842 drawRegion.andSelf(bounds);
843 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700844 layersSortedByZ.add(layer);
845 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700846 }
847 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700848 }
Mathias Agopian42977342012-08-05 00:40:46 -0700849 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700850 hw->undefinedRegion.set(bounds);
851 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
852 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700853 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700854 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700855}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700856
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700858 HWComposer& hwc(getHwComposer());
859 if (hwc.initCheck() == NO_ERROR) {
860 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700861 if (CC_UNLIKELY(mHwWorkListDirty)) {
862 mHwWorkListDirty = false;
863 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
864 sp<const DisplayDevice> hw(mDisplays[dpy]);
865 const int32_t id = hw->getHwcDisplayId();
866 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800867 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700868 hw->getVisibleLayersSortedByZ());
869 const size_t count = currentLayers.size();
870 if (hwc.createWorkList(id, count) == NO_ERROR) {
871 HWComposer::LayerListIterator cur = hwc.begin(id);
872 const HWComposer::LayerListIterator end = hwc.end(id);
873 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700875 layer->setGeometry(hw, *cur);
876 if (mDebugDisableHWC || mDebugRegion) {
877 cur->setSkip(true);
878 }
879 }
880 }
881 }
882 }
883 }
884
885 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700886 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700887 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700888 const int32_t id = hw->getHwcDisplayId();
889 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700891 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700892 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700893 HWComposer::LayerListIterator cur = hwc.begin(id);
894 const HWComposer::LayerListIterator end = hwc.end(id);
895 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
896 /*
897 * update the per-frame h/w composer data for each layer
898 * and build the transparent region of the FB
899 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800900 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700901 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700902 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700903 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700904 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700905
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700906 status_t err = hwc.prepare();
907 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700908
909 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
910 sp<const DisplayDevice> hw(mDisplays[dpy]);
911 hw->prepareFrame(hwc);
912 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700913 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700914}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700915
Mathias Agopiancd60f992012-08-16 16:28:27 -0700916void SurfaceFlinger::doComposition() {
917 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700918 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700919 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700920 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921 if (hw->canDraw()) {
922 // transform the dirty region into this screen's coordinate space
923 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800924
925 // repaint the framebuffer (if needed)
926 doDisplayComposition(hw, dirtyRegion);
927
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928 hw->dirtyRegion.clear();
929 hw->flip(hw->swapRegion);
930 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700931 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700932 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700933 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700934 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700935 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700936}
937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938void SurfaceFlinger::postFramebuffer()
939{
Mathias Agopian841cde52012-03-01 15:44:37 -0800940 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800941
Mathias Agopiana44b0412011-10-16 18:46:35 -0700942 const nsecs_t now = systemTime();
943 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700944
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700946 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700947 if (!hwc.supportsFramebufferTarget()) {
948 // EGL spec says:
949 // "surface must be bound to the calling thread's current context,
950 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700951 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700952 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700953 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700954 }
955
Mathias Agopian92a979a2012-08-02 18:32:23 -0700956 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700957 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800958 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700959 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700960 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700961 int32_t id = hw->getHwcDisplayId();
962 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700963 HWComposer::LayerListIterator cur = hwc.begin(id);
964 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700966 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700967 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700968 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700969 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700970 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700971 }
Jesse Hallef194142012-06-14 14:45:17 -0700972 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800973 }
974
Mathias Agopiana44b0412011-10-16 18:46:35 -0700975 mLastSwapBufferTime = systemTime() - now;
976 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700977
978 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
979 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
980 logFrameStats();
981 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982}
983
Mathias Agopian87baae12012-07-31 12:38:26 -0700984void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985{
Mathias Agopian841cde52012-03-01 15:44:37 -0800986 ATRACE_CALL();
987
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700988 // here we keep a copy of the drawing state (that is the state that's
989 // going to be overwritten by handleTransactionLocked()) outside of
990 // mStateLock so that the side-effects of the State assignment
991 // don't happen with mStateLock held (which can cause deadlocks).
992 State drawingState(mDrawingState);
993
Mathias Agopianca4d3602011-05-19 15:38:14 -0700994 Mutex::Autolock _l(mStateLock);
995 const nsecs_t now = systemTime();
996 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997
Mathias Agopianca4d3602011-05-19 15:38:14 -0700998 // Here we're guaranteed that some transaction flags are set
999 // so we can call handleTransactionLocked() unconditionally.
1000 // We call getTransactionFlags(), which will also clear the flags,
1001 // with mStateLock held to guarantee that mCurrentState won't change
1002 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001003
Mathias Agopiane57f2922012-08-09 16:29:12 -07001004 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001005 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001006
Mathias Agopianca4d3602011-05-19 15:38:14 -07001007 mLastTransactionTime = systemTime() - now;
1008 mDebugInTransaction = 0;
1009 invalidateHwcGeometry();
1010 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001011}
1012
Mathias Agopian87baae12012-07-31 12:38:26 -07001013void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001014{
1015 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 const size_t count = currentLayers.size();
1017
1018 /*
1019 * Traversal of the children
1020 * (perform the transaction for each of them if needed)
1021 */
1022
Mathias Agopian3559b072012-08-15 13:46:03 -07001023 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001025 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1027 if (!trFlags) continue;
1028
1029 const uint32_t flags = layer->doTransaction(0);
1030 if (flags & Layer::eVisibleRegion)
1031 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 }
1033 }
1034
1035 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001036 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 */
1038
Mathias Agopiane57f2922012-08-09 16:29:12 -07001039 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001040 // here we take advantage of Vector's copy-on-write semantics to
1041 // improve performance by skipping the transaction entirely when
1042 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001043 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1044 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001045 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001047 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001048 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001049
1050 // find the displays that were removed
1051 // (ie: in drawing state but not in current state)
1052 // also handle displays that changed
1053 // (ie: displays that are in both lists)
1054 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001055 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1056 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001057 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001058 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001059 // Call makeCurrent() on the primary display so we can
1060 // be sure that nothing associated with this display
1061 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001062 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001063 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001064 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1065 if (hw != NULL)
1066 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001067 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1068 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001069 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001070 } else {
1071 ALOGW("trying to remove the main display");
1072 }
1073 } else {
1074 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001075 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001076 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001077 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001078 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001079 // recreating the DisplayDevice, so we just remove it
1080 // from the drawing state, so that it get re-added
1081 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001082 sp<DisplayDevice> hw(getDisplayDevice(display));
1083 if (hw != NULL)
1084 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001085 mDisplays.removeItem(display);
1086 mDrawingState.displays.removeItemsAt(i);
1087 dc--; i--;
1088 // at this point we must loop to the next item
1089 continue;
1090 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001092 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001093 if (disp != NULL) {
1094 if (state.layerStack != draw[i].layerStack) {
1095 disp->setLayerStack(state.layerStack);
1096 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001097 if ((state.orientation != draw[i].orientation)
1098 || (state.viewport != draw[i].viewport)
1099 || (state.frame != draw[i].frame))
1100 {
1101 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001102 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001103 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001104 }
1105 }
1106 }
1107
1108 // find displays that were added
1109 // (ie: in current state but not in drawing state)
1110 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001112 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001113
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001114 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001115 sp<IGraphicBufferProducer> producer;
1116 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1117
Jesse Hall02d86562013-03-25 14:43:23 -07001118 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001119 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001120 // Virtual displays without a surface are dormant:
1121 // they have external state (layer stack, projection,
1122 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001123 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001124
Jesse Hall02d86562013-03-25 14:43:23 -07001125 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001126 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1127 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001128 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001129
1130 dispSurface = vds;
1131 if (hwcDisplayId >= 0) {
1132 producer = vds;
1133 } else {
1134 // There won't be any interaction with HWC for this virtual display,
1135 // so the GLES driver can pass buffers directly to the sink.
1136 producer = state.surface;
1137 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001138 }
1139 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001140 ALOGE_IF(state.surface!=NULL,
1141 "adding a supported display, but rendering "
1142 "surface is provided (%p), ignoring it",
1143 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001144 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001145 // for supported (by hwc) displays we provide our
1146 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001147 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1148 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001149 }
1150
1151 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001152 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001153 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001154 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001155 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001156 hw->setLayerStack(state.layerStack);
1157 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001158 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001159 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001160 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001161 if (state.isVirtualDisplay()) {
1162 if (hwcDisplayId >= 0) {
1163 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1164 hw->getWidth(), hw->getHeight(),
1165 hw->getFormat());
1166 }
1167 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001168 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001169 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001170 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001171 }
1172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001174 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175
Mathias Agopian84300952012-11-21 16:02:13 -08001176 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1177 // The transform hint might have changed for some layers
1178 // (either because a display has changed, or because a layer
1179 // as changed).
1180 //
1181 // Walk through all the layers in currentLayers,
1182 // and update their transform hint.
1183 //
1184 // If a layer is visible only on a single display, then that
1185 // display is used to calculate the hint, otherwise we use the
1186 // default display.
1187 //
1188 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1189 // the hint is set before we acquire a buffer from the surface texture.
1190 //
1191 // NOTE: layer transactions have taken place already, so we use their
1192 // drawing state. However, SurfaceFlinger's own transaction has not
1193 // happened yet, so we must use the current state layer list
1194 // (soon to become the drawing state list).
1195 //
1196 sp<const DisplayDevice> disp;
1197 uint32_t currentlayerStack = 0;
1198 for (size_t i=0; i<count; i++) {
1199 // NOTE: we rely on the fact that layers are sorted by
1200 // layerStack first (so we don't have to traverse the list
1201 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001202 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001203 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001204 if (i==0 || currentlayerStack != layerStack) {
1205 currentlayerStack = layerStack;
1206 // figure out if this layerstack is mirrored
1207 // (more than one display) if so, pick the default display,
1208 // if not, pick the only display it's on.
1209 disp.clear();
1210 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1211 sp<const DisplayDevice> hw(mDisplays[dpy]);
1212 if (hw->getLayerStack() == currentlayerStack) {
1213 if (disp == NULL) {
1214 disp = hw;
1215 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001216 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001217 break;
1218 }
1219 }
1220 }
1221 }
Chet Haase91d25932013-04-11 15:24:55 -07001222 if (disp == NULL) {
1223 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1224 // redraw after transform hint changes. See bug 8508397.
1225
1226 // could be null when this layer is using a layerStack
1227 // that is not visible on any display. Also can occur at
1228 // screen off/on times.
1229 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001230 }
Chet Haase91d25932013-04-11 15:24:55 -07001231 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001232 }
1233 }
1234
1235
Mathias Agopian3559b072012-08-15 13:46:03 -07001236 /*
1237 * Perform our own transaction if needed
1238 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001239
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001240 const LayerVector& layers(mDrawingState.layersSortedByZ);
1241 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001242 // layers have been added
1243 mVisibleRegionsDirty = true;
1244 }
1245
1246 // some layers might have been removed, so
1247 // we need to update the regions they're exposing.
1248 if (mLayersRemoved) {
1249 mLayersRemoved = false;
1250 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001251 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001252 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001253 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001254 if (currentLayers.indexOf(layer) < 0) {
1255 // this layer is not visible anymore
1256 // TODO: we could traverse the tree from front to back and
1257 // compute the actual visible region
1258 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001259 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001260 Region visibleReg = s.transform.transform(
1261 Region(Rect(s.active.w, s.active.h)));
1262 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001263 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 }
1266
1267 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001268}
1269
1270void SurfaceFlinger::commitTransaction()
1271{
1272 if (!mLayersPendingRemoval.isEmpty()) {
1273 // Notify removed layers now that they can't be drawn from
1274 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1275 mLayersPendingRemoval[i]->onRemoved();
1276 }
1277 mLayersPendingRemoval.clear();
1278 }
1279
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001280 // If this transaction is part of a window animation then the next frame
1281 // we composite should be considered an animation as well.
1282 mAnimCompositionPending = mAnimTransactionPending;
1283
Mathias Agopian4fec8732012-06-29 14:12:52 -07001284 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001285 mTransactionPending = false;
1286 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001287 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288}
1289
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001291 const LayerVector& currentLayers, uint32_t layerStack,
1292 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293{
Mathias Agopian841cde52012-03-01 15:44:37 -08001294 ATRACE_CALL();
1295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 Region aboveOpaqueLayers;
1297 Region aboveCoveredLayers;
1298 Region dirty;
1299
Mathias Agopian87baae12012-07-31 12:38:26 -07001300 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301
1302 size_t i = currentLayers.size();
1303 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001304 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305
1306 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001307 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308
Jesse Hall01e29052013-02-19 16:13:35 -08001309 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001310 if (s.layerStack != layerStack)
1311 continue;
1312
Mathias Agopianab028732010-03-16 16:41:46 -07001313 /*
1314 * opaqueRegion: area of a surface that is fully opaque.
1315 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001317
1318 /*
1319 * visibleRegion: area of a surface that is visible on screen
1320 * and not fully transparent. This is essentially the layer's
1321 * footprint minus the opaque regions above it.
1322 * Areas covered by a translucent surface are considered visible.
1323 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001325
1326 /*
1327 * coveredRegion: area of a surface that is covered by all
1328 * visible regions above it (which includes the translucent areas).
1329 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001331
Jesse Halla8026d22012-09-25 13:25:04 -07001332 /*
1333 * transparentRegion: area of a surface that is hinted to be completely
1334 * transparent. This is only used to tell when the layer has no visible
1335 * non-transparent regions and can be removed from the layer list. It
1336 * does not affect the visibleRegion of this layer or any layers
1337 * beneath it. The hint may not be correct if apps don't respect the
1338 * SurfaceView restrictions (which, sadly, some don't).
1339 */
1340 Region transparentRegion;
1341
Mathias Agopianab028732010-03-16 16:41:46 -07001342
1343 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001344 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001345 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001346 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001348 if (!visibleRegion.isEmpty()) {
1349 // Remove the transparent area from the visible region
1350 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001351 const Transform tr(s.transform);
1352 if (tr.transformed()) {
1353 if (tr.preserveRects()) {
1354 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001355 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001356 } else {
1357 // transformation too complex, can't do the
1358 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001359 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001360 }
1361 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001362 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001363 }
Mathias Agopianab028732010-03-16 16:41:46 -07001364 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365
Mathias Agopianab028732010-03-16 16:41:46 -07001366 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001367 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001368 if (s.alpha==255 && !translucent &&
1369 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1370 // the opaque region is the layer's footprint
1371 opaqueRegion = visibleRegion;
1372 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 }
1374 }
1375
Mathias Agopianab028732010-03-16 16:41:46 -07001376 // Clip the covered region to the visible region
1377 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1378
1379 // Update aboveCoveredLayers for next (lower) layer
1380 aboveCoveredLayers.orSelf(visibleRegion);
1381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 // subtract the opaque region covered by the layers above us
1383 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384
1385 // compute this layer's dirty region
1386 if (layer->contentDirty) {
1387 // we need to invalidate the whole region
1388 dirty = visibleRegion;
1389 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001390 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391 layer->contentDirty = false;
1392 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001393 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001394 * the exposed region consists of two components:
1395 * 1) what's VISIBLE now and was COVERED before
1396 * 2) what's EXPOSED now less what was EXPOSED before
1397 *
1398 * note that (1) is conservative, we start with the whole
1399 * visible region but only keep what used to be covered by
1400 * something -- which mean it may have been exposed.
1401 *
1402 * (2) handles areas that were not covered by anything but got
1403 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001404 */
Mathias Agopianab028732010-03-16 16:41:46 -07001405 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001406 const Region oldVisibleRegion = layer->visibleRegion;
1407 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001408 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1409 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410 }
1411 dirty.subtractSelf(aboveOpaqueLayers);
1412
1413 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001414 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415
Mathias Agopianab028732010-03-16 16:41:46 -07001416 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001418
Jesse Halla8026d22012-09-25 13:25:04 -07001419 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 layer->setVisibleRegion(visibleRegion);
1421 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001422 layer->setVisibleNonTransparentRegion(
1423 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 }
1425
Mathias Agopian87baae12012-07-31 12:38:26 -07001426 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427}
1428
Mathias Agopian87baae12012-07-31 12:38:26 -07001429void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1430 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001431 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001432 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1433 if (hw->getLayerStack() == layerStack) {
1434 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001435 }
1436 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001437}
1438
1439void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001441 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442
Mathias Agopian4fec8732012-06-29 14:12:52 -07001443 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001444 const LayerVector& layers(mDrawingState.layersSortedByZ);
1445 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001446 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001447 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001448 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001449 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001450 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001451 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001452
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001453 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454}
1455
Mathias Agopianad456f92011-01-13 17:53:01 -08001456void SurfaceFlinger::invalidateHwcGeometry()
1457{
1458 mHwWorkListDirty = true;
1459}
1460
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001461
Mathias Agopiancd60f992012-08-16 16:28:27 -07001462void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001463 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464{
Mathias Agopian87baae12012-07-31 12:38:26 -07001465 Region dirtyRegion(inDirtyRegion);
1466
Mathias Agopianb8a55602009-06-26 19:06:36 -07001467 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001468 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469
Mathias Agopian42977342012-08-05 00:40:46 -07001470 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001471 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001472 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1473 // takes a rectangle, we must make sure to update that whole
1474 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001475 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001476 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001477 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001478 // We need to redraw the rectangle that will be updated
1479 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001480 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001481 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001482 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001483 } else {
1484 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001485 dirtyRegion.set(hw->bounds());
1486 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
1488 }
1489
Mathias Agopiancd60f992012-08-16 16:28:27 -07001490 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001492 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001493 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001494
1495 // swap buffers (presentation)
1496 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497}
1498
Mathias Agopiancd60f992012-08-16 16:28:27 -07001499void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001500{
Mathias Agopian3f844832013-08-07 21:24:32 -07001501 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001502 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001503 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001504 HWComposer::LayerListIterator cur = hwc.begin(id);
1505 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001506
Mathias Agopian85d751c2012-08-29 16:59:24 -07001507 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1508 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001509 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001510 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1511 hw->getDisplayName().string());
1512 return;
1513 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001514
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001515 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001516 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001517 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001518 // when using overlays, we assume a fully transparent framebuffer
1519 // NOTE: we could reduce how much we need to clear, for instance
1520 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001521 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001522 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001523 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001524 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001525 // we start with the whole screen area
1526 const Region bounds(hw->getBounds());
1527
1528 // we remove the scissor part
1529 // we're left with the letterbox region
1530 // (common case is that letterbox ends-up being empty)
1531 const Region letterbox(bounds.subtract(hw->getScissor()));
1532
1533 // compute the area to clear
1534 Region region(hw->undefinedRegion.merge(letterbox));
1535
1536 // but limit it to the dirty region
1537 region.andSelf(dirty);
1538
Mathias Agopianb9494d52012-04-18 02:28:45 -07001539 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001540 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001541 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001542 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001543 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001544 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001545
Mathias Agopian766dc492012-10-30 18:08:06 -07001546 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1547 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001548 // scissor on the main display. It should never be needed
1549 // anyways (though in theory it could since the API allows it).
1550 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001551 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001552 if (scissor != bounds) {
1553 // scissor doesn't match the screen's dimensions, so we
1554 // need to clear everything outside of it and enable
1555 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001556
Mathias Agopianf45c5102012-10-24 16:29:17 -07001557 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001558 const uint32_t height = hw->getHeight();
1559 engine.setScissor(scissor.left, height - scissor.bottom,
1560 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001561 }
1562 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001563 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001564
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 /*
1566 * and then, render the layers targeted at the framebuffer
1567 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001568
Mathias Agopian13127d82013-03-05 17:47:11 -08001569 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001570 const size_t count = layers.size();
1571 const Transform& tr = hw->getTransform();
1572 if (cur != end) {
1573 // we're using h/w composer
1574 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001575 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001576 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001577 if (!clip.isEmpty()) {
1578 switch (cur->getCompositionType()) {
1579 case HWC_OVERLAY: {
1580 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1581 && i
1582 && layer->isOpaque()
1583 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001584 // never clear the very first layer since we're
1585 // guaranteed the FB is already cleared
1586 layer->clearWithOpenGL(hw, clip);
1587 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001588 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001589 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001590 case HWC_FRAMEBUFFER: {
1591 layer->draw(hw, clip);
1592 break;
1593 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001594 case HWC_FRAMEBUFFER_TARGET: {
1595 // this should not happen as the iterator shouldn't
1596 // let us get there.
1597 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1598 break;
1599 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001600 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001601 }
1602 layer->setAcquireFence(hw, *cur);
1603 }
1604 } else {
1605 // we're not using h/w composer
1606 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001607 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001608 const Region clip(dirty.intersect(
1609 tr.transform(layer->visibleRegion)));
1610 if (!clip.isEmpty()) {
1611 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001612 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001613 }
1614 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001615
1616 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001617 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618}
1619
Mathias Agopian3f844832013-08-07 21:24:32 -07001620void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001621 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001622 RenderEngine& engine(getRenderEngine());
1623 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624}
1625
Mathias Agopianac9fa422013-02-11 16:40:36 -08001626void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1627 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001628 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001629 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001630{
Mathias Agopian96f08192010-06-02 23:28:45 -07001631 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001632 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001633
Mathias Agopian96f08192010-06-02 23:28:45 -07001634 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001635 Mutex::Autolock _l(mStateLock);
1636 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001637 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001638}
1639
Mathias Agopian3f844832013-08-07 21:24:32 -07001640status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001642 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001644 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001645 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001646 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 return NO_ERROR;
1648 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001649 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650}
1651
Mathias Agopian3f844832013-08-07 21:24:32 -07001652uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001653 return android_atomic_release_load(&mTransactionFlags);
1654}
1655
Mathias Agopian3f844832013-08-07 21:24:32 -07001656uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1658}
1659
Mathias Agopian3f844832013-08-07 21:24:32 -07001660uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1662 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001663 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 }
1665 return old;
1666}
1667
Mathias Agopian8b33f032012-07-24 20:43:54 -07001668void SurfaceFlinger::setTransactionState(
1669 const Vector<ComposerState>& state,
1670 const Vector<DisplayState>& displays,
1671 uint32_t flags)
1672{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001673 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001674 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001675 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001676
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001677 if (flags & eAnimation) {
1678 // For window updates that are part of an animation we must wait for
1679 // previous animation "frames" to be handled.
1680 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001681 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001682 if (CC_UNLIKELY(err != NO_ERROR)) {
1683 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001684 // caller after a few seconds.
1685 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1686 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001687 mAnimTransactionPending = false;
1688 break;
1689 }
1690 }
1691 }
1692
Mathias Agopiane57f2922012-08-09 16:29:12 -07001693 size_t count = displays.size();
1694 for (size_t i=0 ; i<count ; i++) {
1695 const DisplayState& s(displays[i]);
1696 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001697 }
1698
Mathias Agopiane57f2922012-08-09 16:29:12 -07001699 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001700 for (size_t i=0 ; i<count ; i++) {
1701 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001702 // Here we need to check that the interface we're given is indeed
1703 // one of our own. A malicious client could give us a NULL
1704 // IInterface, or one of its own or even one of our own but a
1705 // different type. All these situations would cause us to crash.
1706 //
1707 // NOTE: it would be better to use RTTI as we could directly check
1708 // that we have a Client*. however, RTTI is disabled in Android.
1709 if (s.client != NULL) {
1710 sp<IBinder> binder = s.client->asBinder();
1711 if (binder != NULL) {
1712 String16 desc(binder->getInterfaceDescriptor());
1713 if (desc == ISurfaceComposerClient::descriptor) {
1714 sp<Client> client( static_cast<Client *>(s.client.get()) );
1715 transactionFlags |= setClientStateLocked(client, s.state);
1716 }
1717 }
1718 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001719 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001720
Mathias Agopian386aa982011-11-07 21:58:03 -08001721 if (transactionFlags) {
1722 // this triggers the transaction
1723 setTransactionFlags(transactionFlags);
1724
1725 // if this is a synchronous transaction, wait for it to take effect
1726 // before returning.
1727 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001728 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001729 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001730 if (flags & eAnimation) {
1731 mAnimTransactionPending = true;
1732 }
1733 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001734 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1735 if (CC_UNLIKELY(err != NO_ERROR)) {
1736 // just in case something goes wrong in SF, return to the
1737 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001738 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1739 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001740 break;
1741 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001742 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 }
1744}
1745
Mathias Agopiane57f2922012-08-09 16:29:12 -07001746uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1747{
Jesse Hall9a143922012-10-04 16:29:19 -07001748 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1749 if (dpyIdx < 0)
1750 return 0;
1751
Mathias Agopiane57f2922012-08-09 16:29:12 -07001752 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001753 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001754 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001755 const uint32_t what = s.what;
1756 if (what & DisplayState::eSurfaceChanged) {
1757 if (disp.surface->asBinder() != s.surface->asBinder()) {
1758 disp.surface = s.surface;
1759 flags |= eDisplayTransactionNeeded;
1760 }
1761 }
1762 if (what & DisplayState::eLayerStackChanged) {
1763 if (disp.layerStack != s.layerStack) {
1764 disp.layerStack = s.layerStack;
1765 flags |= eDisplayTransactionNeeded;
1766 }
1767 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001768 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001769 if (disp.orientation != s.orientation) {
1770 disp.orientation = s.orientation;
1771 flags |= eDisplayTransactionNeeded;
1772 }
1773 if (disp.frame != s.frame) {
1774 disp.frame = s.frame;
1775 flags |= eDisplayTransactionNeeded;
1776 }
1777 if (disp.viewport != s.viewport) {
1778 disp.viewport = s.viewport;
1779 flags |= eDisplayTransactionNeeded;
1780 }
1781 }
1782 }
1783 return flags;
1784}
1785
1786uint32_t SurfaceFlinger::setClientStateLocked(
1787 const sp<Client>& client,
1788 const layer_state_t& s)
1789{
1790 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001791 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001792 if (layer != 0) {
1793 const uint32_t what = s.what;
1794 if (what & layer_state_t::ePositionChanged) {
1795 if (layer->setPosition(s.x, s.y))
1796 flags |= eTraversalNeeded;
1797 }
1798 if (what & layer_state_t::eLayerChanged) {
1799 // NOTE: index needs to be calculated before we update the state
1800 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1801 if (layer->setLayer(s.z)) {
1802 mCurrentState.layersSortedByZ.removeAt(idx);
1803 mCurrentState.layersSortedByZ.add(layer);
1804 // we need traversal (state changed)
1805 // AND transaction (list changed)
1806 flags |= eTransactionNeeded|eTraversalNeeded;
1807 }
1808 }
1809 if (what & layer_state_t::eSizeChanged) {
1810 if (layer->setSize(s.w, s.h)) {
1811 flags |= eTraversalNeeded;
1812 }
1813 }
1814 if (what & layer_state_t::eAlphaChanged) {
1815 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1816 flags |= eTraversalNeeded;
1817 }
1818 if (what & layer_state_t::eMatrixChanged) {
1819 if (layer->setMatrix(s.matrix))
1820 flags |= eTraversalNeeded;
1821 }
1822 if (what & layer_state_t::eTransparentRegionChanged) {
1823 if (layer->setTransparentRegionHint(s.transparentRegion))
1824 flags |= eTraversalNeeded;
1825 }
1826 if (what & layer_state_t::eVisibilityChanged) {
1827 if (layer->setFlags(s.flags, s.mask))
1828 flags |= eTraversalNeeded;
1829 }
1830 if (what & layer_state_t::eCropChanged) {
1831 if (layer->setCrop(s.crop))
1832 flags |= eTraversalNeeded;
1833 }
1834 if (what & layer_state_t::eLayerStackChanged) {
1835 // NOTE: index needs to be calculated before we update the state
1836 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1837 if (layer->setLayerStack(s.layerStack)) {
1838 mCurrentState.layersSortedByZ.removeAt(idx);
1839 mCurrentState.layersSortedByZ.add(layer);
1840 // we need traversal (state changed)
1841 // AND transaction (list changed)
1842 flags |= eTransactionNeeded|eTraversalNeeded;
1843 }
1844 }
1845 }
1846 return flags;
1847}
1848
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001849status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001850 const String8& name,
1851 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001852 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1853 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001855 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001856 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001857 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001858 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001859 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001860 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001861
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001862 status_t result = NO_ERROR;
1863
1864 sp<Layer> layer;
1865
Mathias Agopian3165cc22012-08-08 19:42:09 -07001866 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1867 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001868 result = createNormalLayer(client,
1869 name, w, h, flags, format,
1870 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001872 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001873 result = createDimLayer(client,
1874 name, w, h, flags,
1875 handle, gbp, &layer);
1876 break;
1877 default:
1878 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879 break;
1880 }
1881
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001882 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001883 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001884 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001885 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001886 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887}
1888
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001889status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1890 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1891 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001892{
1893 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001894 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895 case PIXEL_FORMAT_TRANSPARENT:
1896 case PIXEL_FORMAT_TRANSLUCENT:
1897 format = PIXEL_FORMAT_RGBA_8888;
1898 break;
1899 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001900#ifdef NO_RGBX_8888
1901 format = PIXEL_FORMAT_RGB_565;
1902#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001903 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001904#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 break;
1906 }
1907
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001908#ifdef NO_RGBX_8888
1909 if (format == PIXEL_FORMAT_RGBX_8888)
1910 format = PIXEL_FORMAT_RGBA_8888;
1911#endif
1912
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001913 *outLayer = new Layer(this, client, name, w, h, flags);
1914 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1915 if (err == NO_ERROR) {
1916 *handle = (*outLayer)->getHandle();
1917 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001919
1920 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1921 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922}
1923
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001924status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1925 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1926 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001928 *outLayer = new LayerDim(this, client, name, w, h, flags);
1929 *handle = (*outLayer)->getHandle();
1930 *gbp = (*outLayer)->getBufferQueue();
1931 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001932}
1933
Mathias Agopianac9fa422013-02-11 16:40:36 -08001934status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935{
Mathias Agopian67106042013-03-14 19:18:13 -07001936 // called by the window manager when it wants to remove a Layer
1937 status_t err = NO_ERROR;
1938 sp<Layer> l(client->getLayerUser(handle));
1939 if (l != NULL) {
1940 err = removeLayer(l);
1941 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1942 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001943 }
1944 return err;
1945}
1946
Mathias Agopian13127d82013-03-05 17:47:11 -08001947status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001948{
Mathias Agopian67106042013-03-14 19:18:13 -07001949 // called by ~LayerCleaner() when all references to the IBinder (handle)
1950 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001951 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001952 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001953 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001954 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001955 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001956 "error removing layer=%p (%s)", l.get(), strerror(-err));
1957 }
1958 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959}
1960
Mathias Agopianb60314a2012-04-10 22:09:54 -07001961// ---------------------------------------------------------------------------
1962
Andy McFadden13a082e2012-08-24 10:16:42 -07001963void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001964 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001965 Vector<ComposerState> state;
1966 Vector<DisplayState> displays;
1967 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001968 d.what = DisplayState::eDisplayProjectionChanged |
1969 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001970 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001971 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001972 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001973 d.frame.makeInvalid();
1974 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001975 displays.add(d);
1976 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001977 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001978
1979 const nsecs_t period =
1980 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1981 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001982}
1983
1984void SurfaceFlinger::initializeDisplays() {
1985 class MessageScreenInitialized : public MessageBase {
1986 SurfaceFlinger* flinger;
1987 public:
1988 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1989 virtual bool handler() {
1990 flinger->onInitializeDisplays();
1991 return true;
1992 }
1993 };
1994 sp<MessageBase> msg = new MessageScreenInitialized(this);
1995 postMessageAsync(msg); // we may be called from main thread, use async message
1996}
1997
1998
Mathias Agopiancde87a32012-09-13 14:09:01 -07001999void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002000 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2001 if (hw->isScreenAcquired()) {
2002 // this is expected, e.g. when power manager wakes up during boot
2003 ALOGD(" screen was previously acquired");
2004 return;
2005 }
2006
Mathias Agopian42977342012-08-05 00:40:46 -07002007 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002008 int32_t type = hw->getDisplayType();
2009 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2010 // built-in display, tell the HWC
2011 getHwComposer().acquire(type);
2012
2013 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2014 // FIXME: eventthread only knows about the main display right now
2015 mEventThread->onScreenAcquired();
2016 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002017 }
Mathias Agopian20128302012-08-13 18:32:13 -07002018 mVisibleRegionsDirty = true;
2019 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002020}
2021
Mathias Agopiancde87a32012-09-13 14:09:01 -07002022void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002023 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2024 if (!hw->isScreenAcquired()) {
2025 ALOGD(" screen was previously released");
2026 return;
2027 }
2028
2029 hw->releaseScreen();
2030 int32_t type = hw->getDisplayType();
2031 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2032 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002033 // FIXME: eventthread only knows about the main display right now
2034 mEventThread->onScreenReleased();
2035 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002036
2037 // built-in display, tell the HWC
2038 getHwComposer().release(type);
2039 }
2040 mVisibleRegionsDirty = true;
2041 // from this point on, SF will stop drawing on this display
2042}
2043
2044void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2045 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002046 SurfaceFlinger& mFlinger;
2047 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002048 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002049 MessageScreenAcquired(SurfaceFlinger& flinger,
2050 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002051 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002052 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2053 if (hw == NULL) {
2054 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2055 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2056 ALOGW("Attempt to unblank virtual display");
2057 } else {
2058 mFlinger.onScreenAcquired(hw);
2059 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002060 return true;
2061 }
2062 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002063 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2064 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002065}
2066
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002067void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002068 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002069 SurfaceFlinger& mFlinger;
2070 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002071 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 MessageScreenReleased(SurfaceFlinger& flinger,
2073 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002074 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002075 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2076 if (hw == NULL) {
2077 ALOGE("Attempt to blank null display %p", mDisplay.get());
2078 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2079 ALOGW("Attempt to blank virtual display");
2080 } else {
2081 mFlinger.onScreenReleased(hw);
2082 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002083 return true;
2084 }
2085 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002086 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2087 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002088}
2089
2090// ---------------------------------------------------------------------------
2091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2093{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002094 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002095
Mathias Agopianbd115332013-04-18 16:41:04 -07002096 IPCThreadState* ipc = IPCThreadState::self();
2097 const int pid = ipc->getCallingPid();
2098 const int uid = ipc->getCallingUid();
2099 if ((uid != AID_SHELL) &&
2100 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002101 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002102 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002103 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002104 // Try to get the main lock, but don't insist if we can't
2105 // (this would indicate SF is stuck, but we want to be able to
2106 // print something in dumpsys).
2107 int retry = 3;
2108 while (mStateLock.tryLock()<0 && --retry>=0) {
2109 usleep(1000000);
2110 }
2111 const bool locked(retry >= 0);
2112 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002113 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002114 "SurfaceFlinger appears to be unresponsive, "
2115 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002116 }
2117
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002118 bool dumpAll = true;
2119 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002120 size_t numArgs = args.size();
2121 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002122 if ((index < numArgs) &&
2123 (args[index] == String16("--list"))) {
2124 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002125 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002126 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002127 }
2128
2129 if ((index < numArgs) &&
2130 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002131 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002132 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002133 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002134 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002135
2136 if ((index < numArgs) &&
2137 (args[index] == String16("--latency-clear"))) {
2138 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002139 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002140 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002141 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002142 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002143
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002144 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002145 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002146 }
2147
Mathias Agopian9795c422009-08-26 16:36:26 -07002148 if (locked) {
2149 mStateLock.unlock();
2150 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002151 }
2152 write(fd, result.string(), result.size());
2153 return NO_ERROR;
2154}
2155
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002156void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002157 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002158{
2159 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2160 const size_t count = currentLayers.size();
2161 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002162 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002163 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002164 }
2165}
2166
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002167void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002168 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002169{
2170 String8 name;
2171 if (index < args.size()) {
2172 name = String8(args[index]);
2173 index++;
2174 }
2175
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002176 const nsecs_t period =
2177 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2178 result.appendFormat("%lld\n", period);
2179
2180 if (name.isEmpty()) {
2181 mAnimFrameTracker.dump(result);
2182 } else {
2183 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2184 const size_t count = currentLayers.size();
2185 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002186 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002187 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002188 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002189 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002190 }
2191 }
2192}
2193
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002194void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002195 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002196{
2197 String8 name;
2198 if (index < args.size()) {
2199 name = String8(args[index]);
2200 index++;
2201 }
2202
2203 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2204 const size_t count = currentLayers.size();
2205 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002206 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002207 if (name.isEmpty() || (name == layer->getName())) {
2208 layer->clearStats();
2209 }
2210 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002211
2212 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002213}
2214
Jamie Gennis6547ff42013-07-16 20:12:42 -07002215// This should only be called from the main thread. Otherwise it would need
2216// the lock and should use mCurrentState rather than mDrawingState.
2217void SurfaceFlinger::logFrameStats() {
2218 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2219 const size_t count = drawingLayers.size();
2220 for (size_t i=0 ; i<count ; i++) {
2221 const sp<Layer>& layer(drawingLayers[i]);
2222 layer->logFrameStats();
2223 }
2224
2225 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2226}
2227
Andy McFadden4803b742012-09-24 19:07:20 -07002228/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2229{
2230 static const char* config =
2231 " [sf"
2232#ifdef NO_RGBX_8888
2233 " NO_RGBX_8888"
2234#endif
2235#ifdef HAS_CONTEXT_PRIORITY
2236 " HAS_CONTEXT_PRIORITY"
2237#endif
2238#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2239 " NEVER_DEFAULT_TO_ASYNC_MODE"
2240#endif
2241#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2242 " TARGET_DISABLE_TRIPLE_BUFFERING"
2243#endif
2244 "]";
2245 result.append(config);
2246}
2247
Mathias Agopian74d211a2013-04-22 16:55:35 +02002248void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2249 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002250{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002251 bool colorize = false;
2252 if (index < args.size()
2253 && (args[index] == String16("--color"))) {
2254 colorize = true;
2255 index++;
2256 }
2257
2258 Colorizer colorizer(colorize);
2259
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002260 // figure out if we're stuck somewhere
2261 const nsecs_t now = systemTime();
2262 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2263 const nsecs_t inTransaction(mDebugInTransaction);
2264 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2265 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2266
2267 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002268 * Dump library configuration.
2269 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002270
2271 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002272 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002273 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002274 appendSfConfigString(result);
2275 appendUiConfigString(result);
2276 appendGuiConfigString(result);
2277 result.append("\n");
2278
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002279 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002280 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002281 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002282 result.append(SyncFeatures::getInstance().toString());
2283 result.append("\n");
2284
Andy McFadden4803b742012-09-24 19:07:20 -07002285 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002286 * Dump the visible layer list
2287 */
2288 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2289 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002290 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002291 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002292 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002293 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002294 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002295 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002296 }
2297
2298 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002299 * Dump Display state
2300 */
2301
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002302 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002303 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002304 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002305 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2306 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002307 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002308 }
2309
2310 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002311 * Dump SurfaceFlinger global state
2312 */
2313
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002314 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002315 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002316 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002317
Mathias Agopian888c8222012-08-04 21:10:38 -07002318 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002319 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002320
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002321 colorizer.bold(result);
2322 result.appendFormat("EGL implementation : %s\n",
2323 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2324 colorizer.reset(result);
2325 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002326 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002327
Mathias Agopian875d8e12013-06-07 15:35:48 -07002328 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002329
Mathias Agopian42977342012-08-05 00:40:46 -07002330 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002331 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002332 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002333 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002334 " last eglSwapBuffers() time: %f us\n"
2335 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002336 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002337 " refresh-rate : %f fps\n"
2338 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002339 " y-dpi : %f\n"
2340 " EGL_NATIVE_VISUAL_ID : %d\n"
2341 " gpu_to_cpu_unsupported : %d\n"
2342 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002343 mLastSwapBufferTime/1000.0,
2344 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002345 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002346 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2347 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002348 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2349 mEGLNativeVisualId,
2350 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002351
Mathias Agopian74d211a2013-04-22 16:55:35 +02002352 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002353 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354
Mathias Agopian74d211a2013-04-22 16:55:35 +02002355 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357
2358 /*
2359 * VSYNC state
2360 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002361 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002362
2363 /*
2364 * Dump HWComposer state
2365 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002366 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002367 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002368 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002369 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002370 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2371 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002372 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002373
2374 /*
2375 * Dump gralloc state
2376 */
2377 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2378 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379}
2380
Mathias Agopian13127d82013-03-05 17:47:11 -08002381const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002382SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002383 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002384 wp<IBinder> dpy;
2385 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2386 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2387 dpy = mDisplays.keyAt(i);
2388 break;
2389 }
2390 }
2391 if (dpy == NULL) {
2392 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2393 // Just use the primary display so we have something to return
2394 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2395 }
2396 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002397}
2398
Keun young Park63f165f2012-08-31 10:53:36 -07002399bool SurfaceFlinger::startDdmConnection()
2400{
2401 void* libddmconnection_dso =
2402 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2403 if (!libddmconnection_dso) {
2404 return false;
2405 }
2406 void (*DdmConnection_start)(const char* name);
2407 DdmConnection_start =
2408 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2409 if (!DdmConnection_start) {
2410 dlclose(libddmconnection_dso);
2411 return false;
2412 }
2413 (*DdmConnection_start)(getServiceName());
2414 return true;
2415}
2416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002417status_t SurfaceFlinger::onTransact(
2418 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2419{
2420 switch (code) {
2421 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002422 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002423 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002424 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002425 case BLANK:
2426 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427 {
2428 // codes that require permission check
2429 IPCThreadState* ipc = IPCThreadState::self();
2430 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002431 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002432 if ((uid != AID_GRAPHICS) &&
2433 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002434 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002435 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2436 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002438 break;
2439 }
2440 case CAPTURE_SCREEN:
2441 {
2442 // codes that require permission check
2443 IPCThreadState* ipc = IPCThreadState::self();
2444 const int pid = ipc->getCallingPid();
2445 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002446 if ((uid != AID_GRAPHICS) &&
2447 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002448 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002449 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2450 return PERMISSION_DENIED;
2451 }
2452 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002453 }
2454 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2457 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002458 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002459 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002460 IPCThreadState* ipc = IPCThreadState::self();
2461 const int pid = ipc->getCallingPid();
2462 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002463 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002464 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002465 return PERMISSION_DENIED;
2466 }
2467 int n;
2468 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002469 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002470 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002471 return NO_ERROR;
2472 case 1002: // SHOW_UPDATES
2473 n = data.readInt32();
2474 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002475 invalidateHwcGeometry();
2476 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002478 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002479 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002480 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002481 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002482 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002483 setTransactionFlags(
2484 eTransactionNeeded|
2485 eDisplayTransactionNeeded|
2486 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002487 return NO_ERROR;
2488 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002489 case 1006:{ // send empty update
2490 signalRefresh();
2491 return NO_ERROR;
2492 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002493 case 1008: // toggle use of hw composer
2494 n = data.readInt32();
2495 mDebugDisableHWC = n ? 1 : 0;
2496 invalidateHwcGeometry();
2497 repaintEverything();
2498 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002499 case 1009: // toggle use of transform hint
2500 n = data.readInt32();
2501 mDebugDisableTransformHint = n ? 1 : 0;
2502 invalidateHwcGeometry();
2503 repaintEverything();
2504 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002506 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507 reply->writeInt32(0);
2508 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002509 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002510 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 return NO_ERROR;
2512 case 1013: {
2513 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002514 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2515 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516 }
2517 return NO_ERROR;
2518 }
2519 }
2520 return err;
2521}
2522
Mathias Agopian53331da2011-08-22 21:44:41 -07002523void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002524 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002525 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002526}
2527
Mathias Agopian59119e62010-10-11 12:37:43 -07002528// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002529// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002530// ---------------------------------------------------------------------------
2531
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002532/* The code below is here to handle b/8734824
2533 *
2534 * We create a IGraphicBufferProducer wrapper that forwards all calls
2535 * to the calling binder thread, where they are executed. This allows
2536 * the calling thread to be reused (on the other side) and not
2537 * depend on having "enough" binder threads to handle the requests.
2538 *
2539 */
2540
2541class GraphicProducerWrapper : public BBinder, public MessageHandler {
2542 sp<IGraphicBufferProducer> impl;
2543 sp<Looper> looper;
2544 status_t result;
2545 bool exitPending;
2546 bool exitRequested;
2547 mutable Barrier barrier;
2548 volatile int32_t memoryBarrier;
2549 uint32_t code;
2550 Parcel const* data;
2551 Parcel* reply;
2552
2553 enum {
2554 MSG_API_CALL,
2555 MSG_EXIT
2556 };
2557
2558 /*
2559 * this is called by our "fake" BpGraphicBufferProducer. We package the
2560 * data and reply Parcel and forward them to the calling thread.
2561 */
2562 virtual status_t transact(uint32_t code,
2563 const Parcel& data, Parcel* reply, uint32_t flags) {
2564 this->code = code;
2565 this->data = &data;
2566 this->reply = reply;
2567 android_atomic_acquire_store(0, &memoryBarrier);
2568 if (exitPending) {
2569 // if we've exited, we run the message synchronously right here
2570 handleMessage(Message(MSG_API_CALL));
2571 } else {
2572 barrier.close();
2573 looper->sendMessage(this, Message(MSG_API_CALL));
2574 barrier.wait();
2575 }
2576 return NO_ERROR;
2577 }
2578
2579 /*
2580 * here we run on the binder calling thread. All we've got to do is
2581 * call the real BpGraphicBufferProducer.
2582 */
2583 virtual void handleMessage(const Message& message) {
2584 android_atomic_release_load(&memoryBarrier);
2585 if (message.what == MSG_API_CALL) {
2586 impl->asBinder()->transact(code, data[0], reply);
2587 barrier.open();
2588 } else if (message.what == MSG_EXIT) {
2589 exitRequested = true;
2590 }
2591 }
2592
2593public:
2594 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2595 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2596 exitPending(false), exitRequested(false) {
2597 }
2598
2599 status_t waitForResponse() {
2600 do {
2601 looper->pollOnce(-1);
2602 } while (!exitRequested);
2603 return result;
2604 }
2605
2606 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002607 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002608 exitPending = true;
2609 looper->sendMessage(this, Message(MSG_EXIT));
2610 }
2611};
2612
2613
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002614status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2615 const sp<IGraphicBufferProducer>& producer,
2616 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002617 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002618
2619 if (CC_UNLIKELY(display == 0))
2620 return BAD_VALUE;
2621
2622 if (CC_UNLIKELY(producer == 0))
2623 return BAD_VALUE;
2624
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002625 // if we have secure windows on this display, never allow the screen capture
2626 // unless the producer interface is local (i.e.: we can take a screenshot for
2627 // ourselves).
2628 if (!producer->asBinder()->localBinder()) {
2629 Mutex::Autolock _l(mStateLock);
2630 sp<const DisplayDevice> hw(getDisplayDevice(display));
2631 if (hw->getSecureLayerVisible()) {
2632 ALOGW("FB is protected: PERMISSION_DENIED");
2633 return PERMISSION_DENIED;
2634 }
2635 }
2636
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002637 class MessageCaptureScreen : public MessageBase {
2638 SurfaceFlinger* flinger;
2639 sp<IBinder> display;
2640 sp<IGraphicBufferProducer> producer;
2641 uint32_t reqWidth, reqHeight;
2642 uint32_t minLayerZ,maxLayerZ;
2643 status_t result;
2644 public:
2645 MessageCaptureScreen(SurfaceFlinger* flinger,
2646 const sp<IBinder>& display,
2647 const sp<IGraphicBufferProducer>& producer,
2648 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002649 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002650 : flinger(flinger), display(display), producer(producer),
2651 reqWidth(reqWidth), reqHeight(reqHeight),
2652 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2653 result(PERMISSION_DENIED)
2654 {
2655 }
2656 status_t getResult() const {
2657 return result;
2658 }
2659 virtual bool handler() {
2660 Mutex::Autolock _l(flinger->mStateLock);
2661 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002662 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002663 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002664 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002665 return true;
2666 }
2667 };
2668
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002669 // make sure to process transactions before screenshots -- a transaction
2670 // might already be pending but scheduled for VSYNC; this guarantees we
2671 // will handle it before the screenshot. When VSYNC finally arrives
2672 // the scheduled transaction will be a no-op. If no transactions are
2673 // scheduled at this time, this will end-up being a no-op as well.
2674 mEventQueue.invalidateTransactionNow();
2675
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002676 // this creates a "fake" BBinder which will serve as a "fake" remote
2677 // binder to receive the marshaled calls and forward them to the
2678 // real remote (a BpGraphicBufferProducer)
2679 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2680
2681 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2682 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002683 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002684 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002685 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002686
2687 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002688 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002689 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002690 }
2691 return res;
2692}
2693
Mathias Agopian180f10d2013-04-10 22:55:41 -07002694
2695void SurfaceFlinger::renderScreenImplLocked(
2696 const sp<const DisplayDevice>& hw,
2697 uint32_t reqWidth, uint32_t reqHeight,
2698 uint32_t minLayerZ, uint32_t maxLayerZ,
2699 bool yswap)
2700{
2701 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002702 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002703
2704 // get screen geometry
2705 const uint32_t hw_w = hw->getWidth();
2706 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002707 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2708
2709 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002710 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002711
2712 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002713 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2714 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002715
2716 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002717 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002718
2719 const LayerVector& layers( mDrawingState.layersSortedByZ );
2720 const size_t count = layers.size();
2721 for (size_t i=0 ; i<count ; ++i) {
2722 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002723 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002724 if (state.layerStack == hw->getLayerStack()) {
2725 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2726 if (layer->isVisible()) {
2727 if (filtering) layer->setFiltering(true);
2728 layer->draw(hw);
2729 if (filtering) layer->setFiltering(false);
2730 }
2731 }
2732 }
2733 }
2734
2735 // compositionComplete is needed for older driver
2736 hw->compositionComplete();
2737}
2738
2739
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002740status_t SurfaceFlinger::captureScreenImplLocked(
2741 const sp<const DisplayDevice>& hw,
2742 const sp<IGraphicBufferProducer>& producer,
2743 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002744 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002745{
2746 ATRACE_CALL();
2747
Mathias Agopian180f10d2013-04-10 22:55:41 -07002748 // get screen geometry
2749 const uint32_t hw_w = hw->getWidth();
2750 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002751
Mathias Agopian180f10d2013-04-10 22:55:41 -07002752 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2753 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2754 reqWidth, reqHeight, hw_w, hw_h);
2755 return BAD_VALUE;
2756 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002757
Mathias Agopian180f10d2013-04-10 22:55:41 -07002758 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2759 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2760
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002761 // create a surface (because we're a producer, and we need to
2762 // dequeue/queue a buffer)
2763 sp<Surface> sur = new Surface(producer);
2764 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002765
2766 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002767 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002768 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2769 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002770
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002771 int err = 0;
2772 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2773 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2774 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002775
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002776 if (err == NO_ERROR) {
2777 ANativeWindowBuffer* buffer;
2778 /* TODO: Once we have the sync framework everywhere this can use
2779 * server-side waits on the fence that dequeueBuffer returns.
2780 */
2781 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2782 if (result == NO_ERROR) {
2783 // create an EGLImage from the buffer so we can later
2784 // turn it into a texture
2785 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2786 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2787 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002788 // this binds the given EGLImage as a framebuffer for the
2789 // duration of this scope.
2790 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2791 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002792 // this will in fact render into our dequeued buffer
2793 // via an FBO, which means we didn't have to create
2794 // an EGLSurface and therefore we're not
2795 // dependent on the context's EGLConfig.
2796 renderScreenImplLocked(hw, reqWidth, reqHeight,
2797 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002798 } else {
2799 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2800 result = INVALID_OPERATION;
2801 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002802 // destroy our image
2803 eglDestroyImageKHR(mEGLDisplay, image);
2804 } else {
2805 result = BAD_VALUE;
2806 }
2807 window->queueBuffer(window, buffer, -1);
2808 }
2809 } else {
2810 result = BAD_VALUE;
2811 }
2812 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2813 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002814
Mathias Agopian875d8e12013-06-07 15:35:48 -07002815 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002816
Mathias Agopian74c40c02010-09-29 13:02:36 -07002817 return result;
2818}
2819
Mathias Agopianfee2b462013-07-03 12:34:01 -07002820void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2821 const sp<const DisplayDevice>& hw,
2822 uint32_t minLayerZ, uint32_t maxLayerZ) {
2823 if (DEBUG_SCREENSHOTS) {
2824 for (ssize_t y=0 ; y<buf->height ; y++) {
2825 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2826 for (ssize_t x=0 ; x<buf->width ; x++) {
2827 if (p[x] != 0xFF000000) return;
2828 }
2829 }
2830 ALOGE("*** we just took a black screenshot ***\n"
2831 "requested minz=%d, maxz=%d, layerStack=%d",
2832 minLayerZ, maxLayerZ, hw->getLayerStack());
2833 const LayerVector& layers( mDrawingState.layersSortedByZ );
2834 const size_t count = layers.size();
2835 for (size_t i=0 ; i<count ; ++i) {
2836 const sp<Layer>& layer(layers[i]);
2837 const Layer::State& state(layer->getDrawingState());
2838 const bool visible = (state.layerStack == hw->getLayerStack())
2839 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2840 && (layer->isVisible());
2841 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2842 visible ? '+' : '-',
2843 i, layer->getName().string(), state.layerStack, state.z,
2844 layer->isVisible(), state.flags, state.alpha);
2845 }
2846 }
2847}
2848
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002849// ---------------------------------------------------------------------------
2850
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002851SurfaceFlinger::LayerVector::LayerVector() {
2852}
2853
2854SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002855 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002856}
2857
2858int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2859 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002860{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002861 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002862 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2863 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002864
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002865 uint32_t ls = l->getCurrentState().layerStack;
2866 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002867 if (ls != rs)
2868 return ls - rs;
2869
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002870 uint32_t lz = l->getCurrentState().z;
2871 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002872 if (lz != rz)
2873 return lz - rz;
2874
2875 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002876}
2877
2878// ---------------------------------------------------------------------------
2879
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002880SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2881 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002882}
2883
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002884SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002885 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002886 viewport.makeInvalid();
2887 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002888}
2889
2890// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002891
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002892}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002893
2894
2895#if defined(__gl_h_)
2896#error "don't include gl/gl.h in this file"
2897#endif
2898
2899#if defined(__gl2_h_)
2900#error "don't include gl2/gl2.h in this file"
2901#endif