blob: 9afa4c12c25188c249a6b9c802228456d3a7e06b [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>
43
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
56#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080057#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070059#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080060#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070064#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopianf33e4b62012-09-20 16:54:14 -070068#include "DisplayHardware/GraphicBufferAlloc.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),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070097 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070098 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070099 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700100 mDebugInSwapBuffers(0),
101 mLastSwapBufferTime(0),
102 mDebugInTransaction(0),
103 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700104 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114 property_get("debug.sf.ddms", value, "0");
115 mDebugDDMS = atoi(value);
116 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700117 if (!startDdmConnection()) {
118 // start failed, and DDMS debugging not enabled
119 mDebugDDMS = 0;
120 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700122 ALOGI_IF(mDebugRegion, "showupdates enabled");
123 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126void SurfaceFlinger::onFirstRef()
127{
128 mEventQueue.init(this);
129
130 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
131
132 // Wait for the main thread to be done with its initialization
133 mReadyToRunBarrier.wait();
134}
135
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137SurfaceFlinger::~SurfaceFlinger()
138{
Mathias Agopiana4912602012-07-12 14:25:33 -0700139 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
140 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
141 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800144void SurfaceFlinger::binderDied(const wp<IBinder>& who)
145{
146 // the window manager died on us. prepare its eulogy.
147
Andy McFadden13a082e2012-08-24 10:16:42 -0700148 // restore initial conditions (default device unblank, etc)
149 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150
151 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700152 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800153}
154
Mathias Agopian7e27f052010-05-28 14:22:23 -0700155sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Mathias Agopian96f08192010-06-02 23:28:45 -0700157 sp<ISurfaceComposerClient> bclient;
158 sp<Client> client(new Client(this));
159 status_t err = client->initCheck();
160 if (err == NO_ERROR) {
161 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 return bclient;
164}
165
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700166sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
167 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700168{
169 class DisplayToken : public BBinder {
170 sp<SurfaceFlinger> flinger;
171 virtual ~DisplayToken() {
172 // no more references, this display must be terminated
173 Mutex::Autolock _l(flinger->mStateLock);
174 flinger->mCurrentState.displays.removeItem(this);
175 flinger->setTransactionFlags(eDisplayTransactionNeeded);
176 }
177 public:
178 DisplayToken(const sp<SurfaceFlinger>& flinger)
179 : flinger(flinger) {
180 }
181 };
182
183 sp<BBinder> token = new DisplayToken(this);
184
185 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700186 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700187 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700188 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700189 mCurrentState.displays.add(token, info);
190
191 return token;
192}
193
Jesse Hall6edebdf2012-11-08 15:41:56 -0800194void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
195 ALOGW_IF(mBuiltinDisplays[type],
196 "Overwriting display token for display type %d", type);
197 mBuiltinDisplays[type] = new BBinder();
198 DisplayDeviceState info(type);
199 // All non-virtual displays are currently considered secure.
200 info.isSecure = true;
201 mCurrentState.displays.add(mBuiltinDisplays[type], info);
202}
203
Mathias Agopiane57f2922012-08-09 16:29:12 -0700204sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700205 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700206 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
207 return NULL;
208 }
Jesse Hall6edebdf2012-11-08 15:41:56 -0800209 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700210}
211
Jamie Gennis9a78c902011-01-12 18:30:40 -0800212sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
213{
214 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
215 return gba;
216}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218void SurfaceFlinger::bootFinished()
219{
220 const nsecs_t now = systemTime();
221 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000222 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700223 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700224
225 // wait patiently for the window manager death
226 const String16 name("window");
227 sp<IBinder> window(defaultServiceManager()->getService(name));
228 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700229 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700230 }
231
232 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700233 // formerly we would just kill the process, but we now ask it to exit so it
234 // can choose where to stop the animation.
235 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236}
237
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700238void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
239 class MessageDestroyGLTexture : public MessageBase {
240 GLuint texture;
241 public:
242 MessageDestroyGLTexture(GLuint texture)
243 : texture(texture) {
244 }
245 virtual bool handler() {
246 glDeleteTextures(1, &texture);
247 return true;
248 }
249 };
250 postMessageAsync(new MessageDestroyGLTexture(texture));
251}
252
Mathias Agopian722b98f2012-09-25 18:24:31 -0700253status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700254 EGLDisplay dpy,
255 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700256 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700257 EGLConfig* outConfig)
258{
259 EGLConfig config = NULL;
260 EGLint numConfigs = -1, n=0;
261 eglGetConfigs(dpy, NULL, 0, &numConfigs);
262 EGLConfig* const configs = new EGLConfig[numConfigs];
263 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700264
Mathias Agopian722b98f2012-09-25 18:24:31 -0700265 if (n) {
266 if (attribute != EGL_NONE) {
267 for (int i=0 ; i<n ; i++) {
268 EGLint value = 0;
269 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
270 if (wanted == value) {
271 *outConfig = configs[i];
272 delete [] configs;
273 return NO_ERROR;
274 }
275 }
276 } else {
277 // just pick the first one
278 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700279 delete [] configs;
280 return NO_ERROR;
281 }
282 }
283 delete [] configs;
284 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285}
286
Mathias Agopian722b98f2012-09-25 18:24:31 -0700287class EGLAttributeVector {
288 struct Attribute;
289 class Adder;
290 friend class Adder;
291 KeyedVector<Attribute, EGLint> mList;
292 struct Attribute {
293 Attribute() {};
294 Attribute(EGLint v) : v(v) { }
295 EGLint v;
296 bool operator < (const Attribute& other) const {
297 // this places EGL_NONE at the end
298 EGLint lhs(v);
299 EGLint rhs(other.v);
300 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
301 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
302 return lhs < rhs;
303 }
304 };
305 class Adder {
306 friend class EGLAttributeVector;
307 EGLAttributeVector& v;
308 EGLint attribute;
309 Adder(EGLAttributeVector& v, EGLint attribute)
310 : v(v), attribute(attribute) {
311 }
312 public:
313 void operator = (EGLint value) {
314 if (attribute != EGL_NONE) {
315 v.mList.add(attribute, value);
316 }
317 }
318 operator EGLint () const { return v.mList[attribute]; }
319 };
320public:
321 EGLAttributeVector() {
322 mList.add(EGL_NONE, EGL_NONE);
323 }
324 void remove(EGLint attribute) {
325 if (attribute != EGL_NONE) {
326 mList.removeItem(attribute);
327 }
328 }
329 Adder operator [] (EGLint attribute) {
330 return Adder(*this, attribute);
331 }
332 EGLint operator [] (EGLint attribute) const {
333 return mList[attribute];
334 }
335 // cast-operator to (EGLint const*)
336 operator EGLint const* () const { return &mList.keyAt(0).v; }
337};
338
Mathias Agopiana4912602012-07-12 14:25:33 -0700339EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
340 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
341 // it is to be used with WIFI displays
342 EGLConfig config;
343 EGLint dummy;
344 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700345
Mathias Agopian722b98f2012-09-25 18:24:31 -0700346 EGLAttributeVector attribs;
347 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
348 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
349 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
350 attribs[EGL_RED_SIZE] = 8;
351 attribs[EGL_GREEN_SIZE] = 8;
352 attribs[EGL_BLUE_SIZE] = 8;
353
354 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
355 if (!err)
356 goto success;
357
358 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
359 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
360 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
361 err = selectConfigForAttribute(display, attribs,
362 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700363 if (!err)
364 goto success;
365
366 // maybe we failed because of EGL_RECORDABLE_ANDROID
367 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700368 attribs.remove(EGL_RECORDABLE_ANDROID);
369 err = selectConfigForAttribute(display, attribs,
370 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700371 if (!err)
372 goto success;
373
374 // allow less than 24-bit color; the non-gpu-accelerated emulator only
375 // supports 16-bit color
376 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700377 attribs.remove(EGL_RED_SIZE);
378 attribs.remove(EGL_GREEN_SIZE);
379 attribs.remove(EGL_BLUE_SIZE);
380 err = selectConfigForAttribute(display, attribs,
381 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700382 if (!err)
383 goto success;
384
385 // this EGL is too lame for Android
386 ALOGE("no suitable EGLConfig found, giving up");
387
388 return 0;
389
390success:
391 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700392 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700393 return config;
394}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395
Mathias Agopiana4912602012-07-12 14:25:33 -0700396EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
397 // Also create our EGLContext
398 EGLint contextAttributes[] = {
399#ifdef EGL_IMG_context_priority
400#ifdef HAS_CONTEXT_PRIORITY
401#warning "using EGL_IMG_context_priority"
402 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
403#endif
404#endif
405 EGL_NONE, EGL_NONE
406 };
407 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
408 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
409 return ctxt;
410}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411
Mathias Agopiancde87a32012-09-13 14:09:01 -0700412void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 GLExtensions& extensions(GLExtensions::getInstance());
414 extensions.initWithGLStrings(
415 glGetString(GL_VENDOR),
416 glGetString(GL_RENDERER),
417 glGetString(GL_VERSION),
418 glGetString(GL_EXTENSIONS),
419 eglQueryString(display, EGL_VENDOR),
420 eglQueryString(display, EGL_VERSION),
421 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700422
Mathias Agopiana4912602012-07-12 14:25:33 -0700423 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
424 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700427 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 glShadeModel(GL_FLAT);
430 glDisable(GL_DITHER);
431 glDisable(GL_CULL_FACE);
432
Mathias Agopiana4912602012-07-12 14:25:33 -0700433 struct pack565 {
434 inline uint16_t operator() (int r, int g, int b) const {
435 return (r<<11)|(g<<5)|b;
436 }
437 } pack565;
438
Jamie Gennis9575f602011-10-07 14:51:16 -0700439 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
440 glGenTextures(1, &mProtectedTexName);
441 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
442 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
443 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
444 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
445 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
446 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
447 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448
Mathias Agopiana4912602012-07-12 14:25:33 -0700449 // print some debugging info
450 EGLint r,g,b,a;
451 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
452 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
453 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
454 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
455 ALOGI("EGL informations:");
456 ALOGI("vendor : %s", extensions.getEglVendor());
457 ALOGI("version : %s", extensions.getEglVersion());
458 ALOGI("extensions: %s", extensions.getEglExtension());
459 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
460 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
461 ALOGI("OpenGL ES informations:");
462 ALOGI("vendor : %s", extensions.getVendor());
463 ALOGI("renderer : %s", extensions.getRenderer());
464 ALOGI("version : %s", extensions.getVersion());
465 ALOGI("extensions: %s", extensions.getExtension());
466 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
467 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
468}
469
Mathias Agopiana4912602012-07-12 14:25:33 -0700470status_t SurfaceFlinger::readyToRun()
471{
472 ALOGI( "SurfaceFlinger's main thread ready to run. "
473 "Initializing graphics H/W...");
474
Jesse Hall6edebdf2012-11-08 15:41:56 -0800475 Mutex::Autolock _l(mStateLock);
476
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700477 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700478 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
479 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700480
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700481 // Initialize the H/W composer object. There may or may not be an
482 // actual hardware composer underneath.
483 mHwc = new HWComposer(this,
484 *static_cast<HWComposer::EventHandler *>(this));
485
Mathias Agopiana4912602012-07-12 14:25:33 -0700486 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700487 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700488 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
489 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700490
Mathias Agopianda27af92012-09-13 18:17:13 -0700491 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
492 "couldn't create EGLContext");
493
Mathias Agopiancde87a32012-09-13 14:09:01 -0700494 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700495 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700496 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700497 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700498 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700499 // All non-virtual displays are currently considered secure.
500 bool isSecure = true;
Jesse Hall6edebdf2012-11-08 15:41:56 -0800501 createBuiltinDisplayLocked(type);
502 wp<IBinder> token = mBuiltinDisplays[i];
503
Mathias Agopianf5a33922012-09-19 18:16:22 -0700504 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
505 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
506 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
507 sp<DisplayDevice> hw = new DisplayDevice(this,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700508 type, isSecure, token, stc, fbs, mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700509 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700510 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700511 // for displays other than the main display, so we always
512 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700513 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700514 hw->acquireScreen();
515 }
516 mDisplays.add(token, hw);
517 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700518 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700519
Mathias Agopianf5a33922012-09-19 18:16:22 -0700520 // we need a GL context current in a few places, when initializing
521 // OpenGL ES (see below), or creating a layer,
522 // or when a texture is (asynchronously) destroyed, and for that
523 // we need a valid surface, so it's convenient to use the main display
524 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700525 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700526
Mathias Agopiana4912602012-07-12 14:25:33 -0700527 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700528 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
529 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800530
Mathias Agopian028508c2012-07-25 21:12:12 -0700531 // start the EventThread
532 mEventThread = new EventThread(this);
533 mEventQueue.setEventThread(mEventThread);
534
Mathias Agopian92a979a2012-08-02 18:32:23 -0700535 // initialize our drawing state
536 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700537
Mathias Agopiancde87a32012-09-13 14:09:01 -0700538
Mathias Agopiana4912602012-07-12 14:25:33 -0700539 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800540 mReadyToRunBarrier.open();
541
Andy McFadden13a082e2012-08-24 10:16:42 -0700542 // set initial conditions (e.g. unblank default device)
543 initializeDisplays();
544
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700545 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700546 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700547
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 return NO_ERROR;
549}
550
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700551int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
552 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
553 type : mHwc->allocateDisplayId();
554}
555
Mathias Agopiana67e4182012-06-19 17:26:12 -0700556void SurfaceFlinger::startBootAnim() {
557 // start boot animation
558 property_set("service.bootanim.exit", "0");
559 property_set("ctl.start", "bootanim");
560}
561
Mathias Agopiana4912602012-07-12 14:25:33 -0700562uint32_t SurfaceFlinger::getMaxTextureSize() const {
563 return mMaxTextureSize;
564}
565
566uint32_t SurfaceFlinger::getMaxViewportDims() const {
567 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
568 mMaxViewportDims[0] : mMaxViewportDims[1];
569}
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800572
Jamie Gennis582270d2011-08-17 18:19:00 -0700573bool SurfaceFlinger::authenticateSurfaceTexture(
574 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800575 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700576 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800577
578 // Check the visible layer list for the ISurface
579 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
580 size_t count = currentLayers.size();
581 for (size_t i=0 ; i<count ; i++) {
582 const sp<LayerBase>& layer(currentLayers[i]);
583 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700584 if (lbc != NULL) {
585 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
586 if (lbcBinder == surfaceTextureBinder) {
587 return true;
588 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800589 }
590 }
591
592 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700593 // SurfaceTexture gets destroyed before all the clients are done using it,
594 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800595 // will instead fail later on when the client tries to use the surface,
596 // which should be reported as "surface XYZ returned an -ENODEV". The
597 // purgatorized layers are no less authentic than the visible ones, so this
598 // should not cause any harm.
599 size_t purgatorySize = mLayerPurgatory.size();
600 for (size_t i=0 ; i<purgatorySize ; i++) {
601 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
602 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700603 if (lbc != NULL) {
604 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
605 if (lbcBinder == surfaceTextureBinder) {
606 return true;
607 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800608 }
609 }
610
611 return false;
612}
613
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700614status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall6edebdf2012-11-08 15:41:56 -0800615 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700616 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall6edebdf2012-11-08 15:41:56 -0800617 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700618 type = i;
619 break;
620 }
621 }
622
623 if (type < 0) {
624 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700625 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700626
627 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700628 float xdpi = hwc.getDpiX(type);
629 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700630
631 // TODO: Not sure if display density should handled by SF any longer
632 class Density {
633 static int getDensityFromProperty(char const* propName) {
634 char property[PROPERTY_VALUE_MAX];
635 int density = 0;
636 if (property_get(propName, property, NULL) > 0) {
637 density = atoi(property);
638 }
639 return density;
640 }
641 public:
642 static int getEmuDensity() {
643 return getDensityFromProperty("qemu.sf.lcd_density"); }
644 static int getBuildDensity() {
645 return getDensityFromProperty("ro.sf.lcd_density"); }
646 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700647
648 if (type == DisplayDevice::DISPLAY_PRIMARY) {
649 // The density of the device is provided by a build property
650 float density = Density::getBuildDensity() / 160.0f;
651 if (density == 0) {
652 // the build doesn't provide a density -- this is wrong!
653 // use xdpi instead
654 ALOGE("ro.sf.lcd_density must be defined as a build property");
655 density = xdpi / 160.0f;
656 }
657 if (Density::getEmuDensity()) {
658 // if "qemu.sf.lcd_density" is specified, it overrides everything
659 xdpi = ydpi = density = Density::getEmuDensity();
660 density /= 160.0f;
661 }
662 info->density = density;
663
664 // TODO: this needs to go away (currently needed only by webkit)
665 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
666 info->orientation = hw->getOrientation();
667 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
668 } else {
669 // TODO: where should this value come from?
670 static const int TV_DENSITY = 213;
671 info->density = TV_DENSITY / 160.0f;
672 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700673 }
674
Mathias Agopian1604f772012-09-18 21:54:42 -0700675 info->w = hwc.getWidth(type);
676 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700677 info->xdpi = xdpi;
678 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700679 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700680
681 // All non-virtual displays are currently considered secure.
682 info->secure = true;
683
Mathias Agopian888c8222012-08-04 21:10:38 -0700684 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700685}
686
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800687// ----------------------------------------------------------------------------
688
689sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800690 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700691}
692
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800694
695void SurfaceFlinger::waitForEvent() {
696 mEventQueue.waitMessage();
697}
698
699void SurfaceFlinger::signalTransaction() {
700 mEventQueue.invalidate();
701}
702
703void SurfaceFlinger::signalLayerUpdate() {
704 mEventQueue.invalidate();
705}
706
707void SurfaceFlinger::signalRefresh() {
708 mEventQueue.refresh();
709}
710
711status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
712 nsecs_t reltime, uint32_t flags) {
713 return mEventQueue.postMessage(msg, reltime);
714}
715
716status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
717 nsecs_t reltime, uint32_t flags) {
718 status_t res = mEventQueue.postMessage(msg, reltime);
719 if (res == NO_ERROR) {
720 msg->wait();
721 }
722 return res;
723}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724
Mathias Agopian4fec8732012-06-29 14:12:52 -0700725bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 return true;
728}
729
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700730void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700731 if (mEventThread == NULL) {
732 // This is a temporary workaround for b/7145521. A non-null pointer
733 // does not mean EventThread has finished initializing, so this
734 // is not a correct fix.
735 ALOGW("WARNING: EventThread not started, ignoring vsync");
736 return;
737 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700738 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
739 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700740 mEventThread->onVSyncReceived(type, timestamp);
741 }
742}
743
744void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
745 if (mEventThread == NULL) {
746 // This is a temporary workaround for b/7145521. A non-null pointer
747 // does not mean EventThread has finished initializing, so this
748 // is not a correct fix.
749 ALOGW("WARNING: EventThread not started, ignoring hotplug");
750 return;
751 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700752
Mathias Agopian148994e2012-09-19 17:31:36 -0700753 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700754 Mutex::Autolock _l(mStateLock);
Jesse Hall6edebdf2012-11-08 15:41:56 -0800755 if (connected) {
756 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700757 } else {
Jesse Hall6edebdf2012-11-08 15:41:56 -0800758 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
759 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700760 }
761 setTransactionFlags(eDisplayTransactionNeeded);
762
Andy McFadden9e9689c2012-10-10 18:17:51 -0700763 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700764 }
Mathias Agopian86303202012-07-24 22:46:10 -0700765}
766
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700767void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
768 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700769}
770
Mathias Agopian4fec8732012-06-29 14:12:52 -0700771void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800772 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800773 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700774 case MessageQueue::INVALIDATE:
775 handleMessageTransaction();
776 handleMessageInvalidate();
777 signalRefresh();
778 break;
779 case MessageQueue::REFRESH:
780 handleMessageRefresh();
781 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800782 }
783}
784
Mathias Agopian4fec8732012-06-29 14:12:52 -0700785void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700786 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700787 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700788 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700789 }
790}
791
792void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700793 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700794 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700795}
796
797void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700798 ATRACE_CALL();
799 preComposition();
800 rebuildLayerStacks();
801 setUpHWComposer();
802 doDebugFlashRegions();
803 doComposition();
804 postComposition();
805}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700806
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807void SurfaceFlinger::doDebugFlashRegions()
808{
809 // is debugging enabled
810 if (CC_LIKELY(!mDebugRegion))
811 return;
812
813 const bool repaintEverything = mRepaintEverything;
814 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
815 const sp<DisplayDevice>& hw(mDisplays[dpy]);
816 if (hw->canDraw()) {
817 // transform the dirty region into this screen's coordinate space
818 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
819 if (!dirtyRegion.isEmpty()) {
820 // redraw the whole screen
821 doComposeSurfaces(hw, Region(hw->bounds()));
822
823 // and draw the dirty region
824 glDisable(GL_TEXTURE_EXTERNAL_OES);
825 glDisable(GL_TEXTURE_2D);
826 glDisable(GL_BLEND);
827 glColor4f(1, 0, 1, 1);
828 const int32_t height = hw->getHeight();
829 Region::const_iterator it = dirtyRegion.begin();
830 Region::const_iterator const end = dirtyRegion.end();
831 while (it != end) {
832 const Rect& r = *it++;
833 GLfloat vertices[][2] = {
834 { r.left, height - r.top },
835 { r.left, height - r.bottom },
836 { r.right, height - r.bottom },
837 { r.right, height - r.top }
838 };
839 glVertexPointer(2, GL_FLOAT, 0, vertices);
840 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
841 }
842 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700843 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 }
845 }
846 }
847
848 postFramebuffer();
849
850 if (mDebugRegion > 1) {
851 usleep(mDebugRegion * 1000);
852 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700853
854 HWComposer& hwc(getHwComposer());
855 if (hwc.initCheck() == NO_ERROR) {
856 status_t err = hwc.prepare();
857 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
858 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859}
860
861void SurfaceFlinger::preComposition()
862{
863 bool needExtraInvalidate = false;
864 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
865 const size_t count = currentLayers.size();
866 for (size_t i=0 ; i<count ; i++) {
867 if (currentLayers[i]->onPreComposition()) {
868 needExtraInvalidate = true;
869 }
870 }
871 if (needExtraInvalidate) {
872 signalLayerUpdate();
873 }
874}
875
876void SurfaceFlinger::postComposition()
877{
878 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
879 const size_t count = currentLayers.size();
880 for (size_t i=0 ; i<count ; i++) {
881 currentLayers[i]->onPostComposition();
882 }
883}
884
885void SurfaceFlinger::rebuildLayerStacks() {
886 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700887 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700888 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700889 mVisibleRegionsDirty = false;
890 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700891
Mathias Agopian87baae12012-07-31 12:38:26 -0700892 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700893 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700894 Region opaqueRegion;
895 Region dirtyRegion;
896 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700897 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700898 const Transform& tr(hw->getTransform());
899 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700900 if (hw->canDraw()) {
901 SurfaceFlinger::computeVisibleRegions(currentLayers,
902 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700903
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700904 const size_t count = currentLayers.size();
905 for (size_t i=0 ; i<count ; i++) {
906 const sp<LayerBase>& layer(currentLayers[i]);
907 const Layer::State& s(layer->drawingState());
908 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700909 Region drawRegion(tr.transform(
910 layer->visibleNonTransparentRegion));
911 drawRegion.andSelf(bounds);
912 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700913 layersSortedByZ.add(layer);
914 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700915 }
916 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700917 }
Mathias Agopian42977342012-08-05 00:40:46 -0700918 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700919 hw->undefinedRegion.set(bounds);
920 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
921 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700922 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700923 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700925
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700927 HWComposer& hwc(getHwComposer());
928 if (hwc.initCheck() == NO_ERROR) {
929 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700930 if (CC_UNLIKELY(mHwWorkListDirty)) {
931 mHwWorkListDirty = false;
932 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
933 sp<const DisplayDevice> hw(mDisplays[dpy]);
934 const int32_t id = hw->getHwcDisplayId();
935 if (id >= 0) {
936 const Vector< sp<LayerBase> >& currentLayers(
937 hw->getVisibleLayersSortedByZ());
938 const size_t count = currentLayers.size();
939 if (hwc.createWorkList(id, count) == NO_ERROR) {
940 HWComposer::LayerListIterator cur = hwc.begin(id);
941 const HWComposer::LayerListIterator end = hwc.end(id);
942 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
943 const sp<LayerBase>& layer(currentLayers[i]);
944 layer->setGeometry(hw, *cur);
945 if (mDebugDisableHWC || mDebugRegion) {
946 cur->setSkip(true);
947 }
948 }
949 }
950 }
951 }
952 }
953
954 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700955 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700956 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700957 const int32_t id = hw->getHwcDisplayId();
958 if (id >= 0) {
959 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700961 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700962 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 /*
966 * update the per-frame h/w composer data for each layer
967 * and build the transparent region of the FB
968 */
969 const sp<LayerBase>& layer(currentLayers[i]);
970 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700971 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700972 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700973 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700974
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700975 status_t err = hwc.prepare();
976 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
977 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700978}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700979
Mathias Agopiancd60f992012-08-16 16:28:27 -0700980void SurfaceFlinger::doComposition() {
981 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700982 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700983 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700984 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700985 if (hw->canDraw()) {
986 // transform the dirty region into this screen's coordinate space
987 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800988
989 // repaint the framebuffer (if needed)
990 doDisplayComposition(hw, dirtyRegion);
991
Mathias Agopiancd60f992012-08-16 16:28:27 -0700992 hw->dirtyRegion.clear();
993 hw->flip(hw->swapRegion);
994 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700995 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700996 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700997 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700998 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700999 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001000}
1001
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002void SurfaceFlinger::postFramebuffer()
1003{
Mathias Agopian841cde52012-03-01 15:44:37 -08001004 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001005
Mathias Agopiana44b0412011-10-16 18:46:35 -07001006 const nsecs_t now = systemTime();
1007 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001008
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001009 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001010 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001011 if (!hwc.supportsFramebufferTarget()) {
1012 // EGL spec says:
1013 // "surface must be bound to the calling thread's current context,
1014 // for the current rendering API."
1015 DisplayDevice::makeCurrent(mEGLDisplay,
1016 getDefaultDisplayDevice(), mEGLContext);
1017 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001018 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001019 }
1020
Mathias Agopian92a979a2012-08-02 18:32:23 -07001021 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001022 sp<const DisplayDevice> hw(mDisplays[dpy]);
1023 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001024 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001025 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001026 int32_t id = hw->getHwcDisplayId();
1027 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001028 HWComposer::LayerListIterator cur = hwc.begin(id);
1029 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001030 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001031 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001032 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001033 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001034 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001035 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001036 }
Jesse Hallef194142012-06-14 14:45:17 -07001037 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001038 }
1039
Mathias Agopiana44b0412011-10-16 18:46:35 -07001040 mLastSwapBufferTime = systemTime() - now;
1041 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042}
1043
Mathias Agopian87baae12012-07-31 12:38:26 -07001044void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045{
Mathias Agopian841cde52012-03-01 15:44:37 -08001046 ATRACE_CALL();
1047
Mathias Agopianca4d3602011-05-19 15:38:14 -07001048 Mutex::Autolock _l(mStateLock);
1049 const nsecs_t now = systemTime();
1050 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051
Mathias Agopianca4d3602011-05-19 15:38:14 -07001052 // Here we're guaranteed that some transaction flags are set
1053 // so we can call handleTransactionLocked() unconditionally.
1054 // We call getTransactionFlags(), which will also clear the flags,
1055 // with mStateLock held to guarantee that mCurrentState won't change
1056 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001057
Mathias Agopiane57f2922012-08-09 16:29:12 -07001058 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001059 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001060
Mathias Agopianca4d3602011-05-19 15:38:14 -07001061 mLastTransactionTime = systemTime() - now;
1062 mDebugInTransaction = 0;
1063 invalidateHwcGeometry();
1064 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001065}
1066
Mathias Agopian87baae12012-07-31 12:38:26 -07001067void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001068{
1069 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 const size_t count = currentLayers.size();
1071
1072 /*
1073 * Traversal of the children
1074 * (perform the transaction for each of them if needed)
1075 */
1076
Mathias Agopian3559b072012-08-15 13:46:03 -07001077 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian84300952012-11-21 16:02:13 -08001079 const sp<LayerBase>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1081 if (!trFlags) continue;
1082
1083 const uint32_t flags = layer->doTransaction(0);
1084 if (flags & Layer::eVisibleRegion)
1085 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 }
1087 }
1088
1089 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001090 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 */
1092
Mathias Agopiane57f2922012-08-09 16:29:12 -07001093 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001094 // here we take advantage of Vector's copy-on-write semantics to
1095 // improve performance by skipping the transaction entirely when
1096 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001097 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1098 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001099 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001101 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001102 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001103
1104 // find the displays that were removed
1105 // (ie: in drawing state but not in current state)
1106 // also handle displays that changed
1107 // (ie: displays that are in both lists)
1108 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001109 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1110 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001111 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001112 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001113 // Call makeCurrent() on the primary display so we can
1114 // be sure that nothing associated with this display
1115 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001116 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001117 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001118 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001119 getHwComposer().disconnectDisplay(draw[i].type);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001120 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001121 } else {
1122 ALOGW("trying to remove the main display");
1123 }
1124 } else {
1125 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001126 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001127 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001128 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001129 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001130 // recreating the DisplayDevice, so we just remove it
1131 // from the drawing state, so that it get re-added
1132 // below.
1133 mDisplays.removeItem(display);
1134 mDrawingState.displays.removeItemsAt(i);
1135 dc--; i--;
1136 // at this point we must loop to the next item
1137 continue;
1138 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001139
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001140 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001141 if (disp != NULL) {
1142 if (state.layerStack != draw[i].layerStack) {
1143 disp->setLayerStack(state.layerStack);
1144 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001145 if ((state.orientation != draw[i].orientation)
1146 || (state.viewport != draw[i].viewport)
1147 || (state.frame != draw[i].frame))
1148 {
1149 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001150 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001151 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001152 }
1153 }
1154 }
1155
1156 // find displays that were added
1157 // (ie: in current state but not in drawing state)
1158 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001159 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001160 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001161
1162 sp<FramebufferSurface> fbs;
1163 sp<SurfaceTextureClient> stc;
1164 if (!state.isVirtualDisplay()) {
1165
1166 ALOGE_IF(state.surface!=NULL,
1167 "adding a supported display, but rendering "
1168 "surface is provided (%p), ignoring it",
1169 state.surface.get());
1170
1171 // for supported (by hwc) displays we provide our
1172 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001173 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 stc = new SurfaceTextureClient(
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001175 static_cast< sp<ISurfaceTexture> >(
1176 fbs->getBufferQueue()));
Mathias Agopiancde87a32012-09-13 14:09:01 -07001177 } else {
1178 if (state.surface != NULL) {
1179 stc = new SurfaceTextureClient(state.surface);
1180 }
1181 }
1182
1183 const wp<IBinder>& display(curr.keyAt(i));
1184 if (stc != NULL) {
1185 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall6edebdf2012-11-08 15:41:56 -08001186 state.type, state.isSecure, display, stc, fbs,
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001187 mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001188 hw->setLayerStack(state.layerStack);
1189 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001190 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001191 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001192 mDisplays.add(display, hw);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001193 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001194 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001195 }
1196 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001198 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199
Mathias Agopian84300952012-11-21 16:02:13 -08001200 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1201 // The transform hint might have changed for some layers
1202 // (either because a display has changed, or because a layer
1203 // as changed).
1204 //
1205 // Walk through all the layers in currentLayers,
1206 // and update their transform hint.
1207 //
1208 // If a layer is visible only on a single display, then that
1209 // display is used to calculate the hint, otherwise we use the
1210 // default display.
1211 //
1212 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1213 // the hint is set before we acquire a buffer from the surface texture.
1214 //
1215 // NOTE: layer transactions have taken place already, so we use their
1216 // drawing state. However, SurfaceFlinger's own transaction has not
1217 // happened yet, so we must use the current state layer list
1218 // (soon to become the drawing state list).
1219 //
1220 sp<const DisplayDevice> disp;
1221 uint32_t currentlayerStack = 0;
1222 for (size_t i=0; i<count; i++) {
1223 // NOTE: we rely on the fact that layers are sorted by
1224 // layerStack first (so we don't have to traverse the list
1225 // of displays for every layer).
1226 const sp<LayerBase>& layerBase(currentLayers[i]);
1227 uint32_t layerStack = layerBase->drawingState().layerStack;
1228 if (i==0 || currentlayerStack != layerStack) {
1229 currentlayerStack = layerStack;
1230 // figure out if this layerstack is mirrored
1231 // (more than one display) if so, pick the default display,
1232 // if not, pick the only display it's on.
1233 disp.clear();
1234 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1235 sp<const DisplayDevice> hw(mDisplays[dpy]);
1236 if (hw->getLayerStack() == currentlayerStack) {
1237 if (disp == NULL) {
1238 disp = hw;
1239 } else {
1240 disp = getDefaultDisplayDevice();
1241 break;
1242 }
1243 }
1244 }
1245 }
1246 if (disp != NULL) {
1247 // presumably this means this layer is using a layerStack
1248 // that is not visible on any display
1249 layerBase->updateTransformHint(disp);
1250 }
1251 }
1252 }
1253
1254
Mathias Agopian3559b072012-08-15 13:46:03 -07001255 /*
1256 * Perform our own transaction if needed
1257 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001258
Mathias Agopiancd60f992012-08-16 16:28:27 -07001259 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1260 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001261 // layers have been added
1262 mVisibleRegionsDirty = true;
1263 }
1264
1265 // some layers might have been removed, so
1266 // we need to update the regions they're exposing.
1267 if (mLayersRemoved) {
1268 mLayersRemoved = false;
1269 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001270 const size_t count = previousLayers.size();
1271 for (size_t i=0 ; i<count ; i++) {
1272 const sp<LayerBase>& layer(previousLayers[i]);
1273 if (currentLayers.indexOf(layer) < 0) {
1274 // this layer is not visible anymore
1275 // TODO: we could traverse the tree from front to back and
1276 // compute the actual visible region
1277 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001278 const Layer::State& s(layer->drawingState());
1279 Region visibleReg = s.transform.transform(
1280 Region(Rect(s.active.w, s.active.h)));
1281 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001282 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 }
1285
1286 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001287}
1288
1289void SurfaceFlinger::commitTransaction()
1290{
1291 if (!mLayersPendingRemoval.isEmpty()) {
1292 // Notify removed layers now that they can't be drawn from
1293 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1294 mLayersPendingRemoval[i]->onRemoved();
1295 }
1296 mLayersPendingRemoval.clear();
1297 }
1298
1299 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001300 mTransactionPending = false;
1301 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001302 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303}
1304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001306 const LayerVector& currentLayers, uint32_t layerStack,
1307 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308{
Mathias Agopian841cde52012-03-01 15:44:37 -08001309 ATRACE_CALL();
1310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 Region aboveOpaqueLayers;
1312 Region aboveCoveredLayers;
1313 Region dirty;
1314
Mathias Agopian87baae12012-07-31 12:38:26 -07001315 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316
1317 size_t i = currentLayers.size();
1318 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001319 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320
1321 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001322 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 // only consider the layers on the given later stack
1325 if (s.layerStack != layerStack)
1326 continue;
1327
Mathias Agopianab028732010-03-16 16:41:46 -07001328 /*
1329 * opaqueRegion: area of a surface that is fully opaque.
1330 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001332
1333 /*
1334 * visibleRegion: area of a surface that is visible on screen
1335 * and not fully transparent. This is essentially the layer's
1336 * footprint minus the opaque regions above it.
1337 * Areas covered by a translucent surface are considered visible.
1338 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001340
1341 /*
1342 * coveredRegion: area of a surface that is covered by all
1343 * visible regions above it (which includes the translucent areas).
1344 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001346
Jesse Halla8026d22012-09-25 13:25:04 -07001347 /*
1348 * transparentRegion: area of a surface that is hinted to be completely
1349 * transparent. This is only used to tell when the layer has no visible
1350 * non-transparent regions and can be removed from the layer list. It
1351 * does not affect the visibleRegion of this layer or any layers
1352 * beneath it. The hint may not be correct if apps don't respect the
1353 * SurfaceView restrictions (which, sadly, some don't).
1354 */
1355 Region transparentRegion;
1356
Mathias Agopianab028732010-03-16 16:41:46 -07001357
1358 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001359 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001360 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001361 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001363 if (!visibleRegion.isEmpty()) {
1364 // Remove the transparent area from the visible region
1365 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366 const Transform tr(s.transform);
1367 if (tr.transformed()) {
1368 if (tr.preserveRects()) {
1369 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001370 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001371 } else {
1372 // transformation too complex, can't do the
1373 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001374 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001375 }
1376 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001377 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001378 }
Mathias Agopianab028732010-03-16 16:41:46 -07001379 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380
Mathias Agopianab028732010-03-16 16:41:46 -07001381 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001383 if (s.alpha==255 && !translucent &&
1384 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1385 // the opaque region is the layer's footprint
1386 opaqueRegion = visibleRegion;
1387 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 }
1389 }
1390
Mathias Agopianab028732010-03-16 16:41:46 -07001391 // Clip the covered region to the visible region
1392 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1393
1394 // Update aboveCoveredLayers for next (lower) layer
1395 aboveCoveredLayers.orSelf(visibleRegion);
1396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 // subtract the opaque region covered by the layers above us
1398 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399
1400 // compute this layer's dirty region
1401 if (layer->contentDirty) {
1402 // we need to invalidate the whole region
1403 dirty = visibleRegion;
1404 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001405 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 layer->contentDirty = false;
1407 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001408 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001409 * the exposed region consists of two components:
1410 * 1) what's VISIBLE now and was COVERED before
1411 * 2) what's EXPOSED now less what was EXPOSED before
1412 *
1413 * note that (1) is conservative, we start with the whole
1414 * visible region but only keep what used to be covered by
1415 * something -- which mean it may have been exposed.
1416 *
1417 * (2) handles areas that were not covered by anything but got
1418 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001419 */
Mathias Agopianab028732010-03-16 16:41:46 -07001420 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001421 const Region oldVisibleRegion = layer->visibleRegion;
1422 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001423 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1424 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 }
1426 dirty.subtractSelf(aboveOpaqueLayers);
1427
1428 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001429 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430
Mathias Agopianab028732010-03-16 16:41:46 -07001431 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001433
Jesse Halla8026d22012-09-25 13:25:04 -07001434 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 layer->setVisibleRegion(visibleRegion);
1436 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001437 layer->setVisibleNonTransparentRegion(
1438 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439 }
1440
Mathias Agopian87baae12012-07-31 12:38:26 -07001441 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442}
1443
Mathias Agopian87baae12012-07-31 12:38:26 -07001444void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1445 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001446 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001447 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1448 if (hw->getLayerStack() == layerStack) {
1449 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001450 }
1451 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001452}
1453
1454void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001456 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457
Mathias Agopian4fec8732012-06-29 14:12:52 -07001458 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001459 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001460 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001461 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001462 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001463 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001464 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001465 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001466 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001467
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001468 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469}
1470
Mathias Agopianad456f92011-01-13 17:53:01 -08001471void SurfaceFlinger::invalidateHwcGeometry()
1472{
1473 mHwWorkListDirty = true;
1474}
1475
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001476
Mathias Agopiancd60f992012-08-16 16:28:27 -07001477void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001478 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479{
Mathias Agopian87baae12012-07-31 12:38:26 -07001480 Region dirtyRegion(inDirtyRegion);
1481
Mathias Agopianb8a55602009-06-26 19:06:36 -07001482 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001483 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484
Mathias Agopian42977342012-08-05 00:40:46 -07001485 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001486 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001487 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1488 // takes a rectangle, we must make sure to update that whole
1489 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001490 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001491 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001492 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001493 // We need to redraw the rectangle that will be updated
1494 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001495 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001496 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001497 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001498 } else {
1499 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001500 dirtyRegion.set(hw->bounds());
1501 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 }
1503 }
1504
Mathias Agopiancd60f992012-08-16 16:28:27 -07001505 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001507 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001508 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001509
1510 // swap buffers (presentation)
1511 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512}
1513
Mathias Agopiancd60f992012-08-16 16:28:27 -07001514void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001515{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001516 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001517 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001518 HWComposer::LayerListIterator cur = hwc.begin(id);
1519 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001520
Mathias Agopian85d751c2012-08-29 16:59:24 -07001521 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1522 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001523 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001524
1525 // set the frame buffer
1526 glMatrixMode(GL_MODELVIEW);
1527 glLoadIdentity();
1528
1529 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001530 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001531 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001532 // when using overlays, we assume a fully transparent framebuffer
1533 // NOTE: we could reduce how much we need to clear, for instance
1534 // remove where there are opaque FB layers. however, on some
1535 // GPUs doing a "clean slate" glClear might be more efficient.
1536 // We'll revisit later if needed.
1537 glClearColor(0, 0, 0, 0);
1538 glClear(GL_COLOR_BUFFER_BIT);
1539 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001540 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001541 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001542 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001543 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001544 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001545 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001546 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001547
1548 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_EXTERNAL) {
1549 // TODO: just to be on the safe side, we don't set the
1550 // scissor on the main display. It should never be needed
1551 // anyways (though in theory it could since the API allows it).
1552 const Rect& bounds(hw->getBounds());
1553 const Transform& tr(hw->getTransform());
1554 const Rect scissor(tr.transform(hw->getViewport()));
1555 if (scissor != bounds) {
1556 // scissor doesn't match the screen's dimensions, so we
1557 // need to clear everything outside of it and enable
1558 // the GL scissor so we don't draw anything where we shouldn't
1559 const GLint height = hw->getHeight();
1560 glScissor(scissor.left, height - scissor.bottom,
1561 scissor.getWidth(), scissor.getHeight());
1562 // clear everything unscissored
1563 glClearColor(0, 0, 0, 0);
1564 glClear(GL_COLOR_BUFFER_BIT);
1565 // enable scissor for this frame
1566 glEnable(GL_SCISSOR_TEST);
1567 }
1568 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001570
Mathias Agopian85d751c2012-08-29 16:59:24 -07001571 /*
1572 * and then, render the layers targeted at the framebuffer
1573 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001574
Mathias Agopian85d751c2012-08-29 16:59:24 -07001575 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1576 const size_t count = layers.size();
1577 const Transform& tr = hw->getTransform();
1578 if (cur != end) {
1579 // we're using h/w composer
1580 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001581 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001582 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001583 if (!clip.isEmpty()) {
1584 switch (cur->getCompositionType()) {
1585 case HWC_OVERLAY: {
1586 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1587 && i
1588 && layer->isOpaque()
1589 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001590 // never clear the very first layer since we're
1591 // guaranteed the FB is already cleared
1592 layer->clearWithOpenGL(hw, clip);
1593 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001594 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001595 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001596 case HWC_FRAMEBUFFER: {
1597 layer->draw(hw, clip);
1598 break;
1599 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001600 case HWC_FRAMEBUFFER_TARGET: {
1601 // this should not happen as the iterator shouldn't
1602 // let us get there.
1603 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1604 break;
1605 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001606 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001607 }
1608 layer->setAcquireFence(hw, *cur);
1609 }
1610 } else {
1611 // we're not using h/w composer
1612 for (size_t i=0 ; i<count ; ++i) {
1613 const sp<LayerBase>& layer(layers[i]);
1614 const Region clip(dirty.intersect(
1615 tr.transform(layer->visibleRegion)));
1616 if (!clip.isEmpty()) {
1617 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001618 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001619 }
1620 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001621
1622 // disable scissor at the end of the frame
1623 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624}
1625
Mathias Agopian55801e42012-08-27 18:54:24 -07001626void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1627 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001629 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001630 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001631 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001632 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001633
Mathias Agopian55801e42012-08-27 18:54:24 -07001634 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001635 Region::const_iterator it = region.begin();
1636 Region::const_iterator const end = region.end();
1637 while (it != end) {
1638 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001639 GLfloat vertices[][2] = {
1640 { r.left, height - r.top },
1641 { r.left, height - r.bottom },
1642 { r.right, height - r.bottom },
1643 { r.right, height - r.top }
1644 };
1645 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001646 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1647 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648}
1649
Mathias Agopian96f08192010-06-02 23:28:45 -07001650ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1651 const sp<LayerBaseClient>& lbc)
1652{
Mathias Agopian96f08192010-06-02 23:28:45 -07001653 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001654 size_t name = client->attachLayer(lbc);
1655
Mathias Agopian96f08192010-06-02 23:28:45 -07001656 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001657 Mutex::Autolock _l(mStateLock);
1658 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001659
Mathias Agopian4f113742011-05-03 16:21:41 -07001660 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001661}
1662
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664{
1665 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001666 status_t err = purgatorizeLayer_l(layer);
1667 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001668 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001669 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670}
1671
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001672status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673{
1674 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1675 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001676 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 return NO_ERROR;
1678 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001679 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680}
1681
Mathias Agopian9a112062009-04-17 19:36:26 -07001682status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1683{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001684 // First add the layer to the purgatory list, which makes sure it won't
1685 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001686 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001687 if (err >= 0) {
1688 mLayerPurgatory.add(layerBase);
1689 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001690
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001691 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001692
Mathias Agopian3d579642009-06-04 18:46:21 -07001693 // it's possible that we don't find a layer, because it might
1694 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001695 // from the user because there is a race between Client::destroySurface(),
1696 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001697 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1698}
1699
Mathias Agopiandea20b12011-05-03 17:04:02 -07001700uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1701{
1702 return android_atomic_release_load(&mTransactionFlags);
1703}
1704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1706{
1707 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1708}
1709
Mathias Agopianbb641242010-05-18 17:06:55 -07001710uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711{
1712 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1713 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001714 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 }
1716 return old;
1717}
1718
Mathias Agopian8b33f032012-07-24 20:43:54 -07001719void SurfaceFlinger::setTransactionState(
1720 const Vector<ComposerState>& state,
1721 const Vector<DisplayState>& displays,
1722 uint32_t flags)
1723{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001724 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001725 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001726 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001727
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001728 if (flags & eAnimation) {
1729 // For window updates that are part of an animation we must wait for
1730 // previous animation "frames" to be handled.
1731 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001732 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001733 if (CC_UNLIKELY(err != NO_ERROR)) {
1734 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001735 // caller after a few seconds.
1736 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1737 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001738 mAnimTransactionPending = false;
1739 break;
1740 }
1741 }
1742 }
1743
Mathias Agopiane57f2922012-08-09 16:29:12 -07001744 size_t count = displays.size();
1745 for (size_t i=0 ; i<count ; i++) {
1746 const DisplayState& s(displays[i]);
1747 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001748 }
1749
Mathias Agopiane57f2922012-08-09 16:29:12 -07001750 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001751 for (size_t i=0 ; i<count ; i++) {
1752 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001753 // Here we need to check that the interface we're given is indeed
1754 // one of our own. A malicious client could give us a NULL
1755 // IInterface, or one of its own or even one of our own but a
1756 // different type. All these situations would cause us to crash.
1757 //
1758 // NOTE: it would be better to use RTTI as we could directly check
1759 // that we have a Client*. however, RTTI is disabled in Android.
1760 if (s.client != NULL) {
1761 sp<IBinder> binder = s.client->asBinder();
1762 if (binder != NULL) {
1763 String16 desc(binder->getInterfaceDescriptor());
1764 if (desc == ISurfaceComposerClient::descriptor) {
1765 sp<Client> client( static_cast<Client *>(s.client.get()) );
1766 transactionFlags |= setClientStateLocked(client, s.state);
1767 }
1768 }
1769 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001770 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001771
Mathias Agopian386aa982011-11-07 21:58:03 -08001772 if (transactionFlags) {
1773 // this triggers the transaction
1774 setTransactionFlags(transactionFlags);
1775
1776 // if this is a synchronous transaction, wait for it to take effect
1777 // before returning.
1778 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001779 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001780 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001781 if (flags & eAnimation) {
1782 mAnimTransactionPending = true;
1783 }
1784 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001785 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1786 if (CC_UNLIKELY(err != NO_ERROR)) {
1787 // just in case something goes wrong in SF, return to the
1788 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001789 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1790 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001791 break;
1792 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001793 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794 }
1795}
1796
Mathias Agopiane57f2922012-08-09 16:29:12 -07001797uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1798{
Jesse Hall9a143922012-10-04 16:29:19 -07001799 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1800 if (dpyIdx < 0)
1801 return 0;
1802
Mathias Agopiane57f2922012-08-09 16:29:12 -07001803 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001804 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001805 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001806 const uint32_t what = s.what;
1807 if (what & DisplayState::eSurfaceChanged) {
1808 if (disp.surface->asBinder() != s.surface->asBinder()) {
1809 disp.surface = s.surface;
1810 flags |= eDisplayTransactionNeeded;
1811 }
1812 }
1813 if (what & DisplayState::eLayerStackChanged) {
1814 if (disp.layerStack != s.layerStack) {
1815 disp.layerStack = s.layerStack;
1816 flags |= eDisplayTransactionNeeded;
1817 }
1818 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001819 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001820 if (disp.orientation != s.orientation) {
1821 disp.orientation = s.orientation;
1822 flags |= eDisplayTransactionNeeded;
1823 }
1824 if (disp.frame != s.frame) {
1825 disp.frame = s.frame;
1826 flags |= eDisplayTransactionNeeded;
1827 }
1828 if (disp.viewport != s.viewport) {
1829 disp.viewport = s.viewport;
1830 flags |= eDisplayTransactionNeeded;
1831 }
1832 }
1833 }
1834 return flags;
1835}
1836
1837uint32_t SurfaceFlinger::setClientStateLocked(
1838 const sp<Client>& client,
1839 const layer_state_t& s)
1840{
1841 uint32_t flags = 0;
1842 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1843 if (layer != 0) {
1844 const uint32_t what = s.what;
1845 if (what & layer_state_t::ePositionChanged) {
1846 if (layer->setPosition(s.x, s.y))
1847 flags |= eTraversalNeeded;
1848 }
1849 if (what & layer_state_t::eLayerChanged) {
1850 // NOTE: index needs to be calculated before we update the state
1851 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1852 if (layer->setLayer(s.z)) {
1853 mCurrentState.layersSortedByZ.removeAt(idx);
1854 mCurrentState.layersSortedByZ.add(layer);
1855 // we need traversal (state changed)
1856 // AND transaction (list changed)
1857 flags |= eTransactionNeeded|eTraversalNeeded;
1858 }
1859 }
1860 if (what & layer_state_t::eSizeChanged) {
1861 if (layer->setSize(s.w, s.h)) {
1862 flags |= eTraversalNeeded;
1863 }
1864 }
1865 if (what & layer_state_t::eAlphaChanged) {
1866 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1867 flags |= eTraversalNeeded;
1868 }
1869 if (what & layer_state_t::eMatrixChanged) {
1870 if (layer->setMatrix(s.matrix))
1871 flags |= eTraversalNeeded;
1872 }
1873 if (what & layer_state_t::eTransparentRegionChanged) {
1874 if (layer->setTransparentRegionHint(s.transparentRegion))
1875 flags |= eTraversalNeeded;
1876 }
1877 if (what & layer_state_t::eVisibilityChanged) {
1878 if (layer->setFlags(s.flags, s.mask))
1879 flags |= eTraversalNeeded;
1880 }
1881 if (what & layer_state_t::eCropChanged) {
1882 if (layer->setCrop(s.crop))
1883 flags |= eTraversalNeeded;
1884 }
1885 if (what & layer_state_t::eLayerStackChanged) {
1886 // NOTE: index needs to be calculated before we update the state
1887 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1888 if (layer->setLayerStack(s.layerStack)) {
1889 mCurrentState.layersSortedByZ.removeAt(idx);
1890 mCurrentState.layersSortedByZ.add(layer);
1891 // we need traversal (state changed)
1892 // AND transaction (list changed)
1893 flags |= eTransactionNeeded|eTraversalNeeded;
1894 }
1895 }
1896 }
1897 return flags;
1898}
1899
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001900sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001901 ISurfaceComposerClient::surface_data_t* params,
1902 const String8& name,
1903 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001904 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 uint32_t flags)
1906{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001907 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001908 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001909
1910 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001911 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001912 int(w), int(h));
1913 return surfaceHandle;
1914 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001915
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001916 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001917 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1918 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001919 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001921 case ISurfaceComposerClient::eFXSurfaceBlur:
1922 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001923 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001925 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001926 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001927 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 }
1929
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001930 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001931 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001932 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001933 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001935 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001936 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001937 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001938 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001939 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 }
1941
1942 return surfaceHandle;
1943}
1944
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001945sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001946 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001947 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001948 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949{
1950 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001951 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 case PIXEL_FORMAT_TRANSPARENT:
1953 case PIXEL_FORMAT_TRANSLUCENT:
1954 format = PIXEL_FORMAT_RGBA_8888;
1955 break;
1956 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001957#ifdef NO_RGBX_8888
1958 format = PIXEL_FORMAT_RGB_565;
1959#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001960 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001961#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962 break;
1963 }
1964
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001965#ifdef NO_RGBX_8888
1966 if (format == PIXEL_FORMAT_RGBX_8888)
1967 format = PIXEL_FORMAT_RGBA_8888;
1968#endif
1969
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001970 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001971 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001972 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001973 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001974 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975 }
1976 return layer;
1977}
1978
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001979sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001980 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001981 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001983 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001984 return layer;
1985}
1986
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001987sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001988 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001989 uint32_t w, uint32_t h, uint32_t flags)
1990{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001991 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992 return layer;
1993}
1994
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001995status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001996{
Mathias Agopian9a112062009-04-17 19:36:26 -07001997 /*
1998 * called by the window manager, when a surface should be marked for
1999 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002000 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002001 * The surface is removed from the current and drawing lists, but placed
2002 * in the purgatory queue, so it's not destroyed right-away (we need
2003 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07002004 */
Mathias Agopian9a112062009-04-17 19:36:26 -07002005
Mathias Agopian48d819a2009-09-10 19:41:18 -07002006 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07002007 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002008 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08002009
Mathias Agopian48d819a2009-09-10 19:41:18 -07002010 if (layer != 0) {
2011 err = purgatorizeLayer_l(layer);
2012 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07002013 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07002014 }
Mathias Agopian9a112062009-04-17 19:36:26 -07002015 }
2016 return err;
2017}
2018
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002019status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002020{
Mathias Agopian759fdb22009-07-02 17:33:40 -07002021 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002022 status_t err = NO_ERROR;
2023 sp<LayerBaseClient> l(layer.promote());
2024 if (l != NULL) {
2025 Mutex::Autolock _l(mStateLock);
2026 err = removeLayer_l(l);
2027 if (err == NAME_NOT_FOUND) {
2028 // The surface wasn't in the current list, which means it was
2029 // removed already, which means it is in the purgatory,
2030 // and need to be removed from there.
2031 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002032 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002033 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07002034 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002035 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002036 "error removing layer=%p (%s)", l.get(), strerror(-err));
2037 }
2038 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039}
2040
Mathias Agopianb60314a2012-04-10 22:09:54 -07002041// ---------------------------------------------------------------------------
2042
Andy McFadden13a082e2012-08-24 10:16:42 -07002043void SurfaceFlinger::onInitializeDisplays() {
2044 // reset screen orientation
2045 Vector<ComposerState> state;
2046 Vector<DisplayState> displays;
2047 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002048 d.what = DisplayState::eDisplayProjectionChanged;
Jesse Hall6edebdf2012-11-08 15:41:56 -08002049 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07002050 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002051 d.frame.makeInvalid();
2052 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002053 displays.add(d);
2054 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002055 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07002056}
2057
2058void SurfaceFlinger::initializeDisplays() {
2059 class MessageScreenInitialized : public MessageBase {
2060 SurfaceFlinger* flinger;
2061 public:
2062 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2063 virtual bool handler() {
2064 flinger->onInitializeDisplays();
2065 return true;
2066 }
2067 };
2068 sp<MessageBase> msg = new MessageScreenInitialized(this);
2069 postMessageAsync(msg); // we may be called from main thread, use async message
2070}
2071
2072
Mathias Agopiancde87a32012-09-13 14:09:01 -07002073void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002074 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2075 if (hw->isScreenAcquired()) {
2076 // this is expected, e.g. when power manager wakes up during boot
2077 ALOGD(" screen was previously acquired");
2078 return;
2079 }
2080
Mathias Agopian42977342012-08-05 00:40:46 -07002081 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002082 int32_t type = hw->getDisplayType();
2083 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2084 // built-in display, tell the HWC
2085 getHwComposer().acquire(type);
2086
2087 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2088 // FIXME: eventthread only knows about the main display right now
2089 mEventThread->onScreenAcquired();
2090 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002091 }
Mathias Agopian20128302012-08-13 18:32:13 -07002092 mVisibleRegionsDirty = true;
2093 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002094}
2095
Mathias Agopiancde87a32012-09-13 14:09:01 -07002096void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002097 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2098 if (!hw->isScreenAcquired()) {
2099 ALOGD(" screen was previously released");
2100 return;
2101 }
2102
2103 hw->releaseScreen();
2104 int32_t type = hw->getDisplayType();
2105 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2106 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002107 // FIXME: eventthread only knows about the main display right now
2108 mEventThread->onScreenReleased();
2109 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002110
2111 // built-in display, tell the HWC
2112 getHwComposer().release(type);
2113 }
2114 mVisibleRegionsDirty = true;
2115 // from this point on, SF will stop drawing on this display
2116}
2117
2118void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2119 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002120 SurfaceFlinger& mFlinger;
2121 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002122 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002123 MessageScreenAcquired(SurfaceFlinger& flinger,
2124 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002125 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002126 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2127 if (hw == NULL) {
2128 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2129 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2130 ALOGW("Attempt to unblank virtual display");
2131 } else {
2132 mFlinger.onScreenAcquired(hw);
2133 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002134 return true;
2135 }
2136 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002137 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2138 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002139}
2140
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002141void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002142 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002143 SurfaceFlinger& mFlinger;
2144 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002145 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002146 MessageScreenReleased(SurfaceFlinger& flinger,
2147 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002148 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002149 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2150 if (hw == NULL) {
2151 ALOGE("Attempt to blank null display %p", mDisplay.get());
2152 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2153 ALOGW("Attempt to blank virtual display");
2154 } else {
2155 mFlinger.onScreenReleased(hw);
2156 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002157 return true;
2158 }
2159 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002160 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2161 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002162}
2163
2164// ---------------------------------------------------------------------------
2165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002166status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2167{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002168 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002169 char buffer[SIZE];
2170 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002171
2172 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002173 snprintf(buffer, SIZE, "Permission Denial: "
2174 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2175 IPCThreadState::self()->getCallingPid(),
2176 IPCThreadState::self()->getCallingUid());
2177 result.append(buffer);
2178 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002179 // Try to get the main lock, but don't insist if we can't
2180 // (this would indicate SF is stuck, but we want to be able to
2181 // print something in dumpsys).
2182 int retry = 3;
2183 while (mStateLock.tryLock()<0 && --retry>=0) {
2184 usleep(1000000);
2185 }
2186 const bool locked(retry >= 0);
2187 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002188 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002189 "SurfaceFlinger appears to be unresponsive, "
2190 "dumping anyways (no locks held)\n");
2191 result.append(buffer);
2192 }
2193
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002194 bool dumpAll = true;
2195 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002196 size_t numArgs = args.size();
2197 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002198 if ((index < numArgs) &&
2199 (args[index] == String16("--list"))) {
2200 index++;
2201 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002202 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002203 }
2204
2205 if ((index < numArgs) &&
2206 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002207 index++;
2208 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002209 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002210 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211
2212 if ((index < numArgs) &&
2213 (args[index] == String16("--latency-clear"))) {
2214 index++;
2215 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002216 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002217 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002218 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002219
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002220 if (dumpAll) {
2221 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002222 }
2223
Mathias Agopian9795c422009-08-26 16:36:26 -07002224 if (locked) {
2225 mStateLock.unlock();
2226 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227 }
2228 write(fd, result.string(), result.size());
2229 return NO_ERROR;
2230}
2231
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002232void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2233 String8& result, char* buffer, size_t SIZE) const
2234{
2235 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2236 const size_t count = currentLayers.size();
2237 for (size_t i=0 ; i<count ; i++) {
2238 const sp<LayerBase>& layer(currentLayers[i]);
2239 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2240 result.append(buffer);
2241 }
2242}
2243
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002244void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2245 String8& result, char* buffer, size_t SIZE) const
2246{
2247 String8 name;
2248 if (index < args.size()) {
2249 name = String8(args[index]);
2250 index++;
2251 }
2252
2253 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2254 const size_t count = currentLayers.size();
2255 for (size_t i=0 ; i<count ; i++) {
2256 const sp<LayerBase>& layer(currentLayers[i]);
2257 if (name.isEmpty()) {
2258 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2259 result.append(buffer);
2260 }
2261 if (name.isEmpty() || (name == layer->getName())) {
2262 layer->dumpStats(result, buffer, SIZE);
2263 }
2264 }
2265}
2266
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002267void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
2268 String8& result, char* buffer, size_t SIZE) const
2269{
2270 String8 name;
2271 if (index < args.size()) {
2272 name = String8(args[index]);
2273 index++;
2274 }
2275
2276 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2277 const size_t count = currentLayers.size();
2278 for (size_t i=0 ; i<count ; i++) {
2279 const sp<LayerBase>& layer(currentLayers[i]);
2280 if (name.isEmpty() || (name == layer->getName())) {
2281 layer->clearStats();
2282 }
2283 }
2284}
2285
Andy McFadden4803b742012-09-24 19:07:20 -07002286/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2287{
2288 static const char* config =
2289 " [sf"
2290#ifdef NO_RGBX_8888
2291 " NO_RGBX_8888"
2292#endif
2293#ifdef HAS_CONTEXT_PRIORITY
2294 " HAS_CONTEXT_PRIORITY"
2295#endif
2296#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2297 " NEVER_DEFAULT_TO_ASYNC_MODE"
2298#endif
2299#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2300 " TARGET_DISABLE_TRIPLE_BUFFERING"
2301#endif
2302 "]";
2303 result.append(config);
2304}
2305
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002306void SurfaceFlinger::dumpAllLocked(
2307 String8& result, char* buffer, size_t SIZE) const
2308{
2309 // figure out if we're stuck somewhere
2310 const nsecs_t now = systemTime();
2311 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2312 const nsecs_t inTransaction(mDebugInTransaction);
2313 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2314 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2315
2316 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002317 * Dump library configuration.
2318 */
2319 result.append("Build configuration:");
2320 appendSfConfigString(result);
2321 appendUiConfigString(result);
2322 appendGuiConfigString(result);
2323 result.append("\n");
2324
2325 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326 * Dump the visible layer list
2327 */
2328 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2329 const size_t count = currentLayers.size();
2330 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2331 result.append(buffer);
2332 for (size_t i=0 ; i<count ; i++) {
2333 const sp<LayerBase>& layer(currentLayers[i]);
2334 layer->dump(result, buffer, SIZE);
2335 }
2336
2337 /*
2338 * Dump the layers in the purgatory
2339 */
2340
2341 const size_t purgatorySize = mLayerPurgatory.size();
2342 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2343 result.append(buffer);
2344 for (size_t i=0 ; i<purgatorySize ; i++) {
2345 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2346 layer->shortDump(result, buffer, SIZE);
2347 }
2348
2349 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002350 * Dump Display state
2351 */
2352
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002353 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2354 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002355 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2356 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002357 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002358 }
2359
2360 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002361 * Dump SurfaceFlinger global state
2362 */
2363
2364 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2365 result.append(buffer);
2366
Mathias Agopian888c8222012-08-04 21:10:38 -07002367 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002368 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002369 const GLExtensions& extensions(GLExtensions::getInstance());
2370 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2371 extensions.getVendor(),
2372 extensions.getRenderer(),
2373 extensions.getVersion());
2374 result.append(buffer);
2375
2376 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002377 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002378 result.append(buffer);
2379
2380 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2381 result.append(buffer);
2382
Mathias Agopian42977342012-08-05 00:40:46 -07002383 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002384 snprintf(buffer, SIZE,
2385 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002386 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002387 result.append(buffer);
2388 snprintf(buffer, SIZE,
2389 " last eglSwapBuffers() time: %f us\n"
2390 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002391 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002392 " refresh-rate : %f fps\n"
2393 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002394 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002395 mLastSwapBufferTime/1000.0,
2396 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002397 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002398 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2399 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2400 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002401 result.append(buffer);
2402
2403 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2404 inSwapBuffersDuration/1000.0);
2405 result.append(buffer);
2406
2407 snprintf(buffer, SIZE, " transaction time: %f us\n",
2408 inTransactionDuration/1000.0);
2409 result.append(buffer);
2410
2411 /*
2412 * VSYNC state
2413 */
2414 mEventThread->dump(result, buffer, SIZE);
2415
2416 /*
2417 * Dump HWComposer state
2418 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002419 snprintf(buffer, SIZE, "h/w composer state:\n");
2420 result.append(buffer);
2421 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2422 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2423 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2424 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002425 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002426
2427 /*
2428 * Dump gralloc state
2429 */
2430 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2431 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002432}
2433
Mathias Agopiancb558572012-10-04 15:58:54 -07002434const Vector< sp<LayerBase> >&
2435SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002436 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002437 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2438}
2439
Keun young Park63f165f2012-08-31 10:53:36 -07002440bool SurfaceFlinger::startDdmConnection()
2441{
2442 void* libddmconnection_dso =
2443 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2444 if (!libddmconnection_dso) {
2445 return false;
2446 }
2447 void (*DdmConnection_start)(const char* name);
2448 DdmConnection_start =
2449 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2450 if (!DdmConnection_start) {
2451 dlclose(libddmconnection_dso);
2452 return false;
2453 }
2454 (*DdmConnection_start)(getServiceName());
2455 return true;
2456}
2457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002458status_t SurfaceFlinger::onTransact(
2459 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2460{
2461 switch (code) {
2462 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002463 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002465 case BLANK:
2466 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002467 {
2468 // codes that require permission check
2469 IPCThreadState* ipc = IPCThreadState::self();
2470 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002471 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002472 if ((uid != AID_GRAPHICS) &&
2473 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002474 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002475 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2476 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002478 break;
2479 }
2480 case CAPTURE_SCREEN:
2481 {
2482 // codes that require permission check
2483 IPCThreadState* ipc = IPCThreadState::self();
2484 const int pid = ipc->getCallingPid();
2485 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002486 if ((uid != AID_GRAPHICS) &&
2487 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002488 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2490 return PERMISSION_DENIED;
2491 }
2492 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493 }
2494 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2497 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002498 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002499 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002500 IPCThreadState* ipc = IPCThreadState::self();
2501 const int pid = ipc->getCallingPid();
2502 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002503 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002504 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 return PERMISSION_DENIED;
2506 }
2507 int n;
2508 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002509 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002510 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 return NO_ERROR;
2512 case 1002: // SHOW_UPDATES
2513 n = data.readInt32();
2514 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002515 invalidateHwcGeometry();
2516 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002519 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002520 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002522 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002523 setTransactionFlags(
2524 eTransactionNeeded|
2525 eDisplayTransactionNeeded|
2526 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002527 return NO_ERROR;
2528 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002529 case 1006:{ // send empty update
2530 signalRefresh();
2531 return NO_ERROR;
2532 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002533 case 1008: // toggle use of hw composer
2534 n = data.readInt32();
2535 mDebugDisableHWC = n ? 1 : 0;
2536 invalidateHwcGeometry();
2537 repaintEverything();
2538 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002539 case 1009: // toggle use of transform hint
2540 n = data.readInt32();
2541 mDebugDisableTransformHint = n ? 1 : 0;
2542 invalidateHwcGeometry();
2543 repaintEverything();
2544 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002545 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002546 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002547 reply->writeInt32(0);
2548 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002549 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002550 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551 return NO_ERROR;
2552 case 1013: {
2553 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002554 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2555 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 }
2557 return NO_ERROR;
2558 }
2559 }
2560 return err;
2561}
2562
Mathias Agopian53331da2011-08-22 21:44:41 -07002563void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002564 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002565 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002566}
2567
Mathias Agopian59119e62010-10-11 12:37:43 -07002568// ---------------------------------------------------------------------------
2569
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002570status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002571 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2572{
2573 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002574 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002575}
2576
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002577status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002578 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002579{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002580 ATRACE_CALL();
2581
Mathias Agopian59119e62010-10-11 12:37:43 -07002582 if (!GLExtensions::getInstance().haveFramebufferObject())
2583 return INVALID_OPERATION;
2584
2585 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002586 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2587 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002588 const uint32_t hw_w = hw->getWidth();
2589 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002590 GLfloat u = 1;
2591 GLfloat v = 1;
2592
2593 // make sure to clear all GL error flags
2594 while ( glGetError() != GL_NO_ERROR ) ;
2595
2596 // create a FBO
2597 GLuint name, tname;
2598 glGenTextures(1, &tname);
2599 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002600 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2601 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002602 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2603 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002604 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002605 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002606 GLint tw = (2 << (31 - clz(hw_w)));
2607 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002608 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2609 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002610 u = GLfloat(hw_w) / tw;
2611 v = GLfloat(hw_h) / th;
2612 }
2613 glGenFramebuffersOES(1, &name);
2614 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002615 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2616 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002617
Mathias Agopianbae92d02012-09-28 01:00:47 -07002618 DisplayDevice::setViewportAndProjection(hw);
2619
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002620 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002621 glDisable(GL_TEXTURE_EXTERNAL_OES);
2622 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002623 glClearColor(0,0,0,1);
2624 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002625 glMatrixMode(GL_MODELVIEW);
2626 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002627 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002628 const size_t count = layers.size();
2629 for (size_t i=0 ; i<count ; ++i) {
2630 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002631 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002632 }
2633
Mathias Agopian42977342012-08-05 00:40:46 -07002634 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002635
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002636 // back to main framebuffer
2637 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002638 glDeleteFramebuffersOES(1, &name);
2639
2640 *textureName = tname;
2641 *uOut = u;
2642 *vOut = v;
2643 return NO_ERROR;
2644}
2645
2646// ---------------------------------------------------------------------------
2647
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002648status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002649 sp<IMemoryHeap>* heap,
2650 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002651 uint32_t sw, uint32_t sh,
2652 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002653{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002654 ATRACE_CALL();
2655
Mathias Agopian74c40c02010-09-29 13:02:36 -07002656 status_t result = PERMISSION_DENIED;
2657
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002658 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002659 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002660 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002661
2662 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002663 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002664 const uint32_t hw_w = hw->getWidth();
2665 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002666
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002667 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002668 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002669 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002670 return PERMISSION_DENIED;
2671 }
2672
2673 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002674 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002675 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002676 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002677
2678 sw = (!sw) ? hw_w : sw;
2679 sh = (!sh) ? hw_h : sh;
2680 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002681 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002682
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002683// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2684// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002685
Mathias Agopian74c40c02010-09-29 13:02:36 -07002686 // make sure to clear all GL error flags
2687 while ( glGetError() != GL_NO_ERROR ) ;
2688
2689 // create a FBO
2690 GLuint name, tname;
2691 glGenRenderbuffersOES(1, &tname);
2692 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2693 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002694
Mathias Agopian74c40c02010-09-29 13:02:36 -07002695 glGenFramebuffersOES(1, &name);
2696 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2697 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2698 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2699
2700 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002701
Mathias Agopian74c40c02010-09-29 13:02:36 -07002702 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2703
2704 // invert everything, b/c glReadPixel() below will invert the FB
Mathias Agopian135e5892012-09-30 16:43:20 -07002705 GLint viewport[4];
2706 glGetIntegerv(GL_VIEWPORT, viewport);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002707 glViewport(0, 0, sw, sh);
2708 glMatrixMode(GL_PROJECTION);
2709 glPushMatrix();
2710 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002711 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002712 glMatrixMode(GL_MODELVIEW);
2713
2714 // redraw the screen entirely...
2715 glClearColor(0,0,0,1);
2716 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002717
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002718 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002719 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002720 for (size_t i=0 ; i<count ; ++i) {
2721 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002722 const uint32_t z = layer->drawingState().z;
2723 if (z >= minLayerZ && z <= maxLayerZ) {
2724 if (filtering) layer->setFiltering(true);
2725 layer->draw(hw);
2726 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002727 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002728 }
2729
Mathias Agopian74c40c02010-09-29 13:02:36 -07002730 // check for errors and return screen capture
2731 if (glGetError() != GL_NO_ERROR) {
2732 // error while rendering
2733 result = INVALID_OPERATION;
2734 } else {
2735 // allocate shared memory large enough to hold the
2736 // screen capture
2737 sp<MemoryHeapBase> base(
2738 new MemoryHeapBase(size, 0, "screen-capture") );
2739 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002740 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002741 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002742 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002743 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2744 if (glGetError() == GL_NO_ERROR) {
2745 *heap = base;
2746 *w = sw;
2747 *h = sh;
2748 *f = PIXEL_FORMAT_RGBA_8888;
2749 result = NO_ERROR;
2750 }
2751 } else {
2752 result = NO_MEMORY;
2753 }
2754 }
Mathias Agopian135e5892012-09-30 16:43:20 -07002755 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002756 glMatrixMode(GL_PROJECTION);
2757 glPopMatrix();
2758 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002759 } else {
2760 result = BAD_VALUE;
2761 }
2762
2763 // release FBO resources
2764 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2765 glDeleteRenderbuffersOES(1, &tname);
2766 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002767
Mathias Agopian42977342012-08-05 00:40:46 -07002768 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002769
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002770// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002771
Mathias Agopian74c40c02010-09-29 13:02:36 -07002772 return result;
2773}
2774
2775
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002776status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002777 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002778 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002779 uint32_t sw, uint32_t sh,
2780 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002781{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002782 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002783 return BAD_VALUE;
2784
2785 if (!GLExtensions::getInstance().haveFramebufferObject())
2786 return INVALID_OPERATION;
2787
2788 class MessageCaptureScreen : public MessageBase {
2789 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002790 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002791 sp<IMemoryHeap>* heap;
2792 uint32_t* w;
2793 uint32_t* h;
2794 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002795 uint32_t sw;
2796 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002797 uint32_t minLayerZ;
2798 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002799 status_t result;
2800 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002801 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002802 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002803 uint32_t sw, uint32_t sh,
2804 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002805 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002806 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2807 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2808 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002809 {
2810 }
2811 status_t getResult() const {
2812 return result;
2813 }
2814 virtual bool handler() {
2815 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002816 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002817 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002818 return true;
2819 }
2820 };
2821
2822 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002823 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002824 status_t res = postMessageSync(msg);
2825 if (res == NO_ERROR) {
2826 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2827 }
2828 return res;
2829}
2830
2831// ---------------------------------------------------------------------------
2832
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002833SurfaceFlinger::LayerVector::LayerVector() {
2834}
2835
2836SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2837 : SortedVector<sp<LayerBase> >(rhs) {
2838}
2839
2840int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2841 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002842{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002843 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002844 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2845 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002846
2847 uint32_t ls = l->currentState().layerStack;
2848 uint32_t rs = r->currentState().layerStack;
2849 if (ls != rs)
2850 return ls - rs;
2851
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002852 uint32_t lz = l->currentState().z;
2853 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002854 if (lz != rz)
2855 return lz - rz;
2856
2857 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002858}
2859
2860// ---------------------------------------------------------------------------
2861
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002862SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2863 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002864}
2865
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002866SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2867 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002868 viewport.makeInvalid();
2869 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002870}
2871
2872// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002873
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002874}; // namespace android