blob: 88c7fa0db959113ac731edbe387477576e627eeb [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"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana67932f2011-04-20 14:20:59 -070071
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080072#define EGL_VERSION_HW_ANDROID 0x3143
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
76namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ---------------------------------------------------------------------------
78
Mathias Agopian99b49842011-06-27 16:05:52 -070079const String16 sHardwareTest("android.permission.HARDWARE_TEST");
80const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
81const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
82const String16 sDump("android.permission.DUMP");
83
84// ---------------------------------------------------------------------------
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086SurfaceFlinger::SurfaceFlinger()
87 : BnSurfaceComposer(), Thread(false),
88 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070089 mTransactionPending(false),
90 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070092 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070095 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -080096 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070098 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070099 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700100 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700101 mDebugInSwapBuffers(0),
102 mLastSwapBufferTime(0),
103 mDebugInTransaction(0),
104 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700105 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106{
Steve Blocka19954a2012-01-04 20:05:49 +0000107 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 property_get("debug.sf.showupdates", value, "0");
113 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115 property_get("debug.sf.ddms", value, "0");
116 mDebugDDMS = atoi(value);
117 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700118 if (!startDdmConnection()) {
119 // start failed, and DDMS debugging not enabled
120 mDebugDDMS = 0;
121 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700122 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700123 ALOGI_IF(mDebugRegion, "showupdates enabled");
124 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125}
126
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800127void SurfaceFlinger::onFirstRef()
128{
129 mEventQueue.init(this);
130
131 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
132
133 // Wait for the main thread to be done with its initialization
134 mReadyToRunBarrier.wait();
135}
136
137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138SurfaceFlinger::~SurfaceFlinger()
139{
Mathias Agopiana4912602012-07-12 14:25:33 -0700140 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
141 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
142 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145void SurfaceFlinger::binderDied(const wp<IBinder>& who)
146{
147 // the window manager died on us. prepare its eulogy.
148
Andy McFadden13a082e2012-08-24 10:16:42 -0700149 // restore initial conditions (default device unblank, etc)
150 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800151
152 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700153 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800154}
155
Mathias Agopian7e27f052010-05-28 14:22:23 -0700156sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Mathias Agopian96f08192010-06-02 23:28:45 -0700158 sp<ISurfaceComposerClient> bclient;
159 sp<Client> client(new Client(this));
160 status_t err = client->initCheck();
161 if (err == NO_ERROR) {
162 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 return bclient;
165}
166
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700167sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
168 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700169{
170 class DisplayToken : public BBinder {
171 sp<SurfaceFlinger> flinger;
172 virtual ~DisplayToken() {
173 // no more references, this display must be terminated
174 Mutex::Autolock _l(flinger->mStateLock);
175 flinger->mCurrentState.displays.removeItem(this);
176 flinger->setTransactionFlags(eDisplayTransactionNeeded);
177 }
178 public:
179 DisplayToken(const sp<SurfaceFlinger>& flinger)
180 : flinger(flinger) {
181 }
182 };
183
184 sp<BBinder> token = new DisplayToken(this);
185
186 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700187 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700188 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700189 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700190 mCurrentState.displays.add(token, info);
191
192 return token;
193}
194
Jesse Hall692c7232012-11-08 15:41:56 -0800195void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
196 ALOGW_IF(mBuiltinDisplays[type],
197 "Overwriting display token for display type %d", type);
198 mBuiltinDisplays[type] = new BBinder();
199 DisplayDeviceState info(type);
200 // All non-virtual displays are currently considered secure.
201 info.isSecure = true;
202 mCurrentState.displays.add(mBuiltinDisplays[type], info);
203}
204
Mathias Agopiane57f2922012-08-09 16:29:12 -0700205sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700206 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700207 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
208 return NULL;
209 }
Jesse Hall692c7232012-11-08 15:41:56 -0800210 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700211}
212
Jamie Gennis9a78c902011-01-12 18:30:40 -0800213sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
214{
215 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
216 return gba;
217}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219void SurfaceFlinger::bootFinished()
220{
221 const nsecs_t now = systemTime();
222 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000223 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700224 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700225
226 // wait patiently for the window manager death
227 const String16 name("window");
228 sp<IBinder> window(defaultServiceManager()->getService(name));
229 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700230 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700231 }
232
233 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700234 // formerly we would just kill the process, but we now ask it to exit so it
235 // can choose where to stop the animation.
236 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237}
238
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700239void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
240 class MessageDestroyGLTexture : public MessageBase {
241 GLuint texture;
242 public:
243 MessageDestroyGLTexture(GLuint texture)
244 : texture(texture) {
245 }
246 virtual bool handler() {
247 glDeleteTextures(1, &texture);
248 return true;
249 }
250 };
251 postMessageAsync(new MessageDestroyGLTexture(texture));
252}
253
Mathias Agopian722b98f2012-09-25 18:24:31 -0700254status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700255 EGLDisplay dpy,
256 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700257 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700258 EGLConfig* outConfig)
259{
260 EGLConfig config = NULL;
261 EGLint numConfigs = -1, n=0;
262 eglGetConfigs(dpy, NULL, 0, &numConfigs);
263 EGLConfig* const configs = new EGLConfig[numConfigs];
264 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700265
Mathias Agopian722b98f2012-09-25 18:24:31 -0700266 if (n) {
267 if (attribute != EGL_NONE) {
268 for (int i=0 ; i<n ; i++) {
269 EGLint value = 0;
270 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
271 if (wanted == value) {
272 *outConfig = configs[i];
273 delete [] configs;
274 return NO_ERROR;
275 }
276 }
277 } else {
278 // just pick the first one
279 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700280 delete [] configs;
281 return NO_ERROR;
282 }
283 }
284 delete [] configs;
285 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
287
Mathias Agopian722b98f2012-09-25 18:24:31 -0700288class EGLAttributeVector {
289 struct Attribute;
290 class Adder;
291 friend class Adder;
292 KeyedVector<Attribute, EGLint> mList;
293 struct Attribute {
294 Attribute() {};
295 Attribute(EGLint v) : v(v) { }
296 EGLint v;
297 bool operator < (const Attribute& other) const {
298 // this places EGL_NONE at the end
299 EGLint lhs(v);
300 EGLint rhs(other.v);
301 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
302 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
303 return lhs < rhs;
304 }
305 };
306 class Adder {
307 friend class EGLAttributeVector;
308 EGLAttributeVector& v;
309 EGLint attribute;
310 Adder(EGLAttributeVector& v, EGLint attribute)
311 : v(v), attribute(attribute) {
312 }
313 public:
314 void operator = (EGLint value) {
315 if (attribute != EGL_NONE) {
316 v.mList.add(attribute, value);
317 }
318 }
319 operator EGLint () const { return v.mList[attribute]; }
320 };
321public:
322 EGLAttributeVector() {
323 mList.add(EGL_NONE, EGL_NONE);
324 }
325 void remove(EGLint attribute) {
326 if (attribute != EGL_NONE) {
327 mList.removeItem(attribute);
328 }
329 }
330 Adder operator [] (EGLint attribute) {
331 return Adder(*this, attribute);
332 }
333 EGLint operator [] (EGLint attribute) const {
334 return mList[attribute];
335 }
336 // cast-operator to (EGLint const*)
337 operator EGLint const* () const { return &mList.keyAt(0).v; }
338};
339
Mathias Agopiana4912602012-07-12 14:25:33 -0700340EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
341 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
342 // it is to be used with WIFI displays
343 EGLConfig config;
344 EGLint dummy;
345 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700346
Mathias Agopian722b98f2012-09-25 18:24:31 -0700347 EGLAttributeVector attribs;
348 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
349 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
350 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
351 attribs[EGL_RED_SIZE] = 8;
352 attribs[EGL_GREEN_SIZE] = 8;
353 attribs[EGL_BLUE_SIZE] = 8;
354
355 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
356 if (!err)
357 goto success;
358
359 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
360 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
361 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
362 err = selectConfigForAttribute(display, attribs,
363 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700364 if (!err)
365 goto success;
366
367 // maybe we failed because of EGL_RECORDABLE_ANDROID
368 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700369 attribs.remove(EGL_RECORDABLE_ANDROID);
370 err = selectConfigForAttribute(display, attribs,
371 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700372 if (!err)
373 goto success;
374
375 // allow less than 24-bit color; the non-gpu-accelerated emulator only
376 // supports 16-bit color
377 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700378 attribs.remove(EGL_RED_SIZE);
379 attribs.remove(EGL_GREEN_SIZE);
380 attribs.remove(EGL_BLUE_SIZE);
381 err = selectConfigForAttribute(display, attribs,
382 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700383 if (!err)
384 goto success;
385
386 // this EGL is too lame for Android
387 ALOGE("no suitable EGLConfig found, giving up");
388
389 return 0;
390
391success:
392 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700393 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700394 return config;
395}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396
Mathias Agopiana4912602012-07-12 14:25:33 -0700397EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
398 // Also create our EGLContext
399 EGLint contextAttributes[] = {
400#ifdef EGL_IMG_context_priority
401#ifdef HAS_CONTEXT_PRIORITY
402#warning "using EGL_IMG_context_priority"
403 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
404#endif
405#endif
406 EGL_NONE, EGL_NONE
407 };
408 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
409 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
410 return ctxt;
411}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412
Mathias Agopiancde87a32012-09-13 14:09:01 -0700413void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700414 GLExtensions& extensions(GLExtensions::getInstance());
415 extensions.initWithGLStrings(
416 glGetString(GL_VENDOR),
417 glGetString(GL_RENDERER),
418 glGetString(GL_VERSION),
419 glGetString(GL_EXTENSIONS),
420 eglQueryString(display, EGL_VENDOR),
421 eglQueryString(display, EGL_VERSION),
422 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700423
Mathias Agopiana4912602012-07-12 14:25:33 -0700424 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
425 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700428 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 glShadeModel(GL_FLAT);
431 glDisable(GL_DITHER);
432 glDisable(GL_CULL_FACE);
433
Mathias Agopiana4912602012-07-12 14:25:33 -0700434 struct pack565 {
435 inline uint16_t operator() (int r, int g, int b) const {
436 return (r<<11)|(g<<5)|b;
437 }
438 } pack565;
439
Jamie Gennis9575f602011-10-07 14:51:16 -0700440 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
441 glGenTextures(1, &mProtectedTexName);
442 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
443 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
444 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
445 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
446 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
447 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
448 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449
Mathias Agopiana4912602012-07-12 14:25:33 -0700450 // print some debugging info
451 EGLint r,g,b,a;
452 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
453 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
454 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
455 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
456 ALOGI("EGL informations:");
457 ALOGI("vendor : %s", extensions.getEglVendor());
458 ALOGI("version : %s", extensions.getEglVersion());
459 ALOGI("extensions: %s", extensions.getEglExtension());
460 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
461 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
462 ALOGI("OpenGL ES informations:");
463 ALOGI("vendor : %s", extensions.getVendor());
464 ALOGI("renderer : %s", extensions.getRenderer());
465 ALOGI("version : %s", extensions.getVersion());
466 ALOGI("extensions: %s", extensions.getExtension());
467 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
468 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
469}
470
Mathias Agopiana4912602012-07-12 14:25:33 -0700471status_t SurfaceFlinger::readyToRun()
472{
473 ALOGI( "SurfaceFlinger's main thread ready to run. "
474 "Initializing graphics H/W...");
475
Jesse Hall692c7232012-11-08 15:41:56 -0800476 Mutex::Autolock _l(mStateLock);
477
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700478 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700479 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
480 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700481
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700482 // Initialize the H/W composer object. There may or may not be an
483 // actual hardware composer underneath.
484 mHwc = new HWComposer(this,
485 *static_cast<HWComposer::EventHandler *>(this));
486
Mathias Agopiana4912602012-07-12 14:25:33 -0700487 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700488 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700489 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
490 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700491
Mathias Agopianda27af92012-09-13 18:17:13 -0700492 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
493 "couldn't create EGLContext");
494
Mathias Agopiancde87a32012-09-13 14:09:01 -0700495 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700496 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700497 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700498 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700499 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700500 // All non-virtual displays are currently considered secure.
501 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800502 createBuiltinDisplayLocked(type);
503 wp<IBinder> token = mBuiltinDisplays[i];
504
Mathias Agopianf5a33922012-09-19 18:16:22 -0700505 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700506 type, isSecure, token, new FramebufferSurface(*mHwc, i),
507 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700508 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700509 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700510 // for displays other than the main display, so we always
511 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700512 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700513 hw->acquireScreen();
514 }
515 mDisplays.add(token, hw);
516 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700517 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700518
Mathias Agopianf5a33922012-09-19 18:16:22 -0700519 // we need a GL context current in a few places, when initializing
520 // OpenGL ES (see below), or creating a layer,
521 // or when a texture is (asynchronously) destroyed, and for that
522 // we need a valid surface, so it's convenient to use the main display
523 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700524 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700525
Mathias Agopiana4912602012-07-12 14:25:33 -0700526 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700527 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
528 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800529
Mathias Agopian028508c2012-07-25 21:12:12 -0700530 // start the EventThread
531 mEventThread = new EventThread(this);
532 mEventQueue.setEventThread(mEventThread);
533
Mathias Agopian92a979a2012-08-02 18:32:23 -0700534 // initialize our drawing state
535 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700536
Mathias Agopiancde87a32012-09-13 14:09:01 -0700537
Mathias Agopiana4912602012-07-12 14:25:33 -0700538 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800539 mReadyToRunBarrier.open();
540
Andy McFadden13a082e2012-08-24 10:16:42 -0700541 // set initial conditions (e.g. unblank default device)
542 initializeDisplays();
543
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700544 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700545 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700546
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 return NO_ERROR;
548}
549
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700550int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
551 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
552 type : mHwc->allocateDisplayId();
553}
554
Mathias Agopiana67e4182012-06-19 17:26:12 -0700555void SurfaceFlinger::startBootAnim() {
556 // start boot animation
557 property_set("service.bootanim.exit", "0");
558 property_set("ctl.start", "bootanim");
559}
560
Mathias Agopiana4912602012-07-12 14:25:33 -0700561uint32_t SurfaceFlinger::getMaxTextureSize() const {
562 return mMaxTextureSize;
563}
564
565uint32_t SurfaceFlinger::getMaxViewportDims() const {
566 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
567 mMaxViewportDims[0] : mMaxViewportDims[1];
568}
569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800571
Jamie Gennis582270d2011-08-17 18:19:00 -0700572bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800573 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800574 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800575 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800576
Andy McFadden882e3a32013-01-08 16:06:15 -0800577 // We want to determine whether the IGraphicBufferProducer was created by
578 // SurfaceFlinger. Check to see if we can find it in the layer list.
Jamie Gennis134f0422011-03-08 12:18:54 -0800579 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
580 size_t count = currentLayers.size();
581 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800582 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700583 // Get the consumer interface of SurfaceFlingerConsumer's
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800584 // BufferQueue. If it's the same Binder object as the graphic
585 // buffer producer interface, return success.
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700586 sp<IBinder> lbcBinder = layer->getBufferQueue()->asBinder();
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800587 if (lbcBinder == surfaceTextureBinder) {
588 return true;
Jamie Gennis134f0422011-03-08 12:18:54 -0800589 }
590 }
591
592 // Check the layers in the purgatory. This check is here so that if a
Andy McFadden2adaf042012-12-18 09:49:45 -0800593 // GLConsumer gets destroyed before all the clients are done using it,
Jamie Gennis582270d2011-08-17 18:19:00 -0700594 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800595 // will instead fail later on when the client tries to use the surface,
596 // which should be reported as "surface XYZ returned an -ENODEV". The
597 // purgatorized layers are no less authentic than the visible ones, so this
598 // should not cause any harm.
599 size_t purgatorySize = mLayerPurgatory.size();
600 for (size_t i=0 ; i<purgatorySize ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800601 const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700602 sp<IBinder> lbcBinder = layer->getBufferQueue()->asBinder();
Mathias Agopianb79f61d2013-03-05 15:14:58 -0800603 if (lbcBinder == surfaceTextureBinder) {
604 return true;
Jamie Gennis134f0422011-03-08 12:18:54 -0800605 }
606 }
607
608 return false;
609}
610
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700611status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800612 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700613 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800614 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700615 type = i;
616 break;
617 }
618 }
619
620 if (type < 0) {
621 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700622 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700623
624 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700625 float xdpi = hwc.getDpiX(type);
626 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700627
628 // TODO: Not sure if display density should handled by SF any longer
629 class Density {
630 static int getDensityFromProperty(char const* propName) {
631 char property[PROPERTY_VALUE_MAX];
632 int density = 0;
633 if (property_get(propName, property, NULL) > 0) {
634 density = atoi(property);
635 }
636 return density;
637 }
638 public:
639 static int getEmuDensity() {
640 return getDensityFromProperty("qemu.sf.lcd_density"); }
641 static int getBuildDensity() {
642 return getDensityFromProperty("ro.sf.lcd_density"); }
643 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700644
645 if (type == DisplayDevice::DISPLAY_PRIMARY) {
646 // The density of the device is provided by a build property
647 float density = Density::getBuildDensity() / 160.0f;
648 if (density == 0) {
649 // the build doesn't provide a density -- this is wrong!
650 // use xdpi instead
651 ALOGE("ro.sf.lcd_density must be defined as a build property");
652 density = xdpi / 160.0f;
653 }
654 if (Density::getEmuDensity()) {
655 // if "qemu.sf.lcd_density" is specified, it overrides everything
656 xdpi = ydpi = density = Density::getEmuDensity();
657 density /= 160.0f;
658 }
659 info->density = density;
660
661 // TODO: this needs to go away (currently needed only by webkit)
662 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
663 info->orientation = hw->getOrientation();
664 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
665 } else {
666 // TODO: where should this value come from?
667 static const int TV_DENSITY = 213;
668 info->density = TV_DENSITY / 160.0f;
669 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700670 }
671
Mathias Agopian1604f772012-09-18 21:54:42 -0700672 info->w = hwc.getWidth(type);
673 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700674 info->xdpi = xdpi;
675 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700676 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700677
678 // All non-virtual displays are currently considered secure.
679 info->secure = true;
680
Mathias Agopian888c8222012-08-04 21:10:38 -0700681 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700682}
683
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800684// ----------------------------------------------------------------------------
685
686sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800687 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700688}
689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800691
692void SurfaceFlinger::waitForEvent() {
693 mEventQueue.waitMessage();
694}
695
696void SurfaceFlinger::signalTransaction() {
697 mEventQueue.invalidate();
698}
699
700void SurfaceFlinger::signalLayerUpdate() {
701 mEventQueue.invalidate();
702}
703
704void SurfaceFlinger::signalRefresh() {
705 mEventQueue.refresh();
706}
707
708status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
709 nsecs_t reltime, uint32_t flags) {
710 return mEventQueue.postMessage(msg, reltime);
711}
712
713status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
714 nsecs_t reltime, uint32_t flags) {
715 status_t res = mEventQueue.postMessage(msg, reltime);
716 if (res == NO_ERROR) {
717 msg->wait();
718 }
719 return res;
720}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721
Mathias Agopian4fec8732012-06-29 14:12:52 -0700722bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 return true;
725}
726
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700727void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700728 if (mEventThread == NULL) {
729 // This is a temporary workaround for b/7145521. A non-null pointer
730 // does not mean EventThread has finished initializing, so this
731 // is not a correct fix.
732 ALOGW("WARNING: EventThread not started, ignoring vsync");
733 return;
734 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700735 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
736 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700737 mEventThread->onVSyncReceived(type, timestamp);
738 }
739}
740
741void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
742 if (mEventThread == NULL) {
743 // This is a temporary workaround for b/7145521. A non-null pointer
744 // does not mean EventThread has finished initializing, so this
745 // is not a correct fix.
746 ALOGW("WARNING: EventThread not started, ignoring hotplug");
747 return;
748 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700749
Mathias Agopian148994e2012-09-19 17:31:36 -0700750 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700751 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800752 if (connected) {
753 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700754 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800755 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
756 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700757 }
758 setTransactionFlags(eDisplayTransactionNeeded);
759
Andy McFadden9e9689c2012-10-10 18:17:51 -0700760 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700761 }
Mathias Agopian86303202012-07-24 22:46:10 -0700762}
763
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700764void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
765 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700766}
767
Mathias Agopian4fec8732012-06-29 14:12:52 -0700768void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800769 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800770 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700771 case MessageQueue::INVALIDATE:
772 handleMessageTransaction();
773 handleMessageInvalidate();
774 signalRefresh();
775 break;
776 case MessageQueue::REFRESH:
777 handleMessageRefresh();
778 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800779 }
780}
781
Mathias Agopian4fec8732012-06-29 14:12:52 -0700782void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700783 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700784 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700785 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700786 }
787}
788
789void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700791 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700792}
793
794void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700795 ATRACE_CALL();
796 preComposition();
797 rebuildLayerStacks();
798 setUpHWComposer();
799 doDebugFlashRegions();
800 doComposition();
801 postComposition();
802}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700803
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804void SurfaceFlinger::doDebugFlashRegions()
805{
806 // is debugging enabled
807 if (CC_LIKELY(!mDebugRegion))
808 return;
809
810 const bool repaintEverything = mRepaintEverything;
811 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
812 const sp<DisplayDevice>& hw(mDisplays[dpy]);
813 if (hw->canDraw()) {
814 // transform the dirty region into this screen's coordinate space
815 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
816 if (!dirtyRegion.isEmpty()) {
817 // redraw the whole screen
818 doComposeSurfaces(hw, Region(hw->bounds()));
819
820 // and draw the dirty region
821 glDisable(GL_TEXTURE_EXTERNAL_OES);
822 glDisable(GL_TEXTURE_2D);
823 glDisable(GL_BLEND);
824 glColor4f(1, 0, 1, 1);
825 const int32_t height = hw->getHeight();
826 Region::const_iterator it = dirtyRegion.begin();
827 Region::const_iterator const end = dirtyRegion.end();
828 while (it != end) {
829 const Rect& r = *it++;
830 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800831 { (GLfloat) r.left, (GLfloat) (height - r.top) },
832 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
833 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
834 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700835 };
836 glVertexPointer(2, GL_FLOAT, 0, vertices);
837 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
838 }
839 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700840 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700841 }
842 }
843 }
844
845 postFramebuffer();
846
847 if (mDebugRegion > 1) {
848 usleep(mDebugRegion * 1000);
849 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700850
851 HWComposer& hwc(getHwComposer());
852 if (hwc.initCheck() == NO_ERROR) {
853 status_t err = hwc.prepare();
854 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
855 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700856}
857
858void SurfaceFlinger::preComposition()
859{
860 bool needExtraInvalidate = false;
861 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
862 const size_t count = currentLayers.size();
863 for (size_t i=0 ; i<count ; i++) {
864 if (currentLayers[i]->onPreComposition()) {
865 needExtraInvalidate = true;
866 }
867 }
868 if (needExtraInvalidate) {
869 signalLayerUpdate();
870 }
871}
872
873void SurfaceFlinger::postComposition()
874{
875 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
876 const size_t count = currentLayers.size();
877 for (size_t i=0 ; i<count ; i++) {
878 currentLayers[i]->onPostComposition();
879 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800880
881 if (mAnimCompositionPending) {
882 mAnimCompositionPending = false;
883
884 const HWComposer& hwc = getHwComposer();
885 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800886 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800887 mAnimFrameTracker.setActualPresentFence(presentFence);
888 } else {
889 // The HWC doesn't support present fences, so use the refresh
890 // timestamp instead.
891 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
892 mAnimFrameTracker.setActualPresentTime(presentTime);
893 }
894 mAnimFrameTracker.advanceFrame();
895 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700896}
897
898void SurfaceFlinger::rebuildLayerStacks() {
899 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700900 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700901 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700902 mVisibleRegionsDirty = false;
903 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700904
Mathias Agopian87baae12012-07-31 12:38:26 -0700905 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700906 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700907 Region opaqueRegion;
908 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800909 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700910 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700911 const Transform& tr(hw->getTransform());
912 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700913 if (hw->canDraw()) {
914 SurfaceFlinger::computeVisibleRegions(currentLayers,
915 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700916
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700917 const size_t count = currentLayers.size();
918 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800919 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700920 const Layer::State& s(layer->drawingState());
921 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700922 Region drawRegion(tr.transform(
923 layer->visibleNonTransparentRegion));
924 drawRegion.andSelf(bounds);
925 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700926 layersSortedByZ.add(layer);
927 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700928 }
929 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700930 }
Mathias Agopian42977342012-08-05 00:40:46 -0700931 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700932 hw->undefinedRegion.set(bounds);
933 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
934 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700935 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700936 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700937}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700938
Mathias Agopiancd60f992012-08-16 16:28:27 -0700939void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700940 HWComposer& hwc(getHwComposer());
941 if (hwc.initCheck() == NO_ERROR) {
942 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700943 if (CC_UNLIKELY(mHwWorkListDirty)) {
944 mHwWorkListDirty = false;
945 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
946 sp<const DisplayDevice> hw(mDisplays[dpy]);
947 const int32_t id = hw->getHwcDisplayId();
948 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800949 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700950 hw->getVisibleLayersSortedByZ());
951 const size_t count = currentLayers.size();
952 if (hwc.createWorkList(id, count) == NO_ERROR) {
953 HWComposer::LayerListIterator cur = hwc.begin(id);
954 const HWComposer::LayerListIterator end = hwc.end(id);
955 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800956 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700957 layer->setGeometry(hw, *cur);
958 if (mDebugDisableHWC || mDebugRegion) {
959 cur->setSkip(true);
960 }
961 }
962 }
963 }
964 }
965 }
966
967 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700968 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700969 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700970 const int32_t id = hw->getHwcDisplayId();
971 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800972 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700974 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700975 HWComposer::LayerListIterator cur = hwc.begin(id);
976 const HWComposer::LayerListIterator end = hwc.end(id);
977 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
978 /*
979 * update the per-frame h/w composer data for each layer
980 * and build the transparent region of the FB
981 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800982 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700983 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700984 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700986 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700987
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988 status_t err = hwc.prepare();
989 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
990 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700991}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700992
Mathias Agopiancd60f992012-08-16 16:28:27 -0700993void SurfaceFlinger::doComposition() {
994 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700995 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700996 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700997 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700998 if (hw->canDraw()) {
999 // transform the dirty region into this screen's coordinate space
1000 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001001
1002 // repaint the framebuffer (if needed)
1003 doDisplayComposition(hw, dirtyRegion);
1004
Mathias Agopiancd60f992012-08-16 16:28:27 -07001005 hw->dirtyRegion.clear();
1006 hw->flip(hw->swapRegion);
1007 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001008 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001009 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001010 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001011 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001012 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001013}
1014
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015void SurfaceFlinger::postFramebuffer()
1016{
Mathias Agopian841cde52012-03-01 15:44:37 -08001017 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001018
Mathias Agopiana44b0412011-10-16 18:46:35 -07001019 const nsecs_t now = systemTime();
1020 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001021
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001022 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001023 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001024 if (!hwc.supportsFramebufferTarget()) {
1025 // EGL spec says:
1026 // "surface must be bound to the calling thread's current context,
1027 // for the current rendering API."
1028 DisplayDevice::makeCurrent(mEGLDisplay,
1029 getDefaultDisplayDevice(), mEGLContext);
1030 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001031 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001032 }
1033
Mathias Agopian92a979a2012-08-02 18:32:23 -07001034 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001035 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001036 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001037 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001038 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001039 int32_t id = hw->getHwcDisplayId();
1040 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001041 HWComposer::LayerListIterator cur = hwc.begin(id);
1042 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001043 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001044 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001045 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001046 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001047 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001048 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001049 }
Jesse Hallef194142012-06-14 14:45:17 -07001050 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001051 }
1052
Mathias Agopiana44b0412011-10-16 18:46:35 -07001053 mLastSwapBufferTime = systemTime() - now;
1054 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055}
1056
Mathias Agopian87baae12012-07-31 12:38:26 -07001057void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
Mathias Agopian841cde52012-03-01 15:44:37 -08001059 ATRACE_CALL();
1060
Mathias Agopianca4d3602011-05-19 15:38:14 -07001061 Mutex::Autolock _l(mStateLock);
1062 const nsecs_t now = systemTime();
1063 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
Mathias Agopianca4d3602011-05-19 15:38:14 -07001065 // Here we're guaranteed that some transaction flags are set
1066 // so we can call handleTransactionLocked() unconditionally.
1067 // We call getTransactionFlags(), which will also clear the flags,
1068 // with mStateLock held to guarantee that mCurrentState won't change
1069 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001070
Mathias Agopiane57f2922012-08-09 16:29:12 -07001071 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001072 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001073
Mathias Agopianca4d3602011-05-19 15:38:14 -07001074 mLastTransactionTime = systemTime() - now;
1075 mDebugInTransaction = 0;
1076 invalidateHwcGeometry();
1077 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001078}
1079
Mathias Agopian87baae12012-07-31 12:38:26 -07001080void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001081{
1082 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 const size_t count = currentLayers.size();
1084
1085 /*
1086 * Traversal of the children
1087 * (perform the transaction for each of them if needed)
1088 */
1089
Mathias Agopian3559b072012-08-15 13:46:03 -07001090 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001092 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1094 if (!trFlags) continue;
1095
1096 const uint32_t flags = layer->doTransaction(0);
1097 if (flags & Layer::eVisibleRegion)
1098 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 }
1100 }
1101
1102 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001103 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 */
1105
Mathias Agopiane57f2922012-08-09 16:29:12 -07001106 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001107 // here we take advantage of Vector's copy-on-write semantics to
1108 // improve performance by skipping the transaction entirely when
1109 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001110 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1111 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001112 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001114 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001115 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001116
1117 // find the displays that were removed
1118 // (ie: in drawing state but not in current state)
1119 // also handle displays that changed
1120 // (ie: displays that are in both lists)
1121 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001122 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1123 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001124 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001125 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001126 // Call makeCurrent() on the primary display so we can
1127 // be sure that nothing associated with this display
1128 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001129 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001130 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001131 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001132 getHwComposer().disconnectDisplay(draw[i].type);
Jesse Hall7adb0f82013-03-06 16:13:49 -08001133 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1134 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001135 } else {
1136 ALOGW("trying to remove the main display");
1137 }
1138 } else {
1139 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001140 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001141 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001142 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001143 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001144 // recreating the DisplayDevice, so we just remove it
1145 // from the drawing state, so that it get re-added
1146 // below.
1147 mDisplays.removeItem(display);
1148 mDrawingState.displays.removeItemsAt(i);
1149 dc--; i--;
1150 // at this point we must loop to the next item
1151 continue;
1152 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001153
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001154 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001155 if (disp != NULL) {
1156 if (state.layerStack != draw[i].layerStack) {
1157 disp->setLayerStack(state.layerStack);
1158 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001159 if ((state.orientation != draw[i].orientation)
1160 || (state.viewport != draw[i].viewport)
1161 || (state.frame != draw[i].frame))
1162 {
1163 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001164 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001165 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001166 }
1167 }
1168 }
1169
1170 // find displays that were added
1171 // (ie: in current state but not in drawing state)
1172 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001173 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001174 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001175
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001176 sp<DisplaySurface> dispSurface;
1177 if (state.isVirtualDisplay()) {
1178 if (state.surface != NULL) {
1179 dispSurface = new VirtualDisplaySurface(
1180 *mHwc, state.type, state.surface,
1181 state.displayName);
1182 }
1183 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001184 ALOGE_IF(state.surface!=NULL,
1185 "adding a supported display, but rendering "
1186 "surface is provided (%p), ignoring it",
1187 state.surface.get());
1188
1189 // for supported (by hwc) displays we provide our
1190 // own rendering surface
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001191 dispSurface = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001192 }
1193
1194 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001195 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001196 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001197 state.type, state.isSecure, display,
1198 dispSurface, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001199 hw->setLayerStack(state.layerStack);
1200 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001201 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001202 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001203 mDisplays.add(display, hw);
Jesse Hall7adb0f82013-03-06 16:13:49 -08001204 if (state.type < DisplayDevice::NUM_DISPLAY_TYPES)
1205 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001206 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001207 }
1208 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211
Mathias Agopian84300952012-11-21 16:02:13 -08001212 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1213 // The transform hint might have changed for some layers
1214 // (either because a display has changed, or because a layer
1215 // as changed).
1216 //
1217 // Walk through all the layers in currentLayers,
1218 // and update their transform hint.
1219 //
1220 // If a layer is visible only on a single display, then that
1221 // display is used to calculate the hint, otherwise we use the
1222 // default display.
1223 //
1224 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1225 // the hint is set before we acquire a buffer from the surface texture.
1226 //
1227 // NOTE: layer transactions have taken place already, so we use their
1228 // drawing state. However, SurfaceFlinger's own transaction has not
1229 // happened yet, so we must use the current state layer list
1230 // (soon to become the drawing state list).
1231 //
1232 sp<const DisplayDevice> disp;
1233 uint32_t currentlayerStack = 0;
1234 for (size_t i=0; i<count; i++) {
1235 // NOTE: we rely on the fact that layers are sorted by
1236 // layerStack first (so we don't have to traverse the list
1237 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 const sp<Layer>& layer(currentLayers[i]);
1239 uint32_t layerStack = layer->drawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001240 if (i==0 || currentlayerStack != layerStack) {
1241 currentlayerStack = layerStack;
1242 // figure out if this layerstack is mirrored
1243 // (more than one display) if so, pick the default display,
1244 // if not, pick the only display it's on.
1245 disp.clear();
1246 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1247 sp<const DisplayDevice> hw(mDisplays[dpy]);
1248 if (hw->getLayerStack() == currentlayerStack) {
1249 if (disp == NULL) {
1250 disp = hw;
1251 } else {
1252 disp = getDefaultDisplayDevice();
1253 break;
1254 }
1255 }
1256 }
1257 }
1258 if (disp != NULL) {
1259 // presumably this means this layer is using a layerStack
1260 // that is not visible on any display
Mathias Agopian13127d82013-03-05 17:47:11 -08001261 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001262 }
1263 }
1264 }
1265
1266
Mathias Agopian3559b072012-08-15 13:46:03 -07001267 /*
1268 * Perform our own transaction if needed
1269 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001270
Mathias Agopiancd60f992012-08-16 16:28:27 -07001271 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1272 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001273 // layers have been added
1274 mVisibleRegionsDirty = true;
1275 }
1276
1277 // some layers might have been removed, so
1278 // we need to update the regions they're exposing.
1279 if (mLayersRemoved) {
1280 mLayersRemoved = false;
1281 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001282 const size_t count = previousLayers.size();
1283 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 const sp<Layer>& layer(previousLayers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001285 if (currentLayers.indexOf(layer) < 0) {
1286 // this layer is not visible anymore
1287 // TODO: we could traverse the tree from front to back and
1288 // compute the actual visible region
1289 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001290 const Layer::State& s(layer->drawingState());
1291 Region visibleReg = s.transform.transform(
1292 Region(Rect(s.active.w, s.active.h)));
1293 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001294 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 }
1297
1298 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001299}
1300
1301void SurfaceFlinger::commitTransaction()
1302{
1303 if (!mLayersPendingRemoval.isEmpty()) {
1304 // Notify removed layers now that they can't be drawn from
1305 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1306 mLayersPendingRemoval[i]->onRemoved();
1307 }
1308 mLayersPendingRemoval.clear();
1309 }
1310
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001311 // If this transaction is part of a window animation then the next frame
1312 // we composite should be considered an animation as well.
1313 mAnimCompositionPending = mAnimTransactionPending;
1314
Mathias Agopian4fec8732012-06-29 14:12:52 -07001315 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001316 mTransactionPending = false;
1317 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001318 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319}
1320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001322 const LayerVector& currentLayers, uint32_t layerStack,
1323 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324{
Mathias Agopian841cde52012-03-01 15:44:37 -08001325 ATRACE_CALL();
1326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 Region aboveOpaqueLayers;
1328 Region aboveCoveredLayers;
1329 Region dirty;
1330
Mathias Agopian87baae12012-07-31 12:38:26 -07001331 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332
1333 size_t i = currentLayers.size();
1334 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001335 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336
1337 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001338 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339
Jesse Hall01e29052013-02-19 16:13:35 -08001340 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001341 if (s.layerStack != layerStack)
1342 continue;
1343
Mathias Agopianab028732010-03-16 16:41:46 -07001344 /*
1345 * opaqueRegion: area of a surface that is fully opaque.
1346 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001348
1349 /*
1350 * visibleRegion: area of a surface that is visible on screen
1351 * and not fully transparent. This is essentially the layer's
1352 * footprint minus the opaque regions above it.
1353 * Areas covered by a translucent surface are considered visible.
1354 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001356
1357 /*
1358 * coveredRegion: area of a surface that is covered by all
1359 * visible regions above it (which includes the translucent areas).
1360 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001362
Jesse Halla8026d22012-09-25 13:25:04 -07001363 /*
1364 * transparentRegion: area of a surface that is hinted to be completely
1365 * transparent. This is only used to tell when the layer has no visible
1366 * non-transparent regions and can be removed from the layer list. It
1367 * does not affect the visibleRegion of this layer or any layers
1368 * beneath it. The hint may not be correct if apps don't respect the
1369 * SurfaceView restrictions (which, sadly, some don't).
1370 */
1371 Region transparentRegion;
1372
Mathias Agopianab028732010-03-16 16:41:46 -07001373
1374 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001375 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001376 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001377 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001379 if (!visibleRegion.isEmpty()) {
1380 // Remove the transparent area from the visible region
1381 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 const Transform tr(s.transform);
1383 if (tr.transformed()) {
1384 if (tr.preserveRects()) {
1385 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001386 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001387 } else {
1388 // transformation too complex, can't do the
1389 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001390 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001391 }
1392 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001393 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001394 }
Mathias Agopianab028732010-03-16 16:41:46 -07001395 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396
Mathias Agopianab028732010-03-16 16:41:46 -07001397 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001399 if (s.alpha==255 && !translucent &&
1400 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1401 // the opaque region is the layer's footprint
1402 opaqueRegion = visibleRegion;
1403 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404 }
1405 }
1406
Mathias Agopianab028732010-03-16 16:41:46 -07001407 // Clip the covered region to the visible region
1408 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1409
1410 // Update aboveCoveredLayers for next (lower) layer
1411 aboveCoveredLayers.orSelf(visibleRegion);
1412
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 // subtract the opaque region covered by the layers above us
1414 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415
1416 // compute this layer's dirty region
1417 if (layer->contentDirty) {
1418 // we need to invalidate the whole region
1419 dirty = visibleRegion;
1420 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001421 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 layer->contentDirty = false;
1423 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001424 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001425 * the exposed region consists of two components:
1426 * 1) what's VISIBLE now and was COVERED before
1427 * 2) what's EXPOSED now less what was EXPOSED before
1428 *
1429 * note that (1) is conservative, we start with the whole
1430 * visible region but only keep what used to be covered by
1431 * something -- which mean it may have been exposed.
1432 *
1433 * (2) handles areas that were not covered by anything but got
1434 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001435 */
Mathias Agopianab028732010-03-16 16:41:46 -07001436 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001437 const Region oldVisibleRegion = layer->visibleRegion;
1438 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001439 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1440 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 }
1442 dirty.subtractSelf(aboveOpaqueLayers);
1443
1444 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001445 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446
Mathias Agopianab028732010-03-16 16:41:46 -07001447 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001449
Jesse Halla8026d22012-09-25 13:25:04 -07001450 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 layer->setVisibleRegion(visibleRegion);
1452 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001453 layer->setVisibleNonTransparentRegion(
1454 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 }
1456
Mathias Agopian87baae12012-07-31 12:38:26 -07001457 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458}
1459
Mathias Agopian87baae12012-07-31 12:38:26 -07001460void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1461 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001462 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001463 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1464 if (hw->getLayerStack() == layerStack) {
1465 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001466 }
1467 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001468}
1469
1470void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001472 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473
Mathias Agopian4fec8732012-06-29 14:12:52 -07001474 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001475 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001476 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001477 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001478 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001479 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001480 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001481 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001482 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001483
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001484 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485}
1486
Mathias Agopianad456f92011-01-13 17:53:01 -08001487void SurfaceFlinger::invalidateHwcGeometry()
1488{
1489 mHwWorkListDirty = true;
1490}
1491
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001492
Mathias Agopiancd60f992012-08-16 16:28:27 -07001493void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001494 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495{
Mathias Agopian87baae12012-07-31 12:38:26 -07001496 Region dirtyRegion(inDirtyRegion);
1497
Mathias Agopianb8a55602009-06-26 19:06:36 -07001498 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001499 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500
Mathias Agopian42977342012-08-05 00:40:46 -07001501 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001502 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001503 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1504 // takes a rectangle, we must make sure to update that whole
1505 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001506 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001507 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001508 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001509 // We need to redraw the rectangle that will be updated
1510 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001511 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001512 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001513 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001514 } else {
1515 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001516 dirtyRegion.set(hw->bounds());
1517 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 }
1519 }
1520
Mathias Agopiancd60f992012-08-16 16:28:27 -07001521 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001523 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001524 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001525
1526 // swap buffers (presentation)
1527 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528}
1529
Mathias Agopiancd60f992012-08-16 16:28:27 -07001530void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001531{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001532 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001533 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001534 HWComposer::LayerListIterator cur = hwc.begin(id);
1535 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001536
Mathias Agopian85d751c2012-08-29 16:59:24 -07001537 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1538 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001539 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001540
1541 // set the frame buffer
1542 glMatrixMode(GL_MODELVIEW);
1543 glLoadIdentity();
1544
1545 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001546 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001547 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001548 // when using overlays, we assume a fully transparent framebuffer
1549 // NOTE: we could reduce how much we need to clear, for instance
1550 // remove where there are opaque FB layers. however, on some
1551 // GPUs doing a "clean slate" glClear might be more efficient.
1552 // We'll revisit later if needed.
1553 glClearColor(0, 0, 0, 0);
1554 glClear(GL_COLOR_BUFFER_BIT);
1555 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001556 // we start with the whole screen area
1557 const Region bounds(hw->getBounds());
1558
1559 // we remove the scissor part
1560 // we're left with the letterbox region
1561 // (common case is that letterbox ends-up being empty)
1562 const Region letterbox(bounds.subtract(hw->getScissor()));
1563
1564 // compute the area to clear
1565 Region region(hw->undefinedRegion.merge(letterbox));
1566
1567 // but limit it to the dirty region
1568 region.andSelf(dirty);
1569
Mathias Agopianb9494d52012-04-18 02:28:45 -07001570 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001571 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001572 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001573 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001574 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001575 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001576
Mathias Agopian766dc492012-10-30 18:08:06 -07001577 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1578 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001579 // scissor on the main display. It should never be needed
1580 // anyways (though in theory it could since the API allows it).
1581 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001582 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001583 if (scissor != bounds) {
1584 // scissor doesn't match the screen's dimensions, so we
1585 // need to clear everything outside of it and enable
1586 // the GL scissor so we don't draw anything where we shouldn't
1587 const GLint height = hw->getHeight();
1588 glScissor(scissor.left, height - scissor.bottom,
1589 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001590 // enable scissor for this frame
1591 glEnable(GL_SCISSOR_TEST);
1592 }
1593 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001594 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001595
Mathias Agopian85d751c2012-08-29 16:59:24 -07001596 /*
1597 * and then, render the layers targeted at the framebuffer
1598 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001599
Mathias Agopian13127d82013-03-05 17:47:11 -08001600 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001601 const size_t count = layers.size();
1602 const Transform& tr = hw->getTransform();
1603 if (cur != end) {
1604 // we're using h/w composer
1605 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001606 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001607 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001608 if (!clip.isEmpty()) {
1609 switch (cur->getCompositionType()) {
1610 case HWC_OVERLAY: {
1611 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1612 && i
1613 && layer->isOpaque()
1614 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001615 // never clear the very first layer since we're
1616 // guaranteed the FB is already cleared
1617 layer->clearWithOpenGL(hw, clip);
1618 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001619 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001620 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001621 case HWC_FRAMEBUFFER: {
1622 layer->draw(hw, clip);
1623 break;
1624 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001625 case HWC_FRAMEBUFFER_TARGET: {
1626 // this should not happen as the iterator shouldn't
1627 // let us get there.
1628 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1629 break;
1630 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001631 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001632 }
1633 layer->setAcquireFence(hw, *cur);
1634 }
1635 } else {
1636 // we're not using h/w composer
1637 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001638 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001639 const Region clip(dirty.intersect(
1640 tr.transform(layer->visibleRegion)));
1641 if (!clip.isEmpty()) {
1642 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001643 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001644 }
1645 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001646
1647 // disable scissor at the end of the frame
1648 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649}
1650
Mathias Agopian55801e42012-08-27 18:54:24 -07001651void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1652 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001654 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001655 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001656 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001657 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001658
Mathias Agopian55801e42012-08-27 18:54:24 -07001659 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001660 Region::const_iterator it = region.begin();
1661 Region::const_iterator const end = region.end();
1662 while (it != end) {
1663 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001664 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001665 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1666 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1667 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1668 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001669 };
1670 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001671 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1672 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673}
1674
Mathias Agopianac9fa422013-02-11 16:40:36 -08001675void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1676 const sp<IBinder>& handle,
Mathias Agopian13127d82013-03-05 17:47:11 -08001677 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001678{
Mathias Agopian96f08192010-06-02 23:28:45 -07001679 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001680 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001681
Mathias Agopian96f08192010-06-02 23:28:45 -07001682 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001683 Mutex::Autolock _l(mStateLock);
1684 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001685}
1686
Mathias Agopian13127d82013-03-05 17:47:11 -08001687status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688{
1689 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001690 status_t err = purgatorizeLayer_l(layer);
1691 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001692 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001693 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694}
1695
Mathias Agopian13127d82013-03-05 17:47:11 -08001696status_t SurfaceFlinger::removeLayer_l(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697{
Mathias Agopian13127d82013-03-05 17:47:11 -08001698 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001700 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 return NO_ERROR;
1702 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001703 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704}
1705
Mathias Agopian13127d82013-03-05 17:47:11 -08001706status_t SurfaceFlinger::purgatorizeLayer_l(const sp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001707{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001708 // First add the layer to the purgatory list, which makes sure it won't
1709 // go away, then remove it from the main list (through a transaction).
Mathias Agopian13127d82013-03-05 17:47:11 -08001710 ssize_t err = removeLayer_l(layer);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001711 if (err >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001712 mLayerPurgatory.add(layer);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001713 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001714
Mathias Agopian13127d82013-03-05 17:47:11 -08001715 mLayersPendingRemoval.push(layer);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001716
Mathias Agopian3d579642009-06-04 18:46:21 -07001717 // it's possible that we don't find a layer, because it might
1718 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001719 // from the user because there is a race between Client::destroySurface(),
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001720 // ~Client() and ~LayerCleaner().
Mathias Agopian9a112062009-04-17 19:36:26 -07001721 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1722}
1723
Mathias Agopiandea20b12011-05-03 17:04:02 -07001724uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1725{
1726 return android_atomic_release_load(&mTransactionFlags);
1727}
1728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1730{
1731 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1732}
1733
Mathias Agopianbb641242010-05-18 17:06:55 -07001734uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735{
1736 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1737 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001738 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 }
1740 return old;
1741}
1742
Mathias Agopian8b33f032012-07-24 20:43:54 -07001743void SurfaceFlinger::setTransactionState(
1744 const Vector<ComposerState>& state,
1745 const Vector<DisplayState>& displays,
1746 uint32_t flags)
1747{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001748 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001749 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001750 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001751
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001752 if (flags & eAnimation) {
1753 // For window updates that are part of an animation we must wait for
1754 // previous animation "frames" to be handled.
1755 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001756 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001757 if (CC_UNLIKELY(err != NO_ERROR)) {
1758 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001759 // caller after a few seconds.
1760 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1761 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001762 mAnimTransactionPending = false;
1763 break;
1764 }
1765 }
1766 }
1767
Mathias Agopiane57f2922012-08-09 16:29:12 -07001768 size_t count = displays.size();
1769 for (size_t i=0 ; i<count ; i++) {
1770 const DisplayState& s(displays[i]);
1771 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001772 }
1773
Mathias Agopiane57f2922012-08-09 16:29:12 -07001774 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001775 for (size_t i=0 ; i<count ; i++) {
1776 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001777 // Here we need to check that the interface we're given is indeed
1778 // one of our own. A malicious client could give us a NULL
1779 // IInterface, or one of its own or even one of our own but a
1780 // different type. All these situations would cause us to crash.
1781 //
1782 // NOTE: it would be better to use RTTI as we could directly check
1783 // that we have a Client*. however, RTTI is disabled in Android.
1784 if (s.client != NULL) {
1785 sp<IBinder> binder = s.client->asBinder();
1786 if (binder != NULL) {
1787 String16 desc(binder->getInterfaceDescriptor());
1788 if (desc == ISurfaceComposerClient::descriptor) {
1789 sp<Client> client( static_cast<Client *>(s.client.get()) );
1790 transactionFlags |= setClientStateLocked(client, s.state);
1791 }
1792 }
1793 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001794 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001795
Mathias Agopian386aa982011-11-07 21:58:03 -08001796 if (transactionFlags) {
1797 // this triggers the transaction
1798 setTransactionFlags(transactionFlags);
1799
1800 // if this is a synchronous transaction, wait for it to take effect
1801 // before returning.
1802 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001803 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001804 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001805 if (flags & eAnimation) {
1806 mAnimTransactionPending = true;
1807 }
1808 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001809 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1810 if (CC_UNLIKELY(err != NO_ERROR)) {
1811 // just in case something goes wrong in SF, return to the
1812 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001813 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1814 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001815 break;
1816 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001817 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 }
1819}
1820
Mathias Agopiane57f2922012-08-09 16:29:12 -07001821uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1822{
Jesse Hall9a143922012-10-04 16:29:19 -07001823 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1824 if (dpyIdx < 0)
1825 return 0;
1826
Mathias Agopiane57f2922012-08-09 16:29:12 -07001827 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001828 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001829 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001830 const uint32_t what = s.what;
1831 if (what & DisplayState::eSurfaceChanged) {
1832 if (disp.surface->asBinder() != s.surface->asBinder()) {
1833 disp.surface = s.surface;
1834 flags |= eDisplayTransactionNeeded;
1835 }
1836 }
1837 if (what & DisplayState::eLayerStackChanged) {
1838 if (disp.layerStack != s.layerStack) {
1839 disp.layerStack = s.layerStack;
1840 flags |= eDisplayTransactionNeeded;
1841 }
1842 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001843 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001844 if (disp.orientation != s.orientation) {
1845 disp.orientation = s.orientation;
1846 flags |= eDisplayTransactionNeeded;
1847 }
1848 if (disp.frame != s.frame) {
1849 disp.frame = s.frame;
1850 flags |= eDisplayTransactionNeeded;
1851 }
1852 if (disp.viewport != s.viewport) {
1853 disp.viewport = s.viewport;
1854 flags |= eDisplayTransactionNeeded;
1855 }
1856 }
1857 }
1858 return flags;
1859}
1860
1861uint32_t SurfaceFlinger::setClientStateLocked(
1862 const sp<Client>& client,
1863 const layer_state_t& s)
1864{
1865 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001866 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001867 if (layer != 0) {
1868 const uint32_t what = s.what;
1869 if (what & layer_state_t::ePositionChanged) {
1870 if (layer->setPosition(s.x, s.y))
1871 flags |= eTraversalNeeded;
1872 }
1873 if (what & layer_state_t::eLayerChanged) {
1874 // NOTE: index needs to be calculated before we update the state
1875 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1876 if (layer->setLayer(s.z)) {
1877 mCurrentState.layersSortedByZ.removeAt(idx);
1878 mCurrentState.layersSortedByZ.add(layer);
1879 // we need traversal (state changed)
1880 // AND transaction (list changed)
1881 flags |= eTransactionNeeded|eTraversalNeeded;
1882 }
1883 }
1884 if (what & layer_state_t::eSizeChanged) {
1885 if (layer->setSize(s.w, s.h)) {
1886 flags |= eTraversalNeeded;
1887 }
1888 }
1889 if (what & layer_state_t::eAlphaChanged) {
1890 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1891 flags |= eTraversalNeeded;
1892 }
1893 if (what & layer_state_t::eMatrixChanged) {
1894 if (layer->setMatrix(s.matrix))
1895 flags |= eTraversalNeeded;
1896 }
1897 if (what & layer_state_t::eTransparentRegionChanged) {
1898 if (layer->setTransparentRegionHint(s.transparentRegion))
1899 flags |= eTraversalNeeded;
1900 }
1901 if (what & layer_state_t::eVisibilityChanged) {
1902 if (layer->setFlags(s.flags, s.mask))
1903 flags |= eTraversalNeeded;
1904 }
1905 if (what & layer_state_t::eCropChanged) {
1906 if (layer->setCrop(s.crop))
1907 flags |= eTraversalNeeded;
1908 }
1909 if (what & layer_state_t::eLayerStackChanged) {
1910 // NOTE: index needs to be calculated before we update the state
1911 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1912 if (layer->setLayerStack(s.layerStack)) {
1913 mCurrentState.layersSortedByZ.removeAt(idx);
1914 mCurrentState.layersSortedByZ.add(layer);
1915 // we need traversal (state changed)
1916 // AND transaction (list changed)
1917 flags |= eTransactionNeeded|eTraversalNeeded;
1918 }
1919 }
1920 }
1921 return flags;
1922}
1923
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001924status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001925 const String8& name,
1926 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001927 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1928 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001930 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001931 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001932 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001933 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001934 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001935 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001936
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001937 status_t result = NO_ERROR;
1938
1939 sp<Layer> layer;
1940
Mathias Agopian3165cc22012-08-08 19:42:09 -07001941 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1942 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001943 result = createNormalLayer(client,
1944 name, w, h, flags, format,
1945 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001947 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001948 result = createDimLayer(client,
1949 name, w, h, flags,
1950 handle, gbp, &layer);
1951 break;
1952 default:
1953 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954 break;
1955 }
1956
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001957 if (result == NO_ERROR) {
1958 addClientLayer(client, *handle, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001959 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001961 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962}
1963
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001964status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1965 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1966 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001967{
1968 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001969 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970 case PIXEL_FORMAT_TRANSPARENT:
1971 case PIXEL_FORMAT_TRANSLUCENT:
1972 format = PIXEL_FORMAT_RGBA_8888;
1973 break;
1974 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001975#ifdef NO_RGBX_8888
1976 format = PIXEL_FORMAT_RGB_565;
1977#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001978 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001979#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001980 break;
1981 }
1982
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001983#ifdef NO_RGBX_8888
1984 if (format == PIXEL_FORMAT_RGBX_8888)
1985 format = PIXEL_FORMAT_RGBA_8888;
1986#endif
1987
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001988 *outLayer = new Layer(this, client, name, w, h, flags);
1989 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1990 if (err == NO_ERROR) {
1991 *handle = (*outLayer)->getHandle();
1992 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001993 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001994
1995 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1996 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997}
1998
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001999status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2000 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2001 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002002{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002003 *outLayer = new LayerDim(this, client, name, w, h, flags);
2004 *handle = (*outLayer)->getHandle();
2005 *gbp = (*outLayer)->getBufferQueue();
2006 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002007}
2008
Mathias Agopianac9fa422013-02-11 16:40:36 -08002009status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010{
Mathias Agopian9a112062009-04-17 19:36:26 -07002011 /*
2012 * called by the window manager, when a surface should be marked for
2013 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002014 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002015 * The surface is removed from the current and drawing lists, but placed
2016 * in the purgatory queue, so it's not destroyed right-away (we need
2017 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07002018 */
Mathias Agopian9a112062009-04-17 19:36:26 -07002019
Mathias Agopian48d819a2009-09-10 19:41:18 -07002020 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002021 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002022 sp<Layer> layer = client->getLayerUser(handle);
Daniel Lamb2675792012-02-23 14:35:13 -08002023
Mathias Agopian48d819a2009-09-10 19:41:18 -07002024 if (layer != 0) {
2025 err = purgatorizeLayer_l(layer);
2026 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07002027 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07002028 }
Mathias Agopian9a112062009-04-17 19:36:26 -07002029 }
2030 return err;
2031}
2032
Mathias Agopian13127d82013-03-05 17:47:11 -08002033status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002034{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002035 // called by ~LayerCleaner() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002036 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002037 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002038 if (l != NULL) {
2039 Mutex::Autolock _l(mStateLock);
2040 err = removeLayer_l(l);
2041 if (err == NAME_NOT_FOUND) {
2042 // The surface wasn't in the current list, which means it was
2043 // removed already, which means it is in the purgatory,
2044 // and need to be removed from there.
2045 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002046 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002047 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07002048 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002049 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002050 "error removing layer=%p (%s)", l.get(), strerror(-err));
2051 }
2052 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002053}
2054
Mathias Agopianb60314a2012-04-10 22:09:54 -07002055// ---------------------------------------------------------------------------
2056
Andy McFadden13a082e2012-08-24 10:16:42 -07002057void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002058 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002059 Vector<ComposerState> state;
2060 Vector<DisplayState> displays;
2061 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002062 d.what = DisplayState::eDisplayProjectionChanged |
2063 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002064 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002065 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002066 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002067 d.frame.makeInvalid();
2068 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002069 displays.add(d);
2070 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002071 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002072}
2073
2074void SurfaceFlinger::initializeDisplays() {
2075 class MessageScreenInitialized : public MessageBase {
2076 SurfaceFlinger* flinger;
2077 public:
2078 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2079 virtual bool handler() {
2080 flinger->onInitializeDisplays();
2081 return true;
2082 }
2083 };
2084 sp<MessageBase> msg = new MessageScreenInitialized(this);
2085 postMessageAsync(msg); // we may be called from main thread, use async message
2086}
2087
2088
Mathias Agopiancde87a32012-09-13 14:09:01 -07002089void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002090 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2091 if (hw->isScreenAcquired()) {
2092 // this is expected, e.g. when power manager wakes up during boot
2093 ALOGD(" screen was previously acquired");
2094 return;
2095 }
2096
Mathias Agopian42977342012-08-05 00:40:46 -07002097 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002098 int32_t type = hw->getDisplayType();
2099 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2100 // built-in display, tell the HWC
2101 getHwComposer().acquire(type);
2102
2103 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2104 // FIXME: eventthread only knows about the main display right now
2105 mEventThread->onScreenAcquired();
2106 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002107 }
Mathias Agopian20128302012-08-13 18:32:13 -07002108 mVisibleRegionsDirty = true;
2109 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002110}
2111
Mathias Agopiancde87a32012-09-13 14:09:01 -07002112void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002113 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2114 if (!hw->isScreenAcquired()) {
2115 ALOGD(" screen was previously released");
2116 return;
2117 }
2118
2119 hw->releaseScreen();
2120 int32_t type = hw->getDisplayType();
2121 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2122 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002123 // FIXME: eventthread only knows about the main display right now
2124 mEventThread->onScreenReleased();
2125 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002126
2127 // built-in display, tell the HWC
2128 getHwComposer().release(type);
2129 }
2130 mVisibleRegionsDirty = true;
2131 // from this point on, SF will stop drawing on this display
2132}
2133
2134void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2135 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002136 SurfaceFlinger& mFlinger;
2137 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002138 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002139 MessageScreenAcquired(SurfaceFlinger& flinger,
2140 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002141 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002142 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2143 if (hw == NULL) {
2144 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2145 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2146 ALOGW("Attempt to unblank virtual display");
2147 } else {
2148 mFlinger.onScreenAcquired(hw);
2149 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002150 return true;
2151 }
2152 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002153 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2154 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002155}
2156
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002157void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002158 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002159 SurfaceFlinger& mFlinger;
2160 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002161 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002162 MessageScreenReleased(SurfaceFlinger& flinger,
2163 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002164 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002165 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2166 if (hw == NULL) {
2167 ALOGE("Attempt to blank null display %p", mDisplay.get());
2168 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2169 ALOGW("Attempt to blank virtual display");
2170 } else {
2171 mFlinger.onScreenReleased(hw);
2172 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002173 return true;
2174 }
2175 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002176 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2177 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002178}
2179
2180// ---------------------------------------------------------------------------
2181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002182status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2183{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002184 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185 char buffer[SIZE];
2186 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002187
2188 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 snprintf(buffer, SIZE, "Permission Denial: "
2190 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2191 IPCThreadState::self()->getCallingPid(),
2192 IPCThreadState::self()->getCallingUid());
2193 result.append(buffer);
2194 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002195 // Try to get the main lock, but don't insist if we can't
2196 // (this would indicate SF is stuck, but we want to be able to
2197 // print something in dumpsys).
2198 int retry = 3;
2199 while (mStateLock.tryLock()<0 && --retry>=0) {
2200 usleep(1000000);
2201 }
2202 const bool locked(retry >= 0);
2203 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002204 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002205 "SurfaceFlinger appears to be unresponsive, "
2206 "dumping anyways (no locks held)\n");
2207 result.append(buffer);
2208 }
2209
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002210 bool dumpAll = true;
2211 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002212 size_t numArgs = args.size();
2213 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002214 if ((index < numArgs) &&
2215 (args[index] == String16("--list"))) {
2216 index++;
2217 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002218 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002219 }
2220
2221 if ((index < numArgs) &&
2222 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002223 index++;
2224 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002225 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002226 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002227
2228 if ((index < numArgs) &&
2229 (args[index] == String16("--latency-clear"))) {
2230 index++;
2231 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002232 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002233 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002234 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002235
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002236 if (dumpAll) {
2237 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002238 }
2239
Mathias Agopian9795c422009-08-26 16:36:26 -07002240 if (locked) {
2241 mStateLock.unlock();
2242 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002243 }
2244 write(fd, result.string(), result.size());
2245 return NO_ERROR;
2246}
2247
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002248void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2249 String8& result, char* buffer, size_t SIZE) const
2250{
2251 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2252 const size_t count = currentLayers.size();
2253 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002254 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002255 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2256 result.append(buffer);
2257 }
2258}
2259
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002260void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2261 String8& result, char* buffer, size_t SIZE) const
2262{
2263 String8 name;
2264 if (index < args.size()) {
2265 name = String8(args[index]);
2266 index++;
2267 }
2268
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002269 const nsecs_t period =
2270 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2271 result.appendFormat("%lld\n", period);
2272
2273 if (name.isEmpty()) {
2274 mAnimFrameTracker.dump(result);
2275 } else {
2276 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2277 const size_t count = currentLayers.size();
2278 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002279 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002280 if (name == layer->getName()) {
2281 layer->dumpStats(result, buffer, SIZE);
2282 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002283 }
2284 }
2285}
2286
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002287void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002288 String8& result, char* buffer, size_t SIZE)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002289{
2290 String8 name;
2291 if (index < args.size()) {
2292 name = String8(args[index]);
2293 index++;
2294 }
2295
2296 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2297 const size_t count = currentLayers.size();
2298 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002299 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002300 if (name.isEmpty() || (name == layer->getName())) {
2301 layer->clearStats();
2302 }
2303 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002304
2305 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002306}
2307
Andy McFadden4803b742012-09-24 19:07:20 -07002308/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2309{
2310 static const char* config =
2311 " [sf"
2312#ifdef NO_RGBX_8888
2313 " NO_RGBX_8888"
2314#endif
2315#ifdef HAS_CONTEXT_PRIORITY
2316 " HAS_CONTEXT_PRIORITY"
2317#endif
2318#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2319 " NEVER_DEFAULT_TO_ASYNC_MODE"
2320#endif
2321#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2322 " TARGET_DISABLE_TRIPLE_BUFFERING"
2323#endif
2324 "]";
2325 result.append(config);
2326}
2327
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328void SurfaceFlinger::dumpAllLocked(
2329 String8& result, char* buffer, size_t SIZE) const
2330{
2331 // figure out if we're stuck somewhere
2332 const nsecs_t now = systemTime();
2333 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2334 const nsecs_t inTransaction(mDebugInTransaction);
2335 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2336 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2337
2338 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002339 * Dump library configuration.
2340 */
2341 result.append("Build configuration:");
2342 appendSfConfigString(result);
2343 appendUiConfigString(result);
2344 appendGuiConfigString(result);
2345 result.append("\n");
2346
2347 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002348 * Dump the visible layer list
2349 */
2350 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2351 const size_t count = currentLayers.size();
2352 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2353 result.append(buffer);
2354 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002355 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356 layer->dump(result, buffer, SIZE);
2357 }
2358
2359 /*
2360 * Dump the layers in the purgatory
2361 */
2362
2363 const size_t purgatorySize = mLayerPurgatory.size();
2364 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2365 result.append(buffer);
2366 for (size_t i=0 ; i<purgatorySize ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002367 const sp<Layer>& layer(mLayerPurgatory.itemAt(i));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002368 layer->shortDump(result, buffer, SIZE);
2369 }
2370
2371 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002372 * Dump Display state
2373 */
2374
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002375 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2376 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002377 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2378 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002379 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002380 }
2381
2382 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002383 * Dump SurfaceFlinger global state
2384 */
2385
2386 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2387 result.append(buffer);
2388
Mathias Agopian888c8222012-08-04 21:10:38 -07002389 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002390 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002391 const GLExtensions& extensions(GLExtensions::getInstance());
2392 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2393 extensions.getVendor(),
2394 extensions.getRenderer(),
2395 extensions.getVersion());
2396 result.append(buffer);
2397
2398 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002399 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400 result.append(buffer);
2401
2402 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2403 result.append(buffer);
2404
Mathias Agopian42977342012-08-05 00:40:46 -07002405 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002406 snprintf(buffer, SIZE,
2407 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002408 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002409 result.append(buffer);
2410 snprintf(buffer, SIZE,
2411 " last eglSwapBuffers() time: %f us\n"
2412 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002413 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002414 " refresh-rate : %f fps\n"
2415 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002416 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002417 mLastSwapBufferTime/1000.0,
2418 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002419 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002420 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2421 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2422 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002423 result.append(buffer);
2424
2425 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2426 inSwapBuffersDuration/1000.0);
2427 result.append(buffer);
2428
2429 snprintf(buffer, SIZE, " transaction time: %f us\n",
2430 inTransactionDuration/1000.0);
2431 result.append(buffer);
2432
2433 /*
2434 * VSYNC state
2435 */
2436 mEventThread->dump(result, buffer, SIZE);
2437
2438 /*
2439 * Dump HWComposer state
2440 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002441 snprintf(buffer, SIZE, "h/w composer state:\n");
2442 result.append(buffer);
2443 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2444 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2445 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2446 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002447 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002448
2449 /*
2450 * Dump gralloc state
2451 */
2452 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2453 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002454}
2455
Mathias Agopian13127d82013-03-05 17:47:11 -08002456const Vector< sp<Layer> >&
Mathias Agopiancb558572012-10-04 15:58:54 -07002457SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002458 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002459 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2460}
2461
Keun young Park63f165f2012-08-31 10:53:36 -07002462bool SurfaceFlinger::startDdmConnection()
2463{
2464 void* libddmconnection_dso =
2465 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2466 if (!libddmconnection_dso) {
2467 return false;
2468 }
2469 void (*DdmConnection_start)(const char* name);
2470 DdmConnection_start =
2471 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2472 if (!DdmConnection_start) {
2473 dlclose(libddmconnection_dso);
2474 return false;
2475 }
2476 (*DdmConnection_start)(getServiceName());
2477 return true;
2478}
2479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480status_t SurfaceFlinger::onTransact(
2481 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2482{
2483 switch (code) {
2484 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002485 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002487 case BLANK:
2488 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489 {
2490 // codes that require permission check
2491 IPCThreadState* ipc = IPCThreadState::self();
2492 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002493 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002494 if ((uid != AID_GRAPHICS) &&
2495 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002496 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002497 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2498 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002499 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002500 break;
2501 }
2502 case CAPTURE_SCREEN:
2503 {
2504 // codes that require permission check
2505 IPCThreadState* ipc = IPCThreadState::self();
2506 const int pid = ipc->getCallingPid();
2507 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002508 if ((uid != AID_GRAPHICS) &&
2509 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002510 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002511 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2512 return PERMISSION_DENIED;
2513 }
2514 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515 }
2516 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002517
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2519 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002520 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002521 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002522 IPCThreadState* ipc = IPCThreadState::self();
2523 const int pid = ipc->getCallingPid();
2524 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002525 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002526 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 return PERMISSION_DENIED;
2528 }
2529 int n;
2530 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002531 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002532 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 return NO_ERROR;
2534 case 1002: // SHOW_UPDATES
2535 n = data.readInt32();
2536 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002537 invalidateHwcGeometry();
2538 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002539 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002540 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002541 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002542 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002544 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002545 setTransactionFlags(
2546 eTransactionNeeded|
2547 eDisplayTransactionNeeded|
2548 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002549 return NO_ERROR;
2550 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002551 case 1006:{ // send empty update
2552 signalRefresh();
2553 return NO_ERROR;
2554 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002555 case 1008: // toggle use of hw composer
2556 n = data.readInt32();
2557 mDebugDisableHWC = n ? 1 : 0;
2558 invalidateHwcGeometry();
2559 repaintEverything();
2560 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002561 case 1009: // toggle use of transform hint
2562 n = data.readInt32();
2563 mDebugDisableTransformHint = n ? 1 : 0;
2564 invalidateHwcGeometry();
2565 repaintEverything();
2566 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002568 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002569 reply->writeInt32(0);
2570 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002571 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002572 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002573 return NO_ERROR;
2574 case 1013: {
2575 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002576 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2577 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002578 }
2579 return NO_ERROR;
2580 }
2581 }
2582 return err;
2583}
2584
Mathias Agopian53331da2011-08-22 21:44:41 -07002585void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002586 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002587 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002588}
2589
Mathias Agopian59119e62010-10-11 12:37:43 -07002590// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002591// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002592// ---------------------------------------------------------------------------
2593
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002594status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2595 const sp<IGraphicBufferProducer>& producer,
2596 uint32_t reqWidth, uint32_t reqHeight,
2597 uint32_t minLayerZ, uint32_t maxLayerZ) {
2598
2599 if (CC_UNLIKELY(display == 0))
2600 return BAD_VALUE;
2601
2602 if (CC_UNLIKELY(producer == 0))
2603 return BAD_VALUE;
2604
2605 class MessageCaptureScreen : public MessageBase {
2606 SurfaceFlinger* flinger;
2607 sp<IBinder> display;
2608 sp<IGraphicBufferProducer> producer;
2609 uint32_t reqWidth, reqHeight;
2610 uint32_t minLayerZ,maxLayerZ;
2611 status_t result;
2612 public:
2613 MessageCaptureScreen(SurfaceFlinger* flinger,
2614 const sp<IBinder>& display,
2615 const sp<IGraphicBufferProducer>& producer,
2616 uint32_t reqWidth, uint32_t reqHeight,
2617 uint32_t minLayerZ, uint32_t maxLayerZ)
2618 : flinger(flinger), display(display), producer(producer),
2619 reqWidth(reqWidth), reqHeight(reqHeight),
2620 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2621 result(PERMISSION_DENIED)
2622 {
2623 }
2624 status_t getResult() const {
2625 return result;
2626 }
2627 virtual bool handler() {
2628 Mutex::Autolock _l(flinger->mStateLock);
2629 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
2630 result = flinger->captureScreenImplLocked(hw, producer,
2631 reqWidth, reqHeight, minLayerZ, maxLayerZ);
2632 return true;
2633 }
2634 };
2635
2636 sp<MessageBase> msg = new MessageCaptureScreen(this,
2637 display, producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
2638 status_t res = postMessageSync(msg);
2639 if (res == NO_ERROR) {
2640 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2641 }
2642 return res;
2643}
2644
2645status_t SurfaceFlinger::captureScreenImplLocked(
2646 const sp<const DisplayDevice>& hw,
2647 const sp<IGraphicBufferProducer>& producer,
2648 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002649 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002650{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002651 ATRACE_CALL();
2652
Mathias Agopian74c40c02010-09-29 13:02:36 -07002653 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002654 const uint32_t hw_w = hw->getWidth();
2655 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002656
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002657 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002658 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002659 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002660 return PERMISSION_DENIED;
2661 }
2662
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002663 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2664 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2665 reqWidth, reqHeight, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002666 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002667 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002668
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002669 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2670 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2671 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002672
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002673 // Create a surface to render into
2674 sp<Surface> surface = new Surface(producer);
2675 ANativeWindow* const window = surface.get();
2676
2677 // set the buffer size to what the user requested
2678 native_window_set_buffers_user_dimensions(window, reqWidth, reqHeight);
2679
2680 // and create the corresponding EGLSurface
2681 EGLSurface eglSurface = eglCreateWindowSurface(
2682 mEGLDisplay, mEGLConfig, window, NULL);
2683 if (eglSurface == EGL_NO_SURFACE) {
2684 ALOGE("captureScreenImplLocked: eglCreateWindowSurface() failed 0x%4x",
2685 eglGetError());
2686 return BAD_VALUE;
2687 }
2688
2689 if (!eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) {
2690 ALOGE("captureScreenImplLocked: eglMakeCurrent() failed 0x%4x",
2691 eglGetError());
2692 eglDestroySurface(mEGLDisplay, eglSurface);
2693 return BAD_VALUE;
2694 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002695
Mathias Agopian74c40c02010-09-29 13:02:36 -07002696 // make sure to clear all GL error flags
2697 while ( glGetError() != GL_NO_ERROR ) ;
2698
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002699 // set-up our viewport
2700 glViewport(0, 0, reqWidth, reqHeight);
2701 glMatrixMode(GL_PROJECTION);
2702 glLoadIdentity();
2703 glOrthof(0, hw_w, 0, hw_h, 0, 1);
2704 glMatrixMode(GL_MODELVIEW);
2705 glLoadIdentity();
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002706
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002707 // redraw the screen entirely...
2708 glDisable(GL_TEXTURE_EXTERNAL_OES);
2709 glDisable(GL_TEXTURE_2D);
2710 glClearColor(0,0,0,1);
2711 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002712
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002713 const LayerVector& layers( mDrawingState.layersSortedByZ );
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002714 const size_t count = layers.size();
2715 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002716 const sp<Layer>& layer(layers[i]);
Mathias Agopianb7a5b052013-03-11 20:47:24 -07002717 const Layer::State& state(layer->drawingState());
2718 if (state.layerStack == hw->getLayerStack()) {
2719 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2720 if (filtering) layer->setFiltering(true);
2721 layer->draw(hw);
2722 if (filtering) layer->setFiltering(false);
2723 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002724 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002725 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002726
Mathias Agopiand5776412013-03-08 13:54:46 -08002727 // compositionComplete is needed for older driver
2728 hw->compositionComplete();
2729
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002730 // and finishing things up...
2731 if (eglSwapBuffers(mEGLDisplay, eglSurface) != EGL_TRUE) {
2732 ALOGE("captureScreenImplLocked: eglSwapBuffers() failed 0x%4x",
2733 eglGetError());
2734 eglDestroySurface(mEGLDisplay, eglSurface);
2735 return BAD_VALUE;
2736 }
2737
2738 eglDestroySurface(mEGLDisplay, eglSurface);
2739 return NO_ERROR;
2740}
2741
2742// ---------------------------------------------------------------------------
2743// Capture screen into an IMemoryHeap (legacy)
2744// ---------------------------------------------------------------------------
2745
2746status_t SurfaceFlinger::captureScreenImplLocked(
2747 const sp<const DisplayDevice>& hw,
2748 sp<IMemoryHeap>* heap,
2749 uint32_t* w, uint32_t* h, PixelFormat* f,
2750 uint32_t sw, uint32_t sh,
2751 uint32_t minLayerZ, uint32_t maxLayerZ)
2752{
2753 ATRACE_CALL();
2754
2755 if (!GLExtensions::getInstance().haveFramebufferObject()) {
2756 return INVALID_OPERATION;
2757 }
2758
2759 // create the texture that will receive the screenshot, later we'll
2760 // attach a FBO to it so we can call glReadPixels().
2761 GLuint tname;
2762 glGenTextures(1, &tname);
2763 glBindTexture(GL_TEXTURE_2D, tname);
2764 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2765 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2766
2767 // the GLConsumer will provide the BufferQueue
2768 sp<GLConsumer> consumer = new GLConsumer(tname, true, GL_TEXTURE_2D);
2769 consumer->getBufferQueue()->setDefaultBufferFormat(HAL_PIXEL_FORMAT_RGBA_8888);
2770
2771 // call the new screenshot taking code, passing a BufferQueue to it
2772 status_t result = captureScreenImplLocked(hw,
2773 consumer->getBufferQueue(), sw, sh, minLayerZ, maxLayerZ);
2774
2775 if (result == NO_ERROR) {
2776 result = consumer->updateTexImage();
2777 if (result == NO_ERROR) {
2778 // create a FBO
2779 GLuint name;
2780 glGenFramebuffersOES(1, &name);
2781 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2782 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2783 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
2784
2785 sp<GraphicBuffer> buf(consumer->getCurrentBuffer());
2786 sw = buf->getWidth();
2787 sh = buf->getHeight();
Mathias Agopian6a531712013-03-13 15:22:11 -07002788 size_t size = sw * sh * 4;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002789
Mathias Agopian74c40c02010-09-29 13:02:36 -07002790 // allocate shared memory large enough to hold the
2791 // screen capture
2792 sp<MemoryHeapBase> base(
2793 new MemoryHeapBase(size, 0, "screen-capture") );
2794 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002795 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002796 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002797 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002798 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2799 if (glGetError() == GL_NO_ERROR) {
2800 *heap = base;
2801 *w = sw;
2802 *h = sh;
2803 *f = PIXEL_FORMAT_RGBA_8888;
2804 result = NO_ERROR;
Jeff Sharkey175264b2013-03-05 17:57:26 -08002805 } else {
2806 result = NO_MEMORY;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002807 }
2808 } else {
2809 result = NO_MEMORY;
2810 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002811
2812 // back to main framebuffer
2813 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2814 glDeleteFramebuffersOES(1, &name);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002815 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002816 }
2817
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002818 glDeleteTextures(1, &tname);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002819
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002820 DisplayDevice::makeCurrent(mEGLDisplay,
2821 getDefaultDisplayDevice(), mEGLContext);
2822
Mathias Agopian74c40c02010-09-29 13:02:36 -07002823 return result;
2824}
2825
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002826status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002827 sp<IMemoryHeap>* heap,
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002828 uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
2829 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002830 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002831{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002832 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002833 return BAD_VALUE;
2834
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002835 class MessageCaptureScreen : public MessageBase {
2836 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002837 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002838 sp<IMemoryHeap>* heap;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002839 uint32_t* outWidth;
2840 uint32_t* outHeight;
2841 PixelFormat* outFormat;
2842 uint32_t reqWidth;
2843 uint32_t reqHeight;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002844 uint32_t minLayerZ;
2845 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002846 status_t result;
2847 public:
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002848 MessageCaptureScreen(SurfaceFlinger* flinger,
2849 const sp<IBinder>& display, sp<IMemoryHeap>* heap,
2850 uint32_t* outWidth, uint32_t* outHeight, PixelFormat* outFormat,
2851 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002852 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002853 : flinger(flinger), display(display), heap(heap),
2854 outWidth(outWidth), outHeight(outHeight), outFormat(outFormat),
2855 reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002856 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2857 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002858 {
2859 }
2860 status_t getResult() const {
2861 return result;
2862 }
2863 virtual bool handler() {
2864 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002865 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
2866 result = flinger->captureScreenImplLocked(hw, heap,
2867 outWidth, outHeight, outFormat,
2868 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002869 return true;
2870 }
2871 };
2872
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002873 sp<MessageBase> msg = new MessageCaptureScreen(this, display, heap,
2874 outWidth, outHeight, outFormat,
2875 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002876 status_t res = postMessageSync(msg);
2877 if (res == NO_ERROR) {
2878 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2879 }
2880 return res;
2881}
2882
2883// ---------------------------------------------------------------------------
2884
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002885SurfaceFlinger::LayerVector::LayerVector() {
2886}
2887
2888SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002889 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002890}
2891
2892int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2893 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002894{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002895 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002896 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2897 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002898
2899 uint32_t ls = l->currentState().layerStack;
2900 uint32_t rs = r->currentState().layerStack;
2901 if (ls != rs)
2902 return ls - rs;
2903
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002904 uint32_t lz = l->currentState().z;
2905 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002906 if (lz != rz)
2907 return lz - rz;
2908
2909 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002910}
2911
2912// ---------------------------------------------------------------------------
2913
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002914SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2915 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002916}
2917
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002918SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002919 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002920 viewport.makeInvalid();
2921 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002922}
2923
2924// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002925
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002926}; // namespace android