blob: c63d0cf146a995fca844c9123510e4321ba682f4 [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>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
39#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/SurfaceTextureClient.h>
41
42#include <ui/GraphicBufferAllocator.h>
43#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/String8.h>
46#include <utils/String16.h>
47#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080048#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian921e6ac2012-07-23 23:11:29 -070050#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080053#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070055#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070060#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana4912602012-07-12 14:25:33 -070063#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070064#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070086 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -070098 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
113
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 ALOGI_IF(mDebugRegion, "showupdates enabled");
115 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800118void SurfaceFlinger::onFirstRef()
119{
120 mEventQueue.init(this);
121
122 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
123
124 // Wait for the main thread to be done with its initialization
125 mReadyToRunBarrier.wait();
126}
127
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::~SurfaceFlinger()
130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void SurfaceFlinger::binderDied(const wp<IBinder>& who)
137{
138 // the window manager died on us. prepare its eulogy.
139
140 // reset screen orientation
141 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700142 Vector<DisplayState> displays;
143 DisplayState d;
Mathias Agopian818b4602012-08-16 20:57:39 -0700144 d.what = DisplayState::eOrientationChanged;
Jesse Hallea599df2012-08-13 14:47:01 -0700145 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
Mathias Agopian3165cc22012-08-08 19:42:09 -0700146 d.orientation = DisplayState::eOrientationDefault;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700147 displays.add(d);
148 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149
150 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700151 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152}
153
Mathias Agopian7e27f052010-05-28 14:22:23 -0700154sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian96f08192010-06-02 23:28:45 -0700156 sp<ISurfaceComposerClient> bclient;
157 sp<Client> client(new Client(this));
158 status_t err = client->initCheck();
159 if (err == NO_ERROR) {
160 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return bclient;
163}
164
Mathias Agopiane57f2922012-08-09 16:29:12 -0700165sp<IBinder> SurfaceFlinger::createDisplay()
166{
167 class DisplayToken : public BBinder {
168 sp<SurfaceFlinger> flinger;
169 virtual ~DisplayToken() {
170 // no more references, this display must be terminated
171 Mutex::Autolock _l(flinger->mStateLock);
172 flinger->mCurrentState.displays.removeItem(this);
173 flinger->setTransactionFlags(eDisplayTransactionNeeded);
174 }
175 public:
176 DisplayToken(const sp<SurfaceFlinger>& flinger)
177 : flinger(flinger) {
178 }
179 };
180
181 sp<BBinder> token = new DisplayToken(this);
182
183 Mutex::Autolock _l(mStateLock);
184 DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
185 mCurrentState.displays.add(token, info);
186
187 return token;
188}
189
190sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
191 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
192 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
193 return NULL;
194 }
195 return mDefaultDisplays[id];
196}
197
Jamie Gennis9a78c902011-01-12 18:30:40 -0800198sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
199{
200 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
201 return gba;
202}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204void SurfaceFlinger::bootFinished()
205{
206 const nsecs_t now = systemTime();
207 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000208 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700209 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700210
211 // wait patiently for the window manager death
212 const String16 name("window");
213 sp<IBinder> window(defaultServiceManager()->getService(name));
214 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700215 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700216 }
217
218 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700219 // formerly we would just kill the process, but we now ask it to exit so it
220 // can choose where to stop the animation.
221 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222}
223
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700224void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
225 class MessageDestroyGLTexture : public MessageBase {
226 GLuint texture;
227 public:
228 MessageDestroyGLTexture(GLuint texture)
229 : texture(texture) {
230 }
231 virtual bool handler() {
232 glDeleteTextures(1, &texture);
233 return true;
234 }
235 };
236 postMessageAsync(new MessageDestroyGLTexture(texture));
237}
238
Mathias Agopiana4912602012-07-12 14:25:33 -0700239status_t SurfaceFlinger::selectConfigForPixelFormat(
240 EGLDisplay dpy,
241 EGLint const* attrs,
242 PixelFormat format,
243 EGLConfig* outConfig)
244{
245 EGLConfig config = NULL;
246 EGLint numConfigs = -1, n=0;
247 eglGetConfigs(dpy, NULL, 0, &numConfigs);
248 EGLConfig* const configs = new EGLConfig[numConfigs];
249 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
250 for (int i=0 ; i<n ; i++) {
251 EGLint nativeVisualId = 0;
252 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
253 if (nativeVisualId>0 && format == nativeVisualId) {
254 *outConfig = configs[i];
255 delete [] configs;
256 return NO_ERROR;
257 }
258 }
259 delete [] configs;
260 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261}
262
Mathias Agopiana4912602012-07-12 14:25:33 -0700263EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
264 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
265 // it is to be used with WIFI displays
266 EGLConfig config;
267 EGLint dummy;
268 status_t err;
269 EGLint attribs[] = {
270 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
271 EGL_RECORDABLE_ANDROID, EGL_TRUE,
272 EGL_NONE
273 };
274 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
275 if (err) {
276 // maybe we failed because of EGL_RECORDABLE_ANDROID
277 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
278 attribs[2] = EGL_NONE;
279 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
280 }
281 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
282 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
283 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
284 }
285 return config;
286}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
Mathias Agopiana4912602012-07-12 14:25:33 -0700288EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
289 // Also create our EGLContext
290 EGLint contextAttributes[] = {
291#ifdef EGL_IMG_context_priority
292#ifdef HAS_CONTEXT_PRIORITY
293#warning "using EGL_IMG_context_priority"
294 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
295#endif
296#endif
297 EGL_NONE, EGL_NONE
298 };
299 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
300 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
301 return ctxt;
302}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
Mathias Agopiana4912602012-07-12 14:25:33 -0700304void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
305 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
306 if (!result) {
307 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
308 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 }
310
Mathias Agopiana4912602012-07-12 14:25:33 -0700311 GLExtensions& extensions(GLExtensions::getInstance());
312 extensions.initWithGLStrings(
313 glGetString(GL_VENDOR),
314 glGetString(GL_RENDERER),
315 glGetString(GL_VERSION),
316 glGetString(GL_EXTENSIONS),
317 eglQueryString(display, EGL_VENDOR),
318 eglQueryString(display, EGL_VERSION),
319 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700320
Mathias Agopiana4912602012-07-12 14:25:33 -0700321 EGLint w, h;
322 eglQuerySurface(display, surface, EGL_WIDTH, &w);
323 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700324
Mathias Agopiana4912602012-07-12 14:25:33 -0700325 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
326 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700329 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 glShadeModel(GL_FLAT);
332 glDisable(GL_DITHER);
333 glDisable(GL_CULL_FACE);
334
Mathias Agopiana4912602012-07-12 14:25:33 -0700335 struct pack565 {
336 inline uint16_t operator() (int r, int g, int b) const {
337 return (r<<11)|(g<<5)|b;
338 }
339 } pack565;
340
Jamie Gennis9575f602011-10-07 14:51:16 -0700341 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
342 glGenTextures(1, &mProtectedTexName);
343 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
344 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
348 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
349 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
351 glViewport(0, 0, w, h);
352 glMatrixMode(GL_PROJECTION);
353 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700354 // put the origin in the left-bottom corner
355 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356
Mathias Agopiana4912602012-07-12 14:25:33 -0700357 // print some debugging info
358 EGLint r,g,b,a;
359 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
360 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
361 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
362 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
363 ALOGI("EGL informations:");
364 ALOGI("vendor : %s", extensions.getEglVendor());
365 ALOGI("version : %s", extensions.getEglVersion());
366 ALOGI("extensions: %s", extensions.getEglExtension());
367 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
368 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
369 ALOGI("OpenGL ES informations:");
370 ALOGI("vendor : %s", extensions.getVendor());
371 ALOGI("renderer : %s", extensions.getRenderer());
372 ALOGI("version : %s", extensions.getVersion());
373 ALOGI("extensions: %s", extensions.getExtension());
374 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
375 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
376}
377
Mathias Agopiana4912602012-07-12 14:25:33 -0700378status_t SurfaceFlinger::readyToRun()
379{
380 ALOGI( "SurfaceFlinger's main thread ready to run. "
381 "Initializing graphics H/W...");
382
Mathias Agopiana4912602012-07-12 14:25:33 -0700383 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700384 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
385 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700386
387 // Initialize the main display
388 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700389 sp<FramebufferSurface> fbs = FramebufferSurface::create();
390 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700391 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
392 exit(0);
393 }
394
Mathias Agopiane57f2922012-08-09 16:29:12 -0700395 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
396 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700397
Mathias Agopiana4912602012-07-12 14:25:33 -0700398 // initialize the config and context
399 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700400 ANativeWindow* const anw = stc.get();
401 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700402 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
403 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700404
405 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700406
407 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
408 mDefaultDisplays[i] = new BBinder();
409 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
410 }
411 sp<DisplayDevice> hw = new DisplayDevice(this,
412 DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
413 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700414
415 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700416 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700417 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800418
Mathias Agopian028508c2012-07-25 21:12:12 -0700419 // start the EventThread
420 mEventThread = new EventThread(this);
421 mEventQueue.setEventThread(mEventThread);
422
Mathias Agopian86303202012-07-24 22:46:10 -0700423 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700424 mHwc = new HWComposer(this,
425 *static_cast<HWComposer::EventHandler *>(this),
426 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700427
428 // initialize our drawing state
429 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700430
Mathias Agopiana4912602012-07-12 14:25:33 -0700431 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800432 mReadyToRunBarrier.open();
433
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700434 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700435 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 return NO_ERROR;
438}
439
Mathias Agopiana67e4182012-06-19 17:26:12 -0700440void SurfaceFlinger::startBootAnim() {
441 // start boot animation
442 property_set("service.bootanim.exit", "0");
443 property_set("ctl.start", "bootanim");
444}
445
Mathias Agopiana4912602012-07-12 14:25:33 -0700446uint32_t SurfaceFlinger::getMaxTextureSize() const {
447 return mMaxTextureSize;
448}
449
450uint32_t SurfaceFlinger::getMaxViewportDims() const {
451 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
452 mMaxViewportDims[0] : mMaxViewportDims[1];
453}
454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800456
Jamie Gennis582270d2011-08-17 18:19:00 -0700457bool SurfaceFlinger::authenticateSurfaceTexture(
458 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800459 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700460 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800461
462 // Check the visible layer list for the ISurface
463 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
464 size_t count = currentLayers.size();
465 for (size_t i=0 ; i<count ; i++) {
466 const sp<LayerBase>& layer(currentLayers[i]);
467 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700468 if (lbc != NULL) {
469 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
470 if (lbcBinder == surfaceTextureBinder) {
471 return true;
472 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800473 }
474 }
475
476 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700477 // SurfaceTexture gets destroyed before all the clients are done using it,
478 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800479 // will instead fail later on when the client tries to use the surface,
480 // which should be reported as "surface XYZ returned an -ENODEV". The
481 // purgatorized layers are no less authentic than the visible ones, so this
482 // should not cause any harm.
483 size_t purgatorySize = mLayerPurgatory.size();
484 for (size_t i=0 ; i<purgatorySize ; i++) {
485 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
486 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700487 if (lbc != NULL) {
488 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
489 if (lbcBinder == surfaceTextureBinder) {
490 return true;
491 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800492 }
493 }
494
495 return false;
496}
497
Mathias Agopianc666cae2012-07-25 18:56:13 -0700498status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700499 // TODO: this is here only for compatibility -- should go away eventually.
500 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700501 return BAD_INDEX;
502 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700503
504 const HWComposer& hwc(getHwComposer());
505 float xdpi = hwc.getDpiX();
506 float ydpi = hwc.getDpiY();
507
508 // TODO: Not sure if display density should handled by SF any longer
509 class Density {
510 static int getDensityFromProperty(char const* propName) {
511 char property[PROPERTY_VALUE_MAX];
512 int density = 0;
513 if (property_get(propName, property, NULL) > 0) {
514 density = atoi(property);
515 }
516 return density;
517 }
518 public:
519 static int getEmuDensity() {
520 return getDensityFromProperty("qemu.sf.lcd_density"); }
521 static int getBuildDensity() {
522 return getDensityFromProperty("ro.sf.lcd_density"); }
523 };
524 // The density of the device is provided by a build property
525 float density = Density::getBuildDensity() / 160.0f;
526 if (density == 0) {
527 // the build doesn't provide a density -- this is wrong!
528 // use xdpi instead
529 ALOGE("ro.sf.lcd_density must be defined as a build property");
530 density = xdpi / 160.0f;
531 }
532 if (Density::getEmuDensity()) {
533 // if "qemu.sf.lcd_density" is specified, it overrides everything
534 xdpi = ydpi = density = Density::getEmuDensity();
535 density /= 160.0f;
536 }
537
Mathias Agopian42977342012-08-05 00:40:46 -0700538 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
539 info->w = hw->getWidth();
540 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700541 info->xdpi = xdpi;
542 info->ydpi = ydpi;
543 info->fps = float(1e9 / hwc.getRefreshPeriod());
544 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700545 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700546 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700547 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700548 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700549}
550
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800551// ----------------------------------------------------------------------------
552
553sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800554 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700555}
556
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700557void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700558
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700559 sp<IBinder> token;
560 { // scope for the lock
561 Mutex::Autolock _l(mStateLock);
562 token = mExtDisplayToken;
563 }
564
565 if (token == 0) {
566 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700567 }
568
569 { // scope for the lock
570 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700571 if (surface == 0) {
572 // release our current display. we're guarantee to have
573 // a reference to it (token), while we hold the lock
574 mExtDisplayToken = 0;
575 } else {
576 mExtDisplayToken = token;
577 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700578
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700579 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
580 info.surface = surface;
581 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700582 }
583}
584
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800586
587void SurfaceFlinger::waitForEvent() {
588 mEventQueue.waitMessage();
589}
590
591void SurfaceFlinger::signalTransaction() {
592 mEventQueue.invalidate();
593}
594
595void SurfaceFlinger::signalLayerUpdate() {
596 mEventQueue.invalidate();
597}
598
599void SurfaceFlinger::signalRefresh() {
600 mEventQueue.refresh();
601}
602
603status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
604 nsecs_t reltime, uint32_t flags) {
605 return mEventQueue.postMessage(msg, reltime);
606}
607
608status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
609 nsecs_t reltime, uint32_t flags) {
610 status_t res = mEventQueue.postMessage(msg, reltime);
611 if (res == NO_ERROR) {
612 msg->wait();
613 }
614 return res;
615}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616
Mathias Agopian4fec8732012-06-29 14:12:52 -0700617bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 return true;
620}
621
Mathias Agopian86303202012-07-24 22:46:10 -0700622void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700623 mEventThread->onVSyncReceived(dpy, timestamp);
624}
625
626void SurfaceFlinger::eventControl(int event, int enabled) {
627 getHwComposer().eventControl(event, enabled);
628}
629
Mathias Agopian4fec8732012-06-29 14:12:52 -0700630void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800631 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800632 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700633 case MessageQueue::INVALIDATE:
634 handleMessageTransaction();
635 handleMessageInvalidate();
636 signalRefresh();
637 break;
638 case MessageQueue::REFRESH:
639 handleMessageRefresh();
640 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800641 }
642}
643
Mathias Agopian4fec8732012-06-29 14:12:52 -0700644void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700645 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700646 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700647 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700648 }
649}
650
651void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700652 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700653 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700654}
655
656void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700657 ATRACE_CALL();
658 preComposition();
659 rebuildLayerStacks();
660 setUpHWComposer();
661 doDebugFlashRegions();
662 doComposition();
663 postComposition();
664}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700665
Mathias Agopiancd60f992012-08-16 16:28:27 -0700666void SurfaceFlinger::doDebugFlashRegions()
667{
668 // is debugging enabled
669 if (CC_LIKELY(!mDebugRegion))
670 return;
671
672 const bool repaintEverything = mRepaintEverything;
673 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
674 const sp<DisplayDevice>& hw(mDisplays[dpy]);
675 if (hw->canDraw()) {
676 // transform the dirty region into this screen's coordinate space
677 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
678 if (!dirtyRegion.isEmpty()) {
679 // redraw the whole screen
680 doComposeSurfaces(hw, Region(hw->bounds()));
681
682 // and draw the dirty region
683 glDisable(GL_TEXTURE_EXTERNAL_OES);
684 glDisable(GL_TEXTURE_2D);
685 glDisable(GL_BLEND);
686 glColor4f(1, 0, 1, 1);
687 const int32_t height = hw->getHeight();
688 Region::const_iterator it = dirtyRegion.begin();
689 Region::const_iterator const end = dirtyRegion.end();
690 while (it != end) {
691 const Rect& r = *it++;
692 GLfloat vertices[][2] = {
693 { r.left, height - r.top },
694 { r.left, height - r.bottom },
695 { r.right, height - r.bottom },
696 { r.right, height - r.top }
697 };
698 glVertexPointer(2, GL_FLOAT, 0, vertices);
699 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
700 }
701 hw->compositionComplete();
702 // FIXME
703 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
704 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
705 }
706 }
707 }
708 }
709
710 postFramebuffer();
711
712 if (mDebugRegion > 1) {
713 usleep(mDebugRegion * 1000);
714 }
715}
716
717void SurfaceFlinger::preComposition()
718{
719 bool needExtraInvalidate = false;
720 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
721 const size_t count = currentLayers.size();
722 for (size_t i=0 ; i<count ; i++) {
723 if (currentLayers[i]->onPreComposition()) {
724 needExtraInvalidate = true;
725 }
726 }
727 if (needExtraInvalidate) {
728 signalLayerUpdate();
729 }
730}
731
732void SurfaceFlinger::postComposition()
733{
734 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
735 const size_t count = currentLayers.size();
736 for (size_t i=0 ; i<count ; i++) {
737 currentLayers[i]->onPostComposition();
738 }
739}
740
741void SurfaceFlinger::rebuildLayerStacks() {
742 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700743 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700744 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700745 mVisibleRegionsDirty = false;
746 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700747 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700748 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700749 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700750 Region opaqueRegion;
751 Region dirtyRegion;
752 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700753 hw->getLayerStack(), dirtyRegion, opaqueRegion);
754 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700755
756 Vector< sp<LayerBase> > layersSortedByZ;
757 const size_t count = currentLayers.size();
758 for (size_t i=0 ; i<count ; i++) {
759 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700760 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700761 if (!currentLayers[i]->visibleRegion.isEmpty()) {
762 layersSortedByZ.add(currentLayers[i]);
763 }
764 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700765 }
Mathias Agopian42977342012-08-05 00:40:46 -0700766 hw->setVisibleLayersSortedByZ(layersSortedByZ);
767 hw->undefinedRegion.set(hw->getBounds());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700768 hw->undefinedRegion.subtractSelf(
769 hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700770 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700771 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700772}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700773
Mathias Agopiancd60f992012-08-16 16:28:27 -0700774void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700775 HWComposer& hwc(getHwComposer());
776 if (hwc.initCheck() == NO_ERROR) {
777 // build the h/w work list
778 const bool workListsDirty = mHwWorkListDirty;
779 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700780 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700781 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700782 const Vector< sp<LayerBase> >& currentLayers(
783 hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700784 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700785
Mathias Agopian1e260872012-08-08 18:35:12 -0700786 const int32_t id = hw->getDisplayId();
787 if (hwc.createWorkList(id, count) >= 0) {
788 HWComposer::LayerListIterator cur = hwc.begin(id);
789 const HWComposer::LayerListIterator end = hwc.end(id);
790 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
791 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700792
Mathias Agopian1e260872012-08-08 18:35:12 -0700793 if (CC_UNLIKELY(workListsDirty)) {
794 layer->setGeometry(hw, *cur);
795 if (mDebugDisableHWC || mDebugRegion) {
796 cur->setSkip(true);
797 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700798 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700799
Mathias Agopian1e260872012-08-08 18:35:12 -0700800 /*
801 * update the per-frame h/w composer data for each layer
802 * and build the transparent region of the FB
803 */
804 layer->setPerFrameData(hw, *cur);
805 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700806 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700807 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808 status_t err = hwc.prepare();
809 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
810 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700812
Mathias Agopiancd60f992012-08-16 16:28:27 -0700813void SurfaceFlinger::doComposition() {
814 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700815 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700816 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700817 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700818 if (hw->canDraw()) {
819 // transform the dirty region into this screen's coordinate space
820 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
821 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700822 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700823 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700824 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700825 hw->dirtyRegion.clear();
826 hw->flip(hw->swapRegion);
827 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700828 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700829 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700830 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700831 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700833}
834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835void SurfaceFlinger::postFramebuffer()
836{
Mathias Agopian841cde52012-03-01 15:44:37 -0800837 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800838
Mathias Agopiana44b0412011-10-16 18:46:35 -0700839 const nsecs_t now = systemTime();
840 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700841
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700842 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700843 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700844 // FIXME: eventually commit() won't take arguments
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700845 // FIXME: EGL spec says:
846 // "surface must be bound to the calling thread's current context,
847 // for the current rendering API."
Mathias Agopiancd60f992012-08-16 16:28:27 -0700848 DisplayDevice::makeCurrent(
849 getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopian42977342012-08-05 00:40:46 -0700850 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851 }
852
Mathias Agopian92a979a2012-08-02 18:32:23 -0700853 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700854 sp<const DisplayDevice> hw(mDisplays[dpy]);
855 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700856 const size_t count = currentLayers.size();
857 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700858 int32_t id = hw->getDisplayId();
859 HWComposer::LayerListIterator cur = hwc.begin(id);
860 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700861 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700862 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700863 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700864 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700865 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700866 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700867 }
Jesse Hallef194142012-06-14 14:45:17 -0700868 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800869 }
870
Mathias Agopiana44b0412011-10-16 18:46:35 -0700871 mLastSwapBufferTime = systemTime() - now;
872 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873}
874
Mathias Agopian87baae12012-07-31 12:38:26 -0700875void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876{
Mathias Agopian841cde52012-03-01 15:44:37 -0800877 ATRACE_CALL();
878
Mathias Agopianca4d3602011-05-19 15:38:14 -0700879 Mutex::Autolock _l(mStateLock);
880 const nsecs_t now = systemTime();
881 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882
Mathias Agopianca4d3602011-05-19 15:38:14 -0700883 // Here we're guaranteed that some transaction flags are set
884 // so we can call handleTransactionLocked() unconditionally.
885 // We call getTransactionFlags(), which will also clear the flags,
886 // with mStateLock held to guarantee that mCurrentState won't change
887 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700888
Mathias Agopiane57f2922012-08-09 16:29:12 -0700889 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700890 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700891
Mathias Agopianca4d3602011-05-19 15:38:14 -0700892 mLastTransactionTime = systemTime() - now;
893 mDebugInTransaction = 0;
894 invalidateHwcGeometry();
895 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700896}
897
Mathias Agopian87baae12012-07-31 12:38:26 -0700898void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700899{
900 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 const size_t count = currentLayers.size();
902
903 /*
904 * Traversal of the children
905 * (perform the transaction for each of them if needed)
906 */
907
Mathias Agopian3559b072012-08-15 13:46:03 -0700908 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700910 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
912 if (!trFlags) continue;
913
914 const uint32_t flags = layer->doTransaction(0);
915 if (flags & Layer::eVisibleRegion)
916 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 }
918 }
919
920 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700921 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 */
923
Mathias Agopiane57f2922012-08-09 16:29:12 -0700924 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700925 // here we take advantage of Vector's copy-on-write semantics to
926 // improve performance by skipping the transaction entirely when
927 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700928 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
929 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700930 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700932 const size_t cc = curr.size();
933 const size_t dc = draw.size();
934
935 // find the displays that were removed
936 // (ie: in drawing state but not in current state)
937 // also handle displays that changed
938 // (ie: displays that are in both lists)
939 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700940 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
941 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700942 // in drawing state but not in current state
943 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700944 mDisplays.removeItem(draw[i].id);
945 } else {
946 ALOGW("trying to remove the main display");
947 }
948 } else {
949 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700950 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700951 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700952 // changing the surface is like destroying and
953 // recreating the DisplayDevice
954
955 sp<SurfaceTextureClient> stc(
956 new SurfaceTextureClient(state.surface));
957
958 sp<DisplayDevice> disp = new DisplayDevice(this,
959 state.id, stc, 0, mEGLConfig);
960
961 disp->setLayerStack(state.layerStack);
962 disp->setOrientation(state.orientation);
963 // TODO: take viewport and frame into account
964 mDisplays.replaceValueFor(state.id, disp);
965 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700966 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700967 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700968 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 if (state.orientation != draw[i].orientation ||
971 state.viewport != draw[i].viewport ||
972 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700973 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
974 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700975 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700976 }
977 }
978 }
979
980 // find displays that were added
981 // (ie: in current state but not in drawing state)
982 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700983 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700984 const DisplayDeviceState& state(curr[i]);
985 sp<SurfaceTextureClient> stc(
986 new SurfaceTextureClient(state.surface));
987 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
988 stc, 0, mEGLConfig);
989 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700990 }
991 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 }
Mathias Agopian3559b072012-08-15 13:46:03 -0700993 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994
Mathias Agopian3559b072012-08-15 13:46:03 -0700995 /*
996 * Perform our own transaction if needed
997 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700998
Mathias Agopiancd60f992012-08-16 16:28:27 -0700999 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1000 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001001 // layers have been added
1002 mVisibleRegionsDirty = true;
1003 }
1004
1005 // some layers might have been removed, so
1006 // we need to update the regions they're exposing.
1007 if (mLayersRemoved) {
1008 mLayersRemoved = false;
1009 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001010 const size_t count = previousLayers.size();
1011 for (size_t i=0 ; i<count ; i++) {
1012 const sp<LayerBase>& layer(previousLayers[i]);
1013 if (currentLayers.indexOf(layer) < 0) {
1014 // this layer is not visible anymore
1015 // TODO: we could traverse the tree from front to back and
1016 // compute the actual visible region
1017 // TODO: we could cache the transformed region
1018 Layer::State front(layer->drawingState());
1019 Region visibleReg = front.transform.transform(
1020 Region(Rect(front.active.w, front.active.h)));
1021 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001022 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 }
1025
1026 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001027}
1028
1029void SurfaceFlinger::commitTransaction()
1030{
1031 if (!mLayersPendingRemoval.isEmpty()) {
1032 // Notify removed layers now that they can't be drawn from
1033 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1034 mLayersPendingRemoval[i]->onRemoved();
1035 }
1036 mLayersPendingRemoval.clear();
1037 }
1038
1039 mDrawingState = mCurrentState;
1040 mTransationPending = false;
1041 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042}
1043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001045 const LayerVector& currentLayers, uint32_t layerStack,
1046 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047{
Mathias Agopian841cde52012-03-01 15:44:37 -08001048 ATRACE_CALL();
1049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 Region aboveOpaqueLayers;
1051 Region aboveCoveredLayers;
1052 Region dirty;
1053
Mathias Agopian87baae12012-07-31 12:38:26 -07001054 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055
1056 size_t i = currentLayers.size();
1057 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001058 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059
1060 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001061 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062
Mathias Agopian87baae12012-07-31 12:38:26 -07001063 // only consider the layers on the given later stack
1064 if (s.layerStack != layerStack)
1065 continue;
1066
Mathias Agopianab028732010-03-16 16:41:46 -07001067 /*
1068 * opaqueRegion: area of a surface that is fully opaque.
1069 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001071
1072 /*
1073 * visibleRegion: area of a surface that is visible on screen
1074 * and not fully transparent. This is essentially the layer's
1075 * footprint minus the opaque regions above it.
1076 * Areas covered by a translucent surface are considered visible.
1077 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001079
1080 /*
1081 * coveredRegion: area of a surface that is covered by all
1082 * visible regions above it (which includes the translucent areas).
1083 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001085
1086
1087 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001088 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001089 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001090 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001092 if (!visibleRegion.isEmpty()) {
1093 // Remove the transparent area from the visible region
1094 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001095 Region transparentRegionScreen;
1096 const Transform tr(s.transform);
1097 if (tr.transformed()) {
1098 if (tr.preserveRects()) {
1099 // transform the transparent region
1100 transparentRegionScreen = tr.transform(s.transparentRegion);
1101 } else {
1102 // transformation too complex, can't do the
1103 // transparent region optimization.
1104 transparentRegionScreen.clear();
1105 }
1106 } else {
1107 transparentRegionScreen = s.transparentRegion;
1108 }
1109 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001110 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111
Mathias Agopianab028732010-03-16 16:41:46 -07001112 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001113 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001114 if (s.alpha==255 && !translucent &&
1115 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1116 // the opaque region is the layer's footprint
1117 opaqueRegion = visibleRegion;
1118 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 }
1120 }
1121
Mathias Agopianab028732010-03-16 16:41:46 -07001122 // Clip the covered region to the visible region
1123 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1124
1125 // Update aboveCoveredLayers for next (lower) layer
1126 aboveCoveredLayers.orSelf(visibleRegion);
1127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 // subtract the opaque region covered by the layers above us
1129 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130
1131 // compute this layer's dirty region
1132 if (layer->contentDirty) {
1133 // we need to invalidate the whole region
1134 dirty = visibleRegion;
1135 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001136 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 layer->contentDirty = false;
1138 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001139 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001140 * the exposed region consists of two components:
1141 * 1) what's VISIBLE now and was COVERED before
1142 * 2) what's EXPOSED now less what was EXPOSED before
1143 *
1144 * note that (1) is conservative, we start with the whole
1145 * visible region but only keep what used to be covered by
1146 * something -- which mean it may have been exposed.
1147 *
1148 * (2) handles areas that were not covered by anything but got
1149 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001150 */
Mathias Agopianab028732010-03-16 16:41:46 -07001151 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001152 const Region oldVisibleRegion = layer->visibleRegion;
1153 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001154 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1155 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 }
1157 dirty.subtractSelf(aboveOpaqueLayers);
1158
1159 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001160 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161
Mathias Agopianab028732010-03-16 16:41:46 -07001162 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 // Store the visible region is screen space
1166 layer->setVisibleRegion(visibleRegion);
1167 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 }
1169
Mathias Agopian87baae12012-07-31 12:38:26 -07001170 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171}
1172
Mathias Agopian87baae12012-07-31 12:38:26 -07001173void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1174 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001175 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001176 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1177 if (hw->getLayerStack() == layerStack) {
1178 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001179 }
1180 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001181}
1182
1183void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001185 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186
Mathias Agopian4fec8732012-06-29 14:12:52 -07001187 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001188 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001189 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001190 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001191 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001192 const Region dirty(layer->latchBuffer(visibleRegions));
1193 Layer::State s(layer->drawingState());
1194 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001195 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001196
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001197 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198}
1199
Mathias Agopianad456f92011-01-13 17:53:01 -08001200void SurfaceFlinger::invalidateHwcGeometry()
1201{
1202 mHwWorkListDirty = true;
1203}
1204
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001205
Mathias Agopiancd60f992012-08-16 16:28:27 -07001206void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001207 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208{
Mathias Agopian87baae12012-07-31 12:38:26 -07001209 Region dirtyRegion(inDirtyRegion);
1210
Mathias Agopianb8a55602009-06-26 19:06:36 -07001211 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001212 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213
Mathias Agopian42977342012-08-05 00:40:46 -07001214 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001215 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001216 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1217 // takes a rectangle, we must make sure to update that whole
1218 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001219 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001220 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001221 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001222 // We need to redraw the rectangle that will be updated
1223 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001224 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001225 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001226 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001227 } else {
1228 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001229 dirtyRegion.set(hw->bounds());
1230 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 }
1232 }
1233
Mathias Agopiancd60f992012-08-16 16:28:27 -07001234 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235
Mathias Agopiancd60f992012-08-16 16:28:27 -07001236 // FIXME: we need to call eglSwapBuffers() on displays that have
1237 // GL composition and only on those.
1238 // however, currently hwc.commit() already does that for the main
1239 // display and never for the other ones
1240 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
1241 // FIXME: EGL spec says:
1242 // "surface must be bound to the calling thread's current context,
1243 // for the current rendering API."
1244 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001245 }
1246
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001247 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001248 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249}
1250
Mathias Agopiancd60f992012-08-16 16:28:27 -07001251void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001252{
Mathias Agopian86303202012-07-24 22:46:10 -07001253 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001254 int32_t id = hw->getDisplayId();
1255 HWComposer::LayerListIterator cur = hwc.begin(id);
1256 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001257
Mathias Agopian1e260872012-08-08 18:35:12 -07001258 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001259 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001260
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001261 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001262
1263 // set the frame buffer
1264 glMatrixMode(GL_MODELVIEW);
1265 glLoadIdentity();
1266
1267 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001268 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001269 // when using overlays, we assume a fully transparent framebuffer
1270 // NOTE: we could reduce how much we need to clear, for instance
1271 // remove where there are opaque FB layers. however, on some
1272 // GPUs doing a "clean slate" glClear might be more efficient.
1273 // We'll revisit later if needed.
1274 glClearColor(0, 0, 0, 0);
1275 glClear(GL_COLOR_BUFFER_BIT);
1276 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001277 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001278 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001279 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001280 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001281 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001282 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001283 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001284
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001285 /*
1286 * and then, render the layers targeted at the framebuffer
1287 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001288
Mathias Agopian42977342012-08-05 00:40:46 -07001289 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001290 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001291 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001292 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001293 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001294 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Jesse Halla6b32db2012-07-19 16:44:38 -07001295 if (cur != end) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001296 // we're using h/w composer
1297 if (!clip.isEmpty()) {
1298 if (cur->getCompositionType() == HWC_OVERLAY) {
1299 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
1300 && layer->isOpaque()) {
1301 // never clear the very first layer since we're
1302 // guaranteed the FB is already cleared
1303 layer->clearWithOpenGL(hw, clip);
1304 }
1305 } else {
1306 layer->draw(hw, clip);
1307 }
1308 layer->setAcquireFence(hw, *cur);
1309 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001310 ++cur;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001311 } else {
1312 // we're not using h/w composer
1313 if (!clip.isEmpty()) {
1314 layer->draw(hw, clip);
1315 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001316 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001317 }
1318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319}
1320
Mathias Agopian87baae12012-07-31 12:38:26 -07001321void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001323 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001324 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001325 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001326 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001327
1328 GLfloat vertices[4][2];
1329 glVertexPointer(2, GL_FLOAT, 0, vertices);
1330 Region::const_iterator it = region.begin();
1331 Region::const_iterator const end = region.end();
1332 while (it != end) {
1333 const Rect& r = *it++;
1334 vertices[0][0] = r.left;
1335 vertices[0][1] = r.top;
1336 vertices[1][0] = r.right;
1337 vertices[1][1] = r.top;
1338 vertices[2][0] = r.right;
1339 vertices[2][1] = r.bottom;
1340 vertices[3][0] = r.left;
1341 vertices[3][1] = r.bottom;
1342 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1343 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344}
1345
Mathias Agopian96f08192010-06-02 23:28:45 -07001346ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1347 const sp<LayerBaseClient>& lbc)
1348{
Mathias Agopian96f08192010-06-02 23:28:45 -07001349 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001350 size_t name = client->attachLayer(lbc);
1351
Mathias Agopian96f08192010-06-02 23:28:45 -07001352 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001353 Mutex::Autolock _l(mStateLock);
1354 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001355
Mathias Agopian4f113742011-05-03 16:21:41 -07001356 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001357}
1358
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001359status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360{
1361 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001362 status_t err = purgatorizeLayer_l(layer);
1363 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001364 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001365 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366}
1367
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001368status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369{
1370 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1371 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001372 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 return NO_ERROR;
1374 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001375 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376}
1377
Mathias Agopian9a112062009-04-17 19:36:26 -07001378status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1379{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001380 // First add the layer to the purgatory list, which makes sure it won't
1381 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001382 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001383 if (err >= 0) {
1384 mLayerPurgatory.add(layerBase);
1385 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001386
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001387 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001388
Mathias Agopian3d579642009-06-04 18:46:21 -07001389 // it's possible that we don't find a layer, because it might
1390 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001391 // from the user because there is a race between Client::destroySurface(),
1392 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001393 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1394}
1395
Mathias Agopiandea20b12011-05-03 17:04:02 -07001396uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1397{
1398 return android_atomic_release_load(&mTransactionFlags);
1399}
1400
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1402{
1403 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1404}
1405
Mathias Agopianbb641242010-05-18 17:06:55 -07001406uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407{
1408 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1409 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001410 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 }
1412 return old;
1413}
1414
Mathias Agopian8b33f032012-07-24 20:43:54 -07001415void SurfaceFlinger::setTransactionState(
1416 const Vector<ComposerState>& state,
1417 const Vector<DisplayState>& displays,
1418 uint32_t flags)
1419{
Mathias Agopian698c0872011-06-28 19:09:31 -07001420 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001421 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001422
1423 size_t count = displays.size();
1424 for (size_t i=0 ; i<count ; i++) {
1425 const DisplayState& s(displays[i]);
1426 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001427 }
1428
Mathias Agopiane57f2922012-08-09 16:29:12 -07001429 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001430 for (size_t i=0 ; i<count ; i++) {
1431 const ComposerState& s(state[i]);
1432 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001433 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001434 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001435
Mathias Agopian386aa982011-11-07 21:58:03 -08001436 if (transactionFlags) {
1437 // this triggers the transaction
1438 setTransactionFlags(transactionFlags);
1439
1440 // if this is a synchronous transaction, wait for it to take effect
1441 // before returning.
1442 if (flags & eSynchronous) {
1443 mTransationPending = true;
1444 }
1445 while (mTransationPending) {
1446 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1447 if (CC_UNLIKELY(err != NO_ERROR)) {
1448 // just in case something goes wrong in SF, return to the
1449 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001450 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001451 mTransationPending = false;
1452 break;
1453 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 }
1456}
1457
Mathias Agopiane57f2922012-08-09 16:29:12 -07001458uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1459{
1460 uint32_t flags = 0;
1461 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1462 if (disp.id >= 0) {
1463 const uint32_t what = s.what;
1464 if (what & DisplayState::eSurfaceChanged) {
1465 if (disp.surface->asBinder() != s.surface->asBinder()) {
1466 disp.surface = s.surface;
1467 flags |= eDisplayTransactionNeeded;
1468 }
1469 }
1470 if (what & DisplayState::eLayerStackChanged) {
1471 if (disp.layerStack != s.layerStack) {
1472 disp.layerStack = s.layerStack;
1473 flags |= eDisplayTransactionNeeded;
1474 }
1475 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001476 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001477 if (disp.orientation != s.orientation) {
1478 disp.orientation = s.orientation;
1479 flags |= eDisplayTransactionNeeded;
1480 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001481 }
1482 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001483 if (disp.frame != s.frame) {
1484 disp.frame = s.frame;
1485 flags |= eDisplayTransactionNeeded;
1486 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001487 }
1488 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001489 if (disp.viewport != s.viewport) {
1490 disp.viewport = s.viewport;
1491 flags |= eDisplayTransactionNeeded;
1492 }
1493 }
1494 }
1495 return flags;
1496}
1497
1498uint32_t SurfaceFlinger::setClientStateLocked(
1499 const sp<Client>& client,
1500 const layer_state_t& s)
1501{
1502 uint32_t flags = 0;
1503 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1504 if (layer != 0) {
1505 const uint32_t what = s.what;
1506 if (what & layer_state_t::ePositionChanged) {
1507 if (layer->setPosition(s.x, s.y))
1508 flags |= eTraversalNeeded;
1509 }
1510 if (what & layer_state_t::eLayerChanged) {
1511 // NOTE: index needs to be calculated before we update the state
1512 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1513 if (layer->setLayer(s.z)) {
1514 mCurrentState.layersSortedByZ.removeAt(idx);
1515 mCurrentState.layersSortedByZ.add(layer);
1516 // we need traversal (state changed)
1517 // AND transaction (list changed)
1518 flags |= eTransactionNeeded|eTraversalNeeded;
1519 }
1520 }
1521 if (what & layer_state_t::eSizeChanged) {
1522 if (layer->setSize(s.w, s.h)) {
1523 flags |= eTraversalNeeded;
1524 }
1525 }
1526 if (what & layer_state_t::eAlphaChanged) {
1527 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1528 flags |= eTraversalNeeded;
1529 }
1530 if (what & layer_state_t::eMatrixChanged) {
1531 if (layer->setMatrix(s.matrix))
1532 flags |= eTraversalNeeded;
1533 }
1534 if (what & layer_state_t::eTransparentRegionChanged) {
1535 if (layer->setTransparentRegionHint(s.transparentRegion))
1536 flags |= eTraversalNeeded;
1537 }
1538 if (what & layer_state_t::eVisibilityChanged) {
1539 if (layer->setFlags(s.flags, s.mask))
1540 flags |= eTraversalNeeded;
1541 }
1542 if (what & layer_state_t::eCropChanged) {
1543 if (layer->setCrop(s.crop))
1544 flags |= eTraversalNeeded;
1545 }
1546 if (what & layer_state_t::eLayerStackChanged) {
1547 // NOTE: index needs to be calculated before we update the state
1548 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1549 if (layer->setLayerStack(s.layerStack)) {
1550 mCurrentState.layersSortedByZ.removeAt(idx);
1551 mCurrentState.layersSortedByZ.add(layer);
1552 // we need traversal (state changed)
1553 // AND transaction (list changed)
1554 flags |= eTransactionNeeded|eTraversalNeeded;
1555 }
1556 }
1557 }
1558 return flags;
1559}
1560
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001561sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001562 ISurfaceComposerClient::surface_data_t* params,
1563 const String8& name,
1564 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1566 uint32_t flags)
1567{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001568 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001569 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001570
1571 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001572 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001573 int(w), int(h));
1574 return surfaceHandle;
1575 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001576
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001577 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001578 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1579 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001580 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001582 case ISurfaceComposerClient::eFXSurfaceBlur:
1583 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001584 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001586 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001587 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001588 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 }
1590
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001591 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001592 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001593 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001594 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001596 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001597 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001598 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001599 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001600 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601 }
1602
1603 return surfaceHandle;
1604}
1605
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001606sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001607 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001608 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001609 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610{
1611 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001612 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 case PIXEL_FORMAT_TRANSPARENT:
1614 case PIXEL_FORMAT_TRANSLUCENT:
1615 format = PIXEL_FORMAT_RGBA_8888;
1616 break;
1617 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001618#ifdef NO_RGBX_8888
1619 format = PIXEL_FORMAT_RGB_565;
1620#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001621 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001622#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 break;
1624 }
1625
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001626#ifdef NO_RGBX_8888
1627 if (format == PIXEL_FORMAT_RGBX_8888)
1628 format = PIXEL_FORMAT_RGBA_8888;
1629#endif
1630
Mathias Agopian96f08192010-06-02 23:28:45 -07001631 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001632 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001633 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001634 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001635 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 }
1637 return layer;
1638}
1639
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001640sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001641 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001642 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643{
Mathias Agopian96f08192010-06-02 23:28:45 -07001644 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001645 return layer;
1646}
1647
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001648sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001649 const sp<Client>& client, DisplayID display,
1650 uint32_t w, uint32_t h, uint32_t flags)
1651{
1652 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 return layer;
1654}
1655
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001656status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657{
Mathias Agopian9a112062009-04-17 19:36:26 -07001658 /*
1659 * called by the window manager, when a surface should be marked for
1660 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001661 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001662 * The surface is removed from the current and drawing lists, but placed
1663 * in the purgatory queue, so it's not destroyed right-away (we need
1664 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001665 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001666
Mathias Agopian48d819a2009-09-10 19:41:18 -07001667 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001668 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001669 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001670
Mathias Agopian48d819a2009-09-10 19:41:18 -07001671 if (layer != 0) {
1672 err = purgatorizeLayer_l(layer);
1673 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001674 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001675 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001676 }
1677 return err;
1678}
1679
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001680status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001681{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001682 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001683 status_t err = NO_ERROR;
1684 sp<LayerBaseClient> l(layer.promote());
1685 if (l != NULL) {
1686 Mutex::Autolock _l(mStateLock);
1687 err = removeLayer_l(l);
1688 if (err == NAME_NOT_FOUND) {
1689 // The surface wasn't in the current list, which means it was
1690 // removed already, which means it is in the purgatory,
1691 // and need to be removed from there.
1692 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001693 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001694 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001695 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001696 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001697 "error removing layer=%p (%s)", l.get(), strerror(-err));
1698 }
1699 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700}
1701
Mathias Agopianb60314a2012-04-10 22:09:54 -07001702// ---------------------------------------------------------------------------
1703
1704void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001705 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001706 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001707 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001708 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001709 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001710 mVisibleRegionsDirty = true;
1711 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712}
1713
Mathias Agopianb60314a2012-04-10 22:09:54 -07001714void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001715 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001716 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1717 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001718 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001719 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001720 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001721 // from this point on, SF will stop drawing
1722 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723}
1724
Colin Cross8e533062012-06-07 13:17:52 -07001725void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001726 class MessageScreenAcquired : public MessageBase {
1727 SurfaceFlinger* flinger;
1728 public:
1729 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1730 virtual bool handler() {
1731 flinger->onScreenAcquired();
1732 return true;
1733 }
1734 };
1735 sp<MessageBase> msg = new MessageScreenAcquired(this);
1736 postMessageSync(msg);
1737}
1738
Colin Cross8e533062012-06-07 13:17:52 -07001739void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001740 class MessageScreenReleased : public MessageBase {
1741 SurfaceFlinger* flinger;
1742 public:
1743 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1744 virtual bool handler() {
1745 flinger->onScreenReleased();
1746 return true;
1747 }
1748 };
1749 sp<MessageBase> msg = new MessageScreenReleased(this);
1750 postMessageSync(msg);
1751}
1752
1753// ---------------------------------------------------------------------------
1754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1756{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001757 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 char buffer[SIZE];
1759 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001760
1761 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762 snprintf(buffer, SIZE, "Permission Denial: "
1763 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1764 IPCThreadState::self()->getCallingPid(),
1765 IPCThreadState::self()->getCallingUid());
1766 result.append(buffer);
1767 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001768 // Try to get the main lock, but don't insist if we can't
1769 // (this would indicate SF is stuck, but we want to be able to
1770 // print something in dumpsys).
1771 int retry = 3;
1772 while (mStateLock.tryLock()<0 && --retry>=0) {
1773 usleep(1000000);
1774 }
1775 const bool locked(retry >= 0);
1776 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001777 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001778 "SurfaceFlinger appears to be unresponsive, "
1779 "dumping anyways (no locks held)\n");
1780 result.append(buffer);
1781 }
1782
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001783 bool dumpAll = true;
1784 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001785 size_t numArgs = args.size();
1786 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001787 if ((index < numArgs) &&
1788 (args[index] == String16("--list"))) {
1789 index++;
1790 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001791 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001792 }
1793
1794 if ((index < numArgs) &&
1795 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001796 index++;
1797 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001798 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001799 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001800
1801 if ((index < numArgs) &&
1802 (args[index] == String16("--latency-clear"))) {
1803 index++;
1804 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001805 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001806 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001808
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001809 if (dumpAll) {
1810 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001811 }
1812
Mathias Agopian9795c422009-08-26 16:36:26 -07001813 if (locked) {
1814 mStateLock.unlock();
1815 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 }
1817 write(fd, result.string(), result.size());
1818 return NO_ERROR;
1819}
1820
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001821void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1822 String8& result, char* buffer, size_t SIZE) const
1823{
1824 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1825 const size_t count = currentLayers.size();
1826 for (size_t i=0 ; i<count ; i++) {
1827 const sp<LayerBase>& layer(currentLayers[i]);
1828 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1829 result.append(buffer);
1830 }
1831}
1832
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001833void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1834 String8& result, char* buffer, size_t SIZE) const
1835{
1836 String8 name;
1837 if (index < args.size()) {
1838 name = String8(args[index]);
1839 index++;
1840 }
1841
1842 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1843 const size_t count = currentLayers.size();
1844 for (size_t i=0 ; i<count ; i++) {
1845 const sp<LayerBase>& layer(currentLayers[i]);
1846 if (name.isEmpty()) {
1847 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1848 result.append(buffer);
1849 }
1850 if (name.isEmpty() || (name == layer->getName())) {
1851 layer->dumpStats(result, buffer, SIZE);
1852 }
1853 }
1854}
1855
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001856void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1857 String8& result, char* buffer, size_t SIZE) const
1858{
1859 String8 name;
1860 if (index < args.size()) {
1861 name = String8(args[index]);
1862 index++;
1863 }
1864
1865 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1866 const size_t count = currentLayers.size();
1867 for (size_t i=0 ; i<count ; i++) {
1868 const sp<LayerBase>& layer(currentLayers[i]);
1869 if (name.isEmpty() || (name == layer->getName())) {
1870 layer->clearStats();
1871 }
1872 }
1873}
1874
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001875void SurfaceFlinger::dumpAllLocked(
1876 String8& result, char* buffer, size_t SIZE) const
1877{
1878 // figure out if we're stuck somewhere
1879 const nsecs_t now = systemTime();
1880 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1881 const nsecs_t inTransaction(mDebugInTransaction);
1882 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1883 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1884
1885 /*
1886 * Dump the visible layer list
1887 */
1888 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1889 const size_t count = currentLayers.size();
1890 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1891 result.append(buffer);
1892 for (size_t i=0 ; i<count ; i++) {
1893 const sp<LayerBase>& layer(currentLayers[i]);
1894 layer->dump(result, buffer, SIZE);
1895 }
1896
1897 /*
1898 * Dump the layers in the purgatory
1899 */
1900
1901 const size_t purgatorySize = mLayerPurgatory.size();
1902 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1903 result.append(buffer);
1904 for (size_t i=0 ; i<purgatorySize ; i++) {
1905 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1906 layer->shortDump(result, buffer, SIZE);
1907 }
1908
1909 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001910 * Dump Display state
1911 */
1912
1913 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1914 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1915 snprintf(buffer, SIZE,
1916 "+ DisplayDevice[%u]\n"
1917 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1918 "flips=%u, secure=%d, numLayers=%u\n",
1919 dpy,
1920 hw->getDisplayId(), hw->getLayerStack(),
1921 hw->getWidth(), hw->getHeight(),
1922 hw->getOrientation(), hw->getTransform().getType(),
1923 hw->getPageFlipCount(),
1924 hw->getSecureLayerVisible(),
1925 hw->getVisibleLayersSortedByZ().size());
1926 result.append(buffer);
1927 }
1928
1929 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001930 * Dump SurfaceFlinger global state
1931 */
1932
1933 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1934 result.append(buffer);
1935
Mathias Agopian888c8222012-08-04 21:10:38 -07001936 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001937 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001938 const GLExtensions& extensions(GLExtensions::getInstance());
1939 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1940 extensions.getVendor(),
1941 extensions.getRenderer(),
1942 extensions.getVersion());
1943 result.append(buffer);
1944
1945 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001946 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001947 result.append(buffer);
1948
1949 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1950 result.append(buffer);
1951
Mathias Agopian42977342012-08-05 00:40:46 -07001952 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001953 snprintf(buffer, SIZE,
1954 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001955 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001956 result.append(buffer);
1957 snprintf(buffer, SIZE,
1958 " last eglSwapBuffers() time: %f us\n"
1959 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001960 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001961 " refresh-rate : %f fps\n"
1962 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001963 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001964 mLastSwapBufferTime/1000.0,
1965 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001966 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001967 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001968 hwc.getDpiX(),
1969 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001970 result.append(buffer);
1971
1972 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1973 inSwapBuffersDuration/1000.0);
1974 result.append(buffer);
1975
1976 snprintf(buffer, SIZE, " transaction time: %f us\n",
1977 inTransactionDuration/1000.0);
1978 result.append(buffer);
1979
1980 /*
1981 * VSYNC state
1982 */
1983 mEventThread->dump(result, buffer, SIZE);
1984
1985 /*
1986 * Dump HWComposer state
1987 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001988 snprintf(buffer, SIZE, "h/w composer state:\n");
1989 result.append(buffer);
1990 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1991 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1992 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1993 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07001994 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001995
1996 /*
1997 * Dump gralloc state
1998 */
1999 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2000 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002001 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002002}
2003
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002004status_t SurfaceFlinger::onTransact(
2005 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2006{
2007 switch (code) {
2008 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002009 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002011 case BLANK:
2012 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013 {
2014 // codes that require permission check
2015 IPCThreadState* ipc = IPCThreadState::self();
2016 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002017 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002018 if ((uid != AID_GRAPHICS) &&
2019 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002020 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002021 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2022 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002024 break;
2025 }
2026 case CAPTURE_SCREEN:
2027 {
2028 // codes that require permission check
2029 IPCThreadState* ipc = IPCThreadState::self();
2030 const int pid = ipc->getCallingPid();
2031 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002032 if ((uid != AID_GRAPHICS) &&
2033 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002034 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002035 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2036 return PERMISSION_DENIED;
2037 }
2038 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039 }
2040 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002042 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2043 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002044 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002045 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002046 IPCThreadState* ipc = IPCThreadState::self();
2047 const int pid = ipc->getCallingPid();
2048 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002049 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002050 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051 return PERMISSION_DENIED;
2052 }
2053 int n;
2054 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002055 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002056 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002057 return NO_ERROR;
2058 case 1002: // SHOW_UPDATES
2059 n = data.readInt32();
2060 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002061 invalidateHwcGeometry();
2062 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002063 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002064 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002065 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002066 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002068 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002069 setTransactionFlags(
2070 eTransactionNeeded|
2071 eDisplayTransactionNeeded|
2072 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002073 return NO_ERROR;
2074 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002075 case 1006:{ // send empty update
2076 signalRefresh();
2077 return NO_ERROR;
2078 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002079 case 1008: // toggle use of hw composer
2080 n = data.readInt32();
2081 mDebugDisableHWC = n ? 1 : 0;
2082 invalidateHwcGeometry();
2083 repaintEverything();
2084 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002085 case 1009: // toggle use of transform hint
2086 n = data.readInt32();
2087 mDebugDisableTransformHint = n ? 1 : 0;
2088 invalidateHwcGeometry();
2089 repaintEverything();
2090 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002092 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 reply->writeInt32(0);
2094 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002095 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002096 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097 return NO_ERROR;
2098 case 1013: {
2099 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002100 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2101 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102 }
2103 return NO_ERROR;
2104 }
2105 }
2106 return err;
2107}
2108
Mathias Agopian53331da2011-08-22 21:44:41 -07002109void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002110 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002111 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002112}
2113
Mathias Agopian59119e62010-10-11 12:37:43 -07002114// ---------------------------------------------------------------------------
2115
Mathias Agopian118d0242011-10-13 16:02:48 -07002116status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2117 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2118{
2119 Mutex::Autolock _l(mStateLock);
2120 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2121}
2122
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002123status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2124 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002125{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002126 ATRACE_CALL();
2127
Mathias Agopian59119e62010-10-11 12:37:43 -07002128 if (!GLExtensions::getInstance().haveFramebufferObject())
2129 return INVALID_OPERATION;
2130
2131 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002132 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2133 const uint32_t hw_w = hw->getWidth();
2134 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002135 GLfloat u = 1;
2136 GLfloat v = 1;
2137
2138 // make sure to clear all GL error flags
2139 while ( glGetError() != GL_NO_ERROR ) ;
2140
2141 // create a FBO
2142 GLuint name, tname;
2143 glGenTextures(1, &tname);
2144 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002145 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2146 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002147 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2148 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002149 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002150 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002151 GLint tw = (2 << (31 - clz(hw_w)));
2152 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002153 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2154 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002155 u = GLfloat(hw_w) / tw;
2156 v = GLfloat(hw_h) / th;
2157 }
2158 glGenFramebuffersOES(1, &name);
2159 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002160 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2161 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002162
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002163 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002164 glDisable(GL_TEXTURE_EXTERNAL_OES);
2165 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002166 glClearColor(0,0,0,1);
2167 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002168 glMatrixMode(GL_MODELVIEW);
2169 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002170 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002171 const size_t count = layers.size();
2172 for (size_t i=0 ; i<count ; ++i) {
2173 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002174 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002175 }
2176
Mathias Agopian42977342012-08-05 00:40:46 -07002177 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002178
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179 // back to main framebuffer
2180 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002181 glDeleteFramebuffersOES(1, &name);
2182
2183 *textureName = tname;
2184 *uOut = u;
2185 *vOut = v;
2186 return NO_ERROR;
2187}
2188
2189// ---------------------------------------------------------------------------
2190
Mathias Agopian74c40c02010-09-29 13:02:36 -07002191status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2192 sp<IMemoryHeap>* heap,
2193 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002194 uint32_t sw, uint32_t sh,
2195 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002196{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002197 ATRACE_CALL();
2198
Mathias Agopian74c40c02010-09-29 13:02:36 -07002199 status_t result = PERMISSION_DENIED;
2200
2201 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002202 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002203 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002204 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002205 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002206
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002207 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002208 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002209 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002210
2211 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002212 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2213 const uint32_t hw_w = hw->getWidth();
2214 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002215
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002216 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002217 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002218 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002219 return PERMISSION_DENIED;
2220 }
2221
2222 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002223 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002224 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002225 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002226
2227 sw = (!sw) ? hw_w : sw;
2228 sh = (!sh) ? hw_h : sh;
2229 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002230 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002231
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002232// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2233// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002234
Mathias Agopian74c40c02010-09-29 13:02:36 -07002235 // make sure to clear all GL error flags
2236 while ( glGetError() != GL_NO_ERROR ) ;
2237
2238 // create a FBO
2239 GLuint name, tname;
2240 glGenRenderbuffersOES(1, &tname);
2241 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2242 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002243
Mathias Agopian74c40c02010-09-29 13:02:36 -07002244 glGenFramebuffersOES(1, &name);
2245 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2246 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2247 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2248
2249 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002250
Mathias Agopian74c40c02010-09-29 13:02:36 -07002251 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2252
2253 // invert everything, b/c glReadPixel() below will invert the FB
2254 glViewport(0, 0, sw, sh);
2255 glMatrixMode(GL_PROJECTION);
2256 glPushMatrix();
2257 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002258 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259 glMatrixMode(GL_MODELVIEW);
2260
2261 // redraw the screen entirely...
2262 glClearColor(0,0,0,1);
2263 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002264
Jamie Gennis9575f602011-10-07 14:51:16 -07002265 const LayerVector& layers(mDrawingState.layersSortedByZ);
2266 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002267 for (size_t i=0 ; i<count ; ++i) {
2268 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002269 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002270 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002271 const uint32_t z = layer->drawingState().z;
2272 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002273 if (filtering) layer->setFiltering(true);
2274 layer->draw(hw);
2275 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002276 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002277 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278 }
2279
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280 // check for errors and return screen capture
2281 if (glGetError() != GL_NO_ERROR) {
2282 // error while rendering
2283 result = INVALID_OPERATION;
2284 } else {
2285 // allocate shared memory large enough to hold the
2286 // screen capture
2287 sp<MemoryHeapBase> base(
2288 new MemoryHeapBase(size, 0, "screen-capture") );
2289 void* const ptr = base->getBase();
2290 if (ptr) {
2291 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002292 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002293 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2294 if (glGetError() == GL_NO_ERROR) {
2295 *heap = base;
2296 *w = sw;
2297 *h = sh;
2298 *f = PIXEL_FORMAT_RGBA_8888;
2299 result = NO_ERROR;
2300 }
2301 } else {
2302 result = NO_MEMORY;
2303 }
2304 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002305 glViewport(0, 0, hw_w, hw_h);
2306 glMatrixMode(GL_PROJECTION);
2307 glPopMatrix();
2308 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002309 } else {
2310 result = BAD_VALUE;
2311 }
2312
2313 // release FBO resources
2314 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2315 glDeleteRenderbuffersOES(1, &tname);
2316 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002317
Mathias Agopian42977342012-08-05 00:40:46 -07002318 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002319
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002320// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002321
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 return result;
2323}
2324
2325
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002326status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2327 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002329 uint32_t sw, uint32_t sh,
2330 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002331{
2332 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002333 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002334 return BAD_VALUE;
2335
2336 if (!GLExtensions::getInstance().haveFramebufferObject())
2337 return INVALID_OPERATION;
2338
2339 class MessageCaptureScreen : public MessageBase {
2340 SurfaceFlinger* flinger;
2341 DisplayID dpy;
2342 sp<IMemoryHeap>* heap;
2343 uint32_t* w;
2344 uint32_t* h;
2345 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002346 uint32_t sw;
2347 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002348 uint32_t minLayerZ;
2349 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002350 status_t result;
2351 public:
2352 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002353 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002354 uint32_t sw, uint32_t sh,
2355 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002356 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002357 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2358 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2359 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002360 {
2361 }
2362 status_t getResult() const {
2363 return result;
2364 }
2365 virtual bool handler() {
2366 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002367 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002368 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002369 return true;
2370 }
2371 };
2372
2373 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002374 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002375 status_t res = postMessageSync(msg);
2376 if (res == NO_ERROR) {
2377 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2378 }
2379 return res;
2380}
2381
2382// ---------------------------------------------------------------------------
2383
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002384SurfaceFlinger::LayerVector::LayerVector() {
2385}
2386
2387SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2388 : SortedVector<sp<LayerBase> >(rhs) {
2389}
2390
2391int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2392 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002393{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002394 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002395 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2396 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002397
2398 uint32_t ls = l->currentState().layerStack;
2399 uint32_t rs = r->currentState().layerStack;
2400 if (ls != rs)
2401 return ls - rs;
2402
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002403 uint32_t lz = l->currentState().z;
2404 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002405 if (lz != rz)
2406 return lz - rz;
2407
2408 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002409}
2410
2411// ---------------------------------------------------------------------------
2412
Mathias Agopiane57f2922012-08-09 16:29:12 -07002413SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2414}
2415
2416SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2417 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002418}
2419
2420// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002421
Jamie Gennis9a78c902011-01-12 18:30:40 -08002422GraphicBufferAlloc::GraphicBufferAlloc() {}
2423
2424GraphicBufferAlloc::~GraphicBufferAlloc() {}
2425
2426sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002427 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002428 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2429 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002430 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002431 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002432 if (err == NO_MEMORY) {
2433 GraphicBuffer::dumpAllocationsToSystemLog();
2434 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002435 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002436 "failed (%s), handle=%p",
2437 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002438 return 0;
2439 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002440 return graphicBuffer;
2441}
2442
Jamie Gennis9a78c902011-01-12 18:30:40 -08002443// ---------------------------------------------------------------------------
2444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002445}; // namespace android