blob: bdd8c67171d7a2824ae7415670b1fd3d5fc27e20 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
40#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/SurfaceTextureClient.h>
42
43#include <ui/GraphicBufferAllocator.h>
44#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045
Mathias Agopiancde87a32012-09-13 14:09:01 -070046#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <utils/String8.h>
48#include <utils/String16.h>
49#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Mathias Agopian921e6ac2012-07-23 23:11:29 -070052#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080055#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070056#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070057#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080058#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070059#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070062#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopiana4912602012-07-12 14:25:33 -070065#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070066#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopiana67932f2011-04-20 14:20:59 -070068
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080069#define EGL_VERSION_HW_ANDROID 0x3143
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#define DISPLAY_COUNT 1
72
73namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian99b49842011-06-27 16:05:52 -070076const String16 sHardwareTest("android.permission.HARDWARE_TEST");
77const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
78const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
79const String16 sDump("android.permission.DUMP");
80
81// ---------------------------------------------------------------------------
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083SurfaceFlinger::SurfaceFlinger()
84 : BnSurfaceComposer(), Thread(false),
85 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070086 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070088 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070091 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700100 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101{
Steve Blocka19954a2012-01-04 20:05:49 +0000102 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 property_get("debug.sf.showupdates", value, "0");
108 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110 property_get("debug.sf.ddms", value, "0");
111 mDebugDDMS = atoi(value);
112 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700113 if (!startDdmConnection()) {
114 // start failed, and DDMS debugging not enabled
115 mDebugDDMS = 0;
116 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700118 ALOGI_IF(mDebugRegion, "showupdates enabled");
119 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800122void SurfaceFlinger::onFirstRef()
123{
124 mEventQueue.init(this);
125
126 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
127
128 // Wait for the main thread to be done with its initialization
129 mReadyToRunBarrier.wait();
130}
131
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133SurfaceFlinger::~SurfaceFlinger()
134{
Mathias Agopiana4912602012-07-12 14:25:33 -0700135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
136 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
137 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::binderDied(const wp<IBinder>& who)
141{
142 // the window manager died on us. prepare its eulogy.
143
Andy McFadden13a082e2012-08-24 10:16:42 -0700144 // restore initial conditions (default device unblank, etc)
145 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146
147 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700148 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149}
150
Mathias Agopian7e27f052010-05-28 14:22:23 -0700151sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Mathias Agopian96f08192010-06-02 23:28:45 -0700153 sp<ISurfaceComposerClient> bclient;
154 sp<Client> client(new Client(this));
155 status_t err = client->initCheck();
156 if (err == NO_ERROR) {
157 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 return bclient;
160}
161
Mathias Agopiane57f2922012-08-09 16:29:12 -0700162sp<IBinder> SurfaceFlinger::createDisplay()
163{
164 class DisplayToken : public BBinder {
165 sp<SurfaceFlinger> flinger;
166 virtual ~DisplayToken() {
167 // no more references, this display must be terminated
168 Mutex::Autolock _l(flinger->mStateLock);
169 flinger->mCurrentState.displays.removeItem(this);
170 flinger->setTransactionFlags(eDisplayTransactionNeeded);
171 }
172 public:
173 DisplayToken(const sp<SurfaceFlinger>& flinger)
174 : flinger(flinger) {
175 }
176 };
177
178 sp<BBinder> token = new DisplayToken(this);
179
180 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700181 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700182 mCurrentState.displays.add(token, info);
183
184 return token;
185}
186
187sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700188 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700189 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
190 return NULL;
191 }
192 return mDefaultDisplays[id];
193}
194
Jamie Gennis9a78c902011-01-12 18:30:40 -0800195sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
196{
197 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
198 return gba;
199}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201void SurfaceFlinger::bootFinished()
202{
203 const nsecs_t now = systemTime();
204 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000205 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700206 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700207
208 // wait patiently for the window manager death
209 const String16 name("window");
210 sp<IBinder> window(defaultServiceManager()->getService(name));
211 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700212 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700213 }
214
215 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700216 // formerly we would just kill the process, but we now ask it to exit so it
217 // can choose where to stop the animation.
218 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219}
220
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700221void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
222 class MessageDestroyGLTexture : public MessageBase {
223 GLuint texture;
224 public:
225 MessageDestroyGLTexture(GLuint texture)
226 : texture(texture) {
227 }
228 virtual bool handler() {
229 glDeleteTextures(1, &texture);
230 return true;
231 }
232 };
233 postMessageAsync(new MessageDestroyGLTexture(texture));
234}
235
Mathias Agopiana4912602012-07-12 14:25:33 -0700236status_t SurfaceFlinger::selectConfigForPixelFormat(
237 EGLDisplay dpy,
238 EGLint const* attrs,
239 PixelFormat format,
240 EGLConfig* outConfig)
241{
242 EGLConfig config = NULL;
243 EGLint numConfigs = -1, n=0;
244 eglGetConfigs(dpy, NULL, 0, &numConfigs);
245 EGLConfig* const configs = new EGLConfig[numConfigs];
246 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700247
Mathias Agopiana4912602012-07-12 14:25:33 -0700248 for (int i=0 ; i<n ; i++) {
249 EGLint nativeVisualId = 0;
250 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
251 if (nativeVisualId>0 && format == nativeVisualId) {
252 *outConfig = configs[i];
253 delete [] configs;
254 return NO_ERROR;
255 }
256 }
257 delete [] configs;
258 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259}
260
Mathias Agopiana4912602012-07-12 14:25:33 -0700261EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
262 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
263 // it is to be used with WIFI displays
264 EGLConfig config;
265 EGLint dummy;
266 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700267
Mathias Agopiana4912602012-07-12 14:25:33 -0700268 EGLint attribs[] = {
269 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiancde87a32012-09-13 14:09:01 -0700270 EGL_RED_SIZE, 8,
271 EGL_GREEN_SIZE, 8,
272 EGL_BLUE_SIZE, 8,
273 // EGL_RECORDABLE_ANDROID must be last so that we can retry
274 // without it easily (see below)
Mathias Agopiana4912602012-07-12 14:25:33 -0700275 EGL_RECORDABLE_ANDROID, EGL_TRUE,
276 EGL_NONE
277 };
278 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
279 if (err) {
280 // maybe we failed because of EGL_RECORDABLE_ANDROID
281 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
Mathias Agopiancde87a32012-09-13 14:09:01 -0700282 attribs[NELEM(attribs) - 3] = EGL_NONE;
Mathias Agopiana4912602012-07-12 14:25:33 -0700283 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
284 }
285 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
286 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
287 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
288 }
289 return config;
290}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291
Mathias Agopiana4912602012-07-12 14:25:33 -0700292EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
293 // Also create our EGLContext
294 EGLint contextAttributes[] = {
295#ifdef EGL_IMG_context_priority
296#ifdef HAS_CONTEXT_PRIORITY
297#warning "using EGL_IMG_context_priority"
298 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
299#endif
300#endif
301 EGL_NONE, EGL_NONE
302 };
303 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
304 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
305 return ctxt;
306}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
Mathias Agopiancde87a32012-09-13 14:09:01 -0700308void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 GLExtensions& extensions(GLExtensions::getInstance());
310 extensions.initWithGLStrings(
311 glGetString(GL_VENDOR),
312 glGetString(GL_RENDERER),
313 glGetString(GL_VERSION),
314 glGetString(GL_EXTENSIONS),
315 eglQueryString(display, EGL_VENDOR),
316 eglQueryString(display, EGL_VERSION),
317 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700318
Mathias Agopiana4912602012-07-12 14:25:33 -0700319 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
320 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700323 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 glShadeModel(GL_FLAT);
326 glDisable(GL_DITHER);
327 glDisable(GL_CULL_FACE);
328
Mathias Agopiana4912602012-07-12 14:25:33 -0700329 struct pack565 {
330 inline uint16_t operator() (int r, int g, int b) const {
331 return (r<<11)|(g<<5)|b;
332 }
333 } pack565;
334
Jamie Gennis9575f602011-10-07 14:51:16 -0700335 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
336 glGenTextures(1, &mProtectedTexName);
337 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
338 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
340 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
341 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
342 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
343 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344
Mathias Agopiana4912602012-07-12 14:25:33 -0700345 // print some debugging info
346 EGLint r,g,b,a;
347 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
348 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
349 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
350 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
351 ALOGI("EGL informations:");
352 ALOGI("vendor : %s", extensions.getEglVendor());
353 ALOGI("version : %s", extensions.getEglVersion());
354 ALOGI("extensions: %s", extensions.getEglExtension());
355 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
356 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
357 ALOGI("OpenGL ES informations:");
358 ALOGI("vendor : %s", extensions.getVendor());
359 ALOGI("renderer : %s", extensions.getRenderer());
360 ALOGI("version : %s", extensions.getVersion());
361 ALOGI("extensions: %s", extensions.getExtension());
362 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
363 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
364}
365
Mathias Agopiana4912602012-07-12 14:25:33 -0700366status_t SurfaceFlinger::readyToRun()
367{
368 ALOGI( "SurfaceFlinger's main thread ready to run. "
369 "Initializing graphics H/W...");
370
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700371 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700372 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
373 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700374
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700375 // Initialize the H/W composer object. There may or may not be an
376 // actual hardware composer underneath.
377 mHwc = new HWComposer(this,
378 *static_cast<HWComposer::EventHandler *>(this));
379
Mathias Agopiana4912602012-07-12 14:25:33 -0700380 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700381 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700382 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
383 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700384
Mathias Agopianda27af92012-09-13 18:17:13 -0700385 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
386 "couldn't create EGLContext");
387
Mathias Agopiancde87a32012-09-13 14:09:01 -0700388 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700389 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700390 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700391 mCurrentState.displays.add(mDefaultDisplays[i],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700392 DisplayDeviceState(DisplayDevice::DisplayType(i)));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700393 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700394
395 // The main display is a bit special and always exists
396 //
397 // if we didn't add it here, it would be added automatically during the
398 // first transaction, however this would also create some difficulties:
399 //
400 // - there would be a race where a client could call getDisplayInfo(),
401 // for instance before the DisplayDevice is created.
402 //
403 // - we need a GL context current in a few places, when initializing
404 // OpenGL ES (see below), or creating a layer,
405 // or when a texture is (asynchronously) destroyed, and for that
406 // we need a valid surface, so it's conveniant to use the main display
407 // for that.
408
409 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc);
410 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
411 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700412 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700413 DisplayDevice::DISPLAY_PRIMARY,
414 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700415 stc, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700416 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700417
Mathias Agopiancde87a32012-09-13 14:09:01 -0700418
Mathias Agopiana4912602012-07-12 14:25:33 -0700419 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700420 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
421 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800422
Mathias Agopian028508c2012-07-25 21:12:12 -0700423 // start the EventThread
424 mEventThread = new EventThread(this);
425 mEventQueue.setEventThread(mEventThread);
426
Mathias Agopian92a979a2012-08-02 18:32:23 -0700427 // initialize our drawing state
428 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700429
Mathias Agopiancde87a32012-09-13 14:09:01 -0700430
Mathias Agopiana4912602012-07-12 14:25:33 -0700431 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800432 mReadyToRunBarrier.open();
433
Andy McFadden13a082e2012-08-24 10:16:42 -0700434 // set initial conditions (e.g. unblank default device)
435 initializeDisplays();
436
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700437 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700438 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 return NO_ERROR;
441}
442
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700443int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
444 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
445 type : mHwc->allocateDisplayId();
446}
447
Mathias Agopiana67e4182012-06-19 17:26:12 -0700448void SurfaceFlinger::startBootAnim() {
449 // start boot animation
450 property_set("service.bootanim.exit", "0");
451 property_set("ctl.start", "bootanim");
452}
453
Mathias Agopiana4912602012-07-12 14:25:33 -0700454uint32_t SurfaceFlinger::getMaxTextureSize() const {
455 return mMaxTextureSize;
456}
457
458uint32_t SurfaceFlinger::getMaxViewportDims() const {
459 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
460 mMaxViewportDims[0] : mMaxViewportDims[1];
461}
462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800464
Jamie Gennis582270d2011-08-17 18:19:00 -0700465bool SurfaceFlinger::authenticateSurfaceTexture(
466 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800467 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700468 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800469
470 // Check the visible layer list for the ISurface
471 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
472 size_t count = currentLayers.size();
473 for (size_t i=0 ; i<count ; i++) {
474 const sp<LayerBase>& layer(currentLayers[i]);
475 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700476 if (lbc != NULL) {
477 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
478 if (lbcBinder == surfaceTextureBinder) {
479 return true;
480 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800481 }
482 }
483
484 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700485 // SurfaceTexture gets destroyed before all the clients are done using it,
486 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800487 // will instead fail later on when the client tries to use the surface,
488 // which should be reported as "surface XYZ returned an -ENODEV". The
489 // purgatorized layers are no less authentic than the visible ones, so this
490 // should not cause any harm.
491 size_t purgatorySize = mLayerPurgatory.size();
492 for (size_t i=0 ; i<purgatorySize ; i++) {
493 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
494 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700495 if (lbc != NULL) {
496 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
497 if (lbcBinder == surfaceTextureBinder) {
498 return true;
499 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800500 }
501 }
502
503 return false;
504}
505
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700506status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
507 // TODO: this is mostly here only for compatibility
508 // the display size is needed but the display metrics should come from elsewhere
509 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
510 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700511 return BAD_INDEX;
512 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700513
514 const HWComposer& hwc(getHwComposer());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700515 float xdpi = hwc.getDpiX(HWC_DISPLAY_PRIMARY);
516 float ydpi = hwc.getDpiY(HWC_DISPLAY_PRIMARY);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700517
518 // TODO: Not sure if display density should handled by SF any longer
519 class Density {
520 static int getDensityFromProperty(char const* propName) {
521 char property[PROPERTY_VALUE_MAX];
522 int density = 0;
523 if (property_get(propName, property, NULL) > 0) {
524 density = atoi(property);
525 }
526 return density;
527 }
528 public:
529 static int getEmuDensity() {
530 return getDensityFromProperty("qemu.sf.lcd_density"); }
531 static int getBuildDensity() {
532 return getDensityFromProperty("ro.sf.lcd_density"); }
533 };
534 // The density of the device is provided by a build property
535 float density = Density::getBuildDensity() / 160.0f;
536 if (density == 0) {
537 // the build doesn't provide a density -- this is wrong!
538 // use xdpi instead
539 ALOGE("ro.sf.lcd_density must be defined as a build property");
540 density = xdpi / 160.0f;
541 }
542 if (Density::getEmuDensity()) {
543 // if "qemu.sf.lcd_density" is specified, it overrides everything
544 xdpi = ydpi = density = Density::getEmuDensity();
545 density /= 160.0f;
546 }
547
Mathias Agopian42977342012-08-05 00:40:46 -0700548 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
549 info->w = hw->getWidth();
550 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700551 info->xdpi = xdpi;
552 info->ydpi = ydpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700553 info->fps = float(1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY));
Mathias Agopian8b736f12012-08-13 17:54:26 -0700554 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700555 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700556 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700557 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700558 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700559}
560
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800561// ----------------------------------------------------------------------------
562
563sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800564 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700565}
566
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700567void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700568
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700569 sp<IBinder> token;
570 { // scope for the lock
571 Mutex::Autolock _l(mStateLock);
572 token = mExtDisplayToken;
573 }
574
575 if (token == 0) {
576 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700577 }
578
579 { // scope for the lock
580 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700581 if (surface == 0) {
582 // release our current display. we're guarantee to have
583 // a reference to it (token), while we hold the lock
584 mExtDisplayToken = 0;
585 } else {
586 mExtDisplayToken = token;
587 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700588
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700589 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
590 info.surface = surface;
591 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700592 }
593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800596
597void SurfaceFlinger::waitForEvent() {
598 mEventQueue.waitMessage();
599}
600
601void SurfaceFlinger::signalTransaction() {
602 mEventQueue.invalidate();
603}
604
605void SurfaceFlinger::signalLayerUpdate() {
606 mEventQueue.invalidate();
607}
608
609void SurfaceFlinger::signalRefresh() {
610 mEventQueue.refresh();
611}
612
613status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
614 nsecs_t reltime, uint32_t flags) {
615 return mEventQueue.postMessage(msg, reltime);
616}
617
618status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
619 nsecs_t reltime, uint32_t flags) {
620 status_t res = mEventQueue.postMessage(msg, reltime);
621 if (res == NO_ERROR) {
622 msg->wait();
623 }
624 return res;
625}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 return true;
630}
631
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700632void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700633 if (mEventThread == NULL) {
634 // This is a temporary workaround for b/7145521. A non-null pointer
635 // does not mean EventThread has finished initializing, so this
636 // is not a correct fix.
637 ALOGW("WARNING: EventThread not started, ignoring vsync");
638 return;
639 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700640 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
641 // we should only receive DisplayDevice::DisplayType from the vsync callback
642 const wp<IBinder>& token(mDefaultDisplays[type]);
643 mEventThread->onVSyncReceived(token, timestamp);
644 }
Mathias Agopian86303202012-07-24 22:46:10 -0700645}
646
647void SurfaceFlinger::eventControl(int event, int enabled) {
648 getHwComposer().eventControl(event, enabled);
649}
650
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800652 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800653 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700654 case MessageQueue::INVALIDATE:
655 handleMessageTransaction();
656 handleMessageInvalidate();
657 signalRefresh();
658 break;
659 case MessageQueue::REFRESH:
660 handleMessageRefresh();
661 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800662 }
663}
664
Mathias Agopian4fec8732012-06-29 14:12:52 -0700665void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700666 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700667 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700668 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700669 }
670}
671
672void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700673 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700674 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700675}
676
677void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700678 ATRACE_CALL();
679 preComposition();
680 rebuildLayerStacks();
681 setUpHWComposer();
682 doDebugFlashRegions();
683 doComposition();
684 postComposition();
685}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700686
Mathias Agopiancd60f992012-08-16 16:28:27 -0700687void SurfaceFlinger::doDebugFlashRegions()
688{
689 // is debugging enabled
690 if (CC_LIKELY(!mDebugRegion))
691 return;
692
693 const bool repaintEverything = mRepaintEverything;
694 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
695 const sp<DisplayDevice>& hw(mDisplays[dpy]);
696 if (hw->canDraw()) {
697 // transform the dirty region into this screen's coordinate space
698 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
699 if (!dirtyRegion.isEmpty()) {
700 // redraw the whole screen
701 doComposeSurfaces(hw, Region(hw->bounds()));
702
703 // and draw the dirty region
704 glDisable(GL_TEXTURE_EXTERNAL_OES);
705 glDisable(GL_TEXTURE_2D);
706 glDisable(GL_BLEND);
707 glColor4f(1, 0, 1, 1);
708 const int32_t height = hw->getHeight();
709 Region::const_iterator it = dirtyRegion.begin();
710 Region::const_iterator const end = dirtyRegion.end();
711 while (it != end) {
712 const Rect& r = *it++;
713 GLfloat vertices[][2] = {
714 { r.left, height - r.top },
715 { r.left, height - r.bottom },
716 { r.right, height - r.bottom },
717 { r.right, height - r.top }
718 };
719 glVertexPointer(2, GL_FLOAT, 0, vertices);
720 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
721 }
722 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700723 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700724 }
725 }
726 }
727
728 postFramebuffer();
729
730 if (mDebugRegion > 1) {
731 usleep(mDebugRegion * 1000);
732 }
733}
734
735void SurfaceFlinger::preComposition()
736{
737 bool needExtraInvalidate = false;
738 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
739 const size_t count = currentLayers.size();
740 for (size_t i=0 ; i<count ; i++) {
741 if (currentLayers[i]->onPreComposition()) {
742 needExtraInvalidate = true;
743 }
744 }
745 if (needExtraInvalidate) {
746 signalLayerUpdate();
747 }
748}
749
750void SurfaceFlinger::postComposition()
751{
752 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
753 const size_t count = currentLayers.size();
754 for (size_t i=0 ; i<count ; i++) {
755 currentLayers[i]->onPostComposition();
756 }
757}
758
759void SurfaceFlinger::rebuildLayerStacks() {
760 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700761 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700762 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700763 mVisibleRegionsDirty = false;
764 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700765
Mathias Agopian87baae12012-07-31 12:38:26 -0700766 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700767 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700768 Region opaqueRegion;
769 Region dirtyRegion;
770 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700771 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700772 const Transform& tr(hw->getTransform());
773 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700774 if (hw->canDraw()) {
775 SurfaceFlinger::computeVisibleRegions(currentLayers,
776 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700777
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700778 const size_t count = currentLayers.size();
779 for (size_t i=0 ; i<count ; i++) {
780 const sp<LayerBase>& layer(currentLayers[i]);
781 const Layer::State& s(layer->drawingState());
782 if (s.layerStack == hw->getLayerStack()) {
783 Region visibleRegion(tr.transform(layer->visibleRegion));
784 visibleRegion.andSelf(bounds);
785 if (!visibleRegion.isEmpty()) {
786 layersSortedByZ.add(layer);
787 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700788 }
789 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700790 }
Mathias Agopian42977342012-08-05 00:40:46 -0700791 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700792 hw->undefinedRegion.set(bounds);
793 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
794 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700795 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700796 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700797}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700798
Mathias Agopiancd60f992012-08-16 16:28:27 -0700799void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700800 HWComposer& hwc(getHwComposer());
801 if (hwc.initCheck() == NO_ERROR) {
802 // build the h/w work list
803 const bool workListsDirty = mHwWorkListDirty;
804 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700805 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700806 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700807 const int32_t id = hw->getHwcDisplayId();
808 if (id >= 0) {
809 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700810 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700811 const size_t count = currentLayers.size();
812 if (hwc.createWorkList(id, count) >= 0) {
813 HWComposer::LayerListIterator cur = hwc.begin(id);
814 const HWComposer::LayerListIterator end = hwc.end(id);
815 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
816 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700817
Mathias Agopiane60b0682012-08-21 23:34:09 -0700818 if (CC_UNLIKELY(workListsDirty)) {
819 layer->setGeometry(hw, *cur);
820 if (mDebugDisableHWC || mDebugRegion) {
821 cur->setSkip(true);
822 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700823 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700824
Mathias Agopiane60b0682012-08-21 23:34:09 -0700825 /*
826 * update the per-frame h/w composer data for each layer
827 * and build the transparent region of the FB
828 */
829 layer->setPerFrameData(hw, *cur);
830 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700831 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700833 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700834 status_t err = hwc.prepare();
835 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
836 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700837}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700838
Mathias Agopiancd60f992012-08-16 16:28:27 -0700839void SurfaceFlinger::doComposition() {
840 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700841 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700842 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700843 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 if (hw->canDraw()) {
845 // transform the dirty region into this screen's coordinate space
846 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
847 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700849 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700850 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700851 hw->dirtyRegion.clear();
852 hw->flip(hw->swapRegion);
853 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700854 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700855 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700856 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700857 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700858 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700859}
860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861void SurfaceFlinger::postFramebuffer()
862{
Mathias Agopian841cde52012-03-01 15:44:37 -0800863 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800864
Mathias Agopiana44b0412011-10-16 18:46:35 -0700865 const nsecs_t now = systemTime();
866 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700867
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700868 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700869 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700870 // FIXME: EGL spec says:
871 // "surface must be bound to the calling thread's current context,
872 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700873 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700874 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700875 }
876
Mathias Agopian92a979a2012-08-02 18:32:23 -0700877 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700878 sp<const DisplayDevice> hw(mDisplays[dpy]);
879 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700880 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700881 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700882 int32_t id = hw->getHwcDisplayId();
883 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700884 HWComposer::LayerListIterator cur = hwc.begin(id);
885 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700886 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700887 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700888 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700889 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700890 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700891 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700892 }
Jesse Hallef194142012-06-14 14:45:17 -0700893 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800894 }
895
Mathias Agopiana44b0412011-10-16 18:46:35 -0700896 mLastSwapBufferTime = systemTime() - now;
897 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898}
899
Mathias Agopian87baae12012-07-31 12:38:26 -0700900void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901{
Mathias Agopian841cde52012-03-01 15:44:37 -0800902 ATRACE_CALL();
903
Mathias Agopianca4d3602011-05-19 15:38:14 -0700904 Mutex::Autolock _l(mStateLock);
905 const nsecs_t now = systemTime();
906 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907
Mathias Agopianca4d3602011-05-19 15:38:14 -0700908 // Here we're guaranteed that some transaction flags are set
909 // so we can call handleTransactionLocked() unconditionally.
910 // We call getTransactionFlags(), which will also clear the flags,
911 // with mStateLock held to guarantee that mCurrentState won't change
912 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700913
Mathias Agopiane57f2922012-08-09 16:29:12 -0700914 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700915 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700916
Mathias Agopianca4d3602011-05-19 15:38:14 -0700917 mLastTransactionTime = systemTime() - now;
918 mDebugInTransaction = 0;
919 invalidateHwcGeometry();
920 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700921}
922
Mathias Agopian87baae12012-07-31 12:38:26 -0700923void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700924{
925 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 const size_t count = currentLayers.size();
927
928 /*
929 * Traversal of the children
930 * (perform the transaction for each of them if needed)
931 */
932
Mathias Agopian3559b072012-08-15 13:46:03 -0700933 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700935 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
937 if (!trFlags) continue;
938
939 const uint32_t flags = layer->doTransaction(0);
940 if (flags & Layer::eVisibleRegion)
941 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 }
943 }
944
945 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700946 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 */
948
Mathias Agopiane57f2922012-08-09 16:29:12 -0700949 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700950 // here we take advantage of Vector's copy-on-write semantics to
951 // improve performance by skipping the transaction entirely when
952 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700953 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
954 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700955 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700957 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700958 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700959
960 // find the displays that were removed
961 // (ie: in drawing state but not in current state)
962 // also handle displays that changed
963 // (ie: displays that are in both lists)
964 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700965 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
966 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700967 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700968 if (!draw[i].isMainDisplay()) {
969 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 } else {
971 ALOGW("trying to remove the main display");
972 }
973 } else {
974 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700975 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700976 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700977 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700978 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700979 // recreating the DisplayDevice, so we just remove it
980 // from the drawing state, so that it get re-added
981 // below.
982 mDisplays.removeItem(display);
983 mDrawingState.displays.removeItemsAt(i);
984 dc--; i--;
985 // at this point we must loop to the next item
986 continue;
987 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700988
Mathias Agopian93997a82012-08-29 17:30:36 -0700989 const sp<DisplayDevice>& disp(getDisplayDevice(display));
990 if (disp != NULL) {
991 if (state.layerStack != draw[i].layerStack) {
992 disp->setLayerStack(state.layerStack);
993 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700994 if ((state.orientation != draw[i].orientation)
995 || (state.viewport != draw[i].viewport)
996 || (state.frame != draw[i].frame))
997 {
998 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -0700999 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001000 }
Andy McFadden69052052012-09-14 16:10:11 -07001001
1002 // Walk through all the layers in currentLayers,
1003 // and update their transform hint.
1004 //
1005 // TODO: we could be much more clever about which
1006 // layers we touch and how often we do these updates
1007 // (e.g. only touch the layers associated with this
1008 // display, and only on a rotation).
1009 for (size_t i = 0; i < count; i++) {
1010 const sp<LayerBase>& layerBase = currentLayers[i];
1011 layerBase->updateTransformHint();
1012 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001013 }
1014 }
1015 }
1016
1017 // find displays that were added
1018 // (ie: in current state but not in drawing state)
1019 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001020 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001021 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001022
1023 sp<FramebufferSurface> fbs;
1024 sp<SurfaceTextureClient> stc;
1025 if (!state.isVirtualDisplay()) {
1026
1027 ALOGE_IF(state.surface!=NULL,
1028 "adding a supported display, but rendering "
1029 "surface is provided (%p), ignoring it",
1030 state.surface.get());
1031
1032 // for supported (by hwc) displays we provide our
1033 // own rendering surface
1034 fbs = new FramebufferSurface(*mHwc);
1035 stc = new SurfaceTextureClient(
1036 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1037 } else {
1038 if (state.surface != NULL) {
1039 stc = new SurfaceTextureClient(state.surface);
1040 }
1041 }
1042
1043 const wp<IBinder>& display(curr.keyAt(i));
1044 if (stc != NULL) {
1045 sp<DisplayDevice> hw = new DisplayDevice(this,
1046 state.type, display, stc, fbs, mEGLConfig);
1047 hw->setLayerStack(state.layerStack);
1048 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001049 state.viewport, state.frame);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001050 mDisplays.add(display, hw);
1051 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1052 // notify the system that this display is now up
1053 // (note onScreenAcquired() is safe to call from
1054 // here because we're in the main thread)
1055 onScreenAcquired(hw);
1056 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001057 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001058 }
1059 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001061 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062
Mathias Agopian3559b072012-08-15 13:46:03 -07001063 /*
1064 * Perform our own transaction if needed
1065 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001066
Mathias Agopiancd60f992012-08-16 16:28:27 -07001067 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1068 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001069 // layers have been added
1070 mVisibleRegionsDirty = true;
1071 }
1072
1073 // some layers might have been removed, so
1074 // we need to update the regions they're exposing.
1075 if (mLayersRemoved) {
1076 mLayersRemoved = false;
1077 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001078 const size_t count = previousLayers.size();
1079 for (size_t i=0 ; i<count ; i++) {
1080 const sp<LayerBase>& layer(previousLayers[i]);
1081 if (currentLayers.indexOf(layer) < 0) {
1082 // this layer is not visible anymore
1083 // TODO: we could traverse the tree from front to back and
1084 // compute the actual visible region
1085 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001086 const Layer::State& s(layer->drawingState());
1087 Region visibleReg = s.transform.transform(
1088 Region(Rect(s.active.w, s.active.h)));
1089 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001090 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 }
1093
1094 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001095}
1096
1097void SurfaceFlinger::commitTransaction()
1098{
1099 if (!mLayersPendingRemoval.isEmpty()) {
1100 // Notify removed layers now that they can't be drawn from
1101 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1102 mLayersPendingRemoval[i]->onRemoved();
1103 }
1104 mLayersPendingRemoval.clear();
1105 }
1106
1107 mDrawingState = mCurrentState;
1108 mTransationPending = false;
1109 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110}
1111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001113 const LayerVector& currentLayers, uint32_t layerStack,
1114 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115{
Mathias Agopian841cde52012-03-01 15:44:37 -08001116 ATRACE_CALL();
1117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 Region aboveOpaqueLayers;
1119 Region aboveCoveredLayers;
1120 Region dirty;
1121
Mathias Agopian87baae12012-07-31 12:38:26 -07001122 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123
1124 size_t i = currentLayers.size();
1125 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001126 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127
1128 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001129 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130
Mathias Agopian87baae12012-07-31 12:38:26 -07001131 // only consider the layers on the given later stack
1132 if (s.layerStack != layerStack)
1133 continue;
1134
Mathias Agopianab028732010-03-16 16:41:46 -07001135 /*
1136 * opaqueRegion: area of a surface that is fully opaque.
1137 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001139
1140 /*
1141 * visibleRegion: area of a surface that is visible on screen
1142 * and not fully transparent. This is essentially the layer's
1143 * footprint minus the opaque regions above it.
1144 * Areas covered by a translucent surface are considered visible.
1145 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001147
1148 /*
1149 * coveredRegion: area of a surface that is covered by all
1150 * visible regions above it (which includes the translucent areas).
1151 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001153
1154
1155 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001156 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001157 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001158 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001160 if (!visibleRegion.isEmpty()) {
1161 // Remove the transparent area from the visible region
1162 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001163 Region transparentRegionScreen;
1164 const Transform tr(s.transform);
1165 if (tr.transformed()) {
1166 if (tr.preserveRects()) {
1167 // transform the transparent region
1168 transparentRegionScreen = tr.transform(s.transparentRegion);
1169 } else {
1170 // transformation too complex, can't do the
1171 // transparent region optimization.
1172 transparentRegionScreen.clear();
1173 }
1174 } else {
1175 transparentRegionScreen = s.transparentRegion;
1176 }
1177 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001178 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179
Mathias Agopianab028732010-03-16 16:41:46 -07001180 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001181 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001182 if (s.alpha==255 && !translucent &&
1183 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1184 // the opaque region is the layer's footprint
1185 opaqueRegion = visibleRegion;
1186 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 }
1188 }
1189
Mathias Agopianab028732010-03-16 16:41:46 -07001190 // Clip the covered region to the visible region
1191 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1192
1193 // Update aboveCoveredLayers for next (lower) layer
1194 aboveCoveredLayers.orSelf(visibleRegion);
1195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 // subtract the opaque region covered by the layers above us
1197 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198
1199 // compute this layer's dirty region
1200 if (layer->contentDirty) {
1201 // we need to invalidate the whole region
1202 dirty = visibleRegion;
1203 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001204 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 layer->contentDirty = false;
1206 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001207 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001208 * the exposed region consists of two components:
1209 * 1) what's VISIBLE now and was COVERED before
1210 * 2) what's EXPOSED now less what was EXPOSED before
1211 *
1212 * note that (1) is conservative, we start with the whole
1213 * visible region but only keep what used to be covered by
1214 * something -- which mean it may have been exposed.
1215 *
1216 * (2) handles areas that were not covered by anything but got
1217 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001218 */
Mathias Agopianab028732010-03-16 16:41:46 -07001219 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001220 const Region oldVisibleRegion = layer->visibleRegion;
1221 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001222 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1223 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
1225 dirty.subtractSelf(aboveOpaqueLayers);
1226
1227 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001228 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229
Mathias Agopianab028732010-03-16 16:41:46 -07001230 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 // Store the visible region is screen space
1234 layer->setVisibleRegion(visibleRegion);
1235 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 }
1237
Mathias Agopian87baae12012-07-31 12:38:26 -07001238 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239}
1240
Mathias Agopian87baae12012-07-31 12:38:26 -07001241void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1242 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001243 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001244 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1245 if (hw->getLayerStack() == layerStack) {
1246 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001247 }
1248 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001249}
1250
1251void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001253 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254
Mathias Agopian4fec8732012-06-29 14:12:52 -07001255 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001256 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001257 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001258 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001259 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001260 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001261 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001262 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001263 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001264
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001265 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266}
1267
Mathias Agopianad456f92011-01-13 17:53:01 -08001268void SurfaceFlinger::invalidateHwcGeometry()
1269{
1270 mHwWorkListDirty = true;
1271}
1272
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001273
Mathias Agopiancd60f992012-08-16 16:28:27 -07001274void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001275 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276{
Mathias Agopian87baae12012-07-31 12:38:26 -07001277 Region dirtyRegion(inDirtyRegion);
1278
Mathias Agopianb8a55602009-06-26 19:06:36 -07001279 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001280 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281
Mathias Agopian42977342012-08-05 00:40:46 -07001282 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001283 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001284 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1285 // takes a rectangle, we must make sure to update that whole
1286 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001287 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001288 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001289 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001290 // We need to redraw the rectangle that will be updated
1291 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001292 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001293 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001294 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001295 } else {
1296 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001297 dirtyRegion.set(hw->bounds());
1298 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 }
1300 }
1301
Mathias Agopiancd60f992012-08-16 16:28:27 -07001302 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001304 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001305 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001306
1307 // swap buffers (presentation)
1308 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309}
1310
Mathias Agopiancd60f992012-08-16 16:28:27 -07001311void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001312{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001313 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001314 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001315 HWComposer::LayerListIterator cur = hwc.begin(id);
1316 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001317
Mathias Agopian85d751c2012-08-29 16:59:24 -07001318 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1319 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001320 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001321
1322 // set the frame buffer
1323 glMatrixMode(GL_MODELVIEW);
1324 glLoadIdentity();
1325
1326 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001327 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001328 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001329 // when using overlays, we assume a fully transparent framebuffer
1330 // NOTE: we could reduce how much we need to clear, for instance
1331 // remove where there are opaque FB layers. however, on some
1332 // GPUs doing a "clean slate" glClear might be more efficient.
1333 // We'll revisit later if needed.
1334 glClearColor(0, 0, 0, 0);
1335 glClear(GL_COLOR_BUFFER_BIT);
1336 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001337 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001338 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001339 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001340 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001341 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001342 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001343 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001344 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001345
Mathias Agopian85d751c2012-08-29 16:59:24 -07001346 /*
1347 * and then, render the layers targeted at the framebuffer
1348 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001349
Mathias Agopian85d751c2012-08-29 16:59:24 -07001350 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1351 const size_t count = layers.size();
1352 const Transform& tr = hw->getTransform();
1353 if (cur != end) {
1354 // we're using h/w composer
1355 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001356 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001357 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001358 if (!clip.isEmpty()) {
1359 switch (cur->getCompositionType()) {
1360 case HWC_OVERLAY: {
1361 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1362 && i
1363 && layer->isOpaque()
1364 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001365 // never clear the very first layer since we're
1366 // guaranteed the FB is already cleared
1367 layer->clearWithOpenGL(hw, clip);
1368 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001369 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001370 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001371 case HWC_FRAMEBUFFER: {
1372 layer->draw(hw, clip);
1373 break;
1374 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001375 case HWC_FRAMEBUFFER_TARGET: {
1376 // this should not happen as the iterator shouldn't
1377 // let us get there.
1378 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1379 break;
1380 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001381 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001382 }
1383 layer->setAcquireFence(hw, *cur);
1384 }
1385 } else {
1386 // we're not using h/w composer
1387 for (size_t i=0 ; i<count ; ++i) {
1388 const sp<LayerBase>& layer(layers[i]);
1389 const Region clip(dirty.intersect(
1390 tr.transform(layer->visibleRegion)));
1391 if (!clip.isEmpty()) {
1392 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001393 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001394 }
1395 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396}
1397
Mathias Agopian55801e42012-08-27 18:54:24 -07001398void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1399 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001401 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001402 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001403 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001404 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001405
Mathias Agopian55801e42012-08-27 18:54:24 -07001406 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001407 Region::const_iterator it = region.begin();
1408 Region::const_iterator const end = region.end();
1409 while (it != end) {
1410 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001411 GLfloat vertices[][2] = {
1412 { r.left, height - r.top },
1413 { r.left, height - r.bottom },
1414 { r.right, height - r.bottom },
1415 { r.right, height - r.top }
1416 };
1417 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001418 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1419 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420}
1421
Mathias Agopian96f08192010-06-02 23:28:45 -07001422ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1423 const sp<LayerBaseClient>& lbc)
1424{
Mathias Agopian96f08192010-06-02 23:28:45 -07001425 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001426 size_t name = client->attachLayer(lbc);
1427
Mathias Agopian96f08192010-06-02 23:28:45 -07001428 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001429 Mutex::Autolock _l(mStateLock);
1430 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001431
Mathias Agopian4f113742011-05-03 16:21:41 -07001432 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001433}
1434
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001435status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436{
1437 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001438 status_t err = purgatorizeLayer_l(layer);
1439 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001440 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001441 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442}
1443
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001444status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445{
1446 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1447 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001448 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 return NO_ERROR;
1450 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001451 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452}
1453
Mathias Agopian9a112062009-04-17 19:36:26 -07001454status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1455{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001456 // First add the layer to the purgatory list, which makes sure it won't
1457 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001458 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001459 if (err >= 0) {
1460 mLayerPurgatory.add(layerBase);
1461 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001462
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001463 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001464
Mathias Agopian3d579642009-06-04 18:46:21 -07001465 // it's possible that we don't find a layer, because it might
1466 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001467 // from the user because there is a race between Client::destroySurface(),
1468 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001469 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1470}
1471
Mathias Agopiandea20b12011-05-03 17:04:02 -07001472uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1473{
1474 return android_atomic_release_load(&mTransactionFlags);
1475}
1476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1478{
1479 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1480}
1481
Mathias Agopianbb641242010-05-18 17:06:55 -07001482uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483{
1484 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1485 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001486 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
1488 return old;
1489}
1490
Mathias Agopian8b33f032012-07-24 20:43:54 -07001491void SurfaceFlinger::setTransactionState(
1492 const Vector<ComposerState>& state,
1493 const Vector<DisplayState>& displays,
1494 uint32_t flags)
1495{
Mathias Agopian698c0872011-06-28 19:09:31 -07001496 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001497 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001498
1499 size_t count = displays.size();
1500 for (size_t i=0 ; i<count ; i++) {
1501 const DisplayState& s(displays[i]);
1502 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001503 }
1504
Mathias Agopiane57f2922012-08-09 16:29:12 -07001505 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001506 for (size_t i=0 ; i<count ; i++) {
1507 const ComposerState& s(state[i]);
1508 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001509 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001510 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001511
Mathias Agopian386aa982011-11-07 21:58:03 -08001512 if (transactionFlags) {
1513 // this triggers the transaction
1514 setTransactionFlags(transactionFlags);
1515
1516 // if this is a synchronous transaction, wait for it to take effect
1517 // before returning.
1518 if (flags & eSynchronous) {
1519 mTransationPending = true;
1520 }
1521 while (mTransationPending) {
1522 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1523 if (CC_UNLIKELY(err != NO_ERROR)) {
1524 // just in case something goes wrong in SF, return to the
1525 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001526 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001527 mTransationPending = false;
1528 break;
1529 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001530 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 }
1532}
1533
Mathias Agopiane57f2922012-08-09 16:29:12 -07001534uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1535{
1536 uint32_t flags = 0;
1537 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001538 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001539 const uint32_t what = s.what;
1540 if (what & DisplayState::eSurfaceChanged) {
1541 if (disp.surface->asBinder() != s.surface->asBinder()) {
1542 disp.surface = s.surface;
1543 flags |= eDisplayTransactionNeeded;
1544 }
1545 }
1546 if (what & DisplayState::eLayerStackChanged) {
1547 if (disp.layerStack != s.layerStack) {
1548 disp.layerStack = s.layerStack;
1549 flags |= eDisplayTransactionNeeded;
1550 }
1551 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001552 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001553 if (disp.orientation != s.orientation) {
1554 disp.orientation = s.orientation;
1555 flags |= eDisplayTransactionNeeded;
1556 }
1557 if (disp.frame != s.frame) {
1558 disp.frame = s.frame;
1559 flags |= eDisplayTransactionNeeded;
1560 }
1561 if (disp.viewport != s.viewport) {
1562 disp.viewport = s.viewport;
1563 flags |= eDisplayTransactionNeeded;
1564 }
1565 }
1566 }
1567 return flags;
1568}
1569
1570uint32_t SurfaceFlinger::setClientStateLocked(
1571 const sp<Client>& client,
1572 const layer_state_t& s)
1573{
1574 uint32_t flags = 0;
1575 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1576 if (layer != 0) {
1577 const uint32_t what = s.what;
1578 if (what & layer_state_t::ePositionChanged) {
1579 if (layer->setPosition(s.x, s.y))
1580 flags |= eTraversalNeeded;
1581 }
1582 if (what & layer_state_t::eLayerChanged) {
1583 // NOTE: index needs to be calculated before we update the state
1584 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1585 if (layer->setLayer(s.z)) {
1586 mCurrentState.layersSortedByZ.removeAt(idx);
1587 mCurrentState.layersSortedByZ.add(layer);
1588 // we need traversal (state changed)
1589 // AND transaction (list changed)
1590 flags |= eTransactionNeeded|eTraversalNeeded;
1591 }
1592 }
1593 if (what & layer_state_t::eSizeChanged) {
1594 if (layer->setSize(s.w, s.h)) {
1595 flags |= eTraversalNeeded;
1596 }
1597 }
1598 if (what & layer_state_t::eAlphaChanged) {
1599 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1600 flags |= eTraversalNeeded;
1601 }
1602 if (what & layer_state_t::eMatrixChanged) {
1603 if (layer->setMatrix(s.matrix))
1604 flags |= eTraversalNeeded;
1605 }
1606 if (what & layer_state_t::eTransparentRegionChanged) {
1607 if (layer->setTransparentRegionHint(s.transparentRegion))
1608 flags |= eTraversalNeeded;
1609 }
1610 if (what & layer_state_t::eVisibilityChanged) {
1611 if (layer->setFlags(s.flags, s.mask))
1612 flags |= eTraversalNeeded;
1613 }
1614 if (what & layer_state_t::eCropChanged) {
1615 if (layer->setCrop(s.crop))
1616 flags |= eTraversalNeeded;
1617 }
1618 if (what & layer_state_t::eLayerStackChanged) {
1619 // NOTE: index needs to be calculated before we update the state
1620 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1621 if (layer->setLayerStack(s.layerStack)) {
1622 mCurrentState.layersSortedByZ.removeAt(idx);
1623 mCurrentState.layersSortedByZ.add(layer);
1624 // we need traversal (state changed)
1625 // AND transaction (list changed)
1626 flags |= eTransactionNeeded|eTraversalNeeded;
1627 }
1628 }
1629 }
1630 return flags;
1631}
1632
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001633sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001634 ISurfaceComposerClient::surface_data_t* params,
1635 const String8& name,
1636 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001637 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 uint32_t flags)
1639{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001640 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001641 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001642
1643 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001644 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001645 int(w), int(h));
1646 return surfaceHandle;
1647 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001648
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001649 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001650 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1651 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001652 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001654 case ISurfaceComposerClient::eFXSurfaceBlur:
1655 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001656 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001658 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001659 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001660 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 }
1662
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001664 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001665 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001666 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001668 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001669 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001670 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001671 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001672 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 }
1674
1675 return surfaceHandle;
1676}
1677
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001678sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001679 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001680 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001681 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682{
1683 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001684 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 case PIXEL_FORMAT_TRANSPARENT:
1686 case PIXEL_FORMAT_TRANSLUCENT:
1687 format = PIXEL_FORMAT_RGBA_8888;
1688 break;
1689 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001690#ifdef NO_RGBX_8888
1691 format = PIXEL_FORMAT_RGB_565;
1692#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001693 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001694#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 break;
1696 }
1697
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001698#ifdef NO_RGBX_8888
1699 if (format == PIXEL_FORMAT_RGBX_8888)
1700 format = PIXEL_FORMAT_RGBA_8888;
1701#endif
1702
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001703 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001704 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001705 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001706 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001707 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 }
1709 return layer;
1710}
1711
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001712sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001713 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001714 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001716 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001717 return layer;
1718}
1719
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001720sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001721 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001722 uint32_t w, uint32_t h, uint32_t flags)
1723{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001724 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 return layer;
1726}
1727
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001728status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729{
Mathias Agopian9a112062009-04-17 19:36:26 -07001730 /*
1731 * called by the window manager, when a surface should be marked for
1732 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001733 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001734 * The surface is removed from the current and drawing lists, but placed
1735 * in the purgatory queue, so it's not destroyed right-away (we need
1736 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001737 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001738
Mathias Agopian48d819a2009-09-10 19:41:18 -07001739 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001740 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001741 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001742
Mathias Agopian48d819a2009-09-10 19:41:18 -07001743 if (layer != 0) {
1744 err = purgatorizeLayer_l(layer);
1745 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001746 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001747 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001748 }
1749 return err;
1750}
1751
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001752status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001753{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001754 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001755 status_t err = NO_ERROR;
1756 sp<LayerBaseClient> l(layer.promote());
1757 if (l != NULL) {
1758 Mutex::Autolock _l(mStateLock);
1759 err = removeLayer_l(l);
1760 if (err == NAME_NOT_FOUND) {
1761 // The surface wasn't in the current list, which means it was
1762 // removed already, which means it is in the purgatory,
1763 // and need to be removed from there.
1764 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001765 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001766 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001767 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001768 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001769 "error removing layer=%p (%s)", l.get(), strerror(-err));
1770 }
1771 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772}
1773
Mathias Agopianb60314a2012-04-10 22:09:54 -07001774// ---------------------------------------------------------------------------
1775
Andy McFadden13a082e2012-08-24 10:16:42 -07001776void SurfaceFlinger::onInitializeDisplays() {
1777 // reset screen orientation
1778 Vector<ComposerState> state;
1779 Vector<DisplayState> displays;
1780 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001781 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001782 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001783 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001784 d.frame.makeInvalid();
1785 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001786 displays.add(d);
1787 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001788 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001789}
1790
1791void SurfaceFlinger::initializeDisplays() {
1792 class MessageScreenInitialized : public MessageBase {
1793 SurfaceFlinger* flinger;
1794 public:
1795 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1796 virtual bool handler() {
1797 flinger->onInitializeDisplays();
1798 return true;
1799 }
1800 };
1801 sp<MessageBase> msg = new MessageScreenInitialized(this);
1802 postMessageAsync(msg); // we may be called from main thread, use async message
1803}
1804
1805
Mathias Agopiancde87a32012-09-13 14:09:01 -07001806void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001807 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001808 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001809 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001810 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1811 // FIXME: eventthread only knows about the main display right now
1812 mEventThread->onScreenAcquired();
1813 }
Mathias Agopian20128302012-08-13 18:32:13 -07001814 mVisibleRegionsDirty = true;
1815 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816}
1817
Mathias Agopiancde87a32012-09-13 14:09:01 -07001818void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001819 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001820 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001821 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1822 // FIXME: eventthread only knows about the main display right now
1823 mEventThread->onScreenReleased();
1824 }
Mathias Agopian42977342012-08-05 00:40:46 -07001825 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001826 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001827 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001828 // from this point on, SF will stop drawing
1829 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001830}
1831
Colin Cross8e533062012-06-07 13:17:52 -07001832void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001833 class MessageScreenAcquired : public MessageBase {
1834 SurfaceFlinger* flinger;
1835 public:
1836 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1837 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001838 // FIXME: should this be per-display?
1839 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001840 return true;
1841 }
1842 };
1843 sp<MessageBase> msg = new MessageScreenAcquired(this);
1844 postMessageSync(msg);
1845}
1846
Colin Cross8e533062012-06-07 13:17:52 -07001847void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001848 class MessageScreenReleased : public MessageBase {
1849 SurfaceFlinger* flinger;
1850 public:
1851 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1852 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001853 // FIXME: should this be per-display?
1854 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001855 return true;
1856 }
1857 };
1858 sp<MessageBase> msg = new MessageScreenReleased(this);
1859 postMessageSync(msg);
1860}
1861
1862// ---------------------------------------------------------------------------
1863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001864status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1865{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001866 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001867 char buffer[SIZE];
1868 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001869
1870 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 snprintf(buffer, SIZE, "Permission Denial: "
1872 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1873 IPCThreadState::self()->getCallingPid(),
1874 IPCThreadState::self()->getCallingUid());
1875 result.append(buffer);
1876 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001877 // Try to get the main lock, but don't insist if we can't
1878 // (this would indicate SF is stuck, but we want to be able to
1879 // print something in dumpsys).
1880 int retry = 3;
1881 while (mStateLock.tryLock()<0 && --retry>=0) {
1882 usleep(1000000);
1883 }
1884 const bool locked(retry >= 0);
1885 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001886 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001887 "SurfaceFlinger appears to be unresponsive, "
1888 "dumping anyways (no locks held)\n");
1889 result.append(buffer);
1890 }
1891
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001892 bool dumpAll = true;
1893 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001894 size_t numArgs = args.size();
1895 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001896 if ((index < numArgs) &&
1897 (args[index] == String16("--list"))) {
1898 index++;
1899 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001900 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001901 }
1902
1903 if ((index < numArgs) &&
1904 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001905 index++;
1906 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001907 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001908 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001909
1910 if ((index < numArgs) &&
1911 (args[index] == String16("--latency-clear"))) {
1912 index++;
1913 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001914 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001915 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001916 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001917
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001918 if (dumpAll) {
1919 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001920 }
1921
Mathias Agopian9795c422009-08-26 16:36:26 -07001922 if (locked) {
1923 mStateLock.unlock();
1924 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 }
1926 write(fd, result.string(), result.size());
1927 return NO_ERROR;
1928}
1929
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001930void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1931 String8& result, char* buffer, size_t SIZE) const
1932{
1933 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1934 const size_t count = currentLayers.size();
1935 for (size_t i=0 ; i<count ; i++) {
1936 const sp<LayerBase>& layer(currentLayers[i]);
1937 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1938 result.append(buffer);
1939 }
1940}
1941
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001942void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1943 String8& result, char* buffer, size_t SIZE) const
1944{
1945 String8 name;
1946 if (index < args.size()) {
1947 name = String8(args[index]);
1948 index++;
1949 }
1950
1951 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1952 const size_t count = currentLayers.size();
1953 for (size_t i=0 ; i<count ; i++) {
1954 const sp<LayerBase>& layer(currentLayers[i]);
1955 if (name.isEmpty()) {
1956 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1957 result.append(buffer);
1958 }
1959 if (name.isEmpty() || (name == layer->getName())) {
1960 layer->dumpStats(result, buffer, SIZE);
1961 }
1962 }
1963}
1964
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001965void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1966 String8& result, char* buffer, size_t SIZE) const
1967{
1968 String8 name;
1969 if (index < args.size()) {
1970 name = String8(args[index]);
1971 index++;
1972 }
1973
1974 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1975 const size_t count = currentLayers.size();
1976 for (size_t i=0 ; i<count ; i++) {
1977 const sp<LayerBase>& layer(currentLayers[i]);
1978 if (name.isEmpty() || (name == layer->getName())) {
1979 layer->clearStats();
1980 }
1981 }
1982}
1983
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001984void SurfaceFlinger::dumpAllLocked(
1985 String8& result, char* buffer, size_t SIZE) const
1986{
1987 // figure out if we're stuck somewhere
1988 const nsecs_t now = systemTime();
1989 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1990 const nsecs_t inTransaction(mDebugInTransaction);
1991 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1992 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1993
1994 /*
1995 * Dump the visible layer list
1996 */
1997 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1998 const size_t count = currentLayers.size();
1999 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2000 result.append(buffer);
2001 for (size_t i=0 ; i<count ; i++) {
2002 const sp<LayerBase>& layer(currentLayers[i]);
2003 layer->dump(result, buffer, SIZE);
2004 }
2005
2006 /*
2007 * Dump the layers in the purgatory
2008 */
2009
2010 const size_t purgatorySize = mLayerPurgatory.size();
2011 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2012 result.append(buffer);
2013 for (size_t i=0 ; i<purgatorySize ; i++) {
2014 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2015 layer->shortDump(result, buffer, SIZE);
2016 }
2017
2018 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002019 * Dump Display state
2020 */
2021
2022 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2023 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
2024 snprintf(buffer, SIZE,
2025 "+ DisplayDevice[%u]\n"
Jeff Brown4fb39992012-09-07 12:55:10 -07002026 " type=%x, layerStack=%u, (%4dx%4d), orient=%2d (type=%08x), "
Jeff Brown6e220a62012-09-13 19:22:41 -07002027 "flips=%u, secure=%d, numLayers=%u, v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], "
2028 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002029 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002030 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002031 hw->getWidth(), hw->getHeight(),
2032 hw->getOrientation(), hw->getTransform().getType(),
2033 hw->getPageFlipCount(),
2034 hw->getSecureLayerVisible(),
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002035 hw->getVisibleLayersSortedByZ().size(),
2036 hw->getViewport().left, hw->getViewport().top, hw->getViewport().right, hw->getViewport().bottom,
Jeff Brown6e220a62012-09-13 19:22:41 -07002037 hw->getFrame().left, hw->getFrame().top, hw->getFrame().right, hw->getFrame().bottom,
2038 hw->getTransform()[0][0], hw->getTransform()[1][0], hw->getTransform()[2][0],
2039 hw->getTransform()[0][1], hw->getTransform()[1][1], hw->getTransform()[2][1],
2040 hw->getTransform()[0][2], hw->getTransform()[1][2], hw->getTransform()[2][2]);
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002041
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002042 result.append(buffer);
2043 }
2044
2045 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002046 * Dump SurfaceFlinger global state
2047 */
2048
2049 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2050 result.append(buffer);
2051
Mathias Agopian888c8222012-08-04 21:10:38 -07002052 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002053 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002054 const GLExtensions& extensions(GLExtensions::getInstance());
2055 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2056 extensions.getVendor(),
2057 extensions.getRenderer(),
2058 extensions.getVersion());
2059 result.append(buffer);
2060
2061 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002062 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002063 result.append(buffer);
2064
2065 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2066 result.append(buffer);
2067
Mathias Agopian42977342012-08-05 00:40:46 -07002068 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002069 snprintf(buffer, SIZE,
2070 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002071 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002072 result.append(buffer);
2073 snprintf(buffer, SIZE,
2074 " last eglSwapBuffers() time: %f us\n"
2075 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002076 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002077 " refresh-rate : %f fps\n"
2078 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002079 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002080 mLastSwapBufferTime/1000.0,
2081 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002082 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002083 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2084 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2085 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002086 result.append(buffer);
2087
2088 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2089 inSwapBuffersDuration/1000.0);
2090 result.append(buffer);
2091
2092 snprintf(buffer, SIZE, " transaction time: %f us\n",
2093 inTransactionDuration/1000.0);
2094 result.append(buffer);
2095
2096 /*
2097 * VSYNC state
2098 */
2099 mEventThread->dump(result, buffer, SIZE);
2100
2101 /*
2102 * Dump HWComposer state
2103 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002104 snprintf(buffer, SIZE, "h/w composer state:\n");
2105 result.append(buffer);
2106 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2107 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2108 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2109 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002110 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002111
2112 /*
2113 * Dump gralloc state
2114 */
2115 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2116 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002117 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002118}
2119
Keun young Park63f165f2012-08-31 10:53:36 -07002120bool SurfaceFlinger::startDdmConnection()
2121{
2122 void* libddmconnection_dso =
2123 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2124 if (!libddmconnection_dso) {
2125 return false;
2126 }
2127 void (*DdmConnection_start)(const char* name);
2128 DdmConnection_start =
2129 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2130 if (!DdmConnection_start) {
2131 dlclose(libddmconnection_dso);
2132 return false;
2133 }
2134 (*DdmConnection_start)(getServiceName());
2135 return true;
2136}
2137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002138status_t SurfaceFlinger::onTransact(
2139 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2140{
2141 switch (code) {
2142 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002143 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002145 case BLANK:
2146 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002147 {
2148 // codes that require permission check
2149 IPCThreadState* ipc = IPCThreadState::self();
2150 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002151 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002152 if ((uid != AID_GRAPHICS) &&
2153 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002154 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002155 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2156 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002157 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002158 break;
2159 }
2160 case CAPTURE_SCREEN:
2161 {
2162 // codes that require permission check
2163 IPCThreadState* ipc = IPCThreadState::self();
2164 const int pid = ipc->getCallingPid();
2165 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002166 if ((uid != AID_GRAPHICS) &&
2167 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002168 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002169 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2170 return PERMISSION_DENIED;
2171 }
2172 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002173 }
2174 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002176 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2177 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002178 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002179 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002180 IPCThreadState* ipc = IPCThreadState::self();
2181 const int pid = ipc->getCallingPid();
2182 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002183 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002184 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185 return PERMISSION_DENIED;
2186 }
2187 int n;
2188 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002189 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002190 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002191 return NO_ERROR;
2192 case 1002: // SHOW_UPDATES
2193 n = data.readInt32();
2194 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002195 invalidateHwcGeometry();
2196 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002197 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002198 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002199 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002200 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002202 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002203 setTransactionFlags(
2204 eTransactionNeeded|
2205 eDisplayTransactionNeeded|
2206 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002207 return NO_ERROR;
2208 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002209 case 1006:{ // send empty update
2210 signalRefresh();
2211 return NO_ERROR;
2212 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002213 case 1008: // toggle use of hw composer
2214 n = data.readInt32();
2215 mDebugDisableHWC = n ? 1 : 0;
2216 invalidateHwcGeometry();
2217 repaintEverything();
2218 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002219 case 1009: // toggle use of transform hint
2220 n = data.readInt32();
2221 mDebugDisableTransformHint = n ? 1 : 0;
2222 invalidateHwcGeometry();
2223 repaintEverything();
2224 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002226 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227 reply->writeInt32(0);
2228 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002229 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002230 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231 return NO_ERROR;
2232 case 1013: {
2233 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002234 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2235 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002236 }
2237 return NO_ERROR;
2238 }
2239 }
2240 return err;
2241}
2242
Mathias Agopian53331da2011-08-22 21:44:41 -07002243void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002244 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002245 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002246}
2247
Mathias Agopian59119e62010-10-11 12:37:43 -07002248// ---------------------------------------------------------------------------
2249
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002250status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002251 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2252{
2253 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002254 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002255}
2256
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002257status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002258 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002259{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002260 ATRACE_CALL();
2261
Mathias Agopian59119e62010-10-11 12:37:43 -07002262 if (!GLExtensions::getInstance().haveFramebufferObject())
2263 return INVALID_OPERATION;
2264
2265 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002266 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2267 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002268 const uint32_t hw_w = hw->getWidth();
2269 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002270 GLfloat u = 1;
2271 GLfloat v = 1;
2272
2273 // make sure to clear all GL error flags
2274 while ( glGetError() != GL_NO_ERROR ) ;
2275
2276 // create a FBO
2277 GLuint name, tname;
2278 glGenTextures(1, &tname);
2279 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002282 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2283 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002284 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002285 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002286 GLint tw = (2 << (31 - clz(hw_w)));
2287 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002288 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2289 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002290 u = GLfloat(hw_w) / tw;
2291 v = GLfloat(hw_h) / th;
2292 }
2293 glGenFramebuffersOES(1, &name);
2294 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002295 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2296 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002297
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002298 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002299 glDisable(GL_TEXTURE_EXTERNAL_OES);
2300 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 glClearColor(0,0,0,1);
2302 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002303 glMatrixMode(GL_MODELVIEW);
2304 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002305 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002306 const size_t count = layers.size();
2307 for (size_t i=0 ; i<count ; ++i) {
2308 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002309 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002310 }
2311
Mathias Agopian42977342012-08-05 00:40:46 -07002312 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002313
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002314 // back to main framebuffer
2315 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002316 glDeleteFramebuffersOES(1, &name);
2317
2318 *textureName = tname;
2319 *uOut = u;
2320 *vOut = v;
2321 return NO_ERROR;
2322}
2323
2324// ---------------------------------------------------------------------------
2325
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002326status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002327 sp<IMemoryHeap>* heap,
2328 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002329 uint32_t sw, uint32_t sh,
2330 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002332 ATRACE_CALL();
2333
Mathias Agopian74c40c02010-09-29 13:02:36 -07002334 status_t result = PERMISSION_DENIED;
2335
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002336 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002338 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002339
2340 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002341 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002342 const uint32_t hw_w = hw->getWidth();
2343 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002345 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002346 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002347 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002348 return PERMISSION_DENIED;
2349 }
2350
2351 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002352 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002353 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002354 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355
2356 sw = (!sw) ? hw_w : sw;
2357 sh = (!sh) ? hw_h : sh;
2358 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002359 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002361// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2362// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002363
Mathias Agopian74c40c02010-09-29 13:02:36 -07002364 // make sure to clear all GL error flags
2365 while ( glGetError() != GL_NO_ERROR ) ;
2366
2367 // create a FBO
2368 GLuint name, tname;
2369 glGenRenderbuffersOES(1, &tname);
2370 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2371 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002372
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 glGenFramebuffersOES(1, &name);
2374 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2375 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2376 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2377
2378 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002379
Mathias Agopian74c40c02010-09-29 13:02:36 -07002380 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2381
2382 // invert everything, b/c glReadPixel() below will invert the FB
2383 glViewport(0, 0, sw, sh);
2384 glMatrixMode(GL_PROJECTION);
2385 glPushMatrix();
2386 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002387 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 glMatrixMode(GL_MODELVIEW);
2389
2390 // redraw the screen entirely...
2391 glClearColor(0,0,0,1);
2392 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002393
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002394 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002395 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002396 for (size_t i=0 ; i<count ; ++i) {
2397 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002398 const uint32_t z = layer->drawingState().z;
2399 if (z >= minLayerZ && z <= maxLayerZ) {
2400 if (filtering) layer->setFiltering(true);
2401 layer->draw(hw);
2402 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002403 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002404 }
2405
Mathias Agopian74c40c02010-09-29 13:02:36 -07002406 // check for errors and return screen capture
2407 if (glGetError() != GL_NO_ERROR) {
2408 // error while rendering
2409 result = INVALID_OPERATION;
2410 } else {
2411 // allocate shared memory large enough to hold the
2412 // screen capture
2413 sp<MemoryHeapBase> base(
2414 new MemoryHeapBase(size, 0, "screen-capture") );
2415 void* const ptr = base->getBase();
2416 if (ptr) {
2417 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002418 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2420 if (glGetError() == GL_NO_ERROR) {
2421 *heap = base;
2422 *w = sw;
2423 *h = sh;
2424 *f = PIXEL_FORMAT_RGBA_8888;
2425 result = NO_ERROR;
2426 }
2427 } else {
2428 result = NO_MEMORY;
2429 }
2430 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431 glViewport(0, 0, hw_w, hw_h);
2432 glMatrixMode(GL_PROJECTION);
2433 glPopMatrix();
2434 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002435 } else {
2436 result = BAD_VALUE;
2437 }
2438
2439 // release FBO resources
2440 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2441 glDeleteRenderbuffersOES(1, &tname);
2442 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002443
Mathias Agopian42977342012-08-05 00:40:46 -07002444 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002445
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002446// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002447
Mathias Agopian74c40c02010-09-29 13:02:36 -07002448 return result;
2449}
2450
2451
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002452status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002453 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002454 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002455 uint32_t sw, uint32_t sh,
2456 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002457{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002458 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002459 return BAD_VALUE;
2460
2461 if (!GLExtensions::getInstance().haveFramebufferObject())
2462 return INVALID_OPERATION;
2463
2464 class MessageCaptureScreen : public MessageBase {
2465 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002466 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002467 sp<IMemoryHeap>* heap;
2468 uint32_t* w;
2469 uint32_t* h;
2470 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002471 uint32_t sw;
2472 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002473 uint32_t minLayerZ;
2474 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002475 status_t result;
2476 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002477 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002478 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002479 uint32_t sw, uint32_t sh,
2480 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002481 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002482 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2483 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2484 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002485 {
2486 }
2487 status_t getResult() const {
2488 return result;
2489 }
2490 virtual bool handler() {
2491 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002492 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002493 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002494 return true;
2495 }
2496 };
2497
2498 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002499 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002500 status_t res = postMessageSync(msg);
2501 if (res == NO_ERROR) {
2502 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2503 }
2504 return res;
2505}
2506
2507// ---------------------------------------------------------------------------
2508
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002509SurfaceFlinger::LayerVector::LayerVector() {
2510}
2511
2512SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2513 : SortedVector<sp<LayerBase> >(rhs) {
2514}
2515
2516int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2517 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002518{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002519 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002520 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2521 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002522
2523 uint32_t ls = l->currentState().layerStack;
2524 uint32_t rs = r->currentState().layerStack;
2525 if (ls != rs)
2526 return ls - rs;
2527
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002528 uint32_t lz = l->currentState().z;
2529 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002530 if (lz != rz)
2531 return lz - rz;
2532
2533 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002534}
2535
2536// ---------------------------------------------------------------------------
2537
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002538SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2539 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002540}
2541
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002542SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2543 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002544 viewport.makeInvalid();
2545 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002546}
2547
2548// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002549
Jamie Gennis9a78c902011-01-12 18:30:40 -08002550GraphicBufferAlloc::GraphicBufferAlloc() {}
2551
2552GraphicBufferAlloc::~GraphicBufferAlloc() {}
2553
2554sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002555 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002556 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2557 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002558 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002559 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002560 if (err == NO_MEMORY) {
2561 GraphicBuffer::dumpAllocationsToSystemLog();
2562 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002563 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002564 "failed (%s), handle=%p",
2565 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002566 return 0;
2567 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002568 return graphicBuffer;
2569}
2570
Jamie Gennis9a78c902011-01-12 18:30:40 -08002571// ---------------------------------------------------------------------------
2572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002573}; // namespace android