blob: ee653f3c01eb766ee0baad29e60894d9c0adda60 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/String8.h>
47#include <utils/String16.h>
48#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian921e6ac2012-07-23 23:11:29 -070051#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080054#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070056#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080057#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070061#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Mathias Agopiana4912602012-07-12 14:25:33 -070064#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070065#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana67932f2011-04-20 14:20:59 -070067
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070087 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070090 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -070099 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Steve Blocka19954a2012-01-04 20:05:49 +0000101 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700112 if (!startDdmConnection()) {
113 // start failed, and DDMS debugging not enabled
114 mDebugDDMS = 0;
115 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700117 ALOGI_IF(mDebugRegion, "showupdates enabled");
118 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121void SurfaceFlinger::onFirstRef()
122{
123 mEventQueue.init(this);
124
125 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
126
127 // Wait for the main thread to be done with its initialization
128 mReadyToRunBarrier.wait();
129}
130
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::~SurfaceFlinger()
133{
Mathias Agopiana4912602012-07-12 14:25:33 -0700134 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
135 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
136 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800139void SurfaceFlinger::binderDied(const wp<IBinder>& who)
140{
141 // the window manager died on us. prepare its eulogy.
142
Andy McFadden13a082e2012-08-24 10:16:42 -0700143 // restore initial conditions (default device unblank, etc)
144 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145
146 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700147 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148}
149
Mathias Agopian7e27f052010-05-28 14:22:23 -0700150sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Mathias Agopian96f08192010-06-02 23:28:45 -0700152 sp<ISurfaceComposerClient> bclient;
153 sp<Client> client(new Client(this));
154 status_t err = client->initCheck();
155 if (err == NO_ERROR) {
156 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 return bclient;
159}
160
Mathias Agopiane57f2922012-08-09 16:29:12 -0700161sp<IBinder> SurfaceFlinger::createDisplay()
162{
163 class DisplayToken : public BBinder {
164 sp<SurfaceFlinger> flinger;
165 virtual ~DisplayToken() {
166 // no more references, this display must be terminated
167 Mutex::Autolock _l(flinger->mStateLock);
168 flinger->mCurrentState.displays.removeItem(this);
169 flinger->setTransactionFlags(eDisplayTransactionNeeded);
170 }
171 public:
172 DisplayToken(const sp<SurfaceFlinger>& flinger)
173 : flinger(flinger) {
174 }
175 };
176
177 sp<BBinder> token = new DisplayToken(this);
178
179 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700180 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700181 mCurrentState.displays.add(token, info);
182
183 return token;
184}
185
186sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700187 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
189 return NULL;
190 }
191 return mDefaultDisplays[id];
192}
193
Jamie Gennis9a78c902011-01-12 18:30:40 -0800194sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
195{
196 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
197 return gba;
198}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200void SurfaceFlinger::bootFinished()
201{
202 const nsecs_t now = systemTime();
203 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000204 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700205 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700206
207 // wait patiently for the window manager death
208 const String16 name("window");
209 sp<IBinder> window(defaultServiceManager()->getService(name));
210 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700211 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700212 }
213
214 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700215 // formerly we would just kill the process, but we now ask it to exit so it
216 // can choose where to stop the animation.
217 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
219
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700220void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
221 class MessageDestroyGLTexture : public MessageBase {
222 GLuint texture;
223 public:
224 MessageDestroyGLTexture(GLuint texture)
225 : texture(texture) {
226 }
227 virtual bool handler() {
228 glDeleteTextures(1, &texture);
229 return true;
230 }
231 };
232 postMessageAsync(new MessageDestroyGLTexture(texture));
233}
234
Mathias Agopiana4912602012-07-12 14:25:33 -0700235status_t SurfaceFlinger::selectConfigForPixelFormat(
236 EGLDisplay dpy,
237 EGLint const* attrs,
238 PixelFormat format,
239 EGLConfig* outConfig)
240{
241 EGLConfig config = NULL;
242 EGLint numConfigs = -1, n=0;
243 eglGetConfigs(dpy, NULL, 0, &numConfigs);
244 EGLConfig* const configs = new EGLConfig[numConfigs];
245 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
246 for (int i=0 ; i<n ; i++) {
247 EGLint nativeVisualId = 0;
248 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
249 if (nativeVisualId>0 && format == nativeVisualId) {
250 *outConfig = configs[i];
251 delete [] configs;
252 return NO_ERROR;
253 }
254 }
255 delete [] configs;
256 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
Mathias Agopiana4912602012-07-12 14:25:33 -0700259EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
260 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
261 // it is to be used with WIFI displays
262 EGLConfig config;
263 EGLint dummy;
264 status_t err;
265 EGLint attribs[] = {
266 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
267 EGL_RECORDABLE_ANDROID, EGL_TRUE,
268 EGL_NONE
269 };
270 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
271 if (err) {
272 // maybe we failed because of EGL_RECORDABLE_ANDROID
273 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
274 attribs[2] = EGL_NONE;
275 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
276 }
277 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
278 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
279 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
280 }
281 return config;
282}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283
Mathias Agopiana4912602012-07-12 14:25:33 -0700284EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
285 // Also create our EGLContext
286 EGLint contextAttributes[] = {
287#ifdef EGL_IMG_context_priority
288#ifdef HAS_CONTEXT_PRIORITY
289#warning "using EGL_IMG_context_priority"
290 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
291#endif
292#endif
293 EGL_NONE, EGL_NONE
294 };
295 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
296 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
297 return ctxt;
298}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
301 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
302 if (!result) {
303 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
304 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
306
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 GLExtensions& extensions(GLExtensions::getInstance());
308 extensions.initWithGLStrings(
309 glGetString(GL_VENDOR),
310 glGetString(GL_RENDERER),
311 glGetString(GL_VERSION),
312 glGetString(GL_EXTENSIONS),
313 eglQueryString(display, EGL_VENDOR),
314 eglQueryString(display, EGL_VERSION),
315 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700316
Mathias Agopiana4912602012-07-12 14:25:33 -0700317 EGLint w, h;
318 eglQuerySurface(display, surface, EGL_WIDTH, &w);
319 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700320
Mathias Agopiana4912602012-07-12 14:25:33 -0700321 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
322 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700325 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 glShadeModel(GL_FLAT);
328 glDisable(GL_DITHER);
329 glDisable(GL_CULL_FACE);
330
Mathias Agopiana4912602012-07-12 14:25:33 -0700331 struct pack565 {
332 inline uint16_t operator() (int r, int g, int b) const {
333 return (r<<11)|(g<<5)|b;
334 }
335 } pack565;
336
Jamie Gennis9575f602011-10-07 14:51:16 -0700337 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
338 glGenTextures(1, &mProtectedTexName);
339 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
340 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
341 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
342 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
343 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
344 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
345 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346
347 glViewport(0, 0, w, h);
348 glMatrixMode(GL_PROJECTION);
349 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700350 // put the origin in the left-bottom corner
351 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352
Mathias Agopiana4912602012-07-12 14:25:33 -0700353 // print some debugging info
354 EGLint r,g,b,a;
355 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
356 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
357 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
358 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
359 ALOGI("EGL informations:");
360 ALOGI("vendor : %s", extensions.getEglVendor());
361 ALOGI("version : %s", extensions.getEglVersion());
362 ALOGI("extensions: %s", extensions.getEglExtension());
363 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
364 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
365 ALOGI("OpenGL ES informations:");
366 ALOGI("vendor : %s", extensions.getVendor());
367 ALOGI("renderer : %s", extensions.getRenderer());
368 ALOGI("version : %s", extensions.getVersion());
369 ALOGI("extensions: %s", extensions.getExtension());
370 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
371 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
372}
373
Mathias Agopiana4912602012-07-12 14:25:33 -0700374status_t SurfaceFlinger::readyToRun()
375{
376 ALOGI( "SurfaceFlinger's main thread ready to run. "
377 "Initializing graphics H/W...");
378
Mathias Agopiana4912602012-07-12 14:25:33 -0700379 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700380 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
381 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700382
383 // Initialize the main display
384 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700385 sp<FramebufferSurface> fbs = FramebufferSurface::create();
386 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700387 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
388 exit(0);
389 }
390
Mathias Agopiane57f2922012-08-09 16:29:12 -0700391 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
392 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700393
Mathias Agopiana4912602012-07-12 14:25:33 -0700394 // initialize the config and context
395 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700396 ANativeWindow* const anw = stc.get();
397 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700398 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
399 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700400
401 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700402
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700403 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700404 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700405 mCurrentState.displays.add(mDefaultDisplays[i],
406 DisplayDeviceState((DisplayDevice::DisplayType)i));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700407 }
408 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700409 DisplayDevice::DISPLAY_PRIMARY,
410 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiane60b0682012-08-21 23:34:09 -0700411 anw, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700412 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700413
414 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700415 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700416 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800417
Mathias Agopian028508c2012-07-25 21:12:12 -0700418 // start the EventThread
419 mEventThread = new EventThread(this);
420 mEventQueue.setEventThread(mEventThread);
421
Mathias Agopian86303202012-07-24 22:46:10 -0700422 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700423 mHwc = new HWComposer(this,
424 *static_cast<HWComposer::EventHandler *>(this),
425 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700426
427 // initialize our drawing state
428 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700429
Mathias Agopiana4912602012-07-12 14:25:33 -0700430 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800431 mReadyToRunBarrier.open();
432
Andy McFadden13a082e2012-08-24 10:16:42 -0700433 // set initial conditions (e.g. unblank default device)
434 initializeDisplays();
435
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700436 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700437 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700438
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 return NO_ERROR;
440}
441
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700442int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
443 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
444 type : mHwc->allocateDisplayId();
445}
446
Mathias Agopiana67e4182012-06-19 17:26:12 -0700447void SurfaceFlinger::startBootAnim() {
448 // start boot animation
449 property_set("service.bootanim.exit", "0");
450 property_set("ctl.start", "bootanim");
451}
452
Mathias Agopiana4912602012-07-12 14:25:33 -0700453uint32_t SurfaceFlinger::getMaxTextureSize() const {
454 return mMaxTextureSize;
455}
456
457uint32_t SurfaceFlinger::getMaxViewportDims() const {
458 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
459 mMaxViewportDims[0] : mMaxViewportDims[1];
460}
461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800463
Jamie Gennis582270d2011-08-17 18:19:00 -0700464bool SurfaceFlinger::authenticateSurfaceTexture(
465 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800466 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700467 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800468
469 // Check the visible layer list for the ISurface
470 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
471 size_t count = currentLayers.size();
472 for (size_t i=0 ; i<count ; i++) {
473 const sp<LayerBase>& layer(currentLayers[i]);
474 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700475 if (lbc != NULL) {
476 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
477 if (lbcBinder == surfaceTextureBinder) {
478 return true;
479 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800480 }
481 }
482
483 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700484 // SurfaceTexture gets destroyed before all the clients are done using it,
485 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800486 // will instead fail later on when the client tries to use the surface,
487 // which should be reported as "surface XYZ returned an -ENODEV". The
488 // purgatorized layers are no less authentic than the visible ones, so this
489 // should not cause any harm.
490 size_t purgatorySize = mLayerPurgatory.size();
491 for (size_t i=0 ; i<purgatorySize ; i++) {
492 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
493 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700494 if (lbc != NULL) {
495 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
496 if (lbcBinder == surfaceTextureBinder) {
497 return true;
498 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800499 }
500 }
501
502 return false;
503}
504
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700505status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
506 // TODO: this is mostly here only for compatibility
507 // the display size is needed but the display metrics should come from elsewhere
508 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
509 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700510 return BAD_INDEX;
511 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700512
513 const HWComposer& hwc(getHwComposer());
514 float xdpi = hwc.getDpiX();
515 float ydpi = hwc.getDpiY();
516
517 // TODO: Not sure if display density should handled by SF any longer
518 class Density {
519 static int getDensityFromProperty(char const* propName) {
520 char property[PROPERTY_VALUE_MAX];
521 int density = 0;
522 if (property_get(propName, property, NULL) > 0) {
523 density = atoi(property);
524 }
525 return density;
526 }
527 public:
528 static int getEmuDensity() {
529 return getDensityFromProperty("qemu.sf.lcd_density"); }
530 static int getBuildDensity() {
531 return getDensityFromProperty("ro.sf.lcd_density"); }
532 };
533 // The density of the device is provided by a build property
534 float density = Density::getBuildDensity() / 160.0f;
535 if (density == 0) {
536 // the build doesn't provide a density -- this is wrong!
537 // use xdpi instead
538 ALOGE("ro.sf.lcd_density must be defined as a build property");
539 density = xdpi / 160.0f;
540 }
541 if (Density::getEmuDensity()) {
542 // if "qemu.sf.lcd_density" is specified, it overrides everything
543 xdpi = ydpi = density = Density::getEmuDensity();
544 density /= 160.0f;
545 }
546
Mathias Agopian42977342012-08-05 00:40:46 -0700547 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
548 info->w = hw->getWidth();
549 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700550 info->xdpi = xdpi;
551 info->ydpi = ydpi;
552 info->fps = float(1e9 / hwc.getRefreshPeriod());
553 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700554 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700555 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700556 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700557 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700558}
559
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800560// ----------------------------------------------------------------------------
561
562sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800563 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700564}
565
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700566void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700567
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700568 sp<IBinder> token;
569 { // scope for the lock
570 Mutex::Autolock _l(mStateLock);
571 token = mExtDisplayToken;
572 }
573
574 if (token == 0) {
575 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700576 }
577
578 { // scope for the lock
579 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700580 if (surface == 0) {
581 // release our current display. we're guarantee to have
582 // a reference to it (token), while we hold the lock
583 mExtDisplayToken = 0;
584 } else {
585 mExtDisplayToken = token;
586 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700587
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700588 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
589 info.surface = surface;
590 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700591 }
592}
593
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800595
596void SurfaceFlinger::waitForEvent() {
597 mEventQueue.waitMessage();
598}
599
600void SurfaceFlinger::signalTransaction() {
601 mEventQueue.invalidate();
602}
603
604void SurfaceFlinger::signalLayerUpdate() {
605 mEventQueue.invalidate();
606}
607
608void SurfaceFlinger::signalRefresh() {
609 mEventQueue.refresh();
610}
611
612status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
613 nsecs_t reltime, uint32_t flags) {
614 return mEventQueue.postMessage(msg, reltime);
615}
616
617status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
618 nsecs_t reltime, uint32_t flags) {
619 status_t res = mEventQueue.postMessage(msg, reltime);
620 if (res == NO_ERROR) {
621 msg->wait();
622 }
623 return res;
624}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625
Mathias Agopian4fec8732012-06-29 14:12:52 -0700626bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 return true;
629}
630
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700631void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
632 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
633 // we should only receive DisplayDevice::DisplayType from the vsync callback
634 const wp<IBinder>& token(mDefaultDisplays[type]);
635 mEventThread->onVSyncReceived(token, timestamp);
636 }
Mathias Agopian86303202012-07-24 22:46:10 -0700637}
638
639void SurfaceFlinger::eventControl(int event, int enabled) {
640 getHwComposer().eventControl(event, enabled);
641}
642
Mathias Agopian4fec8732012-06-29 14:12:52 -0700643void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800644 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800645 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700646 case MessageQueue::INVALIDATE:
647 handleMessageTransaction();
648 handleMessageInvalidate();
649 signalRefresh();
650 break;
651 case MessageQueue::REFRESH:
652 handleMessageRefresh();
653 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800654 }
655}
656
Mathias Agopian4fec8732012-06-29 14:12:52 -0700657void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700658 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700659 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700660 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700661 }
662}
663
664void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700665 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700666 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700667}
668
669void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700670 ATRACE_CALL();
671 preComposition();
672 rebuildLayerStacks();
673 setUpHWComposer();
674 doDebugFlashRegions();
675 doComposition();
676 postComposition();
677}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700678
Mathias Agopiancd60f992012-08-16 16:28:27 -0700679void SurfaceFlinger::doDebugFlashRegions()
680{
681 // is debugging enabled
682 if (CC_LIKELY(!mDebugRegion))
683 return;
684
685 const bool repaintEverything = mRepaintEverything;
686 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
687 const sp<DisplayDevice>& hw(mDisplays[dpy]);
688 if (hw->canDraw()) {
689 // transform the dirty region into this screen's coordinate space
690 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
691 if (!dirtyRegion.isEmpty()) {
692 // redraw the whole screen
693 doComposeSurfaces(hw, Region(hw->bounds()));
694
695 // and draw the dirty region
696 glDisable(GL_TEXTURE_EXTERNAL_OES);
697 glDisable(GL_TEXTURE_2D);
698 glDisable(GL_BLEND);
699 glColor4f(1, 0, 1, 1);
700 const int32_t height = hw->getHeight();
701 Region::const_iterator it = dirtyRegion.begin();
702 Region::const_iterator const end = dirtyRegion.end();
703 while (it != end) {
704 const Rect& r = *it++;
705 GLfloat vertices[][2] = {
706 { r.left, height - r.top },
707 { r.left, height - r.bottom },
708 { r.right, height - r.bottom },
709 { r.right, height - r.top }
710 };
711 glVertexPointer(2, GL_FLOAT, 0, vertices);
712 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
713 }
714 hw->compositionComplete();
715 // FIXME
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700716 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700717 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
718 }
719 }
720 }
721 }
722
723 postFramebuffer();
724
725 if (mDebugRegion > 1) {
726 usleep(mDebugRegion * 1000);
727 }
728}
729
730void SurfaceFlinger::preComposition()
731{
732 bool needExtraInvalidate = false;
733 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
734 const size_t count = currentLayers.size();
735 for (size_t i=0 ; i<count ; i++) {
736 if (currentLayers[i]->onPreComposition()) {
737 needExtraInvalidate = true;
738 }
739 }
740 if (needExtraInvalidate) {
741 signalLayerUpdate();
742 }
743}
744
745void SurfaceFlinger::postComposition()
746{
747 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
748 const size_t count = currentLayers.size();
749 for (size_t i=0 ; i<count ; i++) {
750 currentLayers[i]->onPostComposition();
751 }
752}
753
754void SurfaceFlinger::rebuildLayerStacks() {
755 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700756 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700757 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700758 mVisibleRegionsDirty = false;
759 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700760 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700761 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700762 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700763 const Transform& tr(hw->getTransform());
764 const Rect bounds(hw->getBounds());
765
Mathias Agopian87baae12012-07-31 12:38:26 -0700766 Region opaqueRegion;
767 Region dirtyRegion;
768 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700769 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700770
771 Vector< sp<LayerBase> > layersSortedByZ;
772 const size_t count = currentLayers.size();
773 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700774 const sp<LayerBase>& layer(currentLayers[i]);
775 const Layer::State& s(layer->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700776 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700777 Region visibleRegion(tr.transform(layer->visibleRegion));
778 visibleRegion.andSelf(bounds);
779 if (!visibleRegion.isEmpty()) {
780 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -0700781 }
782 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700783 }
Mathias Agopian42977342012-08-05 00:40:46 -0700784 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700785 hw->undefinedRegion.set(bounds);
786 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
787 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700788 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700789 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700791
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700793 HWComposer& hwc(getHwComposer());
794 if (hwc.initCheck() == NO_ERROR) {
795 // build the h/w work list
796 const bool workListsDirty = mHwWorkListDirty;
797 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700798 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700799 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700800 const int32_t id = hw->getHwcDisplayId();
801 if (id >= 0) {
802 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700803 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700804 const size_t count = currentLayers.size();
805 if (hwc.createWorkList(id, count) >= 0) {
806 HWComposer::LayerListIterator cur = hwc.begin(id);
807 const HWComposer::LayerListIterator end = hwc.end(id);
808 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
809 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700810
Mathias Agopiane60b0682012-08-21 23:34:09 -0700811 if (CC_UNLIKELY(workListsDirty)) {
812 layer->setGeometry(hw, *cur);
813 if (mDebugDisableHWC || mDebugRegion) {
814 cur->setSkip(true);
815 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700816 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700817
Mathias Agopiane60b0682012-08-21 23:34:09 -0700818 /*
819 * update the per-frame h/w composer data for each layer
820 * and build the transparent region of the FB
821 */
822 layer->setPerFrameData(hw, *cur);
823 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700824 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700825 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700826 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700827 status_t err = hwc.prepare();
828 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
829 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700830}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700831
Mathias Agopiancd60f992012-08-16 16:28:27 -0700832void SurfaceFlinger::doComposition() {
833 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700834 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700835 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700836 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700837 if (hw->canDraw()) {
838 // transform the dirty region into this screen's coordinate space
839 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
840 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700841 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700843 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 hw->dirtyRegion.clear();
845 hw->flip(hw->swapRegion);
846 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700847 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700849 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700852}
853
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854void SurfaceFlinger::postFramebuffer()
855{
Mathias Agopian841cde52012-03-01 15:44:37 -0800856 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800857
Mathias Agopiana44b0412011-10-16 18:46:35 -0700858 const nsecs_t now = systemTime();
859 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700860
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700861 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700862 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700863 // FIXME: EGL spec says:
864 // "surface must be bound to the calling thread's current context,
865 // for the current rendering API."
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700866 DisplayDevice::makeCurrent(getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700867 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700868 }
869
Mathias Agopian92a979a2012-08-02 18:32:23 -0700870 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700871 sp<const DisplayDevice> hw(mDisplays[dpy]);
872 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700873 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700874 int32_t id = hw->getHwcDisplayId();
875 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700876 HWComposer::LayerListIterator cur = hwc.begin(id);
877 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700878 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700879 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700880 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700882 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700883 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700884 }
Jesse Hallef194142012-06-14 14:45:17 -0700885 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800886 }
887
Mathias Agopiana44b0412011-10-16 18:46:35 -0700888 mLastSwapBufferTime = systemTime() - now;
889 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890}
891
Mathias Agopian87baae12012-07-31 12:38:26 -0700892void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893{
Mathias Agopian841cde52012-03-01 15:44:37 -0800894 ATRACE_CALL();
895
Mathias Agopianca4d3602011-05-19 15:38:14 -0700896 Mutex::Autolock _l(mStateLock);
897 const nsecs_t now = systemTime();
898 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899
Mathias Agopianca4d3602011-05-19 15:38:14 -0700900 // Here we're guaranteed that some transaction flags are set
901 // so we can call handleTransactionLocked() unconditionally.
902 // We call getTransactionFlags(), which will also clear the flags,
903 // with mStateLock held to guarantee that mCurrentState won't change
904 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700905
Mathias Agopiane57f2922012-08-09 16:29:12 -0700906 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700907 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700908
Mathias Agopianca4d3602011-05-19 15:38:14 -0700909 mLastTransactionTime = systemTime() - now;
910 mDebugInTransaction = 0;
911 invalidateHwcGeometry();
912 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700913}
914
Mathias Agopian87baae12012-07-31 12:38:26 -0700915void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700916{
917 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 const size_t count = currentLayers.size();
919
920 /*
921 * Traversal of the children
922 * (perform the transaction for each of them if needed)
923 */
924
Mathias Agopian3559b072012-08-15 13:46:03 -0700925 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700927 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
929 if (!trFlags) continue;
930
931 const uint32_t flags = layer->doTransaction(0);
932 if (flags & Layer::eVisibleRegion)
933 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 }
935 }
936
937 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700938 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 */
940
Mathias Agopiane57f2922012-08-09 16:29:12 -0700941 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700942 // here we take advantage of Vector's copy-on-write semantics to
943 // improve performance by skipping the transaction entirely when
944 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700945 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
946 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700947 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700949 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700950 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700951
952 // find the displays that were removed
953 // (ie: in drawing state but not in current state)
954 // also handle displays that changed
955 // (ie: displays that are in both lists)
956 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700957 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
958 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700959 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700960 if (!draw[i].isMainDisplay()) {
961 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700962 } else {
963 ALOGW("trying to remove the main display");
964 }
965 } else {
966 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700967 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700968 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700969 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700971 // recreating the DisplayDevice, so we just remove it
972 // from the drawing state, so that it get re-added
973 // below.
974 mDisplays.removeItem(display);
975 mDrawingState.displays.removeItemsAt(i);
976 dc--; i--;
977 // at this point we must loop to the next item
978 continue;
979 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700980
Mathias Agopian93997a82012-08-29 17:30:36 -0700981 const sp<DisplayDevice>& disp(getDisplayDevice(display));
982 if (disp != NULL) {
983 if (state.layerStack != draw[i].layerStack) {
984 disp->setLayerStack(state.layerStack);
985 }
986 if (state.orientation != draw[i].orientation ||
987 state.viewport != draw[i].viewport ||
988 state.frame != draw[i].frame) {
989 disp->setOrientation(state.orientation);
990 // TODO: take viewport and frame into account
991 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700992 }
993 }
994 }
995
996 // find displays that were added
997 // (ie: in current state but not in drawing state)
998 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700999 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001000 const DisplayDeviceState& state(curr[i]);
Mathias Agopian93997a82012-08-29 17:30:36 -07001001 if (state.surface != NULL) {
1002 sp<SurfaceTextureClient> stc(
1003 new SurfaceTextureClient(state.surface));
1004 const wp<IBinder>& display(curr.keyAt(i));
1005 sp<DisplayDevice> disp = new DisplayDevice(this,
1006 state.type, display, stc, 0, mEGLConfig);
1007 disp->setLayerStack(state.layerStack);
1008 disp->setOrientation(state.orientation);
1009 // TODO: take viewport and frame into account
1010 mDisplays.add(display, disp);
1011 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001012 }
1013 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001015 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016
Mathias Agopian3559b072012-08-15 13:46:03 -07001017 /*
1018 * Perform our own transaction if needed
1019 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001020
Mathias Agopiancd60f992012-08-16 16:28:27 -07001021 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1022 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001023 // layers have been added
1024 mVisibleRegionsDirty = true;
1025 }
1026
1027 // some layers might have been removed, so
1028 // we need to update the regions they're exposing.
1029 if (mLayersRemoved) {
1030 mLayersRemoved = false;
1031 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001032 const size_t count = previousLayers.size();
1033 for (size_t i=0 ; i<count ; i++) {
1034 const sp<LayerBase>& layer(previousLayers[i]);
1035 if (currentLayers.indexOf(layer) < 0) {
1036 // this layer is not visible anymore
1037 // TODO: we could traverse the tree from front to back and
1038 // compute the actual visible region
1039 // TODO: we could cache the transformed region
1040 Layer::State front(layer->drawingState());
1041 Region visibleReg = front.transform.transform(
1042 Region(Rect(front.active.w, front.active.h)));
1043 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001044 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 }
1047
1048 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001049}
1050
1051void SurfaceFlinger::commitTransaction()
1052{
1053 if (!mLayersPendingRemoval.isEmpty()) {
1054 // Notify removed layers now that they can't be drawn from
1055 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1056 mLayersPendingRemoval[i]->onRemoved();
1057 }
1058 mLayersPendingRemoval.clear();
1059 }
1060
1061 mDrawingState = mCurrentState;
1062 mTransationPending = false;
1063 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064}
1065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001067 const LayerVector& currentLayers, uint32_t layerStack,
1068 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069{
Mathias Agopian841cde52012-03-01 15:44:37 -08001070 ATRACE_CALL();
1071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 Region aboveOpaqueLayers;
1073 Region aboveCoveredLayers;
1074 Region dirty;
1075
Mathias Agopian87baae12012-07-31 12:38:26 -07001076 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077
1078 size_t i = currentLayers.size();
1079 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001080 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081
1082 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001083 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084
Mathias Agopian87baae12012-07-31 12:38:26 -07001085 // only consider the layers on the given later stack
1086 if (s.layerStack != layerStack)
1087 continue;
1088
Mathias Agopianab028732010-03-16 16:41:46 -07001089 /*
1090 * opaqueRegion: area of a surface that is fully opaque.
1091 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001093
1094 /*
1095 * visibleRegion: area of a surface that is visible on screen
1096 * and not fully transparent. This is essentially the layer's
1097 * footprint minus the opaque regions above it.
1098 * Areas covered by a translucent surface are considered visible.
1099 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001101
1102 /*
1103 * coveredRegion: area of a surface that is covered by all
1104 * visible regions above it (which includes the translucent areas).
1105 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001107
1108
1109 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001110 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001111 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001112 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001114 if (!visibleRegion.isEmpty()) {
1115 // Remove the transparent area from the visible region
1116 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001117 Region transparentRegionScreen;
1118 const Transform tr(s.transform);
1119 if (tr.transformed()) {
1120 if (tr.preserveRects()) {
1121 // transform the transparent region
1122 transparentRegionScreen = tr.transform(s.transparentRegion);
1123 } else {
1124 // transformation too complex, can't do the
1125 // transparent region optimization.
1126 transparentRegionScreen.clear();
1127 }
1128 } else {
1129 transparentRegionScreen = s.transparentRegion;
1130 }
1131 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001132 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133
Mathias Agopianab028732010-03-16 16:41:46 -07001134 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001135 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001136 if (s.alpha==255 && !translucent &&
1137 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1138 // the opaque region is the layer's footprint
1139 opaqueRegion = visibleRegion;
1140 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 }
1142 }
1143
Mathias Agopianab028732010-03-16 16:41:46 -07001144 // Clip the covered region to the visible region
1145 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1146
1147 // Update aboveCoveredLayers for next (lower) layer
1148 aboveCoveredLayers.orSelf(visibleRegion);
1149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 // subtract the opaque region covered by the layers above us
1151 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152
1153 // compute this layer's dirty region
1154 if (layer->contentDirty) {
1155 // we need to invalidate the whole region
1156 dirty = visibleRegion;
1157 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001158 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 layer->contentDirty = false;
1160 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001161 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001162 * the exposed region consists of two components:
1163 * 1) what's VISIBLE now and was COVERED before
1164 * 2) what's EXPOSED now less what was EXPOSED before
1165 *
1166 * note that (1) is conservative, we start with the whole
1167 * visible region but only keep what used to be covered by
1168 * something -- which mean it may have been exposed.
1169 *
1170 * (2) handles areas that were not covered by anything but got
1171 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001172 */
Mathias Agopianab028732010-03-16 16:41:46 -07001173 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001174 const Region oldVisibleRegion = layer->visibleRegion;
1175 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001176 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1177 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 }
1179 dirty.subtractSelf(aboveOpaqueLayers);
1180
1181 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001182 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183
Mathias Agopianab028732010-03-16 16:41:46 -07001184 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 // Store the visible region is screen space
1188 layer->setVisibleRegion(visibleRegion);
1189 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 }
1191
Mathias Agopian87baae12012-07-31 12:38:26 -07001192 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193}
1194
Mathias Agopian87baae12012-07-31 12:38:26 -07001195void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1196 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001197 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001198 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1199 if (hw->getLayerStack() == layerStack) {
1200 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001201 }
1202 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001203}
1204
1205void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001207 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208
Mathias Agopian4fec8732012-06-29 14:12:52 -07001209 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001210 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001211 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001212 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001213 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001214 const Region dirty(layer->latchBuffer(visibleRegions));
1215 Layer::State s(layer->drawingState());
1216 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001217 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001218
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001219 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220}
1221
Mathias Agopianad456f92011-01-13 17:53:01 -08001222void SurfaceFlinger::invalidateHwcGeometry()
1223{
1224 mHwWorkListDirty = true;
1225}
1226
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001227
Mathias Agopiancd60f992012-08-16 16:28:27 -07001228void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001229 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230{
Mathias Agopian87baae12012-07-31 12:38:26 -07001231 Region dirtyRegion(inDirtyRegion);
1232
Mathias Agopianb8a55602009-06-26 19:06:36 -07001233 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001234 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235
Mathias Agopian42977342012-08-05 00:40:46 -07001236 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001237 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001238 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1239 // takes a rectangle, we must make sure to update that whole
1240 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001241 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001242 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001243 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001244 // We need to redraw the rectangle that will be updated
1245 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001246 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001247 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001248 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001249 } else {
1250 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001251 dirtyRegion.set(hw->bounds());
1252 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 }
1254 }
1255
Mathias Agopiancd60f992012-08-16 16:28:27 -07001256 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257
Mathias Agopiancd60f992012-08-16 16:28:27 -07001258 // FIXME: we need to call eglSwapBuffers() on displays that have
1259 // GL composition and only on those.
1260 // however, currently hwc.commit() already does that for the main
Jesse Hall9ca48912012-08-30 13:42:23 -07001261 // display (if there is a hwc) and never for the other ones
1262 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL ||
1263 getHwComposer().initCheck() != NO_ERROR) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001264 // FIXME: EGL spec says:
1265 // "surface must be bound to the calling thread's current context,
1266 // for the current rendering API."
1267 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001268 }
1269
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001270 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001271 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272}
1273
Mathias Agopiancd60f992012-08-16 16:28:27 -07001274void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001275{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001276 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001277 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001278 HWComposer::LayerListIterator cur = hwc.begin(id);
1279 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001280
Mathias Agopian85d751c2012-08-29 16:59:24 -07001281 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1282 if (hasGlesComposition) {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001283 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001284
1285 // set the frame buffer
1286 glMatrixMode(GL_MODELVIEW);
1287 glLoadIdentity();
1288
1289 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001290 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001291 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001292 // when using overlays, we assume a fully transparent framebuffer
1293 // NOTE: we could reduce how much we need to clear, for instance
1294 // remove where there are opaque FB layers. however, on some
1295 // GPUs doing a "clean slate" glClear might be more efficient.
1296 // We'll revisit later if needed.
1297 glClearColor(0, 0, 0, 0);
1298 glClear(GL_COLOR_BUFFER_BIT);
1299 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001300 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001301 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001302 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001303 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001304 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001305 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001306 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001307 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001308
Mathias Agopian85d751c2012-08-29 16:59:24 -07001309 /*
1310 * and then, render the layers targeted at the framebuffer
1311 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001312
Mathias Agopian85d751c2012-08-29 16:59:24 -07001313 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1314 const size_t count = layers.size();
1315 const Transform& tr = hw->getTransform();
1316 if (cur != end) {
1317 // we're using h/w composer
1318 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001319 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001320 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001321 if (!clip.isEmpty()) {
1322 switch (cur->getCompositionType()) {
1323 case HWC_OVERLAY: {
1324 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1325 && i
1326 && layer->isOpaque()
1327 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001328 // never clear the very first layer since we're
1329 // guaranteed the FB is already cleared
1330 layer->clearWithOpenGL(hw, clip);
1331 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001332 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001333 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001334 case HWC_FRAMEBUFFER: {
1335 layer->draw(hw, clip);
1336 break;
1337 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001338 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001339 }
1340 layer->setAcquireFence(hw, *cur);
1341 }
1342 } else {
1343 // we're not using h/w composer
1344 for (size_t i=0 ; i<count ; ++i) {
1345 const sp<LayerBase>& layer(layers[i]);
1346 const Region clip(dirty.intersect(
1347 tr.transform(layer->visibleRegion)));
1348 if (!clip.isEmpty()) {
1349 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001350 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001351 }
1352 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353}
1354
Mathias Agopian55801e42012-08-27 18:54:24 -07001355void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1356 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001358 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001359 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001360 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001361 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001362
Mathias Agopian55801e42012-08-27 18:54:24 -07001363 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001364 Region::const_iterator it = region.begin();
1365 Region::const_iterator const end = region.end();
1366 while (it != end) {
1367 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001368 GLfloat vertices[][2] = {
1369 { r.left, height - r.top },
1370 { r.left, height - r.bottom },
1371 { r.right, height - r.bottom },
1372 { r.right, height - r.top }
1373 };
1374 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001375 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1376 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377}
1378
Mathias Agopian96f08192010-06-02 23:28:45 -07001379ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1380 const sp<LayerBaseClient>& lbc)
1381{
Mathias Agopian96f08192010-06-02 23:28:45 -07001382 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001383 size_t name = client->attachLayer(lbc);
1384
Mathias Agopian96f08192010-06-02 23:28:45 -07001385 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001386 Mutex::Autolock _l(mStateLock);
1387 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001388
Mathias Agopian4f113742011-05-03 16:21:41 -07001389 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001390}
1391
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001392status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393{
1394 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001395 status_t err = purgatorizeLayer_l(layer);
1396 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001397 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001398 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399}
1400
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001401status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402{
1403 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1404 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001405 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 return NO_ERROR;
1407 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001408 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409}
1410
Mathias Agopian9a112062009-04-17 19:36:26 -07001411status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1412{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001413 // First add the layer to the purgatory list, which makes sure it won't
1414 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001415 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001416 if (err >= 0) {
1417 mLayerPurgatory.add(layerBase);
1418 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001419
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001420 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001421
Mathias Agopian3d579642009-06-04 18:46:21 -07001422 // it's possible that we don't find a layer, because it might
1423 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001424 // from the user because there is a race between Client::destroySurface(),
1425 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001426 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1427}
1428
Mathias Agopiandea20b12011-05-03 17:04:02 -07001429uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1430{
1431 return android_atomic_release_load(&mTransactionFlags);
1432}
1433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1435{
1436 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1437}
1438
Mathias Agopianbb641242010-05-18 17:06:55 -07001439uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440{
1441 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1442 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001443 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 }
1445 return old;
1446}
1447
Mathias Agopian8b33f032012-07-24 20:43:54 -07001448void SurfaceFlinger::setTransactionState(
1449 const Vector<ComposerState>& state,
1450 const Vector<DisplayState>& displays,
1451 uint32_t flags)
1452{
Mathias Agopian698c0872011-06-28 19:09:31 -07001453 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001454 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001455
1456 size_t count = displays.size();
1457 for (size_t i=0 ; i<count ; i++) {
1458 const DisplayState& s(displays[i]);
1459 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001460 }
1461
Mathias Agopiane57f2922012-08-09 16:29:12 -07001462 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001463 for (size_t i=0 ; i<count ; i++) {
1464 const ComposerState& s(state[i]);
1465 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001466 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001467 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001468
Mathias Agopian386aa982011-11-07 21:58:03 -08001469 if (transactionFlags) {
1470 // this triggers the transaction
1471 setTransactionFlags(transactionFlags);
1472
1473 // if this is a synchronous transaction, wait for it to take effect
1474 // before returning.
1475 if (flags & eSynchronous) {
1476 mTransationPending = true;
1477 }
1478 while (mTransationPending) {
1479 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1480 if (CC_UNLIKELY(err != NO_ERROR)) {
1481 // just in case something goes wrong in SF, return to the
1482 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001483 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001484 mTransationPending = false;
1485 break;
1486 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001487 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 }
1489}
1490
Mathias Agopiane57f2922012-08-09 16:29:12 -07001491uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1492{
1493 uint32_t flags = 0;
1494 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001495 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001496 const uint32_t what = s.what;
1497 if (what & DisplayState::eSurfaceChanged) {
1498 if (disp.surface->asBinder() != s.surface->asBinder()) {
1499 disp.surface = s.surface;
1500 flags |= eDisplayTransactionNeeded;
1501 }
1502 }
1503 if (what & DisplayState::eLayerStackChanged) {
1504 if (disp.layerStack != s.layerStack) {
1505 disp.layerStack = s.layerStack;
1506 flags |= eDisplayTransactionNeeded;
1507 }
1508 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001509 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001510 if (disp.orientation != s.orientation) {
1511 disp.orientation = s.orientation;
1512 flags |= eDisplayTransactionNeeded;
1513 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001514 }
1515 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001516 if (disp.frame != s.frame) {
1517 disp.frame = s.frame;
1518 flags |= eDisplayTransactionNeeded;
1519 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001520 }
1521 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001522 if (disp.viewport != s.viewport) {
1523 disp.viewport = s.viewport;
1524 flags |= eDisplayTransactionNeeded;
1525 }
1526 }
1527 }
1528 return flags;
1529}
1530
1531uint32_t SurfaceFlinger::setClientStateLocked(
1532 const sp<Client>& client,
1533 const layer_state_t& s)
1534{
1535 uint32_t flags = 0;
1536 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1537 if (layer != 0) {
1538 const uint32_t what = s.what;
1539 if (what & layer_state_t::ePositionChanged) {
1540 if (layer->setPosition(s.x, s.y))
1541 flags |= eTraversalNeeded;
1542 }
1543 if (what & layer_state_t::eLayerChanged) {
1544 // NOTE: index needs to be calculated before we update the state
1545 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1546 if (layer->setLayer(s.z)) {
1547 mCurrentState.layersSortedByZ.removeAt(idx);
1548 mCurrentState.layersSortedByZ.add(layer);
1549 // we need traversal (state changed)
1550 // AND transaction (list changed)
1551 flags |= eTransactionNeeded|eTraversalNeeded;
1552 }
1553 }
1554 if (what & layer_state_t::eSizeChanged) {
1555 if (layer->setSize(s.w, s.h)) {
1556 flags |= eTraversalNeeded;
1557 }
1558 }
1559 if (what & layer_state_t::eAlphaChanged) {
1560 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1561 flags |= eTraversalNeeded;
1562 }
1563 if (what & layer_state_t::eMatrixChanged) {
1564 if (layer->setMatrix(s.matrix))
1565 flags |= eTraversalNeeded;
1566 }
1567 if (what & layer_state_t::eTransparentRegionChanged) {
1568 if (layer->setTransparentRegionHint(s.transparentRegion))
1569 flags |= eTraversalNeeded;
1570 }
1571 if (what & layer_state_t::eVisibilityChanged) {
1572 if (layer->setFlags(s.flags, s.mask))
1573 flags |= eTraversalNeeded;
1574 }
1575 if (what & layer_state_t::eCropChanged) {
1576 if (layer->setCrop(s.crop))
1577 flags |= eTraversalNeeded;
1578 }
1579 if (what & layer_state_t::eLayerStackChanged) {
1580 // NOTE: index needs to be calculated before we update the state
1581 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1582 if (layer->setLayerStack(s.layerStack)) {
1583 mCurrentState.layersSortedByZ.removeAt(idx);
1584 mCurrentState.layersSortedByZ.add(layer);
1585 // we need traversal (state changed)
1586 // AND transaction (list changed)
1587 flags |= eTransactionNeeded|eTraversalNeeded;
1588 }
1589 }
1590 }
1591 return flags;
1592}
1593
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001594sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001595 ISurfaceComposerClient::surface_data_t* params,
1596 const String8& name,
1597 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001598 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599 uint32_t flags)
1600{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001601 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001602 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001603
1604 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001605 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001606 int(w), int(h));
1607 return surfaceHandle;
1608 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001609
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001610 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001611 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1612 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001613 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001615 case ISurfaceComposerClient::eFXSurfaceBlur:
1616 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001617 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001619 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001620 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001621 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 }
1623
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001624 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001625 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001626 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001627 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001629 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001630 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001631 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001632 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001633 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634 }
1635
1636 return surfaceHandle;
1637}
1638
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001639sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001640 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001641 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001642 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643{
1644 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001645 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 case PIXEL_FORMAT_TRANSPARENT:
1647 case PIXEL_FORMAT_TRANSLUCENT:
1648 format = PIXEL_FORMAT_RGBA_8888;
1649 break;
1650 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001651#ifdef NO_RGBX_8888
1652 format = PIXEL_FORMAT_RGB_565;
1653#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001654 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001655#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 break;
1657 }
1658
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001659#ifdef NO_RGBX_8888
1660 if (format == PIXEL_FORMAT_RGBX_8888)
1661 format = PIXEL_FORMAT_RGBA_8888;
1662#endif
1663
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001664 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001665 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001666 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001667 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001668 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 }
1670 return layer;
1671}
1672
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001673sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001674 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001675 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001677 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001678 return layer;
1679}
1680
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001681sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001682 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001683 uint32_t w, uint32_t h, uint32_t flags)
1684{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001685 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 return layer;
1687}
1688
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001689status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690{
Mathias Agopian9a112062009-04-17 19:36:26 -07001691 /*
1692 * called by the window manager, when a surface should be marked for
1693 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001694 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001695 * The surface is removed from the current and drawing lists, but placed
1696 * in the purgatory queue, so it's not destroyed right-away (we need
1697 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001698 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001699
Mathias Agopian48d819a2009-09-10 19:41:18 -07001700 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001701 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001702 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001703
Mathias Agopian48d819a2009-09-10 19:41:18 -07001704 if (layer != 0) {
1705 err = purgatorizeLayer_l(layer);
1706 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001707 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001708 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001709 }
1710 return err;
1711}
1712
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001713status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001714{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001715 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001716 status_t err = NO_ERROR;
1717 sp<LayerBaseClient> l(layer.promote());
1718 if (l != NULL) {
1719 Mutex::Autolock _l(mStateLock);
1720 err = removeLayer_l(l);
1721 if (err == NAME_NOT_FOUND) {
1722 // The surface wasn't in the current list, which means it was
1723 // removed already, which means it is in the purgatory,
1724 // and need to be removed from there.
1725 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001726 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001727 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001728 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001729 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001730 "error removing layer=%p (%s)", l.get(), strerror(-err));
1731 }
1732 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733}
1734
Mathias Agopianb60314a2012-04-10 22:09:54 -07001735// ---------------------------------------------------------------------------
1736
Andy McFadden13a082e2012-08-24 10:16:42 -07001737void SurfaceFlinger::onInitializeDisplays() {
1738 // reset screen orientation
1739 Vector<ComposerState> state;
1740 Vector<DisplayState> displays;
1741 DisplayState d;
1742 d.what = DisplayState::eOrientationChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001743 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001744 d.orientation = DisplayState::eOrientationDefault;
1745 displays.add(d);
1746 setTransactionState(state, displays, 0);
1747
1748 // XXX: this should init default device to "unblank" and all other devices to "blank"
1749 onScreenAcquired();
1750}
1751
1752void SurfaceFlinger::initializeDisplays() {
1753 class MessageScreenInitialized : public MessageBase {
1754 SurfaceFlinger* flinger;
1755 public:
1756 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1757 virtual bool handler() {
1758 flinger->onInitializeDisplays();
1759 return true;
1760 }
1761 };
1762 sp<MessageBase> msg = new MessageScreenInitialized(this);
1763 postMessageAsync(msg); // we may be called from main thread, use async message
1764}
1765
1766
Mathias Agopianb60314a2012-04-10 22:09:54 -07001767void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001768 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001769 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001770 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001771 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001772 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001773 mVisibleRegionsDirty = true;
1774 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775}
1776
Mathias Agopianb60314a2012-04-10 22:09:54 -07001777void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001778 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001779 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1780 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001781 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001782 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001783 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001784 // from this point on, SF will stop drawing
1785 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786}
1787
Colin Cross8e533062012-06-07 13:17:52 -07001788void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001789 class MessageScreenAcquired : public MessageBase {
1790 SurfaceFlinger* flinger;
1791 public:
1792 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1793 virtual bool handler() {
1794 flinger->onScreenAcquired();
1795 return true;
1796 }
1797 };
1798 sp<MessageBase> msg = new MessageScreenAcquired(this);
1799 postMessageSync(msg);
1800}
1801
Colin Cross8e533062012-06-07 13:17:52 -07001802void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001803 class MessageScreenReleased : public MessageBase {
1804 SurfaceFlinger* flinger;
1805 public:
1806 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1807 virtual bool handler() {
1808 flinger->onScreenReleased();
1809 return true;
1810 }
1811 };
1812 sp<MessageBase> msg = new MessageScreenReleased(this);
1813 postMessageSync(msg);
1814}
1815
1816// ---------------------------------------------------------------------------
1817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1819{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001820 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 char buffer[SIZE];
1822 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001823
1824 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 snprintf(buffer, SIZE, "Permission Denial: "
1826 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1827 IPCThreadState::self()->getCallingPid(),
1828 IPCThreadState::self()->getCallingUid());
1829 result.append(buffer);
1830 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001831 // Try to get the main lock, but don't insist if we can't
1832 // (this would indicate SF is stuck, but we want to be able to
1833 // print something in dumpsys).
1834 int retry = 3;
1835 while (mStateLock.tryLock()<0 && --retry>=0) {
1836 usleep(1000000);
1837 }
1838 const bool locked(retry >= 0);
1839 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001840 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001841 "SurfaceFlinger appears to be unresponsive, "
1842 "dumping anyways (no locks held)\n");
1843 result.append(buffer);
1844 }
1845
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001846 bool dumpAll = true;
1847 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001848 size_t numArgs = args.size();
1849 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001850 if ((index < numArgs) &&
1851 (args[index] == String16("--list"))) {
1852 index++;
1853 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001854 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001855 }
1856
1857 if ((index < numArgs) &&
1858 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001859 index++;
1860 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001861 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001862 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001863
1864 if ((index < numArgs) &&
1865 (args[index] == String16("--latency-clear"))) {
1866 index++;
1867 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001868 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001869 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001871
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001872 if (dumpAll) {
1873 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001874 }
1875
Mathias Agopian9795c422009-08-26 16:36:26 -07001876 if (locked) {
1877 mStateLock.unlock();
1878 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879 }
1880 write(fd, result.string(), result.size());
1881 return NO_ERROR;
1882}
1883
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001884void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1885 String8& result, char* buffer, size_t SIZE) const
1886{
1887 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1888 const size_t count = currentLayers.size();
1889 for (size_t i=0 ; i<count ; i++) {
1890 const sp<LayerBase>& layer(currentLayers[i]);
1891 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1892 result.append(buffer);
1893 }
1894}
1895
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001896void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1897 String8& result, char* buffer, size_t SIZE) const
1898{
1899 String8 name;
1900 if (index < args.size()) {
1901 name = String8(args[index]);
1902 index++;
1903 }
1904
1905 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1906 const size_t count = currentLayers.size();
1907 for (size_t i=0 ; i<count ; i++) {
1908 const sp<LayerBase>& layer(currentLayers[i]);
1909 if (name.isEmpty()) {
1910 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1911 result.append(buffer);
1912 }
1913 if (name.isEmpty() || (name == layer->getName())) {
1914 layer->dumpStats(result, buffer, SIZE);
1915 }
1916 }
1917}
1918
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001919void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1920 String8& result, char* buffer, size_t SIZE) const
1921{
1922 String8 name;
1923 if (index < args.size()) {
1924 name = String8(args[index]);
1925 index++;
1926 }
1927
1928 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1929 const size_t count = currentLayers.size();
1930 for (size_t i=0 ; i<count ; i++) {
1931 const sp<LayerBase>& layer(currentLayers[i]);
1932 if (name.isEmpty() || (name == layer->getName())) {
1933 layer->clearStats();
1934 }
1935 }
1936}
1937
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001938void SurfaceFlinger::dumpAllLocked(
1939 String8& result, char* buffer, size_t SIZE) const
1940{
1941 // figure out if we're stuck somewhere
1942 const nsecs_t now = systemTime();
1943 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1944 const nsecs_t inTransaction(mDebugInTransaction);
1945 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1946 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1947
1948 /*
1949 * Dump the visible layer list
1950 */
1951 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1952 const size_t count = currentLayers.size();
1953 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1954 result.append(buffer);
1955 for (size_t i=0 ; i<count ; i++) {
1956 const sp<LayerBase>& layer(currentLayers[i]);
1957 layer->dump(result, buffer, SIZE);
1958 }
1959
1960 /*
1961 * Dump the layers in the purgatory
1962 */
1963
1964 const size_t purgatorySize = mLayerPurgatory.size();
1965 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1966 result.append(buffer);
1967 for (size_t i=0 ; i<purgatorySize ; i++) {
1968 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1969 layer->shortDump(result, buffer, SIZE);
1970 }
1971
1972 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001973 * Dump Display state
1974 */
1975
1976 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1977 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1978 snprintf(buffer, SIZE,
1979 "+ DisplayDevice[%u]\n"
1980 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1981 "flips=%u, secure=%d, numLayers=%u\n",
1982 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001983 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001984 hw->getWidth(), hw->getHeight(),
1985 hw->getOrientation(), hw->getTransform().getType(),
1986 hw->getPageFlipCount(),
1987 hw->getSecureLayerVisible(),
1988 hw->getVisibleLayersSortedByZ().size());
1989 result.append(buffer);
1990 }
1991
1992 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001993 * Dump SurfaceFlinger global state
1994 */
1995
1996 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1997 result.append(buffer);
1998
Mathias Agopian888c8222012-08-04 21:10:38 -07001999 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002000 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002001 const GLExtensions& extensions(GLExtensions::getInstance());
2002 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2003 extensions.getVendor(),
2004 extensions.getRenderer(),
2005 extensions.getVersion());
2006 result.append(buffer);
2007
2008 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002009 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002010 result.append(buffer);
2011
2012 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2013 result.append(buffer);
2014
Mathias Agopian42977342012-08-05 00:40:46 -07002015 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002016 snprintf(buffer, SIZE,
2017 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002018 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002019 result.append(buffer);
2020 snprintf(buffer, SIZE,
2021 " last eglSwapBuffers() time: %f us\n"
2022 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002023 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002024 " refresh-rate : %f fps\n"
2025 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002026 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002027 mLastSwapBufferTime/1000.0,
2028 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002029 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07002030 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07002031 hwc.getDpiX(),
2032 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002033 result.append(buffer);
2034
2035 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2036 inSwapBuffersDuration/1000.0);
2037 result.append(buffer);
2038
2039 snprintf(buffer, SIZE, " transaction time: %f us\n",
2040 inTransactionDuration/1000.0);
2041 result.append(buffer);
2042
2043 /*
2044 * VSYNC state
2045 */
2046 mEventThread->dump(result, buffer, SIZE);
2047
2048 /*
2049 * Dump HWComposer state
2050 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002051 snprintf(buffer, SIZE, "h/w composer state:\n");
2052 result.append(buffer);
2053 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2054 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2055 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2056 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002057 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002058
2059 /*
2060 * Dump gralloc state
2061 */
2062 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2063 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002064 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002065}
2066
Keun young Park63f165f2012-08-31 10:53:36 -07002067bool SurfaceFlinger::startDdmConnection()
2068{
2069 void* libddmconnection_dso =
2070 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2071 if (!libddmconnection_dso) {
2072 return false;
2073 }
2074 void (*DdmConnection_start)(const char* name);
2075 DdmConnection_start =
2076 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2077 if (!DdmConnection_start) {
2078 dlclose(libddmconnection_dso);
2079 return false;
2080 }
2081 (*DdmConnection_start)(getServiceName());
2082 return true;
2083}
2084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085status_t SurfaceFlinger::onTransact(
2086 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2087{
2088 switch (code) {
2089 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002090 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002092 case BLANK:
2093 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002094 {
2095 // codes that require permission check
2096 IPCThreadState* ipc = IPCThreadState::self();
2097 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002098 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002099 if ((uid != AID_GRAPHICS) &&
2100 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002101 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002102 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2103 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002104 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002105 break;
2106 }
2107 case CAPTURE_SCREEN:
2108 {
2109 // codes that require permission check
2110 IPCThreadState* ipc = IPCThreadState::self();
2111 const int pid = ipc->getCallingPid();
2112 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002113 if ((uid != AID_GRAPHICS) &&
2114 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002115 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002116 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2117 return PERMISSION_DENIED;
2118 }
2119 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120 }
2121 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2124 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002125 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002126 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002127 IPCThreadState* ipc = IPCThreadState::self();
2128 const int pid = ipc->getCallingPid();
2129 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002130 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002131 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002132 return PERMISSION_DENIED;
2133 }
2134 int n;
2135 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002136 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002137 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002138 return NO_ERROR;
2139 case 1002: // SHOW_UPDATES
2140 n = data.readInt32();
2141 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002142 invalidateHwcGeometry();
2143 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002145 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002146 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002147 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002149 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002150 setTransactionFlags(
2151 eTransactionNeeded|
2152 eDisplayTransactionNeeded|
2153 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002154 return NO_ERROR;
2155 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002156 case 1006:{ // send empty update
2157 signalRefresh();
2158 return NO_ERROR;
2159 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002160 case 1008: // toggle use of hw composer
2161 n = data.readInt32();
2162 mDebugDisableHWC = n ? 1 : 0;
2163 invalidateHwcGeometry();
2164 repaintEverything();
2165 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002166 case 1009: // toggle use of transform hint
2167 n = data.readInt32();
2168 mDebugDisableTransformHint = n ? 1 : 0;
2169 invalidateHwcGeometry();
2170 repaintEverything();
2171 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002172 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002173 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174 reply->writeInt32(0);
2175 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002176 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002177 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002178 return NO_ERROR;
2179 case 1013: {
2180 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002181 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2182 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183 }
2184 return NO_ERROR;
2185 }
2186 }
2187 return err;
2188}
2189
Mathias Agopian53331da2011-08-22 21:44:41 -07002190void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002191 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002192 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002193}
2194
Mathias Agopian59119e62010-10-11 12:37:43 -07002195// ---------------------------------------------------------------------------
2196
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002197status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002198 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2199{
2200 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002201 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002202}
2203
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002204status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002206{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002207 ATRACE_CALL();
2208
Mathias Agopian59119e62010-10-11 12:37:43 -07002209 if (!GLExtensions::getInstance().haveFramebufferObject())
2210 return INVALID_OPERATION;
2211
2212 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002213 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2214 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002215 const uint32_t hw_w = hw->getWidth();
2216 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002217 GLfloat u = 1;
2218 GLfloat v = 1;
2219
2220 // make sure to clear all GL error flags
2221 while ( glGetError() != GL_NO_ERROR ) ;
2222
2223 // create a FBO
2224 GLuint name, tname;
2225 glGenTextures(1, &tname);
2226 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002227 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2228 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002229 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2230 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002231 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002232 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002233 GLint tw = (2 << (31 - clz(hw_w)));
2234 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002235 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2236 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002237 u = GLfloat(hw_w) / tw;
2238 v = GLfloat(hw_h) / th;
2239 }
2240 glGenFramebuffersOES(1, &name);
2241 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002242 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2243 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002244
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002245 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002246 glDisable(GL_TEXTURE_EXTERNAL_OES);
2247 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002248 glClearColor(0,0,0,1);
2249 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002250 glMatrixMode(GL_MODELVIEW);
2251 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002252 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002253 const size_t count = layers.size();
2254 for (size_t i=0 ; i<count ; ++i) {
2255 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002256 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002257 }
2258
Mathias Agopian42977342012-08-05 00:40:46 -07002259 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002260
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002261 // back to main framebuffer
2262 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002263 glDeleteFramebuffersOES(1, &name);
2264
2265 *textureName = tname;
2266 *uOut = u;
2267 *vOut = v;
2268 return NO_ERROR;
2269}
2270
2271// ---------------------------------------------------------------------------
2272
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002273status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002274 sp<IMemoryHeap>* heap,
2275 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002276 uint32_t sw, uint32_t sh,
2277 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002279 ATRACE_CALL();
2280
Mathias Agopian74c40c02010-09-29 13:02:36 -07002281 status_t result = PERMISSION_DENIED;
2282
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002283 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002284 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002285 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002286
2287 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002288 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002289 const uint32_t hw_w = hw->getWidth();
2290 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002292 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002293 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002294 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002295 return PERMISSION_DENIED;
2296 }
2297
2298 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002299 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002300 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002301 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302
2303 sw = (!sw) ? hw_w : sw;
2304 sh = (!sh) ? hw_h : sh;
2305 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002306 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002307
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002308// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2309// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002310
Mathias Agopian74c40c02010-09-29 13:02:36 -07002311 // make sure to clear all GL error flags
2312 while ( glGetError() != GL_NO_ERROR ) ;
2313
2314 // create a FBO
2315 GLuint name, tname;
2316 glGenRenderbuffersOES(1, &tname);
2317 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2318 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002319
Mathias Agopian74c40c02010-09-29 13:02:36 -07002320 glGenFramebuffersOES(1, &name);
2321 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2322 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2323 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2324
2325 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002326
Mathias Agopian74c40c02010-09-29 13:02:36 -07002327 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2328
2329 // invert everything, b/c glReadPixel() below will invert the FB
2330 glViewport(0, 0, sw, sh);
2331 glMatrixMode(GL_PROJECTION);
2332 glPushMatrix();
2333 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002334 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 glMatrixMode(GL_MODELVIEW);
2336
2337 // redraw the screen entirely...
2338 glClearColor(0,0,0,1);
2339 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002340
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002341 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002342 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 for (size_t i=0 ; i<count ; ++i) {
2344 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002345 const uint32_t z = layer->drawingState().z;
2346 if (z >= minLayerZ && z <= maxLayerZ) {
2347 if (filtering) layer->setFiltering(true);
2348 layer->draw(hw);
2349 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002350 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002351 }
2352
Mathias Agopian74c40c02010-09-29 13:02:36 -07002353 // check for errors and return screen capture
2354 if (glGetError() != GL_NO_ERROR) {
2355 // error while rendering
2356 result = INVALID_OPERATION;
2357 } else {
2358 // allocate shared memory large enough to hold the
2359 // screen capture
2360 sp<MemoryHeapBase> base(
2361 new MemoryHeapBase(size, 0, "screen-capture") );
2362 void* const ptr = base->getBase();
2363 if (ptr) {
2364 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002365 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002366 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2367 if (glGetError() == GL_NO_ERROR) {
2368 *heap = base;
2369 *w = sw;
2370 *h = sh;
2371 *f = PIXEL_FORMAT_RGBA_8888;
2372 result = NO_ERROR;
2373 }
2374 } else {
2375 result = NO_MEMORY;
2376 }
2377 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002378 glViewport(0, 0, hw_w, hw_h);
2379 glMatrixMode(GL_PROJECTION);
2380 glPopMatrix();
2381 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002382 } else {
2383 result = BAD_VALUE;
2384 }
2385
2386 // release FBO resources
2387 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2388 glDeleteRenderbuffersOES(1, &tname);
2389 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002390
Mathias Agopian42977342012-08-05 00:40:46 -07002391 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002392
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002393// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002394
Mathias Agopian74c40c02010-09-29 13:02:36 -07002395 return result;
2396}
2397
2398
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002399status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002400 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002402 uint32_t sw, uint32_t sh,
2403 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002404{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002405 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002406 return BAD_VALUE;
2407
2408 if (!GLExtensions::getInstance().haveFramebufferObject())
2409 return INVALID_OPERATION;
2410
2411 class MessageCaptureScreen : public MessageBase {
2412 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002413 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002414 sp<IMemoryHeap>* heap;
2415 uint32_t* w;
2416 uint32_t* h;
2417 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 uint32_t sw;
2419 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002420 uint32_t minLayerZ;
2421 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002422 status_t result;
2423 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002424 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002425 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002426 uint32_t sw, uint32_t sh,
2427 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002428 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002429 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2430 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2431 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002432 {
2433 }
2434 status_t getResult() const {
2435 return result;
2436 }
2437 virtual bool handler() {
2438 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002439 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002440 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002441 return true;
2442 }
2443 };
2444
2445 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002446 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447 status_t res = postMessageSync(msg);
2448 if (res == NO_ERROR) {
2449 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2450 }
2451 return res;
2452}
2453
2454// ---------------------------------------------------------------------------
2455
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002456SurfaceFlinger::LayerVector::LayerVector() {
2457}
2458
2459SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2460 : SortedVector<sp<LayerBase> >(rhs) {
2461}
2462
2463int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2464 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002465{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002466 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002467 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2468 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002469
2470 uint32_t ls = l->currentState().layerStack;
2471 uint32_t rs = r->currentState().layerStack;
2472 if (ls != rs)
2473 return ls - rs;
2474
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002475 uint32_t lz = l->currentState().z;
2476 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002477 if (lz != rz)
2478 return lz - rz;
2479
2480 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002481}
2482
2483// ---------------------------------------------------------------------------
2484
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002485SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2486 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002487}
2488
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002489SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2490 : type(type), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002491}
2492
2493// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494
Jamie Gennis9a78c902011-01-12 18:30:40 -08002495GraphicBufferAlloc::GraphicBufferAlloc() {}
2496
2497GraphicBufferAlloc::~GraphicBufferAlloc() {}
2498
2499sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002500 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002501 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2502 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002503 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002504 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002505 if (err == NO_MEMORY) {
2506 GraphicBuffer::dumpAllocationsToSystemLog();
2507 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002508 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002509 "failed (%s), handle=%p",
2510 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002511 return 0;
2512 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002513 return graphicBuffer;
2514}
2515
Jamie Gennis9a78c902011-01-12 18:30:40 -08002516// ---------------------------------------------------------------------------
2517
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518}; // namespace android