blob: 0ce526637c07053ed3a5ff03da9b51381f3f29bb [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>
40#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/SurfaceTextureClient.h>
42
43#include <ui/GraphicBufferAllocator.h>
44#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045
Mathias Agopiancde87a32012-09-13 14:09:01 -070046#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <utils/String8.h>
48#include <utils/String16.h>
49#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Mathias Agopian921e6ac2012-07-23 23:11:29 -070052#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080055#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070056#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070057#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080058#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070059#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070062#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopiana4912602012-07-12 14:25:33 -070065#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopianf33e4b62012-09-20 16:54:14 -070066#include "DisplayHardware/GraphicBufferAlloc.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070067#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana67932f2011-04-20 14:20:59 -070069
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080070#define EGL_VERSION_HW_ANDROID 0x3143
71
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072#define DISPLAY_COUNT 1
73
74namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075// ---------------------------------------------------------------------------
76
Mathias Agopian99b49842011-06-27 16:05:52 -070077const String16 sHardwareTest("android.permission.HARDWARE_TEST");
78const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
79const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
80const String16 sDump("android.permission.DUMP");
81
82// ---------------------------------------------------------------------------
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084SurfaceFlinger::SurfaceFlinger()
85 : BnSurfaceComposer(), Thread(false),
86 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070087 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070089 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070092 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070094 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070095 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070096 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070097 mDebugInSwapBuffers(0),
98 mLastSwapBufferTime(0),
99 mDebugInTransaction(0),
100 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700101 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
Steve Blocka19954a2012-01-04 20:05:49 +0000103 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104
105 // debugging stuff...
106 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 property_get("debug.sf.showupdates", value, "0");
109 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111 property_get("debug.sf.ddms", value, "0");
112 mDebugDDMS = atoi(value);
113 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700114 if (!startDdmConnection()) {
115 // start failed, and DDMS debugging not enabled
116 mDebugDDMS = 0;
117 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700119 ALOGI_IF(mDebugRegion, "showupdates enabled");
120 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800123void SurfaceFlinger::onFirstRef()
124{
125 mEventQueue.init(this);
126
127 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
128
129 // Wait for the main thread to be done with its initialization
130 mReadyToRunBarrier.wait();
131}
132
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134SurfaceFlinger::~SurfaceFlinger()
135{
Mathias Agopiana4912602012-07-12 14:25:33 -0700136 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
137 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141void SurfaceFlinger::binderDied(const wp<IBinder>& who)
142{
143 // the window manager died on us. prepare its eulogy.
144
Andy McFadden13a082e2012-08-24 10:16:42 -0700145 // restore initial conditions (default device unblank, etc)
146 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800147
148 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700149 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150}
151
Mathias Agopian7e27f052010-05-28 14:22:23 -0700152sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopian96f08192010-06-02 23:28:45 -0700154 sp<ISurfaceComposerClient> bclient;
155 sp<Client> client(new Client(this));
156 status_t err = client->initCheck();
157 if (err == NO_ERROR) {
158 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return bclient;
161}
162
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700163sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700164{
165 class DisplayToken : public BBinder {
166 sp<SurfaceFlinger> flinger;
167 virtual ~DisplayToken() {
168 // no more references, this display must be terminated
169 Mutex::Autolock _l(flinger->mStateLock);
170 flinger->mCurrentState.displays.removeItem(this);
171 flinger->setTransactionFlags(eDisplayTransactionNeeded);
172 }
173 public:
174 DisplayToken(const sp<SurfaceFlinger>& flinger)
175 : flinger(flinger) {
176 }
177 };
178
179 sp<BBinder> token = new DisplayToken(this);
180
181 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700182 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700183 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700184 mCurrentState.displays.add(token, info);
185
186 return token;
187}
188
189sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700190 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700191 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
192 return NULL;
193 }
194 return mDefaultDisplays[id];
195}
196
Jamie Gennis9a78c902011-01-12 18:30:40 -0800197sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
198{
199 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
200 return gba;
201}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203void SurfaceFlinger::bootFinished()
204{
205 const nsecs_t now = systemTime();
206 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000207 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700208 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700209
210 // wait patiently for the window manager death
211 const String16 name("window");
212 sp<IBinder> window(defaultServiceManager()->getService(name));
213 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700214 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700215 }
216
217 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700218 // formerly we would just kill the process, but we now ask it to exit so it
219 // can choose where to stop the animation.
220 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700223void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
224 class MessageDestroyGLTexture : public MessageBase {
225 GLuint texture;
226 public:
227 MessageDestroyGLTexture(GLuint texture)
228 : texture(texture) {
229 }
230 virtual bool handler() {
231 glDeleteTextures(1, &texture);
232 return true;
233 }
234 };
235 postMessageAsync(new MessageDestroyGLTexture(texture));
236}
237
Mathias Agopiana4912602012-07-12 14:25:33 -0700238status_t SurfaceFlinger::selectConfigForPixelFormat(
239 EGLDisplay dpy,
240 EGLint const* attrs,
241 PixelFormat format,
242 EGLConfig* outConfig)
243{
244 EGLConfig config = NULL;
245 EGLint numConfigs = -1, n=0;
246 eglGetConfigs(dpy, NULL, 0, &numConfigs);
247 EGLConfig* const configs = new EGLConfig[numConfigs];
248 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700249
Mathias Agopiana4912602012-07-12 14:25:33 -0700250 for (int i=0 ; i<n ; i++) {
251 EGLint nativeVisualId = 0;
252 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
253 if (nativeVisualId>0 && format == nativeVisualId) {
254 *outConfig = configs[i];
255 delete [] configs;
256 return NO_ERROR;
257 }
258 }
259 delete [] configs;
260 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261}
262
Mathias Agopiana4912602012-07-12 14:25:33 -0700263EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
264 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
265 // it is to be used with WIFI displays
266 EGLConfig config;
267 EGLint dummy;
268 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700269
Mathias Agopiana4912602012-07-12 14:25:33 -0700270 EGLint attribs[] = {
271 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Jesse Hallf21cffa2012-09-19 21:00:49 -0700272 // The rest of the attributes must be in this order and at the end
273 // of the list; we rely on that for fallback searches below.
Mathias Agopiancde87a32012-09-13 14:09:01 -0700274 EGL_RED_SIZE, 8,
275 EGL_GREEN_SIZE, 8,
276 EGL_BLUE_SIZE, 8,
Mathias Agopiana4912602012-07-12 14:25:33 -0700277 EGL_RECORDABLE_ANDROID, EGL_TRUE,
278 EGL_NONE
279 };
280 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700281 if (!err)
282 goto success;
283
284 // maybe we failed because of EGL_RECORDABLE_ANDROID
285 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
286 attribs[NELEM(attribs) - 3] = EGL_NONE;
287 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
288 if (!err)
289 goto success;
290
291 // allow less than 24-bit color; the non-gpu-accelerated emulator only
292 // supports 16-bit color
293 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
294 attribs[NELEM(attribs) - 9] = EGL_NONE;
295 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
296 if (!err)
297 goto success;
298
299 // this EGL is too lame for Android
300 ALOGE("no suitable EGLConfig found, giving up");
301
302 return 0;
303
304success:
305 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700306 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 return config;
308}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309
Mathias Agopiana4912602012-07-12 14:25:33 -0700310EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
311 // Also create our EGLContext
312 EGLint contextAttributes[] = {
313#ifdef EGL_IMG_context_priority
314#ifdef HAS_CONTEXT_PRIORITY
315#warning "using EGL_IMG_context_priority"
316 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
317#endif
318#endif
319 EGL_NONE, EGL_NONE
320 };
321 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
322 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
323 return ctxt;
324}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325
Mathias Agopiancde87a32012-09-13 14:09:01 -0700326void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700327 GLExtensions& extensions(GLExtensions::getInstance());
328 extensions.initWithGLStrings(
329 glGetString(GL_VENDOR),
330 glGetString(GL_RENDERER),
331 glGetString(GL_VERSION),
332 glGetString(GL_EXTENSIONS),
333 eglQueryString(display, EGL_VENDOR),
334 eglQueryString(display, EGL_VERSION),
335 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700336
Mathias Agopiana4912602012-07-12 14:25:33 -0700337 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
338 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700341 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 glShadeModel(GL_FLAT);
344 glDisable(GL_DITHER);
345 glDisable(GL_CULL_FACE);
346
Mathias Agopiana4912602012-07-12 14:25:33 -0700347 struct pack565 {
348 inline uint16_t operator() (int r, int g, int b) const {
349 return (r<<11)|(g<<5)|b;
350 }
351 } pack565;
352
Jamie Gennis9575f602011-10-07 14:51:16 -0700353 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
354 glGenTextures(1, &mProtectedTexName);
355 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
356 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
357 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
359 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
360 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
361 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362
Mathias Agopiana4912602012-07-12 14:25:33 -0700363 // print some debugging info
364 EGLint r,g,b,a;
365 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
366 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
367 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
368 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
369 ALOGI("EGL informations:");
370 ALOGI("vendor : %s", extensions.getEglVendor());
371 ALOGI("version : %s", extensions.getEglVersion());
372 ALOGI("extensions: %s", extensions.getEglExtension());
373 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
374 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
375 ALOGI("OpenGL ES informations:");
376 ALOGI("vendor : %s", extensions.getVendor());
377 ALOGI("renderer : %s", extensions.getRenderer());
378 ALOGI("version : %s", extensions.getVersion());
379 ALOGI("extensions: %s", extensions.getExtension());
380 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
381 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
382}
383
Mathias Agopiana4912602012-07-12 14:25:33 -0700384status_t SurfaceFlinger::readyToRun()
385{
386 ALOGI( "SurfaceFlinger's main thread ready to run. "
387 "Initializing graphics H/W...");
388
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700389 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700390 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
391 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700392
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700393 // Initialize the H/W composer object. There may or may not be an
394 // actual hardware composer underneath.
395 mHwc = new HWComposer(this,
396 *static_cast<HWComposer::EventHandler *>(this));
397
Mathias Agopiana4912602012-07-12 14:25:33 -0700398 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700399 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700400 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
401 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700402
Mathias Agopianda27af92012-09-13 18:17:13 -0700403 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
404 "couldn't create EGLContext");
405
Mathias Agopiancde87a32012-09-13 14:09:01 -0700406 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700407 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700408 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700409 mDefaultDisplays[i] = new BBinder();
Mathias Agopianf5a33922012-09-19 18:16:22 -0700410 wp<IBinder> token = mDefaultDisplays[i];
411 mCurrentState.displays.add(token, DisplayDeviceState(type));
412
413 // set-up the displays that are already connected
414 if (mHwc->isConnected(i)) {
415 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
416 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
417 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
418 sp<DisplayDevice> hw = new DisplayDevice(this,
419 type, token, stc, fbs, mEGLConfig);
420
421 if (i > DisplayDevice::DISPLAY_PRIMARY) {
422 // FIXME: currently we don't really handle blank/unblank
423 // for displays other than the main display, so we always
424 // assume a connected display is unblanked.
425 hw->acquireScreen();
426 }
427 mDisplays.add(token, hw);
428 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700429 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700430
Mathias Agopianf5a33922012-09-19 18:16:22 -0700431 // we need a GL context current in a few places, when initializing
432 // OpenGL ES (see below), or creating a layer,
433 // or when a texture is (asynchronously) destroyed, and for that
434 // we need a valid surface, so it's convenient to use the main display
435 // for that.
436 sp<const DisplayDevice> hw = getDefaultDisplayDevice();
Mathias Agopiancde87a32012-09-13 14:09:01 -0700437
Mathias Agopiana4912602012-07-12 14:25:33 -0700438 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700439 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
440 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800441
Mathias Agopian028508c2012-07-25 21:12:12 -0700442 // start the EventThread
443 mEventThread = new EventThread(this);
444 mEventQueue.setEventThread(mEventThread);
445
Mathias Agopian92a979a2012-08-02 18:32:23 -0700446 // initialize our drawing state
447 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700448
Mathias Agopiancde87a32012-09-13 14:09:01 -0700449
Mathias Agopiana4912602012-07-12 14:25:33 -0700450 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800451 mReadyToRunBarrier.open();
452
Andy McFadden13a082e2012-08-24 10:16:42 -0700453 // set initial conditions (e.g. unblank default device)
454 initializeDisplays();
455
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700456 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700457 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459 return NO_ERROR;
460}
461
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700462int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
463 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
464 type : mHwc->allocateDisplayId();
465}
466
Mathias Agopiana67e4182012-06-19 17:26:12 -0700467void SurfaceFlinger::startBootAnim() {
468 // start boot animation
469 property_set("service.bootanim.exit", "0");
470 property_set("ctl.start", "bootanim");
471}
472
Mathias Agopiana4912602012-07-12 14:25:33 -0700473uint32_t SurfaceFlinger::getMaxTextureSize() const {
474 return mMaxTextureSize;
475}
476
477uint32_t SurfaceFlinger::getMaxViewportDims() const {
478 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
479 mMaxViewportDims[0] : mMaxViewportDims[1];
480}
481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800483
Jamie Gennis582270d2011-08-17 18:19:00 -0700484bool SurfaceFlinger::authenticateSurfaceTexture(
485 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800486 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700487 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800488
489 // Check the visible layer list for the ISurface
490 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
491 size_t count = currentLayers.size();
492 for (size_t i=0 ; i<count ; i++) {
493 const sp<LayerBase>& layer(currentLayers[i]);
494 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700495 if (lbc != NULL) {
496 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
497 if (lbcBinder == surfaceTextureBinder) {
498 return true;
499 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800500 }
501 }
502
503 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700504 // SurfaceTexture gets destroyed before all the clients are done using it,
505 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800506 // will instead fail later on when the client tries to use the surface,
507 // which should be reported as "surface XYZ returned an -ENODEV". The
508 // purgatorized layers are no less authentic than the visible ones, so this
509 // should not cause any harm.
510 size_t purgatorySize = mLayerPurgatory.size();
511 for (size_t i=0 ; i<purgatorySize ; i++) {
512 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
513 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700514 if (lbc != NULL) {
515 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
516 if (lbcBinder == surfaceTextureBinder) {
517 return true;
518 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800519 }
520 }
521
522 return false;
523}
524
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700525status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700526 int32_t type = BAD_VALUE;
527 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
528 if (display == mDefaultDisplays[i]) {
529 type = i;
530 break;
531 }
532 }
533
534 if (type < 0) {
535 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700536 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700537
538 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700539 float xdpi = hwc.getDpiX(type);
540 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700541
542 // TODO: Not sure if display density should handled by SF any longer
543 class Density {
544 static int getDensityFromProperty(char const* propName) {
545 char property[PROPERTY_VALUE_MAX];
546 int density = 0;
547 if (property_get(propName, property, NULL) > 0) {
548 density = atoi(property);
549 }
550 return density;
551 }
552 public:
553 static int getEmuDensity() {
554 return getDensityFromProperty("qemu.sf.lcd_density"); }
555 static int getBuildDensity() {
556 return getDensityFromProperty("ro.sf.lcd_density"); }
557 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700558
559 if (type == DisplayDevice::DISPLAY_PRIMARY) {
560 // The density of the device is provided by a build property
561 float density = Density::getBuildDensity() / 160.0f;
562 if (density == 0) {
563 // the build doesn't provide a density -- this is wrong!
564 // use xdpi instead
565 ALOGE("ro.sf.lcd_density must be defined as a build property");
566 density = xdpi / 160.0f;
567 }
568 if (Density::getEmuDensity()) {
569 // if "qemu.sf.lcd_density" is specified, it overrides everything
570 xdpi = ydpi = density = Density::getEmuDensity();
571 density /= 160.0f;
572 }
573 info->density = density;
574
575 // TODO: this needs to go away (currently needed only by webkit)
576 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
577 info->orientation = hw->getOrientation();
578 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
579 } else {
580 // TODO: where should this value come from?
581 static const int TV_DENSITY = 213;
582 info->density = TV_DENSITY / 160.0f;
583 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700584 }
585
Mathias Agopian1604f772012-09-18 21:54:42 -0700586 info->w = hwc.getWidth(type);
587 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700588 info->xdpi = xdpi;
589 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700590 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Mathias Agopian888c8222012-08-04 21:10:38 -0700591 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700592}
593
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800594// ----------------------------------------------------------------------------
595
596sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800597 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700598}
599
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700600void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700601
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700602 sp<IBinder> token;
603 { // scope for the lock
604 Mutex::Autolock _l(mStateLock);
605 token = mExtDisplayToken;
606 }
607
608 if (token == 0) {
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700609 token = createDisplay(String8("Display from connectDisplay"));
Mathias Agopian3094df32012-06-18 18:06:45 -0700610 }
611
612 { // scope for the lock
613 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700614 if (surface == 0) {
615 // release our current display. we're guarantee to have
616 // a reference to it (token), while we hold the lock
617 mExtDisplayToken = 0;
618 } else {
619 mExtDisplayToken = token;
620 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700621
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700622 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
623 info.surface = surface;
624 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700625 }
626}
627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800629
630void SurfaceFlinger::waitForEvent() {
631 mEventQueue.waitMessage();
632}
633
634void SurfaceFlinger::signalTransaction() {
635 mEventQueue.invalidate();
636}
637
638void SurfaceFlinger::signalLayerUpdate() {
639 mEventQueue.invalidate();
640}
641
642void SurfaceFlinger::signalRefresh() {
643 mEventQueue.refresh();
644}
645
646status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
647 nsecs_t reltime, uint32_t flags) {
648 return mEventQueue.postMessage(msg, reltime);
649}
650
651status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
652 nsecs_t reltime, uint32_t flags) {
653 status_t res = mEventQueue.postMessage(msg, reltime);
654 if (res == NO_ERROR) {
655 msg->wait();
656 }
657 return res;
658}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659
Mathias Agopian4fec8732012-06-29 14:12:52 -0700660bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 return true;
663}
664
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700665void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700666 if (mEventThread == NULL) {
667 // This is a temporary workaround for b/7145521. A non-null pointer
668 // does not mean EventThread has finished initializing, so this
669 // is not a correct fix.
670 ALOGW("WARNING: EventThread not started, ignoring vsync");
671 return;
672 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700673 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
674 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700675 mEventThread->onVSyncReceived(type, timestamp);
676 }
677}
678
679void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
680 if (mEventThread == NULL) {
681 // This is a temporary workaround for b/7145521. A non-null pointer
682 // does not mean EventThread has finished initializing, so this
683 // is not a correct fix.
684 ALOGW("WARNING: EventThread not started, ignoring hotplug");
685 return;
686 }
687 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
688 // we should only receive DisplayDevice::DisplayType from the vsync callback
689 mEventThread->onHotplugReceived(type, connected);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700690 }
Mathias Agopian86303202012-07-24 22:46:10 -0700691}
692
693void SurfaceFlinger::eventControl(int event, int enabled) {
694 getHwComposer().eventControl(event, enabled);
695}
696
Mathias Agopian4fec8732012-06-29 14:12:52 -0700697void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800698 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800699 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700 case MessageQueue::INVALIDATE:
701 handleMessageTransaction();
702 handleMessageInvalidate();
703 signalRefresh();
704 break;
705 case MessageQueue::REFRESH:
706 handleMessageRefresh();
707 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800708 }
709}
710
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700712 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700713 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700714 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700715 }
716}
717
718void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700719 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700720 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700721}
722
723void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700724 ATRACE_CALL();
725 preComposition();
726 rebuildLayerStacks();
727 setUpHWComposer();
728 doDebugFlashRegions();
729 doComposition();
730 postComposition();
731}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700732
Mathias Agopiancd60f992012-08-16 16:28:27 -0700733void SurfaceFlinger::doDebugFlashRegions()
734{
735 // is debugging enabled
736 if (CC_LIKELY(!mDebugRegion))
737 return;
738
739 const bool repaintEverything = mRepaintEverything;
740 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
741 const sp<DisplayDevice>& hw(mDisplays[dpy]);
742 if (hw->canDraw()) {
743 // transform the dirty region into this screen's coordinate space
744 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
745 if (!dirtyRegion.isEmpty()) {
746 // redraw the whole screen
747 doComposeSurfaces(hw, Region(hw->bounds()));
748
749 // and draw the dirty region
750 glDisable(GL_TEXTURE_EXTERNAL_OES);
751 glDisable(GL_TEXTURE_2D);
752 glDisable(GL_BLEND);
753 glColor4f(1, 0, 1, 1);
754 const int32_t height = hw->getHeight();
755 Region::const_iterator it = dirtyRegion.begin();
756 Region::const_iterator const end = dirtyRegion.end();
757 while (it != end) {
758 const Rect& r = *it++;
759 GLfloat vertices[][2] = {
760 { r.left, height - r.top },
761 { r.left, height - r.bottom },
762 { r.right, height - r.bottom },
763 { r.right, height - r.top }
764 };
765 glVertexPointer(2, GL_FLOAT, 0, vertices);
766 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
767 }
768 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700769 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 }
771 }
772 }
773
774 postFramebuffer();
775
776 if (mDebugRegion > 1) {
777 usleep(mDebugRegion * 1000);
778 }
779}
780
781void SurfaceFlinger::preComposition()
782{
783 bool needExtraInvalidate = false;
784 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
785 const size_t count = currentLayers.size();
786 for (size_t i=0 ; i<count ; i++) {
787 if (currentLayers[i]->onPreComposition()) {
788 needExtraInvalidate = true;
789 }
790 }
791 if (needExtraInvalidate) {
792 signalLayerUpdate();
793 }
794}
795
796void SurfaceFlinger::postComposition()
797{
798 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
799 const size_t count = currentLayers.size();
800 for (size_t i=0 ; i<count ; i++) {
801 currentLayers[i]->onPostComposition();
802 }
803}
804
805void SurfaceFlinger::rebuildLayerStacks() {
806 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700807 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700808 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700809 mVisibleRegionsDirty = false;
810 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700811
Mathias Agopian87baae12012-07-31 12:38:26 -0700812 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700813 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700814 Region opaqueRegion;
815 Region dirtyRegion;
816 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700817 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700818 const Transform& tr(hw->getTransform());
819 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700820 if (hw->canDraw()) {
821 SurfaceFlinger::computeVisibleRegions(currentLayers,
822 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700823
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700824 const size_t count = currentLayers.size();
825 for (size_t i=0 ; i<count ; i++) {
826 const sp<LayerBase>& layer(currentLayers[i]);
827 const Layer::State& s(layer->drawingState());
828 if (s.layerStack == hw->getLayerStack()) {
829 Region visibleRegion(tr.transform(layer->visibleRegion));
830 visibleRegion.andSelf(bounds);
831 if (!visibleRegion.isEmpty()) {
832 layersSortedByZ.add(layer);
833 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700834 }
835 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700836 }
Mathias Agopian42977342012-08-05 00:40:46 -0700837 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700838 hw->undefinedRegion.set(bounds);
839 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
840 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700841 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700842 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700843}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700844
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700846 HWComposer& hwc(getHwComposer());
847 if (hwc.initCheck() == NO_ERROR) {
848 // build the h/w work list
849 const bool workListsDirty = mHwWorkListDirty;
850 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700851 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700852 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700853 const int32_t id = hw->getHwcDisplayId();
854 if (id >= 0) {
855 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700856 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700857 const size_t count = currentLayers.size();
858 if (hwc.createWorkList(id, count) >= 0) {
859 HWComposer::LayerListIterator cur = hwc.begin(id);
860 const HWComposer::LayerListIterator end = hwc.end(id);
861 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
862 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700863
Mathias Agopiane60b0682012-08-21 23:34:09 -0700864 if (CC_UNLIKELY(workListsDirty)) {
865 layer->setGeometry(hw, *cur);
866 if (mDebugDisableHWC || mDebugRegion) {
867 cur->setSkip(true);
868 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700869 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700870
Mathias Agopiane60b0682012-08-21 23:34:09 -0700871 /*
872 * update the per-frame h/w composer data for each layer
873 * and build the transparent region of the FB
874 */
875 layer->setPerFrameData(hw, *cur);
876 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700877 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700878 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700879 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700880 status_t err = hwc.prepare();
881 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
882 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700884
Mathias Agopiancd60f992012-08-16 16:28:27 -0700885void SurfaceFlinger::doComposition() {
886 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700887 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700888 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700889 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700890 if (hw->canDraw()) {
891 // transform the dirty region into this screen's coordinate space
892 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
893 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700894 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700895 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700896 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700897 hw->dirtyRegion.clear();
898 hw->flip(hw->swapRegion);
899 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700900 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700901 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700902 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700903 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700904 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700905}
906
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907void SurfaceFlinger::postFramebuffer()
908{
Mathias Agopian841cde52012-03-01 15:44:37 -0800909 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800910
Mathias Agopiana44b0412011-10-16 18:46:35 -0700911 const nsecs_t now = systemTime();
912 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700913
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700914 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700915 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700916 // FIXME: EGL spec says:
917 // "surface must be bound to the calling thread's current context,
918 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700919 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700920 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700921 }
922
Mathias Agopian92a979a2012-08-02 18:32:23 -0700923 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700924 sp<const DisplayDevice> hw(mDisplays[dpy]);
925 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700926 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700927 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700928 int32_t id = hw->getHwcDisplayId();
929 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700930 HWComposer::LayerListIterator cur = hwc.begin(id);
931 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700932 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700933 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700934 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700935 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700936 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700937 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700938 }
Jesse Hallef194142012-06-14 14:45:17 -0700939 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800940 }
941
Mathias Agopiana44b0412011-10-16 18:46:35 -0700942 mLastSwapBufferTime = systemTime() - now;
943 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944}
945
Mathias Agopian87baae12012-07-31 12:38:26 -0700946void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947{
Mathias Agopian841cde52012-03-01 15:44:37 -0800948 ATRACE_CALL();
949
Mathias Agopianca4d3602011-05-19 15:38:14 -0700950 Mutex::Autolock _l(mStateLock);
951 const nsecs_t now = systemTime();
952 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
Mathias Agopianca4d3602011-05-19 15:38:14 -0700954 // Here we're guaranteed that some transaction flags are set
955 // so we can call handleTransactionLocked() unconditionally.
956 // We call getTransactionFlags(), which will also clear the flags,
957 // with mStateLock held to guarantee that mCurrentState won't change
958 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700959
Mathias Agopiane57f2922012-08-09 16:29:12 -0700960 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700961 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700962
Mathias Agopianca4d3602011-05-19 15:38:14 -0700963 mLastTransactionTime = systemTime() - now;
964 mDebugInTransaction = 0;
965 invalidateHwcGeometry();
966 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700967}
968
Mathias Agopian87baae12012-07-31 12:38:26 -0700969void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700970{
971 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 const size_t count = currentLayers.size();
973
974 /*
975 * Traversal of the children
976 * (perform the transaction for each of them if needed)
977 */
978
Mathias Agopian3559b072012-08-15 13:46:03 -0700979 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700981 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
983 if (!trFlags) continue;
984
985 const uint32_t flags = layer->doTransaction(0);
986 if (flags & Layer::eVisibleRegion)
987 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 }
989 }
990
991 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700992 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 */
994
Mathias Agopiane57f2922012-08-09 16:29:12 -0700995 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700996 // here we take advantage of Vector's copy-on-write semantics to
997 // improve performance by skipping the transaction entirely when
998 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700999 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1000 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001001 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001003 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001004 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001005
1006 // find the displays that were removed
1007 // (ie: in drawing state but not in current state)
1008 // also handle displays that changed
1009 // (ie: displays that are in both lists)
1010 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001011 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1012 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001013 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001014 if (!draw[i].isMainDisplay()) {
1015 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001016 } else {
1017 ALOGW("trying to remove the main display");
1018 }
1019 } else {
1020 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001021 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001022 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001023 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001024 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001025 // recreating the DisplayDevice, so we just remove it
1026 // from the drawing state, so that it get re-added
1027 // below.
1028 mDisplays.removeItem(display);
1029 mDrawingState.displays.removeItemsAt(i);
1030 dc--; i--;
1031 // at this point we must loop to the next item
1032 continue;
1033 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001034
Mathias Agopian93997a82012-08-29 17:30:36 -07001035 const sp<DisplayDevice>& disp(getDisplayDevice(display));
1036 if (disp != NULL) {
1037 if (state.layerStack != draw[i].layerStack) {
1038 disp->setLayerStack(state.layerStack);
1039 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001040 if ((state.orientation != draw[i].orientation)
1041 || (state.viewport != draw[i].viewport)
1042 || (state.frame != draw[i].frame))
1043 {
1044 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001045 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001046 }
Andy McFadden69052052012-09-14 16:10:11 -07001047
1048 // Walk through all the layers in currentLayers,
1049 // and update their transform hint.
1050 //
1051 // TODO: we could be much more clever about which
1052 // layers we touch and how often we do these updates
1053 // (e.g. only touch the layers associated with this
1054 // display, and only on a rotation).
1055 for (size_t i = 0; i < count; i++) {
1056 const sp<LayerBase>& layerBase = currentLayers[i];
1057 layerBase->updateTransformHint();
1058 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 }
1060 }
1061 }
1062
1063 // find displays that were added
1064 // (ie: in current state but not in drawing state)
1065 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001066 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001067 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001068
1069 sp<FramebufferSurface> fbs;
1070 sp<SurfaceTextureClient> stc;
1071 if (!state.isVirtualDisplay()) {
1072
1073 ALOGE_IF(state.surface!=NULL,
1074 "adding a supported display, but rendering "
1075 "surface is provided (%p), ignoring it",
1076 state.surface.get());
1077
1078 // for supported (by hwc) displays we provide our
1079 // own rendering surface
Mathias Agopianf5a33922012-09-19 18:16:22 -07001080 fbs = new FramebufferSurface(*mHwc, state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001081 stc = new SurfaceTextureClient(
1082 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1083 } else {
1084 if (state.surface != NULL) {
1085 stc = new SurfaceTextureClient(state.surface);
1086 }
1087 }
1088
1089 const wp<IBinder>& display(curr.keyAt(i));
1090 if (stc != NULL) {
1091 sp<DisplayDevice> hw = new DisplayDevice(this,
1092 state.type, display, stc, fbs, mEGLConfig);
1093 hw->setLayerStack(state.layerStack);
1094 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001095 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001096 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001097 mDisplays.add(display, hw);
1098 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1099 // notify the system that this display is now up
1100 // (note onScreenAcquired() is safe to call from
1101 // here because we're in the main thread)
1102 onScreenAcquired(hw);
1103 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001104 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001105 }
1106 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001108 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109
Mathias Agopian3559b072012-08-15 13:46:03 -07001110 /*
1111 * Perform our own transaction if needed
1112 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001113
Mathias Agopiancd60f992012-08-16 16:28:27 -07001114 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1115 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001116 // layers have been added
1117 mVisibleRegionsDirty = true;
1118 }
1119
1120 // some layers might have been removed, so
1121 // we need to update the regions they're exposing.
1122 if (mLayersRemoved) {
1123 mLayersRemoved = false;
1124 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001125 const size_t count = previousLayers.size();
1126 for (size_t i=0 ; i<count ; i++) {
1127 const sp<LayerBase>& layer(previousLayers[i]);
1128 if (currentLayers.indexOf(layer) < 0) {
1129 // this layer is not visible anymore
1130 // TODO: we could traverse the tree from front to back and
1131 // compute the actual visible region
1132 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001133 const Layer::State& s(layer->drawingState());
1134 Region visibleReg = s.transform.transform(
1135 Region(Rect(s.active.w, s.active.h)));
1136 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 }
1140
1141 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001142}
1143
1144void SurfaceFlinger::commitTransaction()
1145{
1146 if (!mLayersPendingRemoval.isEmpty()) {
1147 // Notify removed layers now that they can't be drawn from
1148 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1149 mLayersPendingRemoval[i]->onRemoved();
1150 }
1151 mLayersPendingRemoval.clear();
1152 }
1153
1154 mDrawingState = mCurrentState;
1155 mTransationPending = false;
1156 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157}
1158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001160 const LayerVector& currentLayers, uint32_t layerStack,
1161 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162{
Mathias Agopian841cde52012-03-01 15:44:37 -08001163 ATRACE_CALL();
1164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 Region aboveOpaqueLayers;
1166 Region aboveCoveredLayers;
1167 Region dirty;
1168
Mathias Agopian87baae12012-07-31 12:38:26 -07001169 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170
1171 size_t i = currentLayers.size();
1172 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001173 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174
1175 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001176 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177
Mathias Agopian87baae12012-07-31 12:38:26 -07001178 // only consider the layers on the given later stack
1179 if (s.layerStack != layerStack)
1180 continue;
1181
Mathias Agopianab028732010-03-16 16:41:46 -07001182 /*
1183 * opaqueRegion: area of a surface that is fully opaque.
1184 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001186
1187 /*
1188 * visibleRegion: area of a surface that is visible on screen
1189 * and not fully transparent. This is essentially the layer's
1190 * footprint minus the opaque regions above it.
1191 * Areas covered by a translucent surface are considered visible.
1192 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001194
1195 /*
1196 * coveredRegion: area of a surface that is covered by all
1197 * visible regions above it (which includes the translucent areas).
1198 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001200
1201
1202 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001203 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001204 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001205 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001207 if (!visibleRegion.isEmpty()) {
1208 // Remove the transparent area from the visible region
1209 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001210 Region transparentRegionScreen;
1211 const Transform tr(s.transform);
1212 if (tr.transformed()) {
1213 if (tr.preserveRects()) {
1214 // transform the transparent region
1215 transparentRegionScreen = tr.transform(s.transparentRegion);
1216 } else {
1217 // transformation too complex, can't do the
1218 // transparent region optimization.
1219 transparentRegionScreen.clear();
1220 }
1221 } else {
1222 transparentRegionScreen = s.transparentRegion;
1223 }
1224 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226
Mathias Agopianab028732010-03-16 16:41:46 -07001227 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001228 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001229 if (s.alpha==255 && !translucent &&
1230 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1231 // the opaque region is the layer's footprint
1232 opaqueRegion = visibleRegion;
1233 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 }
1235 }
1236
Mathias Agopianab028732010-03-16 16:41:46 -07001237 // Clip the covered region to the visible region
1238 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1239
1240 // Update aboveCoveredLayers for next (lower) layer
1241 aboveCoveredLayers.orSelf(visibleRegion);
1242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 // subtract the opaque region covered by the layers above us
1244 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245
1246 // compute this layer's dirty region
1247 if (layer->contentDirty) {
1248 // we need to invalidate the whole region
1249 dirty = visibleRegion;
1250 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001251 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 layer->contentDirty = false;
1253 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001254 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001255 * the exposed region consists of two components:
1256 * 1) what's VISIBLE now and was COVERED before
1257 * 2) what's EXPOSED now less what was EXPOSED before
1258 *
1259 * note that (1) is conservative, we start with the whole
1260 * visible region but only keep what used to be covered by
1261 * something -- which mean it may have been exposed.
1262 *
1263 * (2) handles areas that were not covered by anything but got
1264 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001265 */
Mathias Agopianab028732010-03-16 16:41:46 -07001266 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001267 const Region oldVisibleRegion = layer->visibleRegion;
1268 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001269 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1270 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 }
1272 dirty.subtractSelf(aboveOpaqueLayers);
1273
1274 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001275 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276
Mathias Agopianab028732010-03-16 16:41:46 -07001277 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 // Store the visible region is screen space
1281 layer->setVisibleRegion(visibleRegion);
1282 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 }
1284
Mathias Agopian87baae12012-07-31 12:38:26 -07001285 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286}
1287
Mathias Agopian87baae12012-07-31 12:38:26 -07001288void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1289 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001290 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001291 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1292 if (hw->getLayerStack() == layerStack) {
1293 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001294 }
1295 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001296}
1297
1298void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001300 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301
Mathias Agopian4fec8732012-06-29 14:12:52 -07001302 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001303 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001304 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001305 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001306 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001307 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001308 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001309 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001310 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001311
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001312 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313}
1314
Mathias Agopianad456f92011-01-13 17:53:01 -08001315void SurfaceFlinger::invalidateHwcGeometry()
1316{
1317 mHwWorkListDirty = true;
1318}
1319
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001320
Mathias Agopiancd60f992012-08-16 16:28:27 -07001321void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001322 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323{
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 Region dirtyRegion(inDirtyRegion);
1325
Mathias Agopianb8a55602009-06-26 19:06:36 -07001326 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001327 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328
Mathias Agopian42977342012-08-05 00:40:46 -07001329 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001330 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001331 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1332 // takes a rectangle, we must make sure to update that whole
1333 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001334 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001335 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001336 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001337 // We need to redraw the rectangle that will be updated
1338 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001339 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001340 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001341 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001342 } else {
1343 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001344 dirtyRegion.set(hw->bounds());
1345 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 }
1347 }
1348
Mathias Agopiancd60f992012-08-16 16:28:27 -07001349 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001351 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001352 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001353
1354 // swap buffers (presentation)
1355 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356}
1357
Mathias Agopiancd60f992012-08-16 16:28:27 -07001358void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001359{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001360 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001361 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001362 HWComposer::LayerListIterator cur = hwc.begin(id);
1363 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001364
Mathias Agopian85d751c2012-08-29 16:59:24 -07001365 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1366 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001367 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001368
1369 // set the frame buffer
1370 glMatrixMode(GL_MODELVIEW);
1371 glLoadIdentity();
1372
1373 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001374 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001375 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001376 // when using overlays, we assume a fully transparent framebuffer
1377 // NOTE: we could reduce how much we need to clear, for instance
1378 // remove where there are opaque FB layers. however, on some
1379 // GPUs doing a "clean slate" glClear might be more efficient.
1380 // We'll revisit later if needed.
1381 glClearColor(0, 0, 0, 0);
1382 glClear(GL_COLOR_BUFFER_BIT);
1383 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001384 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001385 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001386 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001387 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001388 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001389 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001390 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001391 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001392
Mathias Agopian85d751c2012-08-29 16:59:24 -07001393 /*
1394 * and then, render the layers targeted at the framebuffer
1395 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001396
Mathias Agopian85d751c2012-08-29 16:59:24 -07001397 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1398 const size_t count = layers.size();
1399 const Transform& tr = hw->getTransform();
1400 if (cur != end) {
1401 // we're using h/w composer
1402 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001403 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001405 if (!clip.isEmpty()) {
1406 switch (cur->getCompositionType()) {
1407 case HWC_OVERLAY: {
1408 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1409 && i
1410 && layer->isOpaque()
1411 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001412 // never clear the very first layer since we're
1413 // guaranteed the FB is already cleared
1414 layer->clearWithOpenGL(hw, clip);
1415 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001416 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001417 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001418 case HWC_FRAMEBUFFER: {
1419 layer->draw(hw, clip);
1420 break;
1421 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001422 case HWC_FRAMEBUFFER_TARGET: {
1423 // this should not happen as the iterator shouldn't
1424 // let us get there.
1425 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1426 break;
1427 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001428 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001429 }
1430 layer->setAcquireFence(hw, *cur);
1431 }
1432 } else {
1433 // we're not using h/w composer
1434 for (size_t i=0 ; i<count ; ++i) {
1435 const sp<LayerBase>& layer(layers[i]);
1436 const Region clip(dirty.intersect(
1437 tr.transform(layer->visibleRegion)));
1438 if (!clip.isEmpty()) {
1439 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001440 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001441 }
1442 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443}
1444
Mathias Agopian55801e42012-08-27 18:54:24 -07001445void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1446 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001448 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001449 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001450 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001451 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001452
Mathias Agopian55801e42012-08-27 18:54:24 -07001453 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001454 Region::const_iterator it = region.begin();
1455 Region::const_iterator const end = region.end();
1456 while (it != end) {
1457 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001458 GLfloat vertices[][2] = {
1459 { r.left, height - r.top },
1460 { r.left, height - r.bottom },
1461 { r.right, height - r.bottom },
1462 { r.right, height - r.top }
1463 };
1464 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001465 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467}
1468
Mathias Agopian96f08192010-06-02 23:28:45 -07001469ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1470 const sp<LayerBaseClient>& lbc)
1471{
Mathias Agopian96f08192010-06-02 23:28:45 -07001472 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001473 size_t name = client->attachLayer(lbc);
1474
Mathias Agopian96f08192010-06-02 23:28:45 -07001475 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001476 Mutex::Autolock _l(mStateLock);
1477 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001478
Mathias Agopian4f113742011-05-03 16:21:41 -07001479 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001480}
1481
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001482status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483{
1484 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001485 status_t err = purgatorizeLayer_l(layer);
1486 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001487 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001488 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489}
1490
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001491status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492{
1493 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1494 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001495 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 return NO_ERROR;
1497 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001498 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499}
1500
Mathias Agopian9a112062009-04-17 19:36:26 -07001501status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1502{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001503 // First add the layer to the purgatory list, which makes sure it won't
1504 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001505 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001506 if (err >= 0) {
1507 mLayerPurgatory.add(layerBase);
1508 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001509
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001510 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001511
Mathias Agopian3d579642009-06-04 18:46:21 -07001512 // it's possible that we don't find a layer, because it might
1513 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001514 // from the user because there is a race between Client::destroySurface(),
1515 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001516 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1517}
1518
Mathias Agopiandea20b12011-05-03 17:04:02 -07001519uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1520{
1521 return android_atomic_release_load(&mTransactionFlags);
1522}
1523
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1525{
1526 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1527}
1528
Mathias Agopianbb641242010-05-18 17:06:55 -07001529uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530{
1531 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1532 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001533 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534 }
1535 return old;
1536}
1537
Mathias Agopian8b33f032012-07-24 20:43:54 -07001538void SurfaceFlinger::setTransactionState(
1539 const Vector<ComposerState>& state,
1540 const Vector<DisplayState>& displays,
1541 uint32_t flags)
1542{
Mathias Agopian698c0872011-06-28 19:09:31 -07001543 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001544 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001545
1546 size_t count = displays.size();
1547 for (size_t i=0 ; i<count ; i++) {
1548 const DisplayState& s(displays[i]);
1549 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001550 }
1551
Mathias Agopiane57f2922012-08-09 16:29:12 -07001552 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001553 for (size_t i=0 ; i<count ; i++) {
1554 const ComposerState& s(state[i]);
1555 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001556 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001557 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001558
Mathias Agopian386aa982011-11-07 21:58:03 -08001559 if (transactionFlags) {
1560 // this triggers the transaction
1561 setTransactionFlags(transactionFlags);
1562
1563 // if this is a synchronous transaction, wait for it to take effect
1564 // before returning.
1565 if (flags & eSynchronous) {
1566 mTransationPending = true;
1567 }
1568 while (mTransationPending) {
1569 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1570 if (CC_UNLIKELY(err != NO_ERROR)) {
1571 // just in case something goes wrong in SF, return to the
1572 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001573 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001574 mTransationPending = false;
1575 break;
1576 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 }
1579}
1580
Mathias Agopiane57f2922012-08-09 16:29:12 -07001581uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1582{
1583 uint32_t flags = 0;
1584 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001585 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001586 const uint32_t what = s.what;
1587 if (what & DisplayState::eSurfaceChanged) {
1588 if (disp.surface->asBinder() != s.surface->asBinder()) {
1589 disp.surface = s.surface;
1590 flags |= eDisplayTransactionNeeded;
1591 }
1592 }
1593 if (what & DisplayState::eLayerStackChanged) {
1594 if (disp.layerStack != s.layerStack) {
1595 disp.layerStack = s.layerStack;
1596 flags |= eDisplayTransactionNeeded;
1597 }
1598 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001599 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001600 if (disp.orientation != s.orientation) {
1601 disp.orientation = s.orientation;
1602 flags |= eDisplayTransactionNeeded;
1603 }
1604 if (disp.frame != s.frame) {
1605 disp.frame = s.frame;
1606 flags |= eDisplayTransactionNeeded;
1607 }
1608 if (disp.viewport != s.viewport) {
1609 disp.viewport = s.viewport;
1610 flags |= eDisplayTransactionNeeded;
1611 }
1612 }
1613 }
1614 return flags;
1615}
1616
1617uint32_t SurfaceFlinger::setClientStateLocked(
1618 const sp<Client>& client,
1619 const layer_state_t& s)
1620{
1621 uint32_t flags = 0;
1622 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1623 if (layer != 0) {
1624 const uint32_t what = s.what;
1625 if (what & layer_state_t::ePositionChanged) {
1626 if (layer->setPosition(s.x, s.y))
1627 flags |= eTraversalNeeded;
1628 }
1629 if (what & layer_state_t::eLayerChanged) {
1630 // NOTE: index needs to be calculated before we update the state
1631 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1632 if (layer->setLayer(s.z)) {
1633 mCurrentState.layersSortedByZ.removeAt(idx);
1634 mCurrentState.layersSortedByZ.add(layer);
1635 // we need traversal (state changed)
1636 // AND transaction (list changed)
1637 flags |= eTransactionNeeded|eTraversalNeeded;
1638 }
1639 }
1640 if (what & layer_state_t::eSizeChanged) {
1641 if (layer->setSize(s.w, s.h)) {
1642 flags |= eTraversalNeeded;
1643 }
1644 }
1645 if (what & layer_state_t::eAlphaChanged) {
1646 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1647 flags |= eTraversalNeeded;
1648 }
1649 if (what & layer_state_t::eMatrixChanged) {
1650 if (layer->setMatrix(s.matrix))
1651 flags |= eTraversalNeeded;
1652 }
1653 if (what & layer_state_t::eTransparentRegionChanged) {
1654 if (layer->setTransparentRegionHint(s.transparentRegion))
1655 flags |= eTraversalNeeded;
1656 }
1657 if (what & layer_state_t::eVisibilityChanged) {
1658 if (layer->setFlags(s.flags, s.mask))
1659 flags |= eTraversalNeeded;
1660 }
1661 if (what & layer_state_t::eCropChanged) {
1662 if (layer->setCrop(s.crop))
1663 flags |= eTraversalNeeded;
1664 }
1665 if (what & layer_state_t::eLayerStackChanged) {
1666 // NOTE: index needs to be calculated before we update the state
1667 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1668 if (layer->setLayerStack(s.layerStack)) {
1669 mCurrentState.layersSortedByZ.removeAt(idx);
1670 mCurrentState.layersSortedByZ.add(layer);
1671 // we need traversal (state changed)
1672 // AND transaction (list changed)
1673 flags |= eTransactionNeeded|eTraversalNeeded;
1674 }
1675 }
1676 }
1677 return flags;
1678}
1679
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001680sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001681 ISurfaceComposerClient::surface_data_t* params,
1682 const String8& name,
1683 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001684 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 uint32_t flags)
1686{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001687 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001688 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001689
1690 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001691 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001692 int(w), int(h));
1693 return surfaceHandle;
1694 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001695
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001696 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001697 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1698 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001699 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001701 case ISurfaceComposerClient::eFXSurfaceBlur:
1702 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001703 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001705 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001706 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001707 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 }
1709
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001710 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001711 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001712 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001713 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001715 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001716 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001717 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001718 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001719 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 }
1721
1722 return surfaceHandle;
1723}
1724
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001725sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001726 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001727 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001728 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729{
1730 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001731 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732 case PIXEL_FORMAT_TRANSPARENT:
1733 case PIXEL_FORMAT_TRANSLUCENT:
1734 format = PIXEL_FORMAT_RGBA_8888;
1735 break;
1736 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001737#ifdef NO_RGBX_8888
1738 format = PIXEL_FORMAT_RGB_565;
1739#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001740 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001741#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 break;
1743 }
1744
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001745#ifdef NO_RGBX_8888
1746 if (format == PIXEL_FORMAT_RGBX_8888)
1747 format = PIXEL_FORMAT_RGBA_8888;
1748#endif
1749
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001750 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001751 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001752 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001753 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001754 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 }
1756 return layer;
1757}
1758
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001759sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001760 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001761 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001763 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001764 return layer;
1765}
1766
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001767sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001768 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001769 uint32_t w, uint32_t h, uint32_t flags)
1770{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001771 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772 return layer;
1773}
1774
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001775status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776{
Mathias Agopian9a112062009-04-17 19:36:26 -07001777 /*
1778 * called by the window manager, when a surface should be marked for
1779 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001780 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001781 * The surface is removed from the current and drawing lists, but placed
1782 * in the purgatory queue, so it's not destroyed right-away (we need
1783 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001784 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001785
Mathias Agopian48d819a2009-09-10 19:41:18 -07001786 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001787 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001788 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001789
Mathias Agopian48d819a2009-09-10 19:41:18 -07001790 if (layer != 0) {
1791 err = purgatorizeLayer_l(layer);
1792 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001793 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001794 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001795 }
1796 return err;
1797}
1798
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001799status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001800{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001801 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001802 status_t err = NO_ERROR;
1803 sp<LayerBaseClient> l(layer.promote());
1804 if (l != NULL) {
1805 Mutex::Autolock _l(mStateLock);
1806 err = removeLayer_l(l);
1807 if (err == NAME_NOT_FOUND) {
1808 // The surface wasn't in the current list, which means it was
1809 // removed already, which means it is in the purgatory,
1810 // and need to be removed from there.
1811 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001812 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001813 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001814 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001815 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001816 "error removing layer=%p (%s)", l.get(), strerror(-err));
1817 }
1818 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819}
1820
Mathias Agopianb60314a2012-04-10 22:09:54 -07001821// ---------------------------------------------------------------------------
1822
Andy McFadden13a082e2012-08-24 10:16:42 -07001823void SurfaceFlinger::onInitializeDisplays() {
1824 // reset screen orientation
1825 Vector<ComposerState> state;
1826 Vector<DisplayState> displays;
1827 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001828 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001829 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001830 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001831 d.frame.makeInvalid();
1832 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001833 displays.add(d);
1834 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001835 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001836}
1837
1838void SurfaceFlinger::initializeDisplays() {
1839 class MessageScreenInitialized : public MessageBase {
1840 SurfaceFlinger* flinger;
1841 public:
1842 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1843 virtual bool handler() {
1844 flinger->onInitializeDisplays();
1845 return true;
1846 }
1847 };
1848 sp<MessageBase> msg = new MessageScreenInitialized(this);
1849 postMessageAsync(msg); // we may be called from main thread, use async message
1850}
1851
1852
Mathias Agopiancde87a32012-09-13 14:09:01 -07001853void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001854 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001855 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001856 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001857 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1858 // FIXME: eventthread only knows about the main display right now
1859 mEventThread->onScreenAcquired();
1860 }
Mathias Agopian20128302012-08-13 18:32:13 -07001861 mVisibleRegionsDirty = true;
1862 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863}
1864
Mathias Agopiancde87a32012-09-13 14:09:01 -07001865void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001866 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001867 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001868 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1869 // FIXME: eventthread only knows about the main display right now
1870 mEventThread->onScreenReleased();
1871 }
Mathias Agopian42977342012-08-05 00:40:46 -07001872 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001873 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001874 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001875 // from this point on, SF will stop drawing
1876 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877}
1878
Colin Cross8e533062012-06-07 13:17:52 -07001879void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001880 class MessageScreenAcquired : public MessageBase {
1881 SurfaceFlinger* flinger;
1882 public:
1883 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1884 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001885 // FIXME: should this be per-display?
1886 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001887 return true;
1888 }
1889 };
1890 sp<MessageBase> msg = new MessageScreenAcquired(this);
1891 postMessageSync(msg);
1892}
1893
Colin Cross8e533062012-06-07 13:17:52 -07001894void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001895 class MessageScreenReleased : public MessageBase {
1896 SurfaceFlinger* flinger;
1897 public:
1898 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1899 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001900 // FIXME: should this be per-display?
1901 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001902 return true;
1903 }
1904 };
1905 sp<MessageBase> msg = new MessageScreenReleased(this);
1906 postMessageSync(msg);
1907}
1908
1909// ---------------------------------------------------------------------------
1910
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1912{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001913 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 char buffer[SIZE];
1915 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001916
1917 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 snprintf(buffer, SIZE, "Permission Denial: "
1919 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1920 IPCThreadState::self()->getCallingPid(),
1921 IPCThreadState::self()->getCallingUid());
1922 result.append(buffer);
1923 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001924 // Try to get the main lock, but don't insist if we can't
1925 // (this would indicate SF is stuck, but we want to be able to
1926 // print something in dumpsys).
1927 int retry = 3;
1928 while (mStateLock.tryLock()<0 && --retry>=0) {
1929 usleep(1000000);
1930 }
1931 const bool locked(retry >= 0);
1932 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001933 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001934 "SurfaceFlinger appears to be unresponsive, "
1935 "dumping anyways (no locks held)\n");
1936 result.append(buffer);
1937 }
1938
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001939 bool dumpAll = true;
1940 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001941 size_t numArgs = args.size();
1942 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001943 if ((index < numArgs) &&
1944 (args[index] == String16("--list"))) {
1945 index++;
1946 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001947 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001948 }
1949
1950 if ((index < numArgs) &&
1951 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001952 index++;
1953 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001954 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001955 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001956
1957 if ((index < numArgs) &&
1958 (args[index] == String16("--latency-clear"))) {
1959 index++;
1960 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001961 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001962 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001964
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001965 if (dumpAll) {
1966 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001967 }
1968
Mathias Agopian9795c422009-08-26 16:36:26 -07001969 if (locked) {
1970 mStateLock.unlock();
1971 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001972 }
1973 write(fd, result.string(), result.size());
1974 return NO_ERROR;
1975}
1976
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001977void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1978 String8& result, char* buffer, size_t SIZE) const
1979{
1980 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1981 const size_t count = currentLayers.size();
1982 for (size_t i=0 ; i<count ; i++) {
1983 const sp<LayerBase>& layer(currentLayers[i]);
1984 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1985 result.append(buffer);
1986 }
1987}
1988
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001989void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1990 String8& result, char* buffer, size_t SIZE) const
1991{
1992 String8 name;
1993 if (index < args.size()) {
1994 name = String8(args[index]);
1995 index++;
1996 }
1997
1998 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1999 const size_t count = currentLayers.size();
2000 for (size_t i=0 ; i<count ; i++) {
2001 const sp<LayerBase>& layer(currentLayers[i]);
2002 if (name.isEmpty()) {
2003 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
2004 result.append(buffer);
2005 }
2006 if (name.isEmpty() || (name == layer->getName())) {
2007 layer->dumpStats(result, buffer, SIZE);
2008 }
2009 }
2010}
2011
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002012void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
2013 String8& result, char* buffer, size_t SIZE) const
2014{
2015 String8 name;
2016 if (index < args.size()) {
2017 name = String8(args[index]);
2018 index++;
2019 }
2020
2021 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2022 const size_t count = currentLayers.size();
2023 for (size_t i=0 ; i<count ; i++) {
2024 const sp<LayerBase>& layer(currentLayers[i]);
2025 if (name.isEmpty() || (name == layer->getName())) {
2026 layer->clearStats();
2027 }
2028 }
2029}
2030
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002031void SurfaceFlinger::dumpAllLocked(
2032 String8& result, char* buffer, size_t SIZE) const
2033{
2034 // figure out if we're stuck somewhere
2035 const nsecs_t now = systemTime();
2036 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2037 const nsecs_t inTransaction(mDebugInTransaction);
2038 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2039 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2040
2041 /*
2042 * Dump the visible layer list
2043 */
2044 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2045 const size_t count = currentLayers.size();
2046 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2047 result.append(buffer);
2048 for (size_t i=0 ; i<count ; i++) {
2049 const sp<LayerBase>& layer(currentLayers[i]);
2050 layer->dump(result, buffer, SIZE);
2051 }
2052
2053 /*
2054 * Dump the layers in the purgatory
2055 */
2056
2057 const size_t purgatorySize = mLayerPurgatory.size();
2058 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2059 result.append(buffer);
2060 for (size_t i=0 ; i<purgatorySize ; i++) {
2061 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2062 layer->shortDump(result, buffer, SIZE);
2063 }
2064
2065 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002066 * Dump Display state
2067 */
2068
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002069 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2070 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002071 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2072 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002073 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002074 }
2075
2076 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002077 * Dump SurfaceFlinger global state
2078 */
2079
2080 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2081 result.append(buffer);
2082
Mathias Agopian888c8222012-08-04 21:10:38 -07002083 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002084 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002085 const GLExtensions& extensions(GLExtensions::getInstance());
2086 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2087 extensions.getVendor(),
2088 extensions.getRenderer(),
2089 extensions.getVersion());
2090 result.append(buffer);
2091
2092 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002093 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002094 result.append(buffer);
2095
2096 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2097 result.append(buffer);
2098
Mathias Agopian42977342012-08-05 00:40:46 -07002099 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002100 snprintf(buffer, SIZE,
2101 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002102 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002103 result.append(buffer);
2104 snprintf(buffer, SIZE,
2105 " last eglSwapBuffers() time: %f us\n"
2106 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002107 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002108 " refresh-rate : %f fps\n"
2109 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002110 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002111 mLastSwapBufferTime/1000.0,
2112 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002113 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002114 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2115 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2116 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002117 result.append(buffer);
2118
2119 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2120 inSwapBuffersDuration/1000.0);
2121 result.append(buffer);
2122
2123 snprintf(buffer, SIZE, " transaction time: %f us\n",
2124 inTransactionDuration/1000.0);
2125 result.append(buffer);
2126
2127 /*
2128 * VSYNC state
2129 */
2130 mEventThread->dump(result, buffer, SIZE);
2131
2132 /*
2133 * Dump HWComposer state
2134 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002135 snprintf(buffer, SIZE, "h/w composer state:\n");
2136 result.append(buffer);
2137 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2138 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2139 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2140 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002141 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002142
2143 /*
2144 * Dump gralloc state
2145 */
2146 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2147 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002148}
2149
Keun young Park63f165f2012-08-31 10:53:36 -07002150bool SurfaceFlinger::startDdmConnection()
2151{
2152 void* libddmconnection_dso =
2153 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2154 if (!libddmconnection_dso) {
2155 return false;
2156 }
2157 void (*DdmConnection_start)(const char* name);
2158 DdmConnection_start =
2159 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2160 if (!DdmConnection_start) {
2161 dlclose(libddmconnection_dso);
2162 return false;
2163 }
2164 (*DdmConnection_start)(getServiceName());
2165 return true;
2166}
2167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002168status_t SurfaceFlinger::onTransact(
2169 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2170{
2171 switch (code) {
2172 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002173 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002175 case BLANK:
2176 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002177 {
2178 // codes that require permission check
2179 IPCThreadState* ipc = IPCThreadState::self();
2180 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002181 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002182 if ((uid != AID_GRAPHICS) &&
2183 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002184 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002185 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2186 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002187 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002188 break;
2189 }
2190 case CAPTURE_SCREEN:
2191 {
2192 // codes that require permission check
2193 IPCThreadState* ipc = IPCThreadState::self();
2194 const int pid = ipc->getCallingPid();
2195 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002196 if ((uid != AID_GRAPHICS) &&
2197 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002198 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002199 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2200 return PERMISSION_DENIED;
2201 }
2202 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002203 }
2204 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002206 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2207 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002208 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002209 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002210 IPCThreadState* ipc = IPCThreadState::self();
2211 const int pid = ipc->getCallingPid();
2212 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002213 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002214 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002215 return PERMISSION_DENIED;
2216 }
2217 int n;
2218 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002219 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002220 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002221 return NO_ERROR;
2222 case 1002: // SHOW_UPDATES
2223 n = data.readInt32();
2224 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002225 invalidateHwcGeometry();
2226 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002228 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002229 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002230 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002232 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002233 setTransactionFlags(
2234 eTransactionNeeded|
2235 eDisplayTransactionNeeded|
2236 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002237 return NO_ERROR;
2238 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002239 case 1006:{ // send empty update
2240 signalRefresh();
2241 return NO_ERROR;
2242 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002243 case 1008: // toggle use of hw composer
2244 n = data.readInt32();
2245 mDebugDisableHWC = n ? 1 : 0;
2246 invalidateHwcGeometry();
2247 repaintEverything();
2248 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002249 case 1009: // toggle use of transform hint
2250 n = data.readInt32();
2251 mDebugDisableTransformHint = n ? 1 : 0;
2252 invalidateHwcGeometry();
2253 repaintEverything();
2254 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002255 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002256 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002257 reply->writeInt32(0);
2258 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002259 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002260 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002261 return NO_ERROR;
2262 case 1013: {
2263 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002264 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2265 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002266 }
2267 return NO_ERROR;
2268 }
2269 }
2270 return err;
2271}
2272
Mathias Agopian53331da2011-08-22 21:44:41 -07002273void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002274 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002275 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002276}
2277
Mathias Agopian59119e62010-10-11 12:37:43 -07002278// ---------------------------------------------------------------------------
2279
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002280status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002281 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2282{
2283 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002284 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002285}
2286
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002287status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002288 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002289{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002290 ATRACE_CALL();
2291
Mathias Agopian59119e62010-10-11 12:37:43 -07002292 if (!GLExtensions::getInstance().haveFramebufferObject())
2293 return INVALID_OPERATION;
2294
2295 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002296 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2297 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002298 const uint32_t hw_w = hw->getWidth();
2299 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002300 GLfloat u = 1;
2301 GLfloat v = 1;
2302
2303 // make sure to clear all GL error flags
2304 while ( glGetError() != GL_NO_ERROR ) ;
2305
2306 // create a FBO
2307 GLuint name, tname;
2308 glGenTextures(1, &tname);
2309 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002310 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2311 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002312 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2313 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002314 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002315 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002316 GLint tw = (2 << (31 - clz(hw_w)));
2317 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002318 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2319 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002320 u = GLfloat(hw_w) / tw;
2321 v = GLfloat(hw_h) / th;
2322 }
2323 glGenFramebuffersOES(1, &name);
2324 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002325 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2326 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002327
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002328 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002329 glDisable(GL_TEXTURE_EXTERNAL_OES);
2330 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002331 glClearColor(0,0,0,1);
2332 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002333 glMatrixMode(GL_MODELVIEW);
2334 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002335 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002336 const size_t count = layers.size();
2337 for (size_t i=0 ; i<count ; ++i) {
2338 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002339 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002340 }
2341
Mathias Agopian42977342012-08-05 00:40:46 -07002342 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002343
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002344 // back to main framebuffer
2345 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002346 glDeleteFramebuffersOES(1, &name);
2347
2348 *textureName = tname;
2349 *uOut = u;
2350 *vOut = v;
2351 return NO_ERROR;
2352}
2353
2354// ---------------------------------------------------------------------------
2355
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002356status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002357 sp<IMemoryHeap>* heap,
2358 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002359 uint32_t sw, uint32_t sh,
2360 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002362 ATRACE_CALL();
2363
Mathias Agopian74c40c02010-09-29 13:02:36 -07002364 status_t result = PERMISSION_DENIED;
2365
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002366 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002367 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002368 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002369
2370 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002371 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002372 const uint32_t hw_w = hw->getWidth();
2373 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002375 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002376 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002377 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002378 return PERMISSION_DENIED;
2379 }
2380
2381 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002382 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002384 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002385
2386 sw = (!sw) ? hw_w : sw;
2387 sh = (!sh) ? hw_h : sh;
2388 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002389 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002390
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002391// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2392// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002393
Mathias Agopian74c40c02010-09-29 13:02:36 -07002394 // make sure to clear all GL error flags
2395 while ( glGetError() != GL_NO_ERROR ) ;
2396
2397 // create a FBO
2398 GLuint name, tname;
2399 glGenRenderbuffersOES(1, &tname);
2400 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2401 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002402
Mathias Agopian74c40c02010-09-29 13:02:36 -07002403 glGenFramebuffersOES(1, &name);
2404 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2405 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2406 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2407
2408 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002409
Mathias Agopian74c40c02010-09-29 13:02:36 -07002410 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2411
2412 // invert everything, b/c glReadPixel() below will invert the FB
2413 glViewport(0, 0, sw, sh);
2414 glMatrixMode(GL_PROJECTION);
2415 glPushMatrix();
2416 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002417 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 glMatrixMode(GL_MODELVIEW);
2419
2420 // redraw the screen entirely...
2421 glClearColor(0,0,0,1);
2422 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002423
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002424 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002425 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002426 for (size_t i=0 ; i<count ; ++i) {
2427 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002428 const uint32_t z = layer->drawingState().z;
2429 if (z >= minLayerZ && z <= maxLayerZ) {
2430 if (filtering) layer->setFiltering(true);
2431 layer->draw(hw);
2432 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002433 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002434 }
2435
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 // check for errors and return screen capture
2437 if (glGetError() != GL_NO_ERROR) {
2438 // error while rendering
2439 result = INVALID_OPERATION;
2440 } else {
2441 // allocate shared memory large enough to hold the
2442 // screen capture
2443 sp<MemoryHeapBase> base(
2444 new MemoryHeapBase(size, 0, "screen-capture") );
2445 void* const ptr = base->getBase();
2446 if (ptr) {
2447 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002448 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002449 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2450 if (glGetError() == GL_NO_ERROR) {
2451 *heap = base;
2452 *w = sw;
2453 *h = sh;
2454 *f = PIXEL_FORMAT_RGBA_8888;
2455 result = NO_ERROR;
2456 }
2457 } else {
2458 result = NO_MEMORY;
2459 }
2460 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002461 glViewport(0, 0, hw_w, hw_h);
2462 glMatrixMode(GL_PROJECTION);
2463 glPopMatrix();
2464 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002465 } else {
2466 result = BAD_VALUE;
2467 }
2468
2469 // release FBO resources
2470 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2471 glDeleteRenderbuffersOES(1, &tname);
2472 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002473
Mathias Agopian42977342012-08-05 00:40:46 -07002474 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002475
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002476// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002477
Mathias Agopian74c40c02010-09-29 13:02:36 -07002478 return result;
2479}
2480
2481
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002482status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002484 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002485 uint32_t sw, uint32_t sh,
2486 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002487{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002488 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489 return BAD_VALUE;
2490
2491 if (!GLExtensions::getInstance().haveFramebufferObject())
2492 return INVALID_OPERATION;
2493
2494 class MessageCaptureScreen : public MessageBase {
2495 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002496 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002497 sp<IMemoryHeap>* heap;
2498 uint32_t* w;
2499 uint32_t* h;
2500 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002501 uint32_t sw;
2502 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002503 uint32_t minLayerZ;
2504 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002505 status_t result;
2506 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002507 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002508 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002509 uint32_t sw, uint32_t sh,
2510 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002511 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002512 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2513 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2514 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002515 {
2516 }
2517 status_t getResult() const {
2518 return result;
2519 }
2520 virtual bool handler() {
2521 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002522 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002523 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002524 return true;
2525 }
2526 };
2527
2528 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002529 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002530 status_t res = postMessageSync(msg);
2531 if (res == NO_ERROR) {
2532 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2533 }
2534 return res;
2535}
2536
2537// ---------------------------------------------------------------------------
2538
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002539SurfaceFlinger::LayerVector::LayerVector() {
2540}
2541
2542SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2543 : SortedVector<sp<LayerBase> >(rhs) {
2544}
2545
2546int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2547 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002548{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002549 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002550 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2551 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002552
2553 uint32_t ls = l->currentState().layerStack;
2554 uint32_t rs = r->currentState().layerStack;
2555 if (ls != rs)
2556 return ls - rs;
2557
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002558 uint32_t lz = l->currentState().z;
2559 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002560 if (lz != rz)
2561 return lz - rz;
2562
2563 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002564}
2565
2566// ---------------------------------------------------------------------------
2567
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002568SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2569 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002570}
2571
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002572SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2573 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002574 viewport.makeInvalid();
2575 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002576}
2577
2578// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002580}; // namespace android