blob: fba6bba71fe2021306a2c053254654734e4fc426 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080043#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044
45#include <ui/GraphicBufferAllocator.h>
46#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070047#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Mathias Agopiancde87a32012-09-13 14:09:01 -070049#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include <utils/String8.h>
51#include <utils/String16.h>
52#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian921e6ac2012-07-23 23:11:29 -070055#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
57#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080058#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070059#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070060#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080061#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070062#include "GLExtensions.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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopiana67932f2011-04-20 14:20:59 -070070
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080071#define EGL_VERSION_HW_ANDROID 0x3143
72
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073#define DISPLAY_COUNT 1
74
75namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076// ---------------------------------------------------------------------------
77
Mathias Agopian99b49842011-06-27 16:05:52 -070078const String16 sHardwareTest("android.permission.HARDWARE_TEST");
79const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
80const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
81const String16 sDump("android.permission.DUMP");
82
83// ---------------------------------------------------------------------------
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085SurfaceFlinger::SurfaceFlinger()
86 : BnSurfaceComposer(), Thread(false),
87 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070088 mTransactionPending(false),
89 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070091 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070094 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -080095 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070097 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070098 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070099 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700100 mDebugInSwapBuffers(0),
101 mLastSwapBufferTime(0),
102 mDebugInTransaction(0),
103 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700104 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114 property_get("debug.sf.ddms", value, "0");
115 mDebugDDMS = atoi(value);
116 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700117 if (!startDdmConnection()) {
118 // start failed, and DDMS debugging not enabled
119 mDebugDDMS = 0;
120 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700122 ALOGI_IF(mDebugRegion, "showupdates enabled");
123 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126void SurfaceFlinger::onFirstRef()
127{
128 mEventQueue.init(this);
129
130 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
131
132 // Wait for the main thread to be done with its initialization
133 mReadyToRunBarrier.wait();
134}
135
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137SurfaceFlinger::~SurfaceFlinger()
138{
Mathias Agopiana4912602012-07-12 14:25:33 -0700139 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
140 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
141 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800144void SurfaceFlinger::binderDied(const wp<IBinder>& who)
145{
146 // the window manager died on us. prepare its eulogy.
147
Andy McFadden13a082e2012-08-24 10:16:42 -0700148 // restore initial conditions (default device unblank, etc)
149 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150
151 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700152 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800153}
154
Mathias Agopian7e27f052010-05-28 14:22:23 -0700155sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Mathias Agopian96f08192010-06-02 23:28:45 -0700157 sp<ISurfaceComposerClient> bclient;
158 sp<Client> client(new Client(this));
159 status_t err = client->initCheck();
160 if (err == NO_ERROR) {
161 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 return bclient;
164}
165
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700166sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
167 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700168{
169 class DisplayToken : public BBinder {
170 sp<SurfaceFlinger> flinger;
171 virtual ~DisplayToken() {
172 // no more references, this display must be terminated
173 Mutex::Autolock _l(flinger->mStateLock);
174 flinger->mCurrentState.displays.removeItem(this);
175 flinger->setTransactionFlags(eDisplayTransactionNeeded);
176 }
177 public:
178 DisplayToken(const sp<SurfaceFlinger>& flinger)
179 : flinger(flinger) {
180 }
181 };
182
183 sp<BBinder> token = new DisplayToken(this);
184
185 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700186 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700187 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700188 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700189 mCurrentState.displays.add(token, info);
190
191 return token;
192}
193
Jesse Hall692c7232012-11-08 15:41:56 -0800194void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
195 ALOGW_IF(mBuiltinDisplays[type],
196 "Overwriting display token for display type %d", type);
197 mBuiltinDisplays[type] = new BBinder();
198 DisplayDeviceState info(type);
199 // All non-virtual displays are currently considered secure.
200 info.isSecure = true;
201 mCurrentState.displays.add(mBuiltinDisplays[type], info);
202}
203
Mathias Agopiane57f2922012-08-09 16:29:12 -0700204sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700205 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700206 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
207 return NULL;
208 }
Jesse Hall692c7232012-11-08 15:41:56 -0800209 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700210}
211
Jamie Gennis9a78c902011-01-12 18:30:40 -0800212sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
213{
214 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
215 return gba;
216}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218void SurfaceFlinger::bootFinished()
219{
220 const nsecs_t now = systemTime();
221 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000222 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700223 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700224
225 // wait patiently for the window manager death
226 const String16 name("window");
227 sp<IBinder> window(defaultServiceManager()->getService(name));
228 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700229 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700230 }
231
232 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700233 // formerly we would just kill the process, but we now ask it to exit so it
234 // can choose where to stop the animation.
235 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236}
237
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700238void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
239 class MessageDestroyGLTexture : public MessageBase {
240 GLuint texture;
241 public:
242 MessageDestroyGLTexture(GLuint texture)
243 : texture(texture) {
244 }
245 virtual bool handler() {
246 glDeleteTextures(1, &texture);
247 return true;
248 }
249 };
250 postMessageAsync(new MessageDestroyGLTexture(texture));
251}
252
Mathias Agopian722b98f2012-09-25 18:24:31 -0700253status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700254 EGLDisplay dpy,
255 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700256 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700257 EGLConfig* outConfig)
258{
259 EGLConfig config = NULL;
260 EGLint numConfigs = -1, n=0;
261 eglGetConfigs(dpy, NULL, 0, &numConfigs);
262 EGLConfig* const configs = new EGLConfig[numConfigs];
263 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700264
Mathias Agopian722b98f2012-09-25 18:24:31 -0700265 if (n) {
266 if (attribute != EGL_NONE) {
267 for (int i=0 ; i<n ; i++) {
268 EGLint value = 0;
269 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
270 if (wanted == value) {
271 *outConfig = configs[i];
272 delete [] configs;
273 return NO_ERROR;
274 }
275 }
276 } else {
277 // just pick the first one
278 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700279 delete [] configs;
280 return NO_ERROR;
281 }
282 }
283 delete [] configs;
284 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285}
286
Mathias Agopian722b98f2012-09-25 18:24:31 -0700287class EGLAttributeVector {
288 struct Attribute;
289 class Adder;
290 friend class Adder;
291 KeyedVector<Attribute, EGLint> mList;
292 struct Attribute {
293 Attribute() {};
294 Attribute(EGLint v) : v(v) { }
295 EGLint v;
296 bool operator < (const Attribute& other) const {
297 // this places EGL_NONE at the end
298 EGLint lhs(v);
299 EGLint rhs(other.v);
300 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
301 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
302 return lhs < rhs;
303 }
304 };
305 class Adder {
306 friend class EGLAttributeVector;
307 EGLAttributeVector& v;
308 EGLint attribute;
309 Adder(EGLAttributeVector& v, EGLint attribute)
310 : v(v), attribute(attribute) {
311 }
312 public:
313 void operator = (EGLint value) {
314 if (attribute != EGL_NONE) {
315 v.mList.add(attribute, value);
316 }
317 }
318 operator EGLint () const { return v.mList[attribute]; }
319 };
320public:
321 EGLAttributeVector() {
322 mList.add(EGL_NONE, EGL_NONE);
323 }
324 void remove(EGLint attribute) {
325 if (attribute != EGL_NONE) {
326 mList.removeItem(attribute);
327 }
328 }
329 Adder operator [] (EGLint attribute) {
330 return Adder(*this, attribute);
331 }
332 EGLint operator [] (EGLint attribute) const {
333 return mList[attribute];
334 }
335 // cast-operator to (EGLint const*)
336 operator EGLint const* () const { return &mList.keyAt(0).v; }
337};
338
Mathias Agopiana4912602012-07-12 14:25:33 -0700339EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
340 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
341 // it is to be used with WIFI displays
342 EGLConfig config;
343 EGLint dummy;
344 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700345
Mathias Agopian722b98f2012-09-25 18:24:31 -0700346 EGLAttributeVector attribs;
347 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
348 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
349 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
350 attribs[EGL_RED_SIZE] = 8;
351 attribs[EGL_GREEN_SIZE] = 8;
352 attribs[EGL_BLUE_SIZE] = 8;
353
354 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
355 if (!err)
356 goto success;
357
358 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
359 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
360 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
361 err = selectConfigForAttribute(display, attribs,
362 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700363 if (!err)
364 goto success;
365
366 // maybe we failed because of EGL_RECORDABLE_ANDROID
367 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700368 attribs.remove(EGL_RECORDABLE_ANDROID);
369 err = selectConfigForAttribute(display, attribs,
370 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700371 if (!err)
372 goto success;
373
374 // allow less than 24-bit color; the non-gpu-accelerated emulator only
375 // supports 16-bit color
376 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700377 attribs.remove(EGL_RED_SIZE);
378 attribs.remove(EGL_GREEN_SIZE);
379 attribs.remove(EGL_BLUE_SIZE);
380 err = selectConfigForAttribute(display, attribs,
381 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700382 if (!err)
383 goto success;
384
385 // this EGL is too lame for Android
386 ALOGE("no suitable EGLConfig found, giving up");
387
388 return 0;
389
390success:
391 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700392 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700393 return config;
394}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395
Mathias Agopiana4912602012-07-12 14:25:33 -0700396EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
397 // Also create our EGLContext
398 EGLint contextAttributes[] = {
399#ifdef EGL_IMG_context_priority
400#ifdef HAS_CONTEXT_PRIORITY
401#warning "using EGL_IMG_context_priority"
402 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
403#endif
404#endif
405 EGL_NONE, EGL_NONE
406 };
407 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
408 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
409 return ctxt;
410}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411
Mathias Agopiancde87a32012-09-13 14:09:01 -0700412void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 GLExtensions& extensions(GLExtensions::getInstance());
414 extensions.initWithGLStrings(
415 glGetString(GL_VENDOR),
416 glGetString(GL_RENDERER),
417 glGetString(GL_VERSION),
418 glGetString(GL_EXTENSIONS),
419 eglQueryString(display, EGL_VENDOR),
420 eglQueryString(display, EGL_VERSION),
421 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700422
Mathias Agopiana4912602012-07-12 14:25:33 -0700423 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
424 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700427 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 glShadeModel(GL_FLAT);
430 glDisable(GL_DITHER);
431 glDisable(GL_CULL_FACE);
432
Mathias Agopiana4912602012-07-12 14:25:33 -0700433 struct pack565 {
434 inline uint16_t operator() (int r, int g, int b) const {
435 return (r<<11)|(g<<5)|b;
436 }
437 } pack565;
438
Jamie Gennis9575f602011-10-07 14:51:16 -0700439 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
440 glGenTextures(1, &mProtectedTexName);
441 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
442 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
443 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
444 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
445 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
446 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
447 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448
Mathias Agopiana4912602012-07-12 14:25:33 -0700449 // print some debugging info
450 EGLint r,g,b,a;
451 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
452 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
453 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
454 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
455 ALOGI("EGL informations:");
456 ALOGI("vendor : %s", extensions.getEglVendor());
457 ALOGI("version : %s", extensions.getEglVersion());
458 ALOGI("extensions: %s", extensions.getEglExtension());
459 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
460 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
461 ALOGI("OpenGL ES informations:");
462 ALOGI("vendor : %s", extensions.getVendor());
463 ALOGI("renderer : %s", extensions.getRenderer());
464 ALOGI("version : %s", extensions.getVersion());
465 ALOGI("extensions: %s", extensions.getExtension());
466 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
467 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
468}
469
Mathias Agopiana4912602012-07-12 14:25:33 -0700470status_t SurfaceFlinger::readyToRun()
471{
472 ALOGI( "SurfaceFlinger's main thread ready to run. "
473 "Initializing graphics H/W...");
474
Jesse Hall692c7232012-11-08 15:41:56 -0800475 Mutex::Autolock _l(mStateLock);
476
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700477 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700478 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
479 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700480
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700481 // Initialize the H/W composer object. There may or may not be an
482 // actual hardware composer underneath.
483 mHwc = new HWComposer(this,
484 *static_cast<HWComposer::EventHandler *>(this));
485
Mathias Agopiana4912602012-07-12 14:25:33 -0700486 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700487 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700488 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
489 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700490
Mathias Agopianda27af92012-09-13 18:17:13 -0700491 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
492 "couldn't create EGLContext");
493
Mathias Agopiancde87a32012-09-13 14:09:01 -0700494 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700495 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700496 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700497 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700498 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700499 // All non-virtual displays are currently considered secure.
500 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800501 createBuiltinDisplayLocked(type);
502 wp<IBinder> token = mBuiltinDisplays[i];
503
Mathias Agopianf5a33922012-09-19 18:16:22 -0700504 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800505 sp<Surface> stc = new Surface(
Andy McFadden2adaf042012-12-18 09:49:45 -0800506 static_cast< sp<IGraphicBufferProducer> >(fbs->getBufferQueue()));
Mathias Agopianf5a33922012-09-19 18:16:22 -0700507 sp<DisplayDevice> hw = new DisplayDevice(this,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700508 type, isSecure, token, stc, fbs, mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700509 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700510 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700511 // for displays other than the main display, so we always
512 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700513 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700514 hw->acquireScreen();
515 }
516 mDisplays.add(token, hw);
517 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700518 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700519
Mathias Agopianf5a33922012-09-19 18:16:22 -0700520 // we need a GL context current in a few places, when initializing
521 // OpenGL ES (see below), or creating a layer,
522 // or when a texture is (asynchronously) destroyed, and for that
523 // we need a valid surface, so it's convenient to use the main display
524 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700525 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700526
Mathias Agopiana4912602012-07-12 14:25:33 -0700527 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700528 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
529 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800530
Mathias Agopian028508c2012-07-25 21:12:12 -0700531 // start the EventThread
532 mEventThread = new EventThread(this);
533 mEventQueue.setEventThread(mEventThread);
534
Mathias Agopian92a979a2012-08-02 18:32:23 -0700535 // initialize our drawing state
536 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700537
Mathias Agopiancde87a32012-09-13 14:09:01 -0700538
Mathias Agopiana4912602012-07-12 14:25:33 -0700539 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800540 mReadyToRunBarrier.open();
541
Andy McFadden13a082e2012-08-24 10:16:42 -0700542 // set initial conditions (e.g. unblank default device)
543 initializeDisplays();
544
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700545 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700546 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700547
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 return NO_ERROR;
549}
550
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700551int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
552 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
553 type : mHwc->allocateDisplayId();
554}
555
Mathias Agopiana67e4182012-06-19 17:26:12 -0700556void SurfaceFlinger::startBootAnim() {
557 // start boot animation
558 property_set("service.bootanim.exit", "0");
559 property_set("ctl.start", "bootanim");
560}
561
Mathias Agopiana4912602012-07-12 14:25:33 -0700562uint32_t SurfaceFlinger::getMaxTextureSize() const {
563 return mMaxTextureSize;
564}
565
566uint32_t SurfaceFlinger::getMaxViewportDims() const {
567 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
568 mMaxViewportDims[0] : mMaxViewportDims[1];
569}
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800572
Jamie Gennis582270d2011-08-17 18:19:00 -0700573bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800574 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800575 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800576 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700577 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800578}
579
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700580status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800581 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700582 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800583 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700584 type = i;
585 break;
586 }
587 }
588
589 if (type < 0) {
590 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700591 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700592
593 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700594 float xdpi = hwc.getDpiX(type);
595 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700596
597 // TODO: Not sure if display density should handled by SF any longer
598 class Density {
599 static int getDensityFromProperty(char const* propName) {
600 char property[PROPERTY_VALUE_MAX];
601 int density = 0;
602 if (property_get(propName, property, NULL) > 0) {
603 density = atoi(property);
604 }
605 return density;
606 }
607 public:
608 static int getEmuDensity() {
609 return getDensityFromProperty("qemu.sf.lcd_density"); }
610 static int getBuildDensity() {
611 return getDensityFromProperty("ro.sf.lcd_density"); }
612 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700613
614 if (type == DisplayDevice::DISPLAY_PRIMARY) {
615 // The density of the device is provided by a build property
616 float density = Density::getBuildDensity() / 160.0f;
617 if (density == 0) {
618 // the build doesn't provide a density -- this is wrong!
619 // use xdpi instead
620 ALOGE("ro.sf.lcd_density must be defined as a build property");
621 density = xdpi / 160.0f;
622 }
623 if (Density::getEmuDensity()) {
624 // if "qemu.sf.lcd_density" is specified, it overrides everything
625 xdpi = ydpi = density = Density::getEmuDensity();
626 density /= 160.0f;
627 }
628 info->density = density;
629
630 // TODO: this needs to go away (currently needed only by webkit)
631 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
632 info->orientation = hw->getOrientation();
633 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
634 } else {
635 // TODO: where should this value come from?
636 static const int TV_DENSITY = 213;
637 info->density = TV_DENSITY / 160.0f;
638 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700639 }
640
Mathias Agopian1604f772012-09-18 21:54:42 -0700641 info->w = hwc.getWidth(type);
642 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700643 info->xdpi = xdpi;
644 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700645 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700646
647 // All non-virtual displays are currently considered secure.
648 info->secure = true;
649
Mathias Agopian888c8222012-08-04 21:10:38 -0700650 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700651}
652
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800653// ----------------------------------------------------------------------------
654
655sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800656 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700657}
658
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800660
661void SurfaceFlinger::waitForEvent() {
662 mEventQueue.waitMessage();
663}
664
665void SurfaceFlinger::signalTransaction() {
666 mEventQueue.invalidate();
667}
668
669void SurfaceFlinger::signalLayerUpdate() {
670 mEventQueue.invalidate();
671}
672
673void SurfaceFlinger::signalRefresh() {
674 mEventQueue.refresh();
675}
676
677status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
678 nsecs_t reltime, uint32_t flags) {
679 return mEventQueue.postMessage(msg, reltime);
680}
681
682status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
683 nsecs_t reltime, uint32_t flags) {
684 status_t res = mEventQueue.postMessage(msg, reltime);
685 if (res == NO_ERROR) {
686 msg->wait();
687 }
688 return res;
689}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690
Mathias Agopian4fec8732012-06-29 14:12:52 -0700691bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 return true;
694}
695
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700696void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700697 if (mEventThread == NULL) {
698 // This is a temporary workaround for b/7145521. A non-null pointer
699 // does not mean EventThread has finished initializing, so this
700 // is not a correct fix.
701 ALOGW("WARNING: EventThread not started, ignoring vsync");
702 return;
703 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700704 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
705 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700706 mEventThread->onVSyncReceived(type, timestamp);
707 }
708}
709
710void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
711 if (mEventThread == NULL) {
712 // This is a temporary workaround for b/7145521. A non-null pointer
713 // does not mean EventThread has finished initializing, so this
714 // is not a correct fix.
715 ALOGW("WARNING: EventThread not started, ignoring hotplug");
716 return;
717 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700718
Mathias Agopian148994e2012-09-19 17:31:36 -0700719 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700720 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800721 if (connected) {
722 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700723 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800724 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
725 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700726 }
727 setTransactionFlags(eDisplayTransactionNeeded);
728
Andy McFadden9e9689c2012-10-10 18:17:51 -0700729 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700730 }
Mathias Agopian86303202012-07-24 22:46:10 -0700731}
732
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700733void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
734 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700735}
736
Mathias Agopian4fec8732012-06-29 14:12:52 -0700737void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800738 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800739 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700740 case MessageQueue::INVALIDATE:
741 handleMessageTransaction();
742 handleMessageInvalidate();
743 signalRefresh();
744 break;
745 case MessageQueue::REFRESH:
746 handleMessageRefresh();
747 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800748 }
749}
750
Mathias Agopian4fec8732012-06-29 14:12:52 -0700751void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700752 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700753 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700754 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700755 }
756}
757
758void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700759 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700760 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700761}
762
763void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764 ATRACE_CALL();
765 preComposition();
766 rebuildLayerStacks();
767 setUpHWComposer();
768 doDebugFlashRegions();
769 doComposition();
770 postComposition();
771}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700772
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773void SurfaceFlinger::doDebugFlashRegions()
774{
775 // is debugging enabled
776 if (CC_LIKELY(!mDebugRegion))
777 return;
778
779 const bool repaintEverything = mRepaintEverything;
780 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
781 const sp<DisplayDevice>& hw(mDisplays[dpy]);
782 if (hw->canDraw()) {
783 // transform the dirty region into this screen's coordinate space
784 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
785 if (!dirtyRegion.isEmpty()) {
786 // redraw the whole screen
787 doComposeSurfaces(hw, Region(hw->bounds()));
788
789 // and draw the dirty region
790 glDisable(GL_TEXTURE_EXTERNAL_OES);
791 glDisable(GL_TEXTURE_2D);
792 glDisable(GL_BLEND);
793 glColor4f(1, 0, 1, 1);
794 const int32_t height = hw->getHeight();
795 Region::const_iterator it = dirtyRegion.begin();
796 Region::const_iterator const end = dirtyRegion.end();
797 while (it != end) {
798 const Rect& r = *it++;
799 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800800 { (GLfloat) r.left, (GLfloat) (height - r.top) },
801 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
802 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
803 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804 };
805 glVertexPointer(2, GL_FLOAT, 0, vertices);
806 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
807 }
808 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700809 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700810 }
811 }
812 }
813
814 postFramebuffer();
815
816 if (mDebugRegion > 1) {
817 usleep(mDebugRegion * 1000);
818 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700819
820 HWComposer& hwc(getHwComposer());
821 if (hwc.initCheck() == NO_ERROR) {
822 status_t err = hwc.prepare();
823 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
824 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700825}
826
827void SurfaceFlinger::preComposition()
828{
829 bool needExtraInvalidate = false;
830 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
831 const size_t count = currentLayers.size();
832 for (size_t i=0 ; i<count ; i++) {
833 if (currentLayers[i]->onPreComposition()) {
834 needExtraInvalidate = true;
835 }
836 }
837 if (needExtraInvalidate) {
838 signalLayerUpdate();
839 }
840}
841
842void SurfaceFlinger::postComposition()
843{
844 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
845 const size_t count = currentLayers.size();
846 for (size_t i=0 ; i<count ; i++) {
847 currentLayers[i]->onPostComposition();
848 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800849
850 if (mAnimCompositionPending) {
851 mAnimCompositionPending = false;
852
853 const HWComposer& hwc = getHwComposer();
854 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800855 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800856 mAnimFrameTracker.setActualPresentFence(presentFence);
857 } else {
858 // The HWC doesn't support present fences, so use the refresh
859 // timestamp instead.
860 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
861 mAnimFrameTracker.setActualPresentTime(presentTime);
862 }
863 mAnimFrameTracker.advanceFrame();
864 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700865}
866
867void SurfaceFlinger::rebuildLayerStacks() {
868 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700869 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700870 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700871 mVisibleRegionsDirty = false;
872 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700873
Mathias Agopian87baae12012-07-31 12:38:26 -0700874 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700875 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700876 Region opaqueRegion;
877 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800878 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700879 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700880 const Transform& tr(hw->getTransform());
881 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700882 if (hw->canDraw()) {
883 SurfaceFlinger::computeVisibleRegions(currentLayers,
884 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700885
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700886 const size_t count = currentLayers.size();
887 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700889 const Layer::State& s(layer->drawingState());
890 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700891 Region drawRegion(tr.transform(
892 layer->visibleNonTransparentRegion));
893 drawRegion.andSelf(bounds);
894 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700895 layersSortedByZ.add(layer);
896 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700897 }
898 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700899 }
Mathias Agopian42977342012-08-05 00:40:46 -0700900 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700901 hw->undefinedRegion.set(bounds);
902 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
903 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700904 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700905 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700907
Mathias Agopiancd60f992012-08-16 16:28:27 -0700908void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700909 HWComposer& hwc(getHwComposer());
910 if (hwc.initCheck() == NO_ERROR) {
911 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700912 if (CC_UNLIKELY(mHwWorkListDirty)) {
913 mHwWorkListDirty = false;
914 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
915 sp<const DisplayDevice> hw(mDisplays[dpy]);
916 const int32_t id = hw->getHwcDisplayId();
917 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800918 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700919 hw->getVisibleLayersSortedByZ());
920 const size_t count = currentLayers.size();
921 if (hwc.createWorkList(id, count) == NO_ERROR) {
922 HWComposer::LayerListIterator cur = hwc.begin(id);
923 const HWComposer::LayerListIterator end = hwc.end(id);
924 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800925 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700926 layer->setGeometry(hw, *cur);
927 if (mDebugDisableHWC || mDebugRegion) {
928 cur->setSkip(true);
929 }
930 }
931 }
932 }
933 }
934 }
935
936 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700937 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700938 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700939 const int32_t id = hw->getHwcDisplayId();
940 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800941 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700942 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700943 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700944 HWComposer::LayerListIterator cur = hwc.begin(id);
945 const HWComposer::LayerListIterator end = hwc.end(id);
946 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
947 /*
948 * update the per-frame h/w composer data for each layer
949 * and build the transparent region of the FB
950 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800951 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700952 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700953 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700954 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700955 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700956
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700957 status_t err = hwc.prepare();
958 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
959 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961
Mathias Agopiancd60f992012-08-16 16:28:27 -0700962void SurfaceFlinger::doComposition() {
963 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700965 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700966 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700967 if (hw->canDraw()) {
968 // transform the dirty region into this screen's coordinate space
969 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800970
971 // repaint the framebuffer (if needed)
972 doDisplayComposition(hw, dirtyRegion);
973
Mathias Agopiancd60f992012-08-16 16:28:27 -0700974 hw->dirtyRegion.clear();
975 hw->flip(hw->swapRegion);
976 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700977 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700978 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700979 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700980 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700981 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700982}
983
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984void SurfaceFlinger::postFramebuffer()
985{
Mathias Agopian841cde52012-03-01 15:44:37 -0800986 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800987
Mathias Agopiana44b0412011-10-16 18:46:35 -0700988 const nsecs_t now = systemTime();
989 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700990
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700991 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700992 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700993 if (!hwc.supportsFramebufferTarget()) {
994 // EGL spec says:
995 // "surface must be bound to the calling thread's current context,
996 // for the current rendering API."
997 DisplayDevice::makeCurrent(mEGLDisplay,
998 getDefaultDisplayDevice(), mEGLContext);
999 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001000 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001001 }
1002
Mathias Agopian92a979a2012-08-02 18:32:23 -07001003 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001004 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001005 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001006 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001007 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001008 int32_t id = hw->getHwcDisplayId();
1009 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001010 HWComposer::LayerListIterator cur = hwc.begin(id);
1011 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001012 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001013 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001014 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001015 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001016 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001017 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 }
Jesse Hallef194142012-06-14 14:45:17 -07001019 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001020 }
1021
Mathias Agopiana44b0412011-10-16 18:46:35 -07001022 mLastSwapBufferTime = systemTime() - now;
1023 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024}
1025
Mathias Agopian87baae12012-07-31 12:38:26 -07001026void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027{
Mathias Agopian841cde52012-03-01 15:44:37 -08001028 ATRACE_CALL();
1029
Mathias Agopianca4d3602011-05-19 15:38:14 -07001030 Mutex::Autolock _l(mStateLock);
1031 const nsecs_t now = systemTime();
1032 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033
Mathias Agopianca4d3602011-05-19 15:38:14 -07001034 // Here we're guaranteed that some transaction flags are set
1035 // so we can call handleTransactionLocked() unconditionally.
1036 // We call getTransactionFlags(), which will also clear the flags,
1037 // with mStateLock held to guarantee that mCurrentState won't change
1038 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001039
Mathias Agopiane57f2922012-08-09 16:29:12 -07001040 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001041 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001042
Mathias Agopianca4d3602011-05-19 15:38:14 -07001043 mLastTransactionTime = systemTime() - now;
1044 mDebugInTransaction = 0;
1045 invalidateHwcGeometry();
1046 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001047}
1048
Mathias Agopian87baae12012-07-31 12:38:26 -07001049void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001050{
1051 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 const size_t count = currentLayers.size();
1053
1054 /*
1055 * Traversal of the children
1056 * (perform the transaction for each of them if needed)
1057 */
1058
Mathias Agopian3559b072012-08-15 13:46:03 -07001059 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1063 if (!trFlags) continue;
1064
1065 const uint32_t flags = layer->doTransaction(0);
1066 if (flags & Layer::eVisibleRegion)
1067 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 }
1069 }
1070
1071 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001072 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 */
1074
Mathias Agopiane57f2922012-08-09 16:29:12 -07001075 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001076 // here we take advantage of Vector's copy-on-write semantics to
1077 // improve performance by skipping the transaction entirely when
1078 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001079 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1080 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001081 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001084 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001085
1086 // find the displays that were removed
1087 // (ie: in drawing state but not in current state)
1088 // also handle displays that changed
1089 // (ie: displays that are in both lists)
1090 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1092 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001093 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001094 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001095 // Call makeCurrent() on the primary display so we can
1096 // be sure that nothing associated with this display
1097 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001098 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001099 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001100 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001101 getHwComposer().disconnectDisplay(draw[i].type);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001102 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001103 } else {
1104 ALOGW("trying to remove the main display");
1105 }
1106 } else {
1107 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001108 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001109 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001110 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001112 // recreating the DisplayDevice, so we just remove it
1113 // from the drawing state, so that it get re-added
1114 // below.
1115 mDisplays.removeItem(display);
1116 mDrawingState.displays.removeItemsAt(i);
1117 dc--; i--;
1118 // at this point we must loop to the next item
1119 continue;
1120 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001121
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001122 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001123 if (disp != NULL) {
1124 if (state.layerStack != draw[i].layerStack) {
1125 disp->setLayerStack(state.layerStack);
1126 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001127 if ((state.orientation != draw[i].orientation)
1128 || (state.viewport != draw[i].viewport)
1129 || (state.frame != draw[i].frame))
1130 {
1131 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001132 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001133 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001134 }
1135 }
1136 }
1137
1138 // find displays that were added
1139 // (ie: in current state but not in drawing state)
1140 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001141 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001142 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001143
1144 sp<FramebufferSurface> fbs;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001145 sp<Surface> stc;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001146 if (!state.isVirtualDisplay()) {
1147
1148 ALOGE_IF(state.surface!=NULL,
1149 "adding a supported display, but rendering "
1150 "surface is provided (%p), ignoring it",
1151 state.surface.get());
1152
1153 // for supported (by hwc) displays we provide our
1154 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001155 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001156 stc = new Surface(
Andy McFadden2adaf042012-12-18 09:49:45 -08001157 static_cast< sp<IGraphicBufferProducer> >(
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001158 fbs->getBufferQueue()));
Mathias Agopiancde87a32012-09-13 14:09:01 -07001159 } else {
1160 if (state.surface != NULL) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001161 stc = new Surface(state.surface);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001162 }
1163 }
1164
1165 const wp<IBinder>& display(curr.keyAt(i));
1166 if (stc != NULL) {
1167 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall692c7232012-11-08 15:41:56 -08001168 state.type, state.isSecure, display, stc, fbs,
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001169 mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001170 hw->setLayerStack(state.layerStack);
1171 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001172 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001173 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 mDisplays.add(display, hw);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001175 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001176 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001177 }
1178 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001180 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181
Mathias Agopian84300952012-11-21 16:02:13 -08001182 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1183 // The transform hint might have changed for some layers
1184 // (either because a display has changed, or because a layer
1185 // as changed).
1186 //
1187 // Walk through all the layers in currentLayers,
1188 // and update their transform hint.
1189 //
1190 // If a layer is visible only on a single display, then that
1191 // display is used to calculate the hint, otherwise we use the
1192 // default display.
1193 //
1194 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1195 // the hint is set before we acquire a buffer from the surface texture.
1196 //
1197 // NOTE: layer transactions have taken place already, so we use their
1198 // drawing state. However, SurfaceFlinger's own transaction has not
1199 // happened yet, so we must use the current state layer list
1200 // (soon to become the drawing state list).
1201 //
1202 sp<const DisplayDevice> disp;
1203 uint32_t currentlayerStack = 0;
1204 for (size_t i=0; i<count; i++) {
1205 // NOTE: we rely on the fact that layers are sorted by
1206 // layerStack first (so we don't have to traverse the list
1207 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001208 const sp<Layer>& layer(currentLayers[i]);
1209 uint32_t layerStack = layer->drawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001210 if (i==0 || currentlayerStack != layerStack) {
1211 currentlayerStack = layerStack;
1212 // figure out if this layerstack is mirrored
1213 // (more than one display) if so, pick the default display,
1214 // if not, pick the only display it's on.
1215 disp.clear();
1216 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1217 sp<const DisplayDevice> hw(mDisplays[dpy]);
1218 if (hw->getLayerStack() == currentlayerStack) {
1219 if (disp == NULL) {
1220 disp = hw;
1221 } else {
1222 disp = getDefaultDisplayDevice();
1223 break;
1224 }
1225 }
1226 }
1227 }
1228 if (disp != NULL) {
1229 // presumably this means this layer is using a layerStack
1230 // that is not visible on any display
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001232 }
1233 }
1234 }
1235
1236
Mathias Agopian3559b072012-08-15 13:46:03 -07001237 /*
1238 * Perform our own transaction if needed
1239 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001240
Mathias Agopiancd60f992012-08-16 16:28:27 -07001241 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1242 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001243 // layers have been added
1244 mVisibleRegionsDirty = true;
1245 }
1246
1247 // some layers might have been removed, so
1248 // we need to update the regions they're exposing.
1249 if (mLayersRemoved) {
1250 mLayersRemoved = false;
1251 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001252 const size_t count = previousLayers.size();
1253 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 const sp<Layer>& layer(previousLayers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001255 if (currentLayers.indexOf(layer) < 0) {
1256 // this layer is not visible anymore
1257 // TODO: we could traverse the tree from front to back and
1258 // compute the actual visible region
1259 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001260 const Layer::State& s(layer->drawingState());
1261 Region visibleReg = s.transform.transform(
1262 Region(Rect(s.active.w, s.active.h)));
1263 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001264 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 }
1267
1268 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001269}
1270
1271void SurfaceFlinger::commitTransaction()
1272{
1273 if (!mLayersPendingRemoval.isEmpty()) {
1274 // Notify removed layers now that they can't be drawn from
1275 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1276 mLayersPendingRemoval[i]->onRemoved();
1277 }
1278 mLayersPendingRemoval.clear();
1279 }
1280
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001281 // If this transaction is part of a window animation then the next frame
1282 // we composite should be considered an animation as well.
1283 mAnimCompositionPending = mAnimTransactionPending;
1284
Mathias Agopian4fec8732012-06-29 14:12:52 -07001285 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001286 mTransactionPending = false;
1287 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001288 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289}
1290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001292 const LayerVector& currentLayers, uint32_t layerStack,
1293 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294{
Mathias Agopian841cde52012-03-01 15:44:37 -08001295 ATRACE_CALL();
1296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 Region aboveOpaqueLayers;
1298 Region aboveCoveredLayers;
1299 Region dirty;
1300
Mathias Agopian87baae12012-07-31 12:38:26 -07001301 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302
1303 size_t i = currentLayers.size();
1304 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001305 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306
1307 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001308 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309
Jesse Hall01e29052013-02-19 16:13:35 -08001310 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001311 if (s.layerStack != layerStack)
1312 continue;
1313
Mathias Agopianab028732010-03-16 16:41:46 -07001314 /*
1315 * opaqueRegion: area of a surface that is fully opaque.
1316 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001318
1319 /*
1320 * visibleRegion: area of a surface that is visible on screen
1321 * and not fully transparent. This is essentially the layer's
1322 * footprint minus the opaque regions above it.
1323 * Areas covered by a translucent surface are considered visible.
1324 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001326
1327 /*
1328 * coveredRegion: area of a surface that is covered by all
1329 * visible regions above it (which includes the translucent areas).
1330 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001332
Jesse Halla8026d22012-09-25 13:25:04 -07001333 /*
1334 * transparentRegion: area of a surface that is hinted to be completely
1335 * transparent. This is only used to tell when the layer has no visible
1336 * non-transparent regions and can be removed from the layer list. It
1337 * does not affect the visibleRegion of this layer or any layers
1338 * beneath it. The hint may not be correct if apps don't respect the
1339 * SurfaceView restrictions (which, sadly, some don't).
1340 */
1341 Region transparentRegion;
1342
Mathias Agopianab028732010-03-16 16:41:46 -07001343
1344 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001345 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001346 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001347 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001349 if (!visibleRegion.isEmpty()) {
1350 // Remove the transparent area from the visible region
1351 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001352 const Transform tr(s.transform);
1353 if (tr.transformed()) {
1354 if (tr.preserveRects()) {
1355 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001356 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001357 } else {
1358 // transformation too complex, can't do the
1359 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001360 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001361 }
1362 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001363 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001364 }
Mathias Agopianab028732010-03-16 16:41:46 -07001365 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366
Mathias Agopianab028732010-03-16 16:41:46 -07001367 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001368 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001369 if (s.alpha==255 && !translucent &&
1370 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1371 // the opaque region is the layer's footprint
1372 opaqueRegion = visibleRegion;
1373 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 }
1375 }
1376
Mathias Agopianab028732010-03-16 16:41:46 -07001377 // Clip the covered region to the visible region
1378 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1379
1380 // Update aboveCoveredLayers for next (lower) layer
1381 aboveCoveredLayers.orSelf(visibleRegion);
1382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 // subtract the opaque region covered by the layers above us
1384 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385
1386 // compute this layer's dirty region
1387 if (layer->contentDirty) {
1388 // we need to invalidate the whole region
1389 dirty = visibleRegion;
1390 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001391 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 layer->contentDirty = false;
1393 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001394 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001395 * the exposed region consists of two components:
1396 * 1) what's VISIBLE now and was COVERED before
1397 * 2) what's EXPOSED now less what was EXPOSED before
1398 *
1399 * note that (1) is conservative, we start with the whole
1400 * visible region but only keep what used to be covered by
1401 * something -- which mean it may have been exposed.
1402 *
1403 * (2) handles areas that were not covered by anything but got
1404 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001405 */
Mathias Agopianab028732010-03-16 16:41:46 -07001406 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001407 const Region oldVisibleRegion = layer->visibleRegion;
1408 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001409 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1410 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 }
1412 dirty.subtractSelf(aboveOpaqueLayers);
1413
1414 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001415 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416
Mathias Agopianab028732010-03-16 16:41:46 -07001417 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001419
Jesse Halla8026d22012-09-25 13:25:04 -07001420 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 layer->setVisibleRegion(visibleRegion);
1422 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001423 layer->setVisibleNonTransparentRegion(
1424 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 }
1426
Mathias Agopian87baae12012-07-31 12:38:26 -07001427 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428}
1429
Mathias Agopian87baae12012-07-31 12:38:26 -07001430void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1431 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001432 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001433 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1434 if (hw->getLayerStack() == layerStack) {
1435 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001436 }
1437 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001438}
1439
1440void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443
Mathias Agopian4fec8732012-06-29 14:12:52 -07001444 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001445 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001446 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001448 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001449 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001450 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001451 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001452 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001453
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001454 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455}
1456
Mathias Agopianad456f92011-01-13 17:53:01 -08001457void SurfaceFlinger::invalidateHwcGeometry()
1458{
1459 mHwWorkListDirty = true;
1460}
1461
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001462
Mathias Agopiancd60f992012-08-16 16:28:27 -07001463void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001464 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465{
Mathias Agopian87baae12012-07-31 12:38:26 -07001466 Region dirtyRegion(inDirtyRegion);
1467
Mathias Agopianb8a55602009-06-26 19:06:36 -07001468 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001469 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470
Mathias Agopian42977342012-08-05 00:40:46 -07001471 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001472 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001473 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1474 // takes a rectangle, we must make sure to update that whole
1475 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001476 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001477 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001478 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001479 // We need to redraw the rectangle that will be updated
1480 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001481 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001482 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001483 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001484 } else {
1485 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001486 dirtyRegion.set(hw->bounds());
1487 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 }
1489 }
1490
Mathias Agopiancd60f992012-08-16 16:28:27 -07001491 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001493 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001494 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001495
1496 // swap buffers (presentation)
1497 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498}
1499
Mathias Agopiancd60f992012-08-16 16:28:27 -07001500void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001501{
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 Agopianda8d0a52012-09-04 15:05:38 -07001509 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001510
1511 // set the frame buffer
1512 glMatrixMode(GL_MODELVIEW);
1513 glLoadIdentity();
1514
1515 // 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
1521 // GPUs doing a "clean slate" glClear might be more efficient.
1522 // We'll revisit later if needed.
1523 glClearColor(0, 0, 0, 0);
1524 glClear(GL_COLOR_BUFFER_BIT);
1525 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001526 // we start with the whole screen area
1527 const Region bounds(hw->getBounds());
1528
1529 // we remove the scissor part
1530 // we're left with the letterbox region
1531 // (common case is that letterbox ends-up being empty)
1532 const Region letterbox(bounds.subtract(hw->getScissor()));
1533
1534 // compute the area to clear
1535 Region region(hw->undefinedRegion.merge(letterbox));
1536
1537 // but limit it to the dirty region
1538 region.andSelf(dirty);
1539
Mathias Agopianb9494d52012-04-18 02:28:45 -07001540 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001541 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001542 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001543 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001544 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001545 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001546
Mathias Agopian766dc492012-10-30 18:08:06 -07001547 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1548 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001549 // scissor on the main display. It should never be needed
1550 // anyways (though in theory it could since the API allows it).
1551 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001552 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001553 if (scissor != bounds) {
1554 // scissor doesn't match the screen's dimensions, so we
1555 // need to clear everything outside of it and enable
1556 // the GL scissor so we don't draw anything where we shouldn't
1557 const GLint height = hw->getHeight();
1558 glScissor(scissor.left, height - scissor.bottom,
1559 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001560 // enable scissor for this frame
1561 glEnable(GL_SCISSOR_TEST);
1562 }
1563 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001564 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001565
Mathias Agopian85d751c2012-08-29 16:59:24 -07001566 /*
1567 * and then, render the layers targeted at the framebuffer
1568 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001569
Mathias Agopian13127d82013-03-05 17:47:11 -08001570 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001571 const size_t count = layers.size();
1572 const Transform& tr = hw->getTransform();
1573 if (cur != end) {
1574 // we're using h/w composer
1575 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001576 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001577 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001578 if (!clip.isEmpty()) {
1579 switch (cur->getCompositionType()) {
1580 case HWC_OVERLAY: {
1581 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1582 && i
1583 && layer->isOpaque()
1584 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001585 // never clear the very first layer since we're
1586 // guaranteed the FB is already cleared
1587 layer->clearWithOpenGL(hw, clip);
1588 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001589 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001590 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001591 case HWC_FRAMEBUFFER: {
1592 layer->draw(hw, clip);
1593 break;
1594 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001595 case HWC_FRAMEBUFFER_TARGET: {
1596 // this should not happen as the iterator shouldn't
1597 // let us get there.
1598 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1599 break;
1600 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001601 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001602 }
1603 layer->setAcquireFence(hw, *cur);
1604 }
1605 } else {
1606 // we're not using h/w composer
1607 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001608 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001609 const Region clip(dirty.intersect(
1610 tr.transform(layer->visibleRegion)));
1611 if (!clip.isEmpty()) {
1612 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001613 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001614 }
1615 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001616
1617 // disable scissor at the end of the frame
1618 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619}
1620
Mathias Agopian55801e42012-08-27 18:54:24 -07001621void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1622 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001624 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001625 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001626 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001627 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001628
Mathias Agopian55801e42012-08-27 18:54:24 -07001629 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001630 Region::const_iterator it = region.begin();
1631 Region::const_iterator const end = region.end();
1632 while (it != end) {
1633 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001634 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001635 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1636 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1637 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1638 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001639 };
1640 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001641 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1642 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643}
1644
Mathias Agopianac9fa422013-02-11 16:40:36 -08001645void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1646 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001647 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001648 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001649{
Mathias Agopian96f08192010-06-02 23:28:45 -07001650 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001651 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001652
Mathias Agopian96f08192010-06-02 23:28:45 -07001653 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001654 Mutex::Autolock _l(mStateLock);
1655 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001656 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001657}
1658
Mathias Agopian13127d82013-03-05 17:47:11 -08001659status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660{
1661 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001662 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001664 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001665 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001666 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 return NO_ERROR;
1668 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001669 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670}
1671
Mathias Agopiandea20b12011-05-03 17:04:02 -07001672uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1673{
1674 return android_atomic_release_load(&mTransactionFlags);
1675}
1676
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1678{
1679 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1680}
1681
Mathias Agopianbb641242010-05-18 17:06:55 -07001682uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683{
1684 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1685 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001686 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 }
1688 return old;
1689}
1690
Mathias Agopian8b33f032012-07-24 20:43:54 -07001691void SurfaceFlinger::setTransactionState(
1692 const Vector<ComposerState>& state,
1693 const Vector<DisplayState>& displays,
1694 uint32_t flags)
1695{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001696 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001697 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001698 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001699
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001700 if (flags & eAnimation) {
1701 // For window updates that are part of an animation we must wait for
1702 // previous animation "frames" to be handled.
1703 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001704 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001705 if (CC_UNLIKELY(err != NO_ERROR)) {
1706 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001707 // caller after a few seconds.
1708 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1709 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001710 mAnimTransactionPending = false;
1711 break;
1712 }
1713 }
1714 }
1715
Mathias Agopiane57f2922012-08-09 16:29:12 -07001716 size_t count = displays.size();
1717 for (size_t i=0 ; i<count ; i++) {
1718 const DisplayState& s(displays[i]);
1719 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001720 }
1721
Mathias Agopiane57f2922012-08-09 16:29:12 -07001722 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001723 for (size_t i=0 ; i<count ; i++) {
1724 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001725 // Here we need to check that the interface we're given is indeed
1726 // one of our own. A malicious client could give us a NULL
1727 // IInterface, or one of its own or even one of our own but a
1728 // different type. All these situations would cause us to crash.
1729 //
1730 // NOTE: it would be better to use RTTI as we could directly check
1731 // that we have a Client*. however, RTTI is disabled in Android.
1732 if (s.client != NULL) {
1733 sp<IBinder> binder = s.client->asBinder();
1734 if (binder != NULL) {
1735 String16 desc(binder->getInterfaceDescriptor());
1736 if (desc == ISurfaceComposerClient::descriptor) {
1737 sp<Client> client( static_cast<Client *>(s.client.get()) );
1738 transactionFlags |= setClientStateLocked(client, s.state);
1739 }
1740 }
1741 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001742 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001743
Mathias Agopian386aa982011-11-07 21:58:03 -08001744 if (transactionFlags) {
1745 // this triggers the transaction
1746 setTransactionFlags(transactionFlags);
1747
1748 // if this is a synchronous transaction, wait for it to take effect
1749 // before returning.
1750 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001751 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001752 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001753 if (flags & eAnimation) {
1754 mAnimTransactionPending = true;
1755 }
1756 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001757 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1758 if (CC_UNLIKELY(err != NO_ERROR)) {
1759 // just in case something goes wrong in SF, return to the
1760 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001761 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1762 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001763 break;
1764 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001765 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 }
1767}
1768
Mathias Agopiane57f2922012-08-09 16:29:12 -07001769uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1770{
Jesse Hall9a143922012-10-04 16:29:19 -07001771 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1772 if (dpyIdx < 0)
1773 return 0;
1774
Mathias Agopiane57f2922012-08-09 16:29:12 -07001775 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001776 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001777 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001778 const uint32_t what = s.what;
1779 if (what & DisplayState::eSurfaceChanged) {
1780 if (disp.surface->asBinder() != s.surface->asBinder()) {
1781 disp.surface = s.surface;
1782 flags |= eDisplayTransactionNeeded;
1783 }
1784 }
1785 if (what & DisplayState::eLayerStackChanged) {
1786 if (disp.layerStack != s.layerStack) {
1787 disp.layerStack = s.layerStack;
1788 flags |= eDisplayTransactionNeeded;
1789 }
1790 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001791 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001792 if (disp.orientation != s.orientation) {
1793 disp.orientation = s.orientation;
1794 flags |= eDisplayTransactionNeeded;
1795 }
1796 if (disp.frame != s.frame) {
1797 disp.frame = s.frame;
1798 flags |= eDisplayTransactionNeeded;
1799 }
1800 if (disp.viewport != s.viewport) {
1801 disp.viewport = s.viewport;
1802 flags |= eDisplayTransactionNeeded;
1803 }
1804 }
1805 }
1806 return flags;
1807}
1808
1809uint32_t SurfaceFlinger::setClientStateLocked(
1810 const sp<Client>& client,
1811 const layer_state_t& s)
1812{
1813 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001814 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001815 if (layer != 0) {
1816 const uint32_t what = s.what;
1817 if (what & layer_state_t::ePositionChanged) {
1818 if (layer->setPosition(s.x, s.y))
1819 flags |= eTraversalNeeded;
1820 }
1821 if (what & layer_state_t::eLayerChanged) {
1822 // NOTE: index needs to be calculated before we update the state
1823 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1824 if (layer->setLayer(s.z)) {
1825 mCurrentState.layersSortedByZ.removeAt(idx);
1826 mCurrentState.layersSortedByZ.add(layer);
1827 // we need traversal (state changed)
1828 // AND transaction (list changed)
1829 flags |= eTransactionNeeded|eTraversalNeeded;
1830 }
1831 }
1832 if (what & layer_state_t::eSizeChanged) {
1833 if (layer->setSize(s.w, s.h)) {
1834 flags |= eTraversalNeeded;
1835 }
1836 }
1837 if (what & layer_state_t::eAlphaChanged) {
1838 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1839 flags |= eTraversalNeeded;
1840 }
1841 if (what & layer_state_t::eMatrixChanged) {
1842 if (layer->setMatrix(s.matrix))
1843 flags |= eTraversalNeeded;
1844 }
1845 if (what & layer_state_t::eTransparentRegionChanged) {
1846 if (layer->setTransparentRegionHint(s.transparentRegion))
1847 flags |= eTraversalNeeded;
1848 }
1849 if (what & layer_state_t::eVisibilityChanged) {
1850 if (layer->setFlags(s.flags, s.mask))
1851 flags |= eTraversalNeeded;
1852 }
1853 if (what & layer_state_t::eCropChanged) {
1854 if (layer->setCrop(s.crop))
1855 flags |= eTraversalNeeded;
1856 }
1857 if (what & layer_state_t::eLayerStackChanged) {
1858 // NOTE: index needs to be calculated before we update the state
1859 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1860 if (layer->setLayerStack(s.layerStack)) {
1861 mCurrentState.layersSortedByZ.removeAt(idx);
1862 mCurrentState.layersSortedByZ.add(layer);
1863 // we need traversal (state changed)
1864 // AND transaction (list changed)
1865 flags |= eTransactionNeeded|eTraversalNeeded;
1866 }
1867 }
1868 }
1869 return flags;
1870}
1871
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001873 const String8& name,
1874 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001875 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1876 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001878 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001879 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001880 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001881 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001882 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001883 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001884
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001885 status_t result = NO_ERROR;
1886
1887 sp<Layer> layer;
1888
Mathias Agopian3165cc22012-08-08 19:42:09 -07001889 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1890 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001891 result = createNormalLayer(client,
1892 name, w, h, flags, format,
1893 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001895 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001896 result = createDimLayer(client,
1897 name, w, h, flags,
1898 handle, gbp, &layer);
1899 break;
1900 default:
1901 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 break;
1903 }
1904
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001906 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001907 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001909 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910}
1911
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001912status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1913 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1914 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915{
1916 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001917 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 case PIXEL_FORMAT_TRANSPARENT:
1919 case PIXEL_FORMAT_TRANSLUCENT:
1920 format = PIXEL_FORMAT_RGBA_8888;
1921 break;
1922 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001923#ifdef NO_RGBX_8888
1924 format = PIXEL_FORMAT_RGB_565;
1925#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001926 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001927#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 break;
1929 }
1930
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001931#ifdef NO_RGBX_8888
1932 if (format == PIXEL_FORMAT_RGBX_8888)
1933 format = PIXEL_FORMAT_RGBA_8888;
1934#endif
1935
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001936 *outLayer = new Layer(this, client, name, w, h, flags);
1937 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1938 if (err == NO_ERROR) {
1939 *handle = (*outLayer)->getHandle();
1940 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001942
1943 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1944 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945}
1946
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001947status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1948 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1949 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001951 *outLayer = new LayerDim(this, client, name, w, h, flags);
1952 *handle = (*outLayer)->getHandle();
1953 *gbp = (*outLayer)->getBufferQueue();
1954 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001955}
1956
Mathias Agopianac9fa422013-02-11 16:40:36 -08001957status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958{
Mathias Agopian67106042013-03-14 19:18:13 -07001959 // called by the window manager when it wants to remove a Layer
1960 status_t err = NO_ERROR;
1961 sp<Layer> l(client->getLayerUser(handle));
1962 if (l != NULL) {
1963 err = removeLayer(l);
1964 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1965 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001966 }
1967 return err;
1968}
1969
Mathias Agopian13127d82013-03-05 17:47:11 -08001970status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001971{
Mathias Agopian67106042013-03-14 19:18:13 -07001972 // called by ~LayerCleaner() when all references to the IBinder (handle)
1973 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001974 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001975 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001976 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001977 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001978 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001979 "error removing layer=%p (%s)", l.get(), strerror(-err));
1980 }
1981 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982}
1983
Mathias Agopianb60314a2012-04-10 22:09:54 -07001984// ---------------------------------------------------------------------------
1985
Andy McFadden13a082e2012-08-24 10:16:42 -07001986void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001987 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001988 Vector<ComposerState> state;
1989 Vector<DisplayState> displays;
1990 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001991 d.what = DisplayState::eDisplayProjectionChanged |
1992 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001993 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001994 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001995 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001996 d.frame.makeInvalid();
1997 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001998 displays.add(d);
1999 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002000 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002001}
2002
2003void SurfaceFlinger::initializeDisplays() {
2004 class MessageScreenInitialized : public MessageBase {
2005 SurfaceFlinger* flinger;
2006 public:
2007 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2008 virtual bool handler() {
2009 flinger->onInitializeDisplays();
2010 return true;
2011 }
2012 };
2013 sp<MessageBase> msg = new MessageScreenInitialized(this);
2014 postMessageAsync(msg); // we may be called from main thread, use async message
2015}
2016
2017
Mathias Agopiancde87a32012-09-13 14:09:01 -07002018void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002019 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2020 if (hw->isScreenAcquired()) {
2021 // this is expected, e.g. when power manager wakes up during boot
2022 ALOGD(" screen was previously acquired");
2023 return;
2024 }
2025
Mathias Agopian42977342012-08-05 00:40:46 -07002026 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002027 int32_t type = hw->getDisplayType();
2028 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2029 // built-in display, tell the HWC
2030 getHwComposer().acquire(type);
2031
2032 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2033 // FIXME: eventthread only knows about the main display right now
2034 mEventThread->onScreenAcquired();
2035 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002036 }
Mathias Agopian20128302012-08-13 18:32:13 -07002037 mVisibleRegionsDirty = true;
2038 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039}
2040
Mathias Agopiancde87a32012-09-13 14:09:01 -07002041void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002042 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2043 if (!hw->isScreenAcquired()) {
2044 ALOGD(" screen was previously released");
2045 return;
2046 }
2047
2048 hw->releaseScreen();
2049 int32_t type = hw->getDisplayType();
2050 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2051 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002052 // FIXME: eventthread only knows about the main display right now
2053 mEventThread->onScreenReleased();
2054 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002055
2056 // built-in display, tell the HWC
2057 getHwComposer().release(type);
2058 }
2059 mVisibleRegionsDirty = true;
2060 // from this point on, SF will stop drawing on this display
2061}
2062
2063void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2064 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002065 SurfaceFlinger& mFlinger;
2066 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002067 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002068 MessageScreenAcquired(SurfaceFlinger& flinger,
2069 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002070 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002071 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2072 if (hw == NULL) {
2073 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2074 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2075 ALOGW("Attempt to unblank virtual display");
2076 } else {
2077 mFlinger.onScreenAcquired(hw);
2078 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002079 return true;
2080 }
2081 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002082 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2083 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084}
2085
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002086void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002087 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002088 SurfaceFlinger& mFlinger;
2089 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002090 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002091 MessageScreenReleased(SurfaceFlinger& flinger,
2092 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002093 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002094 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2095 if (hw == NULL) {
2096 ALOGE("Attempt to blank null display %p", mDisplay.get());
2097 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2098 ALOGW("Attempt to blank virtual display");
2099 } else {
2100 mFlinger.onScreenReleased(hw);
2101 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002102 return true;
2103 }
2104 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002105 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2106 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002107}
2108
2109// ---------------------------------------------------------------------------
2110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2112{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002113 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114 char buffer[SIZE];
2115 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002116
2117 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002118 snprintf(buffer, SIZE, "Permission Denial: "
2119 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2120 IPCThreadState::self()->getCallingPid(),
2121 IPCThreadState::self()->getCallingUid());
2122 result.append(buffer);
2123 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002124 // Try to get the main lock, but don't insist if we can't
2125 // (this would indicate SF is stuck, but we want to be able to
2126 // print something in dumpsys).
2127 int retry = 3;
2128 while (mStateLock.tryLock()<0 && --retry>=0) {
2129 usleep(1000000);
2130 }
2131 const bool locked(retry >= 0);
2132 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002133 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002134 "SurfaceFlinger appears to be unresponsive, "
2135 "dumping anyways (no locks held)\n");
2136 result.append(buffer);
2137 }
2138
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002139 bool dumpAll = true;
2140 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002141 size_t numArgs = args.size();
2142 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002143 if ((index < numArgs) &&
2144 (args[index] == String16("--list"))) {
2145 index++;
2146 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002147 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002148 }
2149
2150 if ((index < numArgs) &&
2151 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002152 index++;
2153 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002154 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002155 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002156
2157 if ((index < numArgs) &&
2158 (args[index] == String16("--latency-clear"))) {
2159 index++;
2160 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002161 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002163 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002164
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002165 if (dumpAll) {
2166 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002167 }
2168
Mathias Agopian9795c422009-08-26 16:36:26 -07002169 if (locked) {
2170 mStateLock.unlock();
2171 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002172 }
2173 write(fd, result.string(), result.size());
2174 return NO_ERROR;
2175}
2176
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002177void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2178 String8& result, char* buffer, size_t SIZE) const
2179{
2180 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2181 const size_t count = currentLayers.size();
2182 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002183 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002184 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2185 result.append(buffer);
2186 }
2187}
2188
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002189void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2190 String8& result, char* buffer, size_t SIZE) const
2191{
2192 String8 name;
2193 if (index < args.size()) {
2194 name = String8(args[index]);
2195 index++;
2196 }
2197
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002198 const nsecs_t period =
2199 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2200 result.appendFormat("%lld\n", period);
2201
2202 if (name.isEmpty()) {
2203 mAnimFrameTracker.dump(result);
2204 } else {
2205 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2206 const size_t count = currentLayers.size();
2207 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002208 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002209 if (name == layer->getName()) {
2210 layer->dumpStats(result, buffer, SIZE);
2211 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002212 }
2213 }
2214}
2215
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002216void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002217 String8& result, char* buffer, size_t SIZE)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002218{
2219 String8 name;
2220 if (index < args.size()) {
2221 name = String8(args[index]);
2222 index++;
2223 }
2224
2225 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2226 const size_t count = currentLayers.size();
2227 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002228 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002229 if (name.isEmpty() || (name == layer->getName())) {
2230 layer->clearStats();
2231 }
2232 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002233
2234 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002235}
2236
Andy McFadden4803b742012-09-24 19:07:20 -07002237/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2238{
2239 static const char* config =
2240 " [sf"
2241#ifdef NO_RGBX_8888
2242 " NO_RGBX_8888"
2243#endif
2244#ifdef HAS_CONTEXT_PRIORITY
2245 " HAS_CONTEXT_PRIORITY"
2246#endif
2247#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2248 " NEVER_DEFAULT_TO_ASYNC_MODE"
2249#endif
2250#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2251 " TARGET_DISABLE_TRIPLE_BUFFERING"
2252#endif
2253 "]";
2254 result.append(config);
2255}
2256
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002257void SurfaceFlinger::dumpAllLocked(
2258 String8& result, char* buffer, size_t SIZE) const
2259{
2260 // 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 */
2270 result.append("Build configuration:");
2271 appendSfConfigString(result);
2272 appendUiConfigString(result);
2273 appendGuiConfigString(result);
2274 result.append("\n");
2275
2276 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002277 * Dump the visible layer list
2278 */
2279 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2280 const size_t count = currentLayers.size();
2281 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2282 result.append(buffer);
2283 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002284 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002285 layer->dump(result, buffer, SIZE);
2286 }
2287
2288 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002289 * Dump Display state
2290 */
2291
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002292 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2293 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002294 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2295 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002296 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002297 }
2298
2299 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002300 * Dump SurfaceFlinger global state
2301 */
2302
2303 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2304 result.append(buffer);
2305
Mathias Agopian888c8222012-08-04 21:10:38 -07002306 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002307 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002308 const GLExtensions& extensions(GLExtensions::getInstance());
2309 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2310 extensions.getVendor(),
2311 extensions.getRenderer(),
2312 extensions.getVersion());
2313 result.append(buffer);
2314
2315 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002316 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002317 result.append(buffer);
2318
2319 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2320 result.append(buffer);
2321
Mathias Agopian42977342012-08-05 00:40:46 -07002322 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002323 snprintf(buffer, SIZE,
2324 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002325 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326 result.append(buffer);
2327 snprintf(buffer, SIZE,
2328 " last eglSwapBuffers() time: %f us\n"
2329 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002330 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002331 " refresh-rate : %f fps\n"
2332 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002333 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002334 mLastSwapBufferTime/1000.0,
2335 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002336 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002337 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2338 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2339 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002340 result.append(buffer);
2341
2342 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2343 inSwapBuffersDuration/1000.0);
2344 result.append(buffer);
2345
2346 snprintf(buffer, SIZE, " transaction time: %f us\n",
2347 inTransactionDuration/1000.0);
2348 result.append(buffer);
2349
2350 /*
2351 * VSYNC state
2352 */
2353 mEventThread->dump(result, buffer, SIZE);
2354
2355 /*
2356 * Dump HWComposer state
2357 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358 snprintf(buffer, SIZE, "h/w composer state:\n");
2359 result.append(buffer);
2360 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2361 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2362 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2363 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002364 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002365
2366 /*
2367 * Dump gralloc state
2368 */
2369 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2370 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002371}
2372
Mathias Agopian13127d82013-03-05 17:47:11 -08002373const Vector< sp<Layer> >&
Mathias Agopiancb558572012-10-04 15:58:54 -07002374SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002375 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002376 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2377}
2378
Keun young Park63f165f2012-08-31 10:53:36 -07002379bool SurfaceFlinger::startDdmConnection()
2380{
2381 void* libddmconnection_dso =
2382 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2383 if (!libddmconnection_dso) {
2384 return false;
2385 }
2386 void (*DdmConnection_start)(const char* name);
2387 DdmConnection_start =
2388 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2389 if (!DdmConnection_start) {
2390 dlclose(libddmconnection_dso);
2391 return false;
2392 }
2393 (*DdmConnection_start)(getServiceName());
2394 return true;
2395}
2396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002397status_t SurfaceFlinger::onTransact(
2398 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2399{
2400 switch (code) {
2401 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002402 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002403 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002404 case BLANK:
2405 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002406 {
2407 // codes that require permission check
2408 IPCThreadState* ipc = IPCThreadState::self();
2409 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002410 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002411 if ((uid != AID_GRAPHICS) &&
2412 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002413 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002414 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2415 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002416 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002417 break;
2418 }
2419 case CAPTURE_SCREEN:
2420 {
2421 // codes that require permission check
2422 IPCThreadState* ipc = IPCThreadState::self();
2423 const int pid = ipc->getCallingPid();
2424 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002425 if ((uid != AID_GRAPHICS) &&
2426 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002427 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002428 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2429 return PERMISSION_DENIED;
2430 }
2431 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002432 }
2433 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002435 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2436 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002437 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002438 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002439 IPCThreadState* ipc = IPCThreadState::self();
2440 const int pid = ipc->getCallingPid();
2441 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002442 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002443 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444 return PERMISSION_DENIED;
2445 }
2446 int n;
2447 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002448 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002449 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002450 return NO_ERROR;
2451 case 1002: // SHOW_UPDATES
2452 n = data.readInt32();
2453 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002454 invalidateHwcGeometry();
2455 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002458 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002459 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002460 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002461 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002462 setTransactionFlags(
2463 eTransactionNeeded|
2464 eDisplayTransactionNeeded|
2465 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002466 return NO_ERROR;
2467 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002468 case 1006:{ // send empty update
2469 signalRefresh();
2470 return NO_ERROR;
2471 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002472 case 1008: // toggle use of hw composer
2473 n = data.readInt32();
2474 mDebugDisableHWC = n ? 1 : 0;
2475 invalidateHwcGeometry();
2476 repaintEverything();
2477 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002478 case 1009: // toggle use of transform hint
2479 n = data.readInt32();
2480 mDebugDisableTransformHint = n ? 1 : 0;
2481 invalidateHwcGeometry();
2482 repaintEverything();
2483 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002485 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486 reply->writeInt32(0);
2487 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002488 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002489 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490 return NO_ERROR;
2491 case 1013: {
2492 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002493 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2494 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495 }
2496 return NO_ERROR;
2497 }
2498 }
2499 return err;
2500}
2501
Mathias Agopian53331da2011-08-22 21:44:41 -07002502void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002503 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002504 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002505}
2506
Mathias Agopian59119e62010-10-11 12:37:43 -07002507// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002508// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002509// ---------------------------------------------------------------------------
2510
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002511status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2512 const sp<IGraphicBufferProducer>& producer,
2513 uint32_t reqWidth, uint32_t reqHeight,
2514 uint32_t minLayerZ, uint32_t maxLayerZ) {
2515
2516 if (CC_UNLIKELY(display == 0))
2517 return BAD_VALUE;
2518
2519 if (CC_UNLIKELY(producer == 0))
2520 return BAD_VALUE;
2521
2522 class MessageCaptureScreen : public MessageBase {
2523 SurfaceFlinger* flinger;
2524 sp<IBinder> display;
2525 sp<IGraphicBufferProducer> producer;
2526 uint32_t reqWidth, reqHeight;
2527 uint32_t minLayerZ,maxLayerZ;
2528 status_t result;
2529 public:
2530 MessageCaptureScreen(SurfaceFlinger* flinger,
2531 const sp<IBinder>& display,
2532 const sp<IGraphicBufferProducer>& producer,
2533 uint32_t reqWidth, uint32_t reqHeight,
2534 uint32_t minLayerZ, uint32_t maxLayerZ)
2535 : flinger(flinger), display(display), producer(producer),
2536 reqWidth(reqWidth), reqHeight(reqHeight),
2537 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2538 result(PERMISSION_DENIED)
2539 {
2540 }
2541 status_t getResult() const {
2542 return result;
2543 }
2544 virtual bool handler() {
2545 Mutex::Autolock _l(flinger->mStateLock);
2546 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
2547 result = flinger->captureScreenImplLocked(hw, producer,
2548 reqWidth, reqHeight, minLayerZ, maxLayerZ);
2549 return true;
2550 }
2551 };
2552
2553 sp<MessageBase> msg = new MessageCaptureScreen(this,
2554 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2555 status_t res = postMessageSync(msg);
2556 if (res == NO_ERROR) {
2557 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2558 }
2559 return res;
2560}
2561
2562status_t SurfaceFlinger::captureScreenImplLocked(
2563 const sp<const DisplayDevice>& hw,
2564 const sp<IGraphicBufferProducer>& producer,
2565 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002566 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002567{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002568 ATRACE_CALL();
2569
Mathias Agopian74c40c02010-09-29 13:02:36 -07002570 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002571 const uint32_t hw_w = hw->getWidth();
2572 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002573
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002574 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002575 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002576 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002577 return PERMISSION_DENIED;
2578 }
2579
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002580 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2581 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2582 reqWidth, reqHeight, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002583 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002584 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002585
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002586 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2587 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2588 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002589
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002590 // Create a surface to render into
2591 sp<Surface> surface = new Surface(producer);
2592 ANativeWindow* const window = surface.get();
2593
2594 // set the buffer size to what the user requested
2595 native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
2596
2597 // and create the corresponding EGLSurface
2598 EGLSurface eglSurface = eglCreateWindowSurface(
2599 mEGLDisplay, mEGLConfig, window, NULL);
2600 if (eglSurface == EGL_NO_SURFACE) {
2601 ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
2602 eglGetError());
2603 return BAD_VALUE;
2604 }
2605
2606 if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
2607 ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
2608 eglGetError());
2609 eglDestroySurface(mEGLDisplay, eglSurface);
2610 return BAD_VALUE;
2611 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002612
Mathias Agopian74c40c02010-09-29 13:02:36 -07002613 // make sure to clear all GL error flags
2614 while ( glGetError() != GL_NO_ERROR ) ;
2615
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002616 // set-up our viewport
2617 glViewport(0, 0, reqWidth, reqHeight);
2618 glMatrixMode(GL_PROJECTION);
2619 glLoadIdentity();
2620 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2621 glMatrixMode(GL_MODELVIEW);
2622 glLoadIdentity();
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002623
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002624 // redraw the screen entirely...
2625 glDisable(GL_TEXTURE_EXTERNAL_OES);
2626 glDisable(GL_TEXTURE_2D);
2627 glClearColor(0,0,0,1);
2628 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002629
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002630 const LayerVector& layers( mDrawingState.layersSortedByZ );
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002631 const size_t count = layers.size();
2632 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002633 const sp<Layer>& layer(layers[i]);
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002634 const Layer::State& state(layer->drawingState());
2635 if (state.layerStack == hw->getLayerStack()) {
2636 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2637 if (filtering) layer->setFiltering(true);
2638 layer->draw(hw);
2639 if (filtering) layer->setFiltering(false);
2640 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002641 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002642 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002643
Mathias Agopiand5776412013-03-08 13:54:46 -08002644 // compositionComplete is needed for older driver
2645 hw->compositionComplete();
2646
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002647 // and finishing things up...
2648 if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
2649 ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
2650 eglGetError());
2651 eglDestroySurface(mEGLDisplay, eglSurface);
2652 return BAD_VALUE;
2653 }
2654
2655 eglDestroySurface(mEGLDisplay, eglSurface);
2656 return NO_ERROR;
2657}
2658
2659// ---------------------------------------------------------------------------
2660// Capture screen into an IMemoryHeap (legacy)
2661// ---------------------------------------------------------------------------
2662
2663status_t SurfaceFlinger::captureScreenImplLocked(
2664 const sp<const DisplayDevice>& hw,
2665 sp<IMemoryHeap>* heap,
2666 uint32_t* w, uint32_t* h, PixelFormat* f,
2667 uint32_t sw, uint32_t sh,
2668 uint32_t minLayerZ, uint32_t maxLayerZ)
2669{
2670 ATRACE_CALL();
2671
2672 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2673 return INVALID_OPERATION;
2674 }
2675
2676 // create the texture that will receive the screenshot, later we'll
2677 // attach a FBO to it so we can call glReadPixels().
2678 GLuint tname;
2679 glGenTextures(1, &tname);
2680 glBindTexture(GL_TEXTURE_2D, tname);
2681 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2682 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2683
2684 // the GLConsumer will provide the BufferQueue
2685 sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
2686 consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
2687
2688 // call the new screenshot taking code, passing a BufferQueue to it
2689 status_t result = captureScreenImplLocked(hw,
2690 consumer->getBufferQueue(), sw, sh, minLayerZ, maxLayerZ);
2691
2692 if (result == NO_ERROR) {
2693 result = consumer->updateTexImage();
2694 if (result == NO_ERROR) {
2695 // create a FBO
2696 GLuint name;
2697 glGenFramebuffersOES(1, &name);
2698 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2699 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2700 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
2701
2702 sp<GraphicBuffer> buf(consumer->getCurrentBuffer());
2703 sw = buf->getWidth();
2704 sh = buf->getHeight();
Mathias Agopian6a531712013-03-13 15:22:11 -07002705 size_t size = sw * sh * 4;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002706
Mathias Agopian74c40c02010-09-29 13:02:36 -07002707 // allocate shared memory large enough to hold the
2708 // screen capture
2709 sp<MemoryHeapBase> base(
2710 new MemoryHeapBase(size, 0, "screen-capture") );
2711 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002712 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002713 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002714 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002715 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2716 if (glGetError() == GL_NO_ERROR) {
2717 *heap = base;
2718 *w = sw;
2719 *h = sh;
2720 *f = PIXEL_FORMAT_RGBA_8888;
2721 result = NO_ERROR;
Jeff Sharkey175264b2013-03-05 17:57:26 -08002722 } else {
2723 result = NO_MEMORY;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002724 }
2725 } else {
2726 result = NO_MEMORY;
2727 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002728
2729 // back to main framebuffer
2730 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2731 glDeleteFramebuffersOES(1, &name);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002732 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002733 }
2734
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002735 glDeleteTextures(1, &tname);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002736
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002737 DisplayDevice::makeCurrent(mEGLDisplay,
2738 getDefaultDisplayDevice(), mEGLContext);
2739
Mathias Agopian74c40c02010-09-29 13:02:36 -07002740 return result;
2741}
2742
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002743status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002744 sp<IMemoryHeap>* heap,
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002745 uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
2746 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002747 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002748{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002749 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002750 return BAD_VALUE;
2751
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002752 class MessageCaptureScreen : public MessageBase {
2753 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002754 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002755 sp<IMemoryHeap>* heap;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002756 uint32_t* outWidth;
2757 uint32_t* outHeight;
2758 PixelFormat* outFormat;
2759 uint32_t reqWidth;
2760 uint32_t reqHeight;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002761 uint32_t minLayerZ;
2762 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002763 status_t result;
2764 public:
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002765 MessageCaptureScreen(SurfaceFlinger* flinger,
2766 const sp<IBinder>& display, sp<IMemoryHeap>* heap,
2767 uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
2768 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002769 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002770 : flinger(flinger), display(display), heap(heap),
2771 outWidth(outWidth), outHeight(outHeight), outFormat(outFormat),
2772 reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002773 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2774 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002775 {
2776 }
2777 status_t getResult() const {
2778 return result;
2779 }
2780 virtual bool handler() {
2781 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002782 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
2783 result = flinger->captureScreenImplLocked(hw, heap,
2784 outWidth, outHeight, outFormat,
2785 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002786 return true;
2787 }
2788 };
2789
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002790 sp<MessageBase> msg = new MessageCaptureScreen(this, display, heap,
2791 outWidth, outHeight, outFormat,
2792 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002793 status_t res = postMessageSync(msg);
2794 if (res == NO_ERROR) {
2795 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2796 }
2797 return res;
2798}
2799
2800// ---------------------------------------------------------------------------
2801
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002802SurfaceFlinger::LayerVector::LayerVector() {
2803}
2804
2805SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002806 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002807}
2808
2809int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2810 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002811{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002812 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002813 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2814 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002815
2816 uint32_t ls = l->currentState().layerStack;
2817 uint32_t rs = r->currentState().layerStack;
2818 if (ls != rs)
2819 return ls - rs;
2820
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002821 uint32_t lz = l->currentState().z;
2822 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002823 if (lz != rz)
2824 return lz - rz;
2825
2826 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002827}
2828
2829// ---------------------------------------------------------------------------
2830
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002831SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2832 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002833}
2834
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002835SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002836 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002837 viewport.makeInvalid();
2838 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002839}
2840
2841// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002842
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002843}; // namespace android