blob: f62f0759807ba9fd9aef3cd1f0ed58d35a4dc52d [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
248 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
249 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
250 // is supported by the implementation. we can only be in this case
251 // if we have HWC 1.1
252 *outConfig = configs[0];
253 delete [] configs;
254 return NO_ERROR;
255 }
256
Mathias Agopiana4912602012-07-12 14:25:33 -0700257 for (int i=0 ; i<n ; i++) {
258 EGLint nativeVisualId = 0;
259 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
260 if (nativeVisualId>0 && format == nativeVisualId) {
261 *outConfig = configs[i];
262 delete [] configs;
263 return NO_ERROR;
264 }
265 }
266 delete [] configs;
267 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268}
269
Mathias Agopiana4912602012-07-12 14:25:33 -0700270EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
271 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
272 // it is to be used with WIFI displays
273 EGLConfig config;
274 EGLint dummy;
275 status_t err;
276 EGLint attribs[] = {
277 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiancde87a32012-09-13 14:09:01 -0700278 EGL_RED_SIZE, 8,
279 EGL_GREEN_SIZE, 8,
280 EGL_BLUE_SIZE, 8,
281 // EGL_RECORDABLE_ANDROID must be last so that we can retry
282 // without it easily (see below)
Mathias Agopiana4912602012-07-12 14:25:33 -0700283 EGL_RECORDABLE_ANDROID, EGL_TRUE,
284 EGL_NONE
285 };
286 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
287 if (err) {
288 // maybe we failed because of EGL_RECORDABLE_ANDROID
289 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
Mathias Agopiancde87a32012-09-13 14:09:01 -0700290 attribs[NELEM(attribs) - 3] = EGL_NONE;
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
292 }
293 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
294 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
295 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
296 }
297 return config;
298}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
301 // Also create our EGLContext
302 EGLint contextAttributes[] = {
303#ifdef EGL_IMG_context_priority
304#ifdef HAS_CONTEXT_PRIORITY
305#warning "using EGL_IMG_context_priority"
306 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
307#endif
308#endif
309 EGL_NONE, EGL_NONE
310 };
311 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
312 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
313 return ctxt;
314}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
Mathias Agopiancde87a32012-09-13 14:09:01 -0700316void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700317 GLExtensions& extensions(GLExtensions::getInstance());
318 extensions.initWithGLStrings(
319 glGetString(GL_VENDOR),
320 glGetString(GL_RENDERER),
321 glGetString(GL_VERSION),
322 glGetString(GL_EXTENSIONS),
323 eglQueryString(display, EGL_VENDOR),
324 eglQueryString(display, EGL_VERSION),
325 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700326
Mathias Agopiana4912602012-07-12 14:25:33 -0700327 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
328 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700331 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 glShadeModel(GL_FLAT);
334 glDisable(GL_DITHER);
335 glDisable(GL_CULL_FACE);
336
Mathias Agopiana4912602012-07-12 14:25:33 -0700337 struct pack565 {
338 inline uint16_t operator() (int r, int g, int b) const {
339 return (r<<11)|(g<<5)|b;
340 }
341 } pack565;
342
Jamie Gennis9575f602011-10-07 14:51:16 -0700343 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
344 glGenTextures(1, &mProtectedTexName);
345 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
349 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
350 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
351 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352
Mathias Agopiana4912602012-07-12 14:25:33 -0700353 // print some debugging info
354 EGLint r,g,b,a;
355 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
356 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
357 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
358 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
359 ALOGI("EGL informations:");
360 ALOGI("vendor : %s", extensions.getEglVendor());
361 ALOGI("version : %s", extensions.getEglVersion());
362 ALOGI("extensions: %s", extensions.getEglExtension());
363 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
364 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
365 ALOGI("OpenGL ES informations:");
366 ALOGI("vendor : %s", extensions.getVendor());
367 ALOGI("renderer : %s", extensions.getRenderer());
368 ALOGI("version : %s", extensions.getVersion());
369 ALOGI("extensions: %s", extensions.getExtension());
370 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
371 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
372}
373
Mathias Agopiana4912602012-07-12 14:25:33 -0700374status_t SurfaceFlinger::readyToRun()
375{
376 ALOGI( "SurfaceFlinger's main thread ready to run. "
377 "Initializing graphics H/W...");
378
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700379 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700380 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
381 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700382
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700383 // Initialize the H/W composer object. There may or may not be an
384 // actual hardware composer underneath.
385 mHwc = new HWComposer(this,
386 *static_cast<HWComposer::EventHandler *>(this));
387
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700389 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700390 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
391 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700392
Mathias Agopiancde87a32012-09-13 14:09:01 -0700393 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700394 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700395 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700396 mCurrentState.displays.add(mDefaultDisplays[i],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700397 DisplayDeviceState(DisplayDevice::DisplayType(i)));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700398 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700399
400 // The main display is a bit special and always exists
401 //
402 // if we didn't add it here, it would be added automatically during the
403 // first transaction, however this would also create some difficulties:
404 //
405 // - there would be a race where a client could call getDisplayInfo(),
406 // for instance before the DisplayDevice is created.
407 //
408 // - we need a GL context current in a few places, when initializing
409 // OpenGL ES (see below), or creating a layer,
410 // or when a texture is (asynchronously) destroyed, and for that
411 // we need a valid surface, so it's conveniant to use the main display
412 // for that.
413
414 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc);
415 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
416 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700417 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700418 DisplayDevice::DISPLAY_PRIMARY,
419 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700420 stc, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700421 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700422
Mathias Agopiancde87a32012-09-13 14:09:01 -0700423
Mathias Agopiana4912602012-07-12 14:25:33 -0700424 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700425 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
426 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800427
Mathias Agopian028508c2012-07-25 21:12:12 -0700428 // start the EventThread
429 mEventThread = new EventThread(this);
430 mEventQueue.setEventThread(mEventThread);
431
Mathias Agopian92a979a2012-08-02 18:32:23 -0700432 // initialize our drawing state
433 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700434
Mathias Agopiancde87a32012-09-13 14:09:01 -0700435
Mathias Agopiana4912602012-07-12 14:25:33 -0700436 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800437 mReadyToRunBarrier.open();
438
Andy McFadden13a082e2012-08-24 10:16:42 -0700439 // set initial conditions (e.g. unblank default device)
440 initializeDisplays();
441
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700442 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700443 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 return NO_ERROR;
446}
447
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700448int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
449 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
450 type : mHwc->allocateDisplayId();
451}
452
Mathias Agopiana67e4182012-06-19 17:26:12 -0700453void SurfaceFlinger::startBootAnim() {
454 // start boot animation
455 property_set("service.bootanim.exit", "0");
456 property_set("ctl.start", "bootanim");
457}
458
Mathias Agopiana4912602012-07-12 14:25:33 -0700459uint32_t SurfaceFlinger::getMaxTextureSize() const {
460 return mMaxTextureSize;
461}
462
463uint32_t SurfaceFlinger::getMaxViewportDims() const {
464 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
465 mMaxViewportDims[0] : mMaxViewportDims[1];
466}
467
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800469
Jamie Gennis582270d2011-08-17 18:19:00 -0700470bool SurfaceFlinger::authenticateSurfaceTexture(
471 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800472 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700473 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800474
475 // Check the visible layer list for the ISurface
476 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
477 size_t count = currentLayers.size();
478 for (size_t i=0 ; i<count ; i++) {
479 const sp<LayerBase>& layer(currentLayers[i]);
480 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700481 if (lbc != NULL) {
482 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
483 if (lbcBinder == surfaceTextureBinder) {
484 return true;
485 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800486 }
487 }
488
489 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700490 // SurfaceTexture gets destroyed before all the clients are done using it,
491 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800492 // will instead fail later on when the client tries to use the surface,
493 // which should be reported as "surface XYZ returned an -ENODEV". The
494 // purgatorized layers are no less authentic than the visible ones, so this
495 // should not cause any harm.
496 size_t purgatorySize = mLayerPurgatory.size();
497 for (size_t i=0 ; i<purgatorySize ; i++) {
498 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
499 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700500 if (lbc != NULL) {
501 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
502 if (lbcBinder == surfaceTextureBinder) {
503 return true;
504 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800505 }
506 }
507
508 return false;
509}
510
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700511status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
512 // TODO: this is mostly here only for compatibility
513 // the display size is needed but the display metrics should come from elsewhere
514 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
515 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700516 return BAD_INDEX;
517 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700518
519 const HWComposer& hwc(getHwComposer());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700520 float xdpi = hwc.getDpiX(HWC_DISPLAY_PRIMARY);
521 float ydpi = hwc.getDpiY(HWC_DISPLAY_PRIMARY);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700522
523 // TODO: Not sure if display density should handled by SF any longer
524 class Density {
525 static int getDensityFromProperty(char const* propName) {
526 char property[PROPERTY_VALUE_MAX];
527 int density = 0;
528 if (property_get(propName, property, NULL) > 0) {
529 density = atoi(property);
530 }
531 return density;
532 }
533 public:
534 static int getEmuDensity() {
535 return getDensityFromProperty("qemu.sf.lcd_density"); }
536 static int getBuildDensity() {
537 return getDensityFromProperty("ro.sf.lcd_density"); }
538 };
539 // The density of the device is provided by a build property
540 float density = Density::getBuildDensity() / 160.0f;
541 if (density == 0) {
542 // the build doesn't provide a density -- this is wrong!
543 // use xdpi instead
544 ALOGE("ro.sf.lcd_density must be defined as a build property");
545 density = xdpi / 160.0f;
546 }
547 if (Density::getEmuDensity()) {
548 // if "qemu.sf.lcd_density" is specified, it overrides everything
549 xdpi = ydpi = density = Density::getEmuDensity();
550 density /= 160.0f;
551 }
552
Mathias Agopian42977342012-08-05 00:40:46 -0700553 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
554 info->w = hw->getWidth();
555 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700556 info->xdpi = xdpi;
557 info->ydpi = ydpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700558 info->fps = float(1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY));
Mathias Agopian8b736f12012-08-13 17:54:26 -0700559 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700560 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700561 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700562 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700563 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700564}
565
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800566// ----------------------------------------------------------------------------
567
568sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800569 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700570}
571
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700572void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700573
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700574 sp<IBinder> token;
575 { // scope for the lock
576 Mutex::Autolock _l(mStateLock);
577 token = mExtDisplayToken;
578 }
579
580 if (token == 0) {
581 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700582 }
583
584 { // scope for the lock
585 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700586 if (surface == 0) {
587 // release our current display. we're guarantee to have
588 // a reference to it (token), while we hold the lock
589 mExtDisplayToken = 0;
590 } else {
591 mExtDisplayToken = token;
592 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700593
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700594 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
595 info.surface = surface;
596 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700597 }
598}
599
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800601
602void SurfaceFlinger::waitForEvent() {
603 mEventQueue.waitMessage();
604}
605
606void SurfaceFlinger::signalTransaction() {
607 mEventQueue.invalidate();
608}
609
610void SurfaceFlinger::signalLayerUpdate() {
611 mEventQueue.invalidate();
612}
613
614void SurfaceFlinger::signalRefresh() {
615 mEventQueue.refresh();
616}
617
618status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
619 nsecs_t reltime, uint32_t flags) {
620 return mEventQueue.postMessage(msg, reltime);
621}
622
623status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
624 nsecs_t reltime, uint32_t flags) {
625 status_t res = mEventQueue.postMessage(msg, reltime);
626 if (res == NO_ERROR) {
627 msg->wait();
628 }
629 return res;
630}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631
Mathias Agopian4fec8732012-06-29 14:12:52 -0700632bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 return true;
635}
636
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700637void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700638 if (mEventThread == NULL) {
639 // This is a temporary workaround for b/7145521. A non-null pointer
640 // does not mean EventThread has finished initializing, so this
641 // is not a correct fix.
642 ALOGW("WARNING: EventThread not started, ignoring vsync");
643 return;
644 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700645 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
646 // we should only receive DisplayDevice::DisplayType from the vsync callback
647 const wp<IBinder>& token(mDefaultDisplays[type]);
648 mEventThread->onVSyncReceived(token, timestamp);
649 }
Mathias Agopian86303202012-07-24 22:46:10 -0700650}
651
652void SurfaceFlinger::eventControl(int event, int enabled) {
653 getHwComposer().eventControl(event, enabled);
654}
655
Mathias Agopian4fec8732012-06-29 14:12:52 -0700656void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800657 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800658 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700659 case MessageQueue::INVALIDATE:
660 handleMessageTransaction();
661 handleMessageInvalidate();
662 signalRefresh();
663 break;
664 case MessageQueue::REFRESH:
665 handleMessageRefresh();
666 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800667 }
668}
669
Mathias Agopian4fec8732012-06-29 14:12:52 -0700670void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700671 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700672 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700673 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700674 }
675}
676
677void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700678 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700679 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700680}
681
682void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700683 ATRACE_CALL();
684 preComposition();
685 rebuildLayerStacks();
686 setUpHWComposer();
687 doDebugFlashRegions();
688 doComposition();
689 postComposition();
690}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700691
Mathias Agopiancd60f992012-08-16 16:28:27 -0700692void SurfaceFlinger::doDebugFlashRegions()
693{
694 // is debugging enabled
695 if (CC_LIKELY(!mDebugRegion))
696 return;
697
698 const bool repaintEverything = mRepaintEverything;
699 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
700 const sp<DisplayDevice>& hw(mDisplays[dpy]);
701 if (hw->canDraw()) {
702 // transform the dirty region into this screen's coordinate space
703 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
704 if (!dirtyRegion.isEmpty()) {
705 // redraw the whole screen
706 doComposeSurfaces(hw, Region(hw->bounds()));
707
708 // and draw the dirty region
709 glDisable(GL_TEXTURE_EXTERNAL_OES);
710 glDisable(GL_TEXTURE_2D);
711 glDisable(GL_BLEND);
712 glColor4f(1, 0, 1, 1);
713 const int32_t height = hw->getHeight();
714 Region::const_iterator it = dirtyRegion.begin();
715 Region::const_iterator const end = dirtyRegion.end();
716 while (it != end) {
717 const Rect& r = *it++;
718 GLfloat vertices[][2] = {
719 { r.left, height - r.top },
720 { r.left, height - r.bottom },
721 { r.right, height - r.bottom },
722 { r.right, height - r.top }
723 };
724 glVertexPointer(2, GL_FLOAT, 0, vertices);
725 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
726 }
727 hw->compositionComplete();
728 // FIXME
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700729 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700730 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
731 }
732 }
733 }
734 }
735
736 postFramebuffer();
737
738 if (mDebugRegion > 1) {
739 usleep(mDebugRegion * 1000);
740 }
741}
742
743void SurfaceFlinger::preComposition()
744{
745 bool needExtraInvalidate = false;
746 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
747 const size_t count = currentLayers.size();
748 for (size_t i=0 ; i<count ; i++) {
749 if (currentLayers[i]->onPreComposition()) {
750 needExtraInvalidate = true;
751 }
752 }
753 if (needExtraInvalidate) {
754 signalLayerUpdate();
755 }
756}
757
758void SurfaceFlinger::postComposition()
759{
760 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
761 const size_t count = currentLayers.size();
762 for (size_t i=0 ; i<count ; i++) {
763 currentLayers[i]->onPostComposition();
764 }
765}
766
767void SurfaceFlinger::rebuildLayerStacks() {
768 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700769 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700771 mVisibleRegionsDirty = false;
772 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700773
Mathias Agopian87baae12012-07-31 12:38:26 -0700774 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700775 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700776 Region opaqueRegion;
777 Region dirtyRegion;
778 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700779 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700780 const Transform& tr(hw->getTransform());
781 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700782 if (hw->canDraw()) {
783 SurfaceFlinger::computeVisibleRegions(currentLayers,
784 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700785
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700786 const size_t count = currentLayers.size();
787 for (size_t i=0 ; i<count ; i++) {
788 const sp<LayerBase>& layer(currentLayers[i]);
789 const Layer::State& s(layer->drawingState());
790 if (s.layerStack == hw->getLayerStack()) {
791 Region visibleRegion(tr.transform(layer->visibleRegion));
792 visibleRegion.andSelf(bounds);
793 if (!visibleRegion.isEmpty()) {
794 layersSortedByZ.add(layer);
795 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700796 }
797 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700798 }
Mathias Agopian42977342012-08-05 00:40:46 -0700799 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700800 hw->undefinedRegion.set(bounds);
801 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
802 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700803 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700804 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700806
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808 HWComposer& hwc(getHwComposer());
809 if (hwc.initCheck() == NO_ERROR) {
810 // build the h/w work list
811 const bool workListsDirty = mHwWorkListDirty;
812 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700813 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700814 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700815 const int32_t id = hw->getHwcDisplayId();
816 if (id >= 0) {
817 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700818 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700819 const size_t count = currentLayers.size();
820 if (hwc.createWorkList(id, count) >= 0) {
821 HWComposer::LayerListIterator cur = hwc.begin(id);
822 const HWComposer::LayerListIterator end = hwc.end(id);
823 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
824 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700825
Mathias Agopiane60b0682012-08-21 23:34:09 -0700826 if (CC_UNLIKELY(workListsDirty)) {
827 layer->setGeometry(hw, *cur);
828 if (mDebugDisableHWC || mDebugRegion) {
829 cur->setSkip(true);
830 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700831 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832
Mathias Agopiane60b0682012-08-21 23:34:09 -0700833 /*
834 * update the per-frame h/w composer data for each layer
835 * and build the transparent region of the FB
836 */
837 layer->setPerFrameData(hw, *cur);
838 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700839 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700840 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700841 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700842 status_t err = hwc.prepare();
843 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
844 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700846
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847void SurfaceFlinger::doComposition() {
848 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700850 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700851 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852 if (hw->canDraw()) {
853 // transform the dirty region into this screen's coordinate space
854 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
855 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700856 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700858 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859 hw->dirtyRegion.clear();
860 hw->flip(hw->swapRegion);
861 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700862 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700863 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700864 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700865 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700866 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700867}
868
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869void SurfaceFlinger::postFramebuffer()
870{
Mathias Agopian841cde52012-03-01 15:44:37 -0800871 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800872
Mathias Agopiana44b0412011-10-16 18:46:35 -0700873 const nsecs_t now = systemTime();
874 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700875
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700876 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700877 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700878 // FIXME: EGL spec says:
879 // "surface must be bound to the calling thread's current context,
880 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700881 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700882 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700883 }
884
Mathias Agopian92a979a2012-08-02 18:32:23 -0700885 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700886 sp<const DisplayDevice> hw(mDisplays[dpy]);
887 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700888 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700889 int32_t id = hw->getHwcDisplayId();
890 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700891 HWComposer::LayerListIterator cur = hwc.begin(id);
892 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700893 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700894 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700895 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700896 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700897 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700898 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700899 }
Jesse Hallef194142012-06-14 14:45:17 -0700900 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800901 }
902
Mathias Agopiana44b0412011-10-16 18:46:35 -0700903 mLastSwapBufferTime = systemTime() - now;
904 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905}
906
Mathias Agopian87baae12012-07-31 12:38:26 -0700907void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908{
Mathias Agopian841cde52012-03-01 15:44:37 -0800909 ATRACE_CALL();
910
Mathias Agopianca4d3602011-05-19 15:38:14 -0700911 Mutex::Autolock _l(mStateLock);
912 const nsecs_t now = systemTime();
913 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914
Mathias Agopianca4d3602011-05-19 15:38:14 -0700915 // Here we're guaranteed that some transaction flags are set
916 // so we can call handleTransactionLocked() unconditionally.
917 // We call getTransactionFlags(), which will also clear the flags,
918 // with mStateLock held to guarantee that mCurrentState won't change
919 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700920
Mathias Agopiane57f2922012-08-09 16:29:12 -0700921 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700922 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700923
Mathias Agopianca4d3602011-05-19 15:38:14 -0700924 mLastTransactionTime = systemTime() - now;
925 mDebugInTransaction = 0;
926 invalidateHwcGeometry();
927 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700928}
929
Mathias Agopian87baae12012-07-31 12:38:26 -0700930void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700931{
932 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 const size_t count = currentLayers.size();
934
935 /*
936 * Traversal of the children
937 * (perform the transaction for each of them if needed)
938 */
939
Mathias Agopian3559b072012-08-15 13:46:03 -0700940 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700942 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
944 if (!trFlags) continue;
945
946 const uint32_t flags = layer->doTransaction(0);
947 if (flags & Layer::eVisibleRegion)
948 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 }
950 }
951
952 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700953 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 */
955
Mathias Agopiane57f2922012-08-09 16:29:12 -0700956 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700957 // here we take advantage of Vector's copy-on-write semantics to
958 // improve performance by skipping the transaction entirely when
959 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700960 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
961 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700962 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700964 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700965 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700966
967 // find the displays that were removed
968 // (ie: in drawing state but not in current state)
969 // also handle displays that changed
970 // (ie: displays that are in both lists)
971 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700972 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
973 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700974 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700975 if (!draw[i].isMainDisplay()) {
976 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700977 } else {
978 ALOGW("trying to remove the main display");
979 }
980 } else {
981 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700982 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700983 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700984 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700985 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700986 // recreating the DisplayDevice, so we just remove it
987 // from the drawing state, so that it get re-added
988 // below.
989 mDisplays.removeItem(display);
990 mDrawingState.displays.removeItemsAt(i);
991 dc--; i--;
992 // at this point we must loop to the next item
993 continue;
994 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700995
Mathias Agopian93997a82012-08-29 17:30:36 -0700996 const sp<DisplayDevice>& disp(getDisplayDevice(display));
997 if (disp != NULL) {
998 if (state.layerStack != draw[i].layerStack) {
999 disp->setLayerStack(state.layerStack);
1000 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001001 if ((state.orientation != draw[i].orientation)
1002 || (state.viewport != draw[i].viewport)
1003 || (state.frame != draw[i].frame))
1004 {
1005 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001006 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001007 }
Andy McFadden69052052012-09-14 16:10:11 -07001008
1009 // Walk through all the layers in currentLayers,
1010 // and update their transform hint.
1011 //
1012 // TODO: we could be much more clever about which
1013 // layers we touch and how often we do these updates
1014 // (e.g. only touch the layers associated with this
1015 // display, and only on a rotation).
1016 for (size_t i = 0; i < count; i++) {
1017 const sp<LayerBase>& layerBase = currentLayers[i];
1018 layerBase->updateTransformHint();
1019 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001020 }
1021 }
1022 }
1023
1024 // find displays that were added
1025 // (ie: in current state but not in drawing state)
1026 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001027 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001028 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001029
1030 sp<FramebufferSurface> fbs;
1031 sp<SurfaceTextureClient> stc;
1032 if (!state.isVirtualDisplay()) {
1033
1034 ALOGE_IF(state.surface!=NULL,
1035 "adding a supported display, but rendering "
1036 "surface is provided (%p), ignoring it",
1037 state.surface.get());
1038
1039 // for supported (by hwc) displays we provide our
1040 // own rendering surface
1041 fbs = new FramebufferSurface(*mHwc);
1042 stc = new SurfaceTextureClient(
1043 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1044 } else {
1045 if (state.surface != NULL) {
1046 stc = new SurfaceTextureClient(state.surface);
1047 }
1048 }
1049
1050 const wp<IBinder>& display(curr.keyAt(i));
1051 if (stc != NULL) {
1052 sp<DisplayDevice> hw = new DisplayDevice(this,
1053 state.type, display, stc, fbs, mEGLConfig);
1054 hw->setLayerStack(state.layerStack);
1055 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001056 state.viewport, state.frame);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001057 mDisplays.add(display, hw);
1058 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1059 // notify the system that this display is now up
1060 // (note onScreenAcquired() is safe to call from
1061 // here because we're in the main thread)
1062 onScreenAcquired(hw);
1063 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001064 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001065 }
1066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001068 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069
Mathias Agopian3559b072012-08-15 13:46:03 -07001070 /*
1071 * Perform our own transaction if needed
1072 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001073
Mathias Agopiancd60f992012-08-16 16:28:27 -07001074 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1075 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001076 // layers have been added
1077 mVisibleRegionsDirty = true;
1078 }
1079
1080 // some layers might have been removed, so
1081 // we need to update the regions they're exposing.
1082 if (mLayersRemoved) {
1083 mLayersRemoved = false;
1084 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001085 const size_t count = previousLayers.size();
1086 for (size_t i=0 ; i<count ; i++) {
1087 const sp<LayerBase>& layer(previousLayers[i]);
1088 if (currentLayers.indexOf(layer) < 0) {
1089 // this layer is not visible anymore
1090 // TODO: we could traverse the tree from front to back and
1091 // compute the actual visible region
1092 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001093 const Layer::State& s(layer->drawingState());
1094 Region visibleReg = s.transform.transform(
1095 Region(Rect(s.active.w, s.active.h)));
1096 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001097 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 }
1100
1101 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001102}
1103
1104void SurfaceFlinger::commitTransaction()
1105{
1106 if (!mLayersPendingRemoval.isEmpty()) {
1107 // Notify removed layers now that they can't be drawn from
1108 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1109 mLayersPendingRemoval[i]->onRemoved();
1110 }
1111 mLayersPendingRemoval.clear();
1112 }
1113
1114 mDrawingState = mCurrentState;
1115 mTransationPending = false;
1116 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117}
1118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001120 const LayerVector& currentLayers, uint32_t layerStack,
1121 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122{
Mathias Agopian841cde52012-03-01 15:44:37 -08001123 ATRACE_CALL();
1124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 Region aboveOpaqueLayers;
1126 Region aboveCoveredLayers;
1127 Region dirty;
1128
Mathias Agopian87baae12012-07-31 12:38:26 -07001129 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130
1131 size_t i = currentLayers.size();
1132 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001133 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134
1135 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001136 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137
Mathias Agopian87baae12012-07-31 12:38:26 -07001138 // only consider the layers on the given later stack
1139 if (s.layerStack != layerStack)
1140 continue;
1141
Mathias Agopianab028732010-03-16 16:41:46 -07001142 /*
1143 * opaqueRegion: area of a surface that is fully opaque.
1144 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001146
1147 /*
1148 * visibleRegion: area of a surface that is visible on screen
1149 * and not fully transparent. This is essentially the layer's
1150 * footprint minus the opaque regions above it.
1151 * Areas covered by a translucent surface are considered visible.
1152 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001154
1155 /*
1156 * coveredRegion: area of a surface that is covered by all
1157 * visible regions above it (which includes the translucent areas).
1158 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001160
1161
1162 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001163 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001164 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001165 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001167 if (!visibleRegion.isEmpty()) {
1168 // Remove the transparent area from the visible region
1169 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001170 Region transparentRegionScreen;
1171 const Transform tr(s.transform);
1172 if (tr.transformed()) {
1173 if (tr.preserveRects()) {
1174 // transform the transparent region
1175 transparentRegionScreen = tr.transform(s.transparentRegion);
1176 } else {
1177 // transformation too complex, can't do the
1178 // transparent region optimization.
1179 transparentRegionScreen.clear();
1180 }
1181 } else {
1182 transparentRegionScreen = s.transparentRegion;
1183 }
1184 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186
Mathias Agopianab028732010-03-16 16:41:46 -07001187 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001188 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001189 if (s.alpha==255 && !translucent &&
1190 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1191 // the opaque region is the layer's footprint
1192 opaqueRegion = visibleRegion;
1193 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 }
1195 }
1196
Mathias Agopianab028732010-03-16 16:41:46 -07001197 // Clip the covered region to the visible region
1198 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1199
1200 // Update aboveCoveredLayers for next (lower) layer
1201 aboveCoveredLayers.orSelf(visibleRegion);
1202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 // subtract the opaque region covered by the layers above us
1204 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205
1206 // compute this layer's dirty region
1207 if (layer->contentDirty) {
1208 // we need to invalidate the whole region
1209 dirty = visibleRegion;
1210 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001211 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 layer->contentDirty = false;
1213 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001214 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001215 * the exposed region consists of two components:
1216 * 1) what's VISIBLE now and was COVERED before
1217 * 2) what's EXPOSED now less what was EXPOSED before
1218 *
1219 * note that (1) is conservative, we start with the whole
1220 * visible region but only keep what used to be covered by
1221 * something -- which mean it may have been exposed.
1222 *
1223 * (2) handles areas that were not covered by anything but got
1224 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001225 */
Mathias Agopianab028732010-03-16 16:41:46 -07001226 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001227 const Region oldVisibleRegion = layer->visibleRegion;
1228 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001229 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1230 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 }
1232 dirty.subtractSelf(aboveOpaqueLayers);
1233
1234 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001235 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236
Mathias Agopianab028732010-03-16 16:41:46 -07001237 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 // Store the visible region is screen space
1241 layer->setVisibleRegion(visibleRegion);
1242 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 }
1244
Mathias Agopian87baae12012-07-31 12:38:26 -07001245 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246}
1247
Mathias Agopian87baae12012-07-31 12:38:26 -07001248void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1249 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001250 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001251 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1252 if (hw->getLayerStack() == layerStack) {
1253 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001254 }
1255 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001256}
1257
1258void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001260 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261
Mathias Agopian4fec8732012-06-29 14:12:52 -07001262 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001263 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001264 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001265 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001266 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001267 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001268 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001269 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001270 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001271
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001272 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273}
1274
Mathias Agopianad456f92011-01-13 17:53:01 -08001275void SurfaceFlinger::invalidateHwcGeometry()
1276{
1277 mHwWorkListDirty = true;
1278}
1279
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001280
Mathias Agopiancd60f992012-08-16 16:28:27 -07001281void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001282 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283{
Mathias Agopian87baae12012-07-31 12:38:26 -07001284 Region dirtyRegion(inDirtyRegion);
1285
Mathias Agopianb8a55602009-06-26 19:06:36 -07001286 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001287 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288
Mathias Agopian42977342012-08-05 00:40:46 -07001289 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001290 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001291 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1292 // takes a rectangle, we must make sure to update that whole
1293 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001294 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001295 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001296 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001297 // We need to redraw the rectangle that will be updated
1298 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001299 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001300 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001301 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001302 } else {
1303 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001304 dirtyRegion.set(hw->bounds());
1305 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 }
1307 }
1308
Mathias Agopiancd60f992012-08-16 16:28:27 -07001309 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310
Mathias Agopiancd60f992012-08-16 16:28:27 -07001311 // FIXME: we need to call eglSwapBuffers() on displays that have
1312 // GL composition and only on those.
1313 // however, currently hwc.commit() already does that for the main
Jesse Hall9ca48912012-08-30 13:42:23 -07001314 // display (if there is a hwc) and never for the other ones
1315 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL ||
1316 getHwComposer().initCheck() != NO_ERROR) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001317 // FIXME: EGL spec says:
1318 // "surface must be bound to the calling thread's current context,
1319 // for the current rendering API."
1320 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001321 }
1322
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001323 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001324 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325}
1326
Mathias Agopiancd60f992012-08-16 16:28:27 -07001327void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001328{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001329 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001330 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001331 HWComposer::LayerListIterator cur = hwc.begin(id);
1332 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001333
Mathias Agopian85d751c2012-08-29 16:59:24 -07001334 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1335 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001336 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001337
1338 // set the frame buffer
1339 glMatrixMode(GL_MODELVIEW);
1340 glLoadIdentity();
1341
1342 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001343 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001344 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001345 // when using overlays, we assume a fully transparent framebuffer
1346 // NOTE: we could reduce how much we need to clear, for instance
1347 // remove where there are opaque FB layers. however, on some
1348 // GPUs doing a "clean slate" glClear might be more efficient.
1349 // We'll revisit later if needed.
1350 glClearColor(0, 0, 0, 0);
1351 glClear(GL_COLOR_BUFFER_BIT);
1352 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001353 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001354 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001355 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001356 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001357 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001358 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001359 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001360 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001361
Mathias Agopian85d751c2012-08-29 16:59:24 -07001362 /*
1363 * and then, render the layers targeted at the framebuffer
1364 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001365
Mathias Agopian85d751c2012-08-29 16:59:24 -07001366 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1367 const size_t count = layers.size();
1368 const Transform& tr = hw->getTransform();
1369 if (cur != end) {
1370 // we're using h/w composer
1371 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001372 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001373 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001374 if (!clip.isEmpty()) {
1375 switch (cur->getCompositionType()) {
1376 case HWC_OVERLAY: {
1377 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1378 && i
1379 && layer->isOpaque()
1380 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001381 // never clear the very first layer since we're
1382 // guaranteed the FB is already cleared
1383 layer->clearWithOpenGL(hw, clip);
1384 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001385 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001386 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001387 case HWC_FRAMEBUFFER: {
1388 layer->draw(hw, clip);
1389 break;
1390 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001391 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001392 }
1393 layer->setAcquireFence(hw, *cur);
1394 }
1395 } else {
1396 // we're not using h/w composer
1397 for (size_t i=0 ; i<count ; ++i) {
1398 const sp<LayerBase>& layer(layers[i]);
1399 const Region clip(dirty.intersect(
1400 tr.transform(layer->visibleRegion)));
1401 if (!clip.isEmpty()) {
1402 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001403 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001404 }
1405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406}
1407
Mathias Agopian55801e42012-08-27 18:54:24 -07001408void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1409 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001411 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001412 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001413 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001414 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001415
Mathias Agopian55801e42012-08-27 18:54:24 -07001416 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001417 Region::const_iterator it = region.begin();
1418 Region::const_iterator const end = region.end();
1419 while (it != end) {
1420 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001421 GLfloat vertices[][2] = {
1422 { r.left, height - r.top },
1423 { r.left, height - r.bottom },
1424 { r.right, height - r.bottom },
1425 { r.right, height - r.top }
1426 };
1427 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001428 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1429 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430}
1431
Mathias Agopian96f08192010-06-02 23:28:45 -07001432ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1433 const sp<LayerBaseClient>& lbc)
1434{
Mathias Agopian96f08192010-06-02 23:28:45 -07001435 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001436 size_t name = client->attachLayer(lbc);
1437
Mathias Agopian96f08192010-06-02 23:28:45 -07001438 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001439 Mutex::Autolock _l(mStateLock);
1440 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001441
Mathias Agopian4f113742011-05-03 16:21:41 -07001442 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001443}
1444
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001445status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446{
1447 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001448 status_t err = purgatorizeLayer_l(layer);
1449 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001450 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001451 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452}
1453
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001454status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455{
1456 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1457 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001458 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 return NO_ERROR;
1460 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001461 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462}
1463
Mathias Agopian9a112062009-04-17 19:36:26 -07001464status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1465{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001466 // First add the layer to the purgatory list, which makes sure it won't
1467 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001468 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001469 if (err >= 0) {
1470 mLayerPurgatory.add(layerBase);
1471 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001472
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001473 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001474
Mathias Agopian3d579642009-06-04 18:46:21 -07001475 // it's possible that we don't find a layer, because it might
1476 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001477 // from the user because there is a race between Client::destroySurface(),
1478 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001479 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1480}
1481
Mathias Agopiandea20b12011-05-03 17:04:02 -07001482uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1483{
1484 return android_atomic_release_load(&mTransactionFlags);
1485}
1486
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1488{
1489 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1490}
1491
Mathias Agopianbb641242010-05-18 17:06:55 -07001492uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493{
1494 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1495 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001496 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 }
1498 return old;
1499}
1500
Mathias Agopian8b33f032012-07-24 20:43:54 -07001501void SurfaceFlinger::setTransactionState(
1502 const Vector<ComposerState>& state,
1503 const Vector<DisplayState>& displays,
1504 uint32_t flags)
1505{
Mathias Agopian698c0872011-06-28 19:09:31 -07001506 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001507 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001508
1509 size_t count = displays.size();
1510 for (size_t i=0 ; i<count ; i++) {
1511 const DisplayState& s(displays[i]);
1512 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001513 }
1514
Mathias Agopiane57f2922012-08-09 16:29:12 -07001515 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001516 for (size_t i=0 ; i<count ; i++) {
1517 const ComposerState& s(state[i]);
1518 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001519 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001520 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001521
Mathias Agopian386aa982011-11-07 21:58:03 -08001522 if (transactionFlags) {
1523 // this triggers the transaction
1524 setTransactionFlags(transactionFlags);
1525
1526 // if this is a synchronous transaction, wait for it to take effect
1527 // before returning.
1528 if (flags & eSynchronous) {
1529 mTransationPending = true;
1530 }
1531 while (mTransationPending) {
1532 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1533 if (CC_UNLIKELY(err != NO_ERROR)) {
1534 // just in case something goes wrong in SF, return to the
1535 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001536 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001537 mTransationPending = false;
1538 break;
1539 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001540 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 }
1542}
1543
Mathias Agopiane57f2922012-08-09 16:29:12 -07001544uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1545{
1546 uint32_t flags = 0;
1547 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001548 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001549 const uint32_t what = s.what;
1550 if (what & DisplayState::eSurfaceChanged) {
1551 if (disp.surface->asBinder() != s.surface->asBinder()) {
1552 disp.surface = s.surface;
1553 flags |= eDisplayTransactionNeeded;
1554 }
1555 }
1556 if (what & DisplayState::eLayerStackChanged) {
1557 if (disp.layerStack != s.layerStack) {
1558 disp.layerStack = s.layerStack;
1559 flags |= eDisplayTransactionNeeded;
1560 }
1561 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001562 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001563 if (disp.orientation != s.orientation) {
1564 disp.orientation = s.orientation;
1565 flags |= eDisplayTransactionNeeded;
1566 }
1567 if (disp.frame != s.frame) {
1568 disp.frame = s.frame;
1569 flags |= eDisplayTransactionNeeded;
1570 }
1571 if (disp.viewport != s.viewport) {
1572 disp.viewport = s.viewport;
1573 flags |= eDisplayTransactionNeeded;
1574 }
1575 }
1576 }
1577 return flags;
1578}
1579
1580uint32_t SurfaceFlinger::setClientStateLocked(
1581 const sp<Client>& client,
1582 const layer_state_t& s)
1583{
1584 uint32_t flags = 0;
1585 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1586 if (layer != 0) {
1587 const uint32_t what = s.what;
1588 if (what & layer_state_t::ePositionChanged) {
1589 if (layer->setPosition(s.x, s.y))
1590 flags |= eTraversalNeeded;
1591 }
1592 if (what & layer_state_t::eLayerChanged) {
1593 // NOTE: index needs to be calculated before we update the state
1594 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1595 if (layer->setLayer(s.z)) {
1596 mCurrentState.layersSortedByZ.removeAt(idx);
1597 mCurrentState.layersSortedByZ.add(layer);
1598 // we need traversal (state changed)
1599 // AND transaction (list changed)
1600 flags |= eTransactionNeeded|eTraversalNeeded;
1601 }
1602 }
1603 if (what & layer_state_t::eSizeChanged) {
1604 if (layer->setSize(s.w, s.h)) {
1605 flags |= eTraversalNeeded;
1606 }
1607 }
1608 if (what & layer_state_t::eAlphaChanged) {
1609 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1610 flags |= eTraversalNeeded;
1611 }
1612 if (what & layer_state_t::eMatrixChanged) {
1613 if (layer->setMatrix(s.matrix))
1614 flags |= eTraversalNeeded;
1615 }
1616 if (what & layer_state_t::eTransparentRegionChanged) {
1617 if (layer->setTransparentRegionHint(s.transparentRegion))
1618 flags |= eTraversalNeeded;
1619 }
1620 if (what & layer_state_t::eVisibilityChanged) {
1621 if (layer->setFlags(s.flags, s.mask))
1622 flags |= eTraversalNeeded;
1623 }
1624 if (what & layer_state_t::eCropChanged) {
1625 if (layer->setCrop(s.crop))
1626 flags |= eTraversalNeeded;
1627 }
1628 if (what & layer_state_t::eLayerStackChanged) {
1629 // NOTE: index needs to be calculated before we update the state
1630 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1631 if (layer->setLayerStack(s.layerStack)) {
1632 mCurrentState.layersSortedByZ.removeAt(idx);
1633 mCurrentState.layersSortedByZ.add(layer);
1634 // we need traversal (state changed)
1635 // AND transaction (list changed)
1636 flags |= eTransactionNeeded|eTraversalNeeded;
1637 }
1638 }
1639 }
1640 return flags;
1641}
1642
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001643sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001644 ISurfaceComposerClient::surface_data_t* params,
1645 const String8& name,
1646 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001647 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648 uint32_t flags)
1649{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001650 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001651 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001652
1653 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001654 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001655 int(w), int(h));
1656 return surfaceHandle;
1657 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001658
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001659 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001660 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1661 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001662 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001664 case ISurfaceComposerClient::eFXSurfaceBlur:
1665 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001666 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001668 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001669 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001670 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 }
1672
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001673 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001674 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001675 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001676 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001678 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001679 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001680 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001681 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001682 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 }
1684
1685 return surfaceHandle;
1686}
1687
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001688sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001689 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001690 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001691 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692{
1693 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001694 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 case PIXEL_FORMAT_TRANSPARENT:
1696 case PIXEL_FORMAT_TRANSLUCENT:
1697 format = PIXEL_FORMAT_RGBA_8888;
1698 break;
1699 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001700#ifdef NO_RGBX_8888
1701 format = PIXEL_FORMAT_RGB_565;
1702#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001703 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001704#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 break;
1706 }
1707
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001708#ifdef NO_RGBX_8888
1709 if (format == PIXEL_FORMAT_RGBX_8888)
1710 format = PIXEL_FORMAT_RGBA_8888;
1711#endif
1712
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001713 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001714 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001715 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001716 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001717 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 }
1719 return layer;
1720}
1721
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001722sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001723 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001724 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001726 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001727 return layer;
1728}
1729
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001730sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001731 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001732 uint32_t w, uint32_t h, uint32_t flags)
1733{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001734 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 return layer;
1736}
1737
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001738status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739{
Mathias Agopian9a112062009-04-17 19:36:26 -07001740 /*
1741 * called by the window manager, when a surface should be marked for
1742 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001743 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001744 * The surface is removed from the current and drawing lists, but placed
1745 * in the purgatory queue, so it's not destroyed right-away (we need
1746 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001747 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001748
Mathias Agopian48d819a2009-09-10 19:41:18 -07001749 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001750 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001751 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001752
Mathias Agopian48d819a2009-09-10 19:41:18 -07001753 if (layer != 0) {
1754 err = purgatorizeLayer_l(layer);
1755 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001756 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001757 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001758 }
1759 return err;
1760}
1761
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001762status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001763{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001764 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001765 status_t err = NO_ERROR;
1766 sp<LayerBaseClient> l(layer.promote());
1767 if (l != NULL) {
1768 Mutex::Autolock _l(mStateLock);
1769 err = removeLayer_l(l);
1770 if (err == NAME_NOT_FOUND) {
1771 // The surface wasn't in the current list, which means it was
1772 // removed already, which means it is in the purgatory,
1773 // and need to be removed from there.
1774 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001775 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001776 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001777 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001778 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001779 "error removing layer=%p (%s)", l.get(), strerror(-err));
1780 }
1781 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782}
1783
Mathias Agopianb60314a2012-04-10 22:09:54 -07001784// ---------------------------------------------------------------------------
1785
Andy McFadden13a082e2012-08-24 10:16:42 -07001786void SurfaceFlinger::onInitializeDisplays() {
1787 // reset screen orientation
1788 Vector<ComposerState> state;
1789 Vector<DisplayState> displays;
1790 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001791 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001792 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001793 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001794 d.frame.makeInvalid();
1795 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001796 displays.add(d);
1797 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001798 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001799}
1800
1801void SurfaceFlinger::initializeDisplays() {
1802 class MessageScreenInitialized : public MessageBase {
1803 SurfaceFlinger* flinger;
1804 public:
1805 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1806 virtual bool handler() {
1807 flinger->onInitializeDisplays();
1808 return true;
1809 }
1810 };
1811 sp<MessageBase> msg = new MessageScreenInitialized(this);
1812 postMessageAsync(msg); // we may be called from main thread, use async message
1813}
1814
1815
Mathias Agopiancde87a32012-09-13 14:09:01 -07001816void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001817 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001818 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001819 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001820 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1821 // FIXME: eventthread only knows about the main display right now
1822 mEventThread->onScreenAcquired();
1823 }
Mathias Agopian20128302012-08-13 18:32:13 -07001824 mVisibleRegionsDirty = true;
1825 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826}
1827
Mathias Agopiancde87a32012-09-13 14:09:01 -07001828void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001829 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001830 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001831 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1832 // FIXME: eventthread only knows about the main display right now
1833 mEventThread->onScreenReleased();
1834 }
Mathias Agopian42977342012-08-05 00:40:46 -07001835 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001836 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001837 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001838 // from this point on, SF will stop drawing
1839 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840}
1841
Colin Cross8e533062012-06-07 13:17:52 -07001842void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001843 class MessageScreenAcquired : public MessageBase {
1844 SurfaceFlinger* flinger;
1845 public:
1846 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1847 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001848 // FIXME: should this be per-display?
1849 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001850 return true;
1851 }
1852 };
1853 sp<MessageBase> msg = new MessageScreenAcquired(this);
1854 postMessageSync(msg);
1855}
1856
Colin Cross8e533062012-06-07 13:17:52 -07001857void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001858 class MessageScreenReleased : public MessageBase {
1859 SurfaceFlinger* flinger;
1860 public:
1861 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1862 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001863 // FIXME: should this be per-display?
1864 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001865 return true;
1866 }
1867 };
1868 sp<MessageBase> msg = new MessageScreenReleased(this);
1869 postMessageSync(msg);
1870}
1871
1872// ---------------------------------------------------------------------------
1873
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1875{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001876 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877 char buffer[SIZE];
1878 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001879
1880 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 snprintf(buffer, SIZE, "Permission Denial: "
1882 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1883 IPCThreadState::self()->getCallingPid(),
1884 IPCThreadState::self()->getCallingUid());
1885 result.append(buffer);
1886 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001887 // Try to get the main lock, but don't insist if we can't
1888 // (this would indicate SF is stuck, but we want to be able to
1889 // print something in dumpsys).
1890 int retry = 3;
1891 while (mStateLock.tryLock()<0 && --retry>=0) {
1892 usleep(1000000);
1893 }
1894 const bool locked(retry >= 0);
1895 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001896 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001897 "SurfaceFlinger appears to be unresponsive, "
1898 "dumping anyways (no locks held)\n");
1899 result.append(buffer);
1900 }
1901
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001902 bool dumpAll = true;
1903 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001904 size_t numArgs = args.size();
1905 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001906 if ((index < numArgs) &&
1907 (args[index] == String16("--list"))) {
1908 index++;
1909 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001910 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001911 }
1912
1913 if ((index < numArgs) &&
1914 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001915 index++;
1916 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001917 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001918 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001919
1920 if ((index < numArgs) &&
1921 (args[index] == String16("--latency-clear"))) {
1922 index++;
1923 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001924 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001925 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001927
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001928 if (dumpAll) {
1929 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001930 }
1931
Mathias Agopian9795c422009-08-26 16:36:26 -07001932 if (locked) {
1933 mStateLock.unlock();
1934 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 }
1936 write(fd, result.string(), result.size());
1937 return NO_ERROR;
1938}
1939
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001940void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1941 String8& result, char* buffer, size_t SIZE) const
1942{
1943 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1944 const size_t count = currentLayers.size();
1945 for (size_t i=0 ; i<count ; i++) {
1946 const sp<LayerBase>& layer(currentLayers[i]);
1947 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1948 result.append(buffer);
1949 }
1950}
1951
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001952void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1953 String8& result, char* buffer, size_t SIZE) const
1954{
1955 String8 name;
1956 if (index < args.size()) {
1957 name = String8(args[index]);
1958 index++;
1959 }
1960
1961 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1962 const size_t count = currentLayers.size();
1963 for (size_t i=0 ; i<count ; i++) {
1964 const sp<LayerBase>& layer(currentLayers[i]);
1965 if (name.isEmpty()) {
1966 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1967 result.append(buffer);
1968 }
1969 if (name.isEmpty() || (name == layer->getName())) {
1970 layer->dumpStats(result, buffer, SIZE);
1971 }
1972 }
1973}
1974
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001975void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1976 String8& result, char* buffer, size_t SIZE) const
1977{
1978 String8 name;
1979 if (index < args.size()) {
1980 name = String8(args[index]);
1981 index++;
1982 }
1983
1984 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1985 const size_t count = currentLayers.size();
1986 for (size_t i=0 ; i<count ; i++) {
1987 const sp<LayerBase>& layer(currentLayers[i]);
1988 if (name.isEmpty() || (name == layer->getName())) {
1989 layer->clearStats();
1990 }
1991 }
1992}
1993
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001994void SurfaceFlinger::dumpAllLocked(
1995 String8& result, char* buffer, size_t SIZE) const
1996{
1997 // figure out if we're stuck somewhere
1998 const nsecs_t now = systemTime();
1999 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2000 const nsecs_t inTransaction(mDebugInTransaction);
2001 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2002 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2003
2004 /*
2005 * Dump the visible layer list
2006 */
2007 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2008 const size_t count = currentLayers.size();
2009 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2010 result.append(buffer);
2011 for (size_t i=0 ; i<count ; i++) {
2012 const sp<LayerBase>& layer(currentLayers[i]);
2013 layer->dump(result, buffer, SIZE);
2014 }
2015
2016 /*
2017 * Dump the layers in the purgatory
2018 */
2019
2020 const size_t purgatorySize = mLayerPurgatory.size();
2021 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2022 result.append(buffer);
2023 for (size_t i=0 ; i<purgatorySize ; i++) {
2024 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2025 layer->shortDump(result, buffer, SIZE);
2026 }
2027
2028 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002029 * Dump Display state
2030 */
2031
2032 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2033 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
2034 snprintf(buffer, SIZE,
2035 "+ DisplayDevice[%u]\n"
Jeff Brown4fb39992012-09-07 12:55:10 -07002036 " type=%x, layerStack=%u, (%4dx%4d), orient=%2d (type=%08x), "
Jeff Brown6e220a62012-09-13 19:22:41 -07002037 "flips=%u, secure=%d, numLayers=%u, v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], "
2038 "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 -07002039 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002040 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002041 hw->getWidth(), hw->getHeight(),
2042 hw->getOrientation(), hw->getTransform().getType(),
2043 hw->getPageFlipCount(),
2044 hw->getSecureLayerVisible(),
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002045 hw->getVisibleLayersSortedByZ().size(),
2046 hw->getViewport().left, hw->getViewport().top, hw->getViewport().right, hw->getViewport().bottom,
Jeff Brown6e220a62012-09-13 19:22:41 -07002047 hw->getFrame().left, hw->getFrame().top, hw->getFrame().right, hw->getFrame().bottom,
2048 hw->getTransform()[0][0], hw->getTransform()[1][0], hw->getTransform()[2][0],
2049 hw->getTransform()[0][1], hw->getTransform()[1][1], hw->getTransform()[2][1],
2050 hw->getTransform()[0][2], hw->getTransform()[1][2], hw->getTransform()[2][2]);
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002051
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002052 result.append(buffer);
2053 }
2054
2055 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002056 * Dump SurfaceFlinger global state
2057 */
2058
2059 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2060 result.append(buffer);
2061
Mathias Agopian888c8222012-08-04 21:10:38 -07002062 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002063 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002064 const GLExtensions& extensions(GLExtensions::getInstance());
2065 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2066 extensions.getVendor(),
2067 extensions.getRenderer(),
2068 extensions.getVersion());
2069 result.append(buffer);
2070
2071 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002072 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002073 result.append(buffer);
2074
2075 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2076 result.append(buffer);
2077
Mathias Agopian42977342012-08-05 00:40:46 -07002078 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002079 snprintf(buffer, SIZE,
2080 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002081 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002082 result.append(buffer);
2083 snprintf(buffer, SIZE,
2084 " last eglSwapBuffers() time: %f us\n"
2085 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002086 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002087 " refresh-rate : %f fps\n"
2088 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002089 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002090 mLastSwapBufferTime/1000.0,
2091 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002092 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002093 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2094 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2095 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002096 result.append(buffer);
2097
2098 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2099 inSwapBuffersDuration/1000.0);
2100 result.append(buffer);
2101
2102 snprintf(buffer, SIZE, " transaction time: %f us\n",
2103 inTransactionDuration/1000.0);
2104 result.append(buffer);
2105
2106 /*
2107 * VSYNC state
2108 */
2109 mEventThread->dump(result, buffer, SIZE);
2110
2111 /*
2112 * Dump HWComposer state
2113 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002114 snprintf(buffer, SIZE, "h/w composer state:\n");
2115 result.append(buffer);
2116 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2117 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2118 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2119 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002120 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002121
2122 /*
2123 * Dump gralloc state
2124 */
2125 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2126 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002127 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002128}
2129
Keun young Park63f165f2012-08-31 10:53:36 -07002130bool SurfaceFlinger::startDdmConnection()
2131{
2132 void* libddmconnection_dso =
2133 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2134 if (!libddmconnection_dso) {
2135 return false;
2136 }
2137 void (*DdmConnection_start)(const char* name);
2138 DdmConnection_start =
2139 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2140 if (!DdmConnection_start) {
2141 dlclose(libddmconnection_dso);
2142 return false;
2143 }
2144 (*DdmConnection_start)(getServiceName());
2145 return true;
2146}
2147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148status_t SurfaceFlinger::onTransact(
2149 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2150{
2151 switch (code) {
2152 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002153 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002154 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002155 case BLANK:
2156 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002157 {
2158 // codes that require permission check
2159 IPCThreadState* ipc = IPCThreadState::self();
2160 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002161 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002162 if ((uid != AID_GRAPHICS) &&
2163 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002164 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002165 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2166 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002167 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002168 break;
2169 }
2170 case CAPTURE_SCREEN:
2171 {
2172 // codes that require permission check
2173 IPCThreadState* ipc = IPCThreadState::self();
2174 const int pid = ipc->getCallingPid();
2175 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002176 if ((uid != AID_GRAPHICS) &&
2177 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002178 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002179 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2180 return PERMISSION_DENIED;
2181 }
2182 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183 }
2184 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002186 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2187 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002188 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002189 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002190 IPCThreadState* ipc = IPCThreadState::self();
2191 const int pid = ipc->getCallingPid();
2192 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002193 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002194 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195 return PERMISSION_DENIED;
2196 }
2197 int n;
2198 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002199 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002200 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201 return NO_ERROR;
2202 case 1002: // SHOW_UPDATES
2203 n = data.readInt32();
2204 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002205 invalidateHwcGeometry();
2206 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002207 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002208 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002209 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002210 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002211 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002212 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002213 setTransactionFlags(
2214 eTransactionNeeded|
2215 eDisplayTransactionNeeded|
2216 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002217 return NO_ERROR;
2218 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002219 case 1006:{ // send empty update
2220 signalRefresh();
2221 return NO_ERROR;
2222 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002223 case 1008: // toggle use of hw composer
2224 n = data.readInt32();
2225 mDebugDisableHWC = n ? 1 : 0;
2226 invalidateHwcGeometry();
2227 repaintEverything();
2228 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002229 case 1009: // toggle use of transform hint
2230 n = data.readInt32();
2231 mDebugDisableTransformHint = n ? 1 : 0;
2232 invalidateHwcGeometry();
2233 repaintEverything();
2234 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002236 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002237 reply->writeInt32(0);
2238 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002239 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002240 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002241 return NO_ERROR;
2242 case 1013: {
2243 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002244 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2245 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002246 }
2247 return NO_ERROR;
2248 }
2249 }
2250 return err;
2251}
2252
Mathias Agopian53331da2011-08-22 21:44:41 -07002253void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002254 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002255 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002256}
2257
Mathias Agopian59119e62010-10-11 12:37:43 -07002258// ---------------------------------------------------------------------------
2259
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002260status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002261 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2262{
2263 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002264 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002265}
2266
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002267status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002268 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002269{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002270 ATRACE_CALL();
2271
Mathias Agopian59119e62010-10-11 12:37:43 -07002272 if (!GLExtensions::getInstance().haveFramebufferObject())
2273 return INVALID_OPERATION;
2274
2275 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002276 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2277 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002278 const uint32_t hw_w = hw->getWidth();
2279 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002280 GLfloat u = 1;
2281 GLfloat v = 1;
2282
2283 // make sure to clear all GL error flags
2284 while ( glGetError() != GL_NO_ERROR ) ;
2285
2286 // create a FBO
2287 GLuint name, tname;
2288 glGenTextures(1, &tname);
2289 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2293 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002294 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002295 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002296 GLint tw = (2 << (31 - clz(hw_w)));
2297 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002298 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2299 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002300 u = GLfloat(hw_w) / tw;
2301 v = GLfloat(hw_h) / th;
2302 }
2303 glGenFramebuffersOES(1, &name);
2304 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002305 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2306 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002307
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002308 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002309 glDisable(GL_TEXTURE_EXTERNAL_OES);
2310 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002311 glClearColor(0,0,0,1);
2312 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002313 glMatrixMode(GL_MODELVIEW);
2314 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002315 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002316 const size_t count = layers.size();
2317 for (size_t i=0 ; i<count ; ++i) {
2318 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002319 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002320 }
2321
Mathias Agopian42977342012-08-05 00:40:46 -07002322 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002323
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002324 // back to main framebuffer
2325 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002326 glDeleteFramebuffersOES(1, &name);
2327
2328 *textureName = tname;
2329 *uOut = u;
2330 *vOut = v;
2331 return NO_ERROR;
2332}
2333
2334// ---------------------------------------------------------------------------
2335
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002336status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 sp<IMemoryHeap>* heap,
2338 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002339 uint32_t sw, uint32_t sh,
2340 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002341{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002342 ATRACE_CALL();
2343
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344 status_t result = PERMISSION_DENIED;
2345
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002346 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002347 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002348 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349
2350 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002351 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002352 const uint32_t hw_w = hw->getWidth();
2353 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002354
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002355 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002356 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002357 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002358 return PERMISSION_DENIED;
2359 }
2360
2361 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002362 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002363 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002364 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002365
2366 sw = (!sw) ? hw_w : sw;
2367 sh = (!sh) ? hw_h : sh;
2368 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002369 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002370
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002371// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2372// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002373
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 // make sure to clear all GL error flags
2375 while ( glGetError() != GL_NO_ERROR ) ;
2376
2377 // create a FBO
2378 GLuint name, tname;
2379 glGenRenderbuffersOES(1, &tname);
2380 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2381 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002382
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 glGenFramebuffersOES(1, &name);
2384 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2385 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2386 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2387
2388 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002389
Mathias Agopian74c40c02010-09-29 13:02:36 -07002390 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2391
2392 // invert everything, b/c glReadPixel() below will invert the FB
2393 glViewport(0, 0, sw, sh);
2394 glMatrixMode(GL_PROJECTION);
2395 glPushMatrix();
2396 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002397 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002398 glMatrixMode(GL_MODELVIEW);
2399
2400 // redraw the screen entirely...
2401 glClearColor(0,0,0,1);
2402 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002403
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002404 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002405 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002406 for (size_t i=0 ; i<count ; ++i) {
2407 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002408 const uint32_t z = layer->drawingState().z;
2409 if (z >= minLayerZ && z <= maxLayerZ) {
2410 if (filtering) layer->setFiltering(true);
2411 layer->draw(hw);
2412 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002413 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002414 }
2415
Mathias Agopian74c40c02010-09-29 13:02:36 -07002416 // check for errors and return screen capture
2417 if (glGetError() != GL_NO_ERROR) {
2418 // error while rendering
2419 result = INVALID_OPERATION;
2420 } else {
2421 // allocate shared memory large enough to hold the
2422 // screen capture
2423 sp<MemoryHeapBase> base(
2424 new MemoryHeapBase(size, 0, "screen-capture") );
2425 void* const ptr = base->getBase();
2426 if (ptr) {
2427 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002428 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002429 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2430 if (glGetError() == GL_NO_ERROR) {
2431 *heap = base;
2432 *w = sw;
2433 *h = sh;
2434 *f = PIXEL_FORMAT_RGBA_8888;
2435 result = NO_ERROR;
2436 }
2437 } else {
2438 result = NO_MEMORY;
2439 }
2440 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002441 glViewport(0, 0, hw_w, hw_h);
2442 glMatrixMode(GL_PROJECTION);
2443 glPopMatrix();
2444 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002445 } else {
2446 result = BAD_VALUE;
2447 }
2448
2449 // release FBO resources
2450 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2451 glDeleteRenderbuffersOES(1, &tname);
2452 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002453
Mathias Agopian42977342012-08-05 00:40:46 -07002454 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002455
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002456// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002457
Mathias Agopian74c40c02010-09-29 13:02:36 -07002458 return result;
2459}
2460
2461
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002462status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002463 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002464 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002465 uint32_t sw, uint32_t sh,
2466 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002467{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002468 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002469 return BAD_VALUE;
2470
2471 if (!GLExtensions::getInstance().haveFramebufferObject())
2472 return INVALID_OPERATION;
2473
2474 class MessageCaptureScreen : public MessageBase {
2475 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002476 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002477 sp<IMemoryHeap>* heap;
2478 uint32_t* w;
2479 uint32_t* h;
2480 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002481 uint32_t sw;
2482 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002483 uint32_t minLayerZ;
2484 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002485 status_t result;
2486 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002487 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002488 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002489 uint32_t sw, uint32_t sh,
2490 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002491 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002492 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2493 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2494 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002495 {
2496 }
2497 status_t getResult() const {
2498 return result;
2499 }
2500 virtual bool handler() {
2501 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002502 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002503 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504 return true;
2505 }
2506 };
2507
2508 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002509 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002510 status_t res = postMessageSync(msg);
2511 if (res == NO_ERROR) {
2512 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2513 }
2514 return res;
2515}
2516
2517// ---------------------------------------------------------------------------
2518
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002519SurfaceFlinger::LayerVector::LayerVector() {
2520}
2521
2522SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2523 : SortedVector<sp<LayerBase> >(rhs) {
2524}
2525
2526int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2527 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002528{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002529 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002530 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2531 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002532
2533 uint32_t ls = l->currentState().layerStack;
2534 uint32_t rs = r->currentState().layerStack;
2535 if (ls != rs)
2536 return ls - rs;
2537
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002538 uint32_t lz = l->currentState().z;
2539 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002540 if (lz != rz)
2541 return lz - rz;
2542
2543 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002544}
2545
2546// ---------------------------------------------------------------------------
2547
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002548SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2549 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002550}
2551
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002552SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2553 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002554 viewport.makeInvalid();
2555 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002556}
2557
2558// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559
Jamie Gennis9a78c902011-01-12 18:30:40 -08002560GraphicBufferAlloc::GraphicBufferAlloc() {}
2561
2562GraphicBufferAlloc::~GraphicBufferAlloc() {}
2563
2564sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002565 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002566 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2567 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002568 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002569 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002570 if (err == NO_MEMORY) {
2571 GraphicBuffer::dumpAllocationsToSystemLog();
2572 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002573 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002574 "failed (%s), handle=%p",
2575 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002576 return 0;
2577 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002578 return graphicBuffer;
2579}
2580
Jamie Gennis9a78c902011-01-12 18:30:40 -08002581// ---------------------------------------------------------------------------
2582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002583}; // namespace android