blob: ceae8d78967466949264a81e31a73117bc9b7964 [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 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700795
796 HWComposer& hwc(getHwComposer());
797 if (hwc.initCheck() == NO_ERROR) {
798 status_t err = hwc.prepare();
799 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
800 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801}
802
803void SurfaceFlinger::preComposition()
804{
805 bool needExtraInvalidate = false;
806 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
807 const size_t count = currentLayers.size();
808 for (size_t i=0 ; i<count ; i++) {
809 if (currentLayers[i]->onPreComposition()) {
810 needExtraInvalidate = true;
811 }
812 }
813 if (needExtraInvalidate) {
814 signalLayerUpdate();
815 }
816}
817
818void SurfaceFlinger::postComposition()
819{
820 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
821 const size_t count = currentLayers.size();
822 for (size_t i=0 ; i<count ; i++) {
823 currentLayers[i]->onPostComposition();
824 }
825}
826
827void SurfaceFlinger::rebuildLayerStacks() {
828 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700829 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700830 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700831 mVisibleRegionsDirty = false;
832 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700833
Mathias Agopian87baae12012-07-31 12:38:26 -0700834 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700835 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836 Region opaqueRegion;
837 Region dirtyRegion;
838 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700839 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700840 const Transform& tr(hw->getTransform());
841 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700842 if (hw->canDraw()) {
843 SurfaceFlinger::computeVisibleRegions(currentLayers,
844 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700845
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700846 const size_t count = currentLayers.size();
847 for (size_t i=0 ; i<count ; i++) {
848 const sp<LayerBase>& layer(currentLayers[i]);
849 const Layer::State& s(layer->drawingState());
850 if (s.layerStack == hw->getLayerStack()) {
851 Region visibleRegion(tr.transform(layer->visibleRegion));
852 visibleRegion.andSelf(bounds);
853 if (!visibleRegion.isEmpty()) {
854 layersSortedByZ.add(layer);
855 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700856 }
857 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700858 }
Mathias Agopian42977342012-08-05 00:40:46 -0700859 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700860 hw->undefinedRegion.set(bounds);
861 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
862 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700863 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700864 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700865}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700866
Mathias Agopiancd60f992012-08-16 16:28:27 -0700867void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700868 HWComposer& hwc(getHwComposer());
869 if (hwc.initCheck() == NO_ERROR) {
870 // build the h/w work list
871 const bool workListsDirty = mHwWorkListDirty;
872 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700873 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700874 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700875 const int32_t id = hw->getHwcDisplayId();
876 if (id >= 0) {
877 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700878 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700879 const size_t count = currentLayers.size();
880 if (hwc.createWorkList(id, count) >= 0) {
881 HWComposer::LayerListIterator cur = hwc.begin(id);
882 const HWComposer::LayerListIterator end = hwc.end(id);
883 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
884 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700885
Mathias Agopiane60b0682012-08-21 23:34:09 -0700886 if (CC_UNLIKELY(workListsDirty)) {
887 layer->setGeometry(hw, *cur);
888 if (mDebugDisableHWC || mDebugRegion) {
889 cur->setSkip(true);
890 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700891 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700892
Mathias Agopiane60b0682012-08-21 23:34:09 -0700893 /*
894 * update the per-frame h/w composer data for each layer
895 * and build the transparent region of the FB
896 */
897 layer->setPerFrameData(hw, *cur);
898 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700899 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700900 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700901 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700902 status_t err = hwc.prepare();
903 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
904 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700906
Mathias Agopiancd60f992012-08-16 16:28:27 -0700907void SurfaceFlinger::doComposition() {
908 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700909 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700910 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700911 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 if (hw->canDraw()) {
913 // transform the dirty region into this screen's coordinate space
914 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
915 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700916 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700918 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919 hw->dirtyRegion.clear();
920 hw->flip(hw->swapRegion);
921 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700922 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700923 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700924 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700925 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700926 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700927}
928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929void SurfaceFlinger::postFramebuffer()
930{
Mathias Agopian841cde52012-03-01 15:44:37 -0800931 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800932
Mathias Agopiana44b0412011-10-16 18:46:35 -0700933 const nsecs_t now = systemTime();
934 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700935
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700936 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700937 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700938 if (!hwc.supportsFramebufferTarget()) {
939 // EGL spec says:
940 // "surface must be bound to the calling thread's current context,
941 // for the current rendering API."
942 DisplayDevice::makeCurrent(mEGLDisplay,
943 getDefaultDisplayDevice(), mEGLContext);
944 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700945 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 }
947
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700949 sp<const DisplayDevice> hw(mDisplays[dpy]);
950 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700951 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700952 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700953 int32_t id = hw->getHwcDisplayId();
954 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700955 HWComposer::LayerListIterator cur = hwc.begin(id);
956 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700957 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700958 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700962 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963 }
Jesse Hallef194142012-06-14 14:45:17 -0700964 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800965 }
966
Mathias Agopiana44b0412011-10-16 18:46:35 -0700967 mLastSwapBufferTime = systemTime() - now;
968 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969}
970
Mathias Agopian87baae12012-07-31 12:38:26 -0700971void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972{
Mathias Agopian841cde52012-03-01 15:44:37 -0800973 ATRACE_CALL();
974
Mathias Agopianca4d3602011-05-19 15:38:14 -0700975 Mutex::Autolock _l(mStateLock);
976 const nsecs_t now = systemTime();
977 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978
Mathias Agopianca4d3602011-05-19 15:38:14 -0700979 // Here we're guaranteed that some transaction flags are set
980 // so we can call handleTransactionLocked() unconditionally.
981 // We call getTransactionFlags(), which will also clear the flags,
982 // with mStateLock held to guarantee that mCurrentState won't change
983 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700984
Mathias Agopiane57f2922012-08-09 16:29:12 -0700985 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700986 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700987
Mathias Agopianca4d3602011-05-19 15:38:14 -0700988 mLastTransactionTime = systemTime() - now;
989 mDebugInTransaction = 0;
990 invalidateHwcGeometry();
991 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700992}
993
Mathias Agopian87baae12012-07-31 12:38:26 -0700994void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700995{
996 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997 const size_t count = currentLayers.size();
998
999 /*
1000 * Traversal of the children
1001 * (perform the transaction for each of them if needed)
1002 */
1003
Mathias Agopian3559b072012-08-15 13:46:03 -07001004 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001006 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1008 if (!trFlags) continue;
1009
1010 const uint32_t flags = layer->doTransaction(0);
1011 if (flags & Layer::eVisibleRegion)
1012 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 }
1014 }
1015
1016 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001017 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 */
1019
Mathias Agopiane57f2922012-08-09 16:29:12 -07001020 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001021 // here we take advantage of Vector's copy-on-write semantics to
1022 // improve performance by skipping the transaction entirely when
1023 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001024 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1025 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001026 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001028 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001029 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001030
1031 // find the displays that were removed
1032 // (ie: in drawing state but not in current state)
1033 // also handle displays that changed
1034 // (ie: displays that are in both lists)
1035 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001036 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1037 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001038 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001039 if (!draw[i].isMainDisplay()) {
1040 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001041 } else {
1042 ALOGW("trying to remove the main display");
1043 }
1044 } else {
1045 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001046 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001047 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001048 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001049 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001050 // recreating the DisplayDevice, so we just remove it
1051 // from the drawing state, so that it get re-added
1052 // below.
1053 mDisplays.removeItem(display);
1054 mDrawingState.displays.removeItemsAt(i);
1055 dc--; i--;
1056 // at this point we must loop to the next item
1057 continue;
1058 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001059
Mathias Agopian93997a82012-08-29 17:30:36 -07001060 const sp<DisplayDevice>& disp(getDisplayDevice(display));
1061 if (disp != NULL) {
1062 if (state.layerStack != draw[i].layerStack) {
1063 disp->setLayerStack(state.layerStack);
1064 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001065 if ((state.orientation != draw[i].orientation)
1066 || (state.viewport != draw[i].viewport)
1067 || (state.frame != draw[i].frame))
1068 {
1069 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001070 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001071 }
Andy McFadden69052052012-09-14 16:10:11 -07001072
1073 // Walk through all the layers in currentLayers,
1074 // and update their transform hint.
1075 //
1076 // TODO: we could be much more clever about which
1077 // layers we touch and how often we do these updates
1078 // (e.g. only touch the layers associated with this
1079 // display, and only on a rotation).
1080 for (size_t i = 0; i < count; i++) {
1081 const sp<LayerBase>& layerBase = currentLayers[i];
1082 layerBase->updateTransformHint();
1083 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001084 }
1085 }
1086 }
1087
1088 // find displays that were added
1089 // (ie: in current state but not in drawing state)
1090 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001092 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001093
1094 sp<FramebufferSurface> fbs;
1095 sp<SurfaceTextureClient> stc;
1096 if (!state.isVirtualDisplay()) {
1097
1098 ALOGE_IF(state.surface!=NULL,
1099 "adding a supported display, but rendering "
1100 "surface is provided (%p), ignoring it",
1101 state.surface.get());
1102
1103 // for supported (by hwc) displays we provide our
1104 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001105 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001106 stc = new SurfaceTextureClient(
1107 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1108 } else {
1109 if (state.surface != NULL) {
1110 stc = new SurfaceTextureClient(state.surface);
1111 }
1112 }
1113
1114 const wp<IBinder>& display(curr.keyAt(i));
1115 if (stc != NULL) {
1116 sp<DisplayDevice> hw = new DisplayDevice(this,
1117 state.type, display, stc, fbs, mEGLConfig);
1118 hw->setLayerStack(state.layerStack);
1119 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001120 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001121 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001122 mDisplays.add(display, hw);
1123 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1124 // notify the system that this display is now up
1125 // (note onScreenAcquired() is safe to call from
1126 // here because we're in the main thread)
1127 onScreenAcquired(hw);
1128 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001129 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001130 }
1131 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001133 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134
Mathias Agopian3559b072012-08-15 13:46:03 -07001135 /*
1136 * Perform our own transaction if needed
1137 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001138
Mathias Agopiancd60f992012-08-16 16:28:27 -07001139 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1140 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001141 // layers have been added
1142 mVisibleRegionsDirty = true;
1143 }
1144
1145 // some layers might have been removed, so
1146 // we need to update the regions they're exposing.
1147 if (mLayersRemoved) {
1148 mLayersRemoved = false;
1149 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001150 const size_t count = previousLayers.size();
1151 for (size_t i=0 ; i<count ; i++) {
1152 const sp<LayerBase>& layer(previousLayers[i]);
1153 if (currentLayers.indexOf(layer) < 0) {
1154 // this layer is not visible anymore
1155 // TODO: we could traverse the tree from front to back and
1156 // compute the actual visible region
1157 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001158 const Layer::State& s(layer->drawingState());
1159 Region visibleReg = s.transform.transform(
1160 Region(Rect(s.active.w, s.active.h)));
1161 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 }
1165
1166 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001167}
1168
1169void SurfaceFlinger::commitTransaction()
1170{
1171 if (!mLayersPendingRemoval.isEmpty()) {
1172 // Notify removed layers now that they can't be drawn from
1173 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1174 mLayersPendingRemoval[i]->onRemoved();
1175 }
1176 mLayersPendingRemoval.clear();
1177 }
1178
1179 mDrawingState = mCurrentState;
1180 mTransationPending = false;
1181 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182}
1183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001185 const LayerVector& currentLayers, uint32_t layerStack,
1186 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187{
Mathias Agopian841cde52012-03-01 15:44:37 -08001188 ATRACE_CALL();
1189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 Region aboveOpaqueLayers;
1191 Region aboveCoveredLayers;
1192 Region dirty;
1193
Mathias Agopian87baae12012-07-31 12:38:26 -07001194 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195
1196 size_t i = currentLayers.size();
1197 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001198 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199
1200 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001201 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202
Mathias Agopian87baae12012-07-31 12:38:26 -07001203 // only consider the layers on the given later stack
1204 if (s.layerStack != layerStack)
1205 continue;
1206
Mathias Agopianab028732010-03-16 16:41:46 -07001207 /*
1208 * opaqueRegion: area of a surface that is fully opaque.
1209 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001211
1212 /*
1213 * visibleRegion: area of a surface that is visible on screen
1214 * and not fully transparent. This is essentially the layer's
1215 * footprint minus the opaque regions above it.
1216 * Areas covered by a translucent surface are considered visible.
1217 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001219
1220 /*
1221 * coveredRegion: area of a surface that is covered by all
1222 * visible regions above it (which includes the translucent areas).
1223 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001225
1226
1227 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001228 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001229 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001230 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001232 if (!visibleRegion.isEmpty()) {
1233 // Remove the transparent area from the visible region
1234 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001235 Region transparentRegionScreen;
1236 const Transform tr(s.transform);
1237 if (tr.transformed()) {
1238 if (tr.preserveRects()) {
1239 // transform the transparent region
1240 transparentRegionScreen = tr.transform(s.transparentRegion);
1241 } else {
1242 // transformation too complex, can't do the
1243 // transparent region optimization.
1244 transparentRegionScreen.clear();
1245 }
1246 } else {
1247 transparentRegionScreen = s.transparentRegion;
1248 }
1249 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001250 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251
Mathias Agopianab028732010-03-16 16:41:46 -07001252 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001253 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001254 if (s.alpha==255 && !translucent &&
1255 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1256 // the opaque region is the layer's footprint
1257 opaqueRegion = visibleRegion;
1258 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 }
1260 }
1261
Mathias Agopianab028732010-03-16 16:41:46 -07001262 // Clip the covered region to the visible region
1263 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1264
1265 // Update aboveCoveredLayers for next (lower) layer
1266 aboveCoveredLayers.orSelf(visibleRegion);
1267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 // subtract the opaque region covered by the layers above us
1269 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270
1271 // compute this layer's dirty region
1272 if (layer->contentDirty) {
1273 // we need to invalidate the whole region
1274 dirty = visibleRegion;
1275 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001276 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 layer->contentDirty = false;
1278 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001279 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001280 * the exposed region consists of two components:
1281 * 1) what's VISIBLE now and was COVERED before
1282 * 2) what's EXPOSED now less what was EXPOSED before
1283 *
1284 * note that (1) is conservative, we start with the whole
1285 * visible region but only keep what used to be covered by
1286 * something -- which mean it may have been exposed.
1287 *
1288 * (2) handles areas that were not covered by anything but got
1289 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001290 */
Mathias Agopianab028732010-03-16 16:41:46 -07001291 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001292 const Region oldVisibleRegion = layer->visibleRegion;
1293 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001294 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1295 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 }
1297 dirty.subtractSelf(aboveOpaqueLayers);
1298
1299 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001300 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301
Mathias Agopianab028732010-03-16 16:41:46 -07001302 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 // Store the visible region is screen space
1306 layer->setVisibleRegion(visibleRegion);
1307 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 }
1309
Mathias Agopian87baae12012-07-31 12:38:26 -07001310 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311}
1312
Mathias Agopian87baae12012-07-31 12:38:26 -07001313void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1314 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001315 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001316 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1317 if (hw->getLayerStack() == layerStack) {
1318 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001319 }
1320 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001321}
1322
1323void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001325 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001328 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001329 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001330 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001331 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001332 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001333 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001334 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001335 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001336
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001337 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338}
1339
Mathias Agopianad456f92011-01-13 17:53:01 -08001340void SurfaceFlinger::invalidateHwcGeometry()
1341{
1342 mHwWorkListDirty = true;
1343}
1344
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001345
Mathias Agopiancd60f992012-08-16 16:28:27 -07001346void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001347 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348{
Mathias Agopian87baae12012-07-31 12:38:26 -07001349 Region dirtyRegion(inDirtyRegion);
1350
Mathias Agopianb8a55602009-06-26 19:06:36 -07001351 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001352 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353
Mathias Agopian42977342012-08-05 00:40:46 -07001354 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001355 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001356 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1357 // takes a rectangle, we must make sure to update that whole
1358 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001359 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001360 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001361 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001362 // We need to redraw the rectangle that will be updated
1363 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001364 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001365 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001366 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001367 } else {
1368 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001369 dirtyRegion.set(hw->bounds());
1370 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 }
1372 }
1373
Mathias Agopiancd60f992012-08-16 16:28:27 -07001374 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001376 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001377 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001378
1379 // swap buffers (presentation)
1380 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381}
1382
Mathias Agopiancd60f992012-08-16 16:28:27 -07001383void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001384{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001385 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001386 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001387 HWComposer::LayerListIterator cur = hwc.begin(id);
1388 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001389
Mathias Agopian85d751c2012-08-29 16:59:24 -07001390 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1391 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001392 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001393
1394 // set the frame buffer
1395 glMatrixMode(GL_MODELVIEW);
1396 glLoadIdentity();
1397
1398 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001399 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001400 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001401 // when using overlays, we assume a fully transparent framebuffer
1402 // NOTE: we could reduce how much we need to clear, for instance
1403 // remove where there are opaque FB layers. however, on some
1404 // GPUs doing a "clean slate" glClear might be more efficient.
1405 // We'll revisit later if needed.
1406 glClearColor(0, 0, 0, 0);
1407 glClear(GL_COLOR_BUFFER_BIT);
1408 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001409 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001410 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001411 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001412 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001413 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001414 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001415 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001416 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001417
Mathias Agopian85d751c2012-08-29 16:59:24 -07001418 /*
1419 * and then, render the layers targeted at the framebuffer
1420 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001421
Mathias Agopian85d751c2012-08-29 16:59:24 -07001422 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1423 const size_t count = layers.size();
1424 const Transform& tr = hw->getTransform();
1425 if (cur != end) {
1426 // we're using h/w composer
1427 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001428 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001429 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001430 if (!clip.isEmpty()) {
1431 switch (cur->getCompositionType()) {
1432 case HWC_OVERLAY: {
1433 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1434 && i
1435 && layer->isOpaque()
1436 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001437 // never clear the very first layer since we're
1438 // guaranteed the FB is already cleared
1439 layer->clearWithOpenGL(hw, clip);
1440 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001441 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001442 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001443 case HWC_FRAMEBUFFER: {
1444 layer->draw(hw, clip);
1445 break;
1446 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001447 case HWC_FRAMEBUFFER_TARGET: {
1448 // this should not happen as the iterator shouldn't
1449 // let us get there.
1450 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1451 break;
1452 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001453 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001454 }
1455 layer->setAcquireFence(hw, *cur);
1456 }
1457 } else {
1458 // we're not using h/w composer
1459 for (size_t i=0 ; i<count ; ++i) {
1460 const sp<LayerBase>& layer(layers[i]);
1461 const Region clip(dirty.intersect(
1462 tr.transform(layer->visibleRegion)));
1463 if (!clip.isEmpty()) {
1464 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001465 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001466 }
1467 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopian55801e42012-08-27 18:54:24 -07001470void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1471 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001473 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001474 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001475 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001476 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001477
Mathias Agopian55801e42012-08-27 18:54:24 -07001478 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001479 Region::const_iterator it = region.begin();
1480 Region::const_iterator const end = region.end();
1481 while (it != end) {
1482 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001483 GLfloat vertices[][2] = {
1484 { r.left, height - r.top },
1485 { r.left, height - r.bottom },
1486 { r.right, height - r.bottom },
1487 { r.right, height - r.top }
1488 };
1489 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001490 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1491 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492}
1493
Mathias Agopian96f08192010-06-02 23:28:45 -07001494ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1495 const sp<LayerBaseClient>& lbc)
1496{
Mathias Agopian96f08192010-06-02 23:28:45 -07001497 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001498 size_t name = client->attachLayer(lbc);
1499
Mathias Agopian96f08192010-06-02 23:28:45 -07001500 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001501 Mutex::Autolock _l(mStateLock);
1502 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001503
Mathias Agopian4f113742011-05-03 16:21:41 -07001504 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001505}
1506
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001507status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508{
1509 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001510 status_t err = purgatorizeLayer_l(layer);
1511 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001512 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001513 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514}
1515
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001516status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517{
1518 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1519 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001520 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 return NO_ERROR;
1522 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001523 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524}
1525
Mathias Agopian9a112062009-04-17 19:36:26 -07001526status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1527{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001528 // First add the layer to the purgatory list, which makes sure it won't
1529 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001530 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001531 if (err >= 0) {
1532 mLayerPurgatory.add(layerBase);
1533 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001534
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001535 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001536
Mathias Agopian3d579642009-06-04 18:46:21 -07001537 // it's possible that we don't find a layer, because it might
1538 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001539 // from the user because there is a race between Client::destroySurface(),
1540 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001541 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1542}
1543
Mathias Agopiandea20b12011-05-03 17:04:02 -07001544uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1545{
1546 return android_atomic_release_load(&mTransactionFlags);
1547}
1548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1550{
1551 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1552}
1553
Mathias Agopianbb641242010-05-18 17:06:55 -07001554uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555{
1556 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1557 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001558 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 }
1560 return old;
1561}
1562
Mathias Agopian8b33f032012-07-24 20:43:54 -07001563void SurfaceFlinger::setTransactionState(
1564 const Vector<ComposerState>& state,
1565 const Vector<DisplayState>& displays,
1566 uint32_t flags)
1567{
Mathias Agopian698c0872011-06-28 19:09:31 -07001568 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001569 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001570
1571 size_t count = displays.size();
1572 for (size_t i=0 ; i<count ; i++) {
1573 const DisplayState& s(displays[i]);
1574 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001575 }
1576
Mathias Agopiane57f2922012-08-09 16:29:12 -07001577 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001578 for (size_t i=0 ; i<count ; i++) {
1579 const ComposerState& s(state[i]);
1580 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001581 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001582 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001583
Mathias Agopian386aa982011-11-07 21:58:03 -08001584 if (transactionFlags) {
1585 // this triggers the transaction
1586 setTransactionFlags(transactionFlags);
1587
1588 // if this is a synchronous transaction, wait for it to take effect
1589 // before returning.
1590 if (flags & eSynchronous) {
1591 mTransationPending = true;
1592 }
1593 while (mTransationPending) {
1594 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1595 if (CC_UNLIKELY(err != NO_ERROR)) {
1596 // just in case something goes wrong in SF, return to the
1597 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001598 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001599 mTransationPending = false;
1600 break;
1601 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001602 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 }
1604}
1605
Mathias Agopiane57f2922012-08-09 16:29:12 -07001606uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1607{
1608 uint32_t flags = 0;
1609 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001610 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001611 const uint32_t what = s.what;
1612 if (what & DisplayState::eSurfaceChanged) {
1613 if (disp.surface->asBinder() != s.surface->asBinder()) {
1614 disp.surface = s.surface;
1615 flags |= eDisplayTransactionNeeded;
1616 }
1617 }
1618 if (what & DisplayState::eLayerStackChanged) {
1619 if (disp.layerStack != s.layerStack) {
1620 disp.layerStack = s.layerStack;
1621 flags |= eDisplayTransactionNeeded;
1622 }
1623 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001624 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001625 if (disp.orientation != s.orientation) {
1626 disp.orientation = s.orientation;
1627 flags |= eDisplayTransactionNeeded;
1628 }
1629 if (disp.frame != s.frame) {
1630 disp.frame = s.frame;
1631 flags |= eDisplayTransactionNeeded;
1632 }
1633 if (disp.viewport != s.viewport) {
1634 disp.viewport = s.viewport;
1635 flags |= eDisplayTransactionNeeded;
1636 }
1637 }
1638 }
1639 return flags;
1640}
1641
1642uint32_t SurfaceFlinger::setClientStateLocked(
1643 const sp<Client>& client,
1644 const layer_state_t& s)
1645{
1646 uint32_t flags = 0;
1647 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1648 if (layer != 0) {
1649 const uint32_t what = s.what;
1650 if (what & layer_state_t::ePositionChanged) {
1651 if (layer->setPosition(s.x, s.y))
1652 flags |= eTraversalNeeded;
1653 }
1654 if (what & layer_state_t::eLayerChanged) {
1655 // NOTE: index needs to be calculated before we update the state
1656 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1657 if (layer->setLayer(s.z)) {
1658 mCurrentState.layersSortedByZ.removeAt(idx);
1659 mCurrentState.layersSortedByZ.add(layer);
1660 // we need traversal (state changed)
1661 // AND transaction (list changed)
1662 flags |= eTransactionNeeded|eTraversalNeeded;
1663 }
1664 }
1665 if (what & layer_state_t::eSizeChanged) {
1666 if (layer->setSize(s.w, s.h)) {
1667 flags |= eTraversalNeeded;
1668 }
1669 }
1670 if (what & layer_state_t::eAlphaChanged) {
1671 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1672 flags |= eTraversalNeeded;
1673 }
1674 if (what & layer_state_t::eMatrixChanged) {
1675 if (layer->setMatrix(s.matrix))
1676 flags |= eTraversalNeeded;
1677 }
1678 if (what & layer_state_t::eTransparentRegionChanged) {
1679 if (layer->setTransparentRegionHint(s.transparentRegion))
1680 flags |= eTraversalNeeded;
1681 }
1682 if (what & layer_state_t::eVisibilityChanged) {
1683 if (layer->setFlags(s.flags, s.mask))
1684 flags |= eTraversalNeeded;
1685 }
1686 if (what & layer_state_t::eCropChanged) {
1687 if (layer->setCrop(s.crop))
1688 flags |= eTraversalNeeded;
1689 }
1690 if (what & layer_state_t::eLayerStackChanged) {
1691 // NOTE: index needs to be calculated before we update the state
1692 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1693 if (layer->setLayerStack(s.layerStack)) {
1694 mCurrentState.layersSortedByZ.removeAt(idx);
1695 mCurrentState.layersSortedByZ.add(layer);
1696 // we need traversal (state changed)
1697 // AND transaction (list changed)
1698 flags |= eTransactionNeeded|eTraversalNeeded;
1699 }
1700 }
1701 }
1702 return flags;
1703}
1704
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001705sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001706 ISurfaceComposerClient::surface_data_t* params,
1707 const String8& name,
1708 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001709 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 uint32_t flags)
1711{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001712 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001713 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001714
1715 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001716 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001717 int(w), int(h));
1718 return surfaceHandle;
1719 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001720
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001721 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001722 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1723 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001724 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001726 case ISurfaceComposerClient::eFXSurfaceBlur:
1727 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001728 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001730 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001731 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001732 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 }
1734
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001735 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001736 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001737 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001738 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001740 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001741 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001742 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001743 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001744 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 }
1746
1747 return surfaceHandle;
1748}
1749
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001750sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001751 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001752 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001753 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754{
1755 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001756 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 case PIXEL_FORMAT_TRANSPARENT:
1758 case PIXEL_FORMAT_TRANSLUCENT:
1759 format = PIXEL_FORMAT_RGBA_8888;
1760 break;
1761 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001762#ifdef NO_RGBX_8888
1763 format = PIXEL_FORMAT_RGB_565;
1764#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001765 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001766#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 break;
1768 }
1769
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001770#ifdef NO_RGBX_8888
1771 if (format == PIXEL_FORMAT_RGBX_8888)
1772 format = PIXEL_FORMAT_RGBA_8888;
1773#endif
1774
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001775 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001776 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001777 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001778 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001779 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780 }
1781 return layer;
1782}
1783
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001784sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001785 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001786 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001787{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001788 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001789 return layer;
1790}
1791
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001792sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001793 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001794 uint32_t w, uint32_t h, uint32_t flags)
1795{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001796 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797 return layer;
1798}
1799
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001800status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801{
Mathias Agopian9a112062009-04-17 19:36:26 -07001802 /*
1803 * called by the window manager, when a surface should be marked for
1804 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001805 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001806 * The surface is removed from the current and drawing lists, but placed
1807 * in the purgatory queue, so it's not destroyed right-away (we need
1808 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001809 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001810
Mathias Agopian48d819a2009-09-10 19:41:18 -07001811 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001812 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001813 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001814
Mathias Agopian48d819a2009-09-10 19:41:18 -07001815 if (layer != 0) {
1816 err = purgatorizeLayer_l(layer);
1817 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001818 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001819 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001820 }
1821 return err;
1822}
1823
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001824status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001825{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001826 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001827 status_t err = NO_ERROR;
1828 sp<LayerBaseClient> l(layer.promote());
1829 if (l != NULL) {
1830 Mutex::Autolock _l(mStateLock);
1831 err = removeLayer_l(l);
1832 if (err == NAME_NOT_FOUND) {
1833 // The surface wasn't in the current list, which means it was
1834 // removed already, which means it is in the purgatory,
1835 // and need to be removed from there.
1836 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001837 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001838 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001839 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001840 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001841 "error removing layer=%p (%s)", l.get(), strerror(-err));
1842 }
1843 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844}
1845
Mathias Agopianb60314a2012-04-10 22:09:54 -07001846// ---------------------------------------------------------------------------
1847
Andy McFadden13a082e2012-08-24 10:16:42 -07001848void SurfaceFlinger::onInitializeDisplays() {
1849 // reset screen orientation
1850 Vector<ComposerState> state;
1851 Vector<DisplayState> displays;
1852 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001853 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001854 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001855 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001856 d.frame.makeInvalid();
1857 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001858 displays.add(d);
1859 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001860 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001861}
1862
1863void SurfaceFlinger::initializeDisplays() {
1864 class MessageScreenInitialized : public MessageBase {
1865 SurfaceFlinger* flinger;
1866 public:
1867 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1868 virtual bool handler() {
1869 flinger->onInitializeDisplays();
1870 return true;
1871 }
1872 };
1873 sp<MessageBase> msg = new MessageScreenInitialized(this);
1874 postMessageAsync(msg); // we may be called from main thread, use async message
1875}
1876
1877
Mathias Agopiancde87a32012-09-13 14:09:01 -07001878void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001879 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001880 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001881 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001882 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1883 // FIXME: eventthread only knows about the main display right now
1884 mEventThread->onScreenAcquired();
1885 }
Mathias Agopian20128302012-08-13 18:32:13 -07001886 mVisibleRegionsDirty = true;
1887 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888}
1889
Mathias Agopiancde87a32012-09-13 14:09:01 -07001890void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001891 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001892 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001893 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1894 // FIXME: eventthread only knows about the main display right now
1895 mEventThread->onScreenReleased();
1896 }
Mathias Agopian42977342012-08-05 00:40:46 -07001897 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001898 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001899 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001900 // from this point on, SF will stop drawing
1901 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902}
1903
Colin Cross8e533062012-06-07 13:17:52 -07001904void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001905 class MessageScreenAcquired : public MessageBase {
1906 SurfaceFlinger* flinger;
1907 public:
1908 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1909 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001910 // FIXME: should this be per-display?
1911 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001912 return true;
1913 }
1914 };
1915 sp<MessageBase> msg = new MessageScreenAcquired(this);
1916 postMessageSync(msg);
1917}
1918
Colin Cross8e533062012-06-07 13:17:52 -07001919void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001920 class MessageScreenReleased : public MessageBase {
1921 SurfaceFlinger* flinger;
1922 public:
1923 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1924 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001925 // FIXME: should this be per-display?
1926 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001927 return true;
1928 }
1929 };
1930 sp<MessageBase> msg = new MessageScreenReleased(this);
1931 postMessageSync(msg);
1932}
1933
1934// ---------------------------------------------------------------------------
1935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1937{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001938 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 char buffer[SIZE];
1940 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001941
1942 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 snprintf(buffer, SIZE, "Permission Denial: "
1944 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1945 IPCThreadState::self()->getCallingPid(),
1946 IPCThreadState::self()->getCallingUid());
1947 result.append(buffer);
1948 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001949 // Try to get the main lock, but don't insist if we can't
1950 // (this would indicate SF is stuck, but we want to be able to
1951 // print something in dumpsys).
1952 int retry = 3;
1953 while (mStateLock.tryLock()<0 && --retry>=0) {
1954 usleep(1000000);
1955 }
1956 const bool locked(retry >= 0);
1957 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001958 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001959 "SurfaceFlinger appears to be unresponsive, "
1960 "dumping anyways (no locks held)\n");
1961 result.append(buffer);
1962 }
1963
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001964 bool dumpAll = true;
1965 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001966 size_t numArgs = args.size();
1967 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001968 if ((index < numArgs) &&
1969 (args[index] == String16("--list"))) {
1970 index++;
1971 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001972 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001973 }
1974
1975 if ((index < numArgs) &&
1976 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001977 index++;
1978 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001979 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001980 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001981
1982 if ((index < numArgs) &&
1983 (args[index] == String16("--latency-clear"))) {
1984 index++;
1985 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001986 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001987 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001988 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001989
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001990 if (dumpAll) {
1991 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001992 }
1993
Mathias Agopian9795c422009-08-26 16:36:26 -07001994 if (locked) {
1995 mStateLock.unlock();
1996 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997 }
1998 write(fd, result.string(), result.size());
1999 return NO_ERROR;
2000}
2001
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002002void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
2003 String8& result, char* buffer, size_t SIZE) const
2004{
2005 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2006 const size_t count = currentLayers.size();
2007 for (size_t i=0 ; i<count ; i++) {
2008 const sp<LayerBase>& layer(currentLayers[i]);
2009 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2010 result.append(buffer);
2011 }
2012}
2013
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002014void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
2015 String8& result, char* buffer, size_t SIZE) const
2016{
2017 String8 name;
2018 if (index < args.size()) {
2019 name = String8(args[index]);
2020 index++;
2021 }
2022
2023 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2024 const size_t count = currentLayers.size();
2025 for (size_t i=0 ; i<count ; i++) {
2026 const sp<LayerBase>& layer(currentLayers[i]);
2027 if (name.isEmpty()) {
2028 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2029 result.append(buffer);
2030 }
2031 if (name.isEmpty() || (name == layer->getName())) {
2032 layer->dumpStats(result, buffer, SIZE);
2033 }
2034 }
2035}
2036
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002037void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
2038 String8& result, char* buffer, size_t SIZE) const
2039{
2040 String8 name;
2041 if (index < args.size()) {
2042 name = String8(args[index]);
2043 index++;
2044 }
2045
2046 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2047 const size_t count = currentLayers.size();
2048 for (size_t i=0 ; i<count ; i++) {
2049 const sp<LayerBase>& layer(currentLayers[i]);
2050 if (name.isEmpty() || (name == layer->getName())) {
2051 layer->clearStats();
2052 }
2053 }
2054}
2055
Andy McFadden4803b742012-09-24 19:07:20 -07002056/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2057{
2058 static const char* config =
2059 " [sf"
2060#ifdef NO_RGBX_8888
2061 " NO_RGBX_8888"
2062#endif
2063#ifdef HAS_CONTEXT_PRIORITY
2064 " HAS_CONTEXT_PRIORITY"
2065#endif
2066#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2067 " NEVER_DEFAULT_TO_ASYNC_MODE"
2068#endif
2069#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2070 " TARGET_DISABLE_TRIPLE_BUFFERING"
2071#endif
2072 "]";
2073 result.append(config);
2074}
2075
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002076void SurfaceFlinger::dumpAllLocked(
2077 String8& result, char* buffer, size_t SIZE) const
2078{
2079 // figure out if we're stuck somewhere
2080 const nsecs_t now = systemTime();
2081 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2082 const nsecs_t inTransaction(mDebugInTransaction);
2083 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2084 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2085
2086 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002087 * Dump library configuration.
2088 */
2089 result.append("Build configuration:");
2090 appendSfConfigString(result);
2091 appendUiConfigString(result);
2092 appendGuiConfigString(result);
2093 result.append("\n");
2094
2095 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002096 * Dump the visible layer list
2097 */
2098 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2099 const size_t count = currentLayers.size();
2100 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2101 result.append(buffer);
2102 for (size_t i=0 ; i<count ; i++) {
2103 const sp<LayerBase>& layer(currentLayers[i]);
2104 layer->dump(result, buffer, SIZE);
2105 }
2106
2107 /*
2108 * Dump the layers in the purgatory
2109 */
2110
2111 const size_t purgatorySize = mLayerPurgatory.size();
2112 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2113 result.append(buffer);
2114 for (size_t i=0 ; i<purgatorySize ; i++) {
2115 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2116 layer->shortDump(result, buffer, SIZE);
2117 }
2118
2119 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002120 * Dump Display state
2121 */
2122
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002123 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2124 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002125 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2126 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002127 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002128 }
2129
2130 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002131 * Dump SurfaceFlinger global state
2132 */
2133
2134 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2135 result.append(buffer);
2136
Mathias Agopian888c8222012-08-04 21:10:38 -07002137 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002138 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002139 const GLExtensions& extensions(GLExtensions::getInstance());
2140 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2141 extensions.getVendor(),
2142 extensions.getRenderer(),
2143 extensions.getVersion());
2144 result.append(buffer);
2145
2146 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002147 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002148 result.append(buffer);
2149
2150 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2151 result.append(buffer);
2152
Mathias Agopian42977342012-08-05 00:40:46 -07002153 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002154 snprintf(buffer, SIZE,
2155 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002156 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002157 result.append(buffer);
2158 snprintf(buffer, SIZE,
2159 " last eglSwapBuffers() time: %f us\n"
2160 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002161 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002162 " refresh-rate : %f fps\n"
2163 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002164 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002165 mLastSwapBufferTime/1000.0,
2166 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002167 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002168 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2169 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2170 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002171 result.append(buffer);
2172
2173 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2174 inSwapBuffersDuration/1000.0);
2175 result.append(buffer);
2176
2177 snprintf(buffer, SIZE, " transaction time: %f us\n",
2178 inTransactionDuration/1000.0);
2179 result.append(buffer);
2180
2181 /*
2182 * VSYNC state
2183 */
2184 mEventThread->dump(result, buffer, SIZE);
2185
2186 /*
2187 * Dump HWComposer state
2188 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002189 snprintf(buffer, SIZE, "h/w composer state:\n");
2190 result.append(buffer);
2191 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2192 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2193 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2194 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002195 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002196
2197 /*
2198 * Dump gralloc state
2199 */
2200 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2201 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002202}
2203
Keun young Park63f165f2012-08-31 10:53:36 -07002204bool SurfaceFlinger::startDdmConnection()
2205{
2206 void* libddmconnection_dso =
2207 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2208 if (!libddmconnection_dso) {
2209 return false;
2210 }
2211 void (*DdmConnection_start)(const char* name);
2212 DdmConnection_start =
2213 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2214 if (!DdmConnection_start) {
2215 dlclose(libddmconnection_dso);
2216 return false;
2217 }
2218 (*DdmConnection_start)(getServiceName());
2219 return true;
2220}
2221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002222status_t SurfaceFlinger::onTransact(
2223 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2224{
2225 switch (code) {
2226 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002227 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002228 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002229 case BLANK:
2230 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231 {
2232 // codes that require permission check
2233 IPCThreadState* ipc = IPCThreadState::self();
2234 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002235 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002236 if ((uid != AID_GRAPHICS) &&
2237 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002238 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002239 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2240 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002241 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002242 break;
2243 }
2244 case CAPTURE_SCREEN:
2245 {
2246 // codes that require permission check
2247 IPCThreadState* ipc = IPCThreadState::self();
2248 const int pid = ipc->getCallingPid();
2249 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002250 if ((uid != AID_GRAPHICS) &&
2251 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002252 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002253 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2254 return PERMISSION_DENIED;
2255 }
2256 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002257 }
2258 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002260 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2261 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002262 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002263 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002264 IPCThreadState* ipc = IPCThreadState::self();
2265 const int pid = ipc->getCallingPid();
2266 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002267 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002268 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269 return PERMISSION_DENIED;
2270 }
2271 int n;
2272 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002273 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002274 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002275 return NO_ERROR;
2276 case 1002: // SHOW_UPDATES
2277 n = data.readInt32();
2278 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002279 invalidateHwcGeometry();
2280 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002281 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002282 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002283 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002284 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002285 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002286 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002287 setTransactionFlags(
2288 eTransactionNeeded|
2289 eDisplayTransactionNeeded|
2290 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002291 return NO_ERROR;
2292 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002293 case 1006:{ // send empty update
2294 signalRefresh();
2295 return NO_ERROR;
2296 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002297 case 1008: // toggle use of hw composer
2298 n = data.readInt32();
2299 mDebugDisableHWC = n ? 1 : 0;
2300 invalidateHwcGeometry();
2301 repaintEverything();
2302 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002303 case 1009: // toggle use of transform hint
2304 n = data.readInt32();
2305 mDebugDisableTransformHint = n ? 1 : 0;
2306 invalidateHwcGeometry();
2307 repaintEverything();
2308 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002310 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002311 reply->writeInt32(0);
2312 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002313 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002314 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002315 return NO_ERROR;
2316 case 1013: {
2317 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002318 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2319 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002320 }
2321 return NO_ERROR;
2322 }
2323 }
2324 return err;
2325}
2326
Mathias Agopian53331da2011-08-22 21:44:41 -07002327void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002328 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002329 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002330}
2331
Mathias Agopian59119e62010-10-11 12:37:43 -07002332// ---------------------------------------------------------------------------
2333
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002334status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002335 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2336{
2337 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002338 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002339}
2340
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002341status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002342 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002343{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002344 ATRACE_CALL();
2345
Mathias Agopian59119e62010-10-11 12:37:43 -07002346 if (!GLExtensions::getInstance().haveFramebufferObject())
2347 return INVALID_OPERATION;
2348
2349 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002350 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2351 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002352 const uint32_t hw_w = hw->getWidth();
2353 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002354 GLfloat u = 1;
2355 GLfloat v = 1;
2356
2357 // make sure to clear all GL error flags
2358 while ( glGetError() != GL_NO_ERROR ) ;
2359
2360 // create a FBO
2361 GLuint name, tname;
2362 glGenTextures(1, &tname);
2363 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002364 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002366 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2367 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002368 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002369 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002370 GLint tw = (2 << (31 - clz(hw_w)));
2371 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002372 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2373 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002374 u = GLfloat(hw_w) / tw;
2375 v = GLfloat(hw_h) / th;
2376 }
2377 glGenFramebuffersOES(1, &name);
2378 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002379 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2380 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002381
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002382 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002383 glDisable(GL_TEXTURE_EXTERNAL_OES);
2384 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002385 glClearColor(0,0,0,1);
2386 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002387 glMatrixMode(GL_MODELVIEW);
2388 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002389 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002390 const size_t count = layers.size();
2391 for (size_t i=0 ; i<count ; ++i) {
2392 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002393 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002394 }
2395
Mathias Agopian42977342012-08-05 00:40:46 -07002396 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002397
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002398 // back to main framebuffer
2399 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002400 glDeleteFramebuffersOES(1, &name);
2401
2402 *textureName = tname;
2403 *uOut = u;
2404 *vOut = v;
2405 return NO_ERROR;
2406}
2407
2408// ---------------------------------------------------------------------------
2409
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002410status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002411 sp<IMemoryHeap>* heap,
2412 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002413 uint32_t sw, uint32_t sh,
2414 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002415{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002416 ATRACE_CALL();
2417
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 status_t result = PERMISSION_DENIED;
2419
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002420 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002421 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002422 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423
2424 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002425 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002426 const uint32_t hw_w = hw->getWidth();
2427 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002428
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002429 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002430 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002431 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002432 return PERMISSION_DENIED;
2433 }
2434
2435 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002436 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002437 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002438 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002439
2440 sw = (!sw) ? hw_w : sw;
2441 sh = (!sh) ? hw_h : sh;
2442 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002443 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002444
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002445// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2446// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002447
Mathias Agopian74c40c02010-09-29 13:02:36 -07002448 // make sure to clear all GL error flags
2449 while ( glGetError() != GL_NO_ERROR ) ;
2450
2451 // create a FBO
2452 GLuint name, tname;
2453 glGenRenderbuffersOES(1, &tname);
2454 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2455 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002456
Mathias Agopian74c40c02010-09-29 13:02:36 -07002457 glGenFramebuffersOES(1, &name);
2458 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2459 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2460 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2461
2462 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002463
Mathias Agopian74c40c02010-09-29 13:02:36 -07002464 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2465
2466 // invert everything, b/c glReadPixel() below will invert the FB
2467 glViewport(0, 0, sw, sh);
2468 glMatrixMode(GL_PROJECTION);
2469 glPushMatrix();
2470 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002471 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002472 glMatrixMode(GL_MODELVIEW);
2473
2474 // redraw the screen entirely...
2475 glClearColor(0,0,0,1);
2476 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002477
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002478 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002479 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002480 for (size_t i=0 ; i<count ; ++i) {
2481 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002482 const uint32_t z = layer->drawingState().z;
2483 if (z >= minLayerZ && z <= maxLayerZ) {
2484 if (filtering) layer->setFiltering(true);
2485 layer->draw(hw);
2486 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002487 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002488 }
2489
Mathias Agopian74c40c02010-09-29 13:02:36 -07002490 // check for errors and return screen capture
2491 if (glGetError() != GL_NO_ERROR) {
2492 // error while rendering
2493 result = INVALID_OPERATION;
2494 } else {
2495 // allocate shared memory large enough to hold the
2496 // screen capture
2497 sp<MemoryHeapBase> base(
2498 new MemoryHeapBase(size, 0, "screen-capture") );
2499 void* const ptr = base->getBase();
Mathias Agopian7b190512012-09-25 15:28:44 -07002500 if (ptr != MAP_FAILED) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002501 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002502 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002503 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2504 if (glGetError() == GL_NO_ERROR) {
2505 *heap = base;
2506 *w = sw;
2507 *h = sh;
2508 *f = PIXEL_FORMAT_RGBA_8888;
2509 result = NO_ERROR;
2510 }
2511 } else {
2512 result = NO_MEMORY;
2513 }
2514 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002515 glViewport(0, 0, hw_w, hw_h);
2516 glMatrixMode(GL_PROJECTION);
2517 glPopMatrix();
2518 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002519 } else {
2520 result = BAD_VALUE;
2521 }
2522
2523 // release FBO resources
2524 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2525 glDeleteRenderbuffersOES(1, &tname);
2526 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002527
Mathias Agopian42977342012-08-05 00:40:46 -07002528 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002529
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002530// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002531
Mathias Agopian74c40c02010-09-29 13:02:36 -07002532 return result;
2533}
2534
2535
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002536status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002537 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002538 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002539 uint32_t sw, uint32_t sh,
2540 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002541{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002542 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002543 return BAD_VALUE;
2544
2545 if (!GLExtensions::getInstance().haveFramebufferObject())
2546 return INVALID_OPERATION;
2547
2548 class MessageCaptureScreen : public MessageBase {
2549 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002550 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002551 sp<IMemoryHeap>* heap;
2552 uint32_t* w;
2553 uint32_t* h;
2554 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002555 uint32_t sw;
2556 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002557 uint32_t minLayerZ;
2558 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002559 status_t result;
2560 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002561 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002562 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002563 uint32_t sw, uint32_t sh,
2564 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002565 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002566 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2567 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2568 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002569 {
2570 }
2571 status_t getResult() const {
2572 return result;
2573 }
2574 virtual bool handler() {
2575 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002576 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002577 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002578 return true;
2579 }
2580 };
2581
2582 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002583 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002584 status_t res = postMessageSync(msg);
2585 if (res == NO_ERROR) {
2586 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2587 }
2588 return res;
2589}
2590
2591// ---------------------------------------------------------------------------
2592
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002593SurfaceFlinger::LayerVector::LayerVector() {
2594}
2595
2596SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2597 : SortedVector<sp<LayerBase> >(rhs) {
2598}
2599
2600int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2601 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002602{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002603 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002604 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2605 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002606
2607 uint32_t ls = l->currentState().layerStack;
2608 uint32_t rs = r->currentState().layerStack;
2609 if (ls != rs)
2610 return ls - rs;
2611
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002612 uint32_t lz = l->currentState().z;
2613 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002614 if (lz != rz)
2615 return lz - rz;
2616
2617 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002618}
2619
2620// ---------------------------------------------------------------------------
2621
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002622SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2623 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002624}
2625
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002626SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2627 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002628 viewport.makeInvalid();
2629 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002630}
2631
2632// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634}; // namespace android