blob: 6bd300ddef8dbfb3dbf688bf0a4bb6327ee2c9fc [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 Gennis28378392011-10-12 17:39:00 -070089 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070091 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070094 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070096 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070097 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070098 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070099 mDebugInSwapBuffers(0),
100 mLastSwapBufferTime(0),
101 mDebugInTransaction(0),
102 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700103 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104{
Steve Blocka19954a2012-01-04 20:05:49 +0000105 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
107 // debugging stuff...
108 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 property_get("debug.sf.showupdates", value, "0");
111 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113 property_get("debug.sf.ddms", value, "0");
114 mDebugDDMS = atoi(value);
115 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700116 if (!startDdmConnection()) {
117 // start failed, and DDMS debugging not enabled
118 mDebugDDMS = 0;
119 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700121 ALOGI_IF(mDebugRegion, "showupdates enabled");
122 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800125void SurfaceFlinger::onFirstRef()
126{
127 mEventQueue.init(this);
128
129 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
130
131 // Wait for the main thread to be done with its initialization
132 mReadyToRunBarrier.wait();
133}
134
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136SurfaceFlinger::~SurfaceFlinger()
137{
Mathias Agopiana4912602012-07-12 14:25:33 -0700138 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
139 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
140 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141}
142
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800143void SurfaceFlinger::binderDied(const wp<IBinder>& who)
144{
145 // the window manager died on us. prepare its eulogy.
146
Andy McFadden13a082e2012-08-24 10:16:42 -0700147 // restore initial conditions (default device unblank, etc)
148 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149
150 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700151 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152}
153
Mathias Agopian7e27f052010-05-28 14:22:23 -0700154sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian96f08192010-06-02 23:28:45 -0700156 sp<ISurfaceComposerClient> bclient;
157 sp<Client> client(new Client(this));
158 status_t err = client->initCheck();
159 if (err == NO_ERROR) {
160 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return bclient;
163}
164
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700165sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700166{
167 class DisplayToken : public BBinder {
168 sp<SurfaceFlinger> flinger;
169 virtual ~DisplayToken() {
170 // no more references, this display must be terminated
171 Mutex::Autolock _l(flinger->mStateLock);
172 flinger->mCurrentState.displays.removeItem(this);
173 flinger->setTransactionFlags(eDisplayTransactionNeeded);
174 }
175 public:
176 DisplayToken(const sp<SurfaceFlinger>& flinger)
177 : flinger(flinger) {
178 }
179 };
180
181 sp<BBinder> token = new DisplayToken(this);
182
183 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700184 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700185 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700186 mCurrentState.displays.add(token, info);
187
188 return token;
189}
190
191sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700192 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700193 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
194 return NULL;
195 }
196 return mDefaultDisplays[id];
197}
198
Jamie Gennis9a78c902011-01-12 18:30:40 -0800199sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
200{
201 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
202 return gba;
203}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205void SurfaceFlinger::bootFinished()
206{
207 const nsecs_t now = systemTime();
208 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000209 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700210 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700211
212 // wait patiently for the window manager death
213 const String16 name("window");
214 sp<IBinder> window(defaultServiceManager()->getService(name));
215 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700216 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700217 }
218
219 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700220 // formerly we would just kill the process, but we now ask it to exit so it
221 // can choose where to stop the animation.
222 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}
224
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700225void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
226 class MessageDestroyGLTexture : public MessageBase {
227 GLuint texture;
228 public:
229 MessageDestroyGLTexture(GLuint texture)
230 : texture(texture) {
231 }
232 virtual bool handler() {
233 glDeleteTextures(1, &texture);
234 return true;
235 }
236 };
237 postMessageAsync(new MessageDestroyGLTexture(texture));
238}
239
Mathias Agopiana4912602012-07-12 14:25:33 -0700240status_t SurfaceFlinger::selectConfigForPixelFormat(
241 EGLDisplay dpy,
242 EGLint const* attrs,
243 PixelFormat format,
244 EGLConfig* outConfig)
245{
246 EGLConfig config = NULL;
247 EGLint numConfigs = -1, n=0;
248 eglGetConfigs(dpy, NULL, 0, &numConfigs);
249 EGLConfig* const configs = new EGLConfig[numConfigs];
250 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700251
Mathias Agopiana4912602012-07-12 14:25:33 -0700252 for (int i=0 ; i<n ; i++) {
253 EGLint nativeVisualId = 0;
254 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
255 if (nativeVisualId>0 && format == nativeVisualId) {
256 *outConfig = configs[i];
257 delete [] configs;
258 return NO_ERROR;
259 }
260 }
261 delete [] configs;
262 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263}
264
Mathias Agopiana4912602012-07-12 14:25:33 -0700265EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
266 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
267 // it is to be used with WIFI displays
268 EGLConfig config;
269 EGLint dummy;
270 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700271
Mathias Agopiana4912602012-07-12 14:25:33 -0700272 EGLint attribs[] = {
273 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Jesse Hallf21cffa2012-09-19 21:00:49 -0700274 // The rest of the attributes must be in this order and at the end
275 // of the list; we rely on that for fallback searches below.
Mathias Agopiancde87a32012-09-13 14:09:01 -0700276 EGL_RED_SIZE, 8,
277 EGL_GREEN_SIZE, 8,
278 EGL_BLUE_SIZE, 8,
Mathias Agopiana4912602012-07-12 14:25:33 -0700279 EGL_RECORDABLE_ANDROID, EGL_TRUE,
280 EGL_NONE
281 };
282 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700283 if (!err)
284 goto success;
285
286 // maybe we failed because of EGL_RECORDABLE_ANDROID
287 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
288 attribs[NELEM(attribs) - 3] = EGL_NONE;
289 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
290 if (!err)
291 goto success;
292
293 // allow less than 24-bit color; the non-gpu-accelerated emulator only
294 // supports 16-bit color
295 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
296 attribs[NELEM(attribs) - 9] = EGL_NONE;
297 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
298 if (!err)
299 goto success;
300
301 // this EGL is too lame for Android
302 ALOGE("no suitable EGLConfig found, giving up");
303
304 return 0;
305
306success:
307 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700308 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 return config;
310}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311
Mathias Agopiana4912602012-07-12 14:25:33 -0700312EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
313 // Also create our EGLContext
314 EGLint contextAttributes[] = {
315#ifdef EGL_IMG_context_priority
316#ifdef HAS_CONTEXT_PRIORITY
317#warning "using EGL_IMG_context_priority"
318 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
319#endif
320#endif
321 EGL_NONE, EGL_NONE
322 };
323 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
324 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
325 return ctxt;
326}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327
Mathias Agopiancde87a32012-09-13 14:09:01 -0700328void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700329 GLExtensions& extensions(GLExtensions::getInstance());
330 extensions.initWithGLStrings(
331 glGetString(GL_VENDOR),
332 glGetString(GL_RENDERER),
333 glGetString(GL_VERSION),
334 glGetString(GL_EXTENSIONS),
335 eglQueryString(display, EGL_VENDOR),
336 eglQueryString(display, EGL_VERSION),
337 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700338
Mathias Agopiana4912602012-07-12 14:25:33 -0700339 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
340 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700343 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 glShadeModel(GL_FLAT);
346 glDisable(GL_DITHER);
347 glDisable(GL_CULL_FACE);
348
Mathias Agopiana4912602012-07-12 14:25:33 -0700349 struct pack565 {
350 inline uint16_t operator() (int r, int g, int b) const {
351 return (r<<11)|(g<<5)|b;
352 }
353 } pack565;
354
Jamie Gennis9575f602011-10-07 14:51:16 -0700355 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
356 glGenTextures(1, &mProtectedTexName);
357 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
359 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
360 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
361 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
362 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
363 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364
Mathias Agopiana4912602012-07-12 14:25:33 -0700365 // print some debugging info
366 EGLint r,g,b,a;
367 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
368 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
369 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
370 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
371 ALOGI("EGL informations:");
372 ALOGI("vendor : %s", extensions.getEglVendor());
373 ALOGI("version : %s", extensions.getEglVersion());
374 ALOGI("extensions: %s", extensions.getEglExtension());
375 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
376 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
377 ALOGI("OpenGL ES informations:");
378 ALOGI("vendor : %s", extensions.getVendor());
379 ALOGI("renderer : %s", extensions.getRenderer());
380 ALOGI("version : %s", extensions.getVersion());
381 ALOGI("extensions: %s", extensions.getExtension());
382 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
383 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
384}
385
Mathias Agopiana4912602012-07-12 14:25:33 -0700386status_t SurfaceFlinger::readyToRun()
387{
388 ALOGI( "SurfaceFlinger's main thread ready to run. "
389 "Initializing graphics H/W...");
390
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700391 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700392 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
393 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700394
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700395 // Initialize the H/W composer object. There may or may not be an
396 // actual hardware composer underneath.
397 mHwc = new HWComposer(this,
398 *static_cast<HWComposer::EventHandler *>(this));
399
Mathias Agopiana4912602012-07-12 14:25:33 -0700400 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700401 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700402 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
403 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700404
Mathias Agopianda27af92012-09-13 18:17:13 -0700405 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
406 "couldn't create EGLContext");
407
Mathias Agopiancde87a32012-09-13 14:09:01 -0700408 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700409 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700410 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700411 mDefaultDisplays[i] = new BBinder();
Mathias Agopianf5a33922012-09-19 18:16:22 -0700412 wp<IBinder> token = mDefaultDisplays[i];
Mathias Agopianf5a33922012-09-19 18:16:22 -0700413
414 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700415 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
416 mCurrentState.displays.add(token, DisplayDeviceState(type));
Mathias Agopianf5a33922012-09-19 18:16:22 -0700417 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
418 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
419 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
420 sp<DisplayDevice> hw = new DisplayDevice(this,
421 type, token, stc, fbs, mEGLConfig);
422
423 if (i > DisplayDevice::DISPLAY_PRIMARY) {
424 // FIXME: currently we don't really handle blank/unblank
425 // for displays other than the main display, so we always
426 // assume a connected display is unblanked.
427 hw->acquireScreen();
428 }
429 mDisplays.add(token, hw);
430 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700431 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700432
Mathias Agopianf5a33922012-09-19 18:16:22 -0700433 // we need a GL context current in a few places, when initializing
434 // OpenGL ES (see below), or creating a layer,
435 // or when a texture is (asynchronously) destroyed, and for that
436 // we need a valid surface, so it's convenient to use the main display
437 // for that.
438 sp<const DisplayDevice> hw = getDefaultDisplayDevice();
Mathias Agopiancde87a32012-09-13 14:09:01 -0700439
Mathias Agopiana4912602012-07-12 14:25:33 -0700440 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700441 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
442 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800443
Mathias Agopian028508c2012-07-25 21:12:12 -0700444 // start the EventThread
445 mEventThread = new EventThread(this);
446 mEventQueue.setEventThread(mEventThread);
447
Mathias Agopian92a979a2012-08-02 18:32:23 -0700448 // initialize our drawing state
449 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700450
Mathias Agopiancde87a32012-09-13 14:09:01 -0700451
Mathias Agopiana4912602012-07-12 14:25:33 -0700452 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800453 mReadyToRunBarrier.open();
454
Andy McFadden13a082e2012-08-24 10:16:42 -0700455 // set initial conditions (e.g. unblank default device)
456 initializeDisplays();
457
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700458 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700459 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700460
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 return NO_ERROR;
462}
463
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700464int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
465 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
466 type : mHwc->allocateDisplayId();
467}
468
Mathias Agopiana67e4182012-06-19 17:26:12 -0700469void SurfaceFlinger::startBootAnim() {
470 // start boot animation
471 property_set("service.bootanim.exit", "0");
472 property_set("ctl.start", "bootanim");
473}
474
Mathias Agopiana4912602012-07-12 14:25:33 -0700475uint32_t SurfaceFlinger::getMaxTextureSize() const {
476 return mMaxTextureSize;
477}
478
479uint32_t SurfaceFlinger::getMaxViewportDims() const {
480 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
481 mMaxViewportDims[0] : mMaxViewportDims[1];
482}
483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800485
Jamie Gennis582270d2011-08-17 18:19:00 -0700486bool SurfaceFlinger::authenticateSurfaceTexture(
487 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800488 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700489 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800490
491 // Check the visible layer list for the ISurface
492 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
493 size_t count = currentLayers.size();
494 for (size_t i=0 ; i<count ; i++) {
495 const sp<LayerBase>& layer(currentLayers[i]);
496 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700497 if (lbc != NULL) {
498 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
499 if (lbcBinder == surfaceTextureBinder) {
500 return true;
501 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800502 }
503 }
504
505 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700506 // SurfaceTexture gets destroyed before all the clients are done using it,
507 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800508 // will instead fail later on when the client tries to use the surface,
509 // which should be reported as "surface XYZ returned an -ENODEV". The
510 // purgatorized layers are no less authentic than the visible ones, so this
511 // should not cause any harm.
512 size_t purgatorySize = mLayerPurgatory.size();
513 for (size_t i=0 ; i<purgatorySize ; i++) {
514 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
515 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700516 if (lbc != NULL) {
517 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
518 if (lbcBinder == surfaceTextureBinder) {
519 return true;
520 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800521 }
522 }
523
524 return false;
525}
526
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700527status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700528 int32_t type = BAD_VALUE;
529 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
530 if (display == mDefaultDisplays[i]) {
531 type = i;
532 break;
533 }
534 }
535
536 if (type < 0) {
537 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700538 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700539
540 const HWComposer& hwc(getHwComposer());
Mathias Agopian4c0751a2012-09-20 21:28:12 -0700541 if (!hwc.isConnected(type)) {
542 return NAME_NOT_FOUND;
543 }
544
Mathias Agopian1604f772012-09-18 21:54:42 -0700545 float xdpi = hwc.getDpiX(type);
546 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700547
548 // TODO: Not sure if display density should handled by SF any longer
549 class Density {
550 static int getDensityFromProperty(char const* propName) {
551 char property[PROPERTY_VALUE_MAX];
552 int density = 0;
553 if (property_get(propName, property, NULL) > 0) {
554 density = atoi(property);
555 }
556 return density;
557 }
558 public:
559 static int getEmuDensity() {
560 return getDensityFromProperty("qemu.sf.lcd_density"); }
561 static int getBuildDensity() {
562 return getDensityFromProperty("ro.sf.lcd_density"); }
563 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700564
565 if (type == DisplayDevice::DISPLAY_PRIMARY) {
566 // The density of the device is provided by a build property
567 float density = Density::getBuildDensity() / 160.0f;
568 if (density == 0) {
569 // the build doesn't provide a density -- this is wrong!
570 // use xdpi instead
571 ALOGE("ro.sf.lcd_density must be defined as a build property");
572 density = xdpi / 160.0f;
573 }
574 if (Density::getEmuDensity()) {
575 // if "qemu.sf.lcd_density" is specified, it overrides everything
576 xdpi = ydpi = density = Density::getEmuDensity();
577 density /= 160.0f;
578 }
579 info->density = density;
580
581 // TODO: this needs to go away (currently needed only by webkit)
582 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
583 info->orientation = hw->getOrientation();
584 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
585 } else {
586 // TODO: where should this value come from?
587 static const int TV_DENSITY = 213;
588 info->density = TV_DENSITY / 160.0f;
589 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700590 }
591
Mathias Agopian1604f772012-09-18 21:54:42 -0700592 info->w = hwc.getWidth(type);
593 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700594 info->xdpi = xdpi;
595 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700596 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Mathias Agopian888c8222012-08-04 21:10:38 -0700597 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700598}
599
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800600// ----------------------------------------------------------------------------
601
602sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800603 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700604}
605
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700606void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700607
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700608 sp<IBinder> token;
609 { // scope for the lock
610 Mutex::Autolock _l(mStateLock);
611 token = mExtDisplayToken;
612 }
613
614 if (token == 0) {
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700615 token = createDisplay(String8("Display from connectDisplay"));
Mathias Agopian3094df32012-06-18 18:06:45 -0700616 }
617
618 { // scope for the lock
619 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700620 if (surface == 0) {
621 // release our current display. we're guarantee to have
622 // a reference to it (token), while we hold the lock
623 mExtDisplayToken = 0;
624 } else {
625 mExtDisplayToken = token;
626 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700627
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700628 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
629 info.surface = surface;
630 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700631 }
632}
633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800635
636void SurfaceFlinger::waitForEvent() {
637 mEventQueue.waitMessage();
638}
639
640void SurfaceFlinger::signalTransaction() {
641 mEventQueue.invalidate();
642}
643
644void SurfaceFlinger::signalLayerUpdate() {
645 mEventQueue.invalidate();
646}
647
648void SurfaceFlinger::signalRefresh() {
649 mEventQueue.refresh();
650}
651
652status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
653 nsecs_t reltime, uint32_t flags) {
654 return mEventQueue.postMessage(msg, reltime);
655}
656
657status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
658 nsecs_t reltime, uint32_t flags) {
659 status_t res = mEventQueue.postMessage(msg, reltime);
660 if (res == NO_ERROR) {
661 msg->wait();
662 }
663 return res;
664}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
Mathias Agopian4fec8732012-06-29 14:12:52 -0700666bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 return true;
669}
670
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700671void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700672 if (mEventThread == NULL) {
673 // This is a temporary workaround for b/7145521. A non-null pointer
674 // does not mean EventThread has finished initializing, so this
675 // is not a correct fix.
676 ALOGW("WARNING: EventThread not started, ignoring vsync");
677 return;
678 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700679 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
680 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700681 mEventThread->onVSyncReceived(type, timestamp);
682 }
683}
684
685void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
686 if (mEventThread == NULL) {
687 // This is a temporary workaround for b/7145521. A non-null pointer
688 // does not mean EventThread has finished initializing, so this
689 // is not a correct fix.
690 ALOGW("WARNING: EventThread not started, ignoring hotplug");
691 return;
692 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700693
Mathias Agopian148994e2012-09-19 17:31:36 -0700694 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700695 Mutex::Autolock _l(mStateLock);
696 if (connected == false) {
697 mCurrentState.displays.removeItem(mDefaultDisplays[type]);
698 } else {
699 DisplayDeviceState info((DisplayDevice::DisplayType)type);
700 mCurrentState.displays.add(mDefaultDisplays[type], info);
701 }
702 setTransactionFlags(eDisplayTransactionNeeded);
703
Mathias Agopian148994e2012-09-19 17:31:36 -0700704 // we should only receive DisplayDevice::DisplayType from the vsync callback
705 mEventThread->onHotplugReceived(type, connected);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700706 }
Mathias Agopian86303202012-07-24 22:46:10 -0700707}
708
709void SurfaceFlinger::eventControl(int event, int enabled) {
710 getHwComposer().eventControl(event, enabled);
711}
712
Mathias Agopian4fec8732012-06-29 14:12:52 -0700713void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800714 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800715 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700716 case MessageQueue::INVALIDATE:
717 handleMessageTransaction();
718 handleMessageInvalidate();
719 signalRefresh();
720 break;
721 case MessageQueue::REFRESH:
722 handleMessageRefresh();
723 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800724 }
725}
726
Mathias Agopian4fec8732012-06-29 14:12:52 -0700727void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700728 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700729 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700730 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700731 }
732}
733
734void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700735 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700736 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700737}
738
739void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700740 ATRACE_CALL();
741 preComposition();
742 rebuildLayerStacks();
743 setUpHWComposer();
744 doDebugFlashRegions();
745 doComposition();
746 postComposition();
747}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700748
Mathias Agopiancd60f992012-08-16 16:28:27 -0700749void SurfaceFlinger::doDebugFlashRegions()
750{
751 // is debugging enabled
752 if (CC_LIKELY(!mDebugRegion))
753 return;
754
755 const bool repaintEverything = mRepaintEverything;
756 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
757 const sp<DisplayDevice>& hw(mDisplays[dpy]);
758 if (hw->canDraw()) {
759 // transform the dirty region into this screen's coordinate space
760 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
761 if (!dirtyRegion.isEmpty()) {
762 // redraw the whole screen
763 doComposeSurfaces(hw, Region(hw->bounds()));
764
765 // and draw the dirty region
766 glDisable(GL_TEXTURE_EXTERNAL_OES);
767 glDisable(GL_TEXTURE_2D);
768 glDisable(GL_BLEND);
769 glColor4f(1, 0, 1, 1);
770 const int32_t height = hw->getHeight();
771 Region::const_iterator it = dirtyRegion.begin();
772 Region::const_iterator const end = dirtyRegion.end();
773 while (it != end) {
774 const Rect& r = *it++;
775 GLfloat vertices[][2] = {
776 { r.left, height - r.top },
777 { r.left, height - r.bottom },
778 { r.right, height - r.bottom },
779 { r.right, height - r.top }
780 };
781 glVertexPointer(2, GL_FLOAT, 0, vertices);
782 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
783 }
784 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700785 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 }
787 }
788 }
789
790 postFramebuffer();
791
792 if (mDebugRegion > 1) {
793 usleep(mDebugRegion * 1000);
794 }
795}
796
797void SurfaceFlinger::preComposition()
798{
799 bool needExtraInvalidate = false;
800 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
801 const size_t count = currentLayers.size();
802 for (size_t i=0 ; i<count ; i++) {
803 if (currentLayers[i]->onPreComposition()) {
804 needExtraInvalidate = true;
805 }
806 }
807 if (needExtraInvalidate) {
808 signalLayerUpdate();
809 }
810}
811
812void SurfaceFlinger::postComposition()
813{
814 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
815 const size_t count = currentLayers.size();
816 for (size_t i=0 ; i<count ; i++) {
817 currentLayers[i]->onPostComposition();
818 }
819}
820
821void SurfaceFlinger::rebuildLayerStacks() {
822 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700823 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700824 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700825 mVisibleRegionsDirty = false;
826 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700827
Mathias Agopian87baae12012-07-31 12:38:26 -0700828 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700829 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700830 Region opaqueRegion;
831 Region dirtyRegion;
832 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700833 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700834 const Transform& tr(hw->getTransform());
835 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836 if (hw->canDraw()) {
837 SurfaceFlinger::computeVisibleRegions(currentLayers,
838 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700839
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700840 const size_t count = currentLayers.size();
841 for (size_t i=0 ; i<count ; i++) {
842 const sp<LayerBase>& layer(currentLayers[i]);
843 const Layer::State& s(layer->drawingState());
844 if (s.layerStack == hw->getLayerStack()) {
845 Region visibleRegion(tr.transform(layer->visibleRegion));
846 visibleRegion.andSelf(bounds);
847 if (!visibleRegion.isEmpty()) {
848 layersSortedByZ.add(layer);
849 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700850 }
851 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700852 }
Mathias Agopian42977342012-08-05 00:40:46 -0700853 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700854 hw->undefinedRegion.set(bounds);
855 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
856 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700857 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700858 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700860
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700862 HWComposer& hwc(getHwComposer());
863 if (hwc.initCheck() == NO_ERROR) {
864 // build the h/w work list
865 const bool workListsDirty = mHwWorkListDirty;
866 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700867 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700868 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700869 const int32_t id = hw->getHwcDisplayId();
870 if (id >= 0) {
871 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700872 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700873 const size_t count = currentLayers.size();
874 if (hwc.createWorkList(id, count) >= 0) {
875 HWComposer::LayerListIterator cur = hwc.begin(id);
876 const HWComposer::LayerListIterator end = hwc.end(id);
877 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
878 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700879
Mathias Agopiane60b0682012-08-21 23:34:09 -0700880 if (CC_UNLIKELY(workListsDirty)) {
881 layer->setGeometry(hw, *cur);
882 if (mDebugDisableHWC || mDebugRegion) {
883 cur->setSkip(true);
884 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700885 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700886
Mathias Agopiane60b0682012-08-21 23:34:09 -0700887 /*
888 * update the per-frame h/w composer data for each layer
889 * and build the transparent region of the FB
890 */
891 layer->setPerFrameData(hw, *cur);
892 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700893 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700894 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700895 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700896 status_t err = hwc.prepare();
897 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
898 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700899}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700900
Mathias Agopiancd60f992012-08-16 16:28:27 -0700901void SurfaceFlinger::doComposition() {
902 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700903 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700904 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700905 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906 if (hw->canDraw()) {
907 // transform the dirty region into this screen's coordinate space
908 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
909 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700910 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700912 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913 hw->dirtyRegion.clear();
914 hw->flip(hw->swapRegion);
915 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700916 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700917 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700918 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700919 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700920 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700921}
922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923void SurfaceFlinger::postFramebuffer()
924{
Mathias Agopian841cde52012-03-01 15:44:37 -0800925 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800926
Mathias Agopiana44b0412011-10-16 18:46:35 -0700927 const nsecs_t now = systemTime();
928 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700929
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700930 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700931 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700932 if (!hwc.supportsFramebufferTarget()) {
933 // EGL spec says:
934 // "surface must be bound to the calling thread's current context,
935 // for the current rendering API."
936 DisplayDevice::makeCurrent(mEGLDisplay,
937 getDefaultDisplayDevice(), mEGLContext);
938 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700939 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700940 }
941
Mathias Agopian92a979a2012-08-02 18:32:23 -0700942 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700943 sp<const DisplayDevice> hw(mDisplays[dpy]);
944 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700945 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700947 int32_t id = hw->getHwcDisplayId();
948 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700949 HWComposer::LayerListIterator cur = hwc.begin(id);
950 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700951 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700952 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700953 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700954 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700955 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700956 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700957 }
Jesse Hallef194142012-06-14 14:45:17 -0700958 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800959 }
960
Mathias Agopiana44b0412011-10-16 18:46:35 -0700961 mLastSwapBufferTime = systemTime() - now;
962 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963}
964
Mathias Agopian87baae12012-07-31 12:38:26 -0700965void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966{
Mathias Agopian841cde52012-03-01 15:44:37 -0800967 ATRACE_CALL();
968
Mathias Agopianca4d3602011-05-19 15:38:14 -0700969 Mutex::Autolock _l(mStateLock);
970 const nsecs_t now = systemTime();
971 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972
Mathias Agopianca4d3602011-05-19 15:38:14 -0700973 // Here we're guaranteed that some transaction flags are set
974 // so we can call handleTransactionLocked() unconditionally.
975 // We call getTransactionFlags(), which will also clear the flags,
976 // with mStateLock held to guarantee that mCurrentState won't change
977 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700978
Mathias Agopiane57f2922012-08-09 16:29:12 -0700979 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700980 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700981
Mathias Agopianca4d3602011-05-19 15:38:14 -0700982 mLastTransactionTime = systemTime() - now;
983 mDebugInTransaction = 0;
984 invalidateHwcGeometry();
985 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700986}
987
Mathias Agopian87baae12012-07-31 12:38:26 -0700988void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700989{
990 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 const size_t count = currentLayers.size();
992
993 /*
994 * Traversal of the children
995 * (perform the transaction for each of them if needed)
996 */
997
Mathias Agopian3559b072012-08-15 13:46:03 -0700998 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001000 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1002 if (!trFlags) continue;
1003
1004 const uint32_t flags = layer->doTransaction(0);
1005 if (flags & Layer::eVisibleRegion)
1006 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 }
1008 }
1009
1010 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001011 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 */
1013
Mathias Agopiane57f2922012-08-09 16:29:12 -07001014 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001015 // here we take advantage of Vector's copy-on-write semantics to
1016 // improve performance by skipping the transaction entirely when
1017 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001018 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1019 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001020 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001022 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001023 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001024
1025 // find the displays that were removed
1026 // (ie: in drawing state but not in current state)
1027 // also handle displays that changed
1028 // (ie: displays that are in both lists)
1029 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001030 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1031 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001032 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001033 if (!draw[i].isMainDisplay()) {
1034 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001035 } else {
1036 ALOGW("trying to remove the main display");
1037 }
1038 } else {
1039 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001040 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001041 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001042 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001043 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001044 // recreating the DisplayDevice, so we just remove it
1045 // from the drawing state, so that it get re-added
1046 // below.
1047 mDisplays.removeItem(display);
1048 mDrawingState.displays.removeItemsAt(i);
1049 dc--; i--;
1050 // at this point we must loop to the next item
1051 continue;
1052 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001053
Mathias Agopian93997a82012-08-29 17:30:36 -07001054 const sp<DisplayDevice>& disp(getDisplayDevice(display));
1055 if (disp != NULL) {
1056 if (state.layerStack != draw[i].layerStack) {
1057 disp->setLayerStack(state.layerStack);
1058 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001059 if ((state.orientation != draw[i].orientation)
1060 || (state.viewport != draw[i].viewport)
1061 || (state.frame != draw[i].frame))
1062 {
1063 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001064 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001065 }
Andy McFadden69052052012-09-14 16:10:11 -07001066
1067 // Walk through all the layers in currentLayers,
1068 // and update their transform hint.
1069 //
1070 // TODO: we could be much more clever about which
1071 // layers we touch and how often we do these updates
1072 // (e.g. only touch the layers associated with this
1073 // display, and only on a rotation).
1074 for (size_t i = 0; i < count; i++) {
1075 const sp<LayerBase>& layerBase = currentLayers[i];
1076 layerBase->updateTransformHint();
1077 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001078 }
1079 }
1080 }
1081
1082 // find displays that were added
1083 // (ie: in current state but not in drawing state)
1084 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001085 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001086 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001087
1088 sp<FramebufferSurface> fbs;
1089 sp<SurfaceTextureClient> stc;
1090 if (!state.isVirtualDisplay()) {
1091
1092 ALOGE_IF(state.surface!=NULL,
1093 "adding a supported display, but rendering "
1094 "surface is provided (%p), ignoring it",
1095 state.surface.get());
1096
1097 // for supported (by hwc) displays we provide our
1098 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001099 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001100 stc = new SurfaceTextureClient(
1101 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1102 } else {
1103 if (state.surface != NULL) {
1104 stc = new SurfaceTextureClient(state.surface);
1105 }
1106 }
1107
1108 const wp<IBinder>& display(curr.keyAt(i));
1109 if (stc != NULL) {
1110 sp<DisplayDevice> hw = new DisplayDevice(this,
1111 state.type, display, stc, fbs, mEGLConfig);
1112 hw->setLayerStack(state.layerStack);
1113 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001114 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001115 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001116 mDisplays.add(display, hw);
1117 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1118 // notify the system that this display is now up
1119 // (note onScreenAcquired() is safe to call from
1120 // here because we're in the main thread)
1121 onScreenAcquired(hw);
1122 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001123 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001124 }
1125 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001127 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128
Mathias Agopian3559b072012-08-15 13:46:03 -07001129 /*
1130 * Perform our own transaction if needed
1131 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001132
Mathias Agopiancd60f992012-08-16 16:28:27 -07001133 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1134 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001135 // layers have been added
1136 mVisibleRegionsDirty = true;
1137 }
1138
1139 // some layers might have been removed, so
1140 // we need to update the regions they're exposing.
1141 if (mLayersRemoved) {
1142 mLayersRemoved = false;
1143 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001144 const size_t count = previousLayers.size();
1145 for (size_t i=0 ; i<count ; i++) {
1146 const sp<LayerBase>& layer(previousLayers[i]);
1147 if (currentLayers.indexOf(layer) < 0) {
1148 // this layer is not visible anymore
1149 // TODO: we could traverse the tree from front to back and
1150 // compute the actual visible region
1151 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001152 const Layer::State& s(layer->drawingState());
1153 Region visibleReg = s.transform.transform(
1154 Region(Rect(s.active.w, s.active.h)));
1155 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001156 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 }
1159
1160 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001161}
1162
1163void SurfaceFlinger::commitTransaction()
1164{
1165 if (!mLayersPendingRemoval.isEmpty()) {
1166 // Notify removed layers now that they can't be drawn from
1167 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1168 mLayersPendingRemoval[i]->onRemoved();
1169 }
1170 mLayersPendingRemoval.clear();
1171 }
1172
1173 mDrawingState = mCurrentState;
1174 mTransationPending = false;
1175 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176}
1177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001179 const LayerVector& currentLayers, uint32_t layerStack,
1180 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181{
Mathias Agopian841cde52012-03-01 15:44:37 -08001182 ATRACE_CALL();
1183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 Region aboveOpaqueLayers;
1185 Region aboveCoveredLayers;
1186 Region dirty;
1187
Mathias Agopian87baae12012-07-31 12:38:26 -07001188 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
1190 size_t i = currentLayers.size();
1191 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001192 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193
1194 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001195 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196
Mathias Agopian87baae12012-07-31 12:38:26 -07001197 // only consider the layers on the given later stack
1198 if (s.layerStack != layerStack)
1199 continue;
1200
Mathias Agopianab028732010-03-16 16:41:46 -07001201 /*
1202 * opaqueRegion: area of a surface that is fully opaque.
1203 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001205
1206 /*
1207 * visibleRegion: area of a surface that is visible on screen
1208 * and not fully transparent. This is essentially the layer's
1209 * footprint minus the opaque regions above it.
1210 * Areas covered by a translucent surface are considered visible.
1211 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001213
1214 /*
1215 * coveredRegion: area of a surface that is covered by all
1216 * visible regions above it (which includes the translucent areas).
1217 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001219
1220
1221 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001222 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001223 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001224 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001226 if (!visibleRegion.isEmpty()) {
1227 // Remove the transparent area from the visible region
1228 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001229 Region transparentRegionScreen;
1230 const Transform tr(s.transform);
1231 if (tr.transformed()) {
1232 if (tr.preserveRects()) {
1233 // transform the transparent region
1234 transparentRegionScreen = tr.transform(s.transparentRegion);
1235 } else {
1236 // transformation too complex, can't do the
1237 // transparent region optimization.
1238 transparentRegionScreen.clear();
1239 }
1240 } else {
1241 transparentRegionScreen = s.transparentRegion;
1242 }
1243 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001244 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245
Mathias Agopianab028732010-03-16 16:41:46 -07001246 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001247 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001248 if (s.alpha==255 && !translucent &&
1249 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1250 // the opaque region is the layer's footprint
1251 opaqueRegion = visibleRegion;
1252 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 }
1254 }
1255
Mathias Agopianab028732010-03-16 16:41:46 -07001256 // Clip the covered region to the visible region
1257 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1258
1259 // Update aboveCoveredLayers for next (lower) layer
1260 aboveCoveredLayers.orSelf(visibleRegion);
1261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 // subtract the opaque region covered by the layers above us
1263 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264
1265 // compute this layer's dirty region
1266 if (layer->contentDirty) {
1267 // we need to invalidate the whole region
1268 dirty = visibleRegion;
1269 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001270 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 layer->contentDirty = false;
1272 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001273 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001274 * the exposed region consists of two components:
1275 * 1) what's VISIBLE now and was COVERED before
1276 * 2) what's EXPOSED now less what was EXPOSED before
1277 *
1278 * note that (1) is conservative, we start with the whole
1279 * visible region but only keep what used to be covered by
1280 * something -- which mean it may have been exposed.
1281 *
1282 * (2) handles areas that were not covered by anything but got
1283 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001284 */
Mathias Agopianab028732010-03-16 16:41:46 -07001285 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001286 const Region oldVisibleRegion = layer->visibleRegion;
1287 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001288 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1289 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 }
1291 dirty.subtractSelf(aboveOpaqueLayers);
1292
1293 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001294 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295
Mathias Agopianab028732010-03-16 16:41:46 -07001296 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 // Store the visible region is screen space
1300 layer->setVisibleRegion(visibleRegion);
1301 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 }
1303
Mathias Agopian87baae12012-07-31 12:38:26 -07001304 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305}
1306
Mathias Agopian87baae12012-07-31 12:38:26 -07001307void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1308 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001309 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001310 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1311 if (hw->getLayerStack() == layerStack) {
1312 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001313 }
1314 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001315}
1316
1317void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001319 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320
Mathias Agopian4fec8732012-06-29 14:12:52 -07001321 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001322 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001323 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001324 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001325 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001326 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001327 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001328 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001329 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001330
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001331 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332}
1333
Mathias Agopianad456f92011-01-13 17:53:01 -08001334void SurfaceFlinger::invalidateHwcGeometry()
1335{
1336 mHwWorkListDirty = true;
1337}
1338
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001339
Mathias Agopiancd60f992012-08-16 16:28:27 -07001340void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001341 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342{
Mathias Agopian87baae12012-07-31 12:38:26 -07001343 Region dirtyRegion(inDirtyRegion);
1344
Mathias Agopianb8a55602009-06-26 19:06:36 -07001345 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001346 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347
Mathias Agopian42977342012-08-05 00:40:46 -07001348 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001349 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001350 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1351 // takes a rectangle, we must make sure to update that whole
1352 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001353 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001354 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001355 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001356 // We need to redraw the rectangle that will be updated
1357 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001358 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001359 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001360 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001361 } else {
1362 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001363 dirtyRegion.set(hw->bounds());
1364 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 }
1366 }
1367
Mathias Agopiancd60f992012-08-16 16:28:27 -07001368 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001370 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001371 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001372
1373 // swap buffers (presentation)
1374 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375}
1376
Mathias Agopiancd60f992012-08-16 16:28:27 -07001377void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001378{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001379 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001380 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001381 HWComposer::LayerListIterator cur = hwc.begin(id);
1382 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001383
Mathias Agopian85d751c2012-08-29 16:59:24 -07001384 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1385 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001386 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001387
1388 // set the frame buffer
1389 glMatrixMode(GL_MODELVIEW);
1390 glLoadIdentity();
1391
1392 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001393 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001394 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001395 // when using overlays, we assume a fully transparent framebuffer
1396 // NOTE: we could reduce how much we need to clear, for instance
1397 // remove where there are opaque FB layers. however, on some
1398 // GPUs doing a "clean slate" glClear might be more efficient.
1399 // We'll revisit later if needed.
1400 glClearColor(0, 0, 0, 0);
1401 glClear(GL_COLOR_BUFFER_BIT);
1402 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001403 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001404 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001405 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001406 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001407 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001408 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001409 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001410 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001411
Mathias Agopian85d751c2012-08-29 16:59:24 -07001412 /*
1413 * and then, render the layers targeted at the framebuffer
1414 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001415
Mathias Agopian85d751c2012-08-29 16:59:24 -07001416 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1417 const size_t count = layers.size();
1418 const Transform& tr = hw->getTransform();
1419 if (cur != end) {
1420 // we're using h/w composer
1421 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001422 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001423 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001424 if (!clip.isEmpty()) {
1425 switch (cur->getCompositionType()) {
1426 case HWC_OVERLAY: {
1427 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1428 && i
1429 && layer->isOpaque()
1430 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001431 // never clear the very first layer since we're
1432 // guaranteed the FB is already cleared
1433 layer->clearWithOpenGL(hw, clip);
1434 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001435 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001436 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001437 case HWC_FRAMEBUFFER: {
1438 layer->draw(hw, clip);
1439 break;
1440 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001441 case HWC_FRAMEBUFFER_TARGET: {
1442 // this should not happen as the iterator shouldn't
1443 // let us get there.
1444 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1445 break;
1446 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001447 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001448 }
1449 layer->setAcquireFence(hw, *cur);
1450 }
1451 } else {
1452 // we're not using h/w composer
1453 for (size_t i=0 ; i<count ; ++i) {
1454 const sp<LayerBase>& layer(layers[i]);
1455 const Region clip(dirty.intersect(
1456 tr.transform(layer->visibleRegion)));
1457 if (!clip.isEmpty()) {
1458 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001459 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001460 }
1461 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462}
1463
Mathias Agopian55801e42012-08-27 18:54:24 -07001464void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1465 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001467 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001468 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001469 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001470 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001471
Mathias Agopian55801e42012-08-27 18:54:24 -07001472 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001473 Region::const_iterator it = region.begin();
1474 Region::const_iterator const end = region.end();
1475 while (it != end) {
1476 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001477 GLfloat vertices[][2] = {
1478 { r.left, height - r.top },
1479 { r.left, height - r.bottom },
1480 { r.right, height - r.bottom },
1481 { r.right, height - r.top }
1482 };
1483 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001484 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1485 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486}
1487
Mathias Agopian96f08192010-06-02 23:28:45 -07001488ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1489 const sp<LayerBaseClient>& lbc)
1490{
Mathias Agopian96f08192010-06-02 23:28:45 -07001491 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001492 size_t name = client->attachLayer(lbc);
1493
Mathias Agopian96f08192010-06-02 23:28:45 -07001494 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001495 Mutex::Autolock _l(mStateLock);
1496 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001497
Mathias Agopian4f113742011-05-03 16:21:41 -07001498 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001499}
1500
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001501status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502{
1503 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001504 status_t err = purgatorizeLayer_l(layer);
1505 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001506 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001507 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508}
1509
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001510status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511{
1512 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1513 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001514 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515 return NO_ERROR;
1516 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001517 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518}
1519
Mathias Agopian9a112062009-04-17 19:36:26 -07001520status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1521{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001522 // First add the layer to the purgatory list, which makes sure it won't
1523 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001524 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001525 if (err >= 0) {
1526 mLayerPurgatory.add(layerBase);
1527 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001528
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001529 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001530
Mathias Agopian3d579642009-06-04 18:46:21 -07001531 // it's possible that we don't find a layer, because it might
1532 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001533 // from the user because there is a race between Client::destroySurface(),
1534 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001535 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1536}
1537
Mathias Agopiandea20b12011-05-03 17:04:02 -07001538uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1539{
1540 return android_atomic_release_load(&mTransactionFlags);
1541}
1542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1544{
1545 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1546}
1547
Mathias Agopianbb641242010-05-18 17:06:55 -07001548uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549{
1550 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1551 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001552 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 }
1554 return old;
1555}
1556
Mathias Agopian8b33f032012-07-24 20:43:54 -07001557void SurfaceFlinger::setTransactionState(
1558 const Vector<ComposerState>& state,
1559 const Vector<DisplayState>& displays,
1560 uint32_t flags)
1561{
Mathias Agopian698c0872011-06-28 19:09:31 -07001562 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001563 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001564
1565 size_t count = displays.size();
1566 for (size_t i=0 ; i<count ; i++) {
1567 const DisplayState& s(displays[i]);
1568 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001569 }
1570
Mathias Agopiane57f2922012-08-09 16:29:12 -07001571 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001572 for (size_t i=0 ; i<count ; i++) {
1573 const ComposerState& s(state[i]);
1574 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001575 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001576 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001577
Mathias Agopian386aa982011-11-07 21:58:03 -08001578 if (transactionFlags) {
1579 // this triggers the transaction
1580 setTransactionFlags(transactionFlags);
1581
1582 // if this is a synchronous transaction, wait for it to take effect
1583 // before returning.
1584 if (flags & eSynchronous) {
1585 mTransationPending = true;
1586 }
1587 while (mTransationPending) {
1588 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1589 if (CC_UNLIKELY(err != NO_ERROR)) {
1590 // just in case something goes wrong in SF, return to the
1591 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001592 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001593 mTransationPending = false;
1594 break;
1595 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001596 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 }
1598}
1599
Mathias Agopiane57f2922012-08-09 16:29:12 -07001600uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1601{
1602 uint32_t flags = 0;
1603 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001604 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001605 const uint32_t what = s.what;
1606 if (what & DisplayState::eSurfaceChanged) {
1607 if (disp.surface->asBinder() != s.surface->asBinder()) {
1608 disp.surface = s.surface;
1609 flags |= eDisplayTransactionNeeded;
1610 }
1611 }
1612 if (what & DisplayState::eLayerStackChanged) {
1613 if (disp.layerStack != s.layerStack) {
1614 disp.layerStack = s.layerStack;
1615 flags |= eDisplayTransactionNeeded;
1616 }
1617 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001618 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001619 if (disp.orientation != s.orientation) {
1620 disp.orientation = s.orientation;
1621 flags |= eDisplayTransactionNeeded;
1622 }
1623 if (disp.frame != s.frame) {
1624 disp.frame = s.frame;
1625 flags |= eDisplayTransactionNeeded;
1626 }
1627 if (disp.viewport != s.viewport) {
1628 disp.viewport = s.viewport;
1629 flags |= eDisplayTransactionNeeded;
1630 }
1631 }
1632 }
1633 return flags;
1634}
1635
1636uint32_t SurfaceFlinger::setClientStateLocked(
1637 const sp<Client>& client,
1638 const layer_state_t& s)
1639{
1640 uint32_t flags = 0;
1641 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1642 if (layer != 0) {
1643 const uint32_t what = s.what;
1644 if (what & layer_state_t::ePositionChanged) {
1645 if (layer->setPosition(s.x, s.y))
1646 flags |= eTraversalNeeded;
1647 }
1648 if (what & layer_state_t::eLayerChanged) {
1649 // NOTE: index needs to be calculated before we update the state
1650 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1651 if (layer->setLayer(s.z)) {
1652 mCurrentState.layersSortedByZ.removeAt(idx);
1653 mCurrentState.layersSortedByZ.add(layer);
1654 // we need traversal (state changed)
1655 // AND transaction (list changed)
1656 flags |= eTransactionNeeded|eTraversalNeeded;
1657 }
1658 }
1659 if (what & layer_state_t::eSizeChanged) {
1660 if (layer->setSize(s.w, s.h)) {
1661 flags |= eTraversalNeeded;
1662 }
1663 }
1664 if (what & layer_state_t::eAlphaChanged) {
1665 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1666 flags |= eTraversalNeeded;
1667 }
1668 if (what & layer_state_t::eMatrixChanged) {
1669 if (layer->setMatrix(s.matrix))
1670 flags |= eTraversalNeeded;
1671 }
1672 if (what & layer_state_t::eTransparentRegionChanged) {
1673 if (layer->setTransparentRegionHint(s.transparentRegion))
1674 flags |= eTraversalNeeded;
1675 }
1676 if (what & layer_state_t::eVisibilityChanged) {
1677 if (layer->setFlags(s.flags, s.mask))
1678 flags |= eTraversalNeeded;
1679 }
1680 if (what & layer_state_t::eCropChanged) {
1681 if (layer->setCrop(s.crop))
1682 flags |= eTraversalNeeded;
1683 }
1684 if (what & layer_state_t::eLayerStackChanged) {
1685 // NOTE: index needs to be calculated before we update the state
1686 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1687 if (layer->setLayerStack(s.layerStack)) {
1688 mCurrentState.layersSortedByZ.removeAt(idx);
1689 mCurrentState.layersSortedByZ.add(layer);
1690 // we need traversal (state changed)
1691 // AND transaction (list changed)
1692 flags |= eTransactionNeeded|eTraversalNeeded;
1693 }
1694 }
1695 }
1696 return flags;
1697}
1698
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001699sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001700 ISurfaceComposerClient::surface_data_t* params,
1701 const String8& name,
1702 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001703 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 uint32_t flags)
1705{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001706 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001707 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001708
1709 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001710 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001711 int(w), int(h));
1712 return surfaceHandle;
1713 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001714
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001715 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001716 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1717 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001718 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001720 case ISurfaceComposerClient::eFXSurfaceBlur:
1721 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001722 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001724 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001725 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001726 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 }
1728
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001729 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001730 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001731 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001732 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001734 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001735 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001736 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001737 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001738 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 }
1740
1741 return surfaceHandle;
1742}
1743
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001744sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001745 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001746 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001747 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748{
1749 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001750 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751 case PIXEL_FORMAT_TRANSPARENT:
1752 case PIXEL_FORMAT_TRANSLUCENT:
1753 format = PIXEL_FORMAT_RGBA_8888;
1754 break;
1755 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001756#ifdef NO_RGBX_8888
1757 format = PIXEL_FORMAT_RGB_565;
1758#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001759 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001760#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761 break;
1762 }
1763
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001764#ifdef NO_RGBX_8888
1765 if (format == PIXEL_FORMAT_RGBX_8888)
1766 format = PIXEL_FORMAT_RGBA_8888;
1767#endif
1768
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001769 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001770 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001771 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001772 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001773 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 }
1775 return layer;
1776}
1777
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001778sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001779 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001780 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001782 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001783 return layer;
1784}
1785
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001786sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001787 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001788 uint32_t w, uint32_t h, uint32_t flags)
1789{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001790 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791 return layer;
1792}
1793
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001794status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001795{
Mathias Agopian9a112062009-04-17 19:36:26 -07001796 /*
1797 * called by the window manager, when a surface should be marked for
1798 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001799 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001800 * The surface is removed from the current and drawing lists, but placed
1801 * in the purgatory queue, so it's not destroyed right-away (we need
1802 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001803 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001804
Mathias Agopian48d819a2009-09-10 19:41:18 -07001805 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001806 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001807 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001808
Mathias Agopian48d819a2009-09-10 19:41:18 -07001809 if (layer != 0) {
1810 err = purgatorizeLayer_l(layer);
1811 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001812 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001813 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001814 }
1815 return err;
1816}
1817
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001818status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001819{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001820 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001821 status_t err = NO_ERROR;
1822 sp<LayerBaseClient> l(layer.promote());
1823 if (l != NULL) {
1824 Mutex::Autolock _l(mStateLock);
1825 err = removeLayer_l(l);
1826 if (err == NAME_NOT_FOUND) {
1827 // The surface wasn't in the current list, which means it was
1828 // removed already, which means it is in the purgatory,
1829 // and need to be removed from there.
1830 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001831 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001832 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001833 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001834 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001835 "error removing layer=%p (%s)", l.get(), strerror(-err));
1836 }
1837 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001838}
1839
Mathias Agopianb60314a2012-04-10 22:09:54 -07001840// ---------------------------------------------------------------------------
1841
Andy McFadden13a082e2012-08-24 10:16:42 -07001842void SurfaceFlinger::onInitializeDisplays() {
1843 // reset screen orientation
1844 Vector<ComposerState> state;
1845 Vector<DisplayState> displays;
1846 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001847 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001848 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001849 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001850 d.frame.makeInvalid();
1851 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001852 displays.add(d);
1853 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001854 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001855}
1856
1857void SurfaceFlinger::initializeDisplays() {
1858 class MessageScreenInitialized : public MessageBase {
1859 SurfaceFlinger* flinger;
1860 public:
1861 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1862 virtual bool handler() {
1863 flinger->onInitializeDisplays();
1864 return true;
1865 }
1866 };
1867 sp<MessageBase> msg = new MessageScreenInitialized(this);
1868 postMessageAsync(msg); // we may be called from main thread, use async message
1869}
1870
1871
Mathias Agopiancde87a32012-09-13 14:09:01 -07001872void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001873 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001874 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001875 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001876 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1877 // FIXME: eventthread only knows about the main display right now
1878 mEventThread->onScreenAcquired();
1879 }
Mathias Agopian20128302012-08-13 18:32:13 -07001880 mVisibleRegionsDirty = true;
1881 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882}
1883
Mathias Agopiancde87a32012-09-13 14:09:01 -07001884void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001885 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001886 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001887 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1888 // FIXME: eventthread only knows about the main display right now
1889 mEventThread->onScreenReleased();
1890 }
Mathias Agopian42977342012-08-05 00:40:46 -07001891 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001892 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001893 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001894 // from this point on, SF will stop drawing
1895 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896}
1897
Colin Cross8e533062012-06-07 13:17:52 -07001898void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001899 class MessageScreenAcquired : public MessageBase {
1900 SurfaceFlinger* flinger;
1901 public:
1902 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1903 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001904 // FIXME: should this be per-display?
1905 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001906 return true;
1907 }
1908 };
1909 sp<MessageBase> msg = new MessageScreenAcquired(this);
1910 postMessageSync(msg);
1911}
1912
Colin Cross8e533062012-06-07 13:17:52 -07001913void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001914 class MessageScreenReleased : public MessageBase {
1915 SurfaceFlinger* flinger;
1916 public:
1917 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1918 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001919 // FIXME: should this be per-display?
1920 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001921 return true;
1922 }
1923 };
1924 sp<MessageBase> msg = new MessageScreenReleased(this);
1925 postMessageSync(msg);
1926}
1927
1928// ---------------------------------------------------------------------------
1929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1931{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001932 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933 char buffer[SIZE];
1934 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001935
1936 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 snprintf(buffer, SIZE, "Permission Denial: "
1938 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1939 IPCThreadState::self()->getCallingPid(),
1940 IPCThreadState::self()->getCallingUid());
1941 result.append(buffer);
1942 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001943 // Try to get the main lock, but don't insist if we can't
1944 // (this would indicate SF is stuck, but we want to be able to
1945 // print something in dumpsys).
1946 int retry = 3;
1947 while (mStateLock.tryLock()<0 && --retry>=0) {
1948 usleep(1000000);
1949 }
1950 const bool locked(retry >= 0);
1951 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001952 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001953 "SurfaceFlinger appears to be unresponsive, "
1954 "dumping anyways (no locks held)\n");
1955 result.append(buffer);
1956 }
1957
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001958 bool dumpAll = true;
1959 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001960 size_t numArgs = args.size();
1961 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001962 if ((index < numArgs) &&
1963 (args[index] == String16("--list"))) {
1964 index++;
1965 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001966 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001967 }
1968
1969 if ((index < numArgs) &&
1970 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001971 index++;
1972 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001973 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001974 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001975
1976 if ((index < numArgs) &&
1977 (args[index] == String16("--latency-clear"))) {
1978 index++;
1979 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001980 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001981 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001983
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001984 if (dumpAll) {
1985 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001986 }
1987
Mathias Agopian9795c422009-08-26 16:36:26 -07001988 if (locked) {
1989 mStateLock.unlock();
1990 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001991 }
1992 write(fd, result.string(), result.size());
1993 return NO_ERROR;
1994}
1995
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001996void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1997 String8& result, char* buffer, size_t SIZE) const
1998{
1999 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2000 const size_t count = currentLayers.size();
2001 for (size_t i=0 ; i<count ; i++) {
2002 const sp<LayerBase>& layer(currentLayers[i]);
2003 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2004 result.append(buffer);
2005 }
2006}
2007
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002008void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2009 String8& result, char* buffer, size_t SIZE) const
2010{
2011 String8 name;
2012 if (index < args.size()) {
2013 name = String8(args[index]);
2014 index++;
2015 }
2016
2017 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2018 const size_t count = currentLayers.size();
2019 for (size_t i=0 ; i<count ; i++) {
2020 const sp<LayerBase>& layer(currentLayers[i]);
2021 if (name.isEmpty()) {
2022 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2023 result.append(buffer);
2024 }
2025 if (name.isEmpty() || (name == layer->getName())) {
2026 layer->dumpStats(result, buffer, SIZE);
2027 }
2028 }
2029}
2030
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002031void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
2032 String8& result, char* buffer, size_t SIZE) const
2033{
2034 String8 name;
2035 if (index < args.size()) {
2036 name = String8(args[index]);
2037 index++;
2038 }
2039
2040 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2041 const size_t count = currentLayers.size();
2042 for (size_t i=0 ; i<count ; i++) {
2043 const sp<LayerBase>& layer(currentLayers[i]);
2044 if (name.isEmpty() || (name == layer->getName())) {
2045 layer->clearStats();
2046 }
2047 }
2048}
2049
Andy McFadden4803b742012-09-24 19:07:20 -07002050/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2051{
2052 static const char* config =
2053 " [sf"
2054#ifdef NO_RGBX_8888
2055 " NO_RGBX_8888"
2056#endif
2057#ifdef HAS_CONTEXT_PRIORITY
2058 " HAS_CONTEXT_PRIORITY"
2059#endif
2060#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2061 " NEVER_DEFAULT_TO_ASYNC_MODE"
2062#endif
2063#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2064 " TARGET_DISABLE_TRIPLE_BUFFERING"
2065#endif
2066 "]";
2067 result.append(config);
2068}
2069
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002070void SurfaceFlinger::dumpAllLocked(
2071 String8& result, char* buffer, size_t SIZE) const
2072{
2073 // figure out if we're stuck somewhere
2074 const nsecs_t now = systemTime();
2075 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2076 const nsecs_t inTransaction(mDebugInTransaction);
2077 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2078 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2079
2080 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002081 * Dump library configuration.
2082 */
2083 result.append("Build configuration:");
2084 appendSfConfigString(result);
2085 appendUiConfigString(result);
2086 appendGuiConfigString(result);
2087 result.append("\n");
2088
2089 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002090 * Dump the visible layer list
2091 */
2092 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2093 const size_t count = currentLayers.size();
2094 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2095 result.append(buffer);
2096 for (size_t i=0 ; i<count ; i++) {
2097 const sp<LayerBase>& layer(currentLayers[i]);
2098 layer->dump(result, buffer, SIZE);
2099 }
2100
2101 /*
2102 * Dump the layers in the purgatory
2103 */
2104
2105 const size_t purgatorySize = mLayerPurgatory.size();
2106 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2107 result.append(buffer);
2108 for (size_t i=0 ; i<purgatorySize ; i++) {
2109 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2110 layer->shortDump(result, buffer, SIZE);
2111 }
2112
2113 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002114 * Dump Display state
2115 */
2116
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002117 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2118 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002119 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2120 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002121 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002122 }
2123
2124 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002125 * Dump SurfaceFlinger global state
2126 */
2127
2128 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2129 result.append(buffer);
2130
Mathias Agopian888c8222012-08-04 21:10:38 -07002131 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002132 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002133 const GLExtensions& extensions(GLExtensions::getInstance());
2134 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2135 extensions.getVendor(),
2136 extensions.getRenderer(),
2137 extensions.getVersion());
2138 result.append(buffer);
2139
2140 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002141 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002142 result.append(buffer);
2143
2144 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2145 result.append(buffer);
2146
Mathias Agopian42977342012-08-05 00:40:46 -07002147 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002148 snprintf(buffer, SIZE,
2149 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002150 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002151 result.append(buffer);
2152 snprintf(buffer, SIZE,
2153 " last eglSwapBuffers() time: %f us\n"
2154 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002155 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002156 " refresh-rate : %f fps\n"
2157 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002158 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002159 mLastSwapBufferTime/1000.0,
2160 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002161 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002162 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2163 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2164 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002165 result.append(buffer);
2166
2167 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2168 inSwapBuffersDuration/1000.0);
2169 result.append(buffer);
2170
2171 snprintf(buffer, SIZE, " transaction time: %f us\n",
2172 inTransactionDuration/1000.0);
2173 result.append(buffer);
2174
2175 /*
2176 * VSYNC state
2177 */
2178 mEventThread->dump(result, buffer, SIZE);
2179
2180 /*
2181 * Dump HWComposer state
2182 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002183 snprintf(buffer, SIZE, "h/w composer state:\n");
2184 result.append(buffer);
2185 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2186 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2187 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2188 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002189 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002190
2191 /*
2192 * Dump gralloc state
2193 */
2194 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2195 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002196}
2197
Keun young Park63f165f2012-08-31 10:53:36 -07002198bool SurfaceFlinger::startDdmConnection()
2199{
2200 void* libddmconnection_dso =
2201 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2202 if (!libddmconnection_dso) {
2203 return false;
2204 }
2205 void (*DdmConnection_start)(const char* name);
2206 DdmConnection_start =
2207 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2208 if (!DdmConnection_start) {
2209 dlclose(libddmconnection_dso);
2210 return false;
2211 }
2212 (*DdmConnection_start)(getServiceName());
2213 return true;
2214}
2215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002216status_t SurfaceFlinger::onTransact(
2217 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2218{
2219 switch (code) {
2220 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002221 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002222 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002223 case BLANK:
2224 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225 {
2226 // codes that require permission check
2227 IPCThreadState* ipc = IPCThreadState::self();
2228 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002229 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002230 if ((uid != AID_GRAPHICS) &&
2231 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002232 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002233 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2234 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002236 break;
2237 }
2238 case CAPTURE_SCREEN:
2239 {
2240 // codes that require permission check
2241 IPCThreadState* ipc = IPCThreadState::self();
2242 const int pid = ipc->getCallingPid();
2243 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002244 if ((uid != AID_GRAPHICS) &&
2245 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002246 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002247 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2248 return PERMISSION_DENIED;
2249 }
2250 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002251 }
2252 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002254 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2255 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002256 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002257 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002258 IPCThreadState* ipc = IPCThreadState::self();
2259 const int pid = ipc->getCallingPid();
2260 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002261 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002262 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002263 return PERMISSION_DENIED;
2264 }
2265 int n;
2266 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002267 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002268 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269 return NO_ERROR;
2270 case 1002: // SHOW_UPDATES
2271 n = data.readInt32();
2272 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002273 invalidateHwcGeometry();
2274 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002275 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002276 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002277 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002278 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002279 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002280 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002281 setTransactionFlags(
2282 eTransactionNeeded|
2283 eDisplayTransactionNeeded|
2284 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002285 return NO_ERROR;
2286 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002287 case 1006:{ // send empty update
2288 signalRefresh();
2289 return NO_ERROR;
2290 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002291 case 1008: // toggle use of hw composer
2292 n = data.readInt32();
2293 mDebugDisableHWC = n ? 1 : 0;
2294 invalidateHwcGeometry();
2295 repaintEverything();
2296 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002297 case 1009: // toggle use of transform hint
2298 n = data.readInt32();
2299 mDebugDisableTransformHint = n ? 1 : 0;
2300 invalidateHwcGeometry();
2301 repaintEverything();
2302 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002303 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002304 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305 reply->writeInt32(0);
2306 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002307 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002308 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309 return NO_ERROR;
2310 case 1013: {
2311 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002312 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2313 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002314 }
2315 return NO_ERROR;
2316 }
2317 }
2318 return err;
2319}
2320
Mathias Agopian53331da2011-08-22 21:44:41 -07002321void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002322 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002323 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002324}
2325
Mathias Agopian59119e62010-10-11 12:37:43 -07002326// ---------------------------------------------------------------------------
2327
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002328status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002329 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2330{
2331 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002332 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002333}
2334
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002335status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002336 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002337{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002338 ATRACE_CALL();
2339
Mathias Agopian59119e62010-10-11 12:37:43 -07002340 if (!GLExtensions::getInstance().haveFramebufferObject())
2341 return INVALID_OPERATION;
2342
2343 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002344 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2345 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002346 const uint32_t hw_w = hw->getWidth();
2347 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002348 GLfloat u = 1;
2349 GLfloat v = 1;
2350
2351 // make sure to clear all GL error flags
2352 while ( glGetError() != GL_NO_ERROR ) ;
2353
2354 // create a FBO
2355 GLuint name, tname;
2356 glGenTextures(1, &tname);
2357 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2359 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002360 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2361 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002362 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002363 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002364 GLint tw = (2 << (31 - clz(hw_w)));
2365 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002366 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2367 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002368 u = GLfloat(hw_w) / tw;
2369 v = GLfloat(hw_h) / th;
2370 }
2371 glGenFramebuffersOES(1, &name);
2372 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002373 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2374 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002375
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002376 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002377 glDisable(GL_TEXTURE_EXTERNAL_OES);
2378 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002379 glClearColor(0,0,0,1);
2380 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002381 glMatrixMode(GL_MODELVIEW);
2382 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002383 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002384 const size_t count = layers.size();
2385 for (size_t i=0 ; i<count ; ++i) {
2386 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002387 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002388 }
2389
Mathias Agopian42977342012-08-05 00:40:46 -07002390 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002391
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002392 // back to main framebuffer
2393 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002394 glDeleteFramebuffersOES(1, &name);
2395
2396 *textureName = tname;
2397 *uOut = u;
2398 *vOut = v;
2399 return NO_ERROR;
2400}
2401
2402// ---------------------------------------------------------------------------
2403
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002404status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 sp<IMemoryHeap>* heap,
2406 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 uint32_t sw, uint32_t sh,
2408 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002409{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002410 ATRACE_CALL();
2411
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 status_t result = PERMISSION_DENIED;
2413
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002414 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002415 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002416 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417
2418 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002419 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002420 const uint32_t hw_w = hw->getWidth();
2421 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002422
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002423 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002424 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002425 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002426 return PERMISSION_DENIED;
2427 }
2428
2429 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002430 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002432 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002433
2434 sw = (!sw) ? hw_w : sw;
2435 sh = (!sh) ? hw_h : sh;
2436 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002437 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002438
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002439// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2440// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002441
Mathias Agopian74c40c02010-09-29 13:02:36 -07002442 // make sure to clear all GL error flags
2443 while ( glGetError() != GL_NO_ERROR ) ;
2444
2445 // create a FBO
2446 GLuint name, tname;
2447 glGenRenderbuffersOES(1, &tname);
2448 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2449 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002450
Mathias Agopian74c40c02010-09-29 13:02:36 -07002451 glGenFramebuffersOES(1, &name);
2452 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2453 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2454 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2455
2456 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002457
Mathias Agopian74c40c02010-09-29 13:02:36 -07002458 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2459
2460 // invert everything, b/c glReadPixel() below will invert the FB
2461 glViewport(0, 0, sw, sh);
2462 glMatrixMode(GL_PROJECTION);
2463 glPushMatrix();
2464 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002465 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002466 glMatrixMode(GL_MODELVIEW);
2467
2468 // redraw the screen entirely...
2469 glClearColor(0,0,0,1);
2470 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002471
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002472 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002473 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002474 for (size_t i=0 ; i<count ; ++i) {
2475 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002476 const uint32_t z = layer->drawingState().z;
2477 if (z >= minLayerZ && z <= maxLayerZ) {
2478 if (filtering) layer->setFiltering(true);
2479 layer->draw(hw);
2480 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002481 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002482 }
2483
Mathias Agopian74c40c02010-09-29 13:02:36 -07002484 // check for errors and return screen capture
2485 if (glGetError() != GL_NO_ERROR) {
2486 // error while rendering
2487 result = INVALID_OPERATION;
2488 } else {
2489 // allocate shared memory large enough to hold the
2490 // screen capture
2491 sp<MemoryHeapBase> base(
2492 new MemoryHeapBase(size, 0, "screen-capture") );
2493 void* const ptr = base->getBase();
2494 if (ptr) {
2495 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002496 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002497 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2498 if (glGetError() == GL_NO_ERROR) {
2499 *heap = base;
2500 *w = sw;
2501 *h = sh;
2502 *f = PIXEL_FORMAT_RGBA_8888;
2503 result = NO_ERROR;
2504 }
2505 } else {
2506 result = NO_MEMORY;
2507 }
2508 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002509 glViewport(0, 0, hw_w, hw_h);
2510 glMatrixMode(GL_PROJECTION);
2511 glPopMatrix();
2512 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002513 } else {
2514 result = BAD_VALUE;
2515 }
2516
2517 // release FBO resources
2518 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2519 glDeleteRenderbuffersOES(1, &tname);
2520 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002521
Mathias Agopian42977342012-08-05 00:40:46 -07002522 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002523
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002524// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002525
Mathias Agopian74c40c02010-09-29 13:02:36 -07002526 return result;
2527}
2528
2529
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002530status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002531 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002532 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002533 uint32_t sw, uint32_t sh,
2534 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002535{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002536 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002537 return BAD_VALUE;
2538
2539 if (!GLExtensions::getInstance().haveFramebufferObject())
2540 return INVALID_OPERATION;
2541
2542 class MessageCaptureScreen : public MessageBase {
2543 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002544 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002545 sp<IMemoryHeap>* heap;
2546 uint32_t* w;
2547 uint32_t* h;
2548 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002549 uint32_t sw;
2550 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002551 uint32_t minLayerZ;
2552 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002553 status_t result;
2554 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002555 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002556 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002557 uint32_t sw, uint32_t sh,
2558 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002559 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002560 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2561 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2562 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002563 {
2564 }
2565 status_t getResult() const {
2566 return result;
2567 }
2568 virtual bool handler() {
2569 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002570 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002571 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002572 return true;
2573 }
2574 };
2575
2576 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002577 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002578 status_t res = postMessageSync(msg);
2579 if (res == NO_ERROR) {
2580 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2581 }
2582 return res;
2583}
2584
2585// ---------------------------------------------------------------------------
2586
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002587SurfaceFlinger::LayerVector::LayerVector() {
2588}
2589
2590SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2591 : SortedVector<sp<LayerBase> >(rhs) {
2592}
2593
2594int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2595 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002596{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002597 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002598 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2599 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002600
2601 uint32_t ls = l->currentState().layerStack;
2602 uint32_t rs = r->currentState().layerStack;
2603 if (ls != rs)
2604 return ls - rs;
2605
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002606 uint32_t lz = l->currentState().z;
2607 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002608 if (lz != rz)
2609 return lz - rz;
2610
2611 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002612}
2613
2614// ---------------------------------------------------------------------------
2615
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002616SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2617 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002618}
2619
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002620SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2621 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002622 viewport.makeInvalid();
2623 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002624}
2625
2626// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628}; // namespace android