blob: f1d790dc9c1018f31b69740677458a4df5752b19 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
40#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/SurfaceTextureClient.h>
42
43#include <ui/GraphicBufferAllocator.h>
44#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/String8.h>
47#include <utils/String16.h>
48#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian921e6ac2012-07-23 23:11:29 -070051#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080054#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070056#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080057#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070061#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Mathias Agopiana4912602012-07-12 14:25:33 -070064#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070065#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana67932f2011-04-20 14:20:59 -070067
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070087 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070090 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -070099 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Steve Blocka19954a2012-01-04 20:05:49 +0000101 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700112 if (!startDdmConnection()) {
113 // start failed, and DDMS debugging not enabled
114 mDebugDDMS = 0;
115 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700117 ALOGI_IF(mDebugRegion, "showupdates enabled");
118 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121void SurfaceFlinger::onFirstRef()
122{
123 mEventQueue.init(this);
124
125 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
126
127 // Wait for the main thread to be done with its initialization
128 mReadyToRunBarrier.wait();
129}
130
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::~SurfaceFlinger()
133{
Mathias Agopiana4912602012-07-12 14:25:33 -0700134 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
135 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
136 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800139void SurfaceFlinger::binderDied(const wp<IBinder>& who)
140{
141 // the window manager died on us. prepare its eulogy.
142
Andy McFadden13a082e2012-08-24 10:16:42 -0700143 // restore initial conditions (default device unblank, etc)
144 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145
146 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700147 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148}
149
Mathias Agopian7e27f052010-05-28 14:22:23 -0700150sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Mathias Agopian96f08192010-06-02 23:28:45 -0700152 sp<ISurfaceComposerClient> bclient;
153 sp<Client> client(new Client(this));
154 status_t err = client->initCheck();
155 if (err == NO_ERROR) {
156 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 return bclient;
159}
160
Mathias Agopiane57f2922012-08-09 16:29:12 -0700161sp<IBinder> SurfaceFlinger::createDisplay()
162{
163 class DisplayToken : public BBinder {
164 sp<SurfaceFlinger> flinger;
165 virtual ~DisplayToken() {
166 // no more references, this display must be terminated
167 Mutex::Autolock _l(flinger->mStateLock);
168 flinger->mCurrentState.displays.removeItem(this);
169 flinger->setTransactionFlags(eDisplayTransactionNeeded);
170 }
171 public:
172 DisplayToken(const sp<SurfaceFlinger>& flinger)
173 : flinger(flinger) {
174 }
175 };
176
177 sp<BBinder> token = new DisplayToken(this);
178
179 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700180 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700181 mCurrentState.displays.add(token, info);
182
183 return token;
184}
185
186sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700187 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
189 return NULL;
190 }
191 return mDefaultDisplays[id];
192}
193
Jamie Gennis9a78c902011-01-12 18:30:40 -0800194sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
195{
196 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
197 return gba;
198}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200void SurfaceFlinger::bootFinished()
201{
202 const nsecs_t now = systemTime();
203 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000204 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700205 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700206
207 // wait patiently for the window manager death
208 const String16 name("window");
209 sp<IBinder> window(defaultServiceManager()->getService(name));
210 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700211 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700212 }
213
214 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700215 // formerly we would just kill the process, but we now ask it to exit so it
216 // can choose where to stop the animation.
217 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
219
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700220void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
221 class MessageDestroyGLTexture : public MessageBase {
222 GLuint texture;
223 public:
224 MessageDestroyGLTexture(GLuint texture)
225 : texture(texture) {
226 }
227 virtual bool handler() {
228 glDeleteTextures(1, &texture);
229 return true;
230 }
231 };
232 postMessageAsync(new MessageDestroyGLTexture(texture));
233}
234
Mathias Agopiana4912602012-07-12 14:25:33 -0700235status_t SurfaceFlinger::selectConfigForPixelFormat(
236 EGLDisplay dpy,
237 EGLint const* attrs,
238 PixelFormat format,
239 EGLConfig* outConfig)
240{
241 EGLConfig config = NULL;
242 EGLint numConfigs = -1, n=0;
243 eglGetConfigs(dpy, NULL, 0, &numConfigs);
244 EGLConfig* const configs = new EGLConfig[numConfigs];
245 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
246 for (int i=0 ; i<n ; i++) {
247 EGLint nativeVisualId = 0;
248 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
249 if (nativeVisualId>0 && format == nativeVisualId) {
250 *outConfig = configs[i];
251 delete [] configs;
252 return NO_ERROR;
253 }
254 }
255 delete [] configs;
256 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
Mathias Agopiana4912602012-07-12 14:25:33 -0700259EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
260 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
261 // it is to be used with WIFI displays
262 EGLConfig config;
263 EGLint dummy;
264 status_t err;
265 EGLint attribs[] = {
266 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
267 EGL_RECORDABLE_ANDROID, EGL_TRUE,
268 EGL_NONE
269 };
270 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
271 if (err) {
272 // maybe we failed because of EGL_RECORDABLE_ANDROID
273 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
274 attribs[2] = EGL_NONE;
275 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
276 }
277 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
278 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
279 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
280 }
281 return config;
282}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283
Mathias Agopiana4912602012-07-12 14:25:33 -0700284EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
285 // Also create our EGLContext
286 EGLint contextAttributes[] = {
287#ifdef EGL_IMG_context_priority
288#ifdef HAS_CONTEXT_PRIORITY
289#warning "using EGL_IMG_context_priority"
290 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
291#endif
292#endif
293 EGL_NONE, EGL_NONE
294 };
295 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
296 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
297 return ctxt;
298}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700300void SurfaceFlinger::initializeGL(EGLDisplay display, const sp<DisplayDevice>& hw) {
301 EGLBoolean result = DisplayDevice::makeCurrent(display, hw, mEGLContext);
Mathias Agopiana4912602012-07-12 14:25:33 -0700302 if (!result) {
303 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
304 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
306
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 GLExtensions& extensions(GLExtensions::getInstance());
308 extensions.initWithGLStrings(
309 glGetString(GL_VENDOR),
310 glGetString(GL_RENDERER),
311 glGetString(GL_VERSION),
312 glGetString(GL_EXTENSIONS),
313 eglQueryString(display, EGL_VENDOR),
314 eglQueryString(display, EGL_VERSION),
315 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700316
Mathias Agopiana4912602012-07-12 14:25:33 -0700317 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
318 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700321 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 glShadeModel(GL_FLAT);
324 glDisable(GL_DITHER);
325 glDisable(GL_CULL_FACE);
326
Mathias Agopiana4912602012-07-12 14:25:33 -0700327 struct pack565 {
328 inline uint16_t operator() (int r, int g, int b) const {
329 return (r<<11)|(g<<5)|b;
330 }
331 } pack565;
332
Jamie Gennis9575f602011-10-07 14:51:16 -0700333 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
334 glGenTextures(1, &mProtectedTexName);
335 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
336 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
337 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
338 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
340 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
341 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343 // print some debugging info
344 EGLint r,g,b,a;
345 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
346 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
347 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
348 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
349 ALOGI("EGL informations:");
350 ALOGI("vendor : %s", extensions.getEglVendor());
351 ALOGI("version : %s", extensions.getEglVersion());
352 ALOGI("extensions: %s", extensions.getEglExtension());
353 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
354 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
355 ALOGI("OpenGL ES informations:");
356 ALOGI("vendor : %s", extensions.getVendor());
357 ALOGI("renderer : %s", extensions.getRenderer());
358 ALOGI("version : %s", extensions.getVersion());
359 ALOGI("extensions: %s", extensions.getExtension());
360 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
361 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
362}
363
Mathias Agopiana4912602012-07-12 14:25:33 -0700364status_t SurfaceFlinger::readyToRun()
365{
366 ALOGI( "SurfaceFlinger's main thread ready to run. "
367 "Initializing graphics H/W...");
368
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700369 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700370 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
371 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700372
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700373 // Initialize the H/W composer object. There may or may not be an
374 // actual hardware composer underneath.
375 mHwc = new HWComposer(this,
376 *static_cast<HWComposer::EventHandler *>(this));
377
Mathias Agopiana4912602012-07-12 14:25:33 -0700378 // Initialize the main display
379 // create native window to main display
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700380 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700381 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700382 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
383 exit(0);
384 }
385
Mathias Agopiane57f2922012-08-09 16:29:12 -0700386 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
387 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700388
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 // initialize the config and context
390 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700391 ANativeWindow* const anw = stc.get();
392 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700393 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
394 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700395
396 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700397
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700398 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700399 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700400 mCurrentState.displays.add(mDefaultDisplays[i],
401 DisplayDeviceState((DisplayDevice::DisplayType)i));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700402 }
403 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700404 DisplayDevice::DISPLAY_PRIMARY,
405 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiane60b0682012-08-21 23:34:09 -0700406 anw, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700407 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700408
409 // initialize OpenGL ES
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700410 initializeGL(mEGLDisplay, hw);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800411
Mathias Agopian028508c2012-07-25 21:12:12 -0700412 // start the EventThread
413 mEventThread = new EventThread(this);
414 mEventQueue.setEventThread(mEventThread);
415
Mathias Agopian92a979a2012-08-02 18:32:23 -0700416 // initialize our drawing state
417 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700418
Mathias Agopiana4912602012-07-12 14:25:33 -0700419 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800420 mReadyToRunBarrier.open();
421
Andy McFadden13a082e2012-08-24 10:16:42 -0700422 // set initial conditions (e.g. unblank default device)
423 initializeDisplays();
424
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700425 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700426 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 return NO_ERROR;
429}
430
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700431int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
432 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
433 type : mHwc->allocateDisplayId();
434}
435
Mathias Agopiana67e4182012-06-19 17:26:12 -0700436void SurfaceFlinger::startBootAnim() {
437 // start boot animation
438 property_set("service.bootanim.exit", "0");
439 property_set("ctl.start", "bootanim");
440}
441
Mathias Agopiana4912602012-07-12 14:25:33 -0700442uint32_t SurfaceFlinger::getMaxTextureSize() const {
443 return mMaxTextureSize;
444}
445
446uint32_t SurfaceFlinger::getMaxViewportDims() const {
447 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
448 mMaxViewportDims[0] : mMaxViewportDims[1];
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800452
Jamie Gennis582270d2011-08-17 18:19:00 -0700453bool SurfaceFlinger::authenticateSurfaceTexture(
454 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800455 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700456 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800457
458 // Check the visible layer list for the ISurface
459 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
460 size_t count = currentLayers.size();
461 for (size_t i=0 ; i<count ; i++) {
462 const sp<LayerBase>& layer(currentLayers[i]);
463 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700464 if (lbc != NULL) {
465 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
466 if (lbcBinder == surfaceTextureBinder) {
467 return true;
468 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800469 }
470 }
471
472 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700473 // SurfaceTexture gets destroyed before all the clients are done using it,
474 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800475 // will instead fail later on when the client tries to use the surface,
476 // which should be reported as "surface XYZ returned an -ENODEV". The
477 // purgatorized layers are no less authentic than the visible ones, so this
478 // should not cause any harm.
479 size_t purgatorySize = mLayerPurgatory.size();
480 for (size_t i=0 ; i<purgatorySize ; i++) {
481 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
482 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700483 if (lbc != NULL) {
484 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
485 if (lbcBinder == surfaceTextureBinder) {
486 return true;
487 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800488 }
489 }
490
491 return false;
492}
493
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700494status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
495 // TODO: this is mostly here only for compatibility
496 // the display size is needed but the display metrics should come from elsewhere
497 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
498 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700499 return BAD_INDEX;
500 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700501
502 const HWComposer& hwc(getHwComposer());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700503 float xdpi = hwc.getDpiX(HWC_DISPLAY_PRIMARY);
504 float ydpi = hwc.getDpiY(HWC_DISPLAY_PRIMARY);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700505
506 // TODO: Not sure if display density should handled by SF any longer
507 class Density {
508 static int getDensityFromProperty(char const* propName) {
509 char property[PROPERTY_VALUE_MAX];
510 int density = 0;
511 if (property_get(propName, property, NULL) > 0) {
512 density = atoi(property);
513 }
514 return density;
515 }
516 public:
517 static int getEmuDensity() {
518 return getDensityFromProperty("qemu.sf.lcd_density"); }
519 static int getBuildDensity() {
520 return getDensityFromProperty("ro.sf.lcd_density"); }
521 };
522 // The density of the device is provided by a build property
523 float density = Density::getBuildDensity() / 160.0f;
524 if (density == 0) {
525 // the build doesn't provide a density -- this is wrong!
526 // use xdpi instead
527 ALOGE("ro.sf.lcd_density must be defined as a build property");
528 density = xdpi / 160.0f;
529 }
530 if (Density::getEmuDensity()) {
531 // if "qemu.sf.lcd_density" is specified, it overrides everything
532 xdpi = ydpi = density = Density::getEmuDensity();
533 density /= 160.0f;
534 }
535
Mathias Agopian42977342012-08-05 00:40:46 -0700536 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
537 info->w = hw->getWidth();
538 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700539 info->xdpi = xdpi;
540 info->ydpi = ydpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700541 info->fps = float(1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY));
Mathias Agopian8b736f12012-08-13 17:54:26 -0700542 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700543 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700544 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700545 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700546 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700547}
548
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800549// ----------------------------------------------------------------------------
550
551sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800552 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700553}
554
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700555void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700556
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700557 sp<IBinder> token;
558 { // scope for the lock
559 Mutex::Autolock _l(mStateLock);
560 token = mExtDisplayToken;
561 }
562
563 if (token == 0) {
564 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700565 }
566
567 { // scope for the lock
568 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700569 if (surface == 0) {
570 // release our current display. we're guarantee to have
571 // a reference to it (token), while we hold the lock
572 mExtDisplayToken = 0;
573 } else {
574 mExtDisplayToken = token;
575 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700576
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700577 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
578 info.surface = surface;
579 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700580 }
581}
582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800584
585void SurfaceFlinger::waitForEvent() {
586 mEventQueue.waitMessage();
587}
588
589void SurfaceFlinger::signalTransaction() {
590 mEventQueue.invalidate();
591}
592
593void SurfaceFlinger::signalLayerUpdate() {
594 mEventQueue.invalidate();
595}
596
597void SurfaceFlinger::signalRefresh() {
598 mEventQueue.refresh();
599}
600
601status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
602 nsecs_t reltime, uint32_t flags) {
603 return mEventQueue.postMessage(msg, reltime);
604}
605
606status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
607 nsecs_t reltime, uint32_t flags) {
608 status_t res = mEventQueue.postMessage(msg, reltime);
609 if (res == NO_ERROR) {
610 msg->wait();
611 }
612 return res;
613}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614
Mathias Agopian4fec8732012-06-29 14:12:52 -0700615bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 return true;
618}
619
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700620void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700621 if (mEventThread == NULL) {
622 // This is a temporary workaround for b/7145521. A non-null pointer
623 // does not mean EventThread has finished initializing, so this
624 // is not a correct fix.
625 ALOGW("WARNING: EventThread not started, ignoring vsync");
626 return;
627 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700628 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
629 // we should only receive DisplayDevice::DisplayType from the vsync callback
630 const wp<IBinder>& token(mDefaultDisplays[type]);
631 mEventThread->onVSyncReceived(token, timestamp);
632 }
Mathias Agopian86303202012-07-24 22:46:10 -0700633}
634
635void SurfaceFlinger::eventControl(int event, int enabled) {
636 getHwComposer().eventControl(event, enabled);
637}
638
Mathias Agopian4fec8732012-06-29 14:12:52 -0700639void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800640 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800641 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700642 case MessageQueue::INVALIDATE:
643 handleMessageTransaction();
644 handleMessageInvalidate();
645 signalRefresh();
646 break;
647 case MessageQueue::REFRESH:
648 handleMessageRefresh();
649 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800650 }
651}
652
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700654 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700655 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700656 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700657 }
658}
659
660void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700661 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700662 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700663}
664
665void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700666 ATRACE_CALL();
667 preComposition();
668 rebuildLayerStacks();
669 setUpHWComposer();
670 doDebugFlashRegions();
671 doComposition();
672 postComposition();
673}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700674
Mathias Agopiancd60f992012-08-16 16:28:27 -0700675void SurfaceFlinger::doDebugFlashRegions()
676{
677 // is debugging enabled
678 if (CC_LIKELY(!mDebugRegion))
679 return;
680
681 const bool repaintEverything = mRepaintEverything;
682 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
683 const sp<DisplayDevice>& hw(mDisplays[dpy]);
684 if (hw->canDraw()) {
685 // transform the dirty region into this screen's coordinate space
686 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
687 if (!dirtyRegion.isEmpty()) {
688 // redraw the whole screen
689 doComposeSurfaces(hw, Region(hw->bounds()));
690
691 // and draw the dirty region
692 glDisable(GL_TEXTURE_EXTERNAL_OES);
693 glDisable(GL_TEXTURE_2D);
694 glDisable(GL_BLEND);
695 glColor4f(1, 0, 1, 1);
696 const int32_t height = hw->getHeight();
697 Region::const_iterator it = dirtyRegion.begin();
698 Region::const_iterator const end = dirtyRegion.end();
699 while (it != end) {
700 const Rect& r = *it++;
701 GLfloat vertices[][2] = {
702 { r.left, height - r.top },
703 { r.left, height - r.bottom },
704 { r.right, height - r.bottom },
705 { r.right, height - r.top }
706 };
707 glVertexPointer(2, GL_FLOAT, 0, vertices);
708 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
709 }
710 hw->compositionComplete();
711 // FIXME
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700712 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700713 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
714 }
715 }
716 }
717 }
718
719 postFramebuffer();
720
721 if (mDebugRegion > 1) {
722 usleep(mDebugRegion * 1000);
723 }
724}
725
726void SurfaceFlinger::preComposition()
727{
728 bool needExtraInvalidate = false;
729 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
730 const size_t count = currentLayers.size();
731 for (size_t i=0 ; i<count ; i++) {
732 if (currentLayers[i]->onPreComposition()) {
733 needExtraInvalidate = true;
734 }
735 }
736 if (needExtraInvalidate) {
737 signalLayerUpdate();
738 }
739}
740
741void SurfaceFlinger::postComposition()
742{
743 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
744 const size_t count = currentLayers.size();
745 for (size_t i=0 ; i<count ; i++) {
746 currentLayers[i]->onPostComposition();
747 }
748}
749
750void SurfaceFlinger::rebuildLayerStacks() {
751 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700752 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700753 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700754 mVisibleRegionsDirty = false;
755 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700756
Mathias Agopian87baae12012-07-31 12:38:26 -0700757 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700758 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700759 Region opaqueRegion;
760 Region dirtyRegion;
761 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700762 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700763 const Transform& tr(hw->getTransform());
764 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700765 if (hw->canDraw()) {
766 SurfaceFlinger::computeVisibleRegions(currentLayers,
767 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700768
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700769 const size_t count = currentLayers.size();
770 for (size_t i=0 ; i<count ; i++) {
771 const sp<LayerBase>& layer(currentLayers[i]);
772 const Layer::State& s(layer->drawingState());
773 if (s.layerStack == hw->getLayerStack()) {
774 Region visibleRegion(tr.transform(layer->visibleRegion));
775 visibleRegion.andSelf(bounds);
776 if (!visibleRegion.isEmpty()) {
777 layersSortedByZ.add(layer);
778 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700779 }
780 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700781 }
Mathias Agopian42977342012-08-05 00:40:46 -0700782 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700783 hw->undefinedRegion.set(bounds);
784 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
785 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700786 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700787 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700788}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700789
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700791 HWComposer& hwc(getHwComposer());
792 if (hwc.initCheck() == NO_ERROR) {
793 // build the h/w work list
794 const bool workListsDirty = mHwWorkListDirty;
795 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700796 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700797 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700798 const int32_t id = hw->getHwcDisplayId();
799 if (id >= 0) {
800 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700802 const size_t count = currentLayers.size();
803 if (hwc.createWorkList(id, count) >= 0) {
804 HWComposer::LayerListIterator cur = hwc.begin(id);
805 const HWComposer::LayerListIterator end = hwc.end(id);
806 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
807 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700808
Mathias Agopiane60b0682012-08-21 23:34:09 -0700809 if (CC_UNLIKELY(workListsDirty)) {
810 layer->setGeometry(hw, *cur);
811 if (mDebugDisableHWC || mDebugRegion) {
812 cur->setSkip(true);
813 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700814 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700815
Mathias Agopiane60b0682012-08-21 23:34:09 -0700816 /*
817 * update the per-frame h/w composer data for each layer
818 * and build the transparent region of the FB
819 */
820 layer->setPerFrameData(hw, *cur);
821 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700822 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700823 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700824 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700825 status_t err = hwc.prepare();
826 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
827 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700828}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700829
Mathias Agopiancd60f992012-08-16 16:28:27 -0700830void SurfaceFlinger::doComposition() {
831 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700833 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700834 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700835 if (hw->canDraw()) {
836 // transform the dirty region into this screen's coordinate space
837 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
838 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700839 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700840 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700841 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842 hw->dirtyRegion.clear();
843 hw->flip(hw->swapRegion);
844 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700845 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700846 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700847 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850}
851
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852void SurfaceFlinger::postFramebuffer()
853{
Mathias Agopian841cde52012-03-01 15:44:37 -0800854 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800855
Mathias Agopiana44b0412011-10-16 18:46:35 -0700856 const nsecs_t now = systemTime();
857 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700858
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700859 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700860 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700861 // FIXME: EGL spec says:
862 // "surface must be bound to the calling thread's current context,
863 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700864 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700865 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700866 }
867
Mathias Agopian92a979a2012-08-02 18:32:23 -0700868 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700869 sp<const DisplayDevice> hw(mDisplays[dpy]);
870 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700871 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700872 int32_t id = hw->getHwcDisplayId();
873 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700874 HWComposer::LayerListIterator cur = hwc.begin(id);
875 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700876 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700877 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700878 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700879 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700880 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700881 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700882 }
Jesse Hallef194142012-06-14 14:45:17 -0700883 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800884 }
885
Mathias Agopiana44b0412011-10-16 18:46:35 -0700886 mLastSwapBufferTime = systemTime() - now;
887 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888}
889
Mathias Agopian87baae12012-07-31 12:38:26 -0700890void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891{
Mathias Agopian841cde52012-03-01 15:44:37 -0800892 ATRACE_CALL();
893
Mathias Agopianca4d3602011-05-19 15:38:14 -0700894 Mutex::Autolock _l(mStateLock);
895 const nsecs_t now = systemTime();
896 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897
Mathias Agopianca4d3602011-05-19 15:38:14 -0700898 // Here we're guaranteed that some transaction flags are set
899 // so we can call handleTransactionLocked() unconditionally.
900 // We call getTransactionFlags(), which will also clear the flags,
901 // with mStateLock held to guarantee that mCurrentState won't change
902 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700903
Mathias Agopiane57f2922012-08-09 16:29:12 -0700904 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700905 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700906
Mathias Agopianca4d3602011-05-19 15:38:14 -0700907 mLastTransactionTime = systemTime() - now;
908 mDebugInTransaction = 0;
909 invalidateHwcGeometry();
910 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700911}
912
Mathias Agopian87baae12012-07-31 12:38:26 -0700913void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700914{
915 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 const size_t count = currentLayers.size();
917
918 /*
919 * Traversal of the children
920 * (perform the transaction for each of them if needed)
921 */
922
Mathias Agopian3559b072012-08-15 13:46:03 -0700923 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700925 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
927 if (!trFlags) continue;
928
929 const uint32_t flags = layer->doTransaction(0);
930 if (flags & Layer::eVisibleRegion)
931 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 }
933 }
934
935 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700936 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 */
938
Mathias Agopiane57f2922012-08-09 16:29:12 -0700939 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700940 // here we take advantage of Vector's copy-on-write semantics to
941 // improve performance by skipping the transaction entirely when
942 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700943 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
944 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700945 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700947 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700948 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700949
950 // find the displays that were removed
951 // (ie: in drawing state but not in current state)
952 // also handle displays that changed
953 // (ie: displays that are in both lists)
954 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700955 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
956 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700957 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700958 if (!draw[i].isMainDisplay()) {
959 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700960 } else {
961 ALOGW("trying to remove the main display");
962 }
963 } else {
964 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700965 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700966 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700967 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700968 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700969 // recreating the DisplayDevice, so we just remove it
970 // from the drawing state, so that it get re-added
971 // below.
972 mDisplays.removeItem(display);
973 mDrawingState.displays.removeItemsAt(i);
974 dc--; i--;
975 // at this point we must loop to the next item
976 continue;
977 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700978
Mathias Agopian93997a82012-08-29 17:30:36 -0700979 const sp<DisplayDevice>& disp(getDisplayDevice(display));
980 if (disp != NULL) {
981 if (state.layerStack != draw[i].layerStack) {
982 disp->setLayerStack(state.layerStack);
983 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700984 if ((state.orientation != draw[i].orientation)
985 || (state.viewport != draw[i].viewport)
986 || (state.frame != draw[i].frame))
987 {
988 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -0700989 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -0700990 }
Andy McFadden69052052012-09-14 16:10:11 -0700991
992 // Walk through all the layers in currentLayers,
993 // and update their transform hint.
994 //
995 // TODO: we could be much more clever about which
996 // layers we touch and how often we do these updates
997 // (e.g. only touch the layers associated with this
998 // display, and only on a rotation).
999 for (size_t i = 0; i < count; i++) {
1000 const sp<LayerBase>& layerBase = currentLayers[i];
1001 layerBase->updateTransformHint();
1002 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001003 }
1004 }
1005 }
1006
1007 // find displays that were added
1008 // (ie: in current state but not in drawing state)
1009 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001010 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001011 const DisplayDeviceState& state(curr[i]);
Mathias Agopian93997a82012-08-29 17:30:36 -07001012 if (state.surface != NULL) {
1013 sp<SurfaceTextureClient> stc(
1014 new SurfaceTextureClient(state.surface));
1015 const wp<IBinder>& display(curr.keyAt(i));
1016 sp<DisplayDevice> disp = new DisplayDevice(this,
1017 state.type, display, stc, 0, mEGLConfig);
1018 disp->setLayerStack(state.layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001019 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001020 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001021 mDisplays.add(display, disp);
1022 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001023 }
1024 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001026 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027
Mathias Agopian3559b072012-08-15 13:46:03 -07001028 /*
1029 * Perform our own transaction if needed
1030 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001031
Mathias Agopiancd60f992012-08-16 16:28:27 -07001032 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1033 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001034 // layers have been added
1035 mVisibleRegionsDirty = true;
1036 }
1037
1038 // some layers might have been removed, so
1039 // we need to update the regions they're exposing.
1040 if (mLayersRemoved) {
1041 mLayersRemoved = false;
1042 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001043 const size_t count = previousLayers.size();
1044 for (size_t i=0 ; i<count ; i++) {
1045 const sp<LayerBase>& layer(previousLayers[i]);
1046 if (currentLayers.indexOf(layer) < 0) {
1047 // this layer is not visible anymore
1048 // TODO: we could traverse the tree from front to back and
1049 // compute the actual visible region
1050 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001051 const Layer::State& s(layer->drawingState());
1052 Region visibleReg = s.transform.transform(
1053 Region(Rect(s.active.w, s.active.h)));
1054 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001055 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 }
1058
1059 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001060}
1061
1062void SurfaceFlinger::commitTransaction()
1063{
1064 if (!mLayersPendingRemoval.isEmpty()) {
1065 // Notify removed layers now that they can't be drawn from
1066 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1067 mLayersPendingRemoval[i]->onRemoved();
1068 }
1069 mLayersPendingRemoval.clear();
1070 }
1071
1072 mDrawingState = mCurrentState;
1073 mTransationPending = false;
1074 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075}
1076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001078 const LayerVector& currentLayers, uint32_t layerStack,
1079 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080{
Mathias Agopian841cde52012-03-01 15:44:37 -08001081 ATRACE_CALL();
1082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 Region aboveOpaqueLayers;
1084 Region aboveCoveredLayers;
1085 Region dirty;
1086
Mathias Agopian87baae12012-07-31 12:38:26 -07001087 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088
1089 size_t i = currentLayers.size();
1090 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001091 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092
1093 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001094 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095
Mathias Agopian87baae12012-07-31 12:38:26 -07001096 // only consider the layers on the given later stack
1097 if (s.layerStack != layerStack)
1098 continue;
1099
Mathias Agopianab028732010-03-16 16:41:46 -07001100 /*
1101 * opaqueRegion: area of a surface that is fully opaque.
1102 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001104
1105 /*
1106 * visibleRegion: area of a surface that is visible on screen
1107 * and not fully transparent. This is essentially the layer's
1108 * footprint minus the opaque regions above it.
1109 * Areas covered by a translucent surface are considered visible.
1110 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001112
1113 /*
1114 * coveredRegion: area of a surface that is covered by all
1115 * visible regions above it (which includes the translucent areas).
1116 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001118
1119
1120 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001121 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001122 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001123 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001125 if (!visibleRegion.isEmpty()) {
1126 // Remove the transparent area from the visible region
1127 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001128 Region transparentRegionScreen;
1129 const Transform tr(s.transform);
1130 if (tr.transformed()) {
1131 if (tr.preserveRects()) {
1132 // transform the transparent region
1133 transparentRegionScreen = tr.transform(s.transparentRegion);
1134 } else {
1135 // transformation too complex, can't do the
1136 // transparent region optimization.
1137 transparentRegionScreen.clear();
1138 }
1139 } else {
1140 transparentRegionScreen = s.transparentRegion;
1141 }
1142 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
Mathias Agopianab028732010-03-16 16:41:46 -07001145 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001146 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001147 if (s.alpha==255 && !translucent &&
1148 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1149 // the opaque region is the layer's footprint
1150 opaqueRegion = visibleRegion;
1151 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 }
1153 }
1154
Mathias Agopianab028732010-03-16 16:41:46 -07001155 // Clip the covered region to the visible region
1156 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1157
1158 // Update aboveCoveredLayers for next (lower) layer
1159 aboveCoveredLayers.orSelf(visibleRegion);
1160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 // subtract the opaque region covered by the layers above us
1162 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163
1164 // compute this layer's dirty region
1165 if (layer->contentDirty) {
1166 // we need to invalidate the whole region
1167 dirty = visibleRegion;
1168 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001169 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 layer->contentDirty = false;
1171 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001172 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001173 * the exposed region consists of two components:
1174 * 1) what's VISIBLE now and was COVERED before
1175 * 2) what's EXPOSED now less what was EXPOSED before
1176 *
1177 * note that (1) is conservative, we start with the whole
1178 * visible region but only keep what used to be covered by
1179 * something -- which mean it may have been exposed.
1180 *
1181 * (2) handles areas that were not covered by anything but got
1182 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001183 */
Mathias Agopianab028732010-03-16 16:41:46 -07001184 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001185 const Region oldVisibleRegion = layer->visibleRegion;
1186 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001187 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1188 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
1190 dirty.subtractSelf(aboveOpaqueLayers);
1191
1192 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001193 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194
Mathias Agopianab028732010-03-16 16:41:46 -07001195 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 // Store the visible region is screen space
1199 layer->setVisibleRegion(visibleRegion);
1200 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 }
1202
Mathias Agopian87baae12012-07-31 12:38:26 -07001203 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204}
1205
Mathias Agopian87baae12012-07-31 12:38:26 -07001206void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1207 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001208 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001209 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1210 if (hw->getLayerStack() == layerStack) {
1211 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001212 }
1213 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001214}
1215
1216void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001218 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219
Mathias Agopian4fec8732012-06-29 14:12:52 -07001220 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001221 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001222 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001223 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001224 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001225 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001226 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001227 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001228 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001229
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001230 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231}
1232
Mathias Agopianad456f92011-01-13 17:53:01 -08001233void SurfaceFlinger::invalidateHwcGeometry()
1234{
1235 mHwWorkListDirty = true;
1236}
1237
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001238
Mathias Agopiancd60f992012-08-16 16:28:27 -07001239void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001240 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241{
Mathias Agopian87baae12012-07-31 12:38:26 -07001242 Region dirtyRegion(inDirtyRegion);
1243
Mathias Agopianb8a55602009-06-26 19:06:36 -07001244 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001245 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246
Mathias Agopian42977342012-08-05 00:40:46 -07001247 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001248 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001249 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1250 // takes a rectangle, we must make sure to update that whole
1251 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001252 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001253 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001254 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001255 // We need to redraw the rectangle that will be updated
1256 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001257 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001258 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001259 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001260 } else {
1261 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001262 dirtyRegion.set(hw->bounds());
1263 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 }
1265 }
1266
Mathias Agopiancd60f992012-08-16 16:28:27 -07001267 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268
Mathias Agopiancd60f992012-08-16 16:28:27 -07001269 // FIXME: we need to call eglSwapBuffers() on displays that have
1270 // GL composition and only on those.
1271 // however, currently hwc.commit() already does that for the main
Jesse Hall9ca48912012-08-30 13:42:23 -07001272 // display (if there is a hwc) and never for the other ones
1273 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL ||
1274 getHwComposer().initCheck() != NO_ERROR) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001275 // FIXME: EGL spec says:
1276 // "surface must be bound to the calling thread's current context,
1277 // for the current rendering API."
1278 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001279 }
1280
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001281 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001282 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283}
1284
Mathias Agopiancd60f992012-08-16 16:28:27 -07001285void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001286{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001287 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001288 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001289 HWComposer::LayerListIterator cur = hwc.begin(id);
1290 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001291
Mathias Agopian85d751c2012-08-29 16:59:24 -07001292 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1293 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001294 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001295
1296 // set the frame buffer
1297 glMatrixMode(GL_MODELVIEW);
1298 glLoadIdentity();
1299
1300 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001301 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001302 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001303 // when using overlays, we assume a fully transparent framebuffer
1304 // NOTE: we could reduce how much we need to clear, for instance
1305 // remove where there are opaque FB layers. however, on some
1306 // GPUs doing a "clean slate" glClear might be more efficient.
1307 // We'll revisit later if needed.
1308 glClearColor(0, 0, 0, 0);
1309 glClear(GL_COLOR_BUFFER_BIT);
1310 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001311 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001312 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001313 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001314 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001315 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001316 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001317 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001318 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001319
Mathias Agopian85d751c2012-08-29 16:59:24 -07001320 /*
1321 * and then, render the layers targeted at the framebuffer
1322 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001323
Mathias Agopian85d751c2012-08-29 16:59:24 -07001324 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1325 const size_t count = layers.size();
1326 const Transform& tr = hw->getTransform();
1327 if (cur != end) {
1328 // we're using h/w composer
1329 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001330 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001331 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001332 if (!clip.isEmpty()) {
1333 switch (cur->getCompositionType()) {
1334 case HWC_OVERLAY: {
1335 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1336 && i
1337 && layer->isOpaque()
1338 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001339 // never clear the very first layer since we're
1340 // guaranteed the FB is already cleared
1341 layer->clearWithOpenGL(hw, clip);
1342 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001343 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001344 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001345 case HWC_FRAMEBUFFER: {
1346 layer->draw(hw, clip);
1347 break;
1348 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001349 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001350 }
1351 layer->setAcquireFence(hw, *cur);
1352 }
1353 } else {
1354 // we're not using h/w composer
1355 for (size_t i=0 ; i<count ; ++i) {
1356 const sp<LayerBase>& layer(layers[i]);
1357 const Region clip(dirty.intersect(
1358 tr.transform(layer->visibleRegion)));
1359 if (!clip.isEmpty()) {
1360 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001361 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001362 }
1363 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364}
1365
Mathias Agopian55801e42012-08-27 18:54:24 -07001366void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1367 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001369 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001370 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001371 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001372 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001373
Mathias Agopian55801e42012-08-27 18:54:24 -07001374 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001375 Region::const_iterator it = region.begin();
1376 Region::const_iterator const end = region.end();
1377 while (it != end) {
1378 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001379 GLfloat vertices[][2] = {
1380 { r.left, height - r.top },
1381 { r.left, height - r.bottom },
1382 { r.right, height - r.bottom },
1383 { r.right, height - r.top }
1384 };
1385 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001386 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1387 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388}
1389
Mathias Agopian96f08192010-06-02 23:28:45 -07001390ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1391 const sp<LayerBaseClient>& lbc)
1392{
Mathias Agopian96f08192010-06-02 23:28:45 -07001393 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001394 size_t name = client->attachLayer(lbc);
1395
Mathias Agopian96f08192010-06-02 23:28:45 -07001396 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001397 Mutex::Autolock _l(mStateLock);
1398 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001399
Mathias Agopian4f113742011-05-03 16:21:41 -07001400 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001401}
1402
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001403status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404{
1405 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001406 status_t err = purgatorizeLayer_l(layer);
1407 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001408 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001409 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410}
1411
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001412status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413{
1414 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1415 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001416 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 return NO_ERROR;
1418 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001419 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420}
1421
Mathias Agopian9a112062009-04-17 19:36:26 -07001422status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1423{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001424 // First add the layer to the purgatory list, which makes sure it won't
1425 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001426 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001427 if (err >= 0) {
1428 mLayerPurgatory.add(layerBase);
1429 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001430
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001431 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001432
Mathias Agopian3d579642009-06-04 18:46:21 -07001433 // it's possible that we don't find a layer, because it might
1434 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001435 // from the user because there is a race between Client::destroySurface(),
1436 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001437 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1438}
1439
Mathias Agopiandea20b12011-05-03 17:04:02 -07001440uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1441{
1442 return android_atomic_release_load(&mTransactionFlags);
1443}
1444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1446{
1447 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1448}
1449
Mathias Agopianbb641242010-05-18 17:06:55 -07001450uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451{
1452 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1453 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001454 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 }
1456 return old;
1457}
1458
Mathias Agopian8b33f032012-07-24 20:43:54 -07001459void SurfaceFlinger::setTransactionState(
1460 const Vector<ComposerState>& state,
1461 const Vector<DisplayState>& displays,
1462 uint32_t flags)
1463{
Mathias Agopian698c0872011-06-28 19:09:31 -07001464 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001465 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001466
1467 size_t count = displays.size();
1468 for (size_t i=0 ; i<count ; i++) {
1469 const DisplayState& s(displays[i]);
1470 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001471 }
1472
Mathias Agopiane57f2922012-08-09 16:29:12 -07001473 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001474 for (size_t i=0 ; i<count ; i++) {
1475 const ComposerState& s(state[i]);
1476 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001477 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001478 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001479
Mathias Agopian386aa982011-11-07 21:58:03 -08001480 if (transactionFlags) {
1481 // this triggers the transaction
1482 setTransactionFlags(transactionFlags);
1483
1484 // if this is a synchronous transaction, wait for it to take effect
1485 // before returning.
1486 if (flags & eSynchronous) {
1487 mTransationPending = true;
1488 }
1489 while (mTransationPending) {
1490 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1491 if (CC_UNLIKELY(err != NO_ERROR)) {
1492 // just in case something goes wrong in SF, return to the
1493 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001494 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001495 mTransationPending = false;
1496 break;
1497 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001498 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 }
1500}
1501
Mathias Agopiane57f2922012-08-09 16:29:12 -07001502uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1503{
1504 uint32_t flags = 0;
1505 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001506 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001507 const uint32_t what = s.what;
1508 if (what & DisplayState::eSurfaceChanged) {
1509 if (disp.surface->asBinder() != s.surface->asBinder()) {
1510 disp.surface = s.surface;
1511 flags |= eDisplayTransactionNeeded;
1512 }
1513 }
1514 if (what & DisplayState::eLayerStackChanged) {
1515 if (disp.layerStack != s.layerStack) {
1516 disp.layerStack = s.layerStack;
1517 flags |= eDisplayTransactionNeeded;
1518 }
1519 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001520 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001521 if (disp.orientation != s.orientation) {
1522 disp.orientation = s.orientation;
1523 flags |= eDisplayTransactionNeeded;
1524 }
1525 if (disp.frame != s.frame) {
1526 disp.frame = s.frame;
1527 flags |= eDisplayTransactionNeeded;
1528 }
1529 if (disp.viewport != s.viewport) {
1530 disp.viewport = s.viewport;
1531 flags |= eDisplayTransactionNeeded;
1532 }
1533 }
1534 }
1535 return flags;
1536}
1537
1538uint32_t SurfaceFlinger::setClientStateLocked(
1539 const sp<Client>& client,
1540 const layer_state_t& s)
1541{
1542 uint32_t flags = 0;
1543 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1544 if (layer != 0) {
1545 const uint32_t what = s.what;
1546 if (what & layer_state_t::ePositionChanged) {
1547 if (layer->setPosition(s.x, s.y))
1548 flags |= eTraversalNeeded;
1549 }
1550 if (what & layer_state_t::eLayerChanged) {
1551 // NOTE: index needs to be calculated before we update the state
1552 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1553 if (layer->setLayer(s.z)) {
1554 mCurrentState.layersSortedByZ.removeAt(idx);
1555 mCurrentState.layersSortedByZ.add(layer);
1556 // we need traversal (state changed)
1557 // AND transaction (list changed)
1558 flags |= eTransactionNeeded|eTraversalNeeded;
1559 }
1560 }
1561 if (what & layer_state_t::eSizeChanged) {
1562 if (layer->setSize(s.w, s.h)) {
1563 flags |= eTraversalNeeded;
1564 }
1565 }
1566 if (what & layer_state_t::eAlphaChanged) {
1567 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1568 flags |= eTraversalNeeded;
1569 }
1570 if (what & layer_state_t::eMatrixChanged) {
1571 if (layer->setMatrix(s.matrix))
1572 flags |= eTraversalNeeded;
1573 }
1574 if (what & layer_state_t::eTransparentRegionChanged) {
1575 if (layer->setTransparentRegionHint(s.transparentRegion))
1576 flags |= eTraversalNeeded;
1577 }
1578 if (what & layer_state_t::eVisibilityChanged) {
1579 if (layer->setFlags(s.flags, s.mask))
1580 flags |= eTraversalNeeded;
1581 }
1582 if (what & layer_state_t::eCropChanged) {
1583 if (layer->setCrop(s.crop))
1584 flags |= eTraversalNeeded;
1585 }
1586 if (what & layer_state_t::eLayerStackChanged) {
1587 // NOTE: index needs to be calculated before we update the state
1588 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1589 if (layer->setLayerStack(s.layerStack)) {
1590 mCurrentState.layersSortedByZ.removeAt(idx);
1591 mCurrentState.layersSortedByZ.add(layer);
1592 // we need traversal (state changed)
1593 // AND transaction (list changed)
1594 flags |= eTransactionNeeded|eTraversalNeeded;
1595 }
1596 }
1597 }
1598 return flags;
1599}
1600
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001601sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001602 ISurfaceComposerClient::surface_data_t* params,
1603 const String8& name,
1604 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001605 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 uint32_t flags)
1607{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001608 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001609 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001610
1611 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001612 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001613 int(w), int(h));
1614 return surfaceHandle;
1615 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001616
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001617 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001618 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1619 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001620 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001622 case ISurfaceComposerClient::eFXSurfaceBlur:
1623 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001624 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001626 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001627 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001628 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629 }
1630
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001631 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001632 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001633 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001634 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001636 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001637 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001638 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001639 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001640 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 }
1642
1643 return surfaceHandle;
1644}
1645
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001646sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001647 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001648 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001649 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650{
1651 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001652 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 case PIXEL_FORMAT_TRANSPARENT:
1654 case PIXEL_FORMAT_TRANSLUCENT:
1655 format = PIXEL_FORMAT_RGBA_8888;
1656 break;
1657 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001658#ifdef NO_RGBX_8888
1659 format = PIXEL_FORMAT_RGB_565;
1660#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001661 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001662#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 break;
1664 }
1665
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001666#ifdef NO_RGBX_8888
1667 if (format == PIXEL_FORMAT_RGBX_8888)
1668 format = PIXEL_FORMAT_RGBA_8888;
1669#endif
1670
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001671 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001672 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001673 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001674 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001675 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 }
1677 return layer;
1678}
1679
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001680sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001681 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001682 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001684 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001685 return layer;
1686}
1687
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001688sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001689 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001690 uint32_t w, uint32_t h, uint32_t flags)
1691{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001692 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 return layer;
1694}
1695
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001696status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697{
Mathias Agopian9a112062009-04-17 19:36:26 -07001698 /*
1699 * called by the window manager, when a surface should be marked for
1700 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001701 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001702 * The surface is removed from the current and drawing lists, but placed
1703 * in the purgatory queue, so it's not destroyed right-away (we need
1704 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001705 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001706
Mathias Agopian48d819a2009-09-10 19:41:18 -07001707 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001708 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001709 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001710
Mathias Agopian48d819a2009-09-10 19:41:18 -07001711 if (layer != 0) {
1712 err = purgatorizeLayer_l(layer);
1713 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001714 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001715 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001716 }
1717 return err;
1718}
1719
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001720status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001721{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001722 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001723 status_t err = NO_ERROR;
1724 sp<LayerBaseClient> l(layer.promote());
1725 if (l != NULL) {
1726 Mutex::Autolock _l(mStateLock);
1727 err = removeLayer_l(l);
1728 if (err == NAME_NOT_FOUND) {
1729 // The surface wasn't in the current list, which means it was
1730 // removed already, which means it is in the purgatory,
1731 // and need to be removed from there.
1732 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001733 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001734 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001735 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001736 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001737 "error removing layer=%p (%s)", l.get(), strerror(-err));
1738 }
1739 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740}
1741
Mathias Agopianb60314a2012-04-10 22:09:54 -07001742// ---------------------------------------------------------------------------
1743
Andy McFadden13a082e2012-08-24 10:16:42 -07001744void SurfaceFlinger::onInitializeDisplays() {
1745 // reset screen orientation
1746 Vector<ComposerState> state;
1747 Vector<DisplayState> displays;
1748 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001749 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001750 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001751 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001752 d.frame.makeInvalid();
1753 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001754 displays.add(d);
1755 setTransactionState(state, displays, 0);
1756
1757 // XXX: this should init default device to "unblank" and all other devices to "blank"
1758 onScreenAcquired();
1759}
1760
1761void SurfaceFlinger::initializeDisplays() {
1762 class MessageScreenInitialized : public MessageBase {
1763 SurfaceFlinger* flinger;
1764 public:
1765 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1766 virtual bool handler() {
1767 flinger->onInitializeDisplays();
1768 return true;
1769 }
1770 };
1771 sp<MessageBase> msg = new MessageScreenInitialized(this);
1772 postMessageAsync(msg); // we may be called from main thread, use async message
1773}
1774
1775
Mathias Agopianb60314a2012-04-10 22:09:54 -07001776void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001777 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001778 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001779 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001780 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001781 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001782 mVisibleRegionsDirty = true;
1783 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784}
1785
Mathias Agopianb60314a2012-04-10 22:09:54 -07001786void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001787 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001788 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1789 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001790 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001791 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001792 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001793 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001794 // from this point on, SF will stop drawing
1795 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796}
1797
Colin Cross8e533062012-06-07 13:17:52 -07001798void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001799 class MessageScreenAcquired : public MessageBase {
1800 SurfaceFlinger* flinger;
1801 public:
1802 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1803 virtual bool handler() {
1804 flinger->onScreenAcquired();
1805 return true;
1806 }
1807 };
1808 sp<MessageBase> msg = new MessageScreenAcquired(this);
1809 postMessageSync(msg);
1810}
1811
Colin Cross8e533062012-06-07 13:17:52 -07001812void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001813 class MessageScreenReleased : public MessageBase {
1814 SurfaceFlinger* flinger;
1815 public:
1816 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1817 virtual bool handler() {
1818 flinger->onScreenReleased();
1819 return true;
1820 }
1821 };
1822 sp<MessageBase> msg = new MessageScreenReleased(this);
1823 postMessageSync(msg);
1824}
1825
1826// ---------------------------------------------------------------------------
1827
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1829{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001830 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831 char buffer[SIZE];
1832 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001833
1834 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835 snprintf(buffer, SIZE, "Permission Denial: "
1836 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1837 IPCThreadState::self()->getCallingPid(),
1838 IPCThreadState::self()->getCallingUid());
1839 result.append(buffer);
1840 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001841 // Try to get the main lock, but don't insist if we can't
1842 // (this would indicate SF is stuck, but we want to be able to
1843 // print something in dumpsys).
1844 int retry = 3;
1845 while (mStateLock.tryLock()<0 && --retry>=0) {
1846 usleep(1000000);
1847 }
1848 const bool locked(retry >= 0);
1849 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001850 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001851 "SurfaceFlinger appears to be unresponsive, "
1852 "dumping anyways (no locks held)\n");
1853 result.append(buffer);
1854 }
1855
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001856 bool dumpAll = true;
1857 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001858 size_t numArgs = args.size();
1859 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001860 if ((index < numArgs) &&
1861 (args[index] == String16("--list"))) {
1862 index++;
1863 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001864 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001865 }
1866
1867 if ((index < numArgs) &&
1868 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001869 index++;
1870 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001871 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001872 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001873
1874 if ((index < numArgs) &&
1875 (args[index] == String16("--latency-clear"))) {
1876 index++;
1877 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001878 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001879 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001881
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001882 if (dumpAll) {
1883 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001884 }
1885
Mathias Agopian9795c422009-08-26 16:36:26 -07001886 if (locked) {
1887 mStateLock.unlock();
1888 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889 }
1890 write(fd, result.string(), result.size());
1891 return NO_ERROR;
1892}
1893
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001894void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1895 String8& result, char* buffer, size_t SIZE) const
1896{
1897 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1898 const size_t count = currentLayers.size();
1899 for (size_t i=0 ; i<count ; i++) {
1900 const sp<LayerBase>& layer(currentLayers[i]);
1901 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1902 result.append(buffer);
1903 }
1904}
1905
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001906void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1907 String8& result, char* buffer, size_t SIZE) const
1908{
1909 String8 name;
1910 if (index < args.size()) {
1911 name = String8(args[index]);
1912 index++;
1913 }
1914
1915 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1916 const size_t count = currentLayers.size();
1917 for (size_t i=0 ; i<count ; i++) {
1918 const sp<LayerBase>& layer(currentLayers[i]);
1919 if (name.isEmpty()) {
1920 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1921 result.append(buffer);
1922 }
1923 if (name.isEmpty() || (name == layer->getName())) {
1924 layer->dumpStats(result, buffer, SIZE);
1925 }
1926 }
1927}
1928
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001929void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1930 String8& result, char* buffer, size_t SIZE) const
1931{
1932 String8 name;
1933 if (index < args.size()) {
1934 name = String8(args[index]);
1935 index++;
1936 }
1937
1938 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1939 const size_t count = currentLayers.size();
1940 for (size_t i=0 ; i<count ; i++) {
1941 const sp<LayerBase>& layer(currentLayers[i]);
1942 if (name.isEmpty() || (name == layer->getName())) {
1943 layer->clearStats();
1944 }
1945 }
1946}
1947
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001948void SurfaceFlinger::dumpAllLocked(
1949 String8& result, char* buffer, size_t SIZE) const
1950{
1951 // figure out if we're stuck somewhere
1952 const nsecs_t now = systemTime();
1953 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1954 const nsecs_t inTransaction(mDebugInTransaction);
1955 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1956 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1957
1958 /*
1959 * Dump the visible layer list
1960 */
1961 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1962 const size_t count = currentLayers.size();
1963 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1964 result.append(buffer);
1965 for (size_t i=0 ; i<count ; i++) {
1966 const sp<LayerBase>& layer(currentLayers[i]);
1967 layer->dump(result, buffer, SIZE);
1968 }
1969
1970 /*
1971 * Dump the layers in the purgatory
1972 */
1973
1974 const size_t purgatorySize = mLayerPurgatory.size();
1975 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1976 result.append(buffer);
1977 for (size_t i=0 ; i<purgatorySize ; i++) {
1978 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1979 layer->shortDump(result, buffer, SIZE);
1980 }
1981
1982 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001983 * Dump Display state
1984 */
1985
1986 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1987 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1988 snprintf(buffer, SIZE,
1989 "+ DisplayDevice[%u]\n"
Jeff Brown4fb39992012-09-07 12:55:10 -07001990 " type=%x, layerStack=%u, (%4dx%4d), orient=%2d (type=%08x), "
Jeff Brown6e220a62012-09-13 19:22:41 -07001991 "flips=%u, secure=%d, numLayers=%u, v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], "
1992 "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 -07001993 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001994 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001995 hw->getWidth(), hw->getHeight(),
1996 hw->getOrientation(), hw->getTransform().getType(),
1997 hw->getPageFlipCount(),
1998 hw->getSecureLayerVisible(),
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001999 hw->getVisibleLayersSortedByZ().size(),
2000 hw->getViewport().left, hw->getViewport().top, hw->getViewport().right, hw->getViewport().bottom,
Jeff Brown6e220a62012-09-13 19:22:41 -07002001 hw->getFrame().left, hw->getFrame().top, hw->getFrame().right, hw->getFrame().bottom,
2002 hw->getTransform()[0][0], hw->getTransform()[1][0], hw->getTransform()[2][0],
2003 hw->getTransform()[0][1], hw->getTransform()[1][1], hw->getTransform()[2][1],
2004 hw->getTransform()[0][2], hw->getTransform()[1][2], hw->getTransform()[2][2]);
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002005
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002006 result.append(buffer);
2007 }
2008
2009 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002010 * Dump SurfaceFlinger global state
2011 */
2012
2013 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2014 result.append(buffer);
2015
Mathias Agopian888c8222012-08-04 21:10:38 -07002016 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002017 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002018 const GLExtensions& extensions(GLExtensions::getInstance());
2019 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2020 extensions.getVendor(),
2021 extensions.getRenderer(),
2022 extensions.getVersion());
2023 result.append(buffer);
2024
2025 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002026 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002027 result.append(buffer);
2028
2029 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2030 result.append(buffer);
2031
Mathias Agopian42977342012-08-05 00:40:46 -07002032 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002033 snprintf(buffer, SIZE,
2034 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002035 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002036 result.append(buffer);
2037 snprintf(buffer, SIZE,
2038 " last eglSwapBuffers() time: %f us\n"
2039 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002040 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002041 " refresh-rate : %f fps\n"
2042 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002043 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002044 mLastSwapBufferTime/1000.0,
2045 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002046 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002047 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2048 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2049 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002050 result.append(buffer);
2051
2052 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2053 inSwapBuffersDuration/1000.0);
2054 result.append(buffer);
2055
2056 snprintf(buffer, SIZE, " transaction time: %f us\n",
2057 inTransactionDuration/1000.0);
2058 result.append(buffer);
2059
2060 /*
2061 * VSYNC state
2062 */
2063 mEventThread->dump(result, buffer, SIZE);
2064
2065 /*
2066 * Dump HWComposer state
2067 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002068 snprintf(buffer, SIZE, "h/w composer state:\n");
2069 result.append(buffer);
2070 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2071 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2072 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2073 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002074 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002075
2076 /*
2077 * Dump gralloc state
2078 */
2079 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2080 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002081 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002082}
2083
Keun young Park63f165f2012-08-31 10:53:36 -07002084bool SurfaceFlinger::startDdmConnection()
2085{
2086 void* libddmconnection_dso =
2087 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2088 if (!libddmconnection_dso) {
2089 return false;
2090 }
2091 void (*DdmConnection_start)(const char* name);
2092 DdmConnection_start =
2093 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2094 if (!DdmConnection_start) {
2095 dlclose(libddmconnection_dso);
2096 return false;
2097 }
2098 (*DdmConnection_start)(getServiceName());
2099 return true;
2100}
2101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102status_t SurfaceFlinger::onTransact(
2103 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2104{
2105 switch (code) {
2106 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002107 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002108 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002109 case BLANK:
2110 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111 {
2112 // codes that require permission check
2113 IPCThreadState* ipc = IPCThreadState::self();
2114 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002115 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002116 if ((uid != AID_GRAPHICS) &&
2117 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002118 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002119 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2120 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002122 break;
2123 }
2124 case CAPTURE_SCREEN:
2125 {
2126 // codes that require permission check
2127 IPCThreadState* ipc = IPCThreadState::self();
2128 const int pid = ipc->getCallingPid();
2129 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002130 if ((uid != AID_GRAPHICS) &&
2131 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002132 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002133 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2134 return PERMISSION_DENIED;
2135 }
2136 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002137 }
2138 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002140 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2141 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002142 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002143 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002144 IPCThreadState* ipc = IPCThreadState::self();
2145 const int pid = ipc->getCallingPid();
2146 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002147 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002148 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149 return PERMISSION_DENIED;
2150 }
2151 int n;
2152 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002153 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002154 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002155 return NO_ERROR;
2156 case 1002: // SHOW_UPDATES
2157 n = data.readInt32();
2158 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002159 invalidateHwcGeometry();
2160 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002161 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002162 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002163 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002164 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002165 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002166 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002167 setTransactionFlags(
2168 eTransactionNeeded|
2169 eDisplayTransactionNeeded|
2170 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002171 return NO_ERROR;
2172 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002173 case 1006:{ // send empty update
2174 signalRefresh();
2175 return NO_ERROR;
2176 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002177 case 1008: // toggle use of hw composer
2178 n = data.readInt32();
2179 mDebugDisableHWC = n ? 1 : 0;
2180 invalidateHwcGeometry();
2181 repaintEverything();
2182 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002183 case 1009: // toggle use of transform hint
2184 n = data.readInt32();
2185 mDebugDisableTransformHint = n ? 1 : 0;
2186 invalidateHwcGeometry();
2187 repaintEverything();
2188 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002190 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002191 reply->writeInt32(0);
2192 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002193 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002194 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195 return NO_ERROR;
2196 case 1013: {
2197 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002198 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2199 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002200 }
2201 return NO_ERROR;
2202 }
2203 }
2204 return err;
2205}
2206
Mathias Agopian53331da2011-08-22 21:44:41 -07002207void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002208 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002209 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002210}
2211
Mathias Agopian59119e62010-10-11 12:37:43 -07002212// ---------------------------------------------------------------------------
2213
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002214status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002215 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2216{
2217 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002218 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002219}
2220
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002221status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002222 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002223{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002224 ATRACE_CALL();
2225
Mathias Agopian59119e62010-10-11 12:37:43 -07002226 if (!GLExtensions::getInstance().haveFramebufferObject())
2227 return INVALID_OPERATION;
2228
2229 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002230 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2231 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002232 const uint32_t hw_w = hw->getWidth();
2233 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002234 GLfloat u = 1;
2235 GLfloat v = 1;
2236
2237 // make sure to clear all GL error flags
2238 while ( glGetError() != GL_NO_ERROR ) ;
2239
2240 // create a FBO
2241 GLuint name, tname;
2242 glGenTextures(1, &tname);
2243 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002244 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2245 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002246 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2247 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002249 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002250 GLint tw = (2 << (31 - clz(hw_w)));
2251 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002252 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2253 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002254 u = GLfloat(hw_w) / tw;
2255 v = GLfloat(hw_h) / th;
2256 }
2257 glGenFramebuffersOES(1, &name);
2258 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2260 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002261
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002262 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002263 glDisable(GL_TEXTURE_EXTERNAL_OES);
2264 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002265 glClearColor(0,0,0,1);
2266 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002267 glMatrixMode(GL_MODELVIEW);
2268 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002269 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002270 const size_t count = layers.size();
2271 for (size_t i=0 ; i<count ; ++i) {
2272 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002273 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002274 }
2275
Mathias Agopian42977342012-08-05 00:40:46 -07002276 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002277
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002278 // back to main framebuffer
2279 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002280 glDeleteFramebuffersOES(1, &name);
2281
2282 *textureName = tname;
2283 *uOut = u;
2284 *vOut = v;
2285 return NO_ERROR;
2286}
2287
2288// ---------------------------------------------------------------------------
2289
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002290status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291 sp<IMemoryHeap>* heap,
2292 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002293 uint32_t sw, uint32_t sh,
2294 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002295{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002296 ATRACE_CALL();
2297
Mathias Agopian74c40c02010-09-29 13:02:36 -07002298 status_t result = PERMISSION_DENIED;
2299
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002300 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002301 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002302 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002303
2304 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002305 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002306 const uint32_t hw_w = hw->getWidth();
2307 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002308
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002309 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002310 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002311 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002312 return PERMISSION_DENIED;
2313 }
2314
2315 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002316 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002317 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002318 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319
2320 sw = (!sw) ? hw_w : sw;
2321 sh = (!sh) ? hw_h : sh;
2322 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002323 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002325// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2326// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002327
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 // make sure to clear all GL error flags
2329 while ( glGetError() != GL_NO_ERROR ) ;
2330
2331 // create a FBO
2332 GLuint name, tname;
2333 glGenRenderbuffersOES(1, &tname);
2334 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2335 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002336
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 glGenFramebuffersOES(1, &name);
2338 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2339 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2340 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2341
2342 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002343
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2345
2346 // invert everything, b/c glReadPixel() below will invert the FB
2347 glViewport(0, 0, sw, sh);
2348 glMatrixMode(GL_PROJECTION);
2349 glPushMatrix();
2350 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002351 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 glMatrixMode(GL_MODELVIEW);
2353
2354 // redraw the screen entirely...
2355 glClearColor(0,0,0,1);
2356 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002357
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002358 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002359 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360 for (size_t i=0 ; i<count ; ++i) {
2361 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002362 const uint32_t z = layer->drawingState().z;
2363 if (z >= minLayerZ && z <= maxLayerZ) {
2364 if (filtering) layer->setFiltering(true);
2365 layer->draw(hw);
2366 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002367 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002368 }
2369
Mathias Agopian74c40c02010-09-29 13:02:36 -07002370 // check for errors and return screen capture
2371 if (glGetError() != GL_NO_ERROR) {
2372 // error while rendering
2373 result = INVALID_OPERATION;
2374 } else {
2375 // allocate shared memory large enough to hold the
2376 // screen capture
2377 sp<MemoryHeapBase> base(
2378 new MemoryHeapBase(size, 0, "screen-capture") );
2379 void* const ptr = base->getBase();
2380 if (ptr) {
2381 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002382 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2384 if (glGetError() == GL_NO_ERROR) {
2385 *heap = base;
2386 *w = sw;
2387 *h = sh;
2388 *f = PIXEL_FORMAT_RGBA_8888;
2389 result = NO_ERROR;
2390 }
2391 } else {
2392 result = NO_MEMORY;
2393 }
2394 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002395 glViewport(0, 0, hw_w, hw_h);
2396 glMatrixMode(GL_PROJECTION);
2397 glPopMatrix();
2398 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002399 } else {
2400 result = BAD_VALUE;
2401 }
2402
2403 // release FBO resources
2404 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2405 glDeleteRenderbuffersOES(1, &tname);
2406 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002407
Mathias Agopian42977342012-08-05 00:40:46 -07002408 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002409
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002410// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002411
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 return result;
2413}
2414
2415
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002416status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002417 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002419 uint32_t sw, uint32_t sh,
2420 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002421{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002422 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002423 return BAD_VALUE;
2424
2425 if (!GLExtensions::getInstance().haveFramebufferObject())
2426 return INVALID_OPERATION;
2427
2428 class MessageCaptureScreen : public MessageBase {
2429 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002430 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002431 sp<IMemoryHeap>* heap;
2432 uint32_t* w;
2433 uint32_t* h;
2434 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002435 uint32_t sw;
2436 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002437 uint32_t minLayerZ;
2438 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002439 status_t result;
2440 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002441 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002442 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002443 uint32_t sw, uint32_t sh,
2444 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002445 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002446 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2447 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2448 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002449 {
2450 }
2451 status_t getResult() const {
2452 return result;
2453 }
2454 virtual bool handler() {
2455 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002456 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002457 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458 return true;
2459 }
2460 };
2461
2462 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002463 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002464 status_t res = postMessageSync(msg);
2465 if (res == NO_ERROR) {
2466 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2467 }
2468 return res;
2469}
2470
2471// ---------------------------------------------------------------------------
2472
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002473SurfaceFlinger::LayerVector::LayerVector() {
2474}
2475
2476SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2477 : SortedVector<sp<LayerBase> >(rhs) {
2478}
2479
2480int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2481 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002482{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002483 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002484 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2485 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002486
2487 uint32_t ls = l->currentState().layerStack;
2488 uint32_t rs = r->currentState().layerStack;
2489 if (ls != rs)
2490 return ls - rs;
2491
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002492 uint32_t lz = l->currentState().z;
2493 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002494 if (lz != rz)
2495 return lz - rz;
2496
2497 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002498}
2499
2500// ---------------------------------------------------------------------------
2501
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002502SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2503 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002504}
2505
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002506SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2507 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002508 viewport.makeInvalid();
2509 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002510}
2511
2512// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002513
Jamie Gennis9a78c902011-01-12 18:30:40 -08002514GraphicBufferAlloc::GraphicBufferAlloc() {}
2515
2516GraphicBufferAlloc::~GraphicBufferAlloc() {}
2517
2518sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002519 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002520 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2521 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002522 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002523 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002524 if (err == NO_MEMORY) {
2525 GraphicBuffer::dumpAllocationsToSystemLog();
2526 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002527 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002528 "failed (%s), handle=%p",
2529 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002530 return 0;
2531 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002532 return graphicBuffer;
2533}
2534
Jamie Gennis9a78c902011-01-12 18:30:40 -08002535// ---------------------------------------------------------------------------
2536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537}; // namespace android