blob: 551f9023ee2d556afdd32bda5958065b226eed2a [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());
Jamie Gennis134f0422011-03-08 12:18:54 -0800577
Andy McFadden882e3a32013-01-08 16:06:15 -0800578 // We want to determine whether the IGraphicBufferProducer was created by
579 // SurfaceFlinger. Check to see if we can find it in the layer list.
Jamie Gennis134f0422011-03-08 12:18:54 -0800580 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
581 size_t count = currentLayers.size();
582 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800583 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700584 // Get the consumer interface of SurfaceFlingerConsumer's
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800585 // BufferQueue. If it's the same Binder object as the graphic
586 // buffer producer interface, return success.
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700587 sp<IBinder> lbcBinder = layer->getBufferQueue()->asBinder();
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800588 if (lbcBinder == surfaceTextureBinder) {
589 return true;
Jamie Gennis134f0422011-03-08 12:18:54 -0800590 }
591 }
592
593 // Check the layers in the purgatory. This check is here so that if a
Andy McFadden2adaf042012-12-18 09:49:45 -0800594 // GLConsumer gets destroyed before all the clients are done using it,
Jamie Gennis582270d2011-08-17 18:19:00 -0700595 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800596 // will instead fail later on when the client tries to use the surface,
597 // which should be reported as "surface XYZ returned an -ENODEV". The
598 // purgatorized layers are no less authentic than the visible ones, so this
599 // should not cause any harm.
600 size_t purgatorySize = mLayerPurgatory.size();
601 for (size_t i=0 ; i<purgatorySize ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800602 const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700603 sp<IBinder> lbcBinder = layer->getBufferQueue()->asBinder();
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800604 if (lbcBinder == surfaceTextureBinder) {
605 return true;
Jamie Gennis134f0422011-03-08 12:18:54 -0800606 }
607 }
608
609 return false;
610}
611
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700612status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800613 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700614 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800615 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700616 type = i;
617 break;
618 }
619 }
620
621 if (type < 0) {
622 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700623 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700624
625 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700626 float xdpi = hwc.getDpiX(type);
627 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700628
629 // TODO: Not sure if display density should handled by SF any longer
630 class Density {
631 static int getDensityFromProperty(char const* propName) {
632 char property[PROPERTY_VALUE_MAX];
633 int density = 0;
634 if (property_get(propName, property, NULL) > 0) {
635 density = atoi(property);
636 }
637 return density;
638 }
639 public:
640 static int getEmuDensity() {
641 return getDensityFromProperty("qemu.sf.lcd_density"); }
642 static int getBuildDensity() {
643 return getDensityFromProperty("ro.sf.lcd_density"); }
644 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700645
646 if (type == DisplayDevice::DISPLAY_PRIMARY) {
647 // The density of the device is provided by a build property
648 float density = Density::getBuildDensity() / 160.0f;
649 if (density == 0) {
650 // the build doesn't provide a density -- this is wrong!
651 // use xdpi instead
652 ALOGE("ro.sf.lcd_density must be defined as a build property");
653 density = xdpi / 160.0f;
654 }
655 if (Density::getEmuDensity()) {
656 // if "qemu.sf.lcd_density" is specified, it overrides everything
657 xdpi = ydpi = density = Density::getEmuDensity();
658 density /= 160.0f;
659 }
660 info->density = density;
661
662 // TODO: this needs to go away (currently needed only by webkit)
663 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
664 info->orientation = hw->getOrientation();
665 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
666 } else {
667 // TODO: where should this value come from?
668 static const int TV_DENSITY = 213;
669 info->density = TV_DENSITY / 160.0f;
670 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700671 }
672
Mathias Agopian1604f772012-09-18 21:54:42 -0700673 info->w = hwc.getWidth(type);
674 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700675 info->xdpi = xdpi;
676 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700677 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700678
679 // All non-virtual displays are currently considered secure.
680 info->secure = true;
681
Mathias Agopian888c8222012-08-04 21:10:38 -0700682 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700683}
684
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800685// ----------------------------------------------------------------------------
686
687sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800688 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700689}
690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800692
693void SurfaceFlinger::waitForEvent() {
694 mEventQueue.waitMessage();
695}
696
697void SurfaceFlinger::signalTransaction() {
698 mEventQueue.invalidate();
699}
700
701void SurfaceFlinger::signalLayerUpdate() {
702 mEventQueue.invalidate();
703}
704
705void SurfaceFlinger::signalRefresh() {
706 mEventQueue.refresh();
707}
708
709status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
710 nsecs_t reltime, uint32_t flags) {
711 return mEventQueue.postMessage(msg, reltime);
712}
713
714status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
715 nsecs_t reltime, uint32_t flags) {
716 status_t res = mEventQueue.postMessage(msg, reltime);
717 if (res == NO_ERROR) {
718 msg->wait();
719 }
720 return res;
721}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722
Mathias Agopian4fec8732012-06-29 14:12:52 -0700723bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 return true;
726}
727
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700728void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700729 if (mEventThread == NULL) {
730 // This is a temporary workaround for b/7145521. A non-null pointer
731 // does not mean EventThread has finished initializing, so this
732 // is not a correct fix.
733 ALOGW("WARNING: EventThread not started, ignoring vsync");
734 return;
735 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700736 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
737 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700738 mEventThread->onVSyncReceived(type, timestamp);
739 }
740}
741
742void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
743 if (mEventThread == NULL) {
744 // This is a temporary workaround for b/7145521. A non-null pointer
745 // does not mean EventThread has finished initializing, so this
746 // is not a correct fix.
747 ALOGW("WARNING: EventThread not started, ignoring hotplug");
748 return;
749 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700750
Mathias Agopian148994e2012-09-19 17:31:36 -0700751 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700752 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800753 if (connected) {
754 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700755 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800756 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
757 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700758 }
759 setTransactionFlags(eDisplayTransactionNeeded);
760
Andy McFadden9e9689c2012-10-10 18:17:51 -0700761 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700762 }
Mathias Agopian86303202012-07-24 22:46:10 -0700763}
764
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700765void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
766 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700767}
768
Mathias Agopian4fec8732012-06-29 14:12:52 -0700769void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800770 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800771 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700772 case MessageQueue::INVALIDATE:
773 handleMessageTransaction();
774 handleMessageInvalidate();
775 signalRefresh();
776 break;
777 case MessageQueue::REFRESH:
778 handleMessageRefresh();
779 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800780 }
781}
782
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700784 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700785 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700786 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700787 }
788}
789
790void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700791 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700792 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700793}
794
795void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700796 ATRACE_CALL();
797 preComposition();
798 rebuildLayerStacks();
799 setUpHWComposer();
800 doDebugFlashRegions();
801 doComposition();
802 postComposition();
803}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700804
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805void SurfaceFlinger::doDebugFlashRegions()
806{
807 // is debugging enabled
808 if (CC_LIKELY(!mDebugRegion))
809 return;
810
811 const bool repaintEverything = mRepaintEverything;
812 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
813 const sp<DisplayDevice>& hw(mDisplays[dpy]);
814 if (hw->canDraw()) {
815 // transform the dirty region into this screen's coordinate space
816 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
817 if (!dirtyRegion.isEmpty()) {
818 // redraw the whole screen
819 doComposeSurfaces(hw, Region(hw->bounds()));
820
821 // and draw the dirty region
822 glDisable(GL_TEXTURE_EXTERNAL_OES);
823 glDisable(GL_TEXTURE_2D);
824 glDisable(GL_BLEND);
825 glColor4f(1, 0, 1, 1);
826 const int32_t height = hw->getHeight();
827 Region::const_iterator it = dirtyRegion.begin();
828 Region::const_iterator const end = dirtyRegion.end();
829 while (it != end) {
830 const Rect& r = *it++;
831 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800832 { (GLfloat) r.left, (GLfloat) (height - r.top) },
833 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
834 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
835 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700836 };
837 glVertexPointer(2, GL_FLOAT, 0, vertices);
838 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
839 }
840 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700841 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842 }
843 }
844 }
845
846 postFramebuffer();
847
848 if (mDebugRegion > 1) {
849 usleep(mDebugRegion * 1000);
850 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700851
852 HWComposer& hwc(getHwComposer());
853 if (hwc.initCheck() == NO_ERROR) {
854 status_t err = hwc.prepare();
855 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
856 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857}
858
859void SurfaceFlinger::preComposition()
860{
861 bool needExtraInvalidate = false;
862 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
863 const size_t count = currentLayers.size();
864 for (size_t i=0 ; i<count ; i++) {
865 if (currentLayers[i]->onPreComposition()) {
866 needExtraInvalidate = true;
867 }
868 }
869 if (needExtraInvalidate) {
870 signalLayerUpdate();
871 }
872}
873
874void SurfaceFlinger::postComposition()
875{
876 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
877 const size_t count = currentLayers.size();
878 for (size_t i=0 ; i<count ; i++) {
879 currentLayers[i]->onPostComposition();
880 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800881
882 if (mAnimCompositionPending) {
883 mAnimCompositionPending = false;
884
885 const HWComposer& hwc = getHwComposer();
886 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800887 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800888 mAnimFrameTracker.setActualPresentFence(presentFence);
889 } else {
890 // The HWC doesn't support present fences, so use the refresh
891 // timestamp instead.
892 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
893 mAnimFrameTracker.setActualPresentTime(presentTime);
894 }
895 mAnimFrameTracker.advanceFrame();
896 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700897}
898
899void SurfaceFlinger::rebuildLayerStacks() {
900 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700901 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700902 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700903 mVisibleRegionsDirty = false;
904 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700905
Mathias Agopian87baae12012-07-31 12:38:26 -0700906 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700907 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700908 Region opaqueRegion;
909 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800910 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700911 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700912 const Transform& tr(hw->getTransform());
913 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700914 if (hw->canDraw()) {
915 SurfaceFlinger::computeVisibleRegions(currentLayers,
916 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700917
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700918 const size_t count = currentLayers.size();
919 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800920 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700921 const Layer::State& s(layer->drawingState());
922 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700923 Region drawRegion(tr.transform(
924 layer->visibleNonTransparentRegion));
925 drawRegion.andSelf(bounds);
926 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700927 layersSortedByZ.add(layer);
928 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700929 }
930 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700931 }
Mathias Agopian42977342012-08-05 00:40:46 -0700932 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700933 hw->undefinedRegion.set(bounds);
934 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
935 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700936 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700937 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700938}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700939
Mathias Agopiancd60f992012-08-16 16:28:27 -0700940void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700941 HWComposer& hwc(getHwComposer());
942 if (hwc.initCheck() == NO_ERROR) {
943 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700944 if (CC_UNLIKELY(mHwWorkListDirty)) {
945 mHwWorkListDirty = false;
946 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
947 sp<const DisplayDevice> hw(mDisplays[dpy]);
948 const int32_t id = hw->getHwcDisplayId();
949 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800950 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700951 hw->getVisibleLayersSortedByZ());
952 const size_t count = currentLayers.size();
953 if (hwc.createWorkList(id, count) == NO_ERROR) {
954 HWComposer::LayerListIterator cur = hwc.begin(id);
955 const HWComposer::LayerListIterator end = hwc.end(id);
956 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800957 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700958 layer->setGeometry(hw, *cur);
959 if (mDebugDisableHWC || mDebugRegion) {
960 cur->setSkip(true);
961 }
962 }
963 }
964 }
965 }
966 }
967
968 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700970 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700971 const int32_t id = hw->getHwcDisplayId();
972 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700974 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700975 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700976 HWComposer::LayerListIterator cur = hwc.begin(id);
977 const HWComposer::LayerListIterator end = hwc.end(id);
978 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
979 /*
980 * update the per-frame h/w composer data for each layer
981 * and build the transparent region of the FB
982 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700984 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700985 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700986 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700987 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700988
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700989 status_t err = hwc.prepare();
990 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
991 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700992}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700993
Mathias Agopiancd60f992012-08-16 16:28:27 -0700994void SurfaceFlinger::doComposition() {
995 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700996 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700997 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700998 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700999 if (hw->canDraw()) {
1000 // transform the dirty region into this screen's coordinate space
1001 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001002
1003 // repaint the framebuffer (if needed)
1004 doDisplayComposition(hw, dirtyRegion);
1005
Mathias Agopiancd60f992012-08-16 16:28:27 -07001006 hw->dirtyRegion.clear();
1007 hw->flip(hw->swapRegion);
1008 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001009 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001010 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001011 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001012 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001013 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001014}
1015
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016void SurfaceFlinger::postFramebuffer()
1017{
Mathias Agopian841cde52012-03-01 15:44:37 -08001018 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001019
Mathias Agopiana44b0412011-10-16 18:46:35 -07001020 const nsecs_t now = systemTime();
1021 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001022
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001023 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001024 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001025 if (!hwc.supportsFramebufferTarget()) {
1026 // EGL spec says:
1027 // "surface must be bound to the calling thread's current context,
1028 // for the current rendering API."
1029 DisplayDevice::makeCurrent(mEGLDisplay,
1030 getDefaultDisplayDevice(), mEGLContext);
1031 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001032 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001033 }
1034
Mathias Agopian92a979a2012-08-02 18:32:23 -07001035 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001036 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001038 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001039 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001040 int32_t id = hw->getHwcDisplayId();
1041 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001042 HWComposer::LayerListIterator cur = hwc.begin(id);
1043 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001044 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001045 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001046 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001047 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001048 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001049 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001050 }
Jesse Hallef194142012-06-14 14:45:17 -07001051 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001052 }
1053
Mathias Agopiana44b0412011-10-16 18:46:35 -07001054 mLastSwapBufferTime = systemTime() - now;
1055 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
Mathias Agopian87baae12012-07-31 12:38:26 -07001058void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059{
Mathias Agopian841cde52012-03-01 15:44:37 -08001060 ATRACE_CALL();
1061
Mathias Agopianca4d3602011-05-19 15:38:14 -07001062 Mutex::Autolock _l(mStateLock);
1063 const nsecs_t now = systemTime();
1064 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065
Mathias Agopianca4d3602011-05-19 15:38:14 -07001066 // Here we're guaranteed that some transaction flags are set
1067 // so we can call handleTransactionLocked() unconditionally.
1068 // We call getTransactionFlags(), which will also clear the flags,
1069 // with mStateLock held to guarantee that mCurrentState won't change
1070 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001071
Mathias Agopiane57f2922012-08-09 16:29:12 -07001072 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001073 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001074
Mathias Agopianca4d3602011-05-19 15:38:14 -07001075 mLastTransactionTime = systemTime() - now;
1076 mDebugInTransaction = 0;
1077 invalidateHwcGeometry();
1078 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001079}
1080
Mathias Agopian87baae12012-07-31 12:38:26 -07001081void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001082{
1083 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 const size_t count = currentLayers.size();
1085
1086 /*
1087 * Traversal of the children
1088 * (perform the transaction for each of them if needed)
1089 */
1090
Mathias Agopian3559b072012-08-15 13:46:03 -07001091 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1095 if (!trFlags) continue;
1096
1097 const uint32_t flags = layer->doTransaction(0);
1098 if (flags & Layer::eVisibleRegion)
1099 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 }
1101 }
1102
1103 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001104 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 */
1106
Mathias Agopiane57f2922012-08-09 16:29:12 -07001107 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001108 // here we take advantage of Vector's copy-on-write semantics to
1109 // improve performance by skipping the transaction entirely when
1110 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1112 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001113 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001115 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001116 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001117
1118 // find the displays that were removed
1119 // (ie: in drawing state but not in current state)
1120 // also handle displays that changed
1121 // (ie: displays that are in both lists)
1122 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001123 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1124 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001125 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001126 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001127 // Call makeCurrent() on the primary display so we can
1128 // be sure that nothing associated with this display
1129 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001130 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001131 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001132 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001133 getHwComposer().disconnectDisplay(draw[i].type);
Jesse Hall7adb0f82013-03-06 16:13:49 -08001134 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1135 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001136 } else {
1137 ALOGW("trying to remove the main display");
1138 }
1139 } else {
1140 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001141 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001142 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001143 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001144 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001145 // recreating the DisplayDevice, so we just remove it
1146 // from the drawing state, so that it get re-added
1147 // below.
1148 mDisplays.removeItem(display);
1149 mDrawingState.displays.removeItemsAt(i);
1150 dc--; i--;
1151 // at this point we must loop to the next item
1152 continue;
1153 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001154
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001155 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001156 if (disp != NULL) {
1157 if (state.layerStack != draw[i].layerStack) {
1158 disp->setLayerStack(state.layerStack);
1159 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001160 if ((state.orientation != draw[i].orientation)
1161 || (state.viewport != draw[i].viewport)
1162 || (state.frame != draw[i].frame))
1163 {
1164 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001165 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001166 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001167 }
1168 }
1169 }
1170
1171 // find displays that were added
1172 // (ie: in current state but not in drawing state)
1173 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001174 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001175 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001176
1177 sp<FramebufferSurface> fbs;
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001178 sp<Surface> stc;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001179 if (!state.isVirtualDisplay()) {
1180
1181 ALOGE_IF(state.surface!=NULL,
1182 "adding a supported display, but rendering "
1183 "surface is provided (%p), ignoring it",
1184 state.surface.get());
1185
1186 // for supported (by hwc) displays we provide our
1187 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001188 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001189 stc = new Surface(
Andy McFadden2adaf042012-12-18 09:49:45 -08001190 static_cast< sp<IGraphicBufferProducer> >(
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001191 fbs->getBufferQueue()));
Mathias Agopiancde87a32012-09-13 14:09:01 -07001192 } else {
1193 if (state.surface != NULL) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -08001194 stc = new Surface(state.surface);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001195 }
1196 }
1197
1198 const wp<IBinder>& display(curr.keyAt(i));
1199 if (stc != NULL) {
1200 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall692c7232012-11-08 15:41:56 -08001201 state.type, state.isSecure, display, stc, fbs,
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001202 mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001203 hw->setLayerStack(state.layerStack);
1204 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001205 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001206 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001207 mDisplays.add(display, hw);
Jesse Hall7adb0f82013-03-06 16:13:49 -08001208 if (state.type < DisplayDevice::NUM_DISPLAY_TYPES)
1209 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001210 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001211 }
1212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215
Mathias Agopian84300952012-11-21 16:02:13 -08001216 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1217 // The transform hint might have changed for some layers
1218 // (either because a display has changed, or because a layer
1219 // as changed).
1220 //
1221 // Walk through all the layers in currentLayers,
1222 // and update their transform hint.
1223 //
1224 // If a layer is visible only on a single display, then that
1225 // display is used to calculate the hint, otherwise we use the
1226 // default display.
1227 //
1228 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1229 // the hint is set before we acquire a buffer from the surface texture.
1230 //
1231 // NOTE: layer transactions have taken place already, so we use their
1232 // drawing state. However, SurfaceFlinger's own transaction has not
1233 // happened yet, so we must use the current state layer list
1234 // (soon to become the drawing state list).
1235 //
1236 sp<const DisplayDevice> disp;
1237 uint32_t currentlayerStack = 0;
1238 for (size_t i=0; i<count; i++) {
1239 // NOTE: we rely on the fact that layers are sorted by
1240 // layerStack first (so we don't have to traverse the list
1241 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 const sp<Layer>& layer(currentLayers[i]);
1243 uint32_t layerStack = layer->drawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001244 if (i==0 || currentlayerStack != layerStack) {
1245 currentlayerStack = layerStack;
1246 // figure out if this layerstack is mirrored
1247 // (more than one display) if so, pick the default display,
1248 // if not, pick the only display it's on.
1249 disp.clear();
1250 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1251 sp<const DisplayDevice> hw(mDisplays[dpy]);
1252 if (hw->getLayerStack() == currentlayerStack) {
1253 if (disp == NULL) {
1254 disp = hw;
1255 } else {
1256 disp = getDefaultDisplayDevice();
1257 break;
1258 }
1259 }
1260 }
1261 }
1262 if (disp != NULL) {
1263 // presumably this means this layer is using a layerStack
1264 // that is not visible on any display
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001266 }
1267 }
1268 }
1269
1270
Mathias Agopian3559b072012-08-15 13:46:03 -07001271 /*
1272 * Perform our own transaction if needed
1273 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001274
Mathias Agopiancd60f992012-08-16 16:28:27 -07001275 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1276 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001277 // layers have been added
1278 mVisibleRegionsDirty = true;
1279 }
1280
1281 // some layers might have been removed, so
1282 // we need to update the regions they're exposing.
1283 if (mLayersRemoved) {
1284 mLayersRemoved = false;
1285 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001286 const size_t count = previousLayers.size();
1287 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001288 const sp<Layer>& layer(previousLayers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001289 if (currentLayers.indexOf(layer) < 0) {
1290 // this layer is not visible anymore
1291 // TODO: we could traverse the tree from front to back and
1292 // compute the actual visible region
1293 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001294 const Layer::State& s(layer->drawingState());
1295 Region visibleReg = s.transform.transform(
1296 Region(Rect(s.active.w, s.active.h)));
1297 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001298 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 }
1301
1302 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001303}
1304
1305void SurfaceFlinger::commitTransaction()
1306{
1307 if (!mLayersPendingRemoval.isEmpty()) {
1308 // Notify removed layers now that they can't be drawn from
1309 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1310 mLayersPendingRemoval[i]->onRemoved();
1311 }
1312 mLayersPendingRemoval.clear();
1313 }
1314
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001315 // If this transaction is part of a window animation then the next frame
1316 // we composite should be considered an animation as well.
1317 mAnimCompositionPending = mAnimTransactionPending;
1318
Mathias Agopian4fec8732012-06-29 14:12:52 -07001319 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001320 mTransactionPending = false;
1321 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323}
1324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001326 const LayerVector& currentLayers, uint32_t layerStack,
1327 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
Mathias Agopian841cde52012-03-01 15:44:37 -08001329 ATRACE_CALL();
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 Region aboveOpaqueLayers;
1332 Region aboveCoveredLayers;
1333 Region dirty;
1334
Mathias Agopian87baae12012-07-31 12:38:26 -07001335 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336
1337 size_t i = currentLayers.size();
1338 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001339 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340
1341 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001342 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343
Jesse Hall01e29052013-02-19 16:13:35 -08001344 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001345 if (s.layerStack != layerStack)
1346 continue;
1347
Mathias Agopianab028732010-03-16 16:41:46 -07001348 /*
1349 * opaqueRegion: area of a surface that is fully opaque.
1350 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001352
1353 /*
1354 * visibleRegion: area of a surface that is visible on screen
1355 * and not fully transparent. This is essentially the layer's
1356 * footprint minus the opaque regions above it.
1357 * Areas covered by a translucent surface are considered visible.
1358 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001360
1361 /*
1362 * coveredRegion: area of a surface that is covered by all
1363 * visible regions above it (which includes the translucent areas).
1364 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001366
Jesse Halla8026d22012-09-25 13:25:04 -07001367 /*
1368 * transparentRegion: area of a surface that is hinted to be completely
1369 * transparent. This is only used to tell when the layer has no visible
1370 * non-transparent regions and can be removed from the layer list. It
1371 * does not affect the visibleRegion of this layer or any layers
1372 * beneath it. The hint may not be correct if apps don't respect the
1373 * SurfaceView restrictions (which, sadly, some don't).
1374 */
1375 Region transparentRegion;
1376
Mathias Agopianab028732010-03-16 16:41:46 -07001377
1378 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001379 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001380 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001381 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001383 if (!visibleRegion.isEmpty()) {
1384 // Remove the transparent area from the visible region
1385 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001386 const Transform tr(s.transform);
1387 if (tr.transformed()) {
1388 if (tr.preserveRects()) {
1389 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001390 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001391 } else {
1392 // transformation too complex, can't do the
1393 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001394 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001395 }
1396 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001397 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 }
Mathias Agopianab028732010-03-16 16:41:46 -07001399 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400
Mathias Agopianab028732010-03-16 16:41:46 -07001401 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001402 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001403 if (s.alpha==255 && !translucent &&
1404 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1405 // the opaque region is the layer's footprint
1406 opaqueRegion = visibleRegion;
1407 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 }
1409 }
1410
Mathias Agopianab028732010-03-16 16:41:46 -07001411 // Clip the covered region to the visible region
1412 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1413
1414 // Update aboveCoveredLayers for next (lower) layer
1415 aboveCoveredLayers.orSelf(visibleRegion);
1416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 // subtract the opaque region covered by the layers above us
1418 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419
1420 // compute this layer's dirty region
1421 if (layer->contentDirty) {
1422 // we need to invalidate the whole region
1423 dirty = visibleRegion;
1424 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001425 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 layer->contentDirty = false;
1427 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001428 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001429 * the exposed region consists of two components:
1430 * 1) what's VISIBLE now and was COVERED before
1431 * 2) what's EXPOSED now less what was EXPOSED before
1432 *
1433 * note that (1) is conservative, we start with the whole
1434 * visible region but only keep what used to be covered by
1435 * something -- which mean it may have been exposed.
1436 *
1437 * (2) handles areas that were not covered by anything but got
1438 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001439 */
Mathias Agopianab028732010-03-16 16:41:46 -07001440 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001441 const Region oldVisibleRegion = layer->visibleRegion;
1442 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001443 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1444 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 }
1446 dirty.subtractSelf(aboveOpaqueLayers);
1447
1448 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001449 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450
Mathias Agopianab028732010-03-16 16:41:46 -07001451 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001453
Jesse Halla8026d22012-09-25 13:25:04 -07001454 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 layer->setVisibleRegion(visibleRegion);
1456 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001457 layer->setVisibleNonTransparentRegion(
1458 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 }
1460
Mathias Agopian87baae12012-07-31 12:38:26 -07001461 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462}
1463
Mathias Agopian87baae12012-07-31 12:38:26 -07001464void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1465 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001466 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001467 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1468 if (hw->getLayerStack() == layerStack) {
1469 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001470 }
1471 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001472}
1473
1474void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001476 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477
Mathias Agopian4fec8732012-06-29 14:12:52 -07001478 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001479 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001480 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001481 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001482 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001483 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001484 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001485 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001486 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001487
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001488 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489}
1490
Mathias Agopianad456f92011-01-13 17:53:01 -08001491void SurfaceFlinger::invalidateHwcGeometry()
1492{
1493 mHwWorkListDirty = true;
1494}
1495
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001496
Mathias Agopiancd60f992012-08-16 16:28:27 -07001497void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001498 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499{
Mathias Agopian87baae12012-07-31 12:38:26 -07001500 Region dirtyRegion(inDirtyRegion);
1501
Mathias Agopianb8a55602009-06-26 19:06:36 -07001502 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001503 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504
Mathias Agopian42977342012-08-05 00:40:46 -07001505 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001506 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001507 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1508 // takes a rectangle, we must make sure to update that whole
1509 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001510 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001511 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001512 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001513 // We need to redraw the rectangle that will be updated
1514 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001515 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001516 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001517 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001518 } else {
1519 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001520 dirtyRegion.set(hw->bounds());
1521 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 }
1523 }
1524
Mathias Agopiancd60f992012-08-16 16:28:27 -07001525 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001527 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001528 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001529
1530 // swap buffers (presentation)
1531 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532}
1533
Mathias Agopiancd60f992012-08-16 16:28:27 -07001534void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001535{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001536 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001537 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001538 HWComposer::LayerListIterator cur = hwc.begin(id);
1539 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001540
Mathias Agopian85d751c2012-08-29 16:59:24 -07001541 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1542 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001543 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001544
1545 // set the frame buffer
1546 glMatrixMode(GL_MODELVIEW);
1547 glLoadIdentity();
1548
1549 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001550 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001551 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001552 // when using overlays, we assume a fully transparent framebuffer
1553 // NOTE: we could reduce how much we need to clear, for instance
1554 // remove where there are opaque FB layers. however, on some
1555 // GPUs doing a "clean slate" glClear might be more efficient.
1556 // We'll revisit later if needed.
1557 glClearColor(0, 0, 0, 0);
1558 glClear(GL_COLOR_BUFFER_BIT);
1559 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001560 // we start with the whole screen area
1561 const Region bounds(hw->getBounds());
1562
1563 // we remove the scissor part
1564 // we're left with the letterbox region
1565 // (common case is that letterbox ends-up being empty)
1566 const Region letterbox(bounds.subtract(hw->getScissor()));
1567
1568 // compute the area to clear
1569 Region region(hw->undefinedRegion.merge(letterbox));
1570
1571 // but limit it to the dirty region
1572 region.andSelf(dirty);
1573
Mathias Agopianb9494d52012-04-18 02:28:45 -07001574 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001575 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001576 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001577 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001578 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001579 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001580
Mathias Agopian766dc492012-10-30 18:08:06 -07001581 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1582 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001583 // scissor on the main display. It should never be needed
1584 // anyways (though in theory it could since the API allows it).
1585 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001586 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001587 if (scissor != bounds) {
1588 // scissor doesn't match the screen's dimensions, so we
1589 // need to clear everything outside of it and enable
1590 // the GL scissor so we don't draw anything where we shouldn't
1591 const GLint height = hw->getHeight();
1592 glScissor(scissor.left, height - scissor.bottom,
1593 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001594 // enable scissor for this frame
1595 glEnable(GL_SCISSOR_TEST);
1596 }
1597 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001598 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001599
Mathias Agopian85d751c2012-08-29 16:59:24 -07001600 /*
1601 * and then, render the layers targeted at the framebuffer
1602 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001603
Mathias Agopian13127d82013-03-05 17:47:11 -08001604 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001605 const size_t count = layers.size();
1606 const Transform& tr = hw->getTransform();
1607 if (cur != end) {
1608 // we're using h/w composer
1609 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001610 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001611 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001612 if (!clip.isEmpty()) {
1613 switch (cur->getCompositionType()) {
1614 case HWC_OVERLAY: {
1615 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1616 && i
1617 && layer->isOpaque()
1618 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001619 // never clear the very first layer since we're
1620 // guaranteed the FB is already cleared
1621 layer->clearWithOpenGL(hw, clip);
1622 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001623 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001624 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001625 case HWC_FRAMEBUFFER: {
1626 layer->draw(hw, clip);
1627 break;
1628 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001629 case HWC_FRAMEBUFFER_TARGET: {
1630 // this should not happen as the iterator shouldn't
1631 // let us get there.
1632 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1633 break;
1634 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001635 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001636 }
1637 layer->setAcquireFence(hw, *cur);
1638 }
1639 } else {
1640 // we're not using h/w composer
1641 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001642 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001643 const Region clip(dirty.intersect(
1644 tr.transform(layer->visibleRegion)));
1645 if (!clip.isEmpty()) {
1646 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001647 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001648 }
1649 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001650
1651 // disable scissor at the end of the frame
1652 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653}
1654
Mathias Agopian55801e42012-08-27 18:54:24 -07001655void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1656 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001658 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001659 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001660 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001661 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001662
Mathias Agopian55801e42012-08-27 18:54:24 -07001663 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001664 Region::const_iterator it = region.begin();
1665 Region::const_iterator const end = region.end();
1666 while (it != end) {
1667 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001668 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001669 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1670 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1671 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1672 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001673 };
1674 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001675 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1676 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677}
1678
Mathias Agopianac9fa422013-02-11 16:40:36 -08001679void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1680 const sp<IBinder>& handle,
Mathias Agopian13127d82013-03-05 17:47:11 -08001681 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001682{
Mathias Agopian96f08192010-06-02 23:28:45 -07001683 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001684 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001685
Mathias Agopian96f08192010-06-02 23:28:45 -07001686 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001687 Mutex::Autolock _l(mStateLock);
1688 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001689}
1690
Mathias Agopian13127d82013-03-05 17:47:11 -08001691status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692{
1693 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001694 status_t err = purgatorizeLayer_l(layer);
1695 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001696 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001697 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698}
1699
Mathias Agopian13127d82013-03-05 17:47:11 -08001700status_t SurfaceFlinger::removeLayer_l(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701{
Mathias Agopian13127d82013-03-05 17:47:11 -08001702 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001704 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 return NO_ERROR;
1706 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001707 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708}
1709
Mathias Agopian13127d82013-03-05 17:47:11 -08001710status_t SurfaceFlinger::purgatorizeLayer_l(const sp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001711{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001712 // First add the layer to the purgatory list, which makes sure it won't
1713 // go away, then remove it from the main list (through a transaction).
Mathias Agopian13127d82013-03-05 17:47:11 -08001714 ssize_t err = removeLayer_l(layer);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001715 if (err >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001716 mLayerPurgatory.add(layer);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001717 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001718
Mathias Agopian13127d82013-03-05 17:47:11 -08001719 mLayersPendingRemoval.push(layer);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001720
Mathias Agopian3d579642009-06-04 18:46:21 -07001721 // it's possible that we don't find a layer, because it might
1722 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001723 // from the user because there is a race between Client::destroySurface(),
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001724 // ~Client() and ~LayerCleaner().
Mathias Agopian9a112062009-04-17 19:36:26 -07001725 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1726}
1727
Mathias Agopiandea20b12011-05-03 17:04:02 -07001728uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1729{
1730 return android_atomic_release_load(&mTransactionFlags);
1731}
1732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1734{
1735 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1736}
1737
Mathias Agopianbb641242010-05-18 17:06:55 -07001738uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739{
1740 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1741 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001742 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 }
1744 return old;
1745}
1746
Mathias Agopian8b33f032012-07-24 20:43:54 -07001747void SurfaceFlinger::setTransactionState(
1748 const Vector<ComposerState>& state,
1749 const Vector<DisplayState>& displays,
1750 uint32_t flags)
1751{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001752 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001753 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001754 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001755
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001756 if (flags & eAnimation) {
1757 // For window updates that are part of an animation we must wait for
1758 // previous animation "frames" to be handled.
1759 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001760 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001761 if (CC_UNLIKELY(err != NO_ERROR)) {
1762 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001763 // caller after a few seconds.
1764 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1765 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001766 mAnimTransactionPending = false;
1767 break;
1768 }
1769 }
1770 }
1771
Mathias Agopiane57f2922012-08-09 16:29:12 -07001772 size_t count = displays.size();
1773 for (size_t i=0 ; i<count ; i++) {
1774 const DisplayState& s(displays[i]);
1775 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001776 }
1777
Mathias Agopiane57f2922012-08-09 16:29:12 -07001778 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001779 for (size_t i=0 ; i<count ; i++) {
1780 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001781 // Here we need to check that the interface we're given is indeed
1782 // one of our own. A malicious client could give us a NULL
1783 // IInterface, or one of its own or even one of our own but a
1784 // different type. All these situations would cause us to crash.
1785 //
1786 // NOTE: it would be better to use RTTI as we could directly check
1787 // that we have a Client*. however, RTTI is disabled in Android.
1788 if (s.client != NULL) {
1789 sp<IBinder> binder = s.client->asBinder();
1790 if (binder != NULL) {
1791 String16 desc(binder->getInterfaceDescriptor());
1792 if (desc == ISurfaceComposerClient::descriptor) {
1793 sp<Client> client( static_cast<Client *>(s.client.get()) );
1794 transactionFlags |= setClientStateLocked(client, s.state);
1795 }
1796 }
1797 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001798 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001799
Mathias Agopian386aa982011-11-07 21:58:03 -08001800 if (transactionFlags) {
1801 // this triggers the transaction
1802 setTransactionFlags(transactionFlags);
1803
1804 // if this is a synchronous transaction, wait for it to take effect
1805 // before returning.
1806 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001807 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001808 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001809 if (flags & eAnimation) {
1810 mAnimTransactionPending = true;
1811 }
1812 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001813 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1814 if (CC_UNLIKELY(err != NO_ERROR)) {
1815 // just in case something goes wrong in SF, return to the
1816 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001817 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1818 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001819 break;
1820 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001821 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822 }
1823}
1824
Mathias Agopiane57f2922012-08-09 16:29:12 -07001825uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1826{
Jesse Hall9a143922012-10-04 16:29:19 -07001827 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1828 if (dpyIdx < 0)
1829 return 0;
1830
Mathias Agopiane57f2922012-08-09 16:29:12 -07001831 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001832 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001833 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001834 const uint32_t what = s.what;
1835 if (what & DisplayState::eSurfaceChanged) {
1836 if (disp.surface->asBinder() != s.surface->asBinder()) {
1837 disp.surface = s.surface;
1838 flags |= eDisplayTransactionNeeded;
1839 }
1840 }
1841 if (what & DisplayState::eLayerStackChanged) {
1842 if (disp.layerStack != s.layerStack) {
1843 disp.layerStack = s.layerStack;
1844 flags |= eDisplayTransactionNeeded;
1845 }
1846 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001847 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001848 if (disp.orientation != s.orientation) {
1849 disp.orientation = s.orientation;
1850 flags |= eDisplayTransactionNeeded;
1851 }
1852 if (disp.frame != s.frame) {
1853 disp.frame = s.frame;
1854 flags |= eDisplayTransactionNeeded;
1855 }
1856 if (disp.viewport != s.viewport) {
1857 disp.viewport = s.viewport;
1858 flags |= eDisplayTransactionNeeded;
1859 }
1860 }
1861 }
1862 return flags;
1863}
1864
1865uint32_t SurfaceFlinger::setClientStateLocked(
1866 const sp<Client>& client,
1867 const layer_state_t& s)
1868{
1869 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001870 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001871 if (layer != 0) {
1872 const uint32_t what = s.what;
1873 if (what & layer_state_t::ePositionChanged) {
1874 if (layer->setPosition(s.x, s.y))
1875 flags |= eTraversalNeeded;
1876 }
1877 if (what & layer_state_t::eLayerChanged) {
1878 // NOTE: index needs to be calculated before we update the state
1879 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1880 if (layer->setLayer(s.z)) {
1881 mCurrentState.layersSortedByZ.removeAt(idx);
1882 mCurrentState.layersSortedByZ.add(layer);
1883 // we need traversal (state changed)
1884 // AND transaction (list changed)
1885 flags |= eTransactionNeeded|eTraversalNeeded;
1886 }
1887 }
1888 if (what & layer_state_t::eSizeChanged) {
1889 if (layer->setSize(s.w, s.h)) {
1890 flags |= eTraversalNeeded;
1891 }
1892 }
1893 if (what & layer_state_t::eAlphaChanged) {
1894 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1895 flags |= eTraversalNeeded;
1896 }
1897 if (what & layer_state_t::eMatrixChanged) {
1898 if (layer->setMatrix(s.matrix))
1899 flags |= eTraversalNeeded;
1900 }
1901 if (what & layer_state_t::eTransparentRegionChanged) {
1902 if (layer->setTransparentRegionHint(s.transparentRegion))
1903 flags |= eTraversalNeeded;
1904 }
1905 if (what & layer_state_t::eVisibilityChanged) {
1906 if (layer->setFlags(s.flags, s.mask))
1907 flags |= eTraversalNeeded;
1908 }
1909 if (what & layer_state_t::eCropChanged) {
1910 if (layer->setCrop(s.crop))
1911 flags |= eTraversalNeeded;
1912 }
1913 if (what & layer_state_t::eLayerStackChanged) {
1914 // NOTE: index needs to be calculated before we update the state
1915 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1916 if (layer->setLayerStack(s.layerStack)) {
1917 mCurrentState.layersSortedByZ.removeAt(idx);
1918 mCurrentState.layersSortedByZ.add(layer);
1919 // we need traversal (state changed)
1920 // AND transaction (list changed)
1921 flags |= eTransactionNeeded|eTraversalNeeded;
1922 }
1923 }
1924 }
1925 return flags;
1926}
1927
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001928status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001929 const String8& name,
1930 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001931 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1932 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001934 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001935 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001936 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001937 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001938 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001939 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001940
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941 status_t result = NO_ERROR;
1942
1943 sp<Layer> layer;
1944
Mathias Agopian3165cc22012-08-08 19:42:09 -07001945 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1946 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001947 result = createNormalLayer(client,
1948 name, w, h, flags, format,
1949 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001951 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001952 result = createDimLayer(client,
1953 name, w, h, flags,
1954 handle, gbp, &layer);
1955 break;
1956 default:
1957 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958 break;
1959 }
1960
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001961 if (result == NO_ERROR) {
1962 addClientLayer(client, *handle, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001963 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001965 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001966}
1967
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001968status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1969 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1970 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001971{
1972 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001973 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974 case PIXEL_FORMAT_TRANSPARENT:
1975 case PIXEL_FORMAT_TRANSLUCENT:
1976 format = PIXEL_FORMAT_RGBA_8888;
1977 break;
1978 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001979#ifdef NO_RGBX_8888
1980 format = PIXEL_FORMAT_RGB_565;
1981#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001982 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001983#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 break;
1985 }
1986
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001987#ifdef NO_RGBX_8888
1988 if (format == PIXEL_FORMAT_RGBX_8888)
1989 format = PIXEL_FORMAT_RGBA_8888;
1990#endif
1991
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001992 *outLayer = new Layer(this, client, name, w, h, flags);
1993 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1994 if (err == NO_ERROR) {
1995 *handle = (*outLayer)->getHandle();
1996 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001998
1999 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2000 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002001}
2002
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002003status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2004 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2005 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002006{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002007 *outLayer = new LayerDim(this, client, name, w, h, flags);
2008 *handle = (*outLayer)->getHandle();
2009 *gbp = (*outLayer)->getBufferQueue();
2010 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002011}
2012
Mathias Agopianac9fa422013-02-11 16:40:36 -08002013status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002014{
Mathias Agopian9a112062009-04-17 19:36:26 -07002015 /*
2016 * called by the window manager, when a surface should be marked for
2017 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002018 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002019 * The surface is removed from the current and drawing lists, but placed
2020 * in the purgatory queue, so it's not destroyed right-away (we need
2021 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07002022 */
Mathias Agopian9a112062009-04-17 19:36:26 -07002023
Mathias Agopian48d819a2009-09-10 19:41:18 -07002024 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002025 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002026 sp<Layer> layer = client->getLayerUser(handle);
Daniel Lamb2675792012-02-23 14:35:13 -08002027
Mathias Agopian48d819a2009-09-10 19:41:18 -07002028 if (layer != 0) {
2029 err = purgatorizeLayer_l(layer);
2030 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07002031 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07002032 }
Mathias Agopian9a112062009-04-17 19:36:26 -07002033 }
2034 return err;
2035}
2036
Mathias Agopian13127d82013-03-05 17:47:11 -08002037status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002038{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002039 // called by ~LayerCleaner() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002040 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002041 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002042 if (l != NULL) {
2043 Mutex::Autolock _l(mStateLock);
2044 err = removeLayer_l(l);
2045 if (err == NAME_NOT_FOUND) {
2046 // The surface wasn't in the current list, which means it was
2047 // removed already, which means it is in the purgatory,
2048 // and need to be removed from there.
2049 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002050 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002051 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07002052 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002053 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002054 "error removing layer=%p (%s)", l.get(), strerror(-err));
2055 }
2056 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002057}
2058
Mathias Agopianb60314a2012-04-10 22:09:54 -07002059// ---------------------------------------------------------------------------
2060
Andy McFadden13a082e2012-08-24 10:16:42 -07002061void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002062 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002063 Vector<ComposerState> state;
2064 Vector<DisplayState> displays;
2065 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002066 d.what = DisplayState::eDisplayProjectionChanged |
2067 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002068 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002069 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002070 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002071 d.frame.makeInvalid();
2072 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002073 displays.add(d);
2074 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002075 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002076}
2077
2078void SurfaceFlinger::initializeDisplays() {
2079 class MessageScreenInitialized : public MessageBase {
2080 SurfaceFlinger* flinger;
2081 public:
2082 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2083 virtual bool handler() {
2084 flinger->onInitializeDisplays();
2085 return true;
2086 }
2087 };
2088 sp<MessageBase> msg = new MessageScreenInitialized(this);
2089 postMessageAsync(msg); // we may be called from main thread, use async message
2090}
2091
2092
Mathias Agopiancde87a32012-09-13 14:09:01 -07002093void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002094 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2095 if (hw->isScreenAcquired()) {
2096 // this is expected, e.g. when power manager wakes up during boot
2097 ALOGD(" screen was previously acquired");
2098 return;
2099 }
2100
Mathias Agopian42977342012-08-05 00:40:46 -07002101 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002102 int32_t type = hw->getDisplayType();
2103 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2104 // built-in display, tell the HWC
2105 getHwComposer().acquire(type);
2106
2107 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2108 // FIXME: eventthread only knows about the main display right now
2109 mEventThread->onScreenAcquired();
2110 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002111 }
Mathias Agopian20128302012-08-13 18:32:13 -07002112 mVisibleRegionsDirty = true;
2113 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114}
2115
Mathias Agopiancde87a32012-09-13 14:09:01 -07002116void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002117 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2118 if (!hw->isScreenAcquired()) {
2119 ALOGD(" screen was previously released");
2120 return;
2121 }
2122
2123 hw->releaseScreen();
2124 int32_t type = hw->getDisplayType();
2125 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2126 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002127 // FIXME: eventthread only knows about the main display right now
2128 mEventThread->onScreenReleased();
2129 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002130
2131 // built-in display, tell the HWC
2132 getHwComposer().release(type);
2133 }
2134 mVisibleRegionsDirty = true;
2135 // from this point on, SF will stop drawing on this display
2136}
2137
2138void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2139 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002140 SurfaceFlinger& mFlinger;
2141 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002142 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002143 MessageScreenAcquired(SurfaceFlinger& flinger,
2144 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002145 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002146 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2147 if (hw == NULL) {
2148 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2149 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2150 ALOGW("Attempt to unblank virtual display");
2151 } else {
2152 mFlinger.onScreenAcquired(hw);
2153 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002154 return true;
2155 }
2156 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002157 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2158 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002159}
2160
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002161void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002162 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002163 SurfaceFlinger& mFlinger;
2164 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002165 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002166 MessageScreenReleased(SurfaceFlinger& flinger,
2167 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002168 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002169 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2170 if (hw == NULL) {
2171 ALOGE("Attempt to blank null display %p", mDisplay.get());
2172 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2173 ALOGW("Attempt to blank virtual display");
2174 } else {
2175 mFlinger.onScreenReleased(hw);
2176 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002177 return true;
2178 }
2179 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002180 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2181 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002182}
2183
2184// ---------------------------------------------------------------------------
2185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002186status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2187{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002188 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 char buffer[SIZE];
2190 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002191
2192 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002193 snprintf(buffer, SIZE, "Permission Denial: "
2194 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2195 IPCThreadState::self()->getCallingPid(),
2196 IPCThreadState::self()->getCallingUid());
2197 result.append(buffer);
2198 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002199 // Try to get the main lock, but don't insist if we can't
2200 // (this would indicate SF is stuck, but we want to be able to
2201 // print something in dumpsys).
2202 int retry = 3;
2203 while (mStateLock.tryLock()<0 && --retry>=0) {
2204 usleep(1000000);
2205 }
2206 const bool locked(retry >= 0);
2207 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002208 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002209 "SurfaceFlinger appears to be unresponsive, "
2210 "dumping anyways (no locks held)\n");
2211 result.append(buffer);
2212 }
2213
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002214 bool dumpAll = true;
2215 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002216 size_t numArgs = args.size();
2217 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002218 if ((index < numArgs) &&
2219 (args[index] == String16("--list"))) {
2220 index++;
2221 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002222 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002223 }
2224
2225 if ((index < numArgs) &&
2226 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002227 index++;
2228 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002229 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002230 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002231
2232 if ((index < numArgs) &&
2233 (args[index] == String16("--latency-clear"))) {
2234 index++;
2235 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002236 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002238 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002239
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002240 if (dumpAll) {
2241 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002242 }
2243
Mathias Agopian9795c422009-08-26 16:36:26 -07002244 if (locked) {
2245 mStateLock.unlock();
2246 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002247 }
2248 write(fd, result.string(), result.size());
2249 return NO_ERROR;
2250}
2251
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002252void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2253 String8& result, char* buffer, size_t SIZE) const
2254{
2255 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2256 const size_t count = currentLayers.size();
2257 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002258 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002259 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2260 result.append(buffer);
2261 }
2262}
2263
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002264void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2265 String8& result, char* buffer, size_t SIZE) const
2266{
2267 String8 name;
2268 if (index < args.size()) {
2269 name = String8(args[index]);
2270 index++;
2271 }
2272
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002273 const nsecs_t period =
2274 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2275 result.appendFormat("%lld\n", period);
2276
2277 if (name.isEmpty()) {
2278 mAnimFrameTracker.dump(result);
2279 } else {
2280 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2281 const size_t count = currentLayers.size();
2282 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002283 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002284 if (name == layer->getName()) {
2285 layer->dumpStats(result, buffer, SIZE);
2286 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002287 }
2288 }
2289}
2290
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002291void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002292 String8& result, char* buffer, size_t SIZE)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002293{
2294 String8 name;
2295 if (index < args.size()) {
2296 name = String8(args[index]);
2297 index++;
2298 }
2299
2300 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2301 const size_t count = currentLayers.size();
2302 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002303 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002304 if (name.isEmpty() || (name == layer->getName())) {
2305 layer->clearStats();
2306 }
2307 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002308
2309 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002310}
2311
Andy McFadden4803b742012-09-24 19:07:20 -07002312/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2313{
2314 static const char* config =
2315 " [sf"
2316#ifdef NO_RGBX_8888
2317 " NO_RGBX_8888"
2318#endif
2319#ifdef HAS_CONTEXT_PRIORITY
2320 " HAS_CONTEXT_PRIORITY"
2321#endif
2322#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2323 " NEVER_DEFAULT_TO_ASYNC_MODE"
2324#endif
2325#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2326 " TARGET_DISABLE_TRIPLE_BUFFERING"
2327#endif
2328 "]";
2329 result.append(config);
2330}
2331
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332void SurfaceFlinger::dumpAllLocked(
2333 String8& result, char* buffer, size_t SIZE) const
2334{
2335 // figure out if we're stuck somewhere
2336 const nsecs_t now = systemTime();
2337 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2338 const nsecs_t inTransaction(mDebugInTransaction);
2339 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2340 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2341
2342 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002343 * Dump library configuration.
2344 */
2345 result.append("Build configuration:");
2346 appendSfConfigString(result);
2347 appendUiConfigString(result);
2348 appendGuiConfigString(result);
2349 result.append("\n");
2350
2351 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352 * Dump the visible layer list
2353 */
2354 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2355 const size_t count = currentLayers.size();
2356 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2357 result.append(buffer);
2358 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002359 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360 layer->dump(result, buffer, SIZE);
2361 }
2362
2363 /*
2364 * Dump the layers in the purgatory
2365 */
2366
2367 const size_t purgatorySize = mLayerPurgatory.size();
2368 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2369 result.append(buffer);
2370 for (size_t i=0 ; i<purgatorySize ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002371 const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002372 layer->shortDump(result, buffer, SIZE);
2373 }
2374
2375 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002376 * Dump Display state
2377 */
2378
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002379 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2380 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002381 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2382 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002383 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002384 }
2385
2386 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002387 * Dump SurfaceFlinger global state
2388 */
2389
2390 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2391 result.append(buffer);
2392
Mathias Agopian888c8222012-08-04 21:10:38 -07002393 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002394 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002395 const GLExtensions& extensions(GLExtensions::getInstance());
2396 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2397 extensions.getVendor(),
2398 extensions.getRenderer(),
2399 extensions.getVersion());
2400 result.append(buffer);
2401
2402 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002403 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002404 result.append(buffer);
2405
2406 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2407 result.append(buffer);
2408
Mathias Agopian42977342012-08-05 00:40:46 -07002409 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 snprintf(buffer, SIZE,
2411 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002412 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002413 result.append(buffer);
2414 snprintf(buffer, SIZE,
2415 " last eglSwapBuffers() time: %f us\n"
2416 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002417 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002418 " refresh-rate : %f fps\n"
2419 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002420 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002421 mLastSwapBufferTime/1000.0,
2422 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002423 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002424 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2425 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2426 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002427 result.append(buffer);
2428
2429 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2430 inSwapBuffersDuration/1000.0);
2431 result.append(buffer);
2432
2433 snprintf(buffer, SIZE, " transaction time: %f us\n",
2434 inTransactionDuration/1000.0);
2435 result.append(buffer);
2436
2437 /*
2438 * VSYNC state
2439 */
2440 mEventThread->dump(result, buffer, SIZE);
2441
2442 /*
2443 * Dump HWComposer state
2444 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002445 snprintf(buffer, SIZE, "h/w composer state:\n");
2446 result.append(buffer);
2447 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2448 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2449 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2450 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002451 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452
2453 /*
2454 * Dump gralloc state
2455 */
2456 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2457 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002458}
2459
Mathias Agopian13127d82013-03-05 17:47:11 -08002460const Vector< sp<Layer> >&
Mathias Agopiancb558572012-10-04 15:58:54 -07002461SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002462 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002463 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2464}
2465
Keun young Park63f165f2012-08-31 10:53:36 -07002466bool SurfaceFlinger::startDdmConnection()
2467{
2468 void* libddmconnection_dso =
2469 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2470 if (!libddmconnection_dso) {
2471 return false;
2472 }
2473 void (*DdmConnection_start)(const char* name);
2474 DdmConnection_start =
2475 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2476 if (!DdmConnection_start) {
2477 dlclose(libddmconnection_dso);
2478 return false;
2479 }
2480 (*DdmConnection_start)(getServiceName());
2481 return true;
2482}
2483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484status_t SurfaceFlinger::onTransact(
2485 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2486{
2487 switch (code) {
2488 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002489 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002491 case BLANK:
2492 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493 {
2494 // codes that require permission check
2495 IPCThreadState* ipc = IPCThreadState::self();
2496 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002497 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002498 if ((uid != AID_GRAPHICS) &&
2499 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002500 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002501 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2502 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002503 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504 break;
2505 }
2506 case CAPTURE_SCREEN:
2507 {
2508 // codes that require permission check
2509 IPCThreadState* ipc = IPCThreadState::self();
2510 const int pid = ipc->getCallingPid();
2511 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002512 if ((uid != AID_GRAPHICS) &&
2513 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002514 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002515 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2516 return PERMISSION_DENIED;
2517 }
2518 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519 }
2520 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002521
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2523 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002524 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002525 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002526 IPCThreadState* ipc = IPCThreadState::self();
2527 const int pid = ipc->getCallingPid();
2528 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002529 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002530 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002531 return PERMISSION_DENIED;
2532 }
2533 int n;
2534 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002535 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002536 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537 return NO_ERROR;
2538 case 1002: // SHOW_UPDATES
2539 n = data.readInt32();
2540 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002541 invalidateHwcGeometry();
2542 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002544 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002545 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002546 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002547 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002548 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002549 setTransactionFlags(
2550 eTransactionNeeded|
2551 eDisplayTransactionNeeded|
2552 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002553 return NO_ERROR;
2554 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002555 case 1006:{ // send empty update
2556 signalRefresh();
2557 return NO_ERROR;
2558 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002559 case 1008: // toggle use of hw composer
2560 n = data.readInt32();
2561 mDebugDisableHWC = n ? 1 : 0;
2562 invalidateHwcGeometry();
2563 repaintEverything();
2564 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002565 case 1009: // toggle use of transform hint
2566 n = data.readInt32();
2567 mDebugDisableTransformHint = n ? 1 : 0;
2568 invalidateHwcGeometry();
2569 repaintEverything();
2570 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002571 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002572 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002573 reply->writeInt32(0);
2574 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002575 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002576 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577 return NO_ERROR;
2578 case 1013: {
2579 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002580 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2581 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002582 }
2583 return NO_ERROR;
2584 }
2585 }
2586 return err;
2587}
2588
Mathias Agopian53331da2011-08-22 21:44:41 -07002589void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002590 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002591 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002592}
2593
Mathias Agopian59119e62010-10-11 12:37:43 -07002594// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002595// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002596// ---------------------------------------------------------------------------
2597
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002598status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2599 const sp<IGraphicBufferProducer>& producer,
2600 uint32_t reqWidth, uint32_t reqHeight,
2601 uint32_t minLayerZ, uint32_t maxLayerZ) {
2602
2603 if (CC_UNLIKELY(display == 0))
2604 return BAD_VALUE;
2605
2606 if (CC_UNLIKELY(producer == 0))
2607 return BAD_VALUE;
2608
2609 class MessageCaptureScreen : public MessageBase {
2610 SurfaceFlinger* flinger;
2611 sp<IBinder> display;
2612 sp<IGraphicBufferProducer> producer;
2613 uint32_t reqWidth, reqHeight;
2614 uint32_t minLayerZ,maxLayerZ;
2615 status_t result;
2616 public:
2617 MessageCaptureScreen(SurfaceFlinger* flinger,
2618 const sp<IBinder>& display,
2619 const sp<IGraphicBufferProducer>& producer,
2620 uint32_t reqWidth, uint32_t reqHeight,
2621 uint32_t minLayerZ, uint32_t maxLayerZ)
2622 : flinger(flinger), display(display), producer(producer),
2623 reqWidth(reqWidth), reqHeight(reqHeight),
2624 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2625 result(PERMISSION_DENIED)
2626 {
2627 }
2628 status_t getResult() const {
2629 return result;
2630 }
2631 virtual bool handler() {
2632 Mutex::Autolock _l(flinger->mStateLock);
2633 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
2634 result = flinger->captureScreenImplLocked(hw, producer,
2635 reqWidth, reqHeight, minLayerZ, maxLayerZ);
2636 return true;
2637 }
2638 };
2639
2640 sp<MessageBase> msg = new MessageCaptureScreen(this,
2641 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2642 status_t res = postMessageSync(msg);
2643 if (res == NO_ERROR) {
2644 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2645 }
2646 return res;
2647}
2648
2649status_t SurfaceFlinger::captureScreenImplLocked(
2650 const sp<const DisplayDevice>& hw,
2651 const sp<IGraphicBufferProducer>& producer,
2652 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002653 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002654{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002655 ATRACE_CALL();
2656
Mathias Agopian74c40c02010-09-29 13:02:36 -07002657 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002658 const uint32_t hw_w = hw->getWidth();
2659 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002660
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002661 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002662 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002663 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002664 return PERMISSION_DENIED;
2665 }
2666
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002667 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2668 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2669 reqWidth, reqHeight, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002670 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002671 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002672
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002673 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2674 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2675 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002676
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002677 // Create a surface to render into
2678 sp<Surface> surface = new Surface(producer);
2679 ANativeWindow* const window = surface.get();
2680
2681 // set the buffer size to what the user requested
2682 native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
2683
2684 // and create the corresponding EGLSurface
2685 EGLSurface eglSurface = eglCreateWindowSurface(
2686 mEGLDisplay, mEGLConfig, window, NULL);
2687 if (eglSurface == EGL_NO_SURFACE) {
2688 ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
2689 eglGetError());
2690 return BAD_VALUE;
2691 }
2692
2693 if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
2694 ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
2695 eglGetError());
2696 eglDestroySurface(mEGLDisplay, eglSurface);
2697 return BAD_VALUE;
2698 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002699
Mathias Agopian74c40c02010-09-29 13:02:36 -07002700 // make sure to clear all GL error flags
2701 while ( glGetError() != GL_NO_ERROR ) ;
2702
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002703 // set-up our viewport
2704 glViewport(0, 0, reqWidth, reqHeight);
2705 glMatrixMode(GL_PROJECTION);
2706 glLoadIdentity();
2707 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2708 glMatrixMode(GL_MODELVIEW);
2709 glLoadIdentity();
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002710
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002711 // redraw the screen entirely...
2712 glDisable(GL_TEXTURE_EXTERNAL_OES);
2713 glDisable(GL_TEXTURE_2D);
2714 glClearColor(0,0,0,1);
2715 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002716
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002717 const LayerVector& layers( mDrawingState.layersSortedByZ );
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002718 const size_t count = layers.size();
2719 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002720 const sp<Layer>& layer(layers[i]);
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002721 const Layer::State& state(layer->drawingState());
2722 if (state.layerStack == hw->getLayerStack()) {
2723 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2724 if (filtering) layer->setFiltering(true);
2725 layer->draw(hw);
2726 if (filtering) layer->setFiltering(false);
2727 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002728 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002729 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002730
Mathias Agopiand5776412013-03-08 13:54:46 -08002731 // compositionComplete is needed for older driver
2732 hw->compositionComplete();
2733
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002734 // and finishing things up...
2735 if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
2736 ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
2737 eglGetError());
2738 eglDestroySurface(mEGLDisplay, eglSurface);
2739 return BAD_VALUE;
2740 }
2741
2742 eglDestroySurface(mEGLDisplay, eglSurface);
2743 return NO_ERROR;
2744}
2745
2746// ---------------------------------------------------------------------------
2747// Capture screen into an IMemoryHeap (legacy)
2748// ---------------------------------------------------------------------------
2749
2750status_t SurfaceFlinger::captureScreenImplLocked(
2751 const sp<const DisplayDevice>& hw,
2752 sp<IMemoryHeap>* heap,
2753 uint32_t* w, uint32_t* h, PixelFormat* f,
2754 uint32_t sw, uint32_t sh,
2755 uint32_t minLayerZ, uint32_t maxLayerZ)
2756{
2757 ATRACE_CALL();
2758
2759 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2760 return INVALID_OPERATION;
2761 }
2762
2763 // create the texture that will receive the screenshot, later we'll
2764 // attach a FBO to it so we can call glReadPixels().
2765 GLuint tname;
2766 glGenTextures(1, &tname);
2767 glBindTexture(GL_TEXTURE_2D, tname);
2768 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2769 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2770
2771 // the GLConsumer will provide the BufferQueue
2772 sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
2773 consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
2774
2775 // call the new screenshot taking code, passing a BufferQueue to it
2776 status_t result = captureScreenImplLocked(hw,
2777 consumer->getBufferQueue(), sw, sh, minLayerZ, maxLayerZ);
2778
2779 if (result == NO_ERROR) {
2780 result = consumer->updateTexImage();
2781 if (result == NO_ERROR) {
2782 // create a FBO
2783 GLuint name;
2784 glGenFramebuffersOES(1, &name);
2785 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2786 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2787 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
2788
2789 sp<GraphicBuffer> buf(consumer->getCurrentBuffer());
2790 sw = buf->getWidth();
2791 sh = buf->getHeight();
Mathias Agopian6a531712013-03-13 15:22:11 -07002792 size_t size = sw * sh * 4;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002793
Mathias Agopian74c40c02010-09-29 13:02:36 -07002794 // allocate shared memory large enough to hold the
2795 // screen capture
2796 sp<MemoryHeapBase> base(
2797 new MemoryHeapBase(size, 0, "screen-capture") );
2798 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002799 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002800 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002801 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002802 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2803 if (glGetError() == GL_NO_ERROR) {
2804 *heap = base;
2805 *w = sw;
2806 *h = sh;
2807 *f = PIXEL_FORMAT_RGBA_8888;
2808 result = NO_ERROR;
Jeff Sharkey175264b2013-03-05 17:57:26 -08002809 } else {
2810 result = NO_MEMORY;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002811 }
2812 } else {
2813 result = NO_MEMORY;
2814 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002815
2816 // back to main framebuffer
2817 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2818 glDeleteFramebuffersOES(1, &name);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002819 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002820 }
2821
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002822 glDeleteTextures(1, &tname);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002823
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002824 DisplayDevice::makeCurrent(mEGLDisplay,
2825 getDefaultDisplayDevice(), mEGLContext);
2826
Mathias Agopian74c40c02010-09-29 13:02:36 -07002827 return result;
2828}
2829
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002830status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002831 sp<IMemoryHeap>* heap,
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002832 uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
2833 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002834 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002835{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002836 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002837 return BAD_VALUE;
2838
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002839 class MessageCaptureScreen : public MessageBase {
2840 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002841 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002842 sp<IMemoryHeap>* heap;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002843 uint32_t* outWidth;
2844 uint32_t* outHeight;
2845 PixelFormat* outFormat;
2846 uint32_t reqWidth;
2847 uint32_t reqHeight;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002848 uint32_t minLayerZ;
2849 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002850 status_t result;
2851 public:
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002852 MessageCaptureScreen(SurfaceFlinger* flinger,
2853 const sp<IBinder>& display, sp<IMemoryHeap>* heap,
2854 uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
2855 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002856 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002857 : flinger(flinger), display(display), heap(heap),
2858 outWidth(outWidth), outHeight(outHeight), outFormat(outFormat),
2859 reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002860 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2861 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002862 {
2863 }
2864 status_t getResult() const {
2865 return result;
2866 }
2867 virtual bool handler() {
2868 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002869 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
2870 result = flinger->captureScreenImplLocked(hw, heap,
2871 outWidth, outHeight, outFormat,
2872 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002873 return true;
2874 }
2875 };
2876
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002877 sp<MessageBase> msg = new MessageCaptureScreen(this, display, heap,
2878 outWidth, outHeight, outFormat,
2879 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002880 status_t res = postMessageSync(msg);
2881 if (res == NO_ERROR) {
2882 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2883 }
2884 return res;
2885}
2886
2887// ---------------------------------------------------------------------------
2888
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002889SurfaceFlinger::LayerVector::LayerVector() {
2890}
2891
2892SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002893 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002894}
2895
2896int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2897 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002898{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002899 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002900 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2901 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002902
2903 uint32_t ls = l->currentState().layerStack;
2904 uint32_t rs = r->currentState().layerStack;
2905 if (ls != rs)
2906 return ls - rs;
2907
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002908 uint32_t lz = l->currentState().z;
2909 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002910 if (lz != rz)
2911 return lz - rz;
2912
2913 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002914}
2915
2916// ---------------------------------------------------------------------------
2917
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002918SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2919 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002920}
2921
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002922SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002923 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002924 viewport.makeInvalid();
2925 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002926}
2927
2928// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002930}; // namespace android