blob: 13b2c6be25d212cf4a38118ea680d31bd3818cc6 [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
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700166sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700167{
168 class DisplayToken : public BBinder {
169 sp<SurfaceFlinger> flinger;
170 virtual ~DisplayToken() {
171 // no more references, this display must be terminated
172 Mutex::Autolock _l(flinger->mStateLock);
173 flinger->mCurrentState.displays.removeItem(this);
174 flinger->setTransactionFlags(eDisplayTransactionNeeded);
175 }
176 public:
177 DisplayToken(const sp<SurfaceFlinger>& flinger)
178 : flinger(flinger) {
179 }
180 };
181
182 sp<BBinder> token = new DisplayToken(this);
183
184 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700185 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700186 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700187 mCurrentState.displays.add(token, info);
188
189 return token;
190}
191
192sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700193 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700194 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
195 return NULL;
196 }
197 return mDefaultDisplays[id];
198}
199
Jamie Gennis9a78c902011-01-12 18:30:40 -0800200sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
201{
202 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
203 return gba;
204}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206void SurfaceFlinger::bootFinished()
207{
208 const nsecs_t now = systemTime();
209 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000210 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700211 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700212
213 // wait patiently for the window manager death
214 const String16 name("window");
215 sp<IBinder> window(defaultServiceManager()->getService(name));
216 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700217 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700218 }
219
220 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700221 // formerly we would just kill the process, but we now ask it to exit so it
222 // can choose where to stop the animation.
223 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224}
225
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700226void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
227 class MessageDestroyGLTexture : public MessageBase {
228 GLuint texture;
229 public:
230 MessageDestroyGLTexture(GLuint texture)
231 : texture(texture) {
232 }
233 virtual bool handler() {
234 glDeleteTextures(1, &texture);
235 return true;
236 }
237 };
238 postMessageAsync(new MessageDestroyGLTexture(texture));
239}
240
Mathias Agopian722b98f2012-09-25 18:24:31 -0700241status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700242 EGLDisplay dpy,
243 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700244 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700245 EGLConfig* outConfig)
246{
247 EGLConfig config = NULL;
248 EGLint numConfigs = -1, n=0;
249 eglGetConfigs(dpy, NULL, 0, &numConfigs);
250 EGLConfig* const configs = new EGLConfig[numConfigs];
251 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700252
Mathias Agopian722b98f2012-09-25 18:24:31 -0700253 if (n) {
254 if (attribute != EGL_NONE) {
255 for (int i=0 ; i<n ; i++) {
256 EGLint value = 0;
257 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
258 if (wanted == value) {
259 *outConfig = configs[i];
260 delete [] configs;
261 return NO_ERROR;
262 }
263 }
264 } else {
265 // just pick the first one
266 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700267 delete [] configs;
268 return NO_ERROR;
269 }
270 }
271 delete [] configs;
272 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273}
274
Mathias Agopian722b98f2012-09-25 18:24:31 -0700275class EGLAttributeVector {
276 struct Attribute;
277 class Adder;
278 friend class Adder;
279 KeyedVector<Attribute, EGLint> mList;
280 struct Attribute {
281 Attribute() {};
282 Attribute(EGLint v) : v(v) { }
283 EGLint v;
284 bool operator < (const Attribute& other) const {
285 // this places EGL_NONE at the end
286 EGLint lhs(v);
287 EGLint rhs(other.v);
288 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
289 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
290 return lhs < rhs;
291 }
292 };
293 class Adder {
294 friend class EGLAttributeVector;
295 EGLAttributeVector& v;
296 EGLint attribute;
297 Adder(EGLAttributeVector& v, EGLint attribute)
298 : v(v), attribute(attribute) {
299 }
300 public:
301 void operator = (EGLint value) {
302 if (attribute != EGL_NONE) {
303 v.mList.add(attribute, value);
304 }
305 }
306 operator EGLint () const { return v.mList[attribute]; }
307 };
308public:
309 EGLAttributeVector() {
310 mList.add(EGL_NONE, EGL_NONE);
311 }
312 void remove(EGLint attribute) {
313 if (attribute != EGL_NONE) {
314 mList.removeItem(attribute);
315 }
316 }
317 Adder operator [] (EGLint attribute) {
318 return Adder(*this, attribute);
319 }
320 EGLint operator [] (EGLint attribute) const {
321 return mList[attribute];
322 }
323 // cast-operator to (EGLint const*)
324 operator EGLint const* () const { return &mList.keyAt(0).v; }
325};
326
Mathias Agopiana4912602012-07-12 14:25:33 -0700327EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
328 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
329 // it is to be used with WIFI displays
330 EGLConfig config;
331 EGLint dummy;
332 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700333
Mathias Agopian722b98f2012-09-25 18:24:31 -0700334 EGLAttributeVector attribs;
335 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
336 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
337 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
338 attribs[EGL_RED_SIZE] = 8;
339 attribs[EGL_GREEN_SIZE] = 8;
340 attribs[EGL_BLUE_SIZE] = 8;
341
342 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
343 if (!err)
344 goto success;
345
346 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
347 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
348 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
349 err = selectConfigForAttribute(display, attribs,
350 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700351 if (!err)
352 goto success;
353
354 // maybe we failed because of EGL_RECORDABLE_ANDROID
355 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700356 attribs.remove(EGL_RECORDABLE_ANDROID);
357 err = selectConfigForAttribute(display, attribs,
358 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700359 if (!err)
360 goto success;
361
362 // allow less than 24-bit color; the non-gpu-accelerated emulator only
363 // supports 16-bit color
364 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700365 attribs.remove(EGL_RED_SIZE);
366 attribs.remove(EGL_GREEN_SIZE);
367 attribs.remove(EGL_BLUE_SIZE);
368 err = selectConfigForAttribute(display, attribs,
369 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700370 if (!err)
371 goto success;
372
373 // this EGL is too lame for Android
374 ALOGE("no suitable EGLConfig found, giving up");
375
376 return 0;
377
378success:
379 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700380 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700381 return config;
382}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383
Mathias Agopiana4912602012-07-12 14:25:33 -0700384EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
385 // Also create our EGLContext
386 EGLint contextAttributes[] = {
387#ifdef EGL_IMG_context_priority
388#ifdef HAS_CONTEXT_PRIORITY
389#warning "using EGL_IMG_context_priority"
390 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
391#endif
392#endif
393 EGL_NONE, EGL_NONE
394 };
395 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
396 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
397 return ctxt;
398}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
Mathias Agopiancde87a32012-09-13 14:09:01 -0700400void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700401 GLExtensions& extensions(GLExtensions::getInstance());
402 extensions.initWithGLStrings(
403 glGetString(GL_VENDOR),
404 glGetString(GL_RENDERER),
405 glGetString(GL_VERSION),
406 glGetString(GL_EXTENSIONS),
407 eglQueryString(display, EGL_VENDOR),
408 eglQueryString(display, EGL_VERSION),
409 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700410
Mathias Agopiana4912602012-07-12 14:25:33 -0700411 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
412 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700413
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700415 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 glShadeModel(GL_FLAT);
418 glDisable(GL_DITHER);
419 glDisable(GL_CULL_FACE);
420
Mathias Agopiana4912602012-07-12 14:25:33 -0700421 struct pack565 {
422 inline uint16_t operator() (int r, int g, int b) const {
423 return (r<<11)|(g<<5)|b;
424 }
425 } pack565;
426
Jamie Gennis9575f602011-10-07 14:51:16 -0700427 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
428 glGenTextures(1, &mProtectedTexName);
429 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
430 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
431 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
432 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
433 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
434 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
435 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436
Mathias Agopiana4912602012-07-12 14:25:33 -0700437 // print some debugging info
438 EGLint r,g,b,a;
439 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
440 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
441 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
442 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
443 ALOGI("EGL informations:");
444 ALOGI("vendor : %s", extensions.getEglVendor());
445 ALOGI("version : %s", extensions.getEglVersion());
446 ALOGI("extensions: %s", extensions.getEglExtension());
447 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
448 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
449 ALOGI("OpenGL ES informations:");
450 ALOGI("vendor : %s", extensions.getVendor());
451 ALOGI("renderer : %s", extensions.getRenderer());
452 ALOGI("version : %s", extensions.getVersion());
453 ALOGI("extensions: %s", extensions.getExtension());
454 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
455 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
456}
457
Mathias Agopiana4912602012-07-12 14:25:33 -0700458status_t SurfaceFlinger::readyToRun()
459{
460 ALOGI( "SurfaceFlinger's main thread ready to run. "
461 "Initializing graphics H/W...");
462
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700463 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700464 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
465 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700466
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700467 // Initialize the H/W composer object. There may or may not be an
468 // actual hardware composer underneath.
469 mHwc = new HWComposer(this,
470 *static_cast<HWComposer::EventHandler *>(this));
471
Mathias Agopiana4912602012-07-12 14:25:33 -0700472 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700473 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700474 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
475 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700476
Mathias Agopianda27af92012-09-13 18:17:13 -0700477 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
478 "couldn't create EGLContext");
479
Mathias Agopiancde87a32012-09-13 14:09:01 -0700480 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700481 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700482 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700483 mDefaultDisplays[i] = new BBinder();
Mathias Agopianf5a33922012-09-19 18:16:22 -0700484 wp<IBinder> token = mDefaultDisplays[i];
Mathias Agopianf5a33922012-09-19 18:16:22 -0700485
486 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700487 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
488 mCurrentState.displays.add(token, DisplayDeviceState(type));
Mathias Agopianf5a33922012-09-19 18:16:22 -0700489 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
490 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
491 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
492 sp<DisplayDevice> hw = new DisplayDevice(this,
493 type, token, stc, fbs, mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700494 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700495 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700496 // for displays other than the main display, so we always
497 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700498 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700499 hw->acquireScreen();
500 }
501 mDisplays.add(token, hw);
502 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700503 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700504
Mathias Agopianf5a33922012-09-19 18:16:22 -0700505 // we need a GL context current in a few places, when initializing
506 // OpenGL ES (see below), or creating a layer,
507 // or when a texture is (asynchronously) destroyed, and for that
508 // we need a valid surface, so it's convenient to use the main display
509 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700510 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700511
Mathias Agopiana4912602012-07-12 14:25:33 -0700512 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700513 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
514 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800515
Mathias Agopian028508c2012-07-25 21:12:12 -0700516 // start the EventThread
517 mEventThread = new EventThread(this);
518 mEventQueue.setEventThread(mEventThread);
519
Mathias Agopian92a979a2012-08-02 18:32:23 -0700520 // initialize our drawing state
521 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700522
Mathias Agopiancde87a32012-09-13 14:09:01 -0700523
Mathias Agopiana4912602012-07-12 14:25:33 -0700524 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800525 mReadyToRunBarrier.open();
526
Andy McFadden13a082e2012-08-24 10:16:42 -0700527 // set initial conditions (e.g. unblank default device)
528 initializeDisplays();
529
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700530 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700531 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 return NO_ERROR;
534}
535
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700536int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
537 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
538 type : mHwc->allocateDisplayId();
539}
540
Mathias Agopiana67e4182012-06-19 17:26:12 -0700541void SurfaceFlinger::startBootAnim() {
542 // start boot animation
543 property_set("service.bootanim.exit", "0");
544 property_set("ctl.start", "bootanim");
545}
546
Mathias Agopiana4912602012-07-12 14:25:33 -0700547uint32_t SurfaceFlinger::getMaxTextureSize() const {
548 return mMaxTextureSize;
549}
550
551uint32_t SurfaceFlinger::getMaxViewportDims() const {
552 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
553 mMaxViewportDims[0] : mMaxViewportDims[1];
554}
555
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800557
Jamie Gennis582270d2011-08-17 18:19:00 -0700558bool SurfaceFlinger::authenticateSurfaceTexture(
559 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800560 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700561 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800562
563 // Check the visible layer list for the ISurface
564 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
565 size_t count = currentLayers.size();
566 for (size_t i=0 ; i<count ; i++) {
567 const sp<LayerBase>& layer(currentLayers[i]);
568 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700569 if (lbc != NULL) {
570 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
571 if (lbcBinder == surfaceTextureBinder) {
572 return true;
573 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800574 }
575 }
576
577 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700578 // SurfaceTexture gets destroyed before all the clients are done using it,
579 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800580 // will instead fail later on when the client tries to use the surface,
581 // which should be reported as "surface XYZ returned an -ENODEV". The
582 // purgatorized layers are no less authentic than the visible ones, so this
583 // should not cause any harm.
584 size_t purgatorySize = mLayerPurgatory.size();
585 for (size_t i=0 ; i<purgatorySize ; i++) {
586 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
587 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700588 if (lbc != NULL) {
589 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
590 if (lbcBinder == surfaceTextureBinder) {
591 return true;
592 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800593 }
594 }
595
596 return false;
597}
598
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700599status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700600 int32_t type = BAD_VALUE;
601 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
602 if (display == mDefaultDisplays[i]) {
603 type = i;
604 break;
605 }
606 }
607
608 if (type < 0) {
609 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700610 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700611
612 const HWComposer& hwc(getHwComposer());
Mathias Agopian4c0751a2012-09-20 21:28:12 -0700613 if (!hwc.isConnected(type)) {
614 return NAME_NOT_FOUND;
615 }
616
Mathias Agopian1604f772012-09-18 21:54:42 -0700617 float xdpi = hwc.getDpiX(type);
618 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700619
620 // TODO: Not sure if display density should handled by SF any longer
621 class Density {
622 static int getDensityFromProperty(char const* propName) {
623 char property[PROPERTY_VALUE_MAX];
624 int density = 0;
625 if (property_get(propName, property, NULL) > 0) {
626 density = atoi(property);
627 }
628 return density;
629 }
630 public:
631 static int getEmuDensity() {
632 return getDensityFromProperty("qemu.sf.lcd_density"); }
633 static int getBuildDensity() {
634 return getDensityFromProperty("ro.sf.lcd_density"); }
635 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700636
637 if (type == DisplayDevice::DISPLAY_PRIMARY) {
638 // The density of the device is provided by a build property
639 float density = Density::getBuildDensity() / 160.0f;
640 if (density == 0) {
641 // the build doesn't provide a density -- this is wrong!
642 // use xdpi instead
643 ALOGE("ro.sf.lcd_density must be defined as a build property");
644 density = xdpi / 160.0f;
645 }
646 if (Density::getEmuDensity()) {
647 // if "qemu.sf.lcd_density" is specified, it overrides everything
648 xdpi = ydpi = density = Density::getEmuDensity();
649 density /= 160.0f;
650 }
651 info->density = density;
652
653 // TODO: this needs to go away (currently needed only by webkit)
654 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
655 info->orientation = hw->getOrientation();
656 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
657 } else {
658 // TODO: where should this value come from?
659 static const int TV_DENSITY = 213;
660 info->density = TV_DENSITY / 160.0f;
661 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700662 }
663
Mathias Agopian1604f772012-09-18 21:54:42 -0700664 info->w = hwc.getWidth(type);
665 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700666 info->xdpi = xdpi;
667 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700668 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Mathias Agopian888c8222012-08-04 21:10:38 -0700669 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700670}
671
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800672// ----------------------------------------------------------------------------
673
674sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800675 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700676}
677
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700678void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700679
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700680 sp<IBinder> token;
681 { // scope for the lock
682 Mutex::Autolock _l(mStateLock);
683 token = mExtDisplayToken;
684 }
685
686 if (token == 0) {
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700687 token = createDisplay(String8("Display from connectDisplay"));
Mathias Agopian3094df32012-06-18 18:06:45 -0700688 }
689
690 { // scope for the lock
691 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700692 if (surface == 0) {
693 // release our current display. we're guarantee to have
694 // a reference to it (token), while we hold the lock
695 mExtDisplayToken = 0;
696 } else {
697 mExtDisplayToken = token;
698 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700699
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700700 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
701 info.surface = surface;
702 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700703 }
704}
705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800707
708void SurfaceFlinger::waitForEvent() {
709 mEventQueue.waitMessage();
710}
711
712void SurfaceFlinger::signalTransaction() {
713 mEventQueue.invalidate();
714}
715
716void SurfaceFlinger::signalLayerUpdate() {
717 mEventQueue.invalidate();
718}
719
720void SurfaceFlinger::signalRefresh() {
721 mEventQueue.refresh();
722}
723
724status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
725 nsecs_t reltime, uint32_t flags) {
726 return mEventQueue.postMessage(msg, reltime);
727}
728
729status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
730 nsecs_t reltime, uint32_t flags) {
731 status_t res = mEventQueue.postMessage(msg, reltime);
732 if (res == NO_ERROR) {
733 msg->wait();
734 }
735 return res;
736}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737
Mathias Agopian4fec8732012-06-29 14:12:52 -0700738bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 return true;
741}
742
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700743void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700744 if (mEventThread == NULL) {
745 // This is a temporary workaround for b/7145521. A non-null pointer
746 // does not mean EventThread has finished initializing, so this
747 // is not a correct fix.
748 ALOGW("WARNING: EventThread not started, ignoring vsync");
749 return;
750 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700751 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
752 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700753 mEventThread->onVSyncReceived(type, timestamp);
754 }
755}
756
757void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
758 if (mEventThread == NULL) {
759 // This is a temporary workaround for b/7145521. A non-null pointer
760 // does not mean EventThread has finished initializing, so this
761 // is not a correct fix.
762 ALOGW("WARNING: EventThread not started, ignoring hotplug");
763 return;
764 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700765
Mathias Agopian148994e2012-09-19 17:31:36 -0700766 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700767 Mutex::Autolock _l(mStateLock);
768 if (connected == false) {
769 mCurrentState.displays.removeItem(mDefaultDisplays[type]);
770 } else {
771 DisplayDeviceState info((DisplayDevice::DisplayType)type);
772 mCurrentState.displays.add(mDefaultDisplays[type], info);
773 }
774 setTransactionFlags(eDisplayTransactionNeeded);
775
Andy McFadden9e9689c2012-10-10 18:17:51 -0700776 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700777 }
Mathias Agopian86303202012-07-24 22:46:10 -0700778}
779
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700780void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
781 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700782}
783
Mathias Agopian4fec8732012-06-29 14:12:52 -0700784void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800785 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800786 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700787 case MessageQueue::INVALIDATE:
788 handleMessageTransaction();
789 handleMessageInvalidate();
790 signalRefresh();
791 break;
792 case MessageQueue::REFRESH:
793 handleMessageRefresh();
794 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800795 }
796}
797
Mathias Agopian4fec8732012-06-29 14:12:52 -0700798void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700799 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700800 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700801 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700802 }
803}
804
805void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700806 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700807 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700808}
809
810void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 ATRACE_CALL();
812 preComposition();
813 rebuildLayerStacks();
814 setUpHWComposer();
815 doDebugFlashRegions();
816 doComposition();
817 postComposition();
818}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700819
Mathias Agopiancd60f992012-08-16 16:28:27 -0700820void SurfaceFlinger::doDebugFlashRegions()
821{
822 // is debugging enabled
823 if (CC_LIKELY(!mDebugRegion))
824 return;
825
826 const bool repaintEverything = mRepaintEverything;
827 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
828 const sp<DisplayDevice>& hw(mDisplays[dpy]);
829 if (hw->canDraw()) {
830 // transform the dirty region into this screen's coordinate space
831 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
832 if (!dirtyRegion.isEmpty()) {
833 // redraw the whole screen
834 doComposeSurfaces(hw, Region(hw->bounds()));
835
836 // and draw the dirty region
837 glDisable(GL_TEXTURE_EXTERNAL_OES);
838 glDisable(GL_TEXTURE_2D);
839 glDisable(GL_BLEND);
840 glColor4f(1, 0, 1, 1);
841 const int32_t height = hw->getHeight();
842 Region::const_iterator it = dirtyRegion.begin();
843 Region::const_iterator const end = dirtyRegion.end();
844 while (it != end) {
845 const Rect& r = *it++;
846 GLfloat vertices[][2] = {
847 { r.left, height - r.top },
848 { r.left, height - r.bottom },
849 { r.right, height - r.bottom },
850 { r.right, height - r.top }
851 };
852 glVertexPointer(2, GL_FLOAT, 0, vertices);
853 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
854 }
855 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700856 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857 }
858 }
859 }
860
861 postFramebuffer();
862
863 if (mDebugRegion > 1) {
864 usleep(mDebugRegion * 1000);
865 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700866
867 HWComposer& hwc(getHwComposer());
868 if (hwc.initCheck() == NO_ERROR) {
869 status_t err = hwc.prepare();
870 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
871 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700872}
873
874void SurfaceFlinger::preComposition()
875{
876 bool needExtraInvalidate = false;
877 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
878 const size_t count = currentLayers.size();
879 for (size_t i=0 ; i<count ; i++) {
880 if (currentLayers[i]->onPreComposition()) {
881 needExtraInvalidate = true;
882 }
883 }
884 if (needExtraInvalidate) {
885 signalLayerUpdate();
886 }
887}
888
889void SurfaceFlinger::postComposition()
890{
891 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
892 const size_t count = currentLayers.size();
893 for (size_t i=0 ; i<count ; i++) {
894 currentLayers[i]->onPostComposition();
895 }
896}
897
898void SurfaceFlinger::rebuildLayerStacks() {
899 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700900 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700901 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700902 mVisibleRegionsDirty = false;
903 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700904
Mathias Agopian87baae12012-07-31 12:38:26 -0700905 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700906 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700907 Region opaqueRegion;
908 Region dirtyRegion;
909 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700910 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700911 const Transform& tr(hw->getTransform());
912 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700913 if (hw->canDraw()) {
914 SurfaceFlinger::computeVisibleRegions(currentLayers,
915 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700916
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700917 const size_t count = currentLayers.size();
918 for (size_t i=0 ; i<count ; i++) {
919 const sp<LayerBase>& layer(currentLayers[i]);
920 const Layer::State& s(layer->drawingState());
921 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700922 Region drawRegion(tr.transform(
923 layer->visibleNonTransparentRegion));
924 drawRegion.andSelf(bounds);
925 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700926 layersSortedByZ.add(layer);
927 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700928 }
929 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700930 }
Mathias Agopian42977342012-08-05 00:40:46 -0700931 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700932 hw->undefinedRegion.set(bounds);
933 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
934 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700935 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700936 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700937}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700938
Mathias Agopiancd60f992012-08-16 16:28:27 -0700939void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700940 HWComposer& hwc(getHwComposer());
941 if (hwc.initCheck() == NO_ERROR) {
942 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700943 if (CC_UNLIKELY(mHwWorkListDirty)) {
944 mHwWorkListDirty = false;
945 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
946 sp<const DisplayDevice> hw(mDisplays[dpy]);
947 const int32_t id = hw->getHwcDisplayId();
948 if (id >= 0) {
949 const Vector< sp<LayerBase> >& currentLayers(
950 hw->getVisibleLayersSortedByZ());
951 const size_t count = currentLayers.size();
952 if (hwc.createWorkList(id, count) == NO_ERROR) {
953 HWComposer::LayerListIterator cur = hwc.begin(id);
954 const HWComposer::LayerListIterator end = hwc.end(id);
955 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
956 const sp<LayerBase>& layer(currentLayers[i]);
957 layer->setGeometry(hw, *cur);
958 if (mDebugDisableHWC || mDebugRegion) {
959 cur->setSkip(true);
960 }
961 }
962 }
963 }
964 }
965 }
966
967 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700968 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700969 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700970 const int32_t id = hw->getHwcDisplayId();
971 if (id >= 0) {
972 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700974 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700975 HWComposer::LayerListIterator cur = hwc.begin(id);
976 const HWComposer::LayerListIterator end = hwc.end(id);
977 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
978 /*
979 * update the per-frame h/w composer data for each layer
980 * and build the transparent region of the FB
981 */
982 const sp<LayerBase>& layer(currentLayers[i]);
983 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700984 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700986 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700987
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988 status_t err = hwc.prepare();
989 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
990 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700991}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700992
Mathias Agopiancd60f992012-08-16 16:28:27 -0700993void SurfaceFlinger::doComposition() {
994 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700995 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700996 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700997 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700998 if (hw->canDraw()) {
999 // transform the dirty region into this screen's coordinate space
1000 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
1001 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001002 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -07001003 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001004 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001005 hw->dirtyRegion.clear();
1006 hw->flip(hw->swapRegion);
1007 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001008 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001009 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001010 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001011 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001012 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001013}
1014
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015void SurfaceFlinger::postFramebuffer()
1016{
Mathias Agopian841cde52012-03-01 15:44:37 -08001017 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001018
Mathias Agopiana44b0412011-10-16 18:46:35 -07001019 const nsecs_t now = systemTime();
1020 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001021
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001022 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001023 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001024 if (!hwc.supportsFramebufferTarget()) {
1025 // EGL spec says:
1026 // "surface must be bound to the calling thread's current context,
1027 // for the current rendering API."
1028 DisplayDevice::makeCurrent(mEGLDisplay,
1029 getDefaultDisplayDevice(), mEGLContext);
1030 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001031 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001032 }
1033
Mathias Agopian92a979a2012-08-02 18:32:23 -07001034 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001035 sp<const DisplayDevice> hw(mDisplays[dpy]);
1036 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001037 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001038 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001039 int32_t id = hw->getHwcDisplayId();
1040 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001041 HWComposer::LayerListIterator cur = hwc.begin(id);
1042 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001043 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001044 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001045 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001046 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001047 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001048 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001049 }
Jesse Hallef194142012-06-14 14:45:17 -07001050 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001051 }
1052
Mathias Agopiana44b0412011-10-16 18:46:35 -07001053 mLastSwapBufferTime = systemTime() - now;
1054 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055}
1056
Mathias Agopian87baae12012-07-31 12:38:26 -07001057void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
Mathias Agopian841cde52012-03-01 15:44:37 -08001059 ATRACE_CALL();
1060
Mathias Agopianca4d3602011-05-19 15:38:14 -07001061 Mutex::Autolock _l(mStateLock);
1062 const nsecs_t now = systemTime();
1063 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
Mathias Agopianca4d3602011-05-19 15:38:14 -07001065 // Here we're guaranteed that some transaction flags are set
1066 // so we can call handleTransactionLocked() unconditionally.
1067 // We call getTransactionFlags(), which will also clear the flags,
1068 // with mStateLock held to guarantee that mCurrentState won't change
1069 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001070
Mathias Agopiane57f2922012-08-09 16:29:12 -07001071 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001072 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001073
Mathias Agopianca4d3602011-05-19 15:38:14 -07001074 mLastTransactionTime = systemTime() - now;
1075 mDebugInTransaction = 0;
1076 invalidateHwcGeometry();
1077 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001078}
1079
Mathias Agopian87baae12012-07-31 12:38:26 -07001080void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001081{
1082 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 const size_t count = currentLayers.size();
1084
1085 /*
1086 * Traversal of the children
1087 * (perform the transaction for each of them if needed)
1088 */
1089
Mathias Agopian3559b072012-08-15 13:46:03 -07001090 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001092 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1094 if (!trFlags) continue;
1095
1096 const uint32_t flags = layer->doTransaction(0);
1097 if (flags & Layer::eVisibleRegion)
1098 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 }
1100 }
1101
1102 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001103 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 */
1105
Mathias Agopiane57f2922012-08-09 16:29:12 -07001106 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001107 // here we take advantage of Vector's copy-on-write semantics to
1108 // improve performance by skipping the transaction entirely when
1109 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001110 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1111 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001112 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001114 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001115 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001116
1117 // find the displays that were removed
1118 // (ie: in drawing state but not in current state)
1119 // also handle displays that changed
1120 // (ie: displays that are in both lists)
1121 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001122 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1123 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001124 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001125 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001126 // Call makeCurrent() on the primary display so we can
1127 // be sure that nothing associated with this display
1128 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001129 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001130 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001131 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001132 getHwComposer().disconnectDisplay(draw[i].type);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001133 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001134 } else {
1135 ALOGW("trying to remove the main display");
1136 }
1137 } else {
1138 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001139 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001140 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001141 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001142 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001143 // recreating the DisplayDevice, so we just remove it
1144 // from the drawing state, so that it get re-added
1145 // below.
1146 mDisplays.removeItem(display);
1147 mDrawingState.displays.removeItemsAt(i);
1148 dc--; i--;
1149 // at this point we must loop to the next item
1150 continue;
1151 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001152
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001153 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001154 if (disp != NULL) {
1155 if (state.layerStack != draw[i].layerStack) {
1156 disp->setLayerStack(state.layerStack);
1157 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001158 if ((state.orientation != draw[i].orientation)
1159 || (state.viewport != draw[i].viewport)
1160 || (state.frame != draw[i].frame))
1161 {
1162 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001163 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001164 }
Andy McFadden69052052012-09-14 16:10:11 -07001165
1166 // Walk through all the layers in currentLayers,
1167 // and update their transform hint.
1168 //
1169 // TODO: we could be much more clever about which
1170 // layers we touch and how often we do these updates
1171 // (e.g. only touch the layers associated with this
1172 // display, and only on a rotation).
1173 for (size_t i = 0; i < count; i++) {
1174 const sp<LayerBase>& layerBase = currentLayers[i];
1175 layerBase->updateTransformHint();
1176 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001177 }
1178 }
1179 }
1180
1181 // find displays that were added
1182 // (ie: in current state but not in drawing state)
1183 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001184 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001185 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001186
1187 sp<FramebufferSurface> fbs;
1188 sp<SurfaceTextureClient> stc;
1189 if (!state.isVirtualDisplay()) {
1190
1191 ALOGE_IF(state.surface!=NULL,
1192 "adding a supported display, but rendering "
1193 "surface is provided (%p), ignoring it",
1194 state.surface.get());
1195
1196 // for supported (by hwc) displays we provide our
1197 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001198 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001199 stc = new SurfaceTextureClient(
1200 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1201 } else {
1202 if (state.surface != NULL) {
1203 stc = new SurfaceTextureClient(state.surface);
1204 }
1205 }
1206
1207 const wp<IBinder>& display(curr.keyAt(i));
1208 if (stc != NULL) {
1209 sp<DisplayDevice> hw = new DisplayDevice(this,
1210 state.type, display, stc, fbs, mEGLConfig);
1211 hw->setLayerStack(state.layerStack);
1212 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001213 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001214 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001215 mDisplays.add(display, hw);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001216 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001217 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001218 }
1219 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001221 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222
Mathias Agopian3559b072012-08-15 13:46:03 -07001223 /*
1224 * Perform our own transaction if needed
1225 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001226
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1228 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001229 // layers have been added
1230 mVisibleRegionsDirty = true;
1231 }
1232
1233 // some layers might have been removed, so
1234 // we need to update the regions they're exposing.
1235 if (mLayersRemoved) {
1236 mLayersRemoved = false;
1237 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001238 const size_t count = previousLayers.size();
1239 for (size_t i=0 ; i<count ; i++) {
1240 const sp<LayerBase>& layer(previousLayers[i]);
1241 if (currentLayers.indexOf(layer) < 0) {
1242 // this layer is not visible anymore
1243 // TODO: we could traverse the tree from front to back and
1244 // compute the actual visible region
1245 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001246 const Layer::State& s(layer->drawingState());
1247 Region visibleReg = s.transform.transform(
1248 Region(Rect(s.active.w, s.active.h)));
1249 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001250 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 }
1253
1254 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001255}
1256
1257void SurfaceFlinger::commitTransaction()
1258{
1259 if (!mLayersPendingRemoval.isEmpty()) {
1260 // Notify removed layers now that they can't be drawn from
1261 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1262 mLayersPendingRemoval[i]->onRemoved();
1263 }
1264 mLayersPendingRemoval.clear();
1265 }
1266
1267 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001268 mTransactionPending = false;
1269 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001270 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271}
1272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001274 const LayerVector& currentLayers, uint32_t layerStack,
1275 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276{
Mathias Agopian841cde52012-03-01 15:44:37 -08001277 ATRACE_CALL();
1278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 Region aboveOpaqueLayers;
1280 Region aboveCoveredLayers;
1281 Region dirty;
1282
Mathias Agopian87baae12012-07-31 12:38:26 -07001283 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284
1285 size_t i = currentLayers.size();
1286 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288
1289 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001290 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291
Mathias Agopian87baae12012-07-31 12:38:26 -07001292 // only consider the layers on the given later stack
1293 if (s.layerStack != layerStack)
1294 continue;
1295
Mathias Agopianab028732010-03-16 16:41:46 -07001296 /*
1297 * opaqueRegion: area of a surface that is fully opaque.
1298 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001300
1301 /*
1302 * visibleRegion: area of a surface that is visible on screen
1303 * and not fully transparent. This is essentially the layer's
1304 * footprint minus the opaque regions above it.
1305 * Areas covered by a translucent surface are considered visible.
1306 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001308
1309 /*
1310 * coveredRegion: area of a surface that is covered by all
1311 * visible regions above it (which includes the translucent areas).
1312 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001314
Jesse Halla8026d22012-09-25 13:25:04 -07001315 /*
1316 * transparentRegion: area of a surface that is hinted to be completely
1317 * transparent. This is only used to tell when the layer has no visible
1318 * non-transparent regions and can be removed from the layer list. It
1319 * does not affect the visibleRegion of this layer or any layers
1320 * beneath it. The hint may not be correct if apps don't respect the
1321 * SurfaceView restrictions (which, sadly, some don't).
1322 */
1323 Region transparentRegion;
1324
Mathias Agopianab028732010-03-16 16:41:46 -07001325
1326 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001327 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001328 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001329 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001331 if (!visibleRegion.isEmpty()) {
1332 // Remove the transparent area from the visible region
1333 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001334 const Transform tr(s.transform);
1335 if (tr.transformed()) {
1336 if (tr.preserveRects()) {
1337 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001338 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001339 } else {
1340 // transformation too complex, can't do the
1341 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001342 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001343 }
1344 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001345 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001346 }
Mathias Agopianab028732010-03-16 16:41:46 -07001347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348
Mathias Agopianab028732010-03-16 16:41:46 -07001349 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001351 if (s.alpha==255 && !translucent &&
1352 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1353 // the opaque region is the layer's footprint
1354 opaqueRegion = visibleRegion;
1355 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356 }
1357 }
1358
Mathias Agopianab028732010-03-16 16:41:46 -07001359 // Clip the covered region to the visible region
1360 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1361
1362 // Update aboveCoveredLayers for next (lower) layer
1363 aboveCoveredLayers.orSelf(visibleRegion);
1364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 // subtract the opaque region covered by the layers above us
1366 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367
1368 // compute this layer's dirty region
1369 if (layer->contentDirty) {
1370 // we need to invalidate the whole region
1371 dirty = visibleRegion;
1372 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001373 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 layer->contentDirty = false;
1375 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001376 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001377 * the exposed region consists of two components:
1378 * 1) what's VISIBLE now and was COVERED before
1379 * 2) what's EXPOSED now less what was EXPOSED before
1380 *
1381 * note that (1) is conservative, we start with the whole
1382 * visible region but only keep what used to be covered by
1383 * something -- which mean it may have been exposed.
1384 *
1385 * (2) handles areas that were not covered by anything but got
1386 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001387 */
Mathias Agopianab028732010-03-16 16:41:46 -07001388 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001389 const Region oldVisibleRegion = layer->visibleRegion;
1390 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001391 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1392 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 }
1394 dirty.subtractSelf(aboveOpaqueLayers);
1395
1396 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001397 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398
Mathias Agopianab028732010-03-16 16:41:46 -07001399 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001401
Jesse Halla8026d22012-09-25 13:25:04 -07001402 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403 layer->setVisibleRegion(visibleRegion);
1404 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001405 layer->setVisibleNonTransparentRegion(
1406 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 }
1408
Mathias Agopian87baae12012-07-31 12:38:26 -07001409 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410}
1411
Mathias Agopian87baae12012-07-31 12:38:26 -07001412void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1413 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001414 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001415 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1416 if (hw->getLayerStack() == layerStack) {
1417 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001418 }
1419 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001420}
1421
1422void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001424 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425
Mathias Agopian4fec8732012-06-29 14:12:52 -07001426 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001427 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001428 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001429 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001430 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001431 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001432 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001433 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001434 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001435
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001436 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437}
1438
Mathias Agopianad456f92011-01-13 17:53:01 -08001439void SurfaceFlinger::invalidateHwcGeometry()
1440{
1441 mHwWorkListDirty = true;
1442}
1443
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001444
Mathias Agopiancd60f992012-08-16 16:28:27 -07001445void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001446 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447{
Mathias Agopian87baae12012-07-31 12:38:26 -07001448 Region dirtyRegion(inDirtyRegion);
1449
Mathias Agopianb8a55602009-06-26 19:06:36 -07001450 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001451 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452
Mathias Agopian42977342012-08-05 00:40:46 -07001453 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001454 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001455 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1456 // takes a rectangle, we must make sure to update that whole
1457 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001458 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001459 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001460 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001461 // We need to redraw the rectangle that will be updated
1462 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001463 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001464 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001465 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001466 } else {
1467 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001468 dirtyRegion.set(hw->bounds());
1469 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 }
1471 }
1472
Mathias Agopiancd60f992012-08-16 16:28:27 -07001473 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001475 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001476 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001477
1478 // swap buffers (presentation)
1479 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480}
1481
Mathias Agopiancd60f992012-08-16 16:28:27 -07001482void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001483{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001484 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001485 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001486 HWComposer::LayerListIterator cur = hwc.begin(id);
1487 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001488
Mathias Agopian85d751c2012-08-29 16:59:24 -07001489 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1490 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001491 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001492
1493 // set the frame buffer
1494 glMatrixMode(GL_MODELVIEW);
1495 glLoadIdentity();
1496
1497 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001498 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001499 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001500 // when using overlays, we assume a fully transparent framebuffer
1501 // NOTE: we could reduce how much we need to clear, for instance
1502 // remove where there are opaque FB layers. however, on some
1503 // GPUs doing a "clean slate" glClear might be more efficient.
1504 // We'll revisit later if needed.
1505 glClearColor(0, 0, 0, 0);
1506 glClear(GL_COLOR_BUFFER_BIT);
1507 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001508 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001509 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001510 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001511 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001512 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001513 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001514 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001515 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001516
Mathias Agopian85d751c2012-08-29 16:59:24 -07001517 /*
1518 * and then, render the layers targeted at the framebuffer
1519 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001520
Mathias Agopian85d751c2012-08-29 16:59:24 -07001521 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1522 const size_t count = layers.size();
1523 const Transform& tr = hw->getTransform();
1524 if (cur != end) {
1525 // we're using h/w composer
1526 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001527 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001528 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001529 if (!clip.isEmpty()) {
1530 switch (cur->getCompositionType()) {
1531 case HWC_OVERLAY: {
1532 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1533 && i
1534 && layer->isOpaque()
1535 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001536 // never clear the very first layer since we're
1537 // guaranteed the FB is already cleared
1538 layer->clearWithOpenGL(hw, clip);
1539 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001540 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001541 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001542 case HWC_FRAMEBUFFER: {
1543 layer->draw(hw, clip);
1544 break;
1545 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001546 case HWC_FRAMEBUFFER_TARGET: {
1547 // this should not happen as the iterator shouldn't
1548 // let us get there.
1549 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1550 break;
1551 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001552 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001553 }
1554 layer->setAcquireFence(hw, *cur);
1555 }
1556 } else {
1557 // we're not using h/w composer
1558 for (size_t i=0 ; i<count ; ++i) {
1559 const sp<LayerBase>& layer(layers[i]);
1560 const Region clip(dirty.intersect(
1561 tr.transform(layer->visibleRegion)));
1562 if (!clip.isEmpty()) {
1563 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001564 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001565 }
1566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567}
1568
Mathias Agopian55801e42012-08-27 18:54:24 -07001569void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1570 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001572 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001573 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001574 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001575 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001576
Mathias Agopian55801e42012-08-27 18:54:24 -07001577 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001578 Region::const_iterator it = region.begin();
1579 Region::const_iterator const end = region.end();
1580 while (it != end) {
1581 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001582 GLfloat vertices[][2] = {
1583 { r.left, height - r.top },
1584 { r.left, height - r.bottom },
1585 { r.right, height - r.bottom },
1586 { r.right, height - r.top }
1587 };
1588 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001589 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1590 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591}
1592
Mathias Agopian96f08192010-06-02 23:28:45 -07001593ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1594 const sp<LayerBaseClient>& lbc)
1595{
Mathias Agopian96f08192010-06-02 23:28:45 -07001596 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001597 size_t name = client->attachLayer(lbc);
1598
Mathias Agopian96f08192010-06-02 23:28:45 -07001599 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001600 Mutex::Autolock _l(mStateLock);
1601 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001602
Mathias Agopian4f113742011-05-03 16:21:41 -07001603 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001604}
1605
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001606status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607{
1608 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001609 status_t err = purgatorizeLayer_l(layer);
1610 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001611 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001612 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613}
1614
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001615status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616{
1617 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1618 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001619 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 return NO_ERROR;
1621 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001622 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623}
1624
Mathias Agopian9a112062009-04-17 19:36:26 -07001625status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1626{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001627 // First add the layer to the purgatory list, which makes sure it won't
1628 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001629 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001630 if (err >= 0) {
1631 mLayerPurgatory.add(layerBase);
1632 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001633
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001634 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001635
Mathias Agopian3d579642009-06-04 18:46:21 -07001636 // it's possible that we don't find a layer, because it might
1637 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001638 // from the user because there is a race between Client::destroySurface(),
1639 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001640 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1641}
1642
Mathias Agopiandea20b12011-05-03 17:04:02 -07001643uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1644{
1645 return android_atomic_release_load(&mTransactionFlags);
1646}
1647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1649{
1650 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1651}
1652
Mathias Agopianbb641242010-05-18 17:06:55 -07001653uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654{
1655 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1656 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001657 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 }
1659 return old;
1660}
1661
Mathias Agopian8b33f032012-07-24 20:43:54 -07001662void SurfaceFlinger::setTransactionState(
1663 const Vector<ComposerState>& state,
1664 const Vector<DisplayState>& displays,
1665 uint32_t flags)
1666{
Mathias Agopian698c0872011-06-28 19:09:31 -07001667 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001668 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001669
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001670 if (flags & eAnimation) {
1671 // For window updates that are part of an animation we must wait for
1672 // previous animation "frames" to be handled.
1673 while (mAnimTransactionPending) {
1674 status_t err = mTransactionCV.waitRelative(mStateLock, 500 * 1000);
1675 if (CC_UNLIKELY(err != NO_ERROR)) {
1676 // just in case something goes wrong in SF, return to the
1677 // caller after a few hundred microseconds.
1678 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1679 mAnimTransactionPending = false;
1680 break;
1681 }
1682 }
1683 }
1684
Mathias Agopiane57f2922012-08-09 16:29:12 -07001685 size_t count = displays.size();
1686 for (size_t i=0 ; i<count ; i++) {
1687 const DisplayState& s(displays[i]);
1688 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001689 }
1690
Mathias Agopiane57f2922012-08-09 16:29:12 -07001691 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001692 for (size_t i=0 ; i<count ; i++) {
1693 const ComposerState& s(state[i]);
1694 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001695 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001696 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001697
Mathias Agopian386aa982011-11-07 21:58:03 -08001698 if (transactionFlags) {
1699 // this triggers the transaction
1700 setTransactionFlags(transactionFlags);
1701
1702 // if this is a synchronous transaction, wait for it to take effect
1703 // before returning.
1704 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001705 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001706 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001707 if (flags & eAnimation) {
1708 mAnimTransactionPending = true;
1709 }
1710 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001711 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1712 if (CC_UNLIKELY(err != NO_ERROR)) {
1713 // just in case something goes wrong in SF, return to the
1714 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001715 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1716 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001717 break;
1718 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001719 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 }
1721}
1722
Mathias Agopiane57f2922012-08-09 16:29:12 -07001723uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1724{
Jesse Hall9a143922012-10-04 16:29:19 -07001725 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1726 if (dpyIdx < 0)
1727 return 0;
1728
Mathias Agopiane57f2922012-08-09 16:29:12 -07001729 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001730 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001731 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001732 const uint32_t what = s.what;
1733 if (what & DisplayState::eSurfaceChanged) {
1734 if (disp.surface->asBinder() != s.surface->asBinder()) {
1735 disp.surface = s.surface;
1736 flags |= eDisplayTransactionNeeded;
1737 }
1738 }
1739 if (what & DisplayState::eLayerStackChanged) {
1740 if (disp.layerStack != s.layerStack) {
1741 disp.layerStack = s.layerStack;
1742 flags |= eDisplayTransactionNeeded;
1743 }
1744 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001745 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001746 if (disp.orientation != s.orientation) {
1747 disp.orientation = s.orientation;
1748 flags |= eDisplayTransactionNeeded;
1749 }
1750 if (disp.frame != s.frame) {
1751 disp.frame = s.frame;
1752 flags |= eDisplayTransactionNeeded;
1753 }
1754 if (disp.viewport != s.viewport) {
1755 disp.viewport = s.viewport;
1756 flags |= eDisplayTransactionNeeded;
1757 }
1758 }
1759 }
1760 return flags;
1761}
1762
1763uint32_t SurfaceFlinger::setClientStateLocked(
1764 const sp<Client>& client,
1765 const layer_state_t& s)
1766{
1767 uint32_t flags = 0;
1768 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1769 if (layer != 0) {
1770 const uint32_t what = s.what;
1771 if (what & layer_state_t::ePositionChanged) {
1772 if (layer->setPosition(s.x, s.y))
1773 flags |= eTraversalNeeded;
1774 }
1775 if (what & layer_state_t::eLayerChanged) {
1776 // NOTE: index needs to be calculated before we update the state
1777 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1778 if (layer->setLayer(s.z)) {
1779 mCurrentState.layersSortedByZ.removeAt(idx);
1780 mCurrentState.layersSortedByZ.add(layer);
1781 // we need traversal (state changed)
1782 // AND transaction (list changed)
1783 flags |= eTransactionNeeded|eTraversalNeeded;
1784 }
1785 }
1786 if (what & layer_state_t::eSizeChanged) {
1787 if (layer->setSize(s.w, s.h)) {
1788 flags |= eTraversalNeeded;
1789 }
1790 }
1791 if (what & layer_state_t::eAlphaChanged) {
1792 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1793 flags |= eTraversalNeeded;
1794 }
1795 if (what & layer_state_t::eMatrixChanged) {
1796 if (layer->setMatrix(s.matrix))
1797 flags |= eTraversalNeeded;
1798 }
1799 if (what & layer_state_t::eTransparentRegionChanged) {
1800 if (layer->setTransparentRegionHint(s.transparentRegion))
1801 flags |= eTraversalNeeded;
1802 }
1803 if (what & layer_state_t::eVisibilityChanged) {
1804 if (layer->setFlags(s.flags, s.mask))
1805 flags |= eTraversalNeeded;
1806 }
1807 if (what & layer_state_t::eCropChanged) {
1808 if (layer->setCrop(s.crop))
1809 flags |= eTraversalNeeded;
1810 }
1811 if (what & layer_state_t::eLayerStackChanged) {
1812 // NOTE: index needs to be calculated before we update the state
1813 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1814 if (layer->setLayerStack(s.layerStack)) {
1815 mCurrentState.layersSortedByZ.removeAt(idx);
1816 mCurrentState.layersSortedByZ.add(layer);
1817 // we need traversal (state changed)
1818 // AND transaction (list changed)
1819 flags |= eTransactionNeeded|eTraversalNeeded;
1820 }
1821 }
1822 }
1823 return flags;
1824}
1825
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001826sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001827 ISurfaceComposerClient::surface_data_t* params,
1828 const String8& name,
1829 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001830 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831 uint32_t flags)
1832{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001833 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001834 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001835
1836 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001837 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001838 int(w), int(h));
1839 return surfaceHandle;
1840 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001841
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001842 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001843 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1844 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001845 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001847 case ISurfaceComposerClient::eFXSurfaceBlur:
1848 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001849 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001850 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001851 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001852 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001853 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854 }
1855
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001856 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001857 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001858 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001859 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001861 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001862 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001863 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001864 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001865 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 }
1867
1868 return surfaceHandle;
1869}
1870
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001871sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001872 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001873 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001874 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875{
1876 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001877 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878 case PIXEL_FORMAT_TRANSPARENT:
1879 case PIXEL_FORMAT_TRANSLUCENT:
1880 format = PIXEL_FORMAT_RGBA_8888;
1881 break;
1882 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001883#ifdef NO_RGBX_8888
1884 format = PIXEL_FORMAT_RGB_565;
1885#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001886 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001887#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 break;
1889 }
1890
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001891#ifdef NO_RGBX_8888
1892 if (format == PIXEL_FORMAT_RGBX_8888)
1893 format = PIXEL_FORMAT_RGBA_8888;
1894#endif
1895
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001896 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001897 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001898 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001899 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001900 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001901 }
1902 return layer;
1903}
1904
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001905sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001906 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001907 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001909 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001910 return layer;
1911}
1912
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001913sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001914 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001915 uint32_t w, uint32_t h, uint32_t flags)
1916{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001917 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 return layer;
1919}
1920
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001921status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922{
Mathias Agopian9a112062009-04-17 19:36:26 -07001923 /*
1924 * called by the window manager, when a surface should be marked for
1925 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001926 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001927 * The surface is removed from the current and drawing lists, but placed
1928 * in the purgatory queue, so it's not destroyed right-away (we need
1929 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001930 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001931
Mathias Agopian48d819a2009-09-10 19:41:18 -07001932 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001933 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001934 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001935
Mathias Agopian48d819a2009-09-10 19:41:18 -07001936 if (layer != 0) {
1937 err = purgatorizeLayer_l(layer);
1938 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001939 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001940 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001941 }
1942 return err;
1943}
1944
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001945status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001946{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001947 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001948 status_t err = NO_ERROR;
1949 sp<LayerBaseClient> l(layer.promote());
1950 if (l != NULL) {
1951 Mutex::Autolock _l(mStateLock);
1952 err = removeLayer_l(l);
1953 if (err == NAME_NOT_FOUND) {
1954 // The surface wasn't in the current list, which means it was
1955 // removed already, which means it is in the purgatory,
1956 // and need to be removed from there.
1957 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001958 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001959 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001960 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001961 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001962 "error removing layer=%p (%s)", l.get(), strerror(-err));
1963 }
1964 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965}
1966
Mathias Agopianb60314a2012-04-10 22:09:54 -07001967// ---------------------------------------------------------------------------
1968
Andy McFadden13a082e2012-08-24 10:16:42 -07001969void SurfaceFlinger::onInitializeDisplays() {
1970 // reset screen orientation
1971 Vector<ComposerState> state;
1972 Vector<DisplayState> displays;
1973 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001974 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001975 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001976 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001977 d.frame.makeInvalid();
1978 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001979 displays.add(d);
1980 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001981 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001982}
1983
1984void SurfaceFlinger::initializeDisplays() {
1985 class MessageScreenInitialized : public MessageBase {
1986 SurfaceFlinger* flinger;
1987 public:
1988 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1989 virtual bool handler() {
1990 flinger->onInitializeDisplays();
1991 return true;
1992 }
1993 };
1994 sp<MessageBase> msg = new MessageScreenInitialized(this);
1995 postMessageAsync(msg); // we may be called from main thread, use async message
1996}
1997
1998
Mathias Agopiancde87a32012-09-13 14:09:01 -07001999void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002000 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2001 if (hw->isScreenAcquired()) {
2002 // this is expected, e.g. when power manager wakes up during boot
2003 ALOGD(" screen was previously acquired");
2004 return;
2005 }
2006
Mathias Agopian42977342012-08-05 00:40:46 -07002007 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002008 int32_t type = hw->getDisplayType();
2009 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2010 // built-in display, tell the HWC
2011 getHwComposer().acquire(type);
2012
2013 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2014 // FIXME: eventthread only knows about the main display right now
2015 mEventThread->onScreenAcquired();
2016 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002017 }
Mathias Agopian20128302012-08-13 18:32:13 -07002018 mVisibleRegionsDirty = true;
2019 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002020}
2021
Mathias Agopiancde87a32012-09-13 14:09:01 -07002022void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002023 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2024 if (!hw->isScreenAcquired()) {
2025 ALOGD(" screen was previously released");
2026 return;
2027 }
2028
2029 hw->releaseScreen();
2030 int32_t type = hw->getDisplayType();
2031 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2032 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002033 // FIXME: eventthread only knows about the main display right now
2034 mEventThread->onScreenReleased();
2035 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002036
2037 // built-in display, tell the HWC
2038 getHwComposer().release(type);
2039 }
2040 mVisibleRegionsDirty = true;
2041 // from this point on, SF will stop drawing on this display
2042}
2043
2044void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2045 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002046 SurfaceFlinger& mFlinger;
2047 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002048 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002049 MessageScreenAcquired(SurfaceFlinger& flinger,
2050 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002051 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002052 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2053 if (hw == NULL) {
2054 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2055 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2056 ALOGW("Attempt to unblank virtual display");
2057 } else {
2058 mFlinger.onScreenAcquired(hw);
2059 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002060 return true;
2061 }
2062 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002063 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2064 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002065}
2066
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002067void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002068 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002069 SurfaceFlinger& mFlinger;
2070 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002071 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 MessageScreenReleased(SurfaceFlinger& flinger,
2073 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002074 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002075 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2076 if (hw == NULL) {
2077 ALOGE("Attempt to blank null display %p", mDisplay.get());
2078 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2079 ALOGW("Attempt to blank virtual display");
2080 } else {
2081 mFlinger.onScreenReleased(hw);
2082 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002083 return true;
2084 }
2085 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002086 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2087 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002088}
2089
2090// ---------------------------------------------------------------------------
2091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2093{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002094 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 char buffer[SIZE];
2096 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002097
2098 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002099 snprintf(buffer, SIZE, "Permission Denial: "
2100 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2101 IPCThreadState::self()->getCallingPid(),
2102 IPCThreadState::self()->getCallingUid());
2103 result.append(buffer);
2104 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002105 // Try to get the main lock, but don't insist if we can't
2106 // (this would indicate SF is stuck, but we want to be able to
2107 // print something in dumpsys).
2108 int retry = 3;
2109 while (mStateLock.tryLock()<0 && --retry>=0) {
2110 usleep(1000000);
2111 }
2112 const bool locked(retry >= 0);
2113 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002114 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002115 "SurfaceFlinger appears to be unresponsive, "
2116 "dumping anyways (no locks held)\n");
2117 result.append(buffer);
2118 }
2119
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002120 bool dumpAll = true;
2121 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002122 size_t numArgs = args.size();
2123 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002124 if ((index < numArgs) &&
2125 (args[index] == String16("--list"))) {
2126 index++;
2127 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002128 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002129 }
2130
2131 if ((index < numArgs) &&
2132 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002133 index++;
2134 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002135 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002136 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002137
2138 if ((index < numArgs) &&
2139 (args[index] == String16("--latency-clear"))) {
2140 index++;
2141 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002142 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002145
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002146 if (dumpAll) {
2147 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002148 }
2149
Mathias Agopian9795c422009-08-26 16:36:26 -07002150 if (locked) {
2151 mStateLock.unlock();
2152 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002153 }
2154 write(fd, result.string(), result.size());
2155 return NO_ERROR;
2156}
2157
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002158void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2159 String8& result, char* buffer, size_t SIZE) const
2160{
2161 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2162 const size_t count = currentLayers.size();
2163 for (size_t i=0 ; i<count ; i++) {
2164 const sp<LayerBase>& layer(currentLayers[i]);
2165 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2166 result.append(buffer);
2167 }
2168}
2169
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002170void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2171 String8& result, char* buffer, size_t SIZE) const
2172{
2173 String8 name;
2174 if (index < args.size()) {
2175 name = String8(args[index]);
2176 index++;
2177 }
2178
2179 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2180 const size_t count = currentLayers.size();
2181 for (size_t i=0 ; i<count ; i++) {
2182 const sp<LayerBase>& layer(currentLayers[i]);
2183 if (name.isEmpty()) {
2184 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2185 result.append(buffer);
2186 }
2187 if (name.isEmpty() || (name == layer->getName())) {
2188 layer->dumpStats(result, buffer, SIZE);
2189 }
2190 }
2191}
2192
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002193void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
2194 String8& result, char* buffer, size_t SIZE) const
2195{
2196 String8 name;
2197 if (index < args.size()) {
2198 name = String8(args[index]);
2199 index++;
2200 }
2201
2202 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2203 const size_t count = currentLayers.size();
2204 for (size_t i=0 ; i<count ; i++) {
2205 const sp<LayerBase>& layer(currentLayers[i]);
2206 if (name.isEmpty() || (name == layer->getName())) {
2207 layer->clearStats();
2208 }
2209 }
2210}
2211
Andy McFadden4803b742012-09-24 19:07:20 -07002212/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2213{
2214 static const char* config =
2215 " [sf"
2216#ifdef NO_RGBX_8888
2217 " NO_RGBX_8888"
2218#endif
2219#ifdef HAS_CONTEXT_PRIORITY
2220 " HAS_CONTEXT_PRIORITY"
2221#endif
2222#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2223 " NEVER_DEFAULT_TO_ASYNC_MODE"
2224#endif
2225#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2226 " TARGET_DISABLE_TRIPLE_BUFFERING"
2227#endif
2228 "]";
2229 result.append(config);
2230}
2231
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002232void SurfaceFlinger::dumpAllLocked(
2233 String8& result, char* buffer, size_t SIZE) const
2234{
2235 // figure out if we're stuck somewhere
2236 const nsecs_t now = systemTime();
2237 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2238 const nsecs_t inTransaction(mDebugInTransaction);
2239 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2240 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2241
2242 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002243 * Dump library configuration.
2244 */
2245 result.append("Build configuration:");
2246 appendSfConfigString(result);
2247 appendUiConfigString(result);
2248 appendGuiConfigString(result);
2249 result.append("\n");
2250
2251 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002252 * Dump the visible layer list
2253 */
2254 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2255 const size_t count = currentLayers.size();
2256 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2257 result.append(buffer);
2258 for (size_t i=0 ; i<count ; i++) {
2259 const sp<LayerBase>& layer(currentLayers[i]);
2260 layer->dump(result, buffer, SIZE);
2261 }
2262
2263 /*
2264 * Dump the layers in the purgatory
2265 */
2266
2267 const size_t purgatorySize = mLayerPurgatory.size();
2268 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2269 result.append(buffer);
2270 for (size_t i=0 ; i<purgatorySize ; i++) {
2271 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2272 layer->shortDump(result, buffer, SIZE);
2273 }
2274
2275 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002276 * Dump Display state
2277 */
2278
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002279 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2280 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002281 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2282 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002283 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002284 }
2285
2286 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002287 * Dump SurfaceFlinger global state
2288 */
2289
2290 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2291 result.append(buffer);
2292
Mathias Agopian888c8222012-08-04 21:10:38 -07002293 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002294 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002295 const GLExtensions& extensions(GLExtensions::getInstance());
2296 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2297 extensions.getVendor(),
2298 extensions.getRenderer(),
2299 extensions.getVersion());
2300 result.append(buffer);
2301
2302 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002303 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002304 result.append(buffer);
2305
2306 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2307 result.append(buffer);
2308
Mathias Agopian42977342012-08-05 00:40:46 -07002309 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002310 snprintf(buffer, SIZE,
2311 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002312 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002313 result.append(buffer);
2314 snprintf(buffer, SIZE,
2315 " last eglSwapBuffers() time: %f us\n"
2316 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002317 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318 " refresh-rate : %f fps\n"
2319 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002320 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002321 mLastSwapBufferTime/1000.0,
2322 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002323 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002324 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2325 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2326 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002327 result.append(buffer);
2328
2329 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2330 inSwapBuffersDuration/1000.0);
2331 result.append(buffer);
2332
2333 snprintf(buffer, SIZE, " transaction time: %f us\n",
2334 inTransactionDuration/1000.0);
2335 result.append(buffer);
2336
2337 /*
2338 * VSYNC state
2339 */
2340 mEventThread->dump(result, buffer, SIZE);
2341
2342 /*
2343 * Dump HWComposer state
2344 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345 snprintf(buffer, SIZE, "h/w composer state:\n");
2346 result.append(buffer);
2347 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2348 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2349 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2350 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002351 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352
2353 /*
2354 * Dump gralloc state
2355 */
2356 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2357 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358}
2359
Mathias Agopiancb558572012-10-04 15:58:54 -07002360const Vector< sp<LayerBase> >&
2361SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002362 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002363 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2364}
2365
Keun young Park63f165f2012-08-31 10:53:36 -07002366bool SurfaceFlinger::startDdmConnection()
2367{
2368 void* libddmconnection_dso =
2369 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2370 if (!libddmconnection_dso) {
2371 return false;
2372 }
2373 void (*DdmConnection_start)(const char* name);
2374 DdmConnection_start =
2375 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2376 if (!DdmConnection_start) {
2377 dlclose(libddmconnection_dso);
2378 return false;
2379 }
2380 (*DdmConnection_start)(getServiceName());
2381 return true;
2382}
2383
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002384status_t SurfaceFlinger::onTransact(
2385 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2386{
2387 switch (code) {
2388 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002389 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002390 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002391 case BLANK:
2392 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002393 {
2394 // codes that require permission check
2395 IPCThreadState* ipc = IPCThreadState::self();
2396 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002397 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002398 if ((uid != AID_GRAPHICS) &&
2399 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002400 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002401 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2402 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002403 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002404 break;
2405 }
2406 case CAPTURE_SCREEN:
2407 {
2408 // codes that require permission check
2409 IPCThreadState* ipc = IPCThreadState::self();
2410 const int pid = ipc->getCallingPid();
2411 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002412 if ((uid != AID_GRAPHICS) &&
2413 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002414 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002415 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2416 return PERMISSION_DENIED;
2417 }
2418 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419 }
2420 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002422 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2423 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002424 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002425 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002426 IPCThreadState* ipc = IPCThreadState::self();
2427 const int pid = ipc->getCallingPid();
2428 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002429 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002430 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431 return PERMISSION_DENIED;
2432 }
2433 int n;
2434 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002435 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002436 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437 return NO_ERROR;
2438 case 1002: // SHOW_UPDATES
2439 n = data.readInt32();
2440 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002441 invalidateHwcGeometry();
2442 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002443 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002445 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002446 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002448 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002449 setTransactionFlags(
2450 eTransactionNeeded|
2451 eDisplayTransactionNeeded|
2452 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002453 return NO_ERROR;
2454 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002455 case 1006:{ // send empty update
2456 signalRefresh();
2457 return NO_ERROR;
2458 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002459 case 1008: // toggle use of hw composer
2460 n = data.readInt32();
2461 mDebugDisableHWC = n ? 1 : 0;
2462 invalidateHwcGeometry();
2463 repaintEverything();
2464 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002465 case 1009: // toggle use of transform hint
2466 n = data.readInt32();
2467 mDebugDisableTransformHint = n ? 1 : 0;
2468 invalidateHwcGeometry();
2469 repaintEverything();
2470 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002471 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002472 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 reply->writeInt32(0);
2474 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002475 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002476 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 return NO_ERROR;
2478 case 1013: {
2479 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002480 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2481 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 }
2483 return NO_ERROR;
2484 }
2485 }
2486 return err;
2487}
2488
Mathias Agopian53331da2011-08-22 21:44:41 -07002489void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002490 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002491 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002492}
2493
Mathias Agopian59119e62010-10-11 12:37:43 -07002494// ---------------------------------------------------------------------------
2495
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002496status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002497 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2498{
2499 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002500 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002501}
2502
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002503status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002504 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002505{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002506 ATRACE_CALL();
2507
Mathias Agopian59119e62010-10-11 12:37:43 -07002508 if (!GLExtensions::getInstance().haveFramebufferObject())
2509 return INVALID_OPERATION;
2510
2511 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002512 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2513 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002514 const uint32_t hw_w = hw->getWidth();
2515 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002516 GLfloat u = 1;
2517 GLfloat v = 1;
2518
2519 // make sure to clear all GL error flags
2520 while ( glGetError() != GL_NO_ERROR ) ;
2521
2522 // create a FBO
2523 GLuint name, tname;
2524 glGenTextures(1, &tname);
2525 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002526 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2527 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002528 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2529 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002530 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002531 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002532 GLint tw = (2 << (31 - clz(hw_w)));
2533 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002534 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2535 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002536 u = GLfloat(hw_w) / tw;
2537 v = GLfloat(hw_h) / th;
2538 }
2539 glGenFramebuffersOES(1, &name);
2540 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002541 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2542 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002543
Mathias Agopianbae92d02012-09-28 01:00:47 -07002544 DisplayDevice::setViewportAndProjection(hw);
2545
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002546 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002547 glDisable(GL_TEXTURE_EXTERNAL_OES);
2548 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002549 glClearColor(0,0,0,1);
2550 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002551 glMatrixMode(GL_MODELVIEW);
2552 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002553 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002554 const size_t count = layers.size();
2555 for (size_t i=0 ; i<count ; ++i) {
2556 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002557 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002558 }
2559
Mathias Agopian42977342012-08-05 00:40:46 -07002560 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002561
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002562 // back to main framebuffer
2563 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002564 glDeleteFramebuffersOES(1, &name);
2565
2566 *textureName = tname;
2567 *uOut = u;
2568 *vOut = v;
2569 return NO_ERROR;
2570}
2571
2572// ---------------------------------------------------------------------------
2573
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002574status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002575 sp<IMemoryHeap>* heap,
2576 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002577 uint32_t sw, uint32_t sh,
2578 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002579{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002580 ATRACE_CALL();
2581
Mathias Agopian74c40c02010-09-29 13:02:36 -07002582 status_t result = PERMISSION_DENIED;
2583
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002584 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002585 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002586 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002587
2588 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002589 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002590 const uint32_t hw_w = hw->getWidth();
2591 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002592
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002593 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002594 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002595 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002596 return PERMISSION_DENIED;
2597 }
2598
2599 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002600 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002601 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002602 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002603
2604 sw = (!sw) ? hw_w : sw;
2605 sh = (!sh) ? hw_h : sh;
2606 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002607 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002608
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002609// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2610// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002611
Mathias Agopian74c40c02010-09-29 13:02:36 -07002612 // make sure to clear all GL error flags
2613 while ( glGetError() != GL_NO_ERROR ) ;
2614
2615 // create a FBO
2616 GLuint name, tname;
2617 glGenRenderbuffersOES(1, &tname);
2618 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2619 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002620
Mathias Agopian74c40c02010-09-29 13:02:36 -07002621 glGenFramebuffersOES(1, &name);
2622 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2623 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2624 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2625
2626 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002627
Mathias Agopian74c40c02010-09-29 13:02:36 -07002628 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2629
2630 // invert everything, b/c glReadPixel() below will invert the FB
Mathias Agopian135e5892012-09-30 16:43:20 -07002631 GLint viewport[4];
2632 glGetIntegerv(GL_VIEWPORT, viewport);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002633 glViewport(0, 0, sw, sh);
2634 glMatrixMode(GL_PROJECTION);
2635 glPushMatrix();
2636 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002637 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002638 glMatrixMode(GL_MODELVIEW);
2639
2640 // redraw the screen entirely...
2641 glClearColor(0,0,0,1);
2642 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002643
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002644 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002645 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002646 for (size_t i=0 ; i<count ; ++i) {
2647 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002648 const uint32_t z = layer->drawingState().z;
2649 if (z >= minLayerZ && z <= maxLayerZ) {
2650 if (filtering) layer->setFiltering(true);
2651 layer->draw(hw);
2652 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002653 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002654 }
2655
Mathias Agopian74c40c02010-09-29 13:02:36 -07002656 // check for errors and return screen capture
2657 if (glGetError() != GL_NO_ERROR) {
2658 // error while rendering
2659 result = INVALID_OPERATION;
2660 } else {
2661 // allocate shared memory large enough to hold the
2662 // screen capture
2663 sp<MemoryHeapBase> base(
2664 new MemoryHeapBase(size, 0, "screen-capture") );
2665 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002666 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002667 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002668 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002669 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2670 if (glGetError() == GL_NO_ERROR) {
2671 *heap = base;
2672 *w = sw;
2673 *h = sh;
2674 *f = PIXEL_FORMAT_RGBA_8888;
2675 result = NO_ERROR;
2676 }
2677 } else {
2678 result = NO_MEMORY;
2679 }
2680 }
Mathias Agopian135e5892012-09-30 16:43:20 -07002681 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002682 glMatrixMode(GL_PROJECTION);
2683 glPopMatrix();
2684 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002685 } else {
2686 result = BAD_VALUE;
2687 }
2688
2689 // release FBO resources
2690 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2691 glDeleteRenderbuffersOES(1, &tname);
2692 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002693
Mathias Agopian42977342012-08-05 00:40:46 -07002694 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002695
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002696// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002697
Mathias Agopian74c40c02010-09-29 13:02:36 -07002698 return result;
2699}
2700
2701
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002702status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002703 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002704 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002705 uint32_t sw, uint32_t sh,
2706 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002707{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002708 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002709 return BAD_VALUE;
2710
2711 if (!GLExtensions::getInstance().haveFramebufferObject())
2712 return INVALID_OPERATION;
2713
2714 class MessageCaptureScreen : public MessageBase {
2715 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002716 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002717 sp<IMemoryHeap>* heap;
2718 uint32_t* w;
2719 uint32_t* h;
2720 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002721 uint32_t sw;
2722 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002723 uint32_t minLayerZ;
2724 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002725 status_t result;
2726 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002727 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002728 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002729 uint32_t sw, uint32_t sh,
2730 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002731 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002732 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2733 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2734 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002735 {
2736 }
2737 status_t getResult() const {
2738 return result;
2739 }
2740 virtual bool handler() {
2741 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002742 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002743 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002744 return true;
2745 }
2746 };
2747
2748 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002749 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002750 status_t res = postMessageSync(msg);
2751 if (res == NO_ERROR) {
2752 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2753 }
2754 return res;
2755}
2756
2757// ---------------------------------------------------------------------------
2758
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002759SurfaceFlinger::LayerVector::LayerVector() {
2760}
2761
2762SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2763 : SortedVector<sp<LayerBase> >(rhs) {
2764}
2765
2766int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2767 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002768{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002769 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002770 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2771 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002772
2773 uint32_t ls = l->currentState().layerStack;
2774 uint32_t rs = r->currentState().layerStack;
2775 if (ls != rs)
2776 return ls - rs;
2777
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002778 uint32_t lz = l->currentState().z;
2779 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002780 if (lz != rz)
2781 return lz - rz;
2782
2783 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002784}
2785
2786// ---------------------------------------------------------------------------
2787
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002788SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2789 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002790}
2791
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002792SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2793 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002794 viewport.makeInvalid();
2795 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002796}
2797
2798// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002799
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002800}; // namespace android