blob: caabf1d71cf7788234087fb08a4f5b9a591483a6 [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 Agopian921e6ac2012-07-23 23:11:29 -070042#include <gui/SurfaceTextureClient.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"
Mathias Agopian118d0242011-10-13 16:02:48 -070065#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopiana4912602012-07-12 14:25:33 -070068#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070069#include "DisplayHardware/HWComposer.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<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
506 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
Andy McFadden2adaf042012-12-18 09:49:45 -0800507 static_cast< sp<IGraphicBufferProducer> >(fbs->getBufferQueue()));
Mathias Agopianf5a33922012-09-19 18:16:22 -0700508 sp<DisplayDevice> hw = new DisplayDevice(this,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700509 type, isSecure, token, stc, fbs, mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700510 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700511 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700512 // for displays other than the main display, so we always
513 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700514 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700515 hw->acquireScreen();
516 }
517 mDisplays.add(token, hw);
518 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700519 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700520
Mathias Agopianf5a33922012-09-19 18:16:22 -0700521 // we need a GL context current in a few places, when initializing
522 // OpenGL ES (see below), or creating a layer,
523 // or when a texture is (asynchronously) destroyed, and for that
524 // we need a valid surface, so it's convenient to use the main display
525 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700526 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700527
Mathias Agopiana4912602012-07-12 14:25:33 -0700528 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700529 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
530 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800531
Mathias Agopian028508c2012-07-25 21:12:12 -0700532 // start the EventThread
533 mEventThread = new EventThread(this);
534 mEventQueue.setEventThread(mEventThread);
535
Mathias Agopian92a979a2012-08-02 18:32:23 -0700536 // initialize our drawing state
537 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700538
Mathias Agopiancde87a32012-09-13 14:09:01 -0700539
Mathias Agopiana4912602012-07-12 14:25:33 -0700540 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800541 mReadyToRunBarrier.open();
542
Andy McFadden13a082e2012-08-24 10:16:42 -0700543 // set initial conditions (e.g. unblank default device)
544 initializeDisplays();
545
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700546 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700547 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 return NO_ERROR;
550}
551
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700552int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
553 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
554 type : mHwc->allocateDisplayId();
555}
556
Mathias Agopiana67e4182012-06-19 17:26:12 -0700557void SurfaceFlinger::startBootAnim() {
558 // start boot animation
559 property_set("service.bootanim.exit", "0");
560 property_set("ctl.start", "bootanim");
561}
562
Mathias Agopiana4912602012-07-12 14:25:33 -0700563uint32_t SurfaceFlinger::getMaxTextureSize() const {
564 return mMaxTextureSize;
565}
566
567uint32_t SurfaceFlinger::getMaxViewportDims() const {
568 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
569 mMaxViewportDims[0] : mMaxViewportDims[1];
570}
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800573
Jamie Gennis582270d2011-08-17 18:19:00 -0700574bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800575 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800576 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800577 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800578
Andy McFadden882e3a32013-01-08 16:06:15 -0800579 // We want to determine whether the IGraphicBufferProducer was created by
580 // SurfaceFlinger. Check to see if we can find it in the layer list.
Jamie Gennis134f0422011-03-08 12:18:54 -0800581 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
582 size_t count = currentLayers.size();
583 for (size_t i=0 ; i<count ; i++) {
584 const sp<LayerBase>& layer(currentLayers[i]);
585 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700586 if (lbc != NULL) {
Andy McFadden882e3a32013-01-08 16:06:15 -0800587 // If this is an instance of Layer (as opposed to, say, LayerDim),
588 // we will get the consumer interface of SurfaceFlingerConsumer's
589 // BufferQueue. If it's the same Binder object as the graphic
590 // buffer producer interface, return success.
Jamie Gennis582270d2011-08-17 18:19:00 -0700591 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
592 if (lbcBinder == surfaceTextureBinder) {
593 return true;
594 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800595 }
596 }
597
598 // Check the layers in the purgatory. This check is here so that if a
Andy McFadden2adaf042012-12-18 09:49:45 -0800599 // GLConsumer gets destroyed before all the clients are done using it,
Jamie Gennis582270d2011-08-17 18:19:00 -0700600 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800601 // will instead fail later on when the client tries to use the surface,
602 // which should be reported as "surface XYZ returned an -ENODEV". The
603 // purgatorized layers are no less authentic than the visible ones, so this
604 // should not cause any harm.
605 size_t purgatorySize = mLayerPurgatory.size();
606 for (size_t i=0 ; i<purgatorySize ; i++) {
607 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
608 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700609 if (lbc != NULL) {
610 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
611 if (lbcBinder == surfaceTextureBinder) {
612 return true;
613 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800614 }
615 }
616
617 return false;
618}
619
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700620status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800621 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700622 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800623 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700624 type = i;
625 break;
626 }
627 }
628
629 if (type < 0) {
630 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700631 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700632
633 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700634 float xdpi = hwc.getDpiX(type);
635 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700636
637 // TODO: Not sure if display density should handled by SF any longer
638 class Density {
639 static int getDensityFromProperty(char const* propName) {
640 char property[PROPERTY_VALUE_MAX];
641 int density = 0;
642 if (property_get(propName, property, NULL) > 0) {
643 density = atoi(property);
644 }
645 return density;
646 }
647 public:
648 static int getEmuDensity() {
649 return getDensityFromProperty("qemu.sf.lcd_density"); }
650 static int getBuildDensity() {
651 return getDensityFromProperty("ro.sf.lcd_density"); }
652 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700653
654 if (type == DisplayDevice::DISPLAY_PRIMARY) {
655 // The density of the device is provided by a build property
656 float density = Density::getBuildDensity() / 160.0f;
657 if (density == 0) {
658 // the build doesn't provide a density -- this is wrong!
659 // use xdpi instead
660 ALOGE("ro.sf.lcd_density must be defined as a build property");
661 density = xdpi / 160.0f;
662 }
663 if (Density::getEmuDensity()) {
664 // if "qemu.sf.lcd_density" is specified, it overrides everything
665 xdpi = ydpi = density = Density::getEmuDensity();
666 density /= 160.0f;
667 }
668 info->density = density;
669
670 // TODO: this needs to go away (currently needed only by webkit)
671 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
672 info->orientation = hw->getOrientation();
673 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
674 } else {
675 // TODO: where should this value come from?
676 static const int TV_DENSITY = 213;
677 info->density = TV_DENSITY / 160.0f;
678 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700679 }
680
Mathias Agopian1604f772012-09-18 21:54:42 -0700681 info->w = hwc.getWidth(type);
682 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700683 info->xdpi = xdpi;
684 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700685 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700686
687 // All non-virtual displays are currently considered secure.
688 info->secure = true;
689
Mathias Agopian888c8222012-08-04 21:10:38 -0700690 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700691}
692
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800693// ----------------------------------------------------------------------------
694
695sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800696 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700697}
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800700
701void SurfaceFlinger::waitForEvent() {
702 mEventQueue.waitMessage();
703}
704
705void SurfaceFlinger::signalTransaction() {
706 mEventQueue.invalidate();
707}
708
709void SurfaceFlinger::signalLayerUpdate() {
710 mEventQueue.invalidate();
711}
712
713void SurfaceFlinger::signalRefresh() {
714 mEventQueue.refresh();
715}
716
717status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
718 nsecs_t reltime, uint32_t flags) {
719 return mEventQueue.postMessage(msg, reltime);
720}
721
722status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
723 nsecs_t reltime, uint32_t flags) {
724 status_t res = mEventQueue.postMessage(msg, reltime);
725 if (res == NO_ERROR) {
726 msg->wait();
727 }
728 return res;
729}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730
Mathias Agopian4fec8732012-06-29 14:12:52 -0700731bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 return true;
734}
735
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700736void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700737 if (mEventThread == NULL) {
738 // This is a temporary workaround for b/7145521. A non-null pointer
739 // does not mean EventThread has finished initializing, so this
740 // is not a correct fix.
741 ALOGW("WARNING: EventThread not started, ignoring vsync");
742 return;
743 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700744 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
745 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700746 mEventThread->onVSyncReceived(type, timestamp);
747 }
748}
749
750void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
751 if (mEventThread == NULL) {
752 // This is a temporary workaround for b/7145521. A non-null pointer
753 // does not mean EventThread has finished initializing, so this
754 // is not a correct fix.
755 ALOGW("WARNING: EventThread not started, ignoring hotplug");
756 return;
757 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700758
Mathias Agopian148994e2012-09-19 17:31:36 -0700759 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700760 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800761 if (connected) {
762 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700763 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800764 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
765 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700766 }
767 setTransactionFlags(eDisplayTransactionNeeded);
768
Andy McFadden9e9689c2012-10-10 18:17:51 -0700769 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700770 }
Mathias Agopian86303202012-07-24 22:46:10 -0700771}
772
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700773void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
774 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700775}
776
Mathias Agopian4fec8732012-06-29 14:12:52 -0700777void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800778 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800779 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700780 case MessageQueue::INVALIDATE:
781 handleMessageTransaction();
782 handleMessageInvalidate();
783 signalRefresh();
784 break;
785 case MessageQueue::REFRESH:
786 handleMessageRefresh();
787 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800788 }
789}
790
Mathias Agopian4fec8732012-06-29 14:12:52 -0700791void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700792 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700793 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700794 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700795 }
796}
797
798void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700799 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700800 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700801}
802
803void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804 ATRACE_CALL();
805 preComposition();
806 rebuildLayerStacks();
807 setUpHWComposer();
808 doDebugFlashRegions();
809 doComposition();
810 postComposition();
811}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700812
Mathias Agopiancd60f992012-08-16 16:28:27 -0700813void SurfaceFlinger::doDebugFlashRegions()
814{
815 // is debugging enabled
816 if (CC_LIKELY(!mDebugRegion))
817 return;
818
819 const bool repaintEverything = mRepaintEverything;
820 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
821 const sp<DisplayDevice>& hw(mDisplays[dpy]);
822 if (hw->canDraw()) {
823 // transform the dirty region into this screen's coordinate space
824 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
825 if (!dirtyRegion.isEmpty()) {
826 // redraw the whole screen
827 doComposeSurfaces(hw, Region(hw->bounds()));
828
829 // and draw the dirty region
830 glDisable(GL_TEXTURE_EXTERNAL_OES);
831 glDisable(GL_TEXTURE_2D);
832 glDisable(GL_BLEND);
833 glColor4f(1, 0, 1, 1);
834 const int32_t height = hw->getHeight();
835 Region::const_iterator it = dirtyRegion.begin();
836 Region::const_iterator const end = dirtyRegion.end();
837 while (it != end) {
838 const Rect& r = *it++;
839 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800840 { (GLfloat) r.left, (GLfloat) (height - r.top) },
841 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
842 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
843 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 };
845 glVertexPointer(2, GL_FLOAT, 0, vertices);
846 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
847 }
848 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700849 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700850 }
851 }
852 }
853
854 postFramebuffer();
855
856 if (mDebugRegion > 1) {
857 usleep(mDebugRegion * 1000);
858 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700859
860 HWComposer& hwc(getHwComposer());
861 if (hwc.initCheck() == NO_ERROR) {
862 status_t err = hwc.prepare();
863 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
864 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700865}
866
867void SurfaceFlinger::preComposition()
868{
869 bool needExtraInvalidate = false;
870 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
871 const size_t count = currentLayers.size();
872 for (size_t i=0 ; i<count ; i++) {
873 if (currentLayers[i]->onPreComposition()) {
874 needExtraInvalidate = true;
875 }
876 }
877 if (needExtraInvalidate) {
878 signalLayerUpdate();
879 }
880}
881
882void SurfaceFlinger::postComposition()
883{
884 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
885 const size_t count = currentLayers.size();
886 for (size_t i=0 ; i<count ; i++) {
887 currentLayers[i]->onPostComposition();
888 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800889
890 if (mAnimCompositionPending) {
891 mAnimCompositionPending = false;
892
893 const HWComposer& hwc = getHwComposer();
894 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
895 if (presentFence != NULL) {
896 mAnimFrameTracker.setActualPresentFence(presentFence);
897 } else {
898 // The HWC doesn't support present fences, so use the refresh
899 // timestamp instead.
900 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
901 mAnimFrameTracker.setActualPresentTime(presentTime);
902 }
903 mAnimFrameTracker.advanceFrame();
904 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905}
906
907void SurfaceFlinger::rebuildLayerStacks() {
908 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700909 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700911 mVisibleRegionsDirty = false;
912 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700913
Mathias Agopian87baae12012-07-31 12:38:26 -0700914 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700915 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700916 Region opaqueRegion;
917 Region dirtyRegion;
918 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700919 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700920 const Transform& tr(hw->getTransform());
921 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700922 if (hw->canDraw()) {
923 SurfaceFlinger::computeVisibleRegions(currentLayers,
924 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700925
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700926 const size_t count = currentLayers.size();
927 for (size_t i=0 ; i<count ; i++) {
928 const sp<LayerBase>& layer(currentLayers[i]);
929 const Layer::State& s(layer->drawingState());
930 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700931 Region drawRegion(tr.transform(
932 layer->visibleNonTransparentRegion));
933 drawRegion.andSelf(bounds);
934 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700935 layersSortedByZ.add(layer);
936 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700937 }
938 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700939 }
Mathias Agopian42977342012-08-05 00:40:46 -0700940 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700941 hw->undefinedRegion.set(bounds);
942 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
943 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700944 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700945 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700946}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700947
Mathias Agopiancd60f992012-08-16 16:28:27 -0700948void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949 HWComposer& hwc(getHwComposer());
950 if (hwc.initCheck() == NO_ERROR) {
951 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700952 if (CC_UNLIKELY(mHwWorkListDirty)) {
953 mHwWorkListDirty = false;
954 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
955 sp<const DisplayDevice> hw(mDisplays[dpy]);
956 const int32_t id = hw->getHwcDisplayId();
957 if (id >= 0) {
958 const Vector< sp<LayerBase> >& currentLayers(
959 hw->getVisibleLayersSortedByZ());
960 const size_t count = currentLayers.size();
961 if (hwc.createWorkList(id, count) == NO_ERROR) {
962 HWComposer::LayerListIterator cur = hwc.begin(id);
963 const HWComposer::LayerListIterator end = hwc.end(id);
964 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
965 const sp<LayerBase>& layer(currentLayers[i]);
966 layer->setGeometry(hw, *cur);
967 if (mDebugDisableHWC || mDebugRegion) {
968 cur->setSkip(true);
969 }
970 }
971 }
972 }
973 }
974 }
975
976 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700977 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700978 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700979 const int32_t id = hw->getHwcDisplayId();
980 if (id >= 0) {
981 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700983 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700984 HWComposer::LayerListIterator cur = hwc.begin(id);
985 const HWComposer::LayerListIterator end = hwc.end(id);
986 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
987 /*
988 * update the per-frame h/w composer data for each layer
989 * and build the transparent region of the FB
990 */
991 const sp<LayerBase>& layer(currentLayers[i]);
992 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700993 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700994 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700995 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700996
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700997 status_t err = hwc.prepare();
998 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
999 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001000}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001001
Mathias Agopiancd60f992012-08-16 16:28:27 -07001002void SurfaceFlinger::doComposition() {
1003 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001004 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001005 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001006 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -07001007 if (hw->canDraw()) {
1008 // transform the dirty region into this screen's coordinate space
1009 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001010
1011 // repaint the framebuffer (if needed)
1012 doDisplayComposition(hw, dirtyRegion);
1013
Mathias Agopiancd60f992012-08-16 16:28:27 -07001014 hw->dirtyRegion.clear();
1015 hw->flip(hw->swapRegion);
1016 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001017 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001019 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001020 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001021 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001022}
1023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024void SurfaceFlinger::postFramebuffer()
1025{
Mathias Agopian841cde52012-03-01 15:44:37 -08001026 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001027
Mathias Agopiana44b0412011-10-16 18:46:35 -07001028 const nsecs_t now = systemTime();
1029 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001030
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001031 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001032 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001033 if (!hwc.supportsFramebufferTarget()) {
1034 // EGL spec says:
1035 // "surface must be bound to the calling thread's current context,
1036 // for the current rendering API."
1037 DisplayDevice::makeCurrent(mEGLDisplay,
1038 getDefaultDisplayDevice(), mEGLContext);
1039 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001040 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001041 }
1042
Mathias Agopian92a979a2012-08-02 18:32:23 -07001043 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001044 sp<const DisplayDevice> hw(mDisplays[dpy]);
1045 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001046 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001047 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001048 int32_t id = hw->getHwcDisplayId();
1049 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001050 HWComposer::LayerListIterator cur = hwc.begin(id);
1051 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001052 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001053 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001054 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001055 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001056 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001057 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001058 }
Jesse Hallef194142012-06-14 14:45:17 -07001059 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001060 }
1061
Mathias Agopiana44b0412011-10-16 18:46:35 -07001062 mLastSwapBufferTime = systemTime() - now;
1063 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064}
1065
Mathias Agopian87baae12012-07-31 12:38:26 -07001066void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067{
Mathias Agopian841cde52012-03-01 15:44:37 -08001068 ATRACE_CALL();
1069
Mathias Agopianca4d3602011-05-19 15:38:14 -07001070 Mutex::Autolock _l(mStateLock);
1071 const nsecs_t now = systemTime();
1072 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073
Mathias Agopianca4d3602011-05-19 15:38:14 -07001074 // Here we're guaranteed that some transaction flags are set
1075 // so we can call handleTransactionLocked() unconditionally.
1076 // We call getTransactionFlags(), which will also clear the flags,
1077 // with mStateLock held to guarantee that mCurrentState won't change
1078 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001079
Mathias Agopiane57f2922012-08-09 16:29:12 -07001080 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001082
Mathias Agopianca4d3602011-05-19 15:38:14 -07001083 mLastTransactionTime = systemTime() - now;
1084 mDebugInTransaction = 0;
1085 invalidateHwcGeometry();
1086 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001087}
1088
Mathias Agopian87baae12012-07-31 12:38:26 -07001089void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001090{
1091 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 const size_t count = currentLayers.size();
1093
1094 /*
1095 * Traversal of the children
1096 * (perform the transaction for each of them if needed)
1097 */
1098
Mathias Agopian3559b072012-08-15 13:46:03 -07001099 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian84300952012-11-21 16:02:13 -08001101 const sp<LayerBase>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1103 if (!trFlags) continue;
1104
1105 const uint32_t flags = layer->doTransaction(0);
1106 if (flags & Layer::eVisibleRegion)
1107 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 }
1109 }
1110
1111 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001112 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 */
1114
Mathias Agopiane57f2922012-08-09 16:29:12 -07001115 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001116 // here we take advantage of Vector's copy-on-write semantics to
1117 // improve performance by skipping the transaction entirely when
1118 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001119 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1120 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001121 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001123 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001124 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001125
1126 // find the displays that were removed
1127 // (ie: in drawing state but not in current state)
1128 // also handle displays that changed
1129 // (ie: displays that are in both lists)
1130 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001131 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1132 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001133 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001134 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001135 // Call makeCurrent() on the primary display so we can
1136 // be sure that nothing associated with this display
1137 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001138 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001139 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001140 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001141 getHwComposer().disconnectDisplay(draw[i].type);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001142 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001143 } else {
1144 ALOGW("trying to remove the main display");
1145 }
1146 } else {
1147 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001148 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001149 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001150 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001151 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001152 // recreating the DisplayDevice, so we just remove it
1153 // from the drawing state, so that it get re-added
1154 // below.
1155 mDisplays.removeItem(display);
1156 mDrawingState.displays.removeItemsAt(i);
1157 dc--; i--;
1158 // at this point we must loop to the next item
1159 continue;
1160 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001161
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001162 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001163 if (disp != NULL) {
1164 if (state.layerStack != draw[i].layerStack) {
1165 disp->setLayerStack(state.layerStack);
1166 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001167 if ((state.orientation != draw[i].orientation)
1168 || (state.viewport != draw[i].viewport)
1169 || (state.frame != draw[i].frame))
1170 {
1171 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001172 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001173 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001174 }
1175 }
1176 }
1177
1178 // find displays that were added
1179 // (ie: in current state but not in drawing state)
1180 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001181 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001182 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001183
1184 sp<FramebufferSurface> fbs;
1185 sp<SurfaceTextureClient> stc;
1186 if (!state.isVirtualDisplay()) {
1187
1188 ALOGE_IF(state.surface!=NULL,
1189 "adding a supported display, but rendering "
1190 "surface is provided (%p), ignoring it",
1191 state.surface.get());
1192
1193 // for supported (by hwc) displays we provide our
1194 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001195 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001196 stc = new SurfaceTextureClient(
Andy McFadden2adaf042012-12-18 09:49:45 -08001197 static_cast< sp<IGraphicBufferProducer> >(
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001198 fbs->getBufferQueue()));
Mathias Agopiancde87a32012-09-13 14:09:01 -07001199 } else {
1200 if (state.surface != NULL) {
1201 stc = new SurfaceTextureClient(state.surface);
1202 }
1203 }
1204
1205 const wp<IBinder>& display(curr.keyAt(i));
1206 if (stc != NULL) {
1207 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall692c7232012-11-08 15:41:56 -08001208 state.type, state.isSecure, display, stc, fbs,
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001209 mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001210 hw->setLayerStack(state.layerStack);
1211 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001212 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001213 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001214 mDisplays.add(display, hw);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001215 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001216 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001217 }
1218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001220 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221
Mathias Agopian84300952012-11-21 16:02:13 -08001222 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1223 // The transform hint might have changed for some layers
1224 // (either because a display has changed, or because a layer
1225 // as changed).
1226 //
1227 // Walk through all the layers in currentLayers,
1228 // and update their transform hint.
1229 //
1230 // If a layer is visible only on a single display, then that
1231 // display is used to calculate the hint, otherwise we use the
1232 // default display.
1233 //
1234 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1235 // the hint is set before we acquire a buffer from the surface texture.
1236 //
1237 // NOTE: layer transactions have taken place already, so we use their
1238 // drawing state. However, SurfaceFlinger's own transaction has not
1239 // happened yet, so we must use the current state layer list
1240 // (soon to become the drawing state list).
1241 //
1242 sp<const DisplayDevice> disp;
1243 uint32_t currentlayerStack = 0;
1244 for (size_t i=0; i<count; i++) {
1245 // NOTE: we rely on the fact that layers are sorted by
1246 // layerStack first (so we don't have to traverse the list
1247 // of displays for every layer).
1248 const sp<LayerBase>& layerBase(currentLayers[i]);
1249 uint32_t layerStack = layerBase->drawingState().layerStack;
1250 if (i==0 || currentlayerStack != layerStack) {
1251 currentlayerStack = layerStack;
1252 // figure out if this layerstack is mirrored
1253 // (more than one display) if so, pick the default display,
1254 // if not, pick the only display it's on.
1255 disp.clear();
1256 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1257 sp<const DisplayDevice> hw(mDisplays[dpy]);
1258 if (hw->getLayerStack() == currentlayerStack) {
1259 if (disp == NULL) {
1260 disp = hw;
1261 } else {
1262 disp = getDefaultDisplayDevice();
1263 break;
1264 }
1265 }
1266 }
1267 }
1268 if (disp != NULL) {
1269 // presumably this means this layer is using a layerStack
1270 // that is not visible on any display
1271 layerBase->updateTransformHint(disp);
1272 }
1273 }
1274 }
1275
1276
Mathias Agopian3559b072012-08-15 13:46:03 -07001277 /*
1278 * Perform our own transaction if needed
1279 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001280
Mathias Agopiancd60f992012-08-16 16:28:27 -07001281 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1282 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001283 // layers have been added
1284 mVisibleRegionsDirty = true;
1285 }
1286
1287 // some layers might have been removed, so
1288 // we need to update the regions they're exposing.
1289 if (mLayersRemoved) {
1290 mLayersRemoved = false;
1291 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001292 const size_t count = previousLayers.size();
1293 for (size_t i=0 ; i<count ; i++) {
1294 const sp<LayerBase>& layer(previousLayers[i]);
1295 if (currentLayers.indexOf(layer) < 0) {
1296 // this layer is not visible anymore
1297 // TODO: we could traverse the tree from front to back and
1298 // compute the actual visible region
1299 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001300 const Layer::State& s(layer->drawingState());
1301 Region visibleReg = s.transform.transform(
1302 Region(Rect(s.active.w, s.active.h)));
1303 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 }
1307
1308 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001309}
1310
1311void SurfaceFlinger::commitTransaction()
1312{
1313 if (!mLayersPendingRemoval.isEmpty()) {
1314 // Notify removed layers now that they can't be drawn from
1315 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1316 mLayersPendingRemoval[i]->onRemoved();
1317 }
1318 mLayersPendingRemoval.clear();
1319 }
1320
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001321 // If this transaction is part of a window animation then the next frame
1322 // we composite should be considered an animation as well.
1323 mAnimCompositionPending = mAnimTransactionPending;
1324
Mathias Agopian4fec8732012-06-29 14:12:52 -07001325 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001326 mTransactionPending = false;
1327 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001328 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329}
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001332 const LayerVector& currentLayers, uint32_t layerStack,
1333 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334{
Mathias Agopian841cde52012-03-01 15:44:37 -08001335 ATRACE_CALL();
1336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 Region aboveOpaqueLayers;
1338 Region aboveCoveredLayers;
1339 Region dirty;
1340
Mathias Agopian87baae12012-07-31 12:38:26 -07001341 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342
1343 size_t i = currentLayers.size();
1344 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001345 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346
1347 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001348 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349
Mathias Agopian87baae12012-07-31 12:38:26 -07001350 // only consider the layers on the given later stack
1351 if (s.layerStack != layerStack)
1352 continue;
1353
Mathias Agopianab028732010-03-16 16:41:46 -07001354 /*
1355 * opaqueRegion: area of a surface that is fully opaque.
1356 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001358
1359 /*
1360 * visibleRegion: area of a surface that is visible on screen
1361 * and not fully transparent. This is essentially the layer's
1362 * footprint minus the opaque regions above it.
1363 * Areas covered by a translucent surface are considered visible.
1364 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001366
1367 /*
1368 * coveredRegion: area of a surface that is covered by all
1369 * visible regions above it (which includes the translucent areas).
1370 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001372
Jesse Halla8026d22012-09-25 13:25:04 -07001373 /*
1374 * transparentRegion: area of a surface that is hinted to be completely
1375 * transparent. This is only used to tell when the layer has no visible
1376 * non-transparent regions and can be removed from the layer list. It
1377 * does not affect the visibleRegion of this layer or any layers
1378 * beneath it. The hint may not be correct if apps don't respect the
1379 * SurfaceView restrictions (which, sadly, some don't).
1380 */
1381 Region transparentRegion;
1382
Mathias Agopianab028732010-03-16 16:41:46 -07001383
1384 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001385 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001386 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001387 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001389 if (!visibleRegion.isEmpty()) {
1390 // Remove the transparent area from the visible region
1391 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001392 const Transform tr(s.transform);
1393 if (tr.transformed()) {
1394 if (tr.preserveRects()) {
1395 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001396 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001397 } else {
1398 // transformation too complex, can't do the
1399 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001400 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 }
1402 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001403 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 }
Mathias Agopianab028732010-03-16 16:41:46 -07001405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406
Mathias Agopianab028732010-03-16 16:41:46 -07001407 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001408 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001409 if (s.alpha==255 && !translucent &&
1410 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1411 // the opaque region is the layer's footprint
1412 opaqueRegion = visibleRegion;
1413 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 }
1415 }
1416
Mathias Agopianab028732010-03-16 16:41:46 -07001417 // Clip the covered region to the visible region
1418 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1419
1420 // Update aboveCoveredLayers for next (lower) layer
1421 aboveCoveredLayers.orSelf(visibleRegion);
1422
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 // subtract the opaque region covered by the layers above us
1424 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425
1426 // compute this layer's dirty region
1427 if (layer->contentDirty) {
1428 // we need to invalidate the whole region
1429 dirty = visibleRegion;
1430 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001431 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 layer->contentDirty = false;
1433 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001434 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001435 * the exposed region consists of two components:
1436 * 1) what's VISIBLE now and was COVERED before
1437 * 2) what's EXPOSED now less what was EXPOSED before
1438 *
1439 * note that (1) is conservative, we start with the whole
1440 * visible region but only keep what used to be covered by
1441 * something -- which mean it may have been exposed.
1442 *
1443 * (2) handles areas that were not covered by anything but got
1444 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001445 */
Mathias Agopianab028732010-03-16 16:41:46 -07001446 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 const Region oldVisibleRegion = layer->visibleRegion;
1448 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001449 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1450 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 }
1452 dirty.subtractSelf(aboveOpaqueLayers);
1453
1454 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001455 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456
Mathias Agopianab028732010-03-16 16:41:46 -07001457 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001459
Jesse Halla8026d22012-09-25 13:25:04 -07001460 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 layer->setVisibleRegion(visibleRegion);
1462 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001463 layer->setVisibleNonTransparentRegion(
1464 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 }
1466
Mathias Agopian87baae12012-07-31 12:38:26 -07001467 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopian87baae12012-07-31 12:38:26 -07001470void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1471 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001472 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001473 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1474 if (hw->getLayerStack() == layerStack) {
1475 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001476 }
1477 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001478}
1479
1480void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001482 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483
Mathias Agopian4fec8732012-06-29 14:12:52 -07001484 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001485 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001486 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001487 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001488 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001489 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001490 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001491 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001492 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001493
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001494 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495}
1496
Mathias Agopianad456f92011-01-13 17:53:01 -08001497void SurfaceFlinger::invalidateHwcGeometry()
1498{
1499 mHwWorkListDirty = true;
1500}
1501
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001502
Mathias Agopiancd60f992012-08-16 16:28:27 -07001503void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001504 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505{
Mathias Agopian87baae12012-07-31 12:38:26 -07001506 Region dirtyRegion(inDirtyRegion);
1507
Mathias Agopianb8a55602009-06-26 19:06:36 -07001508 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001509 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510
Mathias Agopian42977342012-08-05 00:40:46 -07001511 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001512 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001513 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1514 // takes a rectangle, we must make sure to update that whole
1515 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001516 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001517 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001518 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001519 // We need to redraw the rectangle that will be updated
1520 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001521 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001522 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001523 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001524 } else {
1525 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001526 dirtyRegion.set(hw->bounds());
1527 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 }
1529 }
1530
Mathias Agopiancd60f992012-08-16 16:28:27 -07001531 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001533 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001534 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001535
1536 // swap buffers (presentation)
1537 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538}
1539
Mathias Agopiancd60f992012-08-16 16:28:27 -07001540void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001541{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001542 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001543 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001544 HWComposer::LayerListIterator cur = hwc.begin(id);
1545 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001546
Mathias Agopian85d751c2012-08-29 16:59:24 -07001547 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1548 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001549 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001550
1551 // set the frame buffer
1552 glMatrixMode(GL_MODELVIEW);
1553 glLoadIdentity();
1554
1555 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001556 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001557 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001558 // when using overlays, we assume a fully transparent framebuffer
1559 // NOTE: we could reduce how much we need to clear, for instance
1560 // remove where there are opaque FB layers. however, on some
1561 // GPUs doing a "clean slate" glClear might be more efficient.
1562 // We'll revisit later if needed.
1563 glClearColor(0, 0, 0, 0);
1564 glClear(GL_COLOR_BUFFER_BIT);
1565 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001566 // we start with the whole screen area
1567 const Region bounds(hw->getBounds());
1568
1569 // we remove the scissor part
1570 // we're left with the letterbox region
1571 // (common case is that letterbox ends-up being empty)
1572 const Region letterbox(bounds.subtract(hw->getScissor()));
1573
1574 // compute the area to clear
1575 Region region(hw->undefinedRegion.merge(letterbox));
1576
1577 // but limit it to the dirty region
1578 region.andSelf(dirty);
1579
Mathias Agopianb9494d52012-04-18 02:28:45 -07001580 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001581 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001582 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001583 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001584 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001585 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001586
Mathias Agopian766dc492012-10-30 18:08:06 -07001587 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1588 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001589 // scissor on the main display. It should never be needed
1590 // anyways (though in theory it could since the API allows it).
1591 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001592 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001593 if (scissor != bounds) {
1594 // scissor doesn't match the screen's dimensions, so we
1595 // need to clear everything outside of it and enable
1596 // the GL scissor so we don't draw anything where we shouldn't
1597 const GLint height = hw->getHeight();
1598 glScissor(scissor.left, height - scissor.bottom,
1599 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001600 // enable scissor for this frame
1601 glEnable(GL_SCISSOR_TEST);
1602 }
1603 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001604 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001605
Mathias Agopian85d751c2012-08-29 16:59:24 -07001606 /*
1607 * and then, render the layers targeted at the framebuffer
1608 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001609
Mathias Agopian85d751c2012-08-29 16:59:24 -07001610 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1611 const size_t count = layers.size();
1612 const Transform& tr = hw->getTransform();
1613 if (cur != end) {
1614 // we're using h/w composer
1615 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001616 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001617 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001618 if (!clip.isEmpty()) {
1619 switch (cur->getCompositionType()) {
1620 case HWC_OVERLAY: {
1621 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1622 && i
1623 && layer->isOpaque()
1624 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001625 // never clear the very first layer since we're
1626 // guaranteed the FB is already cleared
1627 layer->clearWithOpenGL(hw, clip);
1628 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001629 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001630 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001631 case HWC_FRAMEBUFFER: {
1632 layer->draw(hw, clip);
1633 break;
1634 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001635 case HWC_FRAMEBUFFER_TARGET: {
1636 // this should not happen as the iterator shouldn't
1637 // let us get there.
1638 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1639 break;
1640 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001641 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001642 }
1643 layer->setAcquireFence(hw, *cur);
1644 }
1645 } else {
1646 // we're not using h/w composer
1647 for (size_t i=0 ; i<count ; ++i) {
1648 const sp<LayerBase>& layer(layers[i]);
1649 const Region clip(dirty.intersect(
1650 tr.transform(layer->visibleRegion)));
1651 if (!clip.isEmpty()) {
1652 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001653 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001654 }
1655 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001656
1657 // disable scissor at the end of the frame
1658 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659}
1660
Mathias Agopian55801e42012-08-27 18:54:24 -07001661void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1662 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001664 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001665 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001666 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001667 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001668
Mathias Agopian55801e42012-08-27 18:54:24 -07001669 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001670 Region::const_iterator it = region.begin();
1671 Region::const_iterator const end = region.end();
1672 while (it != end) {
1673 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001674 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001675 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1676 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1677 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1678 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001679 };
1680 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001681 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1682 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683}
1684
Mathias Agopian96f08192010-06-02 23:28:45 -07001685ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1686 const sp<LayerBaseClient>& lbc)
1687{
Mathias Agopian96f08192010-06-02 23:28:45 -07001688 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001689 size_t name = client->attachLayer(lbc);
1690
Mathias Agopian96f08192010-06-02 23:28:45 -07001691 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001692 Mutex::Autolock _l(mStateLock);
1693 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001694
Mathias Agopian4f113742011-05-03 16:21:41 -07001695 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001696}
1697
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001698status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699{
1700 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001701 status_t err = purgatorizeLayer_l(layer);
1702 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001703 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001704 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705}
1706
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001707status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708{
1709 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1710 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001711 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 return NO_ERROR;
1713 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001714 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715}
1716
Mathias Agopian9a112062009-04-17 19:36:26 -07001717status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1718{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001719 // First add the layer to the purgatory list, which makes sure it won't
1720 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001721 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001722 if (err >= 0) {
1723 mLayerPurgatory.add(layerBase);
1724 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001725
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001726 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001727
Mathias Agopian3d579642009-06-04 18:46:21 -07001728 // it's possible that we don't find a layer, because it might
1729 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001730 // from the user because there is a race between Client::destroySurface(),
1731 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001732 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1733}
1734
Mathias Agopiandea20b12011-05-03 17:04:02 -07001735uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1736{
1737 return android_atomic_release_load(&mTransactionFlags);
1738}
1739
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1741{
1742 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1743}
1744
Mathias Agopianbb641242010-05-18 17:06:55 -07001745uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746{
1747 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1748 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001749 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 }
1751 return old;
1752}
1753
Mathias Agopian8b33f032012-07-24 20:43:54 -07001754void SurfaceFlinger::setTransactionState(
1755 const Vector<ComposerState>& state,
1756 const Vector<DisplayState>& displays,
1757 uint32_t flags)
1758{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001759 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001760 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001761 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001762
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001763 if (flags & eAnimation) {
1764 // For window updates that are part of an animation we must wait for
1765 // previous animation "frames" to be handled.
1766 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001767 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001768 if (CC_UNLIKELY(err != NO_ERROR)) {
1769 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001770 // caller after a few seconds.
1771 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1772 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001773 mAnimTransactionPending = false;
1774 break;
1775 }
1776 }
1777 }
1778
Mathias Agopiane57f2922012-08-09 16:29:12 -07001779 size_t count = displays.size();
1780 for (size_t i=0 ; i<count ; i++) {
1781 const DisplayState& s(displays[i]);
1782 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001783 }
1784
Mathias Agopiane57f2922012-08-09 16:29:12 -07001785 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001786 for (size_t i=0 ; i<count ; i++) {
1787 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001788 // Here we need to check that the interface we're given is indeed
1789 // one of our own. A malicious client could give us a NULL
1790 // IInterface, or one of its own or even one of our own but a
1791 // different type. All these situations would cause us to crash.
1792 //
1793 // NOTE: it would be better to use RTTI as we could directly check
1794 // that we have a Client*. however, RTTI is disabled in Android.
1795 if (s.client != NULL) {
1796 sp<IBinder> binder = s.client->asBinder();
1797 if (binder != NULL) {
1798 String16 desc(binder->getInterfaceDescriptor());
1799 if (desc == ISurfaceComposerClient::descriptor) {
1800 sp<Client> client( static_cast<Client *>(s.client.get()) );
1801 transactionFlags |= setClientStateLocked(client, s.state);
1802 }
1803 }
1804 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001805 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001806
Mathias Agopian386aa982011-11-07 21:58:03 -08001807 if (transactionFlags) {
1808 // this triggers the transaction
1809 setTransactionFlags(transactionFlags);
1810
1811 // if this is a synchronous transaction, wait for it to take effect
1812 // before returning.
1813 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001814 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001815 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001816 if (flags & eAnimation) {
1817 mAnimTransactionPending = true;
1818 }
1819 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001820 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1821 if (CC_UNLIKELY(err != NO_ERROR)) {
1822 // just in case something goes wrong in SF, return to the
1823 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001824 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1825 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001826 break;
1827 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001828 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001829 }
1830}
1831
Mathias Agopiane57f2922012-08-09 16:29:12 -07001832uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1833{
Jesse Hall9a143922012-10-04 16:29:19 -07001834 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1835 if (dpyIdx < 0)
1836 return 0;
1837
Mathias Agopiane57f2922012-08-09 16:29:12 -07001838 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001839 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001840 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001841 const uint32_t what = s.what;
1842 if (what & DisplayState::eSurfaceChanged) {
1843 if (disp.surface->asBinder() != s.surface->asBinder()) {
1844 disp.surface = s.surface;
1845 flags |= eDisplayTransactionNeeded;
1846 }
1847 }
1848 if (what & DisplayState::eLayerStackChanged) {
1849 if (disp.layerStack != s.layerStack) {
1850 disp.layerStack = s.layerStack;
1851 flags |= eDisplayTransactionNeeded;
1852 }
1853 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001854 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001855 if (disp.orientation != s.orientation) {
1856 disp.orientation = s.orientation;
1857 flags |= eDisplayTransactionNeeded;
1858 }
1859 if (disp.frame != s.frame) {
1860 disp.frame = s.frame;
1861 flags |= eDisplayTransactionNeeded;
1862 }
1863 if (disp.viewport != s.viewport) {
1864 disp.viewport = s.viewport;
1865 flags |= eDisplayTransactionNeeded;
1866 }
1867 }
1868 }
1869 return flags;
1870}
1871
1872uint32_t SurfaceFlinger::setClientStateLocked(
1873 const sp<Client>& client,
1874 const layer_state_t& s)
1875{
1876 uint32_t flags = 0;
1877 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1878 if (layer != 0) {
1879 const uint32_t what = s.what;
1880 if (what & layer_state_t::ePositionChanged) {
1881 if (layer->setPosition(s.x, s.y))
1882 flags |= eTraversalNeeded;
1883 }
1884 if (what & layer_state_t::eLayerChanged) {
1885 // NOTE: index needs to be calculated before we update the state
1886 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1887 if (layer->setLayer(s.z)) {
1888 mCurrentState.layersSortedByZ.removeAt(idx);
1889 mCurrentState.layersSortedByZ.add(layer);
1890 // we need traversal (state changed)
1891 // AND transaction (list changed)
1892 flags |= eTransactionNeeded|eTraversalNeeded;
1893 }
1894 }
1895 if (what & layer_state_t::eSizeChanged) {
1896 if (layer->setSize(s.w, s.h)) {
1897 flags |= eTraversalNeeded;
1898 }
1899 }
1900 if (what & layer_state_t::eAlphaChanged) {
1901 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1902 flags |= eTraversalNeeded;
1903 }
1904 if (what & layer_state_t::eMatrixChanged) {
1905 if (layer->setMatrix(s.matrix))
1906 flags |= eTraversalNeeded;
1907 }
1908 if (what & layer_state_t::eTransparentRegionChanged) {
1909 if (layer->setTransparentRegionHint(s.transparentRegion))
1910 flags |= eTraversalNeeded;
1911 }
1912 if (what & layer_state_t::eVisibilityChanged) {
1913 if (layer->setFlags(s.flags, s.mask))
1914 flags |= eTraversalNeeded;
1915 }
1916 if (what & layer_state_t::eCropChanged) {
1917 if (layer->setCrop(s.crop))
1918 flags |= eTraversalNeeded;
1919 }
1920 if (what & layer_state_t::eLayerStackChanged) {
1921 // NOTE: index needs to be calculated before we update the state
1922 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1923 if (layer->setLayerStack(s.layerStack)) {
1924 mCurrentState.layersSortedByZ.removeAt(idx);
1925 mCurrentState.layersSortedByZ.add(layer);
1926 // we need traversal (state changed)
1927 // AND transaction (list changed)
1928 flags |= eTransactionNeeded|eTraversalNeeded;
1929 }
1930 }
1931 }
1932 return flags;
1933}
1934
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001935sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001936 ISurfaceComposerClient::surface_data_t* params,
1937 const String8& name,
1938 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001939 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 uint32_t flags)
1941{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001942 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001943 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001944
1945 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001946 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001947 int(w), int(h));
1948 return surfaceHandle;
1949 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001950
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001951 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001952 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1953 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001954 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001956 case ISurfaceComposerClient::eFXSurfaceBlur:
1957 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001958 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001960 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001961 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001962 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963 }
1964
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001965 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001966 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001967 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001968 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001969 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001970 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001971 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001972 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001973 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001974 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975 }
1976
1977 return surfaceHandle;
1978}
1979
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001980sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001981 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001982 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001983 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984{
1985 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001986 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987 case PIXEL_FORMAT_TRANSPARENT:
1988 case PIXEL_FORMAT_TRANSLUCENT:
1989 format = PIXEL_FORMAT_RGBA_8888;
1990 break;
1991 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001992#ifdef NO_RGBX_8888
1993 format = PIXEL_FORMAT_RGB_565;
1994#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001995 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001996#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997 break;
1998 }
1999
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002000#ifdef NO_RGBX_8888
2001 if (format == PIXEL_FORMAT_RGBX_8888)
2002 format = PIXEL_FORMAT_RGBA_8888;
2003#endif
2004
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002005 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07002006 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002007 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002008 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002009 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010 }
2011 return layer;
2012}
2013
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002014sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002015 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07002016 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002017{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002018 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07002019 return layer;
2020}
2021
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002022sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002023 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07002024 uint32_t w, uint32_t h, uint32_t flags)
2025{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002026 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002027 return layer;
2028}
2029
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002030status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002031{
Mathias Agopian9a112062009-04-17 19:36:26 -07002032 /*
2033 * called by the window manager, when a surface should be marked for
2034 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002035 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002036 * The surface is removed from the current and drawing lists, but placed
2037 * in the purgatory queue, so it's not destroyed right-away (we need
2038 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07002039 */
Mathias Agopian9a112062009-04-17 19:36:26 -07002040
Mathias Agopian48d819a2009-09-10 19:41:18 -07002041 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002042 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002043 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08002044
Mathias Agopian48d819a2009-09-10 19:41:18 -07002045 if (layer != 0) {
2046 err = purgatorizeLayer_l(layer);
2047 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07002048 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07002049 }
Mathias Agopian9a112062009-04-17 19:36:26 -07002050 }
2051 return err;
2052}
2053
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002054status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002055{
Mathias Agopian759fdb22009-07-02 17:33:40 -07002056 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002057 status_t err = NO_ERROR;
2058 sp<LayerBaseClient> l(layer.promote());
2059 if (l != NULL) {
2060 Mutex::Autolock _l(mStateLock);
2061 err = removeLayer_l(l);
2062 if (err == NAME_NOT_FOUND) {
2063 // The surface wasn't in the current list, which means it was
2064 // removed already, which means it is in the purgatory,
2065 // and need to be removed from there.
2066 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002067 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002068 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07002069 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002070 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002071 "error removing layer=%p (%s)", l.get(), strerror(-err));
2072 }
2073 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074}
2075
Mathias Agopianb60314a2012-04-10 22:09:54 -07002076// ---------------------------------------------------------------------------
2077
Andy McFadden13a082e2012-08-24 10:16:42 -07002078void SurfaceFlinger::onInitializeDisplays() {
2079 // reset screen orientation
2080 Vector<ComposerState> state;
2081 Vector<DisplayState> displays;
2082 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002083 d.what = DisplayState::eDisplayProjectionChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002084 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07002085 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002086 d.frame.makeInvalid();
2087 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002088 displays.add(d);
2089 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002090 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002091}
2092
2093void SurfaceFlinger::initializeDisplays() {
2094 class MessageScreenInitialized : public MessageBase {
2095 SurfaceFlinger* flinger;
2096 public:
2097 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2098 virtual bool handler() {
2099 flinger->onInitializeDisplays();
2100 return true;
2101 }
2102 };
2103 sp<MessageBase> msg = new MessageScreenInitialized(this);
2104 postMessageAsync(msg); // we may be called from main thread, use async message
2105}
2106
2107
Mathias Agopiancde87a32012-09-13 14:09:01 -07002108void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002109 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2110 if (hw->isScreenAcquired()) {
2111 // this is expected, e.g. when power manager wakes up during boot
2112 ALOGD(" screen was previously acquired");
2113 return;
2114 }
2115
Mathias Agopian42977342012-08-05 00:40:46 -07002116 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002117 int32_t type = hw->getDisplayType();
2118 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2119 // built-in display, tell the HWC
2120 getHwComposer().acquire(type);
2121
2122 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2123 // FIXME: eventthread only knows about the main display right now
2124 mEventThread->onScreenAcquired();
2125 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002126 }
Mathias Agopian20128302012-08-13 18:32:13 -07002127 mVisibleRegionsDirty = true;
2128 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002129}
2130
Mathias Agopiancde87a32012-09-13 14:09:01 -07002131void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002132 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2133 if (!hw->isScreenAcquired()) {
2134 ALOGD(" screen was previously released");
2135 return;
2136 }
2137
2138 hw->releaseScreen();
2139 int32_t type = hw->getDisplayType();
2140 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2141 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002142 // FIXME: eventthread only knows about the main display right now
2143 mEventThread->onScreenReleased();
2144 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002145
2146 // built-in display, tell the HWC
2147 getHwComposer().release(type);
2148 }
2149 mVisibleRegionsDirty = true;
2150 // from this point on, SF will stop drawing on this display
2151}
2152
2153void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2154 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002155 SurfaceFlinger& mFlinger;
2156 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002157 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002158 MessageScreenAcquired(SurfaceFlinger& flinger,
2159 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002160 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002161 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2162 if (hw == NULL) {
2163 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2164 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2165 ALOGW("Attempt to unblank virtual display");
2166 } else {
2167 mFlinger.onScreenAcquired(hw);
2168 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002169 return true;
2170 }
2171 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002172 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2173 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174}
2175
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002176void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002177 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002178 SurfaceFlinger& mFlinger;
2179 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002180 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002181 MessageScreenReleased(SurfaceFlinger& flinger,
2182 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002183 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002184 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2185 if (hw == NULL) {
2186 ALOGE("Attempt to blank null display %p", mDisplay.get());
2187 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2188 ALOGW("Attempt to blank virtual display");
2189 } else {
2190 mFlinger.onScreenReleased(hw);
2191 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002192 return true;
2193 }
2194 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002195 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2196 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002197}
2198
2199// ---------------------------------------------------------------------------
2200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2202{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002203 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002204 char buffer[SIZE];
2205 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002206
2207 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002208 snprintf(buffer, SIZE, "Permission Denial: "
2209 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2210 IPCThreadState::self()->getCallingPid(),
2211 IPCThreadState::self()->getCallingUid());
2212 result.append(buffer);
2213 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002214 // Try to get the main lock, but don't insist if we can't
2215 // (this would indicate SF is stuck, but we want to be able to
2216 // print something in dumpsys).
2217 int retry = 3;
2218 while (mStateLock.tryLock()<0 && --retry>=0) {
2219 usleep(1000000);
2220 }
2221 const bool locked(retry >= 0);
2222 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002223 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002224 "SurfaceFlinger appears to be unresponsive, "
2225 "dumping anyways (no locks held)\n");
2226 result.append(buffer);
2227 }
2228
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002229 bool dumpAll = true;
2230 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002231 size_t numArgs = args.size();
2232 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002233 if ((index < numArgs) &&
2234 (args[index] == String16("--list"))) {
2235 index++;
2236 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002237 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002238 }
2239
2240 if ((index < numArgs) &&
2241 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002242 index++;
2243 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002244 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002245 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002246
2247 if ((index < numArgs) &&
2248 (args[index] == String16("--latency-clear"))) {
2249 index++;
2250 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002251 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002252 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002253 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002254
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002255 if (dumpAll) {
2256 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002257 }
2258
Mathias Agopian9795c422009-08-26 16:36:26 -07002259 if (locked) {
2260 mStateLock.unlock();
2261 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002262 }
2263 write(fd, result.string(), result.size());
2264 return NO_ERROR;
2265}
2266
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002267void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2268 String8& result, char* buffer, size_t SIZE) const
2269{
2270 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2271 const size_t count = currentLayers.size();
2272 for (size_t i=0 ; i<count ; i++) {
2273 const sp<LayerBase>& layer(currentLayers[i]);
2274 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2275 result.append(buffer);
2276 }
2277}
2278
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002279void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2280 String8& result, char* buffer, size_t SIZE) const
2281{
2282 String8 name;
2283 if (index < args.size()) {
2284 name = String8(args[index]);
2285 index++;
2286 }
2287
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002288 const nsecs_t period =
2289 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2290 result.appendFormat("%lld\n", period);
2291
2292 if (name.isEmpty()) {
2293 mAnimFrameTracker.dump(result);
2294 } else {
2295 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2296 const size_t count = currentLayers.size();
2297 for (size_t i=0 ; i<count ; i++) {
2298 const sp<LayerBase>& layer(currentLayers[i]);
2299 if (name == layer->getName()) {
2300 layer->dumpStats(result, buffer, SIZE);
2301 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002302 }
2303 }
2304}
2305
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002306void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002307 String8& result, char* buffer, size_t SIZE)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002308{
2309 String8 name;
2310 if (index < args.size()) {
2311 name = String8(args[index]);
2312 index++;
2313 }
2314
2315 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2316 const size_t count = currentLayers.size();
2317 for (size_t i=0 ; i<count ; i++) {
2318 const sp<LayerBase>& layer(currentLayers[i]);
2319 if (name.isEmpty() || (name == layer->getName())) {
2320 layer->clearStats();
2321 }
2322 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002323
2324 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002325}
2326
Andy McFadden4803b742012-09-24 19:07:20 -07002327/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2328{
2329 static const char* config =
2330 " [sf"
2331#ifdef NO_RGBX_8888
2332 " NO_RGBX_8888"
2333#endif
2334#ifdef HAS_CONTEXT_PRIORITY
2335 " HAS_CONTEXT_PRIORITY"
2336#endif
2337#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2338 " NEVER_DEFAULT_TO_ASYNC_MODE"
2339#endif
2340#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2341 " TARGET_DISABLE_TRIPLE_BUFFERING"
2342#endif
2343 "]";
2344 result.append(config);
2345}
2346
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002347void SurfaceFlinger::dumpAllLocked(
2348 String8& result, char* buffer, size_t SIZE) const
2349{
2350 // figure out if we're stuck somewhere
2351 const nsecs_t now = systemTime();
2352 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2353 const nsecs_t inTransaction(mDebugInTransaction);
2354 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2355 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2356
2357 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002358 * Dump library configuration.
2359 */
2360 result.append("Build configuration:");
2361 appendSfConfigString(result);
2362 appendUiConfigString(result);
2363 appendGuiConfigString(result);
2364 result.append("\n");
2365
2366 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002367 * Dump the visible layer list
2368 */
2369 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2370 const size_t count = currentLayers.size();
2371 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2372 result.append(buffer);
2373 for (size_t i=0 ; i<count ; i++) {
2374 const sp<LayerBase>& layer(currentLayers[i]);
2375 layer->dump(result, buffer, SIZE);
2376 }
2377
2378 /*
2379 * Dump the layers in the purgatory
2380 */
2381
2382 const size_t purgatorySize = mLayerPurgatory.size();
2383 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2384 result.append(buffer);
2385 for (size_t i=0 ; i<purgatorySize ; i++) {
2386 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2387 layer->shortDump(result, buffer, SIZE);
2388 }
2389
2390 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002391 * Dump Display state
2392 */
2393
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002394 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2395 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002396 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2397 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002398 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002399 }
2400
2401 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402 * Dump SurfaceFlinger global state
2403 */
2404
2405 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2406 result.append(buffer);
2407
Mathias Agopian888c8222012-08-04 21:10:38 -07002408 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002409 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 const GLExtensions& extensions(GLExtensions::getInstance());
2411 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2412 extensions.getVendor(),
2413 extensions.getRenderer(),
2414 extensions.getVersion());
2415 result.append(buffer);
2416
2417 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002418 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002419 result.append(buffer);
2420
2421 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2422 result.append(buffer);
2423
Mathias Agopian42977342012-08-05 00:40:46 -07002424 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002425 snprintf(buffer, SIZE,
2426 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002427 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002428 result.append(buffer);
2429 snprintf(buffer, SIZE,
2430 " last eglSwapBuffers() time: %f us\n"
2431 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002432 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002433 " refresh-rate : %f fps\n"
2434 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002435 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002436 mLastSwapBufferTime/1000.0,
2437 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002438 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002439 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2440 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2441 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002442 result.append(buffer);
2443
2444 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2445 inSwapBuffersDuration/1000.0);
2446 result.append(buffer);
2447
2448 snprintf(buffer, SIZE, " transaction time: %f us\n",
2449 inTransactionDuration/1000.0);
2450 result.append(buffer);
2451
2452 /*
2453 * VSYNC state
2454 */
2455 mEventThread->dump(result, buffer, SIZE);
2456
2457 /*
2458 * Dump HWComposer state
2459 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002460 snprintf(buffer, SIZE, "h/w composer state:\n");
2461 result.append(buffer);
2462 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2463 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2464 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2465 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002466 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002467
2468 /*
2469 * Dump gralloc state
2470 */
2471 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2472 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002473}
2474
Mathias Agopiancb558572012-10-04 15:58:54 -07002475const Vector< sp<LayerBase> >&
2476SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002477 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002478 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2479}
2480
Keun young Park63f165f2012-08-31 10:53:36 -07002481bool SurfaceFlinger::startDdmConnection()
2482{
2483 void* libddmconnection_dso =
2484 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2485 if (!libddmconnection_dso) {
2486 return false;
2487 }
2488 void (*DdmConnection_start)(const char* name);
2489 DdmConnection_start =
2490 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2491 if (!DdmConnection_start) {
2492 dlclose(libddmconnection_dso);
2493 return false;
2494 }
2495 (*DdmConnection_start)(getServiceName());
2496 return true;
2497}
2498
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002499status_t SurfaceFlinger::onTransact(
2500 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2501{
2502 switch (code) {
2503 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002504 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002506 case BLANK:
2507 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 {
2509 // codes that require permission check
2510 IPCThreadState* ipc = IPCThreadState::self();
2511 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002512 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002513 if ((uid != AID_GRAPHICS) &&
2514 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002515 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002516 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2517 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002519 break;
2520 }
2521 case CAPTURE_SCREEN:
2522 {
2523 // codes that require permission check
2524 IPCThreadState* ipc = IPCThreadState::self();
2525 const int pid = ipc->getCallingPid();
2526 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002527 if ((uid != AID_GRAPHICS) &&
2528 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002529 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002530 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2531 return PERMISSION_DENIED;
2532 }
2533 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534 }
2535 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2538 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002539 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002540 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002541 IPCThreadState* ipc = IPCThreadState::self();
2542 const int pid = ipc->getCallingPid();
2543 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002544 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002545 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546 return PERMISSION_DENIED;
2547 }
2548 int n;
2549 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002550 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002551 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002552 return NO_ERROR;
2553 case 1002: // SHOW_UPDATES
2554 n = data.readInt32();
2555 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002556 invalidateHwcGeometry();
2557 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002558 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002560 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002561 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002563 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002564 setTransactionFlags(
2565 eTransactionNeeded|
2566 eDisplayTransactionNeeded|
2567 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002568 return NO_ERROR;
2569 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002570 case 1006:{ // send empty update
2571 signalRefresh();
2572 return NO_ERROR;
2573 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002574 case 1008: // toggle use of hw composer
2575 n = data.readInt32();
2576 mDebugDisableHWC = n ? 1 : 0;
2577 invalidateHwcGeometry();
2578 repaintEverything();
2579 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002580 case 1009: // toggle use of transform hint
2581 n = data.readInt32();
2582 mDebugDisableTransformHint = n ? 1 : 0;
2583 invalidateHwcGeometry();
2584 repaintEverything();
2585 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002586 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002587 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002588 reply->writeInt32(0);
2589 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002590 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002591 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002592 return NO_ERROR;
2593 case 1013: {
2594 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002595 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2596 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002597 }
2598 return NO_ERROR;
2599 }
2600 }
2601 return err;
2602}
2603
Mathias Agopian53331da2011-08-22 21:44:41 -07002604void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002605 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002606 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002607}
2608
Mathias Agopian59119e62010-10-11 12:37:43 -07002609// ---------------------------------------------------------------------------
2610
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002611status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002612 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2613{
2614 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002615 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002616}
2617
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002618status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002619 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002620{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002621 ATRACE_CALL();
2622
Mathias Agopian59119e62010-10-11 12:37:43 -07002623 if (!GLExtensions::getInstance().haveFramebufferObject())
2624 return INVALID_OPERATION;
2625
2626 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002627 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2628 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002629 const uint32_t hw_w = hw->getWidth();
2630 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002631 GLfloat u = 1;
2632 GLfloat v = 1;
2633
2634 // make sure to clear all GL error flags
2635 while ( glGetError() != GL_NO_ERROR ) ;
2636
2637 // create a FBO
2638 GLuint name, tname;
2639 glGenTextures(1, &tname);
2640 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002641 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2642 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002643 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2644 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002645 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002646 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002647 GLint tw = (2 << (31 - clz(hw_w)));
2648 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002649 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2650 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002651 u = GLfloat(hw_w) / tw;
2652 v = GLfloat(hw_h) / th;
2653 }
2654 glGenFramebuffersOES(1, &name);
2655 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002656 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2657 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002658
Mathias Agopianbae92d02012-09-28 01:00:47 -07002659 DisplayDevice::setViewportAndProjection(hw);
2660
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002661 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002662 glDisable(GL_TEXTURE_EXTERNAL_OES);
2663 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002664 glClearColor(0,0,0,1);
2665 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002666 glMatrixMode(GL_MODELVIEW);
2667 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002668 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002669 const size_t count = layers.size();
2670 for (size_t i=0 ; i<count ; ++i) {
2671 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002672 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002673 }
2674
Mathias Agopian42977342012-08-05 00:40:46 -07002675 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002676
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002677 // back to main framebuffer
2678 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002679 glDeleteFramebuffersOES(1, &name);
2680
2681 *textureName = tname;
2682 *uOut = u;
2683 *vOut = v;
2684 return NO_ERROR;
2685}
2686
2687// ---------------------------------------------------------------------------
2688
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002689status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002690 sp<IMemoryHeap>* heap,
2691 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002692 uint32_t sw, uint32_t sh,
2693 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002694{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002695 ATRACE_CALL();
2696
Mathias Agopian74c40c02010-09-29 13:02:36 -07002697 status_t result = PERMISSION_DENIED;
2698
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002699 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002700 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002701 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002702
2703 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002704 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002705 const uint32_t hw_w = hw->getWidth();
2706 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002707
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002708 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002709 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002710 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002711 return PERMISSION_DENIED;
2712 }
2713
2714 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002715 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002716 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002717 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002718
2719 sw = (!sw) ? hw_w : sw;
2720 sh = (!sh) ? hw_h : sh;
2721 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002722 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002723
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002724// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2725// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002726
Mathias Agopian74c40c02010-09-29 13:02:36 -07002727 // make sure to clear all GL error flags
2728 while ( glGetError() != GL_NO_ERROR ) ;
2729
2730 // create a FBO
2731 GLuint name, tname;
2732 glGenRenderbuffersOES(1, &tname);
2733 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2734 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002735
Mathias Agopian74c40c02010-09-29 13:02:36 -07002736 glGenFramebuffersOES(1, &name);
2737 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2738 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2739 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2740
2741 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002742
Mathias Agopian74c40c02010-09-29 13:02:36 -07002743 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2744
2745 // invert everything, b/c glReadPixel() below will invert the FB
Mathias Agopian135e5892012-09-30 16:43:20 -07002746 GLint viewport[4];
2747 glGetIntegerv(GL_VIEWPORT, viewport);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002748 glViewport(0, 0, sw, sh);
2749 glMatrixMode(GL_PROJECTION);
2750 glPushMatrix();
2751 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002752 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002753 glMatrixMode(GL_MODELVIEW);
2754
2755 // redraw the screen entirely...
2756 glClearColor(0,0,0,1);
2757 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002758
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002759 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002760 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002761 for (size_t i=0 ; i<count ; ++i) {
2762 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002763 const uint32_t z = layer->drawingState().z;
2764 if (z >= minLayerZ && z <= maxLayerZ) {
2765 if (filtering) layer->setFiltering(true);
2766 layer->draw(hw);
2767 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002768 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002769 }
2770
Mathias Agopian74c40c02010-09-29 13:02:36 -07002771 // check for errors and return screen capture
2772 if (glGetError() != GL_NO_ERROR) {
2773 // error while rendering
2774 result = INVALID_OPERATION;
2775 } else {
2776 // allocate shared memory large enough to hold the
2777 // screen capture
2778 sp<MemoryHeapBase> base(
2779 new MemoryHeapBase(size, 0, "screen-capture") );
2780 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002781 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002782 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002783 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002784 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2785 if (glGetError() == GL_NO_ERROR) {
2786 *heap = base;
2787 *w = sw;
2788 *h = sh;
2789 *f = PIXEL_FORMAT_RGBA_8888;
2790 result = NO_ERROR;
2791 }
2792 } else {
2793 result = NO_MEMORY;
2794 }
2795 }
Mathias Agopian135e5892012-09-30 16:43:20 -07002796 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002797 glMatrixMode(GL_PROJECTION);
2798 glPopMatrix();
2799 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002800 } else {
2801 result = BAD_VALUE;
2802 }
2803
2804 // release FBO resources
2805 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2806 glDeleteRenderbuffersOES(1, &tname);
2807 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002808
Mathias Agopian42977342012-08-05 00:40:46 -07002809 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002810
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002811// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002812
Mathias Agopian74c40c02010-09-29 13:02:36 -07002813 return result;
2814}
2815
2816
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002817status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002818 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002819 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002820 uint32_t sw, uint32_t sh,
2821 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002822{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002823 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002824 return BAD_VALUE;
2825
2826 if (!GLExtensions::getInstance().haveFramebufferObject())
2827 return INVALID_OPERATION;
2828
2829 class MessageCaptureScreen : public MessageBase {
2830 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002831 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002832 sp<IMemoryHeap>* heap;
2833 uint32_t* w;
2834 uint32_t* h;
2835 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002836 uint32_t sw;
2837 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002838 uint32_t minLayerZ;
2839 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002840 status_t result;
2841 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002842 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002843 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002844 uint32_t sw, uint32_t sh,
2845 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002846 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002847 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2848 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2849 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002850 {
2851 }
2852 status_t getResult() const {
2853 return result;
2854 }
2855 virtual bool handler() {
2856 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002857 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002858 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002859 return true;
2860 }
2861 };
2862
2863 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002864 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002865 status_t res = postMessageSync(msg);
2866 if (res == NO_ERROR) {
2867 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2868 }
2869 return res;
2870}
2871
2872// ---------------------------------------------------------------------------
2873
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002874SurfaceFlinger::LayerVector::LayerVector() {
2875}
2876
2877SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2878 : SortedVector<sp<LayerBase> >(rhs) {
2879}
2880
2881int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2882 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002883{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002884 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002885 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2886 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002887
2888 uint32_t ls = l->currentState().layerStack;
2889 uint32_t rs = r->currentState().layerStack;
2890 if (ls != rs)
2891 return ls - rs;
2892
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002893 uint32_t lz = l->currentState().z;
2894 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002895 if (lz != rz)
2896 return lz - rz;
2897
2898 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002899}
2900
2901// ---------------------------------------------------------------------------
2902
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002903SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2904 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002905}
2906
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002907SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2908 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002909 viewport.makeInvalid();
2910 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002911}
2912
2913// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002914
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002915}; // namespace android