blob: e6e258f4ef6871121911c8b25c999546aafd187e [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
Ramanan Rajeswaranf1bf89d2012-08-22 14:23:50 -0700140 // reset screen orientation
141 Vector<ComposerState> state;
142 Vector<DisplayState> displays;
143 DisplayState d;
144 d.what = DisplayState::eOrientationChanged;
145 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
146 d.orientation = DisplayState::eOrientationDefault;
147 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,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700412 DisplayDevice::DISPLAY_ID_MAIN, HWC_DISPLAY_PRIMARY,
413 anw, fbs, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700414 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700415
416 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700417 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700418 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800419
Mathias Agopian028508c2012-07-25 21:12:12 -0700420 // start the EventThread
421 mEventThread = new EventThread(this);
422 mEventQueue.setEventThread(mEventThread);
423
Mathias Agopian86303202012-07-24 22:46:10 -0700424 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700425 mHwc = new HWComposer(this,
426 *static_cast<HWComposer::EventHandler *>(this),
427 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700428
429 // initialize our drawing state
430 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700431
Mathias Agopiana4912602012-07-12 14:25:33 -0700432 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800433 mReadyToRunBarrier.open();
434
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700435 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700436 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700437
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 return NO_ERROR;
439}
440
Mathias Agopiana67e4182012-06-19 17:26:12 -0700441void SurfaceFlinger::startBootAnim() {
442 // start boot animation
443 property_set("service.bootanim.exit", "0");
444 property_set("ctl.start", "bootanim");
445}
446
Mathias Agopiana4912602012-07-12 14:25:33 -0700447uint32_t SurfaceFlinger::getMaxTextureSize() const {
448 return mMaxTextureSize;
449}
450
451uint32_t SurfaceFlinger::getMaxViewportDims() const {
452 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
453 mMaxViewportDims[0] : mMaxViewportDims[1];
454}
455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800457
Jamie Gennis582270d2011-08-17 18:19:00 -0700458bool SurfaceFlinger::authenticateSurfaceTexture(
459 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800460 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700461 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800462
463 // Check the visible layer list for the ISurface
464 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
465 size_t count = currentLayers.size();
466 for (size_t i=0 ; i<count ; i++) {
467 const sp<LayerBase>& layer(currentLayers[i]);
468 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700469 if (lbc != NULL) {
470 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
471 if (lbcBinder == surfaceTextureBinder) {
472 return true;
473 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800474 }
475 }
476
477 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700478 // SurfaceTexture gets destroyed before all the clients are done using it,
479 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800480 // will instead fail later on when the client tries to use the surface,
481 // which should be reported as "surface XYZ returned an -ENODEV". The
482 // purgatorized layers are no less authentic than the visible ones, so this
483 // should not cause any harm.
484 size_t purgatorySize = mLayerPurgatory.size();
485 for (size_t i=0 ; i<purgatorySize ; i++) {
486 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
487 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700488 if (lbc != NULL) {
489 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
490 if (lbcBinder == surfaceTextureBinder) {
491 return true;
492 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800493 }
494 }
495
496 return false;
497}
498
Mathias Agopianc666cae2012-07-25 18:56:13 -0700499status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700500 // TODO: this is here only for compatibility -- should go away eventually.
501 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700502 return BAD_INDEX;
503 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700504
505 const HWComposer& hwc(getHwComposer());
506 float xdpi = hwc.getDpiX();
507 float ydpi = hwc.getDpiY();
508
509 // TODO: Not sure if display density should handled by SF any longer
510 class Density {
511 static int getDensityFromProperty(char const* propName) {
512 char property[PROPERTY_VALUE_MAX];
513 int density = 0;
514 if (property_get(propName, property, NULL) > 0) {
515 density = atoi(property);
516 }
517 return density;
518 }
519 public:
520 static int getEmuDensity() {
521 return getDensityFromProperty("qemu.sf.lcd_density"); }
522 static int getBuildDensity() {
523 return getDensityFromProperty("ro.sf.lcd_density"); }
524 };
525 // The density of the device is provided by a build property
526 float density = Density::getBuildDensity() / 160.0f;
527 if (density == 0) {
528 // the build doesn't provide a density -- this is wrong!
529 // use xdpi instead
530 ALOGE("ro.sf.lcd_density must be defined as a build property");
531 density = xdpi / 160.0f;
532 }
533 if (Density::getEmuDensity()) {
534 // if "qemu.sf.lcd_density" is specified, it overrides everything
535 xdpi = ydpi = density = Density::getEmuDensity();
536 density /= 160.0f;
537 }
538
Mathias Agopian42977342012-08-05 00:40:46 -0700539 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
540 info->w = hw->getWidth();
541 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700542 info->xdpi = xdpi;
543 info->ydpi = ydpi;
544 info->fps = float(1e9 / hwc.getRefreshPeriod());
545 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700546 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700547 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700548 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700549 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700550}
551
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800552// ----------------------------------------------------------------------------
553
554sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800555 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700556}
557
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700558void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700559
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700560 sp<IBinder> token;
561 { // scope for the lock
562 Mutex::Autolock _l(mStateLock);
563 token = mExtDisplayToken;
564 }
565
566 if (token == 0) {
567 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700568 }
569
570 { // scope for the lock
571 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700572 if (surface == 0) {
573 // release our current display. we're guarantee to have
574 // a reference to it (token), while we hold the lock
575 mExtDisplayToken = 0;
576 } else {
577 mExtDisplayToken = token;
578 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700579
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700580 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
581 info.surface = surface;
582 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700583 }
584}
585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800587
588void SurfaceFlinger::waitForEvent() {
589 mEventQueue.waitMessage();
590}
591
592void SurfaceFlinger::signalTransaction() {
593 mEventQueue.invalidate();
594}
595
596void SurfaceFlinger::signalLayerUpdate() {
597 mEventQueue.invalidate();
598}
599
600void SurfaceFlinger::signalRefresh() {
601 mEventQueue.refresh();
602}
603
604status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
605 nsecs_t reltime, uint32_t flags) {
606 return mEventQueue.postMessage(msg, reltime);
607}
608
609status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
610 nsecs_t reltime, uint32_t flags) {
611 status_t res = mEventQueue.postMessage(msg, reltime);
612 if (res == NO_ERROR) {
613 msg->wait();
614 }
615 return res;
616}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617
Mathias Agopian4fec8732012-06-29 14:12:52 -0700618bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 return true;
621}
622
Mathias Agopian86303202012-07-24 22:46:10 -0700623void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700624 mEventThread->onVSyncReceived(dpy, timestamp);
625}
626
627void SurfaceFlinger::eventControl(int event, int enabled) {
628 getHwComposer().eventControl(event, enabled);
629}
630
Mathias Agopian4fec8732012-06-29 14:12:52 -0700631void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800632 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800633 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700634 case MessageQueue::INVALIDATE:
635 handleMessageTransaction();
636 handleMessageInvalidate();
637 signalRefresh();
638 break;
639 case MessageQueue::REFRESH:
640 handleMessageRefresh();
641 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800642 }
643}
644
Mathias Agopian4fec8732012-06-29 14:12:52 -0700645void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700646 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700647 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700648 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700649 }
650}
651
652void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700653 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700654 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700655}
656
657void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700658 ATRACE_CALL();
659 preComposition();
660 rebuildLayerStacks();
661 setUpHWComposer();
662 doDebugFlashRegions();
663 doComposition();
664 postComposition();
665}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700666
Mathias Agopiancd60f992012-08-16 16:28:27 -0700667void SurfaceFlinger::doDebugFlashRegions()
668{
669 // is debugging enabled
670 if (CC_LIKELY(!mDebugRegion))
671 return;
672
673 const bool repaintEverything = mRepaintEverything;
674 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
675 const sp<DisplayDevice>& hw(mDisplays[dpy]);
676 if (hw->canDraw()) {
677 // transform the dirty region into this screen's coordinate space
678 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
679 if (!dirtyRegion.isEmpty()) {
680 // redraw the whole screen
681 doComposeSurfaces(hw, Region(hw->bounds()));
682
683 // and draw the dirty region
684 glDisable(GL_TEXTURE_EXTERNAL_OES);
685 glDisable(GL_TEXTURE_2D);
686 glDisable(GL_BLEND);
687 glColor4f(1, 0, 1, 1);
688 const int32_t height = hw->getHeight();
689 Region::const_iterator it = dirtyRegion.begin();
690 Region::const_iterator const end = dirtyRegion.end();
691 while (it != end) {
692 const Rect& r = *it++;
693 GLfloat vertices[][2] = {
694 { r.left, height - r.top },
695 { r.left, height - r.bottom },
696 { r.right, height - r.bottom },
697 { r.right, height - r.top }
698 };
699 glVertexPointer(2, GL_FLOAT, 0, vertices);
700 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
701 }
702 hw->compositionComplete();
703 // FIXME
704 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
705 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
706 }
707 }
708 }
709 }
710
711 postFramebuffer();
712
713 if (mDebugRegion > 1) {
714 usleep(mDebugRegion * 1000);
715 }
716}
717
718void SurfaceFlinger::preComposition()
719{
720 bool needExtraInvalidate = false;
721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
722 const size_t count = currentLayers.size();
723 for (size_t i=0 ; i<count ; i++) {
724 if (currentLayers[i]->onPreComposition()) {
725 needExtraInvalidate = true;
726 }
727 }
728 if (needExtraInvalidate) {
729 signalLayerUpdate();
730 }
731}
732
733void SurfaceFlinger::postComposition()
734{
735 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
736 const size_t count = currentLayers.size();
737 for (size_t i=0 ; i<count ; i++) {
738 currentLayers[i]->onPostComposition();
739 }
740}
741
742void SurfaceFlinger::rebuildLayerStacks() {
743 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700744 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700745 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700746 mVisibleRegionsDirty = false;
747 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700748 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700749 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700750 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700751 Region opaqueRegion;
752 Region dirtyRegion;
753 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700754 hw->getLayerStack(), dirtyRegion, opaqueRegion);
755 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700756
757 Vector< sp<LayerBase> > layersSortedByZ;
758 const size_t count = currentLayers.size();
759 for (size_t i=0 ; i<count ; i++) {
760 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700761 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700762 if (!currentLayers[i]->visibleRegion.isEmpty()) {
763 layersSortedByZ.add(currentLayers[i]);
764 }
765 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700766 }
Mathias Agopian42977342012-08-05 00:40:46 -0700767 hw->setVisibleLayersSortedByZ(layersSortedByZ);
768 hw->undefinedRegion.set(hw->getBounds());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769 hw->undefinedRegion.subtractSelf(
770 hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700771 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700772 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700774
Mathias Agopiancd60f992012-08-16 16:28:27 -0700775void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700776 HWComposer& hwc(getHwComposer());
777 if (hwc.initCheck() == NO_ERROR) {
778 // build the h/w work list
779 const bool workListsDirty = mHwWorkListDirty;
780 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700781 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700782 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700783 const int32_t id = hw->getHwcDisplayId();
784 if (id >= 0) {
785 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700787 const size_t count = currentLayers.size();
788 if (hwc.createWorkList(id, count) >= 0) {
789 HWComposer::LayerListIterator cur = hwc.begin(id);
790 const HWComposer::LayerListIterator end = hwc.end(id);
791 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
792 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700793
Mathias Agopiane60b0682012-08-21 23:34:09 -0700794 if (CC_UNLIKELY(workListsDirty)) {
795 layer->setGeometry(hw, *cur);
796 if (mDebugDisableHWC || mDebugRegion) {
797 cur->setSkip(true);
798 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700799 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700800
Mathias Agopiane60b0682012-08-21 23:34:09 -0700801 /*
802 * update the per-frame h/w composer data for each layer
803 * and build the transparent region of the FB
804 */
805 layer->setPerFrameData(hw, *cur);
806 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700807 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700809 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700810 status_t err = hwc.prepare();
811 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
812 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700813}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700814
Mathias Agopiancd60f992012-08-16 16:28:27 -0700815void SurfaceFlinger::doComposition() {
816 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700817 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700818 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700819 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700820 if (hw->canDraw()) {
821 // transform the dirty region into this screen's coordinate space
822 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
823 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700824 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700825 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700826 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700827 hw->dirtyRegion.clear();
828 hw->flip(hw->swapRegion);
829 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700830 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700831 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700832 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700833 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700834 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700835}
836
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837void SurfaceFlinger::postFramebuffer()
838{
Mathias Agopian841cde52012-03-01 15:44:37 -0800839 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800840
Mathias Agopiana44b0412011-10-16 18:46:35 -0700841 const nsecs_t now = systemTime();
842 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700843
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700844 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700845 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700846 // FIXME: EGL spec says:
847 // "surface must be bound to the calling thread's current context,
848 // for the current rendering API."
Mathias Agopiancd60f992012-08-16 16:28:27 -0700849 DisplayDevice::makeCurrent(
850 getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700851 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700852 }
853
Mathias Agopian92a979a2012-08-02 18:32:23 -0700854 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700855 sp<const DisplayDevice> hw(mDisplays[dpy]);
856 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700857 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700858 int32_t id = hw->getHwcDisplayId();
859 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700860 HWComposer::LayerListIterator cur = hwc.begin(id);
861 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700862 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700863 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700864 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700865 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700866 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700867 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700868 }
Jesse Hallef194142012-06-14 14:45:17 -0700869 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800870 }
871
Mathias Agopiana44b0412011-10-16 18:46:35 -0700872 mLastSwapBufferTime = systemTime() - now;
873 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874}
875
Mathias Agopian87baae12012-07-31 12:38:26 -0700876void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877{
Mathias Agopian841cde52012-03-01 15:44:37 -0800878 ATRACE_CALL();
879
Mathias Agopianca4d3602011-05-19 15:38:14 -0700880 Mutex::Autolock _l(mStateLock);
881 const nsecs_t now = systemTime();
882 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883
Mathias Agopianca4d3602011-05-19 15:38:14 -0700884 // Here we're guaranteed that some transaction flags are set
885 // so we can call handleTransactionLocked() unconditionally.
886 // We call getTransactionFlags(), which will also clear the flags,
887 // with mStateLock held to guarantee that mCurrentState won't change
888 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700889
Mathias Agopiane57f2922012-08-09 16:29:12 -0700890 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700891 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700892
Mathias Agopianca4d3602011-05-19 15:38:14 -0700893 mLastTransactionTime = systemTime() - now;
894 mDebugInTransaction = 0;
895 invalidateHwcGeometry();
896 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700897}
898
Mathias Agopian87baae12012-07-31 12:38:26 -0700899void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700900{
901 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 const size_t count = currentLayers.size();
903
904 /*
905 * Traversal of the children
906 * (perform the transaction for each of them if needed)
907 */
908
Mathias Agopian3559b072012-08-15 13:46:03 -0700909 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700911 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
913 if (!trFlags) continue;
914
915 const uint32_t flags = layer->doTransaction(0);
916 if (flags & Layer::eVisibleRegion)
917 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 }
919 }
920
921 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700922 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 */
924
Mathias Agopiane57f2922012-08-09 16:29:12 -0700925 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700926 // here we take advantage of Vector's copy-on-write semantics to
927 // improve performance by skipping the transaction entirely when
928 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700929 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
930 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700931 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700933 const size_t cc = curr.size();
934 const size_t dc = draw.size();
935
936 // find the displays that were removed
937 // (ie: in drawing state but not in current state)
938 // also handle displays that changed
939 // (ie: displays that are in both lists)
940 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700941 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
942 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700943 // in drawing state but not in current state
944 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700945 mDisplays.removeItem(draw[i].id);
946 } else {
947 ALOGW("trying to remove the main display");
948 }
949 } else {
950 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700951 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700952 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700953 // changing the surface is like destroying and
954 // recreating the DisplayDevice
Mathias Agopiane60b0682012-08-21 23:34:09 -0700955 const int32_t hwcDisplayId =
956 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
957 state.id : getHwComposer().allocateDisplayId();
Mathias Agopiane57f2922012-08-09 16:29:12 -0700958
959 sp<SurfaceTextureClient> stc(
960 new SurfaceTextureClient(state.surface));
961
962 sp<DisplayDevice> disp = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700963 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700964
965 disp->setLayerStack(state.layerStack);
966 disp->setOrientation(state.orientation);
967 // TODO: take viewport and frame into account
968 mDisplays.replaceValueFor(state.id, disp);
969 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700971 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700972 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700973 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700974 if (state.orientation != draw[i].orientation ||
975 state.viewport != draw[i].viewport ||
976 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700977 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
978 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700979 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700980 }
981 }
982 }
983
984 // find displays that were added
985 // (ie: in current state but not in drawing state)
986 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700987 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700988 const DisplayDeviceState& state(curr[i]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700989 const int32_t hwcDisplayId =
990 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
991 state.id : getHwComposer().allocateDisplayId();
992
Mathias Agopiane57f2922012-08-09 16:29:12 -0700993 sp<SurfaceTextureClient> stc(
994 new SurfaceTextureClient(state.surface));
Mathias Agopiane60b0682012-08-21 23:34:09 -0700995 sp<DisplayDevice> disp = new DisplayDevice(this,
996 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700997 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700998 }
999 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001001 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002
Mathias Agopian3559b072012-08-15 13:46:03 -07001003 /*
1004 * Perform our own transaction if needed
1005 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001006
Mathias Agopiancd60f992012-08-16 16:28:27 -07001007 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1008 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001009 // layers have been added
1010 mVisibleRegionsDirty = true;
1011 }
1012
1013 // some layers might have been removed, so
1014 // we need to update the regions they're exposing.
1015 if (mLayersRemoved) {
1016 mLayersRemoved = false;
1017 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001018 const size_t count = previousLayers.size();
1019 for (size_t i=0 ; i<count ; i++) {
1020 const sp<LayerBase>& layer(previousLayers[i]);
1021 if (currentLayers.indexOf(layer) < 0) {
1022 // this layer is not visible anymore
1023 // TODO: we could traverse the tree from front to back and
1024 // compute the actual visible region
1025 // TODO: we could cache the transformed region
1026 Layer::State front(layer->drawingState());
1027 Region visibleReg = front.transform.transform(
1028 Region(Rect(front.active.w, front.active.h)));
1029 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001030 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 }
1033
1034 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001035}
1036
1037void SurfaceFlinger::commitTransaction()
1038{
1039 if (!mLayersPendingRemoval.isEmpty()) {
1040 // Notify removed layers now that they can't be drawn from
1041 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1042 mLayersPendingRemoval[i]->onRemoved();
1043 }
1044 mLayersPendingRemoval.clear();
1045 }
1046
1047 mDrawingState = mCurrentState;
1048 mTransationPending = false;
1049 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050}
1051
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001053 const LayerVector& currentLayers, uint32_t layerStack,
1054 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055{
Mathias Agopian841cde52012-03-01 15:44:37 -08001056 ATRACE_CALL();
1057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 Region aboveOpaqueLayers;
1059 Region aboveCoveredLayers;
1060 Region dirty;
1061
Mathias Agopian87baae12012-07-31 12:38:26 -07001062 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063
1064 size_t i = currentLayers.size();
1065 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067
1068 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001069 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070
Mathias Agopian87baae12012-07-31 12:38:26 -07001071 // only consider the layers on the given later stack
1072 if (s.layerStack != layerStack)
1073 continue;
1074
Mathias Agopianab028732010-03-16 16:41:46 -07001075 /*
1076 * opaqueRegion: area of a surface that is fully opaque.
1077 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001079
1080 /*
1081 * visibleRegion: area of a surface that is visible on screen
1082 * and not fully transparent. This is essentially the layer's
1083 * footprint minus the opaque regions above it.
1084 * Areas covered by a translucent surface are considered visible.
1085 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001087
1088 /*
1089 * coveredRegion: area of a surface that is covered by all
1090 * visible regions above it (which includes the translucent areas).
1091 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001093
1094
1095 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001096 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001097 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001098 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001100 if (!visibleRegion.isEmpty()) {
1101 // Remove the transparent area from the visible region
1102 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001103 Region transparentRegionScreen;
1104 const Transform tr(s.transform);
1105 if (tr.transformed()) {
1106 if (tr.preserveRects()) {
1107 // transform the transparent region
1108 transparentRegionScreen = tr.transform(s.transparentRegion);
1109 } else {
1110 // transformation too complex, can't do the
1111 // transparent region optimization.
1112 transparentRegionScreen.clear();
1113 }
1114 } else {
1115 transparentRegionScreen = s.transparentRegion;
1116 }
1117 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001118 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119
Mathias Agopianab028732010-03-16 16:41:46 -07001120 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001121 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001122 if (s.alpha==255 && !translucent &&
1123 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1124 // the opaque region is the layer's footprint
1125 opaqueRegion = visibleRegion;
1126 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 }
1128 }
1129
Mathias Agopianab028732010-03-16 16:41:46 -07001130 // Clip the covered region to the visible region
1131 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1132
1133 // Update aboveCoveredLayers for next (lower) layer
1134 aboveCoveredLayers.orSelf(visibleRegion);
1135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 // subtract the opaque region covered by the layers above us
1137 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
1139 // compute this layer's dirty region
1140 if (layer->contentDirty) {
1141 // we need to invalidate the whole region
1142 dirty = visibleRegion;
1143 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001144 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 layer->contentDirty = false;
1146 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001147 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001148 * the exposed region consists of two components:
1149 * 1) what's VISIBLE now and was COVERED before
1150 * 2) what's EXPOSED now less what was EXPOSED before
1151 *
1152 * note that (1) is conservative, we start with the whole
1153 * visible region but only keep what used to be covered by
1154 * something -- which mean it may have been exposed.
1155 *
1156 * (2) handles areas that were not covered by anything but got
1157 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001158 */
Mathias Agopianab028732010-03-16 16:41:46 -07001159 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001160 const Region oldVisibleRegion = layer->visibleRegion;
1161 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001162 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1163 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 }
1165 dirty.subtractSelf(aboveOpaqueLayers);
1166
1167 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001168 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169
Mathias Agopianab028732010-03-16 16:41:46 -07001170 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 // Store the visible region is screen space
1174 layer->setVisibleRegion(visibleRegion);
1175 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 }
1177
Mathias Agopian87baae12012-07-31 12:38:26 -07001178 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179}
1180
Mathias Agopian87baae12012-07-31 12:38:26 -07001181void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1182 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001183 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001184 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1185 if (hw->getLayerStack() == layerStack) {
1186 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001187 }
1188 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001189}
1190
1191void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001193 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194
Mathias Agopian4fec8732012-06-29 14:12:52 -07001195 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001196 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001197 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001198 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001199 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001200 const Region dirty(layer->latchBuffer(visibleRegions));
1201 Layer::State s(layer->drawingState());
1202 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001203 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001204
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001205 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206}
1207
Mathias Agopianad456f92011-01-13 17:53:01 -08001208void SurfaceFlinger::invalidateHwcGeometry()
1209{
1210 mHwWorkListDirty = true;
1211}
1212
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001213
Mathias Agopiancd60f992012-08-16 16:28:27 -07001214void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001215 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216{
Mathias Agopian87baae12012-07-31 12:38:26 -07001217 Region dirtyRegion(inDirtyRegion);
1218
Mathias Agopianb8a55602009-06-26 19:06:36 -07001219 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001220 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221
Mathias Agopian42977342012-08-05 00:40:46 -07001222 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001223 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001224 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1225 // takes a rectangle, we must make sure to update that whole
1226 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001227 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001228 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001229 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001230 // We need to redraw the rectangle that will be updated
1231 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001232 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001233 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001234 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001235 } else {
1236 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001237 dirtyRegion.set(hw->bounds());
1238 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
1240 }
1241
Mathias Agopiancd60f992012-08-16 16:28:27 -07001242 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243
Mathias Agopiancd60f992012-08-16 16:28:27 -07001244 // FIXME: we need to call eglSwapBuffers() on displays that have
1245 // GL composition and only on those.
1246 // however, currently hwc.commit() already does that for the main
1247 // display and never for the other ones
1248 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
1249 // FIXME: EGL spec says:
1250 // "surface must be bound to the calling thread's current context,
1251 // for the current rendering API."
1252 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001253 }
1254
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001255 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001256 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257}
1258
Mathias Agopiancd60f992012-08-16 16:28:27 -07001259void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001260{
Mathias Agopian86303202012-07-24 22:46:10 -07001261 HWComposer& hwc(getHwComposer());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001262 int32_t id = hw->getHwcDisplayId();
Mathias Agopian1e260872012-08-08 18:35:12 -07001263 HWComposer::LayerListIterator cur = hwc.begin(id);
1264 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001265
Mathias Agopiane60b0682012-08-21 23:34:09 -07001266 const bool hasGlesComposition = hwc.hasGlesComposition(id);
1267 const bool hasHwcComposition = hwc.hasHwcComposition(id);
1268 if (cur==end || hasGlesComposition) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001269
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001270 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001271
1272 // set the frame buffer
1273 glMatrixMode(GL_MODELVIEW);
1274 glLoadIdentity();
1275
1276 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07001277 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001278 // when using overlays, we assume a fully transparent framebuffer
1279 // NOTE: we could reduce how much we need to clear, for instance
1280 // remove where there are opaque FB layers. however, on some
1281 // GPUs doing a "clean slate" glClear might be more efficient.
1282 // We'll revisit later if needed.
1283 glClearColor(0, 0, 0, 0);
1284 glClear(GL_COLOR_BUFFER_BIT);
1285 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001286 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001287 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001288 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001289 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001290 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001291 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001292 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001293
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001294 /*
1295 * and then, render the layers targeted at the framebuffer
1296 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001297
Mathias Agopian42977342012-08-05 00:40:46 -07001298 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001299 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001300 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001301 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001302 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001303 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Jesse Halla6b32db2012-07-19 16:44:38 -07001304 if (cur != end) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001305 // we're using h/w composer
1306 if (!clip.isEmpty()) {
1307 if (cur->getCompositionType() == HWC_OVERLAY) {
1308 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
1309 && layer->isOpaque()) {
1310 // never clear the very first layer since we're
1311 // guaranteed the FB is already cleared
1312 layer->clearWithOpenGL(hw, clip);
1313 }
1314 } else {
1315 layer->draw(hw, clip);
1316 }
1317 layer->setAcquireFence(hw, *cur);
1318 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001319 ++cur;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001320 } else {
1321 // we're not using h/w composer
1322 if (!clip.isEmpty()) {
1323 layer->draw(hw, clip);
1324 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001325 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001326 }
1327 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328}
1329
Mathias Agopian87baae12012-07-31 12:38:26 -07001330void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001332 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001333 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001334 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001335 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001336
1337 GLfloat vertices[4][2];
1338 glVertexPointer(2, GL_FLOAT, 0, vertices);
1339 Region::const_iterator it = region.begin();
1340 Region::const_iterator const end = region.end();
1341 while (it != end) {
1342 const Rect& r = *it++;
1343 vertices[0][0] = r.left;
1344 vertices[0][1] = r.top;
1345 vertices[1][0] = r.right;
1346 vertices[1][1] = r.top;
1347 vertices[2][0] = r.right;
1348 vertices[2][1] = r.bottom;
1349 vertices[3][0] = r.left;
1350 vertices[3][1] = r.bottom;
1351 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1352 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353}
1354
Mathias Agopian96f08192010-06-02 23:28:45 -07001355ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1356 const sp<LayerBaseClient>& lbc)
1357{
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001359 size_t name = client->attachLayer(lbc);
1360
Mathias Agopian96f08192010-06-02 23:28:45 -07001361 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001362 Mutex::Autolock _l(mStateLock);
1363 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001364
Mathias Agopian4f113742011-05-03 16:21:41 -07001365 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001366}
1367
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001368status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369{
1370 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001371 status_t err = purgatorizeLayer_l(layer);
1372 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001373 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001374 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375}
1376
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378{
1379 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1380 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001381 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 return NO_ERROR;
1383 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001384 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385}
1386
Mathias Agopian9a112062009-04-17 19:36:26 -07001387status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1388{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001389 // First add the layer to the purgatory list, which makes sure it won't
1390 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001391 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001392 if (err >= 0) {
1393 mLayerPurgatory.add(layerBase);
1394 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001395
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001396 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001397
Mathias Agopian3d579642009-06-04 18:46:21 -07001398 // it's possible that we don't find a layer, because it might
1399 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001400 // from the user because there is a race between Client::destroySurface(),
1401 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001402 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1403}
1404
Mathias Agopiandea20b12011-05-03 17:04:02 -07001405uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1406{
1407 return android_atomic_release_load(&mTransactionFlags);
1408}
1409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1411{
1412 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1413}
1414
Mathias Agopianbb641242010-05-18 17:06:55 -07001415uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416{
1417 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1418 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001419 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 }
1421 return old;
1422}
1423
Mathias Agopian8b33f032012-07-24 20:43:54 -07001424void SurfaceFlinger::setTransactionState(
1425 const Vector<ComposerState>& state,
1426 const Vector<DisplayState>& displays,
1427 uint32_t flags)
1428{
Mathias Agopian698c0872011-06-28 19:09:31 -07001429 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001430 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001431
1432 size_t count = displays.size();
1433 for (size_t i=0 ; i<count ; i++) {
1434 const DisplayState& s(displays[i]);
1435 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001436 }
1437
Mathias Agopiane57f2922012-08-09 16:29:12 -07001438 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001439 for (size_t i=0 ; i<count ; i++) {
1440 const ComposerState& s(state[i]);
1441 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001442 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001443 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001444
Mathias Agopian386aa982011-11-07 21:58:03 -08001445 if (transactionFlags) {
1446 // this triggers the transaction
1447 setTransactionFlags(transactionFlags);
1448
1449 // if this is a synchronous transaction, wait for it to take effect
1450 // before returning.
1451 if (flags & eSynchronous) {
1452 mTransationPending = true;
1453 }
1454 while (mTransationPending) {
1455 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1456 if (CC_UNLIKELY(err != NO_ERROR)) {
1457 // just in case something goes wrong in SF, return to the
1458 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001459 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001460 mTransationPending = false;
1461 break;
1462 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001463 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 }
1465}
1466
Mathias Agopiane57f2922012-08-09 16:29:12 -07001467uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1468{
1469 uint32_t flags = 0;
1470 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1471 if (disp.id >= 0) {
1472 const uint32_t what = s.what;
1473 if (what & DisplayState::eSurfaceChanged) {
1474 if (disp.surface->asBinder() != s.surface->asBinder()) {
1475 disp.surface = s.surface;
1476 flags |= eDisplayTransactionNeeded;
1477 }
1478 }
1479 if (what & DisplayState::eLayerStackChanged) {
1480 if (disp.layerStack != s.layerStack) {
1481 disp.layerStack = s.layerStack;
1482 flags |= eDisplayTransactionNeeded;
1483 }
1484 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001485 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001486 if (disp.orientation != s.orientation) {
1487 disp.orientation = s.orientation;
1488 flags |= eDisplayTransactionNeeded;
1489 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001490 }
1491 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001492 if (disp.frame != s.frame) {
1493 disp.frame = s.frame;
1494 flags |= eDisplayTransactionNeeded;
1495 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001496 }
1497 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001498 if (disp.viewport != s.viewport) {
1499 disp.viewport = s.viewport;
1500 flags |= eDisplayTransactionNeeded;
1501 }
1502 }
1503 }
1504 return flags;
1505}
1506
1507uint32_t SurfaceFlinger::setClientStateLocked(
1508 const sp<Client>& client,
1509 const layer_state_t& s)
1510{
1511 uint32_t flags = 0;
1512 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1513 if (layer != 0) {
1514 const uint32_t what = s.what;
1515 if (what & layer_state_t::ePositionChanged) {
1516 if (layer->setPosition(s.x, s.y))
1517 flags |= eTraversalNeeded;
1518 }
1519 if (what & layer_state_t::eLayerChanged) {
1520 // NOTE: index needs to be calculated before we update the state
1521 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1522 if (layer->setLayer(s.z)) {
1523 mCurrentState.layersSortedByZ.removeAt(idx);
1524 mCurrentState.layersSortedByZ.add(layer);
1525 // we need traversal (state changed)
1526 // AND transaction (list changed)
1527 flags |= eTransactionNeeded|eTraversalNeeded;
1528 }
1529 }
1530 if (what & layer_state_t::eSizeChanged) {
1531 if (layer->setSize(s.w, s.h)) {
1532 flags |= eTraversalNeeded;
1533 }
1534 }
1535 if (what & layer_state_t::eAlphaChanged) {
1536 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1537 flags |= eTraversalNeeded;
1538 }
1539 if (what & layer_state_t::eMatrixChanged) {
1540 if (layer->setMatrix(s.matrix))
1541 flags |= eTraversalNeeded;
1542 }
1543 if (what & layer_state_t::eTransparentRegionChanged) {
1544 if (layer->setTransparentRegionHint(s.transparentRegion))
1545 flags |= eTraversalNeeded;
1546 }
1547 if (what & layer_state_t::eVisibilityChanged) {
1548 if (layer->setFlags(s.flags, s.mask))
1549 flags |= eTraversalNeeded;
1550 }
1551 if (what & layer_state_t::eCropChanged) {
1552 if (layer->setCrop(s.crop))
1553 flags |= eTraversalNeeded;
1554 }
1555 if (what & layer_state_t::eLayerStackChanged) {
1556 // NOTE: index needs to be calculated before we update the state
1557 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1558 if (layer->setLayerStack(s.layerStack)) {
1559 mCurrentState.layersSortedByZ.removeAt(idx);
1560 mCurrentState.layersSortedByZ.add(layer);
1561 // we need traversal (state changed)
1562 // AND transaction (list changed)
1563 flags |= eTransactionNeeded|eTraversalNeeded;
1564 }
1565 }
1566 }
1567 return flags;
1568}
1569
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001570sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001571 ISurfaceComposerClient::surface_data_t* params,
1572 const String8& name,
1573 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1575 uint32_t flags)
1576{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001577 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001578 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001579
1580 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001581 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001582 int(w), int(h));
1583 return surfaceHandle;
1584 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001585
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001586 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001587 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1588 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001589 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001591 case ISurfaceComposerClient::eFXSurfaceBlur:
1592 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001593 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001595 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001596 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001597 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 }
1599
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001600 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001601 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001602 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001603 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001605 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001606 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001607 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001608 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001609 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 }
1611
1612 return surfaceHandle;
1613}
1614
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001615sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001616 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001617 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001618 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619{
1620 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001621 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 case PIXEL_FORMAT_TRANSPARENT:
1623 case PIXEL_FORMAT_TRANSLUCENT:
1624 format = PIXEL_FORMAT_RGBA_8888;
1625 break;
1626 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001627#ifdef NO_RGBX_8888
1628 format = PIXEL_FORMAT_RGB_565;
1629#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001630 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001631#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 break;
1633 }
1634
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001635#ifdef NO_RGBX_8888
1636 if (format == PIXEL_FORMAT_RGBX_8888)
1637 format = PIXEL_FORMAT_RGBA_8888;
1638#endif
1639
Mathias Agopian96f08192010-06-02 23:28:45 -07001640 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001641 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001642 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001643 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 }
1646 return layer;
1647}
1648
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001649sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001650 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001651 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652{
Mathias Agopian96f08192010-06-02 23:28:45 -07001653 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001654 return layer;
1655}
1656
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001657sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001658 const sp<Client>& client, DisplayID display,
1659 uint32_t w, uint32_t h, uint32_t flags)
1660{
1661 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 return layer;
1663}
1664
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001665status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666{
Mathias Agopian9a112062009-04-17 19:36:26 -07001667 /*
1668 * called by the window manager, when a surface should be marked for
1669 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001670 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001671 * The surface is removed from the current and drawing lists, but placed
1672 * in the purgatory queue, so it's not destroyed right-away (we need
1673 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001674 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001675
Mathias Agopian48d819a2009-09-10 19:41:18 -07001676 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001677 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001678 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001679
Mathias Agopian48d819a2009-09-10 19:41:18 -07001680 if (layer != 0) {
1681 err = purgatorizeLayer_l(layer);
1682 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001683 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001684 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001685 }
1686 return err;
1687}
1688
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001689status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001690{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001691 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001692 status_t err = NO_ERROR;
1693 sp<LayerBaseClient> l(layer.promote());
1694 if (l != NULL) {
1695 Mutex::Autolock _l(mStateLock);
1696 err = removeLayer_l(l);
1697 if (err == NAME_NOT_FOUND) {
1698 // The surface wasn't in the current list, which means it was
1699 // removed already, which means it is in the purgatory,
1700 // and need to be removed from there.
1701 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001702 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001703 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001704 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001705 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001706 "error removing layer=%p (%s)", l.get(), strerror(-err));
1707 }
1708 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709}
1710
Mathias Agopianb60314a2012-04-10 22:09:54 -07001711// ---------------------------------------------------------------------------
1712
1713void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001714 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001715 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001716 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001717 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001718 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001719 mVisibleRegionsDirty = true;
1720 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721}
1722
Mathias Agopianb60314a2012-04-10 22:09:54 -07001723void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001724 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001725 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1726 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001727 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001728 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001729 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001730 // from this point on, SF will stop drawing
1731 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732}
1733
Colin Cross8e533062012-06-07 13:17:52 -07001734void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001735 class MessageScreenAcquired : public MessageBase {
1736 SurfaceFlinger* flinger;
1737 public:
1738 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1739 virtual bool handler() {
1740 flinger->onScreenAcquired();
1741 return true;
1742 }
1743 };
1744 sp<MessageBase> msg = new MessageScreenAcquired(this);
1745 postMessageSync(msg);
1746}
1747
Colin Cross8e533062012-06-07 13:17:52 -07001748void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001749 class MessageScreenReleased : public MessageBase {
1750 SurfaceFlinger* flinger;
1751 public:
1752 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1753 virtual bool handler() {
1754 flinger->onScreenReleased();
1755 return true;
1756 }
1757 };
1758 sp<MessageBase> msg = new MessageScreenReleased(this);
1759 postMessageSync(msg);
1760}
1761
1762// ---------------------------------------------------------------------------
1763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1765{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001766 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 char buffer[SIZE];
1768 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001769
1770 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 snprintf(buffer, SIZE, "Permission Denial: "
1772 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1773 IPCThreadState::self()->getCallingPid(),
1774 IPCThreadState::self()->getCallingUid());
1775 result.append(buffer);
1776 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001777 // Try to get the main lock, but don't insist if we can't
1778 // (this would indicate SF is stuck, but we want to be able to
1779 // print something in dumpsys).
1780 int retry = 3;
1781 while (mStateLock.tryLock()<0 && --retry>=0) {
1782 usleep(1000000);
1783 }
1784 const bool locked(retry >= 0);
1785 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001786 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001787 "SurfaceFlinger appears to be unresponsive, "
1788 "dumping anyways (no locks held)\n");
1789 result.append(buffer);
1790 }
1791
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001792 bool dumpAll = true;
1793 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001794 size_t numArgs = args.size();
1795 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001796 if ((index < numArgs) &&
1797 (args[index] == String16("--list"))) {
1798 index++;
1799 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001800 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001801 }
1802
1803 if ((index < numArgs) &&
1804 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001805 index++;
1806 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001807 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001808 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001809
1810 if ((index < numArgs) &&
1811 (args[index] == String16("--latency-clear"))) {
1812 index++;
1813 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001814 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001815 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001817
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001818 if (dumpAll) {
1819 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001820 }
1821
Mathias Agopian9795c422009-08-26 16:36:26 -07001822 if (locked) {
1823 mStateLock.unlock();
1824 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 }
1826 write(fd, result.string(), result.size());
1827 return NO_ERROR;
1828}
1829
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001830void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1831 String8& result, char* buffer, size_t SIZE) const
1832{
1833 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1834 const size_t count = currentLayers.size();
1835 for (size_t i=0 ; i<count ; i++) {
1836 const sp<LayerBase>& layer(currentLayers[i]);
1837 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1838 result.append(buffer);
1839 }
1840}
1841
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001842void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1843 String8& result, char* buffer, size_t SIZE) const
1844{
1845 String8 name;
1846 if (index < args.size()) {
1847 name = String8(args[index]);
1848 index++;
1849 }
1850
1851 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1852 const size_t count = currentLayers.size();
1853 for (size_t i=0 ; i<count ; i++) {
1854 const sp<LayerBase>& layer(currentLayers[i]);
1855 if (name.isEmpty()) {
1856 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1857 result.append(buffer);
1858 }
1859 if (name.isEmpty() || (name == layer->getName())) {
1860 layer->dumpStats(result, buffer, SIZE);
1861 }
1862 }
1863}
1864
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001865void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1866 String8& result, char* buffer, size_t SIZE) const
1867{
1868 String8 name;
1869 if (index < args.size()) {
1870 name = String8(args[index]);
1871 index++;
1872 }
1873
1874 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1875 const size_t count = currentLayers.size();
1876 for (size_t i=0 ; i<count ; i++) {
1877 const sp<LayerBase>& layer(currentLayers[i]);
1878 if (name.isEmpty() || (name == layer->getName())) {
1879 layer->clearStats();
1880 }
1881 }
1882}
1883
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001884void SurfaceFlinger::dumpAllLocked(
1885 String8& result, char* buffer, size_t SIZE) const
1886{
1887 // figure out if we're stuck somewhere
1888 const nsecs_t now = systemTime();
1889 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1890 const nsecs_t inTransaction(mDebugInTransaction);
1891 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1892 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1893
1894 /*
1895 * Dump the visible layer list
1896 */
1897 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1898 const size_t count = currentLayers.size();
1899 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1900 result.append(buffer);
1901 for (size_t i=0 ; i<count ; i++) {
1902 const sp<LayerBase>& layer(currentLayers[i]);
1903 layer->dump(result, buffer, SIZE);
1904 }
1905
1906 /*
1907 * Dump the layers in the purgatory
1908 */
1909
1910 const size_t purgatorySize = mLayerPurgatory.size();
1911 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1912 result.append(buffer);
1913 for (size_t i=0 ; i<purgatorySize ; i++) {
1914 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1915 layer->shortDump(result, buffer, SIZE);
1916 }
1917
1918 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001919 * Dump Display state
1920 */
1921
1922 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1923 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1924 snprintf(buffer, SIZE,
1925 "+ DisplayDevice[%u]\n"
1926 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1927 "flips=%u, secure=%d, numLayers=%u\n",
1928 dpy,
1929 hw->getDisplayId(), hw->getLayerStack(),
1930 hw->getWidth(), hw->getHeight(),
1931 hw->getOrientation(), hw->getTransform().getType(),
1932 hw->getPageFlipCount(),
1933 hw->getSecureLayerVisible(),
1934 hw->getVisibleLayersSortedByZ().size());
1935 result.append(buffer);
1936 }
1937
1938 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001939 * Dump SurfaceFlinger global state
1940 */
1941
1942 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1943 result.append(buffer);
1944
Mathias Agopian888c8222012-08-04 21:10:38 -07001945 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001946 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001947 const GLExtensions& extensions(GLExtensions::getInstance());
1948 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1949 extensions.getVendor(),
1950 extensions.getRenderer(),
1951 extensions.getVersion());
1952 result.append(buffer);
1953
1954 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001955 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001956 result.append(buffer);
1957
1958 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1959 result.append(buffer);
1960
Mathias Agopian42977342012-08-05 00:40:46 -07001961 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001962 snprintf(buffer, SIZE,
1963 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001964 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001965 result.append(buffer);
1966 snprintf(buffer, SIZE,
1967 " last eglSwapBuffers() time: %f us\n"
1968 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001969 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001970 " refresh-rate : %f fps\n"
1971 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001972 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001973 mLastSwapBufferTime/1000.0,
1974 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001975 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001976 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001977 hwc.getDpiX(),
1978 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001979 result.append(buffer);
1980
1981 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1982 inSwapBuffersDuration/1000.0);
1983 result.append(buffer);
1984
1985 snprintf(buffer, SIZE, " transaction time: %f us\n",
1986 inTransactionDuration/1000.0);
1987 result.append(buffer);
1988
1989 /*
1990 * VSYNC state
1991 */
1992 mEventThread->dump(result, buffer, SIZE);
1993
1994 /*
1995 * Dump HWComposer state
1996 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001997 snprintf(buffer, SIZE, "h/w composer state:\n");
1998 result.append(buffer);
1999 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2000 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2001 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2002 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002003 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002004
2005 /*
2006 * Dump gralloc state
2007 */
2008 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2009 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002010 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002011}
2012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013status_t SurfaceFlinger::onTransact(
2014 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2015{
2016 switch (code) {
2017 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002018 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002020 case BLANK:
2021 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002022 {
2023 // codes that require permission check
2024 IPCThreadState* ipc = IPCThreadState::self();
2025 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002026 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002027 if ((uid != AID_GRAPHICS) &&
2028 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002029 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002030 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2031 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002032 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002033 break;
2034 }
2035 case CAPTURE_SCREEN:
2036 {
2037 // codes that require permission check
2038 IPCThreadState* ipc = IPCThreadState::self();
2039 const int pid = ipc->getCallingPid();
2040 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002041 if ((uid != AID_GRAPHICS) &&
2042 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002043 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002044 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2045 return PERMISSION_DENIED;
2046 }
2047 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048 }
2049 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2052 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002053 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002054 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002055 IPCThreadState* ipc = IPCThreadState::self();
2056 const int pid = ipc->getCallingPid();
2057 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002058 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002059 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060 return PERMISSION_DENIED;
2061 }
2062 int n;
2063 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002064 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002065 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 return NO_ERROR;
2067 case 1002: // SHOW_UPDATES
2068 n = data.readInt32();
2069 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002070 invalidateHwcGeometry();
2071 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002073 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002074 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002075 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002077 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002078 setTransactionFlags(
2079 eTransactionNeeded|
2080 eDisplayTransactionNeeded|
2081 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002082 return NO_ERROR;
2083 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002084 case 1006:{ // send empty update
2085 signalRefresh();
2086 return NO_ERROR;
2087 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002088 case 1008: // toggle use of hw composer
2089 n = data.readInt32();
2090 mDebugDisableHWC = n ? 1 : 0;
2091 invalidateHwcGeometry();
2092 repaintEverything();
2093 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002094 case 1009: // toggle use of transform hint
2095 n = data.readInt32();
2096 mDebugDisableTransformHint = n ? 1 : 0;
2097 invalidateHwcGeometry();
2098 repaintEverything();
2099 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002101 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102 reply->writeInt32(0);
2103 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002104 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002105 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002106 return NO_ERROR;
2107 case 1013: {
2108 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002109 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2110 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111 }
2112 return NO_ERROR;
2113 }
2114 }
2115 return err;
2116}
2117
Mathias Agopian53331da2011-08-22 21:44:41 -07002118void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002119 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002120 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002121}
2122
Mathias Agopian59119e62010-10-11 12:37:43 -07002123// ---------------------------------------------------------------------------
2124
Mathias Agopian118d0242011-10-13 16:02:48 -07002125status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2126 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2127{
2128 Mutex::Autolock _l(mStateLock);
2129 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2130}
2131
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002132status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2133 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002134{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002135 ATRACE_CALL();
2136
Mathias Agopian59119e62010-10-11 12:37:43 -07002137 if (!GLExtensions::getInstance().haveFramebufferObject())
2138 return INVALID_OPERATION;
2139
2140 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002141 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2142 const uint32_t hw_w = hw->getWidth();
2143 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002144 GLfloat u = 1;
2145 GLfloat v = 1;
2146
2147 // make sure to clear all GL error flags
2148 while ( glGetError() != GL_NO_ERROR ) ;
2149
2150 // create a FBO
2151 GLuint name, tname;
2152 glGenTextures(1, &tname);
2153 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002154 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2155 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002156 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2157 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002158 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002159 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002160 GLint tw = (2 << (31 - clz(hw_w)));
2161 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2163 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002164 u = GLfloat(hw_w) / tw;
2165 v = GLfloat(hw_h) / th;
2166 }
2167 glGenFramebuffersOES(1, &name);
2168 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2170 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002171
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002172 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002173 glDisable(GL_TEXTURE_EXTERNAL_OES);
2174 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002175 glClearColor(0,0,0,1);
2176 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002177 glMatrixMode(GL_MODELVIEW);
2178 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002179 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002180 const size_t count = layers.size();
2181 for (size_t i=0 ; i<count ; ++i) {
2182 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002183 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002184 }
2185
Mathias Agopian42977342012-08-05 00:40:46 -07002186 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002187
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 // back to main framebuffer
2189 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002190 glDeleteFramebuffersOES(1, &name);
2191
2192 *textureName = tname;
2193 *uOut = u;
2194 *vOut = v;
2195 return NO_ERROR;
2196}
2197
2198// ---------------------------------------------------------------------------
2199
Mathias Agopian74c40c02010-09-29 13:02:36 -07002200status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2201 sp<IMemoryHeap>* heap,
2202 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002203 uint32_t sw, uint32_t sh,
2204 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002205{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002206 ATRACE_CALL();
2207
Mathias Agopian74c40c02010-09-29 13:02:36 -07002208 status_t result = PERMISSION_DENIED;
2209
2210 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002211 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002212 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002213 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002214 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002215
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002216 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002218 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219
2220 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002221 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2222 const uint32_t hw_w = hw->getWidth();
2223 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002224
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002225 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002226 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002227 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002228 return PERMISSION_DENIED;
2229 }
2230
2231 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002232 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002233 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002234 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002235
2236 sw = (!sw) ? hw_w : sw;
2237 sh = (!sh) ? hw_h : sh;
2238 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002239 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002240
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002241// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2242// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002243
Mathias Agopian74c40c02010-09-29 13:02:36 -07002244 // make sure to clear all GL error flags
2245 while ( glGetError() != GL_NO_ERROR ) ;
2246
2247 // create a FBO
2248 GLuint name, tname;
2249 glGenRenderbuffersOES(1, &tname);
2250 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2251 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002252
Mathias Agopian74c40c02010-09-29 13:02:36 -07002253 glGenFramebuffersOES(1, &name);
2254 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2255 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2256 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2257
2258 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002259
Mathias Agopian74c40c02010-09-29 13:02:36 -07002260 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2261
2262 // invert everything, b/c glReadPixel() below will invert the FB
2263 glViewport(0, 0, sw, sh);
2264 glMatrixMode(GL_PROJECTION);
2265 glPushMatrix();
2266 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002267 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002268 glMatrixMode(GL_MODELVIEW);
2269
2270 // redraw the screen entirely...
2271 glClearColor(0,0,0,1);
2272 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002273
Jamie Gennis9575f602011-10-07 14:51:16 -07002274 const LayerVector& layers(mDrawingState.layersSortedByZ);
2275 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002276 for (size_t i=0 ; i<count ; ++i) {
2277 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002278 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002279 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002280 const uint32_t z = layer->drawingState().z;
2281 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002282 if (filtering) layer->setFiltering(true);
2283 layer->draw(hw);
2284 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002285 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002286 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287 }
2288
Mathias Agopian74c40c02010-09-29 13:02:36 -07002289 // check for errors and return screen capture
2290 if (glGetError() != GL_NO_ERROR) {
2291 // error while rendering
2292 result = INVALID_OPERATION;
2293 } else {
2294 // allocate shared memory large enough to hold the
2295 // screen capture
2296 sp<MemoryHeapBase> base(
2297 new MemoryHeapBase(size, 0, "screen-capture") );
2298 void* const ptr = base->getBase();
2299 if (ptr) {
2300 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002301 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2303 if (glGetError() == GL_NO_ERROR) {
2304 *heap = base;
2305 *w = sw;
2306 *h = sh;
2307 *f = PIXEL_FORMAT_RGBA_8888;
2308 result = NO_ERROR;
2309 }
2310 } else {
2311 result = NO_MEMORY;
2312 }
2313 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314 glViewport(0, 0, hw_w, hw_h);
2315 glMatrixMode(GL_PROJECTION);
2316 glPopMatrix();
2317 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002318 } else {
2319 result = BAD_VALUE;
2320 }
2321
2322 // release FBO resources
2323 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2324 glDeleteRenderbuffersOES(1, &tname);
2325 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002326
Mathias Agopian42977342012-08-05 00:40:46 -07002327 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002328
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002329// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002330
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 return result;
2332}
2333
2334
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002335status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2336 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002338 uint32_t sw, uint32_t sh,
2339 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002340{
2341 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002342 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002343 return BAD_VALUE;
2344
2345 if (!GLExtensions::getInstance().haveFramebufferObject())
2346 return INVALID_OPERATION;
2347
2348 class MessageCaptureScreen : public MessageBase {
2349 SurfaceFlinger* flinger;
2350 DisplayID dpy;
2351 sp<IMemoryHeap>* heap;
2352 uint32_t* w;
2353 uint32_t* h;
2354 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355 uint32_t sw;
2356 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002357 uint32_t minLayerZ;
2358 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002359 status_t result;
2360 public:
2361 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002363 uint32_t sw, uint32_t sh,
2364 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002365 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002366 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2367 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2368 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002369 {
2370 }
2371 status_t getResult() const {
2372 return result;
2373 }
2374 virtual bool handler() {
2375 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002377 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002378 return true;
2379 }
2380 };
2381
2382 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002383 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002384 status_t res = postMessageSync(msg);
2385 if (res == NO_ERROR) {
2386 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2387 }
2388 return res;
2389}
2390
2391// ---------------------------------------------------------------------------
2392
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002393SurfaceFlinger::LayerVector::LayerVector() {
2394}
2395
2396SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2397 : SortedVector<sp<LayerBase> >(rhs) {
2398}
2399
2400int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2401 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002402{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002403 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002404 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2405 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002406
2407 uint32_t ls = l->currentState().layerStack;
2408 uint32_t rs = r->currentState().layerStack;
2409 if (ls != rs)
2410 return ls - rs;
2411
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002412 uint32_t lz = l->currentState().z;
2413 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002414 if (lz != rz)
2415 return lz - rz;
2416
2417 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002418}
2419
2420// ---------------------------------------------------------------------------
2421
Mathias Agopiane57f2922012-08-09 16:29:12 -07002422SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2423}
2424
2425SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2426 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002427}
2428
2429// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430
Jamie Gennis9a78c902011-01-12 18:30:40 -08002431GraphicBufferAlloc::GraphicBufferAlloc() {}
2432
2433GraphicBufferAlloc::~GraphicBufferAlloc() {}
2434
2435sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002436 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002437 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2438 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002439 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002440 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002441 if (err == NO_MEMORY) {
2442 GraphicBuffer::dumpAllocationsToSystemLog();
2443 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002444 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002445 "failed (%s), handle=%p",
2446 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002447 return 0;
2448 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002449 return graphicBuffer;
2450}
2451
Jamie Gennis9a78c902011-01-12 18:30:40 -08002452// ---------------------------------------------------------------------------
2453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454}; // namespace android