blob: 26e9c605467f8760d633db18ab7b93969a012580 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070042#include <gui/SurfaceTextureClient.h>
43
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
56#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080057#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070059#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080060#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070061#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070064#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopianf33e4b62012-09-20 16:54:14 -070068#include "DisplayHardware/GraphicBufferAlloc.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070069#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana67932f2011-04-20 14:20:59 -070071
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080072#define EGL_VERSION_HW_ANDROID 0x3143
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
76namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ---------------------------------------------------------------------------
78
Mathias Agopian99b49842011-06-27 16:05:52 -070079const String16 sHardwareTest("android.permission.HARDWARE_TEST");
80const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
81const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
82const String16 sDump("android.permission.DUMP");
83
84// ---------------------------------------------------------------------------
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086SurfaceFlinger::SurfaceFlinger()
87 : BnSurfaceComposer(), Thread(false),
88 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070089 mTransactionPending(false),
90 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070092 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070095 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070097 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070098 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070099 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700100 mDebugInSwapBuffers(0),
101 mLastSwapBufferTime(0),
102 mDebugInTransaction(0),
103 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700104 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114 property_get("debug.sf.ddms", value, "0");
115 mDebugDDMS = atoi(value);
116 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700117 if (!startDdmConnection()) {
118 // start failed, and DDMS debugging not enabled
119 mDebugDDMS = 0;
120 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700122 ALOGI_IF(mDebugRegion, "showupdates enabled");
123 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126void SurfaceFlinger::onFirstRef()
127{
128 mEventQueue.init(this);
129
130 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
131
132 // Wait for the main thread to be done with its initialization
133 mReadyToRunBarrier.wait();
134}
135
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137SurfaceFlinger::~SurfaceFlinger()
138{
Mathias Agopiana4912602012-07-12 14:25:33 -0700139 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
140 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
141 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800144void SurfaceFlinger::binderDied(const wp<IBinder>& who)
145{
146 // the window manager died on us. prepare its eulogy.
147
Andy McFadden13a082e2012-08-24 10:16:42 -0700148 // restore initial conditions (default device unblank, etc)
149 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150
151 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700152 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800153}
154
Mathias Agopian7e27f052010-05-28 14:22:23 -0700155sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Mathias Agopian96f08192010-06-02 23:28:45 -0700157 sp<ISurfaceComposerClient> bclient;
158 sp<Client> client(new Client(this));
159 status_t err = client->initCheck();
160 if (err == NO_ERROR) {
161 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 return bclient;
164}
165
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700166sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
167 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700168{
169 class DisplayToken : public BBinder {
170 sp<SurfaceFlinger> flinger;
171 virtual ~DisplayToken() {
172 // no more references, this display must be terminated
173 Mutex::Autolock _l(flinger->mStateLock);
174 flinger->mCurrentState.displays.removeItem(this);
175 flinger->setTransactionFlags(eDisplayTransactionNeeded);
176 }
177 public:
178 DisplayToken(const sp<SurfaceFlinger>& flinger)
179 : flinger(flinger) {
180 }
181 };
182
183 sp<BBinder> token = new DisplayToken(this);
184
185 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700186 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700187 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700188 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700189 mCurrentState.displays.add(token, info);
190
191 return token;
192}
193
194sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700195 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700196 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
197 return NULL;
198 }
199 return mDefaultDisplays[id];
200}
201
Jamie Gennis9a78c902011-01-12 18:30:40 -0800202sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
203{
204 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
205 return gba;
206}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208void SurfaceFlinger::bootFinished()
209{
210 const nsecs_t now = systemTime();
211 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000212 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700213 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700214
215 // wait patiently for the window manager death
216 const String16 name("window");
217 sp<IBinder> window(defaultServiceManager()->getService(name));
218 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700219 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700220 }
221
222 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700223 // formerly we would just kill the process, but we now ask it to exit so it
224 // can choose where to stop the animation.
225 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700228void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
229 class MessageDestroyGLTexture : public MessageBase {
230 GLuint texture;
231 public:
232 MessageDestroyGLTexture(GLuint texture)
233 : texture(texture) {
234 }
235 virtual bool handler() {
236 glDeleteTextures(1, &texture);
237 return true;
238 }
239 };
240 postMessageAsync(new MessageDestroyGLTexture(texture));
241}
242
Mathias Agopian722b98f2012-09-25 18:24:31 -0700243status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700244 EGLDisplay dpy,
245 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700246 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700247 EGLConfig* outConfig)
248{
249 EGLConfig config = NULL;
250 EGLint numConfigs = -1, n=0;
251 eglGetConfigs(dpy, NULL, 0, &numConfigs);
252 EGLConfig* const configs = new EGLConfig[numConfigs];
253 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700254
Mathias Agopian722b98f2012-09-25 18:24:31 -0700255 if (n) {
256 if (attribute != EGL_NONE) {
257 for (int i=0 ; i<n ; i++) {
258 EGLint value = 0;
259 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
260 if (wanted == value) {
261 *outConfig = configs[i];
262 delete [] configs;
263 return NO_ERROR;
264 }
265 }
266 } else {
267 // just pick the first one
268 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700269 delete [] configs;
270 return NO_ERROR;
271 }
272 }
273 delete [] configs;
274 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275}
276
Mathias Agopian722b98f2012-09-25 18:24:31 -0700277class EGLAttributeVector {
278 struct Attribute;
279 class Adder;
280 friend class Adder;
281 KeyedVector<Attribute, EGLint> mList;
282 struct Attribute {
283 Attribute() {};
284 Attribute(EGLint v) : v(v) { }
285 EGLint v;
286 bool operator < (const Attribute& other) const {
287 // this places EGL_NONE at the end
288 EGLint lhs(v);
289 EGLint rhs(other.v);
290 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
291 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
292 return lhs < rhs;
293 }
294 };
295 class Adder {
296 friend class EGLAttributeVector;
297 EGLAttributeVector& v;
298 EGLint attribute;
299 Adder(EGLAttributeVector& v, EGLint attribute)
300 : v(v), attribute(attribute) {
301 }
302 public:
303 void operator = (EGLint value) {
304 if (attribute != EGL_NONE) {
305 v.mList.add(attribute, value);
306 }
307 }
308 operator EGLint () const { return v.mList[attribute]; }
309 };
310public:
311 EGLAttributeVector() {
312 mList.add(EGL_NONE, EGL_NONE);
313 }
314 void remove(EGLint attribute) {
315 if (attribute != EGL_NONE) {
316 mList.removeItem(attribute);
317 }
318 }
319 Adder operator [] (EGLint attribute) {
320 return Adder(*this, attribute);
321 }
322 EGLint operator [] (EGLint attribute) const {
323 return mList[attribute];
324 }
325 // cast-operator to (EGLint const*)
326 operator EGLint const* () const { return &mList.keyAt(0).v; }
327};
328
Mathias Agopiana4912602012-07-12 14:25:33 -0700329EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
330 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
331 // it is to be used with WIFI displays
332 EGLConfig config;
333 EGLint dummy;
334 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700335
Mathias Agopian722b98f2012-09-25 18:24:31 -0700336 EGLAttributeVector attribs;
337 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT;
338 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
339 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
340 attribs[EGL_RED_SIZE] = 8;
341 attribs[EGL_GREEN_SIZE] = 8;
342 attribs[EGL_BLUE_SIZE] = 8;
343
344 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
345 if (!err)
346 goto success;
347
348 // maybe we failed because of EGL_FRAMEBUFFER_TARGET_ANDROID
349 ALOGW("no suitable EGLConfig found, trying without EGL_FRAMEBUFFER_TARGET_ANDROID");
350 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
351 err = selectConfigForAttribute(display, attribs,
352 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700353 if (!err)
354 goto success;
355
356 // maybe we failed because of EGL_RECORDABLE_ANDROID
357 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700358 attribs.remove(EGL_RECORDABLE_ANDROID);
359 err = selectConfigForAttribute(display, attribs,
360 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700361 if (!err)
362 goto success;
363
364 // allow less than 24-bit color; the non-gpu-accelerated emulator only
365 // supports 16-bit color
366 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
Mathias Agopian722b98f2012-09-25 18:24:31 -0700367 attribs.remove(EGL_RED_SIZE);
368 attribs.remove(EGL_GREEN_SIZE);
369 attribs.remove(EGL_BLUE_SIZE);
370 err = selectConfigForAttribute(display, attribs,
371 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700372 if (!err)
373 goto success;
374
375 // this EGL is too lame for Android
376 ALOGE("no suitable EGLConfig found, giving up");
377
378 return 0;
379
380success:
381 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700382 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700383 return config;
384}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385
Mathias Agopiana4912602012-07-12 14:25:33 -0700386EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
387 // Also create our EGLContext
388 EGLint contextAttributes[] = {
389#ifdef EGL_IMG_context_priority
390#ifdef HAS_CONTEXT_PRIORITY
391#warning "using EGL_IMG_context_priority"
392 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
393#endif
394#endif
395 EGL_NONE, EGL_NONE
396 };
397 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
398 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
399 return ctxt;
400}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401
Mathias Agopiancde87a32012-09-13 14:09:01 -0700402void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700403 GLExtensions& extensions(GLExtensions::getInstance());
404 extensions.initWithGLStrings(
405 glGetString(GL_VENDOR),
406 glGetString(GL_RENDERER),
407 glGetString(GL_VERSION),
408 glGetString(GL_EXTENSIONS),
409 eglQueryString(display, EGL_VENDOR),
410 eglQueryString(display, EGL_VERSION),
411 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700412
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
414 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700415
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700417 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 glShadeModel(GL_FLAT);
420 glDisable(GL_DITHER);
421 glDisable(GL_CULL_FACE);
422
Mathias Agopiana4912602012-07-12 14:25:33 -0700423 struct pack565 {
424 inline uint16_t operator() (int r, int g, int b) const {
425 return (r<<11)|(g<<5)|b;
426 }
427 } pack565;
428
Jamie Gennis9575f602011-10-07 14:51:16 -0700429 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
430 glGenTextures(1, &mProtectedTexName);
431 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
432 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
433 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
434 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
435 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
436 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
437 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438
Mathias Agopiana4912602012-07-12 14:25:33 -0700439 // print some debugging info
440 EGLint r,g,b,a;
441 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
442 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
443 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
444 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
445 ALOGI("EGL informations:");
446 ALOGI("vendor : %s", extensions.getEglVendor());
447 ALOGI("version : %s", extensions.getEglVersion());
448 ALOGI("extensions: %s", extensions.getEglExtension());
449 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
450 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
451 ALOGI("OpenGL ES informations:");
452 ALOGI("vendor : %s", extensions.getVendor());
453 ALOGI("renderer : %s", extensions.getRenderer());
454 ALOGI("version : %s", extensions.getVersion());
455 ALOGI("extensions: %s", extensions.getExtension());
456 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
457 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
458}
459
Mathias Agopiana4912602012-07-12 14:25:33 -0700460status_t SurfaceFlinger::readyToRun()
461{
462 ALOGI( "SurfaceFlinger's main thread ready to run. "
463 "Initializing graphics H/W...");
464
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700465 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700466 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
467 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700468
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700469 // Initialize the H/W composer object. There may or may not be an
470 // actual hardware composer underneath.
471 mHwc = new HWComposer(this,
472 *static_cast<HWComposer::EventHandler *>(this));
473
Mathias Agopiana4912602012-07-12 14:25:33 -0700474 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700475 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700476 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
477 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700478
Mathias Agopianda27af92012-09-13 18:17:13 -0700479 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
480 "couldn't create EGLContext");
481
Mathias Agopiancde87a32012-09-13 14:09:01 -0700482 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700483 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700484 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700485 mDefaultDisplays[i] = new BBinder();
Mathias Agopianf5a33922012-09-19 18:16:22 -0700486 wp<IBinder> token = mDefaultDisplays[i];
Mathias Agopianf5a33922012-09-19 18:16:22 -0700487
488 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700489 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700490 // All non-virtual displays are currently considered secure.
491 bool isSecure = true;
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700492 mCurrentState.displays.add(token, DisplayDeviceState(type));
Mathias Agopianf5a33922012-09-19 18:16:22 -0700493 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
494 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
495 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
496 sp<DisplayDevice> hw = new DisplayDevice(this,
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700497 type, isSecure, token, stc, fbs, mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700498 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700499 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700500 // for displays other than the main display, so we always
501 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700502 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700503 hw->acquireScreen();
504 }
505 mDisplays.add(token, hw);
506 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700507 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700508
Mathias Agopianf5a33922012-09-19 18:16:22 -0700509 // we need a GL context current in a few places, when initializing
510 // OpenGL ES (see below), or creating a layer,
511 // or when a texture is (asynchronously) destroyed, and for that
512 // we need a valid surface, so it's convenient to use the main display
513 // for that.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -0700514 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopiancde87a32012-09-13 14:09:01 -0700515
Mathias Agopiana4912602012-07-12 14:25:33 -0700516 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700517 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
518 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800519
Mathias Agopian028508c2012-07-25 21:12:12 -0700520 // start the EventThread
521 mEventThread = new EventThread(this);
522 mEventQueue.setEventThread(mEventThread);
523
Mathias Agopian92a979a2012-08-02 18:32:23 -0700524 // initialize our drawing state
525 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700526
Mathias Agopiancde87a32012-09-13 14:09:01 -0700527
Mathias Agopiana4912602012-07-12 14:25:33 -0700528 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800529 mReadyToRunBarrier.open();
530
Andy McFadden13a082e2012-08-24 10:16:42 -0700531 // set initial conditions (e.g. unblank default device)
532 initializeDisplays();
533
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700534 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700535 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 return NO_ERROR;
538}
539
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700540int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
541 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
542 type : mHwc->allocateDisplayId();
543}
544
Mathias Agopiana67e4182012-06-19 17:26:12 -0700545void SurfaceFlinger::startBootAnim() {
546 // start boot animation
547 property_set("service.bootanim.exit", "0");
548 property_set("ctl.start", "bootanim");
549}
550
Mathias Agopiana4912602012-07-12 14:25:33 -0700551uint32_t SurfaceFlinger::getMaxTextureSize() const {
552 return mMaxTextureSize;
553}
554
555uint32_t SurfaceFlinger::getMaxViewportDims() const {
556 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
557 mMaxViewportDims[0] : mMaxViewportDims[1];
558}
559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800561
Jamie Gennis582270d2011-08-17 18:19:00 -0700562bool SurfaceFlinger::authenticateSurfaceTexture(
563 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800564 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700565 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800566
567 // Check the visible layer list for the ISurface
568 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
569 size_t count = currentLayers.size();
570 for (size_t i=0 ; i<count ; i++) {
571 const sp<LayerBase>& layer(currentLayers[i]);
572 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700573 if (lbc != NULL) {
574 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
575 if (lbcBinder == surfaceTextureBinder) {
576 return true;
577 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800578 }
579 }
580
581 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700582 // SurfaceTexture gets destroyed before all the clients are done using it,
583 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800584 // will instead fail later on when the client tries to use the surface,
585 // which should be reported as "surface XYZ returned an -ENODEV". The
586 // purgatorized layers are no less authentic than the visible ones, so this
587 // should not cause any harm.
588 size_t purgatorySize = mLayerPurgatory.size();
589 for (size_t i=0 ; i<purgatorySize ; i++) {
590 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
591 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700592 if (lbc != NULL) {
593 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
594 if (lbcBinder == surfaceTextureBinder) {
595 return true;
596 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800597 }
598 }
599
600 return false;
601}
602
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700603status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700604 int32_t type = BAD_VALUE;
605 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
606 if (display == mDefaultDisplays[i]) {
607 type = i;
608 break;
609 }
610 }
611
612 if (type < 0) {
613 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700614 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700615
616 const HWComposer& hwc(getHwComposer());
Mathias Agopian4c0751a2012-09-20 21:28:12 -0700617 if (!hwc.isConnected(type)) {
618 return NAME_NOT_FOUND;
619 }
620
Mathias Agopian1604f772012-09-18 21:54:42 -0700621 float xdpi = hwc.getDpiX(type);
622 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700623
624 // TODO: Not sure if display density should handled by SF any longer
625 class Density {
626 static int getDensityFromProperty(char const* propName) {
627 char property[PROPERTY_VALUE_MAX];
628 int density = 0;
629 if (property_get(propName, property, NULL) > 0) {
630 density = atoi(property);
631 }
632 return density;
633 }
634 public:
635 static int getEmuDensity() {
636 return getDensityFromProperty("qemu.sf.lcd_density"); }
637 static int getBuildDensity() {
638 return getDensityFromProperty("ro.sf.lcd_density"); }
639 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700640
641 if (type == DisplayDevice::DISPLAY_PRIMARY) {
642 // The density of the device is provided by a build property
643 float density = Density::getBuildDensity() / 160.0f;
644 if (density == 0) {
645 // the build doesn't provide a density -- this is wrong!
646 // use xdpi instead
647 ALOGE("ro.sf.lcd_density must be defined as a build property");
648 density = xdpi / 160.0f;
649 }
650 if (Density::getEmuDensity()) {
651 // if "qemu.sf.lcd_density" is specified, it overrides everything
652 xdpi = ydpi = density = Density::getEmuDensity();
653 density /= 160.0f;
654 }
655 info->density = density;
656
657 // TODO: this needs to go away (currently needed only by webkit)
658 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
659 info->orientation = hw->getOrientation();
660 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
661 } else {
662 // TODO: where should this value come from?
663 static const int TV_DENSITY = 213;
664 info->density = TV_DENSITY / 160.0f;
665 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700666 }
667
Mathias Agopian1604f772012-09-18 21:54:42 -0700668 info->w = hwc.getWidth(type);
669 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700670 info->xdpi = xdpi;
671 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700672 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700673
674 // All non-virtual displays are currently considered secure.
675 info->secure = true;
676
Mathias Agopian888c8222012-08-04 21:10:38 -0700677 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700678}
679
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800680// ----------------------------------------------------------------------------
681
682sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800683 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700684}
685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800687
688void SurfaceFlinger::waitForEvent() {
689 mEventQueue.waitMessage();
690}
691
692void SurfaceFlinger::signalTransaction() {
693 mEventQueue.invalidate();
694}
695
696void SurfaceFlinger::signalLayerUpdate() {
697 mEventQueue.invalidate();
698}
699
700void SurfaceFlinger::signalRefresh() {
701 mEventQueue.refresh();
702}
703
704status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
705 nsecs_t reltime, uint32_t flags) {
706 return mEventQueue.postMessage(msg, reltime);
707}
708
709status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
710 nsecs_t reltime, uint32_t flags) {
711 status_t res = mEventQueue.postMessage(msg, reltime);
712 if (res == NO_ERROR) {
713 msg->wait();
714 }
715 return res;
716}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717
Mathias Agopian4fec8732012-06-29 14:12:52 -0700718bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 return true;
721}
722
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700723void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700724 if (mEventThread == NULL) {
725 // This is a temporary workaround for b/7145521. A non-null pointer
726 // does not mean EventThread has finished initializing, so this
727 // is not a correct fix.
728 ALOGW("WARNING: EventThread not started, ignoring vsync");
729 return;
730 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700731 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
732 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700733 mEventThread->onVSyncReceived(type, timestamp);
734 }
735}
736
737void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
738 if (mEventThread == NULL) {
739 // This is a temporary workaround for b/7145521. A non-null pointer
740 // does not mean EventThread has finished initializing, so this
741 // is not a correct fix.
742 ALOGW("WARNING: EventThread not started, ignoring hotplug");
743 return;
744 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700745
Mathias Agopian148994e2012-09-19 17:31:36 -0700746 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700747 Mutex::Autolock _l(mStateLock);
748 if (connected == false) {
749 mCurrentState.displays.removeItem(mDefaultDisplays[type]);
750 } else {
751 DisplayDeviceState info((DisplayDevice::DisplayType)type);
752 mCurrentState.displays.add(mDefaultDisplays[type], info);
753 }
754 setTransactionFlags(eDisplayTransactionNeeded);
755
Andy McFadden9e9689c2012-10-10 18:17:51 -0700756 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700757 }
Mathias Agopian86303202012-07-24 22:46:10 -0700758}
759
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700760void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
761 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700762}
763
Mathias Agopian4fec8732012-06-29 14:12:52 -0700764void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800765 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800766 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700767 case MessageQueue::INVALIDATE:
768 handleMessageTransaction();
769 handleMessageInvalidate();
770 signalRefresh();
771 break;
772 case MessageQueue::REFRESH:
773 handleMessageRefresh();
774 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800775 }
776}
777
Mathias Agopian4fec8732012-06-29 14:12:52 -0700778void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700779 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700780 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700781 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700782 }
783}
784
785void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700787 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700788}
789
790void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700791 ATRACE_CALL();
792 preComposition();
793 rebuildLayerStacks();
794 setUpHWComposer();
795 doDebugFlashRegions();
796 doComposition();
797 postComposition();
798}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700799
Mathias Agopiancd60f992012-08-16 16:28:27 -0700800void SurfaceFlinger::doDebugFlashRegions()
801{
802 // is debugging enabled
803 if (CC_LIKELY(!mDebugRegion))
804 return;
805
806 const bool repaintEverything = mRepaintEverything;
807 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
808 const sp<DisplayDevice>& hw(mDisplays[dpy]);
809 if (hw->canDraw()) {
810 // transform the dirty region into this screen's coordinate space
811 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
812 if (!dirtyRegion.isEmpty()) {
813 // redraw the whole screen
814 doComposeSurfaces(hw, Region(hw->bounds()));
815
816 // and draw the dirty region
817 glDisable(GL_TEXTURE_EXTERNAL_OES);
818 glDisable(GL_TEXTURE_2D);
819 glDisable(GL_BLEND);
820 glColor4f(1, 0, 1, 1);
821 const int32_t height = hw->getHeight();
822 Region::const_iterator it = dirtyRegion.begin();
823 Region::const_iterator const end = dirtyRegion.end();
824 while (it != end) {
825 const Rect& r = *it++;
826 GLfloat vertices[][2] = {
827 { r.left, height - r.top },
828 { r.left, height - r.bottom },
829 { r.right, height - r.bottom },
830 { r.right, height - r.top }
831 };
832 glVertexPointer(2, GL_FLOAT, 0, vertices);
833 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
834 }
835 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700836 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700837 }
838 }
839 }
840
841 postFramebuffer();
842
843 if (mDebugRegion > 1) {
844 usleep(mDebugRegion * 1000);
845 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700846
847 HWComposer& hwc(getHwComposer());
848 if (hwc.initCheck() == NO_ERROR) {
849 status_t err = hwc.prepare();
850 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
851 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852}
853
854void SurfaceFlinger::preComposition()
855{
856 bool needExtraInvalidate = false;
857 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
858 const size_t count = currentLayers.size();
859 for (size_t i=0 ; i<count ; i++) {
860 if (currentLayers[i]->onPreComposition()) {
861 needExtraInvalidate = true;
862 }
863 }
864 if (needExtraInvalidate) {
865 signalLayerUpdate();
866 }
867}
868
869void SurfaceFlinger::postComposition()
870{
871 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
872 const size_t count = currentLayers.size();
873 for (size_t i=0 ; i<count ; i++) {
874 currentLayers[i]->onPostComposition();
875 }
876}
877
878void SurfaceFlinger::rebuildLayerStacks() {
879 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700880 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700882 mVisibleRegionsDirty = false;
883 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700884
Mathias Agopian87baae12012-07-31 12:38:26 -0700885 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700886 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700887 Region opaqueRegion;
888 Region dirtyRegion;
889 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700890 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700891 const Transform& tr(hw->getTransform());
892 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700893 if (hw->canDraw()) {
894 SurfaceFlinger::computeVisibleRegions(currentLayers,
895 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700896
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700897 const size_t count = currentLayers.size();
898 for (size_t i=0 ; i<count ; i++) {
899 const sp<LayerBase>& layer(currentLayers[i]);
900 const Layer::State& s(layer->drawingState());
901 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700902 Region drawRegion(tr.transform(
903 layer->visibleNonTransparentRegion));
904 drawRegion.andSelf(bounds);
905 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700906 layersSortedByZ.add(layer);
907 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700908 }
909 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700910 }
Mathias Agopian42977342012-08-05 00:40:46 -0700911 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700912 hw->undefinedRegion.set(bounds);
913 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
914 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700915 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700916 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700918
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700920 HWComposer& hwc(getHwComposer());
921 if (hwc.initCheck() == NO_ERROR) {
922 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700923 if (CC_UNLIKELY(mHwWorkListDirty)) {
924 mHwWorkListDirty = false;
925 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
926 sp<const DisplayDevice> hw(mDisplays[dpy]);
927 const int32_t id = hw->getHwcDisplayId();
928 if (id >= 0) {
929 const Vector< sp<LayerBase> >& currentLayers(
930 hw->getVisibleLayersSortedByZ());
931 const size_t count = currentLayers.size();
932 if (hwc.createWorkList(id, count) == NO_ERROR) {
933 HWComposer::LayerListIterator cur = hwc.begin(id);
934 const HWComposer::LayerListIterator end = hwc.end(id);
935 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
936 const sp<LayerBase>& layer(currentLayers[i]);
937 layer->setGeometry(hw, *cur);
938 if (mDebugDisableHWC || mDebugRegion) {
939 cur->setSkip(true);
940 }
941 }
942 }
943 }
944 }
945 }
946
947 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700949 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700950 const int32_t id = hw->getHwcDisplayId();
951 if (id >= 0) {
952 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700953 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700954 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700955 HWComposer::LayerListIterator cur = hwc.begin(id);
956 const HWComposer::LayerListIterator end = hwc.end(id);
957 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
958 /*
959 * update the per-frame h/w composer data for each layer
960 * and build the transparent region of the FB
961 */
962 const sp<LayerBase>& layer(currentLayers[i]);
963 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700964 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700966 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700967
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 status_t err = hwc.prepare();
969 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
970 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700971}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700972
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973void SurfaceFlinger::doComposition() {
974 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700975 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700976 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700977 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700978 if (hw->canDraw()) {
979 // transform the dirty region into this screen's coordinate space
980 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
981 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700982 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700983 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700984 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700985 hw->dirtyRegion.clear();
986 hw->flip(hw->swapRegion);
987 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700988 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700989 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700990 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700991 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700992 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700993}
994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995void SurfaceFlinger::postFramebuffer()
996{
Mathias Agopian841cde52012-03-01 15:44:37 -0800997 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800998
Mathias Agopiana44b0412011-10-16 18:46:35 -0700999 const nsecs_t now = systemTime();
1000 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001001
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001002 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001003 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001004 if (!hwc.supportsFramebufferTarget()) {
1005 // EGL spec says:
1006 // "surface must be bound to the calling thread's current context,
1007 // for the current rendering API."
1008 DisplayDevice::makeCurrent(mEGLDisplay,
1009 getDefaultDisplayDevice(), mEGLContext);
1010 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001011 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001012 }
1013
Mathias Agopian92a979a2012-08-02 18:32:23 -07001014 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001015 sp<const DisplayDevice> hw(mDisplays[dpy]);
1016 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001017 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001019 int32_t id = hw->getHwcDisplayId();
1020 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001021 HWComposer::LayerListIterator cur = hwc.begin(id);
1022 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001023 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001024 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001025 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001026 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001027 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001028 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001029 }
Jesse Hallef194142012-06-14 14:45:17 -07001030 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001031 }
1032
Mathias Agopiana44b0412011-10-16 18:46:35 -07001033 mLastSwapBufferTime = systemTime() - now;
1034 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035}
1036
Mathias Agopian87baae12012-07-31 12:38:26 -07001037void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038{
Mathias Agopian841cde52012-03-01 15:44:37 -08001039 ATRACE_CALL();
1040
Mathias Agopianca4d3602011-05-19 15:38:14 -07001041 Mutex::Autolock _l(mStateLock);
1042 const nsecs_t now = systemTime();
1043 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044
Mathias Agopianca4d3602011-05-19 15:38:14 -07001045 // Here we're guaranteed that some transaction flags are set
1046 // so we can call handleTransactionLocked() unconditionally.
1047 // We call getTransactionFlags(), which will also clear the flags,
1048 // with mStateLock held to guarantee that mCurrentState won't change
1049 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001050
Mathias Agopiane57f2922012-08-09 16:29:12 -07001051 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001052 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001053
Mathias Agopianca4d3602011-05-19 15:38:14 -07001054 mLastTransactionTime = systemTime() - now;
1055 mDebugInTransaction = 0;
1056 invalidateHwcGeometry();
1057 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001058}
1059
Mathias Agopian87baae12012-07-31 12:38:26 -07001060void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001061{
1062 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 const size_t count = currentLayers.size();
1064
1065 /*
1066 * Traversal of the children
1067 * (perform the transaction for each of them if needed)
1068 */
1069
Mathias Agopian3559b072012-08-15 13:46:03 -07001070 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001072 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1074 if (!trFlags) continue;
1075
1076 const uint32_t flags = layer->doTransaction(0);
1077 if (flags & Layer::eVisibleRegion)
1078 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 }
1080 }
1081
1082 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001083 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 */
1085
Mathias Agopiane57f2922012-08-09 16:29:12 -07001086 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001087 // here we take advantage of Vector's copy-on-write semantics to
1088 // improve performance by skipping the transaction entirely when
1089 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001090 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1091 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001092 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001094 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001095 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001096
1097 // find the displays that were removed
1098 // (ie: in drawing state but not in current state)
1099 // also handle displays that changed
1100 // (ie: displays that are in both lists)
1101 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001102 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1103 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001104 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001105 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001106 // Call makeCurrent() on the primary display so we can
1107 // be sure that nothing associated with this display
1108 // is current.
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001109 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden27ec5732012-10-02 19:04:45 -07001110 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001111 mDisplays.removeItem(draw.keyAt(i));
Andy McFadden27ec5732012-10-02 19:04:45 -07001112 getHwComposer().disconnectDisplay(draw[i].type);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001113 mEventThread->onHotplugReceived(draw[i].type, false);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001114 } else {
1115 ALOGW("trying to remove the main display");
1116 }
1117 } else {
1118 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001119 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001120 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001121 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001122 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001123 // recreating the DisplayDevice, so we just remove it
1124 // from the drawing state, so that it get re-added
1125 // below.
1126 mDisplays.removeItem(display);
1127 mDrawingState.displays.removeItemsAt(i);
1128 dc--; i--;
1129 // at this point we must loop to the next item
1130 continue;
1131 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001132
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001133 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001134 if (disp != NULL) {
1135 if (state.layerStack != draw[i].layerStack) {
1136 disp->setLayerStack(state.layerStack);
1137 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001138 if ((state.orientation != draw[i].orientation)
1139 || (state.viewport != draw[i].viewport)
1140 || (state.frame != draw[i].frame))
1141 {
1142 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001143 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001144 }
Andy McFadden69052052012-09-14 16:10:11 -07001145
1146 // Walk through all the layers in currentLayers,
1147 // and update their transform hint.
1148 //
1149 // TODO: we could be much more clever about which
1150 // layers we touch and how often we do these updates
1151 // (e.g. only touch the layers associated with this
1152 // display, and only on a rotation).
1153 for (size_t i = 0; i < count; i++) {
1154 const sp<LayerBase>& layerBase = currentLayers[i];
1155 layerBase->updateTransformHint();
1156 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001157 }
1158 }
1159 }
1160
1161 // find displays that were added
1162 // (ie: in current state but not in drawing state)
1163 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001164 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001165 const DisplayDeviceState& state(curr[i]);
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001166 bool isSecure = false;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001167
1168 sp<FramebufferSurface> fbs;
1169 sp<SurfaceTextureClient> stc;
1170 if (!state.isVirtualDisplay()) {
1171
1172 ALOGE_IF(state.surface!=NULL,
1173 "adding a supported display, but rendering "
1174 "surface is provided (%p), ignoring it",
1175 state.surface.get());
1176
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001177 // All non-virtual displays are currently considered
1178 // secure.
1179 isSecure = true;
1180
Mathias Agopiancde87a32012-09-13 14:09:01 -07001181 // for supported (by hwc) displays we provide our
1182 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001183 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001184 stc = new SurfaceTextureClient(
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001185 static_cast< sp<ISurfaceTexture> >(
1186 fbs->getBufferQueue()));
Mathias Agopiancde87a32012-09-13 14:09:01 -07001187 } else {
1188 if (state.surface != NULL) {
1189 stc = new SurfaceTextureClient(state.surface);
1190 }
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001191 isSecure = state.isSecure;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001192 }
1193
1194 const wp<IBinder>& display(curr.keyAt(i));
1195 if (stc != NULL) {
1196 sp<DisplayDevice> hw = new DisplayDevice(this,
Jamie Gennisdd3cb842012-10-19 18:19:11 -07001197 state.type, isSecure, display, stc, fbs,
1198 mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001199 hw->setLayerStack(state.layerStack);
1200 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001201 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001202 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001203 mDisplays.add(display, hw);
Andy McFadden9e9689c2012-10-10 18:17:51 -07001204 mEventThread->onHotplugReceived(state.type, true);
Mathias Agopian93997a82012-08-29 17:30:36 -07001205 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001206 }
1207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210
Mathias Agopian3559b072012-08-15 13:46:03 -07001211 /*
1212 * Perform our own transaction if needed
1213 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001214
Mathias Agopiancd60f992012-08-16 16:28:27 -07001215 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1216 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001217 // layers have been added
1218 mVisibleRegionsDirty = true;
1219 }
1220
1221 // some layers might have been removed, so
1222 // we need to update the regions they're exposing.
1223 if (mLayersRemoved) {
1224 mLayersRemoved = false;
1225 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001226 const size_t count = previousLayers.size();
1227 for (size_t i=0 ; i<count ; i++) {
1228 const sp<LayerBase>& layer(previousLayers[i]);
1229 if (currentLayers.indexOf(layer) < 0) {
1230 // this layer is not visible anymore
1231 // TODO: we could traverse the tree from front to back and
1232 // compute the actual visible region
1233 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001234 const Layer::State& s(layer->drawingState());
1235 Region visibleReg = s.transform.transform(
1236 Region(Rect(s.active.w, s.active.h)));
1237 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001238 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 }
1241
1242 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001243}
1244
1245void SurfaceFlinger::commitTransaction()
1246{
1247 if (!mLayersPendingRemoval.isEmpty()) {
1248 // Notify removed layers now that they can't be drawn from
1249 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1250 mLayersPendingRemoval[i]->onRemoved();
1251 }
1252 mLayersPendingRemoval.clear();
1253 }
1254
1255 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001256 mTransactionPending = false;
1257 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001258 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259}
1260
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001262 const LayerVector& currentLayers, uint32_t layerStack,
1263 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264{
Mathias Agopian841cde52012-03-01 15:44:37 -08001265 ATRACE_CALL();
1266
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 Region aboveOpaqueLayers;
1268 Region aboveCoveredLayers;
1269 Region dirty;
1270
Mathias Agopian87baae12012-07-31 12:38:26 -07001271 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272
1273 size_t i = currentLayers.size();
1274 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001275 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276
1277 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001278 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279
Mathias Agopian87baae12012-07-31 12:38:26 -07001280 // only consider the layers on the given later stack
1281 if (s.layerStack != layerStack)
1282 continue;
1283
Mathias Agopianab028732010-03-16 16:41:46 -07001284 /*
1285 * opaqueRegion: area of a surface that is fully opaque.
1286 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001288
1289 /*
1290 * visibleRegion: area of a surface that is visible on screen
1291 * and not fully transparent. This is essentially the layer's
1292 * footprint minus the opaque regions above it.
1293 * Areas covered by a translucent surface are considered visible.
1294 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001296
1297 /*
1298 * coveredRegion: area of a surface that is covered by all
1299 * visible regions above it (which includes the translucent areas).
1300 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001302
Jesse Halla8026d22012-09-25 13:25:04 -07001303 /*
1304 * transparentRegion: area of a surface that is hinted to be completely
1305 * transparent. This is only used to tell when the layer has no visible
1306 * non-transparent regions and can be removed from the layer list. It
1307 * does not affect the visibleRegion of this layer or any layers
1308 * beneath it. The hint may not be correct if apps don't respect the
1309 * SurfaceView restrictions (which, sadly, some don't).
1310 */
1311 Region transparentRegion;
1312
Mathias Agopianab028732010-03-16 16:41:46 -07001313
1314 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001315 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001316 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001317 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001319 if (!visibleRegion.isEmpty()) {
1320 // Remove the transparent area from the visible region
1321 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 const Transform tr(s.transform);
1323 if (tr.transformed()) {
1324 if (tr.preserveRects()) {
1325 // transform the transparent region
Jesse Halla8026d22012-09-25 13:25:04 -07001326 transparentRegion = tr.transform(s.transparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 } else {
1328 // transformation too complex, can't do the
1329 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001330 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001331 }
1332 } else {
Jesse Halla8026d22012-09-25 13:25:04 -07001333 transparentRegion = s.transparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001334 }
Mathias Agopianab028732010-03-16 16:41:46 -07001335 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336
Mathias Agopianab028732010-03-16 16:41:46 -07001337 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001338 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001339 if (s.alpha==255 && !translucent &&
1340 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1341 // the opaque region is the layer's footprint
1342 opaqueRegion = visibleRegion;
1343 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 }
1345 }
1346
Mathias Agopianab028732010-03-16 16:41:46 -07001347 // Clip the covered region to the visible region
1348 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1349
1350 // Update aboveCoveredLayers for next (lower) layer
1351 aboveCoveredLayers.orSelf(visibleRegion);
1352
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 // subtract the opaque region covered by the layers above us
1354 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355
1356 // compute this layer's dirty region
1357 if (layer->contentDirty) {
1358 // we need to invalidate the whole region
1359 dirty = visibleRegion;
1360 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001361 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 layer->contentDirty = false;
1363 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001364 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001365 * the exposed region consists of two components:
1366 * 1) what's VISIBLE now and was COVERED before
1367 * 2) what's EXPOSED now less what was EXPOSED before
1368 *
1369 * note that (1) is conservative, we start with the whole
1370 * visible region but only keep what used to be covered by
1371 * something -- which mean it may have been exposed.
1372 *
1373 * (2) handles areas that were not covered by anything but got
1374 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001375 */
Mathias Agopianab028732010-03-16 16:41:46 -07001376 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001377 const Region oldVisibleRegion = layer->visibleRegion;
1378 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001379 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1380 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 }
1382 dirty.subtractSelf(aboveOpaqueLayers);
1383
1384 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001385 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386
Mathias Agopianab028732010-03-16 16:41:46 -07001387 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001389
Jesse Halla8026d22012-09-25 13:25:04 -07001390 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391 layer->setVisibleRegion(visibleRegion);
1392 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001393 layer->setVisibleNonTransparentRegion(
1394 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 }
1396
Mathias Agopian87baae12012-07-31 12:38:26 -07001397 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398}
1399
Mathias Agopian87baae12012-07-31 12:38:26 -07001400void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1401 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001402 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001403 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1404 if (hw->getLayerStack() == layerStack) {
1405 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001406 }
1407 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001408}
1409
1410void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001412 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413
Mathias Agopian4fec8732012-06-29 14:12:52 -07001414 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001415 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001416 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001417 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001418 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001419 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001420 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001421 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001423
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001424 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425}
1426
Mathias Agopianad456f92011-01-13 17:53:01 -08001427void SurfaceFlinger::invalidateHwcGeometry()
1428{
1429 mHwWorkListDirty = true;
1430}
1431
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001432
Mathias Agopiancd60f992012-08-16 16:28:27 -07001433void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001434 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435{
Mathias Agopian87baae12012-07-31 12:38:26 -07001436 Region dirtyRegion(inDirtyRegion);
1437
Mathias Agopianb8a55602009-06-26 19:06:36 -07001438 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001439 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440
Mathias Agopian42977342012-08-05 00:40:46 -07001441 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001442 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001443 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1444 // takes a rectangle, we must make sure to update that whole
1445 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001446 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001447 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001448 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001449 // We need to redraw the rectangle that will be updated
1450 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001451 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001452 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001453 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001454 } else {
1455 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001456 dirtyRegion.set(hw->bounds());
1457 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
1459 }
1460
Mathias Agopiancd60f992012-08-16 16:28:27 -07001461 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001463 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001464 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001465
1466 // swap buffers (presentation)
1467 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopiancd60f992012-08-16 16:28:27 -07001470void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001471{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001472 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001473 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001474 HWComposer::LayerListIterator cur = hwc.begin(id);
1475 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001476
Mathias Agopian85d751c2012-08-29 16:59:24 -07001477 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1478 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001479 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001480
1481 // set the frame buffer
1482 glMatrixMode(GL_MODELVIEW);
1483 glLoadIdentity();
1484
1485 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001486 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001487 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001488 // when using overlays, we assume a fully transparent framebuffer
1489 // NOTE: we could reduce how much we need to clear, for instance
1490 // remove where there are opaque FB layers. however, on some
1491 // GPUs doing a "clean slate" glClear might be more efficient.
1492 // We'll revisit later if needed.
1493 glClearColor(0, 0, 0, 0);
1494 glClear(GL_COLOR_BUFFER_BIT);
1495 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001496 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001497 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001498 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001499 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001500 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001501 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001502 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001503 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001504
Mathias Agopian85d751c2012-08-29 16:59:24 -07001505 /*
1506 * and then, render the layers targeted at the framebuffer
1507 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001508
Mathias Agopian85d751c2012-08-29 16:59:24 -07001509 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1510 const size_t count = layers.size();
1511 const Transform& tr = hw->getTransform();
1512 if (cur != end) {
1513 // we're using h/w composer
1514 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001515 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001516 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001517 if (!clip.isEmpty()) {
1518 switch (cur->getCompositionType()) {
1519 case HWC_OVERLAY: {
1520 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1521 && i
1522 && layer->isOpaque()
1523 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001524 // never clear the very first layer since we're
1525 // guaranteed the FB is already cleared
1526 layer->clearWithOpenGL(hw, clip);
1527 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001528 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001529 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001530 case HWC_FRAMEBUFFER: {
1531 layer->draw(hw, clip);
1532 break;
1533 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001534 case HWC_FRAMEBUFFER_TARGET: {
1535 // this should not happen as the iterator shouldn't
1536 // let us get there.
1537 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1538 break;
1539 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001540 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001541 }
1542 layer->setAcquireFence(hw, *cur);
1543 }
1544 } else {
1545 // we're not using h/w composer
1546 for (size_t i=0 ; i<count ; ++i) {
1547 const sp<LayerBase>& layer(layers[i]);
1548 const Region clip(dirty.intersect(
1549 tr.transform(layer->visibleRegion)));
1550 if (!clip.isEmpty()) {
1551 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001552 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001553 }
1554 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555}
1556
Mathias Agopian55801e42012-08-27 18:54:24 -07001557void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1558 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001560 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001561 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001562 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001563 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001564
Mathias Agopian55801e42012-08-27 18:54:24 -07001565 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001566 Region::const_iterator it = region.begin();
1567 Region::const_iterator const end = region.end();
1568 while (it != end) {
1569 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001570 GLfloat vertices[][2] = {
1571 { r.left, height - r.top },
1572 { r.left, height - r.bottom },
1573 { r.right, height - r.bottom },
1574 { r.right, height - r.top }
1575 };
1576 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001577 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1578 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579}
1580
Mathias Agopian96f08192010-06-02 23:28:45 -07001581ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1582 const sp<LayerBaseClient>& lbc)
1583{
Mathias Agopian96f08192010-06-02 23:28:45 -07001584 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001585 size_t name = client->attachLayer(lbc);
1586
Mathias Agopian96f08192010-06-02 23:28:45 -07001587 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001588 Mutex::Autolock _l(mStateLock);
1589 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001590
Mathias Agopian4f113742011-05-03 16:21:41 -07001591 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001592}
1593
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001594status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595{
1596 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001597 status_t err = purgatorizeLayer_l(layer);
1598 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001599 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001600 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601}
1602
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001603status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604{
1605 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1606 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001607 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 return NO_ERROR;
1609 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001610 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611}
1612
Mathias Agopian9a112062009-04-17 19:36:26 -07001613status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1614{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001615 // First add the layer to the purgatory list, which makes sure it won't
1616 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001617 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001618 if (err >= 0) {
1619 mLayerPurgatory.add(layerBase);
1620 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001621
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001622 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001623
Mathias Agopian3d579642009-06-04 18:46:21 -07001624 // it's possible that we don't find a layer, because it might
1625 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 // from the user because there is a race between Client::destroySurface(),
1627 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001628 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1629}
1630
Mathias Agopiandea20b12011-05-03 17:04:02 -07001631uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1632{
1633 return android_atomic_release_load(&mTransactionFlags);
1634}
1635
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1637{
1638 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1639}
1640
Mathias Agopianbb641242010-05-18 17:06:55 -07001641uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642{
1643 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1644 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001645 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 }
1647 return old;
1648}
1649
Mathias Agopian8b33f032012-07-24 20:43:54 -07001650void SurfaceFlinger::setTransactionState(
1651 const Vector<ComposerState>& state,
1652 const Vector<DisplayState>& displays,
1653 uint32_t flags)
1654{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001655 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001656 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001657 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001658
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001659 if (flags & eAnimation) {
1660 // For window updates that are part of an animation we must wait for
1661 // previous animation "frames" to be handled.
1662 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001663 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001664 if (CC_UNLIKELY(err != NO_ERROR)) {
1665 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001666 // caller after a few seconds.
1667 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1668 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001669 mAnimTransactionPending = false;
1670 break;
1671 }
1672 }
1673 }
1674
Mathias Agopiane57f2922012-08-09 16:29:12 -07001675 size_t count = displays.size();
1676 for (size_t i=0 ; i<count ; i++) {
1677 const DisplayState& s(displays[i]);
1678 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001679 }
1680
Mathias Agopiane57f2922012-08-09 16:29:12 -07001681 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001682 for (size_t i=0 ; i<count ; i++) {
1683 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001684 // Here we need to check that the interface we're given is indeed
1685 // one of our own. A malicious client could give us a NULL
1686 // IInterface, or one of its own or even one of our own but a
1687 // different type. All these situations would cause us to crash.
1688 //
1689 // NOTE: it would be better to use RTTI as we could directly check
1690 // that we have a Client*. however, RTTI is disabled in Android.
1691 if (s.client != NULL) {
1692 sp<IBinder> binder = s.client->asBinder();
1693 if (binder != NULL) {
1694 String16 desc(binder->getInterfaceDescriptor());
1695 if (desc == ISurfaceComposerClient::descriptor) {
1696 sp<Client> client( static_cast<Client *>(s.client.get()) );
1697 transactionFlags |= setClientStateLocked(client, s.state);
1698 }
1699 }
1700 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001701 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001702
Mathias Agopian386aa982011-11-07 21:58:03 -08001703 if (transactionFlags) {
1704 // this triggers the transaction
1705 setTransactionFlags(transactionFlags);
1706
1707 // if this is a synchronous transaction, wait for it to take effect
1708 // before returning.
1709 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001710 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001711 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001712 if (flags & eAnimation) {
1713 mAnimTransactionPending = true;
1714 }
1715 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001716 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1717 if (CC_UNLIKELY(err != NO_ERROR)) {
1718 // just in case something goes wrong in SF, return to the
1719 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001720 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1721 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001722 break;
1723 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001724 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 }
1726}
1727
Mathias Agopiane57f2922012-08-09 16:29:12 -07001728uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1729{
Jesse Hall9a143922012-10-04 16:29:19 -07001730 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1731 if (dpyIdx < 0)
1732 return 0;
1733
Mathias Agopiane57f2922012-08-09 16:29:12 -07001734 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001735 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001736 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001737 const uint32_t what = s.what;
1738 if (what & DisplayState::eSurfaceChanged) {
1739 if (disp.surface->asBinder() != s.surface->asBinder()) {
1740 disp.surface = s.surface;
1741 flags |= eDisplayTransactionNeeded;
1742 }
1743 }
1744 if (what & DisplayState::eLayerStackChanged) {
1745 if (disp.layerStack != s.layerStack) {
1746 disp.layerStack = s.layerStack;
1747 flags |= eDisplayTransactionNeeded;
1748 }
1749 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001750 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001751 if (disp.orientation != s.orientation) {
1752 disp.orientation = s.orientation;
1753 flags |= eDisplayTransactionNeeded;
1754 }
1755 if (disp.frame != s.frame) {
1756 disp.frame = s.frame;
1757 flags |= eDisplayTransactionNeeded;
1758 }
1759 if (disp.viewport != s.viewport) {
1760 disp.viewport = s.viewport;
1761 flags |= eDisplayTransactionNeeded;
1762 }
1763 }
1764 }
1765 return flags;
1766}
1767
1768uint32_t SurfaceFlinger::setClientStateLocked(
1769 const sp<Client>& client,
1770 const layer_state_t& s)
1771{
1772 uint32_t flags = 0;
1773 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1774 if (layer != 0) {
1775 const uint32_t what = s.what;
1776 if (what & layer_state_t::ePositionChanged) {
1777 if (layer->setPosition(s.x, s.y))
1778 flags |= eTraversalNeeded;
1779 }
1780 if (what & layer_state_t::eLayerChanged) {
1781 // NOTE: index needs to be calculated before we update the state
1782 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1783 if (layer->setLayer(s.z)) {
1784 mCurrentState.layersSortedByZ.removeAt(idx);
1785 mCurrentState.layersSortedByZ.add(layer);
1786 // we need traversal (state changed)
1787 // AND transaction (list changed)
1788 flags |= eTransactionNeeded|eTraversalNeeded;
1789 }
1790 }
1791 if (what & layer_state_t::eSizeChanged) {
1792 if (layer->setSize(s.w, s.h)) {
1793 flags |= eTraversalNeeded;
1794 }
1795 }
1796 if (what & layer_state_t::eAlphaChanged) {
1797 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1798 flags |= eTraversalNeeded;
1799 }
1800 if (what & layer_state_t::eMatrixChanged) {
1801 if (layer->setMatrix(s.matrix))
1802 flags |= eTraversalNeeded;
1803 }
1804 if (what & layer_state_t::eTransparentRegionChanged) {
1805 if (layer->setTransparentRegionHint(s.transparentRegion))
1806 flags |= eTraversalNeeded;
1807 }
1808 if (what & layer_state_t::eVisibilityChanged) {
1809 if (layer->setFlags(s.flags, s.mask))
1810 flags |= eTraversalNeeded;
1811 }
1812 if (what & layer_state_t::eCropChanged) {
1813 if (layer->setCrop(s.crop))
1814 flags |= eTraversalNeeded;
1815 }
1816 if (what & layer_state_t::eLayerStackChanged) {
1817 // NOTE: index needs to be calculated before we update the state
1818 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1819 if (layer->setLayerStack(s.layerStack)) {
1820 mCurrentState.layersSortedByZ.removeAt(idx);
1821 mCurrentState.layersSortedByZ.add(layer);
1822 // we need traversal (state changed)
1823 // AND transaction (list changed)
1824 flags |= eTransactionNeeded|eTraversalNeeded;
1825 }
1826 }
1827 }
1828 return flags;
1829}
1830
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001831sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001832 ISurfaceComposerClient::surface_data_t* params,
1833 const String8& name,
1834 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001835 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836 uint32_t flags)
1837{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001838 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001839 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001840
1841 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001842 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001843 int(w), int(h));
1844 return surfaceHandle;
1845 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001846
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001847 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001848 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1849 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001850 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001852 case ISurfaceComposerClient::eFXSurfaceBlur:
1853 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001854 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001856 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001857 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001858 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001859 }
1860
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001861 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001862 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001863 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001864 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001866 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001867 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001868 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001869 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001870 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 }
1872
1873 return surfaceHandle;
1874}
1875
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001876sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001877 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001878 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001879 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880{
1881 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001882 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 case PIXEL_FORMAT_TRANSPARENT:
1884 case PIXEL_FORMAT_TRANSLUCENT:
1885 format = PIXEL_FORMAT_RGBA_8888;
1886 break;
1887 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001888#ifdef NO_RGBX_8888
1889 format = PIXEL_FORMAT_RGB_565;
1890#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001891 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001892#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 break;
1894 }
1895
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001896#ifdef NO_RGBX_8888
1897 if (format == PIXEL_FORMAT_RGBX_8888)
1898 format = PIXEL_FORMAT_RGBA_8888;
1899#endif
1900
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001901 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001902 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001903 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001904 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001905 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001906 }
1907 return layer;
1908}
1909
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001910sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001911 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001912 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001914 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001915 return layer;
1916}
1917
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001918sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001919 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001920 uint32_t w, uint32_t h, uint32_t flags)
1921{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001922 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001923 return layer;
1924}
1925
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001926status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927{
Mathias Agopian9a112062009-04-17 19:36:26 -07001928 /*
1929 * called by the window manager, when a surface should be marked for
1930 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001931 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001932 * The surface is removed from the current and drawing lists, but placed
1933 * in the purgatory queue, so it's not destroyed right-away (we need
1934 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001935 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001936
Mathias Agopian48d819a2009-09-10 19:41:18 -07001937 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001938 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001939 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001940
Mathias Agopian48d819a2009-09-10 19:41:18 -07001941 if (layer != 0) {
1942 err = purgatorizeLayer_l(layer);
1943 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001944 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001945 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001946 }
1947 return err;
1948}
1949
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001950status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001951{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001952 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001953 status_t err = NO_ERROR;
1954 sp<LayerBaseClient> l(layer.promote());
1955 if (l != NULL) {
1956 Mutex::Autolock _l(mStateLock);
1957 err = removeLayer_l(l);
1958 if (err == NAME_NOT_FOUND) {
1959 // The surface wasn't in the current list, which means it was
1960 // removed already, which means it is in the purgatory,
1961 // and need to be removed from there.
1962 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001963 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001964 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001965 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001966 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001967 "error removing layer=%p (%s)", l.get(), strerror(-err));
1968 }
1969 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970}
1971
Mathias Agopianb60314a2012-04-10 22:09:54 -07001972// ---------------------------------------------------------------------------
1973
Andy McFadden13a082e2012-08-24 10:16:42 -07001974void SurfaceFlinger::onInitializeDisplays() {
1975 // reset screen orientation
1976 Vector<ComposerState> state;
1977 Vector<DisplayState> displays;
1978 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001979 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001980 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001981 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001982 d.frame.makeInvalid();
1983 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001984 displays.add(d);
1985 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001986 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001987}
1988
1989void SurfaceFlinger::initializeDisplays() {
1990 class MessageScreenInitialized : public MessageBase {
1991 SurfaceFlinger* flinger;
1992 public:
1993 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1994 virtual bool handler() {
1995 flinger->onInitializeDisplays();
1996 return true;
1997 }
1998 };
1999 sp<MessageBase> msg = new MessageScreenInitialized(this);
2000 postMessageAsync(msg); // we may be called from main thread, use async message
2001}
2002
2003
Mathias Agopiancde87a32012-09-13 14:09:01 -07002004void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002005 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2006 if (hw->isScreenAcquired()) {
2007 // this is expected, e.g. when power manager wakes up during boot
2008 ALOGD(" screen was previously acquired");
2009 return;
2010 }
2011
Mathias Agopian42977342012-08-05 00:40:46 -07002012 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002013 int32_t type = hw->getDisplayType();
2014 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2015 // built-in display, tell the HWC
2016 getHwComposer().acquire(type);
2017
2018 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2019 // FIXME: eventthread only knows about the main display right now
2020 mEventThread->onScreenAcquired();
2021 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002022 }
Mathias Agopian20128302012-08-13 18:32:13 -07002023 mVisibleRegionsDirty = true;
2024 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025}
2026
Mathias Agopiancde87a32012-09-13 14:09:01 -07002027void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002028 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2029 if (!hw->isScreenAcquired()) {
2030 ALOGD(" screen was previously released");
2031 return;
2032 }
2033
2034 hw->releaseScreen();
2035 int32_t type = hw->getDisplayType();
2036 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2037 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002038 // FIXME: eventthread only knows about the main display right now
2039 mEventThread->onScreenReleased();
2040 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002041
2042 // built-in display, tell the HWC
2043 getHwComposer().release(type);
2044 }
2045 mVisibleRegionsDirty = true;
2046 // from this point on, SF will stop drawing on this display
2047}
2048
2049void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2050 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002051 SurfaceFlinger& mFlinger;
2052 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002053 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002054 MessageScreenAcquired(SurfaceFlinger& flinger,
2055 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002056 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002057 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2058 if (hw == NULL) {
2059 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2060 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2061 ALOGW("Attempt to unblank virtual display");
2062 } else {
2063 mFlinger.onScreenAcquired(hw);
2064 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002065 return true;
2066 }
2067 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002068 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2069 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002070}
2071
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002072void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002073 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002074 SurfaceFlinger& mFlinger;
2075 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002076 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002077 MessageScreenReleased(SurfaceFlinger& flinger,
2078 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002079 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002080 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2081 if (hw == NULL) {
2082 ALOGE("Attempt to blank null display %p", mDisplay.get());
2083 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2084 ALOGW("Attempt to blank virtual display");
2085 } else {
2086 mFlinger.onScreenReleased(hw);
2087 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002088 return true;
2089 }
2090 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002091 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2092 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002093}
2094
2095// ---------------------------------------------------------------------------
2096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2098{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08002099 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100 char buffer[SIZE];
2101 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002102
2103 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002104 snprintf(buffer, SIZE, "Permission Denial: "
2105 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
2106 IPCThreadState::self()->getCallingPid(),
2107 IPCThreadState::self()->getCallingUid());
2108 result.append(buffer);
2109 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002110 // Try to get the main lock, but don't insist if we can't
2111 // (this would indicate SF is stuck, but we want to be able to
2112 // print something in dumpsys).
2113 int retry = 3;
2114 while (mStateLock.tryLock()<0 && --retry>=0) {
2115 usleep(1000000);
2116 }
2117 const bool locked(retry >= 0);
2118 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002119 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07002120 "SurfaceFlinger appears to be unresponsive, "
2121 "dumping anyways (no locks held)\n");
2122 result.append(buffer);
2123 }
2124
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002125 bool dumpAll = true;
2126 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002127 size_t numArgs = args.size();
2128 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002129 if ((index < numArgs) &&
2130 (args[index] == String16("--list"))) {
2131 index++;
2132 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002133 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002134 }
2135
2136 if ((index < numArgs) &&
2137 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002138 index++;
2139 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002140 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002141 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002142
2143 if ((index < numArgs) &&
2144 (args[index] == String16("--latency-clear"))) {
2145 index++;
2146 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002147 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002150
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002151 if (dumpAll) {
2152 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002153 }
2154
Mathias Agopian9795c422009-08-26 16:36:26 -07002155 if (locked) {
2156 mStateLock.unlock();
2157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002158 }
2159 write(fd, result.string(), result.size());
2160 return NO_ERROR;
2161}
2162
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002163void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2164 String8& result, char* buffer, size_t SIZE) const
2165{
2166 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2167 const size_t count = currentLayers.size();
2168 for (size_t i=0 ; i<count ; i++) {
2169 const sp<LayerBase>& layer(currentLayers[i]);
2170 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2171 result.append(buffer);
2172 }
2173}
2174
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002175void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2176 String8& result, char* buffer, size_t SIZE) const
2177{
2178 String8 name;
2179 if (index < args.size()) {
2180 name = String8(args[index]);
2181 index++;
2182 }
2183
2184 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2185 const size_t count = currentLayers.size();
2186 for (size_t i=0 ; i<count ; i++) {
2187 const sp<LayerBase>& layer(currentLayers[i]);
2188 if (name.isEmpty()) {
2189 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2190 result.append(buffer);
2191 }
2192 if (name.isEmpty() || (name == layer->getName())) {
2193 layer->dumpStats(result, buffer, SIZE);
2194 }
2195 }
2196}
2197
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002198void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
2199 String8& result, char* buffer, size_t SIZE) const
2200{
2201 String8 name;
2202 if (index < args.size()) {
2203 name = String8(args[index]);
2204 index++;
2205 }
2206
2207 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2208 const size_t count = currentLayers.size();
2209 for (size_t i=0 ; i<count ; i++) {
2210 const sp<LayerBase>& layer(currentLayers[i]);
2211 if (name.isEmpty() || (name == layer->getName())) {
2212 layer->clearStats();
2213 }
2214 }
2215}
2216
Andy McFadden4803b742012-09-24 19:07:20 -07002217/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2218{
2219 static const char* config =
2220 " [sf"
2221#ifdef NO_RGBX_8888
2222 " NO_RGBX_8888"
2223#endif
2224#ifdef HAS_CONTEXT_PRIORITY
2225 " HAS_CONTEXT_PRIORITY"
2226#endif
2227#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2228 " NEVER_DEFAULT_TO_ASYNC_MODE"
2229#endif
2230#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2231 " TARGET_DISABLE_TRIPLE_BUFFERING"
2232#endif
2233 "]";
2234 result.append(config);
2235}
2236
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002237void SurfaceFlinger::dumpAllLocked(
2238 String8& result, char* buffer, size_t SIZE) const
2239{
2240 // figure out if we're stuck somewhere
2241 const nsecs_t now = systemTime();
2242 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2243 const nsecs_t inTransaction(mDebugInTransaction);
2244 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2245 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2246
2247 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002248 * Dump library configuration.
2249 */
2250 result.append("Build configuration:");
2251 appendSfConfigString(result);
2252 appendUiConfigString(result);
2253 appendGuiConfigString(result);
2254 result.append("\n");
2255
2256 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002257 * Dump the visible layer list
2258 */
2259 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2260 const size_t count = currentLayers.size();
2261 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2262 result.append(buffer);
2263 for (size_t i=0 ; i<count ; i++) {
2264 const sp<LayerBase>& layer(currentLayers[i]);
2265 layer->dump(result, buffer, SIZE);
2266 }
2267
2268 /*
2269 * Dump the layers in the purgatory
2270 */
2271
2272 const size_t purgatorySize = mLayerPurgatory.size();
2273 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2274 result.append(buffer);
2275 for (size_t i=0 ; i<purgatorySize ; i++) {
2276 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2277 layer->shortDump(result, buffer, SIZE);
2278 }
2279
2280 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002281 * Dump Display state
2282 */
2283
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002284 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2285 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002286 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2287 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002288 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002289 }
2290
2291 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002292 * Dump SurfaceFlinger global state
2293 */
2294
2295 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2296 result.append(buffer);
2297
Mathias Agopian888c8222012-08-04 21:10:38 -07002298 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002299 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002300 const GLExtensions& extensions(GLExtensions::getInstance());
2301 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2302 extensions.getVendor(),
2303 extensions.getRenderer(),
2304 extensions.getVersion());
2305 result.append(buffer);
2306
2307 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002308 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309 result.append(buffer);
2310
2311 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2312 result.append(buffer);
2313
Mathias Agopian42977342012-08-05 00:40:46 -07002314 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002315 snprintf(buffer, SIZE,
2316 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002317 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318 result.append(buffer);
2319 snprintf(buffer, SIZE,
2320 " last eglSwapBuffers() time: %f us\n"
2321 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002322 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002323 " refresh-rate : %f fps\n"
2324 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002325 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326 mLastSwapBufferTime/1000.0,
2327 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002328 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002329 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2330 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2331 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332 result.append(buffer);
2333
2334 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2335 inSwapBuffersDuration/1000.0);
2336 result.append(buffer);
2337
2338 snprintf(buffer, SIZE, " transaction time: %f us\n",
2339 inTransactionDuration/1000.0);
2340 result.append(buffer);
2341
2342 /*
2343 * VSYNC state
2344 */
2345 mEventThread->dump(result, buffer, SIZE);
2346
2347 /*
2348 * Dump HWComposer state
2349 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002350 snprintf(buffer, SIZE, "h/w composer state:\n");
2351 result.append(buffer);
2352 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2353 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2354 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2355 result.append(buffer);
Mathias Agopiancb558572012-10-04 15:58:54 -07002356 hwc.dump(result, buffer, SIZE);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357
2358 /*
2359 * Dump gralloc state
2360 */
2361 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2362 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002363}
2364
Mathias Agopiancb558572012-10-04 15:58:54 -07002365const Vector< sp<LayerBase> >&
2366SurfaceFlinger::getLayerSortedByZForHwcDisplay(int disp) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002367 // Note: mStateLock is held here
Mathias Agopiancb558572012-10-04 15:58:54 -07002368 return getDisplayDevice( getBuiltInDisplay(disp) )->getVisibleLayersSortedByZ();
2369}
2370
Keun young Park63f165f2012-08-31 10:53:36 -07002371bool SurfaceFlinger::startDdmConnection()
2372{
2373 void* libddmconnection_dso =
2374 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2375 if (!libddmconnection_dso) {
2376 return false;
2377 }
2378 void (*DdmConnection_start)(const char* name);
2379 DdmConnection_start =
2380 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2381 if (!DdmConnection_start) {
2382 dlclose(libddmconnection_dso);
2383 return false;
2384 }
2385 (*DdmConnection_start)(getServiceName());
2386 return true;
2387}
2388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002389status_t SurfaceFlinger::onTransact(
2390 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2391{
2392 switch (code) {
2393 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002394 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002395 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002396 case BLANK:
2397 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002398 {
2399 // codes that require permission check
2400 IPCThreadState* ipc = IPCThreadState::self();
2401 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002402 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002403 if ((uid != AID_GRAPHICS) &&
2404 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002405 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002406 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2407 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002408 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002409 break;
2410 }
2411 case CAPTURE_SCREEN:
2412 {
2413 // codes that require permission check
2414 IPCThreadState* ipc = IPCThreadState::self();
2415 const int pid = ipc->getCallingPid();
2416 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002417 if ((uid != AID_GRAPHICS) &&
2418 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002419 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002420 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2421 return PERMISSION_DENIED;
2422 }
2423 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002424 }
2425 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2428 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002429 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002430 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002431 IPCThreadState* ipc = IPCThreadState::self();
2432 const int pid = ipc->getCallingPid();
2433 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002434 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002435 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002436 return PERMISSION_DENIED;
2437 }
2438 int n;
2439 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002440 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002441 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002442 return NO_ERROR;
2443 case 1002: // SHOW_UPDATES
2444 n = data.readInt32();
2445 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002446 invalidateHwcGeometry();
2447 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002448 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002449 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002450 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002451 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002452 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002453 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002454 setTransactionFlags(
2455 eTransactionNeeded|
2456 eDisplayTransactionNeeded|
2457 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002458 return NO_ERROR;
2459 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002460 case 1006:{ // send empty update
2461 signalRefresh();
2462 return NO_ERROR;
2463 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002464 case 1008: // toggle use of hw composer
2465 n = data.readInt32();
2466 mDebugDisableHWC = n ? 1 : 0;
2467 invalidateHwcGeometry();
2468 repaintEverything();
2469 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002470 case 1009: // toggle use of transform hint
2471 n = data.readInt32();
2472 mDebugDisableTransformHint = n ? 1 : 0;
2473 invalidateHwcGeometry();
2474 repaintEverything();
2475 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002477 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002478 reply->writeInt32(0);
2479 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002480 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002481 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 return NO_ERROR;
2483 case 1013: {
2484 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002485 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2486 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002487 }
2488 return NO_ERROR;
2489 }
2490 }
2491 return err;
2492}
2493
Mathias Agopian53331da2011-08-22 21:44:41 -07002494void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002495 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002496 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002497}
2498
Mathias Agopian59119e62010-10-11 12:37:43 -07002499// ---------------------------------------------------------------------------
2500
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002501status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002502 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2503{
2504 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002505 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002506}
2507
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002508status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002509 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002510{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002511 ATRACE_CALL();
2512
Mathias Agopian59119e62010-10-11 12:37:43 -07002513 if (!GLExtensions::getInstance().haveFramebufferObject())
2514 return INVALID_OPERATION;
2515
2516 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002517 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2518 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002519 const uint32_t hw_w = hw->getWidth();
2520 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002521 GLfloat u = 1;
2522 GLfloat v = 1;
2523
2524 // make sure to clear all GL error flags
2525 while ( glGetError() != GL_NO_ERROR ) ;
2526
2527 // create a FBO
2528 GLuint name, tname;
2529 glGenTextures(1, &tname);
2530 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002531 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2532 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002533 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2534 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002535 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002536 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002537 GLint tw = (2 << (31 - clz(hw_w)));
2538 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002539 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2540 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002541 u = GLfloat(hw_w) / tw;
2542 v = GLfloat(hw_h) / th;
2543 }
2544 glGenFramebuffersOES(1, &name);
2545 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002546 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2547 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002548
Mathias Agopianbae92d02012-09-28 01:00:47 -07002549 DisplayDevice::setViewportAndProjection(hw);
2550
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002551 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002552 glDisable(GL_TEXTURE_EXTERNAL_OES);
2553 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002554 glClearColor(0,0,0,1);
2555 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002556 glMatrixMode(GL_MODELVIEW);
2557 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002558 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002559 const size_t count = layers.size();
2560 for (size_t i=0 ; i<count ; ++i) {
2561 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002562 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002563 }
2564
Mathias Agopian42977342012-08-05 00:40:46 -07002565 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002566
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002567 // back to main framebuffer
2568 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002569 glDeleteFramebuffersOES(1, &name);
2570
2571 *textureName = tname;
2572 *uOut = u;
2573 *vOut = v;
2574 return NO_ERROR;
2575}
2576
2577// ---------------------------------------------------------------------------
2578
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002579status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002580 sp<IMemoryHeap>* heap,
2581 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002582 uint32_t sw, uint32_t sh,
2583 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002584{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002585 ATRACE_CALL();
2586
Mathias Agopian74c40c02010-09-29 13:02:36 -07002587 status_t result = PERMISSION_DENIED;
2588
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002589 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002590 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002591 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002592
2593 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002594 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002595 const uint32_t hw_w = hw->getWidth();
2596 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002597
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002598 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002599 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002600 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002601 return PERMISSION_DENIED;
2602 }
2603
2604 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002605 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002606 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002607 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002608
2609 sw = (!sw) ? hw_w : sw;
2610 sh = (!sh) ? hw_h : sh;
2611 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002612 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002613
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002614// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2615// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002616
Mathias Agopian74c40c02010-09-29 13:02:36 -07002617 // make sure to clear all GL error flags
2618 while ( glGetError() != GL_NO_ERROR ) ;
2619
2620 // create a FBO
2621 GLuint name, tname;
2622 glGenRenderbuffersOES(1, &tname);
2623 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2624 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002625
Mathias Agopian74c40c02010-09-29 13:02:36 -07002626 glGenFramebuffersOES(1, &name);
2627 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2628 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2629 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2630
2631 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002632
Mathias Agopian74c40c02010-09-29 13:02:36 -07002633 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2634
2635 // invert everything, b/c glReadPixel() below will invert the FB
Mathias Agopian135e5892012-09-30 16:43:20 -07002636 GLint viewport[4];
2637 glGetIntegerv(GL_VIEWPORT, viewport);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002638 glViewport(0, 0, sw, sh);
2639 glMatrixMode(GL_PROJECTION);
2640 glPushMatrix();
2641 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002642 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002643 glMatrixMode(GL_MODELVIEW);
2644
2645 // redraw the screen entirely...
2646 glClearColor(0,0,0,1);
2647 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002648
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002649 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002650 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002651 for (size_t i=0 ; i<count ; ++i) {
2652 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002653 const uint32_t z = layer->drawingState().z;
2654 if (z >= minLayerZ && z <= maxLayerZ) {
2655 if (filtering) layer->setFiltering(true);
2656 layer->draw(hw);
2657 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002658 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002659 }
2660
Mathias Agopian74c40c02010-09-29 13:02:36 -07002661 // check for errors and return screen capture
2662 if (glGetError() != GL_NO_ERROR) {
2663 // error while rendering
2664 result = INVALID_OPERATION;
2665 } else {
2666 // allocate shared memory large enough to hold the
2667 // screen capture
2668 sp<MemoryHeapBase> base(
2669 new MemoryHeapBase(size, 0, "screen-capture") );
2670 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002671 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002672 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002673 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002674 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2675 if (glGetError() == GL_NO_ERROR) {
2676 *heap = base;
2677 *w = sw;
2678 *h = sh;
2679 *f = PIXEL_FORMAT_RGBA_8888;
2680 result = NO_ERROR;
2681 }
2682 } else {
2683 result = NO_MEMORY;
2684 }
2685 }
Mathias Agopian135e5892012-09-30 16:43:20 -07002686 glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002687 glMatrixMode(GL_PROJECTION);
2688 glPopMatrix();
2689 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002690 } else {
2691 result = BAD_VALUE;
2692 }
2693
2694 // release FBO resources
2695 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2696 glDeleteRenderbuffersOES(1, &tname);
2697 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002698
Mathias Agopian42977342012-08-05 00:40:46 -07002699 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002700
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002701// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002702
Mathias Agopian74c40c02010-09-29 13:02:36 -07002703 return result;
2704}
2705
2706
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002707status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002708 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002709 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002710 uint32_t sw, uint32_t sh,
2711 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002712{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002713 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002714 return BAD_VALUE;
2715
2716 if (!GLExtensions::getInstance().haveFramebufferObject())
2717 return INVALID_OPERATION;
2718
2719 class MessageCaptureScreen : public MessageBase {
2720 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002721 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002722 sp<IMemoryHeap>* heap;
2723 uint32_t* w;
2724 uint32_t* h;
2725 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002726 uint32_t sw;
2727 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002728 uint32_t minLayerZ;
2729 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002730 status_t result;
2731 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002732 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002733 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002734 uint32_t sw, uint32_t sh,
2735 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002736 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002737 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2738 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2739 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002740 {
2741 }
2742 status_t getResult() const {
2743 return result;
2744 }
2745 virtual bool handler() {
2746 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002747 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002748 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002749 return true;
2750 }
2751 };
2752
2753 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002754 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002755 status_t res = postMessageSync(msg);
2756 if (res == NO_ERROR) {
2757 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2758 }
2759 return res;
2760}
2761
2762// ---------------------------------------------------------------------------
2763
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002764SurfaceFlinger::LayerVector::LayerVector() {
2765}
2766
2767SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2768 : SortedVector<sp<LayerBase> >(rhs) {
2769}
2770
2771int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2772 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002773{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002774 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002775 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2776 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002777
2778 uint32_t ls = l->currentState().layerStack;
2779 uint32_t rs = r->currentState().layerStack;
2780 if (ls != rs)
2781 return ls - rs;
2782
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002783 uint32_t lz = l->currentState().z;
2784 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002785 if (lz != rz)
2786 return lz - rz;
2787
2788 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002789}
2790
2791// ---------------------------------------------------------------------------
2792
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002793SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2794 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002795}
2796
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002797SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2798 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002799 viewport.makeInvalid();
2800 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002801}
2802
2803// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002804
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002805}; // namespace android