blob: ed91d44c4b3b65760779245f365ad5163f40782f [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 Agopianda8d0a52012-09-04 15:05:38 -0700300void SurfaceFlinger::initializeGL(EGLDisplay display, const sp<DisplayDevice>& hw) {
301 EGLBoolean result = DisplayDevice::makeCurrent(display, hw, mEGLContext);
Mathias Agopiana4912602012-07-12 14:25:33 -0700302 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 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
318 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700321 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 glShadeModel(GL_FLAT);
324 glDisable(GL_DITHER);
325 glDisable(GL_CULL_FACE);
326
Mathias Agopiana4912602012-07-12 14:25:33 -0700327 struct pack565 {
328 inline uint16_t operator() (int r, int g, int b) const {
329 return (r<<11)|(g<<5)|b;
330 }
331 } pack565;
332
Jamie Gennis9575f602011-10-07 14:51:16 -0700333 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
334 glGenTextures(1, &mProtectedTexName);
335 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
336 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
337 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
338 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
340 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
341 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343 // print some debugging info
344 EGLint r,g,b,a;
345 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
346 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
347 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
348 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
349 ALOGI("EGL informations:");
350 ALOGI("vendor : %s", extensions.getEglVendor());
351 ALOGI("version : %s", extensions.getEglVersion());
352 ALOGI("extensions: %s", extensions.getEglExtension());
353 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
354 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
355 ALOGI("OpenGL ES informations:");
356 ALOGI("vendor : %s", extensions.getVendor());
357 ALOGI("renderer : %s", extensions.getRenderer());
358 ALOGI("version : %s", extensions.getVersion());
359 ALOGI("extensions: %s", extensions.getExtension());
360 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
361 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
362}
363
Mathias Agopiana4912602012-07-12 14:25:33 -0700364status_t SurfaceFlinger::readyToRun()
365{
366 ALOGI( "SurfaceFlinger's main thread ready to run. "
367 "Initializing graphics H/W...");
368
Mathias Agopiana4912602012-07-12 14:25:33 -0700369 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700370 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
371 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700372
373 // Initialize the main display
374 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700375 sp<FramebufferSurface> fbs = FramebufferSurface::create();
376 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700377 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
378 exit(0);
379 }
380
Mathias Agopiane57f2922012-08-09 16:29:12 -0700381 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
382 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700383
Mathias Agopiana4912602012-07-12 14:25:33 -0700384 // initialize the config and context
385 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700386 ANativeWindow* const anw = stc.get();
387 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700388 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
389 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700390
391 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700392
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700393 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700394 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700395 mCurrentState.displays.add(mDefaultDisplays[i],
396 DisplayDeviceState((DisplayDevice::DisplayType)i));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700397 }
398 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700399 DisplayDevice::DISPLAY_PRIMARY,
400 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiane60b0682012-08-21 23:34:09 -0700401 anw, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700402 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700403
404 // initialize OpenGL ES
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700405 initializeGL(mEGLDisplay, hw);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800406
Mathias Agopian028508c2012-07-25 21:12:12 -0700407 // start the EventThread
408 mEventThread = new EventThread(this);
409 mEventQueue.setEventThread(mEventThread);
410
Mathias Agopian86303202012-07-24 22:46:10 -0700411 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700412 mHwc = new HWComposer(this,
413 *static_cast<HWComposer::EventHandler *>(this),
414 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700415
416 // initialize our drawing state
417 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700418
Mathias Agopiana4912602012-07-12 14:25:33 -0700419 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800420 mReadyToRunBarrier.open();
421
Andy McFadden13a082e2012-08-24 10:16:42 -0700422 // set initial conditions (e.g. unblank default device)
423 initializeDisplays();
424
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700425 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700426 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 return NO_ERROR;
429}
430
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700431int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
432 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
433 type : mHwc->allocateDisplayId();
434}
435
Mathias Agopiana67e4182012-06-19 17:26:12 -0700436void SurfaceFlinger::startBootAnim() {
437 // start boot animation
438 property_set("service.bootanim.exit", "0");
439 property_set("ctl.start", "bootanim");
440}
441
Mathias Agopiana4912602012-07-12 14:25:33 -0700442uint32_t SurfaceFlinger::getMaxTextureSize() const {
443 return mMaxTextureSize;
444}
445
446uint32_t SurfaceFlinger::getMaxViewportDims() const {
447 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
448 mMaxViewportDims[0] : mMaxViewportDims[1];
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800452
Jamie Gennis582270d2011-08-17 18:19:00 -0700453bool SurfaceFlinger::authenticateSurfaceTexture(
454 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800455 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700456 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800457
458 // Check the visible layer list for the ISurface
459 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
460 size_t count = currentLayers.size();
461 for (size_t i=0 ; i<count ; i++) {
462 const sp<LayerBase>& layer(currentLayers[i]);
463 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700464 if (lbc != NULL) {
465 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
466 if (lbcBinder == surfaceTextureBinder) {
467 return true;
468 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800469 }
470 }
471
472 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700473 // SurfaceTexture gets destroyed before all the clients are done using it,
474 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800475 // will instead fail later on when the client tries to use the surface,
476 // which should be reported as "surface XYZ returned an -ENODEV". The
477 // purgatorized layers are no less authentic than the visible ones, so this
478 // should not cause any harm.
479 size_t purgatorySize = mLayerPurgatory.size();
480 for (size_t i=0 ; i<purgatorySize ; i++) {
481 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
482 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700483 if (lbc != NULL) {
484 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
485 if (lbcBinder == surfaceTextureBinder) {
486 return true;
487 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800488 }
489 }
490
491 return false;
492}
493
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700494status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
495 // TODO: this is mostly here only for compatibility
496 // the display size is needed but the display metrics should come from elsewhere
497 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
498 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700499 return BAD_INDEX;
500 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700501
502 const HWComposer& hwc(getHwComposer());
503 float xdpi = hwc.getDpiX();
504 float ydpi = hwc.getDpiY();
505
506 // TODO: Not sure if display density should handled by SF any longer
507 class Density {
508 static int getDensityFromProperty(char const* propName) {
509 char property[PROPERTY_VALUE_MAX];
510 int density = 0;
511 if (property_get(propName, property, NULL) > 0) {
512 density = atoi(property);
513 }
514 return density;
515 }
516 public:
517 static int getEmuDensity() {
518 return getDensityFromProperty("qemu.sf.lcd_density"); }
519 static int getBuildDensity() {
520 return getDensityFromProperty("ro.sf.lcd_density"); }
521 };
522 // The density of the device is provided by a build property
523 float density = Density::getBuildDensity() / 160.0f;
524 if (density == 0) {
525 // the build doesn't provide a density -- this is wrong!
526 // use xdpi instead
527 ALOGE("ro.sf.lcd_density must be defined as a build property");
528 density = xdpi / 160.0f;
529 }
530 if (Density::getEmuDensity()) {
531 // if "qemu.sf.lcd_density" is specified, it overrides everything
532 xdpi = ydpi = density = Density::getEmuDensity();
533 density /= 160.0f;
534 }
535
Mathias Agopian42977342012-08-05 00:40:46 -0700536 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
537 info->w = hw->getWidth();
538 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700539 info->xdpi = xdpi;
540 info->ydpi = ydpi;
541 info->fps = float(1e9 / hwc.getRefreshPeriod());
542 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700543 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700544 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700545 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700546 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700547}
548
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800549// ----------------------------------------------------------------------------
550
551sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800552 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700553}
554
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700555void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700556
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700557 sp<IBinder> token;
558 { // scope for the lock
559 Mutex::Autolock _l(mStateLock);
560 token = mExtDisplayToken;
561 }
562
563 if (token == 0) {
564 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700565 }
566
567 { // scope for the lock
568 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700569 if (surface == 0) {
570 // release our current display. we're guarantee to have
571 // a reference to it (token), while we hold the lock
572 mExtDisplayToken = 0;
573 } else {
574 mExtDisplayToken = token;
575 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700576
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700577 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
578 info.surface = surface;
579 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700580 }
581}
582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800584
585void SurfaceFlinger::waitForEvent() {
586 mEventQueue.waitMessage();
587}
588
589void SurfaceFlinger::signalTransaction() {
590 mEventQueue.invalidate();
591}
592
593void SurfaceFlinger::signalLayerUpdate() {
594 mEventQueue.invalidate();
595}
596
597void SurfaceFlinger::signalRefresh() {
598 mEventQueue.refresh();
599}
600
601status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
602 nsecs_t reltime, uint32_t flags) {
603 return mEventQueue.postMessage(msg, reltime);
604}
605
606status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
607 nsecs_t reltime, uint32_t flags) {
608 status_t res = mEventQueue.postMessage(msg, reltime);
609 if (res == NO_ERROR) {
610 msg->wait();
611 }
612 return res;
613}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614
Mathias Agopian4fec8732012-06-29 14:12:52 -0700615bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 return true;
618}
619
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700620void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
621 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
622 // we should only receive DisplayDevice::DisplayType from the vsync callback
623 const wp<IBinder>& token(mDefaultDisplays[type]);
624 mEventThread->onVSyncReceived(token, timestamp);
625 }
Mathias Agopian86303202012-07-24 22:46:10 -0700626}
627
628void SurfaceFlinger::eventControl(int event, int enabled) {
629 getHwComposer().eventControl(event, enabled);
630}
631
Mathias Agopian4fec8732012-06-29 14:12:52 -0700632void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800633 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800634 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700635 case MessageQueue::INVALIDATE:
636 handleMessageTransaction();
637 handleMessageInvalidate();
638 signalRefresh();
639 break;
640 case MessageQueue::REFRESH:
641 handleMessageRefresh();
642 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800643 }
644}
645
Mathias Agopian4fec8732012-06-29 14:12:52 -0700646void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700647 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700648 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700649 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700650 }
651}
652
653void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700654 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700655 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700656}
657
658void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700659 ATRACE_CALL();
660 preComposition();
661 rebuildLayerStacks();
662 setUpHWComposer();
663 doDebugFlashRegions();
664 doComposition();
665 postComposition();
666}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700667
Mathias Agopiancd60f992012-08-16 16:28:27 -0700668void SurfaceFlinger::doDebugFlashRegions()
669{
670 // is debugging enabled
671 if (CC_LIKELY(!mDebugRegion))
672 return;
673
674 const bool repaintEverything = mRepaintEverything;
675 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
676 const sp<DisplayDevice>& hw(mDisplays[dpy]);
677 if (hw->canDraw()) {
678 // transform the dirty region into this screen's coordinate space
679 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
680 if (!dirtyRegion.isEmpty()) {
681 // redraw the whole screen
682 doComposeSurfaces(hw, Region(hw->bounds()));
683
684 // and draw the dirty region
685 glDisable(GL_TEXTURE_EXTERNAL_OES);
686 glDisable(GL_TEXTURE_2D);
687 glDisable(GL_BLEND);
688 glColor4f(1, 0, 1, 1);
689 const int32_t height = hw->getHeight();
690 Region::const_iterator it = dirtyRegion.begin();
691 Region::const_iterator const end = dirtyRegion.end();
692 while (it != end) {
693 const Rect& r = *it++;
694 GLfloat vertices[][2] = {
695 { r.left, height - r.top },
696 { r.left, height - r.bottom },
697 { r.right, height - r.bottom },
698 { r.right, height - r.top }
699 };
700 glVertexPointer(2, GL_FLOAT, 0, vertices);
701 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
702 }
703 hw->compositionComplete();
704 // FIXME
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700705 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700706 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
707 }
708 }
709 }
710 }
711
712 postFramebuffer();
713
714 if (mDebugRegion > 1) {
715 usleep(mDebugRegion * 1000);
716 }
717}
718
719void SurfaceFlinger::preComposition()
720{
721 bool needExtraInvalidate = false;
722 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
723 const size_t count = currentLayers.size();
724 for (size_t i=0 ; i<count ; i++) {
725 if (currentLayers[i]->onPreComposition()) {
726 needExtraInvalidate = true;
727 }
728 }
729 if (needExtraInvalidate) {
730 signalLayerUpdate();
731 }
732}
733
734void SurfaceFlinger::postComposition()
735{
736 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
737 const size_t count = currentLayers.size();
738 for (size_t i=0 ; i<count ; i++) {
739 currentLayers[i]->onPostComposition();
740 }
741}
742
743void SurfaceFlinger::rebuildLayerStacks() {
744 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700745 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700746 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700747 mVisibleRegionsDirty = false;
748 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700749 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700750 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700751 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700752 const Transform& tr(hw->getTransform());
753 const Rect bounds(hw->getBounds());
754
Mathias Agopian87baae12012-07-31 12:38:26 -0700755 Region opaqueRegion;
756 Region dirtyRegion;
757 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700758 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700759
760 Vector< sp<LayerBase> > layersSortedByZ;
761 const size_t count = currentLayers.size();
762 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700763 const sp<LayerBase>& layer(currentLayers[i]);
764 const Layer::State& s(layer->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700765 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700766 Region visibleRegion(tr.transform(layer->visibleRegion));
767 visibleRegion.andSelf(bounds);
768 if (!visibleRegion.isEmpty()) {
769 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -0700770 }
771 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700772 }
Mathias Agopian42977342012-08-05 00:40:46 -0700773 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700774 hw->undefinedRegion.set(bounds);
775 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
776 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700777 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700778 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700779}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700780
Mathias Agopiancd60f992012-08-16 16:28:27 -0700781void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700782 HWComposer& hwc(getHwComposer());
783 if (hwc.initCheck() == NO_ERROR) {
784 // build the h/w work list
785 const bool workListsDirty = mHwWorkListDirty;
786 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700787 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700788 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700789 const int32_t id = hw->getHwcDisplayId();
790 if (id >= 0) {
791 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700793 const size_t count = currentLayers.size();
794 if (hwc.createWorkList(id, count) >= 0) {
795 HWComposer::LayerListIterator cur = hwc.begin(id);
796 const HWComposer::LayerListIterator end = hwc.end(id);
797 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
798 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700799
Mathias Agopiane60b0682012-08-21 23:34:09 -0700800 if (CC_UNLIKELY(workListsDirty)) {
801 layer->setGeometry(hw, *cur);
802 if (mDebugDisableHWC || mDebugRegion) {
803 cur->setSkip(true);
804 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700805 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700806
Mathias Agopiane60b0682012-08-21 23:34:09 -0700807 /*
808 * update the per-frame h/w composer data for each layer
809 * and build the transparent region of the FB
810 */
811 layer->setPerFrameData(hw, *cur);
812 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700813 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700814 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700815 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700816 status_t err = hwc.prepare();
817 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
818 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700819}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700820
Mathias Agopiancd60f992012-08-16 16:28:27 -0700821void SurfaceFlinger::doComposition() {
822 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700823 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700824 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700825 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700826 if (hw->canDraw()) {
827 // transform the dirty region into this screen's coordinate space
828 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
829 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700830 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700831 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700833 hw->dirtyRegion.clear();
834 hw->flip(hw->swapRegion);
835 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700836 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700837 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700838 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700839 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700840 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700841}
842
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843void SurfaceFlinger::postFramebuffer()
844{
Mathias Agopian841cde52012-03-01 15:44:37 -0800845 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800846
Mathias Agopiana44b0412011-10-16 18:46:35 -0700847 const nsecs_t now = systemTime();
848 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700849
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700850 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700851 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700852 // FIXME: EGL spec says:
853 // "surface must be bound to the calling thread's current context,
854 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700855 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700856 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700857 }
858
Mathias Agopian92a979a2012-08-02 18:32:23 -0700859 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700860 sp<const DisplayDevice> hw(mDisplays[dpy]);
861 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700862 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700863 int32_t id = hw->getHwcDisplayId();
864 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700865 HWComposer::LayerListIterator cur = hwc.begin(id);
866 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700867 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700868 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700869 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700870 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700871 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700872 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700873 }
Jesse Hallef194142012-06-14 14:45:17 -0700874 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800875 }
876
Mathias Agopiana44b0412011-10-16 18:46:35 -0700877 mLastSwapBufferTime = systemTime() - now;
878 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879}
880
Mathias Agopian87baae12012-07-31 12:38:26 -0700881void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882{
Mathias Agopian841cde52012-03-01 15:44:37 -0800883 ATRACE_CALL();
884
Mathias Agopianca4d3602011-05-19 15:38:14 -0700885 Mutex::Autolock _l(mStateLock);
886 const nsecs_t now = systemTime();
887 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888
Mathias Agopianca4d3602011-05-19 15:38:14 -0700889 // Here we're guaranteed that some transaction flags are set
890 // so we can call handleTransactionLocked() unconditionally.
891 // We call getTransactionFlags(), which will also clear the flags,
892 // with mStateLock held to guarantee that mCurrentState won't change
893 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700894
Mathias Agopiane57f2922012-08-09 16:29:12 -0700895 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700896 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700897
Mathias Agopianca4d3602011-05-19 15:38:14 -0700898 mLastTransactionTime = systemTime() - now;
899 mDebugInTransaction = 0;
900 invalidateHwcGeometry();
901 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700902}
903
Mathias Agopian87baae12012-07-31 12:38:26 -0700904void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700905{
906 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 const size_t count = currentLayers.size();
908
909 /*
910 * Traversal of the children
911 * (perform the transaction for each of them if needed)
912 */
913
Mathias Agopian3559b072012-08-15 13:46:03 -0700914 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700916 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
918 if (!trFlags) continue;
919
920 const uint32_t flags = layer->doTransaction(0);
921 if (flags & Layer::eVisibleRegion)
922 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 }
924 }
925
926 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700927 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 */
929
Mathias Agopiane57f2922012-08-09 16:29:12 -0700930 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700931 // here we take advantage of Vector's copy-on-write semantics to
932 // improve performance by skipping the transaction entirely when
933 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700934 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
935 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700936 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700938 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700939 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700940
941 // find the displays that were removed
942 // (ie: in drawing state but not in current state)
943 // also handle displays that changed
944 // (ie: displays that are in both lists)
945 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700946 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
947 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700949 if (!draw[i].isMainDisplay()) {
950 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700951 } else {
952 ALOGW("trying to remove the main display");
953 }
954 } else {
955 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700956 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700957 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700958 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700959 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700960 // recreating the DisplayDevice, so we just remove it
961 // from the drawing state, so that it get re-added
962 // below.
963 mDisplays.removeItem(display);
964 mDrawingState.displays.removeItemsAt(i);
965 dc--; i--;
966 // at this point we must loop to the next item
967 continue;
968 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700969
Mathias Agopian93997a82012-08-29 17:30:36 -0700970 const sp<DisplayDevice>& disp(getDisplayDevice(display));
971 if (disp != NULL) {
972 if (state.layerStack != draw[i].layerStack) {
973 disp->setLayerStack(state.layerStack);
974 }
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700975 if (state.orientation != draw[i].orientation) {
Mathias Agopian93997a82012-08-29 17:30:36 -0700976 disp->setOrientation(state.orientation);
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700977 }
978 if (state.viewport != draw[i].viewport) {
979 disp->setViewport(state.viewport);
980 }
981 if (state.frame != draw[i].frame) {
982 disp->setFrame(state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -0700983 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700984 }
985 }
986 }
987
988 // find displays that were added
989 // (ie: in current state but not in drawing state)
990 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700991 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700992 const DisplayDeviceState& state(curr[i]);
Mathias Agopian93997a82012-08-29 17:30:36 -0700993 if (state.surface != NULL) {
994 sp<SurfaceTextureClient> stc(
995 new SurfaceTextureClient(state.surface));
996 const wp<IBinder>& display(curr.keyAt(i));
997 sp<DisplayDevice> disp = new DisplayDevice(this,
998 state.type, display, stc, 0, mEGLConfig);
999 disp->setLayerStack(state.layerStack);
1000 disp->setOrientation(state.orientation);
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001001 disp->setViewport(state.viewport);
1002 disp->setFrame(state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001003 mDisplays.add(display, disp);
1004 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001005 }
1006 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001008 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009
Mathias Agopian3559b072012-08-15 13:46:03 -07001010 /*
1011 * Perform our own transaction if needed
1012 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001013
Mathias Agopiancd60f992012-08-16 16:28:27 -07001014 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1015 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001016 // layers have been added
1017 mVisibleRegionsDirty = true;
1018 }
1019
1020 // some layers might have been removed, so
1021 // we need to update the regions they're exposing.
1022 if (mLayersRemoved) {
1023 mLayersRemoved = false;
1024 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001025 const size_t count = previousLayers.size();
1026 for (size_t i=0 ; i<count ; i++) {
1027 const sp<LayerBase>& layer(previousLayers[i]);
1028 if (currentLayers.indexOf(layer) < 0) {
1029 // this layer is not visible anymore
1030 // TODO: we could traverse the tree from front to back and
1031 // compute the actual visible region
1032 // TODO: we could cache the transformed region
1033 Layer::State front(layer->drawingState());
1034 Region visibleReg = front.transform.transform(
1035 Region(Rect(front.active.w, front.active.h)));
1036 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001037 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 }
1040
1041 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001042}
1043
1044void SurfaceFlinger::commitTransaction()
1045{
1046 if (!mLayersPendingRemoval.isEmpty()) {
1047 // Notify removed layers now that they can't be drawn from
1048 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1049 mLayersPendingRemoval[i]->onRemoved();
1050 }
1051 mLayersPendingRemoval.clear();
1052 }
1053
1054 mDrawingState = mCurrentState;
1055 mTransationPending = false;
1056 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057}
1058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001060 const LayerVector& currentLayers, uint32_t layerStack,
1061 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062{
Mathias Agopian841cde52012-03-01 15:44:37 -08001063 ATRACE_CALL();
1064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 Region aboveOpaqueLayers;
1066 Region aboveCoveredLayers;
1067 Region dirty;
1068
Mathias Agopian87baae12012-07-31 12:38:26 -07001069 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070
1071 size_t i = currentLayers.size();
1072 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001073 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074
1075 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001076 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077
Mathias Agopian87baae12012-07-31 12:38:26 -07001078 // only consider the layers on the given later stack
1079 if (s.layerStack != layerStack)
1080 continue;
1081
Mathias Agopianab028732010-03-16 16:41:46 -07001082 /*
1083 * opaqueRegion: area of a surface that is fully opaque.
1084 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001086
1087 /*
1088 * visibleRegion: area of a surface that is visible on screen
1089 * and not fully transparent. This is essentially the layer's
1090 * footprint minus the opaque regions above it.
1091 * Areas covered by a translucent surface are considered visible.
1092 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001094
1095 /*
1096 * coveredRegion: area of a surface that is covered by all
1097 * visible regions above it (which includes the translucent areas).
1098 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001100
1101
1102 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001103 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001104 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001105 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001107 if (!visibleRegion.isEmpty()) {
1108 // Remove the transparent area from the visible region
1109 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001110 Region transparentRegionScreen;
1111 const Transform tr(s.transform);
1112 if (tr.transformed()) {
1113 if (tr.preserveRects()) {
1114 // transform the transparent region
1115 transparentRegionScreen = tr.transform(s.transparentRegion);
1116 } else {
1117 // transformation too complex, can't do the
1118 // transparent region optimization.
1119 transparentRegionScreen.clear();
1120 }
1121 } else {
1122 transparentRegionScreen = s.transparentRegion;
1123 }
1124 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001125 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126
Mathias Agopianab028732010-03-16 16:41:46 -07001127 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001128 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001129 if (s.alpha==255 && !translucent &&
1130 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1131 // the opaque region is the layer's footprint
1132 opaqueRegion = visibleRegion;
1133 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 }
1135 }
1136
Mathias Agopianab028732010-03-16 16:41:46 -07001137 // Clip the covered region to the visible region
1138 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1139
1140 // Update aboveCoveredLayers for next (lower) layer
1141 aboveCoveredLayers.orSelf(visibleRegion);
1142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 // subtract the opaque region covered by the layers above us
1144 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145
1146 // compute this layer's dirty region
1147 if (layer->contentDirty) {
1148 // we need to invalidate the whole region
1149 dirty = visibleRegion;
1150 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001151 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 layer->contentDirty = false;
1153 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001154 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001155 * the exposed region consists of two components:
1156 * 1) what's VISIBLE now and was COVERED before
1157 * 2) what's EXPOSED now less what was EXPOSED before
1158 *
1159 * note that (1) is conservative, we start with the whole
1160 * visible region but only keep what used to be covered by
1161 * something -- which mean it may have been exposed.
1162 *
1163 * (2) handles areas that were not covered by anything but got
1164 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001165 */
Mathias Agopianab028732010-03-16 16:41:46 -07001166 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001167 const Region oldVisibleRegion = layer->visibleRegion;
1168 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001169 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1170 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 }
1172 dirty.subtractSelf(aboveOpaqueLayers);
1173
1174 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001175 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176
Mathias Agopianab028732010-03-16 16:41:46 -07001177 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 // Store the visible region is screen space
1181 layer->setVisibleRegion(visibleRegion);
1182 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 }
1184
Mathias Agopian87baae12012-07-31 12:38:26 -07001185 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186}
1187
Mathias Agopian87baae12012-07-31 12:38:26 -07001188void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1189 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001190 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001191 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1192 if (hw->getLayerStack() == layerStack) {
1193 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001194 }
1195 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001196}
1197
1198void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001200 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201
Mathias Agopian4fec8732012-06-29 14:12:52 -07001202 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001203 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001204 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001205 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001206 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001207 const Region dirty(layer->latchBuffer(visibleRegions));
1208 Layer::State s(layer->drawingState());
1209 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001210 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001211
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001212 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213}
1214
Mathias Agopianad456f92011-01-13 17:53:01 -08001215void SurfaceFlinger::invalidateHwcGeometry()
1216{
1217 mHwWorkListDirty = true;
1218}
1219
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001220
Mathias Agopiancd60f992012-08-16 16:28:27 -07001221void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001222 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223{
Mathias Agopian87baae12012-07-31 12:38:26 -07001224 Region dirtyRegion(inDirtyRegion);
1225
Mathias Agopianb8a55602009-06-26 19:06:36 -07001226 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001227 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228
Mathias Agopian42977342012-08-05 00:40:46 -07001229 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001230 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001231 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1232 // takes a rectangle, we must make sure to update that whole
1233 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001234 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001235 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001236 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001237 // We need to redraw the rectangle that will be updated
1238 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001239 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001240 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001241 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001242 } else {
1243 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001244 dirtyRegion.set(hw->bounds());
1245 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247 }
1248
Mathias Agopiancd60f992012-08-16 16:28:27 -07001249 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250
Mathias Agopiancd60f992012-08-16 16:28:27 -07001251 // FIXME: we need to call eglSwapBuffers() on displays that have
1252 // GL composition and only on those.
1253 // however, currently hwc.commit() already does that for the main
Jesse Hall9ca48912012-08-30 13:42:23 -07001254 // display (if there is a hwc) and never for the other ones
1255 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL ||
1256 getHwComposer().initCheck() != NO_ERROR) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001257 // FIXME: EGL spec says:
1258 // "surface must be bound to the calling thread's current context,
1259 // for the current rendering API."
1260 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001261 }
1262
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001263 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001264 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265}
1266
Mathias Agopiancd60f992012-08-16 16:28:27 -07001267void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001268{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001269 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001270 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001271 HWComposer::LayerListIterator cur = hwc.begin(id);
1272 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001273
Mathias Agopian85d751c2012-08-29 16:59:24 -07001274 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1275 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001276 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001277
1278 // set the frame buffer
1279 glMatrixMode(GL_MODELVIEW);
1280 glLoadIdentity();
1281
1282 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001283 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001284 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001285 // when using overlays, we assume a fully transparent framebuffer
1286 // NOTE: we could reduce how much we need to clear, for instance
1287 // remove where there are opaque FB layers. however, on some
1288 // GPUs doing a "clean slate" glClear might be more efficient.
1289 // We'll revisit later if needed.
1290 glClearColor(0, 0, 0, 0);
1291 glClear(GL_COLOR_BUFFER_BIT);
1292 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001293 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001294 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001295 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001296 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001297 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001298 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001299 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001300 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001301
Mathias Agopian85d751c2012-08-29 16:59:24 -07001302 /*
1303 * and then, render the layers targeted at the framebuffer
1304 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001305
Mathias Agopian85d751c2012-08-29 16:59:24 -07001306 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1307 const size_t count = layers.size();
1308 const Transform& tr = hw->getTransform();
1309 if (cur != end) {
1310 // we're using h/w composer
1311 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001312 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001313 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001314 if (!clip.isEmpty()) {
1315 switch (cur->getCompositionType()) {
1316 case HWC_OVERLAY: {
1317 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1318 && i
1319 && layer->isOpaque()
1320 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001321 // never clear the very first layer since we're
1322 // guaranteed the FB is already cleared
1323 layer->clearWithOpenGL(hw, clip);
1324 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001325 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001326 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001327 case HWC_FRAMEBUFFER: {
1328 layer->draw(hw, clip);
1329 break;
1330 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001331 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001332 }
1333 layer->setAcquireFence(hw, *cur);
1334 }
1335 } else {
1336 // we're not using h/w composer
1337 for (size_t i=0 ; i<count ; ++i) {
1338 const sp<LayerBase>& layer(layers[i]);
1339 const Region clip(dirty.intersect(
1340 tr.transform(layer->visibleRegion)));
1341 if (!clip.isEmpty()) {
1342 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001343 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001344 }
1345 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346}
1347
Mathias Agopian55801e42012-08-27 18:54:24 -07001348void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1349 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001351 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001352 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001353 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001354 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001355
Mathias Agopian55801e42012-08-27 18:54:24 -07001356 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001357 Region::const_iterator it = region.begin();
1358 Region::const_iterator const end = region.end();
1359 while (it != end) {
1360 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001361 GLfloat vertices[][2] = {
1362 { r.left, height - r.top },
1363 { r.left, height - r.bottom },
1364 { r.right, height - r.bottom },
1365 { r.right, height - r.top }
1366 };
1367 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001368 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1369 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370}
1371
Mathias Agopian96f08192010-06-02 23:28:45 -07001372ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1373 const sp<LayerBaseClient>& lbc)
1374{
Mathias Agopian96f08192010-06-02 23:28:45 -07001375 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001376 size_t name = client->attachLayer(lbc);
1377
Mathias Agopian96f08192010-06-02 23:28:45 -07001378 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001379 Mutex::Autolock _l(mStateLock);
1380 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001381
Mathias Agopian4f113742011-05-03 16:21:41 -07001382 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001383}
1384
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001385status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386{
1387 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001388 status_t err = purgatorizeLayer_l(layer);
1389 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001390 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001391 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392}
1393
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001394status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395{
1396 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1397 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001398 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 return NO_ERROR;
1400 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001401 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402}
1403
Mathias Agopian9a112062009-04-17 19:36:26 -07001404status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1405{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001406 // First add the layer to the purgatory list, which makes sure it won't
1407 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001408 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001409 if (err >= 0) {
1410 mLayerPurgatory.add(layerBase);
1411 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001412
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001413 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001414
Mathias Agopian3d579642009-06-04 18:46:21 -07001415 // it's possible that we don't find a layer, because it might
1416 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001417 // from the user because there is a race between Client::destroySurface(),
1418 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001419 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1420}
1421
Mathias Agopiandea20b12011-05-03 17:04:02 -07001422uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1423{
1424 return android_atomic_release_load(&mTransactionFlags);
1425}
1426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1428{
1429 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1430}
1431
Mathias Agopianbb641242010-05-18 17:06:55 -07001432uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433{
1434 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1435 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001436 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 }
1438 return old;
1439}
1440
Mathias Agopian8b33f032012-07-24 20:43:54 -07001441void SurfaceFlinger::setTransactionState(
1442 const Vector<ComposerState>& state,
1443 const Vector<DisplayState>& displays,
1444 uint32_t flags)
1445{
Mathias Agopian698c0872011-06-28 19:09:31 -07001446 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001447 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001448
1449 size_t count = displays.size();
1450 for (size_t i=0 ; i<count ; i++) {
1451 const DisplayState& s(displays[i]);
1452 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001453 }
1454
Mathias Agopiane57f2922012-08-09 16:29:12 -07001455 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001456 for (size_t i=0 ; i<count ; i++) {
1457 const ComposerState& s(state[i]);
1458 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001459 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001460 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001461
Mathias Agopian386aa982011-11-07 21:58:03 -08001462 if (transactionFlags) {
1463 // this triggers the transaction
1464 setTransactionFlags(transactionFlags);
1465
1466 // if this is a synchronous transaction, wait for it to take effect
1467 // before returning.
1468 if (flags & eSynchronous) {
1469 mTransationPending = true;
1470 }
1471 while (mTransationPending) {
1472 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1473 if (CC_UNLIKELY(err != NO_ERROR)) {
1474 // just in case something goes wrong in SF, return to the
1475 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001476 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001477 mTransationPending = false;
1478 break;
1479 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 }
1482}
1483
Mathias Agopiane57f2922012-08-09 16:29:12 -07001484uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1485{
1486 uint32_t flags = 0;
1487 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001488 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001489 const uint32_t what = s.what;
1490 if (what & DisplayState::eSurfaceChanged) {
1491 if (disp.surface->asBinder() != s.surface->asBinder()) {
1492 disp.surface = s.surface;
1493 flags |= eDisplayTransactionNeeded;
1494 }
1495 }
1496 if (what & DisplayState::eLayerStackChanged) {
1497 if (disp.layerStack != s.layerStack) {
1498 disp.layerStack = s.layerStack;
1499 flags |= eDisplayTransactionNeeded;
1500 }
1501 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001502 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001503 if (disp.orientation != s.orientation) {
1504 disp.orientation = s.orientation;
1505 flags |= eDisplayTransactionNeeded;
1506 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001507 }
1508 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001509 if (disp.frame != s.frame) {
1510 disp.frame = s.frame;
1511 flags |= eDisplayTransactionNeeded;
1512 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001513 }
1514 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001515 if (disp.viewport != s.viewport) {
1516 disp.viewport = s.viewport;
1517 flags |= eDisplayTransactionNeeded;
1518 }
1519 }
1520 }
1521 return flags;
1522}
1523
1524uint32_t SurfaceFlinger::setClientStateLocked(
1525 const sp<Client>& client,
1526 const layer_state_t& s)
1527{
1528 uint32_t flags = 0;
1529 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1530 if (layer != 0) {
1531 const uint32_t what = s.what;
1532 if (what & layer_state_t::ePositionChanged) {
1533 if (layer->setPosition(s.x, s.y))
1534 flags |= eTraversalNeeded;
1535 }
1536 if (what & layer_state_t::eLayerChanged) {
1537 // NOTE: index needs to be calculated before we update the state
1538 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1539 if (layer->setLayer(s.z)) {
1540 mCurrentState.layersSortedByZ.removeAt(idx);
1541 mCurrentState.layersSortedByZ.add(layer);
1542 // we need traversal (state changed)
1543 // AND transaction (list changed)
1544 flags |= eTransactionNeeded|eTraversalNeeded;
1545 }
1546 }
1547 if (what & layer_state_t::eSizeChanged) {
1548 if (layer->setSize(s.w, s.h)) {
1549 flags |= eTraversalNeeded;
1550 }
1551 }
1552 if (what & layer_state_t::eAlphaChanged) {
1553 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1554 flags |= eTraversalNeeded;
1555 }
1556 if (what & layer_state_t::eMatrixChanged) {
1557 if (layer->setMatrix(s.matrix))
1558 flags |= eTraversalNeeded;
1559 }
1560 if (what & layer_state_t::eTransparentRegionChanged) {
1561 if (layer->setTransparentRegionHint(s.transparentRegion))
1562 flags |= eTraversalNeeded;
1563 }
1564 if (what & layer_state_t::eVisibilityChanged) {
1565 if (layer->setFlags(s.flags, s.mask))
1566 flags |= eTraversalNeeded;
1567 }
1568 if (what & layer_state_t::eCropChanged) {
1569 if (layer->setCrop(s.crop))
1570 flags |= eTraversalNeeded;
1571 }
1572 if (what & layer_state_t::eLayerStackChanged) {
1573 // NOTE: index needs to be calculated before we update the state
1574 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1575 if (layer->setLayerStack(s.layerStack)) {
1576 mCurrentState.layersSortedByZ.removeAt(idx);
1577 mCurrentState.layersSortedByZ.add(layer);
1578 // we need traversal (state changed)
1579 // AND transaction (list changed)
1580 flags |= eTransactionNeeded|eTraversalNeeded;
1581 }
1582 }
1583 }
1584 return flags;
1585}
1586
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001587sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001588 ISurfaceComposerClient::surface_data_t* params,
1589 const String8& name,
1590 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001591 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 uint32_t flags)
1593{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001594 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001595 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001596
1597 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001598 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001599 int(w), int(h));
1600 return surfaceHandle;
1601 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001602
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001603 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001604 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1605 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001606 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001608 case ISurfaceComposerClient::eFXSurfaceBlur:
1609 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001610 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001612 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001613 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001614 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615 }
1616
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001617 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001618 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001619 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001620 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001622 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001623 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001624 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001625 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627 }
1628
1629 return surfaceHandle;
1630}
1631
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001632sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001633 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001634 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001635 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636{
1637 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001638 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 case PIXEL_FORMAT_TRANSPARENT:
1640 case PIXEL_FORMAT_TRANSLUCENT:
1641 format = PIXEL_FORMAT_RGBA_8888;
1642 break;
1643 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001644#ifdef NO_RGBX_8888
1645 format = PIXEL_FORMAT_RGB_565;
1646#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001647 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001648#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 break;
1650 }
1651
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001652#ifdef NO_RGBX_8888
1653 if (format == PIXEL_FORMAT_RGBX_8888)
1654 format = PIXEL_FORMAT_RGBA_8888;
1655#endif
1656
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001657 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001658 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001659 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001660 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001661 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 }
1663 return layer;
1664}
1665
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001666sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001667 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001668 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001670 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001671 return layer;
1672}
1673
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001674sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001675 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001676 uint32_t w, uint32_t h, uint32_t flags)
1677{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001678 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 return layer;
1680}
1681
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001682status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683{
Mathias Agopian9a112062009-04-17 19:36:26 -07001684 /*
1685 * called by the window manager, when a surface should be marked for
1686 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001687 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001688 * The surface is removed from the current and drawing lists, but placed
1689 * in the purgatory queue, so it's not destroyed right-away (we need
1690 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001691 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001692
Mathias Agopian48d819a2009-09-10 19:41:18 -07001693 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001694 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001695 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001696
Mathias Agopian48d819a2009-09-10 19:41:18 -07001697 if (layer != 0) {
1698 err = purgatorizeLayer_l(layer);
1699 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001700 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001701 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001702 }
1703 return err;
1704}
1705
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001706status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001707{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001708 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001709 status_t err = NO_ERROR;
1710 sp<LayerBaseClient> l(layer.promote());
1711 if (l != NULL) {
1712 Mutex::Autolock _l(mStateLock);
1713 err = removeLayer_l(l);
1714 if (err == NAME_NOT_FOUND) {
1715 // The surface wasn't in the current list, which means it was
1716 // removed already, which means it is in the purgatory,
1717 // and need to be removed from there.
1718 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001719 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001720 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001721 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001722 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001723 "error removing layer=%p (%s)", l.get(), strerror(-err));
1724 }
1725 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726}
1727
Mathias Agopianb60314a2012-04-10 22:09:54 -07001728// ---------------------------------------------------------------------------
1729
Andy McFadden13a082e2012-08-24 10:16:42 -07001730void SurfaceFlinger::onInitializeDisplays() {
1731 // reset screen orientation
1732 Vector<ComposerState> state;
1733 Vector<DisplayState> displays;
1734 DisplayState d;
1735 d.what = DisplayState::eOrientationChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001736 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001737 d.orientation = DisplayState::eOrientationDefault;
1738 displays.add(d);
1739 setTransactionState(state, displays, 0);
1740
1741 // XXX: this should init default device to "unblank" and all other devices to "blank"
1742 onScreenAcquired();
1743}
1744
1745void SurfaceFlinger::initializeDisplays() {
1746 class MessageScreenInitialized : public MessageBase {
1747 SurfaceFlinger* flinger;
1748 public:
1749 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1750 virtual bool handler() {
1751 flinger->onInitializeDisplays();
1752 return true;
1753 }
1754 };
1755 sp<MessageBase> msg = new MessageScreenInitialized(this);
1756 postMessageAsync(msg); // we may be called from main thread, use async message
1757}
1758
1759
Mathias Agopianb60314a2012-04-10 22:09:54 -07001760void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001761 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001762 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001763 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001764 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001765 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001766 mVisibleRegionsDirty = true;
1767 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768}
1769
Mathias Agopianb60314a2012-04-10 22:09:54 -07001770void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001771 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001772 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1773 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001774 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001775 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001776 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001777 // from this point on, SF will stop drawing
1778 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779}
1780
Colin Cross8e533062012-06-07 13:17:52 -07001781void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001782 class MessageScreenAcquired : public MessageBase {
1783 SurfaceFlinger* flinger;
1784 public:
1785 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1786 virtual bool handler() {
1787 flinger->onScreenAcquired();
1788 return true;
1789 }
1790 };
1791 sp<MessageBase> msg = new MessageScreenAcquired(this);
1792 postMessageSync(msg);
1793}
1794
Colin Cross8e533062012-06-07 13:17:52 -07001795void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001796 class MessageScreenReleased : public MessageBase {
1797 SurfaceFlinger* flinger;
1798 public:
1799 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1800 virtual bool handler() {
1801 flinger->onScreenReleased();
1802 return true;
1803 }
1804 };
1805 sp<MessageBase> msg = new MessageScreenReleased(this);
1806 postMessageSync(msg);
1807}
1808
1809// ---------------------------------------------------------------------------
1810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1812{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001813 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814 char buffer[SIZE];
1815 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001816
1817 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 snprintf(buffer, SIZE, "Permission Denial: "
1819 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1820 IPCThreadState::self()->getCallingPid(),
1821 IPCThreadState::self()->getCallingUid());
1822 result.append(buffer);
1823 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001824 // Try to get the main lock, but don't insist if we can't
1825 // (this would indicate SF is stuck, but we want to be able to
1826 // print something in dumpsys).
1827 int retry = 3;
1828 while (mStateLock.tryLock()<0 && --retry>=0) {
1829 usleep(1000000);
1830 }
1831 const bool locked(retry >= 0);
1832 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001833 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001834 "SurfaceFlinger appears to be unresponsive, "
1835 "dumping anyways (no locks held)\n");
1836 result.append(buffer);
1837 }
1838
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001839 bool dumpAll = true;
1840 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001841 size_t numArgs = args.size();
1842 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001843 if ((index < numArgs) &&
1844 (args[index] == String16("--list"))) {
1845 index++;
1846 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001847 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001848 }
1849
1850 if ((index < numArgs) &&
1851 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001852 index++;
1853 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001854 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001855 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001856
1857 if ((index < numArgs) &&
1858 (args[index] == String16("--latency-clear"))) {
1859 index++;
1860 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001861 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001862 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001864
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001865 if (dumpAll) {
1866 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001867 }
1868
Mathias Agopian9795c422009-08-26 16:36:26 -07001869 if (locked) {
1870 mStateLock.unlock();
1871 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872 }
1873 write(fd, result.string(), result.size());
1874 return NO_ERROR;
1875}
1876
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001877void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1878 String8& result, char* buffer, size_t SIZE) const
1879{
1880 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1881 const size_t count = currentLayers.size();
1882 for (size_t i=0 ; i<count ; i++) {
1883 const sp<LayerBase>& layer(currentLayers[i]);
1884 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1885 result.append(buffer);
1886 }
1887}
1888
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001889void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1890 String8& result, char* buffer, size_t SIZE) const
1891{
1892 String8 name;
1893 if (index < args.size()) {
1894 name = String8(args[index]);
1895 index++;
1896 }
1897
1898 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1899 const size_t count = currentLayers.size();
1900 for (size_t i=0 ; i<count ; i++) {
1901 const sp<LayerBase>& layer(currentLayers[i]);
1902 if (name.isEmpty()) {
1903 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1904 result.append(buffer);
1905 }
1906 if (name.isEmpty() || (name == layer->getName())) {
1907 layer->dumpStats(result, buffer, SIZE);
1908 }
1909 }
1910}
1911
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001912void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1913 String8& result, char* buffer, size_t SIZE) const
1914{
1915 String8 name;
1916 if (index < args.size()) {
1917 name = String8(args[index]);
1918 index++;
1919 }
1920
1921 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1922 const size_t count = currentLayers.size();
1923 for (size_t i=0 ; i<count ; i++) {
1924 const sp<LayerBase>& layer(currentLayers[i]);
1925 if (name.isEmpty() || (name == layer->getName())) {
1926 layer->clearStats();
1927 }
1928 }
1929}
1930
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001931void SurfaceFlinger::dumpAllLocked(
1932 String8& result, char* buffer, size_t SIZE) const
1933{
1934 // figure out if we're stuck somewhere
1935 const nsecs_t now = systemTime();
1936 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1937 const nsecs_t inTransaction(mDebugInTransaction);
1938 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1939 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1940
1941 /*
1942 * Dump the visible layer list
1943 */
1944 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1945 const size_t count = currentLayers.size();
1946 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1947 result.append(buffer);
1948 for (size_t i=0 ; i<count ; i++) {
1949 const sp<LayerBase>& layer(currentLayers[i]);
1950 layer->dump(result, buffer, SIZE);
1951 }
1952
1953 /*
1954 * Dump the layers in the purgatory
1955 */
1956
1957 const size_t purgatorySize = mLayerPurgatory.size();
1958 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1959 result.append(buffer);
1960 for (size_t i=0 ; i<purgatorySize ; i++) {
1961 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1962 layer->shortDump(result, buffer, SIZE);
1963 }
1964
1965 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001966 * Dump Display state
1967 */
1968
1969 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1970 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1971 snprintf(buffer, SIZE,
1972 "+ DisplayDevice[%u]\n"
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001973 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d (type=%08x), "
1974 "flips=%u, secure=%d, numLayers=%u, v:[%d,%d,%d,%d], f:[%d,%d,%d,%d]\n",
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001975 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001976 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001977 hw->getWidth(), hw->getHeight(),
1978 hw->getOrientation(), hw->getTransform().getType(),
1979 hw->getPageFlipCount(),
1980 hw->getSecureLayerVisible(),
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001981 hw->getVisibleLayersSortedByZ().size(),
1982 hw->getViewport().left, hw->getViewport().top, hw->getViewport().right, hw->getViewport().bottom,
1983 hw->getFrame().left, hw->getFrame().top, hw->getFrame().right, hw->getFrame().bottom);
1984
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001985 result.append(buffer);
1986 }
1987
1988 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001989 * Dump SurfaceFlinger global state
1990 */
1991
1992 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1993 result.append(buffer);
1994
Mathias Agopian888c8222012-08-04 21:10:38 -07001995 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001996 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001997 const GLExtensions& extensions(GLExtensions::getInstance());
1998 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1999 extensions.getVendor(),
2000 extensions.getRenderer(),
2001 extensions.getVersion());
2002 result.append(buffer);
2003
2004 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002005 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002006 result.append(buffer);
2007
2008 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2009 result.append(buffer);
2010
Mathias Agopian42977342012-08-05 00:40:46 -07002011 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002012 snprintf(buffer, SIZE,
2013 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002014 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002015 result.append(buffer);
2016 snprintf(buffer, SIZE,
2017 " last eglSwapBuffers() time: %f us\n"
2018 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002019 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002020 " refresh-rate : %f fps\n"
2021 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002022 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002023 mLastSwapBufferTime/1000.0,
2024 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002025 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07002026 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07002027 hwc.getDpiX(),
2028 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002029 result.append(buffer);
2030
2031 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2032 inSwapBuffersDuration/1000.0);
2033 result.append(buffer);
2034
2035 snprintf(buffer, SIZE, " transaction time: %f us\n",
2036 inTransactionDuration/1000.0);
2037 result.append(buffer);
2038
2039 /*
2040 * VSYNC state
2041 */
2042 mEventThread->dump(result, buffer, SIZE);
2043
2044 /*
2045 * Dump HWComposer state
2046 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002047 snprintf(buffer, SIZE, "h/w composer state:\n");
2048 result.append(buffer);
2049 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2050 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2051 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2052 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002053 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002054
2055 /*
2056 * Dump gralloc state
2057 */
2058 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2059 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002060 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002061}
2062
Keun young Park63f165f2012-08-31 10:53:36 -07002063bool SurfaceFlinger::startDdmConnection()
2064{
2065 void* libddmconnection_dso =
2066 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2067 if (!libddmconnection_dso) {
2068 return false;
2069 }
2070 void (*DdmConnection_start)(const char* name);
2071 DdmConnection_start =
2072 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2073 if (!DdmConnection_start) {
2074 dlclose(libddmconnection_dso);
2075 return false;
2076 }
2077 (*DdmConnection_start)(getServiceName());
2078 return true;
2079}
2080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002081status_t SurfaceFlinger::onTransact(
2082 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2083{
2084 switch (code) {
2085 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002086 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002088 case BLANK:
2089 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002090 {
2091 // codes that require permission check
2092 IPCThreadState* ipc = IPCThreadState::self();
2093 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002094 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002095 if ((uid != AID_GRAPHICS) &&
2096 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002097 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002098 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2099 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002101 break;
2102 }
2103 case CAPTURE_SCREEN:
2104 {
2105 // codes that require permission check
2106 IPCThreadState* ipc = IPCThreadState::self();
2107 const int pid = ipc->getCallingPid();
2108 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002109 if ((uid != AID_GRAPHICS) &&
2110 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002111 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002112 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2113 return PERMISSION_DENIED;
2114 }
2115 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002116 }
2117 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2120 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002121 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002122 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002123 IPCThreadState* ipc = IPCThreadState::self();
2124 const int pid = ipc->getCallingPid();
2125 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002126 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002127 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002128 return PERMISSION_DENIED;
2129 }
2130 int n;
2131 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002132 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002133 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002134 return NO_ERROR;
2135 case 1002: // SHOW_UPDATES
2136 n = data.readInt32();
2137 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002138 invalidateHwcGeometry();
2139 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002140 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002141 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002142 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002143 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002145 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002146 setTransactionFlags(
2147 eTransactionNeeded|
2148 eDisplayTransactionNeeded|
2149 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002150 return NO_ERROR;
2151 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002152 case 1006:{ // send empty update
2153 signalRefresh();
2154 return NO_ERROR;
2155 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002156 case 1008: // toggle use of hw composer
2157 n = data.readInt32();
2158 mDebugDisableHWC = n ? 1 : 0;
2159 invalidateHwcGeometry();
2160 repaintEverything();
2161 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002162 case 1009: // toggle use of transform hint
2163 n = data.readInt32();
2164 mDebugDisableTransformHint = n ? 1 : 0;
2165 invalidateHwcGeometry();
2166 repaintEverything();
2167 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002168 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002169 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002170 reply->writeInt32(0);
2171 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002172 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002173 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174 return NO_ERROR;
2175 case 1013: {
2176 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002177 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2178 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179 }
2180 return NO_ERROR;
2181 }
2182 }
2183 return err;
2184}
2185
Mathias Agopian53331da2011-08-22 21:44:41 -07002186void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002187 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002188 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002189}
2190
Mathias Agopian59119e62010-10-11 12:37:43 -07002191// ---------------------------------------------------------------------------
2192
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002193status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002194 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2195{
2196 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002197 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002198}
2199
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002200status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002201 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002202{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002203 ATRACE_CALL();
2204
Mathias Agopian59119e62010-10-11 12:37:43 -07002205 if (!GLExtensions::getInstance().haveFramebufferObject())
2206 return INVALID_OPERATION;
2207
2208 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002209 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2210 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002211 const uint32_t hw_w = hw->getWidth();
2212 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002213 GLfloat u = 1;
2214 GLfloat v = 1;
2215
2216 // make sure to clear all GL error flags
2217 while ( glGetError() != GL_NO_ERROR ) ;
2218
2219 // create a FBO
2220 GLuint name, tname;
2221 glGenTextures(1, &tname);
2222 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002223 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2224 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002225 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2226 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002227 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002228 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002229 GLint tw = (2 << (31 - clz(hw_w)));
2230 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002231 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2232 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002233 u = GLfloat(hw_w) / tw;
2234 v = GLfloat(hw_h) / th;
2235 }
2236 glGenFramebuffersOES(1, &name);
2237 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002238 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2239 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002240
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002241 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002242 glDisable(GL_TEXTURE_EXTERNAL_OES);
2243 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244 glClearColor(0,0,0,1);
2245 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002246 glMatrixMode(GL_MODELVIEW);
2247 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002248 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002249 const size_t count = layers.size();
2250 for (size_t i=0 ; i<count ; ++i) {
2251 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002252 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002253 }
2254
Mathias Agopian42977342012-08-05 00:40:46 -07002255 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002256
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002257 // back to main framebuffer
2258 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 glDeleteFramebuffersOES(1, &name);
2260
2261 *textureName = tname;
2262 *uOut = u;
2263 *vOut = v;
2264 return NO_ERROR;
2265}
2266
2267// ---------------------------------------------------------------------------
2268
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002269status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270 sp<IMemoryHeap>* heap,
2271 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002272 uint32_t sw, uint32_t sh,
2273 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002274{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002275 ATRACE_CALL();
2276
Mathias Agopian74c40c02010-09-29 13:02:36 -07002277 status_t result = PERMISSION_DENIED;
2278
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002279 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002281 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002282
2283 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002284 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002285 const uint32_t hw_w = hw->getWidth();
2286 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002288 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002289 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002290 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002291 return PERMISSION_DENIED;
2292 }
2293
2294 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002295 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002296 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002297 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002298
2299 sw = (!sw) ? hw_w : sw;
2300 sh = (!sh) ? hw_h : sh;
2301 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002302 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002303
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002304// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2305// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002306
Mathias Agopian74c40c02010-09-29 13:02:36 -07002307 // make sure to clear all GL error flags
2308 while ( glGetError() != GL_NO_ERROR ) ;
2309
2310 // create a FBO
2311 GLuint name, tname;
2312 glGenRenderbuffersOES(1, &tname);
2313 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2314 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002315
Mathias Agopian74c40c02010-09-29 13:02:36 -07002316 glGenFramebuffersOES(1, &name);
2317 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2318 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2319 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2320
2321 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002322
Mathias Agopian74c40c02010-09-29 13:02:36 -07002323 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2324
2325 // invert everything, b/c glReadPixel() below will invert the FB
2326 glViewport(0, 0, sw, sh);
2327 glMatrixMode(GL_PROJECTION);
2328 glPushMatrix();
2329 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002330 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 glMatrixMode(GL_MODELVIEW);
2332
2333 // redraw the screen entirely...
2334 glClearColor(0,0,0,1);
2335 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002336
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002337 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002338 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002339 for (size_t i=0 ; i<count ; ++i) {
2340 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002341 const uint32_t z = layer->drawingState().z;
2342 if (z >= minLayerZ && z <= maxLayerZ) {
2343 if (filtering) layer->setFiltering(true);
2344 layer->draw(hw);
2345 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002346 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002347 }
2348
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 // check for errors and return screen capture
2350 if (glGetError() != GL_NO_ERROR) {
2351 // error while rendering
2352 result = INVALID_OPERATION;
2353 } else {
2354 // allocate shared memory large enough to hold the
2355 // screen capture
2356 sp<MemoryHeapBase> base(
2357 new MemoryHeapBase(size, 0, "screen-capture") );
2358 void* const ptr = base->getBase();
2359 if (ptr) {
2360 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002361 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2363 if (glGetError() == GL_NO_ERROR) {
2364 *heap = base;
2365 *w = sw;
2366 *h = sh;
2367 *f = PIXEL_FORMAT_RGBA_8888;
2368 result = NO_ERROR;
2369 }
2370 } else {
2371 result = NO_MEMORY;
2372 }
2373 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 glViewport(0, 0, hw_w, hw_h);
2375 glMatrixMode(GL_PROJECTION);
2376 glPopMatrix();
2377 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002378 } else {
2379 result = BAD_VALUE;
2380 }
2381
2382 // release FBO resources
2383 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2384 glDeleteRenderbuffersOES(1, &tname);
2385 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002386
Mathias Agopian42977342012-08-05 00:40:46 -07002387 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002388
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002389// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002390
Mathias Agopian74c40c02010-09-29 13:02:36 -07002391 return result;
2392}
2393
2394
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002395status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002396 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002397 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002398 uint32_t sw, uint32_t sh,
2399 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002400{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002401 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002402 return BAD_VALUE;
2403
2404 if (!GLExtensions::getInstance().haveFramebufferObject())
2405 return INVALID_OPERATION;
2406
2407 class MessageCaptureScreen : public MessageBase {
2408 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002409 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002410 sp<IMemoryHeap>* heap;
2411 uint32_t* w;
2412 uint32_t* h;
2413 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002414 uint32_t sw;
2415 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002416 uint32_t minLayerZ;
2417 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002418 status_t result;
2419 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002420 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002421 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002422 uint32_t sw, uint32_t sh,
2423 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002424 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002425 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2426 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2427 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002428 {
2429 }
2430 status_t getResult() const {
2431 return result;
2432 }
2433 virtual bool handler() {
2434 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002435 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002436 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002437 return true;
2438 }
2439 };
2440
2441 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002442 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443 status_t res = postMessageSync(msg);
2444 if (res == NO_ERROR) {
2445 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2446 }
2447 return res;
2448}
2449
2450// ---------------------------------------------------------------------------
2451
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002452SurfaceFlinger::LayerVector::LayerVector() {
2453}
2454
2455SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2456 : SortedVector<sp<LayerBase> >(rhs) {
2457}
2458
2459int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2460 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002461{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002462 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002463 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2464 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002465
2466 uint32_t ls = l->currentState().layerStack;
2467 uint32_t rs = r->currentState().layerStack;
2468 if (ls != rs)
2469 return ls - rs;
2470
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002471 uint32_t lz = l->currentState().z;
2472 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002473 if (lz != rz)
2474 return lz - rz;
2475
2476 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002477}
2478
2479// ---------------------------------------------------------------------------
2480
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002481SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2482 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002483}
2484
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002485SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2486 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002487 viewport.makeInvalid();
2488 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002489}
2490
2491// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492
Jamie Gennis9a78c902011-01-12 18:30:40 -08002493GraphicBufferAlloc::GraphicBufferAlloc() {}
2494
2495GraphicBufferAlloc::~GraphicBufferAlloc() {}
2496
2497sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002498 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002499 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2500 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002501 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002502 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002503 if (err == NO_MEMORY) {
2504 GraphicBuffer::dumpAllocationsToSystemLog();
2505 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002506 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002507 "failed (%s), handle=%p",
2508 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002509 return 0;
2510 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002511 return graphicBuffer;
2512}
2513
Jamie Gennis9a78c902011-01-12 18:30:40 -08002514// ---------------------------------------------------------------------------
2515
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516}; // namespace android