blob: 382e0134675304c17f3a59d2cea480464c634d1b [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 Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
39#include <gui/SurfaceTextureClient.h>
40
41#include <ui/GraphicBufferAllocator.h>
42#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <utils/String8.h>
45#include <utils/String16.h>
46#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Mathias Agopian921e6ac2012-07-23 23:11:29 -070049#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070053#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070054#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070056#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070059#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopiana4912602012-07-12 14:25:33 -070062#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070063#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070085 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070098 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
113
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 ALOGI_IF(mDebugRegion, "showupdates enabled");
115 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800118void SurfaceFlinger::onFirstRef()
119{
120 mEventQueue.init(this);
121
122 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
123
124 // Wait for the main thread to be done with its initialization
125 mReadyToRunBarrier.wait();
126}
127
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::~SurfaceFlinger()
130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void SurfaceFlinger::binderDied(const wp<IBinder>& who)
137{
138 // the window manager died on us. prepare its eulogy.
139
140 // reset screen orientation
141 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700142 Vector<DisplayState> displays;
143 DisplayState d;
144 d.orientation = eOrientationDefault;
145 displays.add(d);
146 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800147
148 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700149 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150}
151
Mathias Agopian7e27f052010-05-28 14:22:23 -0700152sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopian96f08192010-06-02 23:28:45 -0700154 sp<ISurfaceComposerClient> bclient;
155 sp<Client> client(new Client(this));
156 status_t err = client->initCheck();
157 if (err == NO_ERROR) {
158 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return bclient;
161}
162
Jamie Gennis9a78c902011-01-12 18:30:40 -0800163sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
164{
165 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
166 return gba;
167}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000173 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700175
176 // wait patiently for the window manager death
177 const String16 name("window");
178 sp<IBinder> window(defaultServiceManager()->getService(name));
179 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700180 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700181 }
182
183 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700184 // formerly we would just kill the process, but we now ask it to exit so it
185 // can choose where to stop the animation.
186 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700189void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
190 class MessageDestroyGLTexture : public MessageBase {
191 GLuint texture;
192 public:
193 MessageDestroyGLTexture(GLuint texture)
194 : texture(texture) {
195 }
196 virtual bool handler() {
197 glDeleteTextures(1, &texture);
198 return true;
199 }
200 };
201 postMessageAsync(new MessageDestroyGLTexture(texture));
202}
203
Mathias Agopiana4912602012-07-12 14:25:33 -0700204status_t SurfaceFlinger::selectConfigForPixelFormat(
205 EGLDisplay dpy,
206 EGLint const* attrs,
207 PixelFormat format,
208 EGLConfig* outConfig)
209{
210 EGLConfig config = NULL;
211 EGLint numConfigs = -1, n=0;
212 eglGetConfigs(dpy, NULL, 0, &numConfigs);
213 EGLConfig* const configs = new EGLConfig[numConfigs];
214 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
215 for (int i=0 ; i<n ; i++) {
216 EGLint nativeVisualId = 0;
217 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
218 if (nativeVisualId>0 && format == nativeVisualId) {
219 *outConfig = configs[i];
220 delete [] configs;
221 return NO_ERROR;
222 }
223 }
224 delete [] configs;
225 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
Mathias Agopiana4912602012-07-12 14:25:33 -0700228EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
229 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
230 // it is to be used with WIFI displays
231 EGLConfig config;
232 EGLint dummy;
233 status_t err;
234 EGLint attribs[] = {
235 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
236 EGL_RECORDABLE_ANDROID, EGL_TRUE,
237 EGL_NONE
238 };
239 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
240 if (err) {
241 // maybe we failed because of EGL_RECORDABLE_ANDROID
242 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
243 attribs[2] = EGL_NONE;
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 }
246 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
247 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
248 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
249 }
250 return config;
251}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
Mathias Agopiana4912602012-07-12 14:25:33 -0700253EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
254 // Also create our EGLContext
255 EGLint contextAttributes[] = {
256#ifdef EGL_IMG_context_priority
257#ifdef HAS_CONTEXT_PRIORITY
258#warning "using EGL_IMG_context_priority"
259 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
260#endif
261#endif
262 EGL_NONE, EGL_NONE
263 };
264 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
265 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
266 return ctxt;
267}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268
Mathias Agopiana4912602012-07-12 14:25:33 -0700269void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
270 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
271 if (!result) {
272 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
273 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 }
275
Mathias Agopiana4912602012-07-12 14:25:33 -0700276 GLExtensions& extensions(GLExtensions::getInstance());
277 extensions.initWithGLStrings(
278 glGetString(GL_VENDOR),
279 glGetString(GL_RENDERER),
280 glGetString(GL_VERSION),
281 glGetString(GL_EXTENSIONS),
282 eglQueryString(display, EGL_VENDOR),
283 eglQueryString(display, EGL_VERSION),
284 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700285
Mathias Agopiana4912602012-07-12 14:25:33 -0700286 EGLint w, h;
287 eglQuerySurface(display, surface, EGL_WIDTH, &w);
288 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700289
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
291 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700292
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 glShadeModel(GL_FLAT);
297 glDisable(GL_DITHER);
298 glDisable(GL_CULL_FACE);
299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300 struct pack565 {
301 inline uint16_t operator() (int r, int g, int b) const {
302 return (r<<11)|(g<<5)|b;
303 }
304 } pack565;
305
Jamie Gennis9575f602011-10-07 14:51:16 -0700306 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
307 glGenTextures(1, &mProtectedTexName);
308 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
309 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
310 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
311 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
312 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
313 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
314 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316 glViewport(0, 0, w, h);
317 glMatrixMode(GL_PROJECTION);
318 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700319 // put the origin in the left-bottom corner
320 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 -0800321
Mathias Agopiana4912602012-07-12 14:25:33 -0700322 // print some debugging info
323 EGLint r,g,b,a;
324 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
325 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
326 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
327 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
328 ALOGI("EGL informations:");
329 ALOGI("vendor : %s", extensions.getEglVendor());
330 ALOGI("version : %s", extensions.getEglVersion());
331 ALOGI("extensions: %s", extensions.getEglExtension());
332 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
333 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
334 ALOGI("OpenGL ES informations:");
335 ALOGI("vendor : %s", extensions.getVendor());
336 ALOGI("renderer : %s", extensions.getRenderer());
337 ALOGI("version : %s", extensions.getVersion());
338 ALOGI("extensions: %s", extensions.getExtension());
339 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
340 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
341}
342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343status_t SurfaceFlinger::readyToRun()
344{
345 ALOGI( "SurfaceFlinger's main thread ready to run. "
346 "Initializing graphics H/W...");
347
Mathias Agopiana4912602012-07-12 14:25:33 -0700348 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700349 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
350 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700351
352 // Initialize the main display
353 // create native window to main display
354 sp<FramebufferSurface> anw = FramebufferSurface::create();
355 ANativeWindow* const window = anw.get();
356 if (!window) {
357 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
358 exit(0);
359 }
360
361 // initialize the config and context
362 int format;
363 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700364 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
365 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700366
367 // initialize our main display hardware
Mathias Agopian92a979a2012-08-02 18:32:23 -0700368 mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState());
369 DisplayDevice hw(this, DisplayDevice::DISPLAY_ID_MAIN, anw, mEGLConfig);
370 mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700371
372 // initialize OpenGL ES
Mathias Agopian92a979a2012-08-02 18:32:23 -0700373 EGLSurface surface = hw.getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700374 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800375
Mathias Agopian028508c2012-07-25 21:12:12 -0700376 // start the EventThread
377 mEventThread = new EventThread(this);
378 mEventQueue.setEventThread(mEventThread);
379
Mathias Agopian86303202012-07-24 22:46:10 -0700380 // initialize the H/W composer
381 mHwc = new HWComposer(this,
382 *static_cast<HWComposer::EventHandler *>(this),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700383 hw.getRefreshPeriod());
384
385 // initialize our drawing state
386 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700387
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800389 mReadyToRunBarrier.open();
390
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700391 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700392 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700393
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 return NO_ERROR;
395}
396
Mathias Agopiana67e4182012-06-19 17:26:12 -0700397void SurfaceFlinger::startBootAnim() {
398 // start boot animation
399 property_set("service.bootanim.exit", "0");
400 property_set("ctl.start", "bootanim");
401}
402
Mathias Agopiana4912602012-07-12 14:25:33 -0700403uint32_t SurfaceFlinger::getMaxTextureSize() const {
404 return mMaxTextureSize;
405}
406
407uint32_t SurfaceFlinger::getMaxViewportDims() const {
408 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
409 mMaxViewportDims[0] : mMaxViewportDims[1];
410}
411
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800413
Jamie Gennis582270d2011-08-17 18:19:00 -0700414bool SurfaceFlinger::authenticateSurfaceTexture(
415 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800416 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700417 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800418
419 // Check the visible layer list for the ISurface
420 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
421 size_t count = currentLayers.size();
422 for (size_t i=0 ; i<count ; i++) {
423 const sp<LayerBase>& layer(currentLayers[i]);
424 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700425 if (lbc != NULL) {
426 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
427 if (lbcBinder == surfaceTextureBinder) {
428 return true;
429 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800430 }
431 }
432
433 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700434 // SurfaceTexture gets destroyed before all the clients are done using it,
435 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800436 // will instead fail later on when the client tries to use the surface,
437 // which should be reported as "surface XYZ returned an -ENODEV". The
438 // purgatorized layers are no less authentic than the visible ones, so this
439 // should not cause any harm.
440 size_t purgatorySize = mLayerPurgatory.size();
441 for (size_t i=0 ; i<purgatorySize ; i++) {
442 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
443 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700444 if (lbc != NULL) {
445 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
446 if (lbcBinder == surfaceTextureBinder) {
447 return true;
448 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800449 }
450 }
451
452 return false;
453}
454
Mathias Agopianc666cae2012-07-25 18:56:13 -0700455status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
456 if (uint32_t(dpy) >= 2) {
457 return BAD_INDEX;
458 }
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700459 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopianc666cae2012-07-25 18:56:13 -0700460 return hw.getInfo(info);
461}
462
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800463// ----------------------------------------------------------------------------
464
465sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800466 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700467}
468
Mathias Agopian3094df32012-06-18 18:06:45 -0700469void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700470 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian3094df32012-06-18 18:06:45 -0700471 EGLSurface result = EGL_NO_SURFACE;
472 EGLSurface old_surface = EGL_NO_SURFACE;
473 sp<SurfaceTextureClient> stc;
474
475 if (display != NULL) {
476 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700477 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700478 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700479 ALOGE_IF(result == EGL_NO_SURFACE,
480 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
481 display.get());
482 }
483
484 { // scope for the lock
485 Mutex::Autolock _l(mStateLock);
486 old_surface = mExternalDisplaySurface;
487 mExternalDisplayNativeWindow = stc;
488 mExternalDisplaySurface = result;
489 ALOGD("mExternalDisplaySurface = %p", result);
490 }
491
492 if (old_surface != EGL_NO_SURFACE) {
493 // Note: EGL allows to destroy an object while its current
494 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700495 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700496 }
497}
498
499EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
500 Mutex::Autolock _l(mStateLock);
501 return mExternalDisplaySurface;
502}
503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800505
506void SurfaceFlinger::waitForEvent() {
507 mEventQueue.waitMessage();
508}
509
510void SurfaceFlinger::signalTransaction() {
511 mEventQueue.invalidate();
512}
513
514void SurfaceFlinger::signalLayerUpdate() {
515 mEventQueue.invalidate();
516}
517
518void SurfaceFlinger::signalRefresh() {
519 mEventQueue.refresh();
520}
521
522status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
523 nsecs_t reltime, uint32_t flags) {
524 return mEventQueue.postMessage(msg, reltime);
525}
526
527status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
528 nsecs_t reltime, uint32_t flags) {
529 status_t res = mEventQueue.postMessage(msg, reltime);
530 if (res == NO_ERROR) {
531 msg->wait();
532 }
533 return res;
534}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535
Mathias Agopian4fec8732012-06-29 14:12:52 -0700536bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 return true;
539}
540
Mathias Agopian86303202012-07-24 22:46:10 -0700541void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700542 mEventThread->onVSyncReceived(dpy, timestamp);
543}
544
545void SurfaceFlinger::eventControl(int event, int enabled) {
546 getHwComposer().eventControl(event, enabled);
547}
548
Mathias Agopian4fec8732012-06-29 14:12:52 -0700549void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800550 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800551 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700552 case MessageQueue::INVALIDATE:
553 handleMessageTransaction();
554 handleMessageInvalidate();
555 signalRefresh();
556 break;
557 case MessageQueue::REFRESH:
558 handleMessageRefresh();
559 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800560 }
561}
562
Mathias Agopian4fec8732012-06-29 14:12:52 -0700563void SurfaceFlinger::handleMessageTransaction() {
564 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
565 uint32_t transactionFlags = peekTransactionFlags(mask);
566 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700567 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700568 }
569}
570
571void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700572 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700573}
574
575void SurfaceFlinger::handleMessageRefresh() {
576 handleRefresh();
577
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700578 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700579 mVisibleRegionsDirty = false;
580 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700581
582 /*
583 * rebuild the visible layer list per screen
584 */
585
Mathias Agopian87baae12012-07-31 12:38:26 -0700586 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700587 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
588 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian87baae12012-07-31 12:38:26 -0700589 Region opaqueRegion;
590 Region dirtyRegion;
591 computeVisibleRegions(currentLayers,
592 hw.getLayerStack(), dirtyRegion, opaqueRegion);
593 hw.dirtyRegion.orSelf(dirtyRegion);
594
595 Vector< sp<LayerBase> > layersSortedByZ;
596 const size_t count = currentLayers.size();
597 for (size_t i=0 ; i<count ; i++) {
598 const Layer::State& s(currentLayers[i]->drawingState());
599 if (s.layerStack == hw.getLayerStack()) {
600 if (!currentLayers[i]->visibleRegion.isEmpty()) {
601 layersSortedByZ.add(currentLayers[i]);
602 }
603 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700604 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700605 hw.setVisibleLayersSortedByZ(layersSortedByZ);
606 hw.undefinedRegion.set(hw.getBounds());
607 hw.undefinedRegion.subtractSelf(hw.getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700608 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700609 }
610
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700611 HWComposer& hwc(getHwComposer());
612 if (hwc.initCheck() == NO_ERROR) {
613 // build the h/w work list
614 const bool workListsDirty = mHwWorkListDirty;
615 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700616 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
617 const DisplayDevice& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700618 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
619 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700620
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700621 hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
622
623 HWComposer::LayerListIterator cur = hwc.begin();
624 const HWComposer::LayerListIterator end = hwc.end();
625 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
626 const sp<LayerBase>& layer(currentLayers[i]);
627
628 if (CC_UNLIKELY(workListsDirty)) {
629 layer->setGeometry(hw, *cur);
630 if (mDebugDisableHWC || mDebugRegion) {
631 cur->setSkip(true);
632 }
633 }
634
635 /*
636 * update the per-frame h/w composer data for each layer
637 * and build the transparent region of the FB
638 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700639 layer->setPerFrameData(hw, *cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700640 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700641 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700642 status_t err = hwc.prepare();
643 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
644 }
645
646 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700647 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
648 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian4fec8732012-06-29 14:12:52 -0700649
Mathias Agopian87baae12012-07-31 12:38:26 -0700650 // transform the dirty region into this screen's coordinate space
651 const Transform& planeTransform(hw.getTransform());
652 Region dirtyRegion;
653 if (repaintEverything) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700654 dirtyRegion.set(hw.bounds());
655 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -0700656 dirtyRegion = planeTransform.transform(hw.dirtyRegion);
657 dirtyRegion.andSelf(hw.bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700658 }
659 hw.dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700660
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700661 if (!dirtyRegion.isEmpty()) {
662 if (hw.canDraw()) {
663 // repaint the framebuffer (if needed)
664 handleRepaint(hw, dirtyRegion);
665 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700666 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700667 // inform the h/w that we're done compositing
668 hw.compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700669 }
670
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700671 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700672
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700673
674#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700675 // render to the external display if we have one
676 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
677 if (externalDisplaySurface != EGL_NO_SURFACE) {
678 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
679 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
680 externalDisplaySurface, externalDisplaySurface,
681 eglGetCurrentContext());
682
683 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
684
685 if (success) {
686 // redraw the screen entirely...
687 glDisable(GL_TEXTURE_EXTERNAL_OES);
688 glDisable(GL_TEXTURE_2D);
689 glClearColor(0,0,0,1);
690 glClear(GL_COLOR_BUFFER_BIT);
691 glMatrixMode(GL_MODELVIEW);
692 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700693
Mathias Agopian92a979a2012-08-02 18:32:23 -0700694 DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice()));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700695 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700696 const size_t count = layers.size();
697 for (size_t i=0 ; i<count ; ++i) {
698 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700699 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700 }
701
702 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
703 ALOGE_IF(!success, "external display eglSwapBuffers failed");
704
705 hw.compositionComplete();
706 }
707
708 success = eglMakeCurrent(eglGetCurrentDisplay(),
709 cur, cur, eglGetCurrentContext());
710
711 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
712 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700713#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700714
715}
716
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717void SurfaceFlinger::postFramebuffer()
718{
Mathias Agopian841cde52012-03-01 15:44:37 -0800719 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800720
Mathias Agopiana44b0412011-10-16 18:46:35 -0700721 const nsecs_t now = systemTime();
722 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700723
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700724 HWComposer& hwc(getHwComposer());
725
Mathias Agopian92a979a2012-08-02 18:32:23 -0700726 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
727 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700728 if (hwc.initCheck() == NO_ERROR) {
729 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
730 const size_t count = currentLayers.size();
731 HWComposer::LayerListIterator cur = hwc.begin();
732 const HWComposer::LayerListIterator end = hwc.end();
733 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
734 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700735 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700736 }
737 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700738 hw.flip(hw.swapRegion);
739 hw.swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700740 }
741
Jesse Hallef194142012-06-14 14:45:17 -0700742 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700743 // FIXME: eventually commit() won't take arguments
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700744 hwc.commit(mEGLDisplay, getDefaultDisplayDevice().getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700745 }
746
Mathias Agopian92a979a2012-08-02 18:32:23 -0700747 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
748 const DisplayDevice& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700749 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
750 const size_t count = currentLayers.size();
751 if (hwc.initCheck() == NO_ERROR) {
752 HWComposer::LayerListIterator cur = hwc.begin();
753 const HWComposer::LayerListIterator end = hwc.end();
754 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700755 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700756 }
757 } else {
758 eglSwapBuffers(mEGLDisplay, hw.getEGLSurface());
759 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700760 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700761 }
Jesse Hallef194142012-06-14 14:45:17 -0700762 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700763
764 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800765 }
766
Mathias Agopiana44b0412011-10-16 18:46:35 -0700767 mLastSwapBufferTime = systemTime() - now;
768 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769}
770
Mathias Agopian87baae12012-07-31 12:38:26 -0700771void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772{
Mathias Agopian841cde52012-03-01 15:44:37 -0800773 ATRACE_CALL();
774
Mathias Agopianca4d3602011-05-19 15:38:14 -0700775 Mutex::Autolock _l(mStateLock);
776 const nsecs_t now = systemTime();
777 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778
Mathias Agopianca4d3602011-05-19 15:38:14 -0700779 // Here we're guaranteed that some transaction flags are set
780 // so we can call handleTransactionLocked() unconditionally.
781 // We call getTransactionFlags(), which will also clear the flags,
782 // with mStateLock held to guarantee that mCurrentState won't change
783 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700784
Mathias Agopianca4d3602011-05-19 15:38:14 -0700785 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
786 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700787 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700788
Mathias Agopianca4d3602011-05-19 15:38:14 -0700789 mLastTransactionTime = systemTime() - now;
790 mDebugInTransaction = 0;
791 invalidateHwcGeometry();
792 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700793}
794
Mathias Agopian87baae12012-07-31 12:38:26 -0700795void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700796{
797 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 const size_t count = currentLayers.size();
799
800 /*
801 * Traversal of the children
802 * (perform the transaction for each of them if needed)
803 */
804
805 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
806 if (layersNeedTransaction) {
807 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700808 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
810 if (!trFlags) continue;
811
812 const uint32_t flags = layer->doTransaction(0);
813 if (flags & Layer::eVisibleRegion)
814 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 }
816 }
817
818 /*
819 * Perform our own transaction if needed
820 */
821
822 if (transactionFlags & eTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700823 // here we take advantage of Vector's copy-on-write semantics to
824 // improve performance by skipping the transaction entirely when
825 // know that the lists are identical
826 const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
827 const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
828 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700830 const size_t cc = curr.size();
831 const size_t dc = draw.size();
832
833 // find the displays that were removed
834 // (ie: in drawing state but not in current state)
835 // also handle displays that changed
836 // (ie: displays that are in both lists)
837 for (size_t i=0 ; i<dc ; i++) {
838 if (curr.indexOfKey(draw[i].id) < 0) {
839 // in drawing state but not in current state
840 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
841 mDisplays.editValueFor(draw[i].id).terminate();
842 mDisplays.removeItem(draw[i].id);
843 } else {
844 ALOGW("trying to remove the main display");
845 }
846 } else {
847 // this display is in both lists. see if something changed.
848 const DisplayDeviceState& state(curr[i]);
849 if (state.layerStack != draw[i].layerStack) {
850 DisplayDevice& disp(mDisplays.editValueFor(state.id));
851 //disp.setLayerStack(state.layerStack);
852 }
853 if (curr[i].orientation != draw[i].orientation) {
854 DisplayDevice& disp(mDisplays.editValueFor(state.id));
855 disp.setOrientation(state.orientation);
856 }
857 }
858 }
859
860 // find displays that were added
861 // (ie: in current state but not in drawing state)
862 for (size_t i=0 ; i<cc ; i++) {
863 if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
864 // FIXME: we need to pass the surface here
865 DisplayDevice disp(this, curr[i].id, 0, mEGLConfig);
866 mDisplays.add(curr[i].id, disp);
867 }
868 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869 }
870
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700871 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
872 // layers have been added
873 mVisibleRegionsDirty = true;
874 }
875
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 // some layers might have been removed, so
877 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700878 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700879 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700881 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700882 const size_t count = previousLayers.size();
883 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700884 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700885 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700886 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700887 // TODO: we could traverse the tree from front to back and
888 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700889 // TODO: we could cache the transformed region
890 Layer::State front(layer->drawingState());
891 Region visibleReg = front.transform.transform(
892 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700893 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700894 }
895 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 }
898
899 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700900}
901
902void SurfaceFlinger::commitTransaction()
903{
904 if (!mLayersPendingRemoval.isEmpty()) {
905 // Notify removed layers now that they can't be drawn from
906 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
907 mLayersPendingRemoval[i]->onRemoved();
908 }
909 mLayersPendingRemoval.clear();
910 }
911
912 mDrawingState = mCurrentState;
913 mTransationPending = false;
914 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915}
916
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700918 const LayerVector& currentLayers, uint32_t layerStack,
919 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920{
Mathias Agopian841cde52012-03-01 15:44:37 -0800921 ATRACE_CALL();
922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 Region aboveOpaqueLayers;
924 Region aboveCoveredLayers;
925 Region dirty;
926
Mathias Agopian87baae12012-07-31 12:38:26 -0700927 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928
929 size_t i = currentLayers.size();
930 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700931 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932
933 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700934 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935
Mathias Agopian87baae12012-07-31 12:38:26 -0700936 // only consider the layers on the given later stack
937 if (s.layerStack != layerStack)
938 continue;
939
Mathias Agopianab028732010-03-16 16:41:46 -0700940 /*
941 * opaqueRegion: area of a surface that is fully opaque.
942 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700944
945 /*
946 * visibleRegion: area of a surface that is visible on screen
947 * and not fully transparent. This is essentially the layer's
948 * footprint minus the opaque regions above it.
949 * Areas covered by a translucent surface are considered visible.
950 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700952
953 /*
954 * coveredRegion: area of a surface that is covered by all
955 * visible regions above it (which includes the translucent areas).
956 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700958
959
960 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800961 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700962 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700963 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700965 if (!visibleRegion.isEmpty()) {
966 // Remove the transparent area from the visible region
967 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700968 Region transparentRegionScreen;
969 const Transform tr(s.transform);
970 if (tr.transformed()) {
971 if (tr.preserveRects()) {
972 // transform the transparent region
973 transparentRegionScreen = tr.transform(s.transparentRegion);
974 } else {
975 // transformation too complex, can't do the
976 // transparent region optimization.
977 transparentRegionScreen.clear();
978 }
979 } else {
980 transparentRegionScreen = s.transparentRegion;
981 }
982 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700983 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984
Mathias Agopianab028732010-03-16 16:41:46 -0700985 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700986 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700987 if (s.alpha==255 && !translucent &&
988 ((layerOrientation & Transform::ROT_INVALID) == false)) {
989 // the opaque region is the layer's footprint
990 opaqueRegion = visibleRegion;
991 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 }
993 }
994
Mathias Agopianab028732010-03-16 16:41:46 -0700995 // Clip the covered region to the visible region
996 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
997
998 // Update aboveCoveredLayers for next (lower) layer
999 aboveCoveredLayers.orSelf(visibleRegion);
1000
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 // subtract the opaque region covered by the layers above us
1002 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003
1004 // compute this layer's dirty region
1005 if (layer->contentDirty) {
1006 // we need to invalidate the whole region
1007 dirty = visibleRegion;
1008 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001009 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 layer->contentDirty = false;
1011 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001012 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001013 * the exposed region consists of two components:
1014 * 1) what's VISIBLE now and was COVERED before
1015 * 2) what's EXPOSED now less what was EXPOSED before
1016 *
1017 * note that (1) is conservative, we start with the whole
1018 * visible region but only keep what used to be covered by
1019 * something -- which mean it may have been exposed.
1020 *
1021 * (2) handles areas that were not covered by anything but got
1022 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001023 */
Mathias Agopianab028732010-03-16 16:41:46 -07001024 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001025 const Region oldVisibleRegion = layer->visibleRegion;
1026 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001027 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1028 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 }
1030 dirty.subtractSelf(aboveOpaqueLayers);
1031
1032 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001033 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034
Mathias Agopianab028732010-03-16 16:41:46 -07001035 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 // Store the visible region is screen space
1039 layer->setVisibleRegion(visibleRegion);
1040 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 }
1042
Mathias Agopian87baae12012-07-31 12:38:26 -07001043 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044}
1045
Mathias Agopian87baae12012-07-31 12:38:26 -07001046void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1047 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001048 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1049 DisplayDevice& hw(mDisplays.editValueAt(dpy));
1050 if (hw.getLayerStack() == layerStack) {
1051 hw.dirtyRegion.orSelf(dirty);
1052 }
1053 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001054}
1055
1056void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001058 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001059 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001061 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001062
Mathias Agopian4fec8732012-06-29 14:12:52 -07001063 bool visibleRegions = false;
1064 const size_t count = currentLayers.size();
1065 sp<LayerBase> const* layers = currentLayers.array();
1066 for (size_t i=0 ; i<count ; i++) {
1067 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001068 const Region dirty(layer->latchBuffer(visibleRegions));
1069 Layer::State s(layer->drawingState());
1070 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001071 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001072
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001073 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074}
1075
Mathias Agopianad456f92011-01-13 17:53:01 -08001076void SurfaceFlinger::invalidateHwcGeometry()
1077{
1078 mHwWorkListDirty = true;
1079}
1080
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001081void SurfaceFlinger::handleRefresh()
1082{
1083 bool needInvalidate = false;
1084 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1085 const size_t count = currentLayers.size();
1086 for (size_t i=0 ; i<count ; i++) {
1087 const sp<LayerBase>& layer(currentLayers[i]);
1088 if (layer->onPreComposition()) {
1089 needInvalidate = true;
1090 }
1091 }
1092 if (needInvalidate) {
1093 signalLayerUpdate();
1094 }
1095}
1096
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001097void SurfaceFlinger::handleRepaint(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001098 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099{
Mathias Agopian841cde52012-03-01 15:44:37 -08001100 ATRACE_CALL();
1101
Mathias Agopian87baae12012-07-31 12:38:26 -07001102 Region dirtyRegion(inDirtyRegion);
1103
Mathias Agopianb8a55602009-06-26 19:06:36 -07001104 // compute the invalid region
Mathias Agopian87baae12012-07-31 12:38:26 -07001105 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106
Glenn Kasten99ed2242011-12-15 09:51:17 -08001107 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001108 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 }
1110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 uint32_t flags = hw.getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001112 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001113 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1114 // takes a rectangle, we must make sure to update that whole
1115 // rectangle in that case
Mathias Agopian87baae12012-07-31 12:38:26 -07001116 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001117 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001118 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001119 // We need to redraw the rectangle that will be updated
1120 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001121 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001122 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian87baae12012-07-31 12:38:26 -07001123 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001124 } else {
1125 // we need to redraw everything (the whole screen)
Mathias Agopian87baae12012-07-31 12:38:26 -07001126 dirtyRegion.set(hw.bounds());
1127 hw.swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 }
1129 }
1130
Mathias Agopian87baae12012-07-31 12:38:26 -07001131 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001133 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1134 const size_t count = currentLayers.size();
1135 for (size_t i=0 ; i<count ; i++) {
1136 currentLayers[i]->onPostComposition();
1137 }
1138
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001139 // update the swap region and clear the dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001140 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141}
1142
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001143void SurfaceFlinger::composeSurfaces(const DisplayDevice& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001144{
Mathias Agopian86303202012-07-24 22:46:10 -07001145 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001146 HWComposer::LayerListIterator cur = hwc.begin();
1147 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001148
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001149 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001150 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001151
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001152 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001153
1154 // set the frame buffer
1155 glMatrixMode(GL_MODELVIEW);
1156 glLoadIdentity();
1157
1158 // Never touch the framebuffer if we don't have any framebuffer layers
1159 if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
Mathias Agopianb9494d52012-04-18 02:28:45 -07001160 // when using overlays, we assume a fully transparent framebuffer
1161 // NOTE: we could reduce how much we need to clear, for instance
1162 // remove where there are opaque FB layers. however, on some
1163 // GPUs doing a "clean slate" glClear might be more efficient.
1164 // We'll revisit later if needed.
1165 glClearColor(0, 0, 0, 0);
1166 glClear(GL_COLOR_BUFFER_BIT);
1167 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -07001168 const Region region(hw.undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001169 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001170 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001171 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001172 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001173 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001174 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001175
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001176 /*
1177 * and then, render the layers targeted at the framebuffer
1178 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001179
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001180 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001181 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001182 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001183 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001184 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001185 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001186 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001187 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001188 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001189 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001190 // never clear the very first layer since we're
1191 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001192 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001193 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001194 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001195 continue;
1196 }
1197 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001198 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001199 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001200 if (cur != end) {
1201 ++cur;
1202 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001203 }
1204 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205}
1206
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001207void SurfaceFlinger::debugFlashRegions(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001208 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209{
Mathias Agopian0a917752010-06-14 21:20:00 -07001210 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001211 const int32_t height = hw.getHeight();
Mathias Agopian87baae12012-07-31 12:38:26 -07001212 if (hw.swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001213 return;
1214 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001215
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001216 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1217 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian87baae12012-07-31 12:38:26 -07001218 dirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001219 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001220 }
1221
Mathias Agopianc492e672011-10-18 14:49:27 -07001222 glDisable(GL_TEXTURE_EXTERNAL_OES);
1223 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225
Mathias Agopian0926f502009-05-04 14:17:04 -07001226 static int toggle = 0;
1227 toggle = 1 - toggle;
1228 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001229 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001230 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001231 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001232 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233
Mathias Agopian87baae12012-07-31 12:38:26 -07001234 Region::const_iterator it = dirtyRegion.begin();
1235 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001236 while (it != end) {
1237 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001239 { r.left, height - r.top },
1240 { r.left, height - r.bottom },
1241 { r.right, height - r.bottom },
1242 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 };
1244 glVertexPointer(2, GL_FLOAT, 0, vertices);
1245 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1246 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001247
Mathias Agopian87baae12012-07-31 12:38:26 -07001248 hw.flip(hw.swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249
1250 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001251 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252}
1253
Mathias Agopian87baae12012-07-31 12:38:26 -07001254void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001256 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001257 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001258 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001259 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001260
1261 GLfloat vertices[4][2];
1262 glVertexPointer(2, GL_FLOAT, 0, vertices);
1263 Region::const_iterator it = region.begin();
1264 Region::const_iterator const end = region.end();
1265 while (it != end) {
1266 const Rect& r = *it++;
1267 vertices[0][0] = r.left;
1268 vertices[0][1] = r.top;
1269 vertices[1][0] = r.right;
1270 vertices[1][1] = r.top;
1271 vertices[2][0] = r.right;
1272 vertices[2][1] = r.bottom;
1273 vertices[3][0] = r.left;
1274 vertices[3][1] = r.bottom;
1275 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1276 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277}
1278
Mathias Agopian96f08192010-06-02 23:28:45 -07001279ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1280 const sp<LayerBaseClient>& lbc)
1281{
Mathias Agopian96f08192010-06-02 23:28:45 -07001282 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001283 size_t name = client->attachLayer(lbc);
1284
Mathias Agopian96f08192010-06-02 23:28:45 -07001285 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001286 Mutex::Autolock _l(mStateLock);
1287 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001288
Mathias Agopian4f113742011-05-03 16:21:41 -07001289 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001290}
1291
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001292status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293{
1294 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001295 status_t err = purgatorizeLayer_l(layer);
1296 if (err == NO_ERROR)
1297 setTransactionFlags(eTransactionNeeded);
1298 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299}
1300
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001301status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302{
1303 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1304 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001305 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 return NO_ERROR;
1307 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001308 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309}
1310
Mathias Agopian9a112062009-04-17 19:36:26 -07001311status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1312{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001313 // First add the layer to the purgatory list, which makes sure it won't
1314 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001315 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001316 if (err >= 0) {
1317 mLayerPurgatory.add(layerBase);
1318 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001319
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001320 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001321
Mathias Agopian3d579642009-06-04 18:46:21 -07001322 // it's possible that we don't find a layer, because it might
1323 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001324 // from the user because there is a race between Client::destroySurface(),
1325 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001326 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1327}
1328
Mathias Agopiandea20b12011-05-03 17:04:02 -07001329uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1330{
1331 return android_atomic_release_load(&mTransactionFlags);
1332}
1333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1335{
1336 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1337}
1338
Mathias Agopianbb641242010-05-18 17:06:55 -07001339uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340{
1341 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1342 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001343 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 }
1345 return old;
1346}
1347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348
Mathias Agopian8b33f032012-07-24 20:43:54 -07001349void SurfaceFlinger::setTransactionState(
1350 const Vector<ComposerState>& state,
1351 const Vector<DisplayState>& displays,
1352 uint32_t flags)
1353{
Mathias Agopian698c0872011-06-28 19:09:31 -07001354 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001355
Mathias Agopian8b33f032012-07-24 20:43:54 -07001356 int orientation = eOrientationUnchanged;
1357 if (displays.size()) {
1358 // TODO: handle all displays
1359 orientation = displays[0].orientation;
1360 }
1361
Jamie Gennis28378392011-10-12 17:39:00 -07001362 uint32_t transactionFlags = 0;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001363 // FIXME: don't hardcode display id here
1364 if (mCurrentState.displays.valueFor(0).orientation != orientation) {
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001365 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001366 mCurrentState.displays.editValueFor(0).orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001367 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001368 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001369 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001370 orientation);
1371 }
1372 }
1373
Mathias Agopian698c0872011-06-28 19:09:31 -07001374 const size_t count = state.size();
1375 for (size_t i=0 ; i<count ; i++) {
1376 const ComposerState& s(state[i]);
1377 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001378 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001379 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001380
Mathias Agopian386aa982011-11-07 21:58:03 -08001381 if (transactionFlags) {
1382 // this triggers the transaction
1383 setTransactionFlags(transactionFlags);
1384
1385 // if this is a synchronous transaction, wait for it to take effect
1386 // before returning.
1387 if (flags & eSynchronous) {
1388 mTransationPending = true;
1389 }
1390 while (mTransationPending) {
1391 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1392 if (CC_UNLIKELY(err != NO_ERROR)) {
1393 // just in case something goes wrong in SF, return to the
1394 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001395 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001396 mTransationPending = false;
1397 break;
1398 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001399 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 }
1401}
1402
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001403sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001404 ISurfaceComposerClient::surface_data_t* params,
1405 const String8& name,
1406 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1408 uint32_t flags)
1409{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001410 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001411 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001412
1413 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001414 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001415 int(w), int(h));
1416 return surfaceHandle;
1417 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001418
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001419 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 switch (flags & eFXSurfaceMask) {
1421 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001422 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 break;
1424 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001425 // for now we treat Blur as Dim, until we can implement it
1426 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001428 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001430 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001431 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001432 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 }
1434
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001435 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001436 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001437 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001438 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001440 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001441 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001442 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001443 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001444 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 }
1446
1447 return surfaceHandle;
1448}
1449
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001450sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001451 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001452 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001453 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454{
1455 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001456 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457 case PIXEL_FORMAT_TRANSPARENT:
1458 case PIXEL_FORMAT_TRANSLUCENT:
1459 format = PIXEL_FORMAT_RGBA_8888;
1460 break;
1461 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001462#ifdef NO_RGBX_8888
1463 format = PIXEL_FORMAT_RGB_565;
1464#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001465 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001466#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 break;
1468 }
1469
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001470#ifdef NO_RGBX_8888
1471 if (format == PIXEL_FORMAT_RGBX_8888)
1472 format = PIXEL_FORMAT_RGBA_8888;
1473#endif
1474
Mathias Agopian96f08192010-06-02 23:28:45 -07001475 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001476 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001477 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001478 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001479 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 }
1481 return layer;
1482}
1483
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001484sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001485 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001486 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487{
Mathias Agopian96f08192010-06-02 23:28:45 -07001488 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001489 return layer;
1490}
1491
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001492sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001493 const sp<Client>& client, DisplayID display,
1494 uint32_t w, uint32_t h, uint32_t flags)
1495{
1496 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 return layer;
1498}
1499
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001500status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501{
Mathias Agopian9a112062009-04-17 19:36:26 -07001502 /*
1503 * called by the window manager, when a surface should be marked for
1504 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001505 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001506 * The surface is removed from the current and drawing lists, but placed
1507 * in the purgatory queue, so it's not destroyed right-away (we need
1508 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001509 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001510
Mathias Agopian48d819a2009-09-10 19:41:18 -07001511 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001512 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001513 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001514
Mathias Agopian48d819a2009-09-10 19:41:18 -07001515 if (layer != 0) {
1516 err = purgatorizeLayer_l(layer);
1517 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001518 setTransactionFlags(eTransactionNeeded);
1519 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001520 }
1521 return err;
1522}
1523
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001524status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001525{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001526 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001527 status_t err = NO_ERROR;
1528 sp<LayerBaseClient> l(layer.promote());
1529 if (l != NULL) {
1530 Mutex::Autolock _l(mStateLock);
1531 err = removeLayer_l(l);
1532 if (err == NAME_NOT_FOUND) {
1533 // The surface wasn't in the current list, which means it was
1534 // removed already, which means it is in the purgatory,
1535 // and need to be removed from there.
1536 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001537 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001538 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001539 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001540 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001541 "error removing layer=%p (%s)", l.get(), strerror(-err));
1542 }
1543 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001544}
1545
Mathias Agopian698c0872011-06-28 19:09:31 -07001546uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001547 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001548 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001551 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1552 if (layer != 0) {
1553 const uint32_t what = s.what;
1554 if (what & ePositionChanged) {
1555 if (layer->setPosition(s.x, s.y))
1556 flags |= eTraversalNeeded;
1557 }
1558 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001559 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001560 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1561 if (layer->setLayer(s.z)) {
1562 mCurrentState.layersSortedByZ.removeAt(idx);
1563 mCurrentState.layersSortedByZ.add(layer);
1564 // we need traversal (state changed)
1565 // AND transaction (list changed)
1566 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 }
1568 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001569 if (what & eSizeChanged) {
1570 if (layer->setSize(s.w, s.h)) {
1571 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001572 }
1573 }
1574 if (what & eAlphaChanged) {
1575 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1576 flags |= eTraversalNeeded;
1577 }
1578 if (what & eMatrixChanged) {
1579 if (layer->setMatrix(s.matrix))
1580 flags |= eTraversalNeeded;
1581 }
1582 if (what & eTransparentRegionChanged) {
1583 if (layer->setTransparentRegionHint(s.transparentRegion))
1584 flags |= eTraversalNeeded;
1585 }
1586 if (what & eVisibilityChanged) {
1587 if (layer->setFlags(s.flags, s.mask))
1588 flags |= eTraversalNeeded;
1589 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001590 if (what & eCropChanged) {
1591 if (layer->setCrop(s.crop))
1592 flags |= eTraversalNeeded;
1593 }
Mathias Agopian87855782012-07-24 21:41:09 -07001594 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001595 // NOTE: index needs to be calculated before we update the state
1596 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1597 if (layer->setLayerStack(s.layerStack)) {
1598 mCurrentState.layersSortedByZ.removeAt(idx);
1599 mCurrentState.layersSortedByZ.add(layer);
1600 // we need traversal (state changed)
1601 // AND transaction (list changed)
1602 flags |= eTransactionNeeded|eTraversalNeeded;
1603 }
Mathias Agopian87855782012-07-24 21:41:09 -07001604 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001606 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607}
1608
Mathias Agopianb60314a2012-04-10 22:09:54 -07001609// ---------------------------------------------------------------------------
1610
1611void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001612 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001613 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001614 getHwComposer().acquire();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001615 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001616 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}
1618
Mathias Agopianb60314a2012-04-10 22:09:54 -07001619void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001620 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001621 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopianb60314a2012-04-10 22:09:54 -07001622 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001623 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001624 hw.releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001625 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001626 // from this point on, SF will stop drawing
1627 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628}
1629
Colin Cross8e533062012-06-07 13:17:52 -07001630void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001631 class MessageScreenAcquired : public MessageBase {
1632 SurfaceFlinger* flinger;
1633 public:
1634 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1635 virtual bool handler() {
1636 flinger->onScreenAcquired();
1637 return true;
1638 }
1639 };
1640 sp<MessageBase> msg = new MessageScreenAcquired(this);
1641 postMessageSync(msg);
1642}
1643
Colin Cross8e533062012-06-07 13:17:52 -07001644void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001645 class MessageScreenReleased : public MessageBase {
1646 SurfaceFlinger* flinger;
1647 public:
1648 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1649 virtual bool handler() {
1650 flinger->onScreenReleased();
1651 return true;
1652 }
1653 };
1654 sp<MessageBase> msg = new MessageScreenReleased(this);
1655 postMessageSync(msg);
1656}
1657
1658// ---------------------------------------------------------------------------
1659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1661{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001662 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 char buffer[SIZE];
1664 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001665
1666 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 snprintf(buffer, SIZE, "Permission Denial: "
1668 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1669 IPCThreadState::self()->getCallingPid(),
1670 IPCThreadState::self()->getCallingUid());
1671 result.append(buffer);
1672 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001673 // Try to get the main lock, but don't insist if we can't
1674 // (this would indicate SF is stuck, but we want to be able to
1675 // print something in dumpsys).
1676 int retry = 3;
1677 while (mStateLock.tryLock()<0 && --retry>=0) {
1678 usleep(1000000);
1679 }
1680 const bool locked(retry >= 0);
1681 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001682 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001683 "SurfaceFlinger appears to be unresponsive, "
1684 "dumping anyways (no locks held)\n");
1685 result.append(buffer);
1686 }
1687
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001688 bool dumpAll = true;
1689 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001690 size_t numArgs = args.size();
1691 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001692 if ((index < numArgs) &&
1693 (args[index] == String16("--list"))) {
1694 index++;
1695 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001696 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001697 }
1698
1699 if ((index < numArgs) &&
1700 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001701 index++;
1702 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001703 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001704 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001705
1706 if ((index < numArgs) &&
1707 (args[index] == String16("--latency-clear"))) {
1708 index++;
1709 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001710 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001711 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001713
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001714 if (dumpAll) {
1715 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001716 }
1717
Mathias Agopian9795c422009-08-26 16:36:26 -07001718 if (locked) {
1719 mStateLock.unlock();
1720 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 }
1722 write(fd, result.string(), result.size());
1723 return NO_ERROR;
1724}
1725
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001726void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1727 String8& result, char* buffer, size_t SIZE) const
1728{
1729 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1730 const size_t count = currentLayers.size();
1731 for (size_t i=0 ; i<count ; i++) {
1732 const sp<LayerBase>& layer(currentLayers[i]);
1733 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1734 result.append(buffer);
1735 }
1736}
1737
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001738void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1739 String8& result, char* buffer, size_t SIZE) const
1740{
1741 String8 name;
1742 if (index < args.size()) {
1743 name = String8(args[index]);
1744 index++;
1745 }
1746
1747 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1748 const size_t count = currentLayers.size();
1749 for (size_t i=0 ; i<count ; i++) {
1750 const sp<LayerBase>& layer(currentLayers[i]);
1751 if (name.isEmpty()) {
1752 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1753 result.append(buffer);
1754 }
1755 if (name.isEmpty() || (name == layer->getName())) {
1756 layer->dumpStats(result, buffer, SIZE);
1757 }
1758 }
1759}
1760
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001761void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1762 String8& result, char* buffer, size_t SIZE) const
1763{
1764 String8 name;
1765 if (index < args.size()) {
1766 name = String8(args[index]);
1767 index++;
1768 }
1769
1770 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1771 const size_t count = currentLayers.size();
1772 for (size_t i=0 ; i<count ; i++) {
1773 const sp<LayerBase>& layer(currentLayers[i]);
1774 if (name.isEmpty() || (name == layer->getName())) {
1775 layer->clearStats();
1776 }
1777 }
1778}
1779
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001780void SurfaceFlinger::dumpAllLocked(
1781 String8& result, char* buffer, size_t SIZE) const
1782{
1783 // figure out if we're stuck somewhere
1784 const nsecs_t now = systemTime();
1785 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1786 const nsecs_t inTransaction(mDebugInTransaction);
1787 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1788 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1789
1790 /*
1791 * Dump the visible layer list
1792 */
1793 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1794 const size_t count = currentLayers.size();
1795 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1796 result.append(buffer);
1797 for (size_t i=0 ; i<count ; i++) {
1798 const sp<LayerBase>& layer(currentLayers[i]);
1799 layer->dump(result, buffer, SIZE);
1800 }
1801
1802 /*
1803 * Dump the layers in the purgatory
1804 */
1805
1806 const size_t purgatorySize = mLayerPurgatory.size();
1807 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1808 result.append(buffer);
1809 for (size_t i=0 ; i<purgatorySize ; i++) {
1810 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1811 layer->shortDump(result, buffer, SIZE);
1812 }
1813
1814 /*
1815 * Dump SurfaceFlinger global state
1816 */
1817
1818 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1819 result.append(buffer);
1820
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001821 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001822 const GLExtensions& extensions(GLExtensions::getInstance());
1823 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1824 extensions.getVendor(),
1825 extensions.getRenderer(),
1826 extensions.getVersion());
1827 result.append(buffer);
1828
1829 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001830 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001831 result.append(buffer);
1832
1833 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1834 result.append(buffer);
1835
Mathias Agopian87baae12012-07-31 12:38:26 -07001836 hw.undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001837 snprintf(buffer, SIZE,
1838 " orientation=%d, canDraw=%d\n",
Mathias Agopian92a979a2012-08-02 18:32:23 -07001839 hw.getOrientation(), hw.canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001840 result.append(buffer);
1841 snprintf(buffer, SIZE,
1842 " last eglSwapBuffers() time: %f us\n"
1843 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001844 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001845 " refresh-rate : %f fps\n"
1846 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001847 " y-dpi : %f\n"
1848 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001849 mLastSwapBufferTime/1000.0,
1850 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001851 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001852 hw.getRefreshRate(),
1853 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001854 hw.getDpiY(),
1855 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001856 result.append(buffer);
1857
1858 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1859 inSwapBuffersDuration/1000.0);
1860 result.append(buffer);
1861
1862 snprintf(buffer, SIZE, " transaction time: %f us\n",
1863 inTransactionDuration/1000.0);
1864 result.append(buffer);
1865
1866 /*
1867 * VSYNC state
1868 */
1869 mEventThread->dump(result, buffer, SIZE);
1870
1871 /*
1872 * Dump HWComposer state
1873 */
Mathias Agopian86303202012-07-24 22:46:10 -07001874 HWComposer& hwc(getHwComposer());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001875 snprintf(buffer, SIZE, "h/w composer state:\n");
1876 result.append(buffer);
1877 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1878 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1879 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1880 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001881 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001882
1883 /*
1884 * Dump gralloc state
1885 */
1886 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1887 alloc.dump(result);
1888 hw.dump(result);
1889}
1890
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891status_t SurfaceFlinger::onTransact(
1892 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1893{
1894 switch (code) {
1895 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001896 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001898 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001899 case BLANK:
1900 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001901 {
1902 // codes that require permission check
1903 IPCThreadState* ipc = IPCThreadState::self();
1904 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001905 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001906 if ((uid != AID_GRAPHICS) &&
1907 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001908 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001909 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1910 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001912 break;
1913 }
1914 case CAPTURE_SCREEN:
1915 {
1916 // codes that require permission check
1917 IPCThreadState* ipc = IPCThreadState::self();
1918 const int pid = ipc->getCallingPid();
1919 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001920 if ((uid != AID_GRAPHICS) &&
1921 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001922 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001923 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1924 return PERMISSION_DENIED;
1925 }
1926 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927 }
1928 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1931 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001932 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001933 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001934 IPCThreadState* ipc = IPCThreadState::self();
1935 const int pid = ipc->getCallingPid();
1936 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001937 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001938 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 return PERMISSION_DENIED;
1940 }
1941 int n;
1942 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001943 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001944 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 return NO_ERROR;
1946 case 1002: // SHOW_UPDATES
1947 n = data.readInt32();
1948 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001949 invalidateHwcGeometry();
1950 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001953 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001954 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001956 case 1005:{ // force transaction
1957 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1958 return NO_ERROR;
1959 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001960 case 1006:{ // send empty update
1961 signalRefresh();
1962 return NO_ERROR;
1963 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001964 case 1008: // toggle use of hw composer
1965 n = data.readInt32();
1966 mDebugDisableHWC = n ? 1 : 0;
1967 invalidateHwcGeometry();
1968 repaintEverything();
1969 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001970 case 1009: // toggle use of transform hint
1971 n = data.readInt32();
1972 mDebugDisableTransformHint = n ? 1 : 0;
1973 invalidateHwcGeometry();
1974 repaintEverything();
1975 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001976 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001977 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001978 reply->writeInt32(0);
1979 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001980 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001981 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982 return NO_ERROR;
1983 case 1013: {
1984 Mutex::Autolock _l(mStateLock);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001985 const DisplayDevice& hw(getDefaultDisplayDevice());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986 reply->writeInt32(hw.getPageFlipCount());
1987 }
1988 return NO_ERROR;
1989 }
1990 }
1991 return err;
1992}
1993
Mathias Agopian53331da2011-08-22 21:44:41 -07001994void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07001995 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001996 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001997}
1998
Mathias Agopian59119e62010-10-11 12:37:43 -07001999// ---------------------------------------------------------------------------
2000
Mathias Agopian118d0242011-10-13 16:02:48 -07002001status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2002 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2003{
2004 Mutex::Autolock _l(mStateLock);
2005 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2006}
2007
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002008status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2009 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002010{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002011 ATRACE_CALL();
2012
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 if (!GLExtensions::getInstance().haveFramebufferObject())
2014 return INVALID_OPERATION;
2015
2016 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002017 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002018 const uint32_t hw_w = hw.getWidth();
2019 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002020 GLfloat u = 1;
2021 GLfloat v = 1;
2022
2023 // make sure to clear all GL error flags
2024 while ( glGetError() != GL_NO_ERROR ) ;
2025
2026 // create a FBO
2027 GLuint name, tname;
2028 glGenTextures(1, &tname);
2029 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002030 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2031 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002032 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2033 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002034 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002035 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002036 GLint tw = (2 << (31 - clz(hw_w)));
2037 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2039 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002040 u = GLfloat(hw_w) / tw;
2041 v = GLfloat(hw_h) / th;
2042 }
2043 glGenFramebuffersOES(1, &name);
2044 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2046 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002047
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002048 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002049 glDisable(GL_TEXTURE_EXTERNAL_OES);
2050 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 glClearColor(0,0,0,1);
2052 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002053 glMatrixMode(GL_MODELVIEW);
2054 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002055 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002056 const size_t count = layers.size();
2057 for (size_t i=0 ; i<count ; ++i) {
2058 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002059 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002060 }
2061
Mathias Agopian118d0242011-10-13 16:02:48 -07002062 hw.compositionComplete();
2063
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 // back to main framebuffer
2065 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002066 glDeleteFramebuffersOES(1, &name);
2067
2068 *textureName = tname;
2069 *uOut = u;
2070 *vOut = v;
2071 return NO_ERROR;
2072}
2073
2074// ---------------------------------------------------------------------------
2075
Mathias Agopian74c40c02010-09-29 13:02:36 -07002076status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2077 sp<IMemoryHeap>* heap,
2078 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002079 uint32_t sw, uint32_t sh,
2080 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002081{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002082 ATRACE_CALL();
2083
Mathias Agopian74c40c02010-09-29 13:02:36 -07002084 status_t result = PERMISSION_DENIED;
2085
2086 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002087 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002088 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002089 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002090
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002091 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002092 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002093 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002094
2095 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002096 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002097 const uint32_t hw_w = hw.getWidth();
2098 const uint32_t hw_h = hw.getHeight();
2099
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002100 // if we have secure windows on this display, never allow the screen capture
2101 if (hw.getSecureLayerVisible()) {
2102 return PERMISSION_DENIED;
2103 }
2104
2105 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002106 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002107 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002108
2109 sw = (!sw) ? hw_w : sw;
2110 sh = (!sh) ? hw_h : sh;
2111 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002112 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002113
Steve Block9d453682011-12-20 16:23:08 +00002114 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002115 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002116
Mathias Agopian74c40c02010-09-29 13:02:36 -07002117 // make sure to clear all GL error flags
2118 while ( glGetError() != GL_NO_ERROR ) ;
2119
2120 // create a FBO
2121 GLuint name, tname;
2122 glGenRenderbuffersOES(1, &tname);
2123 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2124 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002125
Mathias Agopian74c40c02010-09-29 13:02:36 -07002126 glGenFramebuffersOES(1, &name);
2127 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2128 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2129 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2130
2131 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002132
Mathias Agopian74c40c02010-09-29 13:02:36 -07002133 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2134
2135 // invert everything, b/c glReadPixel() below will invert the FB
2136 glViewport(0, 0, sw, sh);
2137 glMatrixMode(GL_PROJECTION);
2138 glPushMatrix();
2139 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002140 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002141 glMatrixMode(GL_MODELVIEW);
2142
2143 // redraw the screen entirely...
2144 glClearColor(0,0,0,1);
2145 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002146
Jamie Gennis9575f602011-10-07 14:51:16 -07002147 const LayerVector& layers(mDrawingState.layersSortedByZ);
2148 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002149 for (size_t i=0 ; i<count ; ++i) {
2150 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002151 const uint32_t flags = layer->drawingState().flags;
2152 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2153 const uint32_t z = layer->drawingState().z;
2154 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002155 if (filtering) layer->setFiltering(true);
2156 layer->draw(hw);
2157 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002158 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002159 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002160 }
2161
Mathias Agopian74c40c02010-09-29 13:02:36 -07002162 // check for errors and return screen capture
2163 if (glGetError() != GL_NO_ERROR) {
2164 // error while rendering
2165 result = INVALID_OPERATION;
2166 } else {
2167 // allocate shared memory large enough to hold the
2168 // screen capture
2169 sp<MemoryHeapBase> base(
2170 new MemoryHeapBase(size, 0, "screen-capture") );
2171 void* const ptr = base->getBase();
2172 if (ptr) {
2173 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002174 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002175 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2176 if (glGetError() == GL_NO_ERROR) {
2177 *heap = base;
2178 *w = sw;
2179 *h = sh;
2180 *f = PIXEL_FORMAT_RGBA_8888;
2181 result = NO_ERROR;
2182 }
2183 } else {
2184 result = NO_MEMORY;
2185 }
2186 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002187 glViewport(0, 0, hw_w, hw_h);
2188 glMatrixMode(GL_PROJECTION);
2189 glPopMatrix();
2190 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002191 } else {
2192 result = BAD_VALUE;
2193 }
2194
2195 // release FBO resources
2196 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2197 glDeleteRenderbuffersOES(1, &tname);
2198 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002199
2200 hw.compositionComplete();
2201
Steve Block9d453682011-12-20 16:23:08 +00002202 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002203
Mathias Agopian74c40c02010-09-29 13:02:36 -07002204 return result;
2205}
2206
2207
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002208status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2209 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002210 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002211 uint32_t sw, uint32_t sh,
2212 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002213{
2214 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002215 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002216 return BAD_VALUE;
2217
2218 if (!GLExtensions::getInstance().haveFramebufferObject())
2219 return INVALID_OPERATION;
2220
2221 class MessageCaptureScreen : public MessageBase {
2222 SurfaceFlinger* flinger;
2223 DisplayID dpy;
2224 sp<IMemoryHeap>* heap;
2225 uint32_t* w;
2226 uint32_t* h;
2227 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002228 uint32_t sw;
2229 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002230 uint32_t minLayerZ;
2231 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002232 status_t result;
2233 public:
2234 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002235 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002236 uint32_t sw, uint32_t sh,
2237 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002238 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002239 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2240 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2241 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002242 {
2243 }
2244 status_t getResult() const {
2245 return result;
2246 }
2247 virtual bool handler() {
2248 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002249 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002250 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002251 return true;
2252 }
2253 };
2254
2255 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002256 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002257 status_t res = postMessageSync(msg);
2258 if (res == NO_ERROR) {
2259 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2260 }
2261 return res;
2262}
2263
2264// ---------------------------------------------------------------------------
2265
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002266SurfaceFlinger::LayerVector::LayerVector() {
2267}
2268
2269SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2270 : SortedVector<sp<LayerBase> >(rhs) {
2271}
2272
2273int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2274 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002275{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002276 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002277 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2278 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002279
2280 uint32_t ls = l->currentState().layerStack;
2281 uint32_t rs = r->currentState().layerStack;
2282 if (ls != rs)
2283 return ls - rs;
2284
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002285 uint32_t lz = l->currentState().z;
2286 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002287 if (lz != rz)
2288 return lz - rz;
2289
2290 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002291}
2292
2293// ---------------------------------------------------------------------------
2294
Mathias Agopian92a979a2012-08-02 18:32:23 -07002295SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2296 : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002297}
2298
2299// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002300
Jamie Gennis9a78c902011-01-12 18:30:40 -08002301GraphicBufferAlloc::GraphicBufferAlloc() {}
2302
2303GraphicBufferAlloc::~GraphicBufferAlloc() {}
2304
2305sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002306 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002307 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2308 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002309 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002310 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002311 if (err == NO_MEMORY) {
2312 GraphicBuffer::dumpAllocationsToSystemLog();
2313 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002314 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002315 "failed (%s), handle=%p",
2316 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002317 return 0;
2318 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002319 return graphicBuffer;
2320}
2321
Jamie Gennis9a78c902011-01-12 18:30:40 -08002322// ---------------------------------------------------------------------------
2323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002324}; // namespace android