blob: c64515d2fff2ac6b75a94f7b633194762636e03e [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
Colin Cross3854ed52012-03-23 14:17:18 -0700108#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
112 DdmConnection::start(getServiceName());
113 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700114#else
115#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700116#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117
Steve Blocka19954a2012-01-04 20:05:49 +0000118 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000119 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800122void SurfaceFlinger::onFirstRef()
123{
124 mEventQueue.init(this);
125
126 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
127
128 // Wait for the main thread to be done with its initialization
129 mReadyToRunBarrier.wait();
130}
131
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133SurfaceFlinger::~SurfaceFlinger()
134{
Mathias Agopiana4912602012-07-12 14:25:33 -0700135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
136 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
137 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::binderDied(const wp<IBinder>& who)
141{
142 // the window manager died on us. prepare its eulogy.
143
144 // reset screen orientation
145 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700146 Vector<DisplayState> displays;
147 DisplayState d;
148 d.orientation = eOrientationDefault;
149 displays.add(d);
150 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800151
152 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700153 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800154}
155
Mathias Agopian7e27f052010-05-28 14:22:23 -0700156sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Mathias Agopian96f08192010-06-02 23:28:45 -0700158 sp<ISurfaceComposerClient> bclient;
159 sp<Client> client(new Client(this));
160 status_t err = client->initCheck();
161 if (err == NO_ERROR) {
162 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 return bclient;
165}
166
Jamie Gennis9a78c902011-01-12 18:30:40 -0800167sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
168{
169 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
170 return gba;
171}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173void SurfaceFlinger::bootFinished()
174{
175 const nsecs_t now = systemTime();
176 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000177 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700178 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700179
180 // wait patiently for the window manager death
181 const String16 name("window");
182 sp<IBinder> window(defaultServiceManager()->getService(name));
183 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700184 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700185 }
186
187 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700188 // formerly we would just kill the process, but we now ask it to exit so it
189 // can choose where to stop the animation.
190 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700193void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
194 class MessageDestroyGLTexture : public MessageBase {
195 GLuint texture;
196 public:
197 MessageDestroyGLTexture(GLuint texture)
198 : texture(texture) {
199 }
200 virtual bool handler() {
201 glDeleteTextures(1, &texture);
202 return true;
203 }
204 };
205 postMessageAsync(new MessageDestroyGLTexture(texture));
206}
207
Mathias Agopiana4912602012-07-12 14:25:33 -0700208status_t SurfaceFlinger::selectConfigForPixelFormat(
209 EGLDisplay dpy,
210 EGLint const* attrs,
211 PixelFormat format,
212 EGLConfig* outConfig)
213{
214 EGLConfig config = NULL;
215 EGLint numConfigs = -1, n=0;
216 eglGetConfigs(dpy, NULL, 0, &numConfigs);
217 EGLConfig* const configs = new EGLConfig[numConfigs];
218 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
219 for (int i=0 ; i<n ; i++) {
220 EGLint nativeVisualId = 0;
221 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
222 if (nativeVisualId>0 && format == nativeVisualId) {
223 *outConfig = configs[i];
224 delete [] configs;
225 return NO_ERROR;
226 }
227 }
228 delete [] configs;
229 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230}
231
Mathias Agopiana4912602012-07-12 14:25:33 -0700232EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
233 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
234 // it is to be used with WIFI displays
235 EGLConfig config;
236 EGLint dummy;
237 status_t err;
238 EGLint attribs[] = {
239 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
240 EGL_RECORDABLE_ANDROID, EGL_TRUE,
241 EGL_NONE
242 };
243 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
244 if (err) {
245 // maybe we failed because of EGL_RECORDABLE_ANDROID
246 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
247 attribs[2] = EGL_NONE;
248 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
249 }
250 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
251 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
252 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
253 }
254 return config;
255}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256
Mathias Agopiana4912602012-07-12 14:25:33 -0700257EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
258 // Also create our EGLContext
259 EGLint contextAttributes[] = {
260#ifdef EGL_IMG_context_priority
261#ifdef HAS_CONTEXT_PRIORITY
262#warning "using EGL_IMG_context_priority"
263 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
264#endif
265#endif
266 EGL_NONE, EGL_NONE
267 };
268 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
269 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
270 return ctxt;
271}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272
Mathias Agopiana4912602012-07-12 14:25:33 -0700273void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
274 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
275 if (!result) {
276 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
277 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 }
279
Mathias Agopiana4912602012-07-12 14:25:33 -0700280 GLExtensions& extensions(GLExtensions::getInstance());
281 extensions.initWithGLStrings(
282 glGetString(GL_VENDOR),
283 glGetString(GL_RENDERER),
284 glGetString(GL_VERSION),
285 glGetString(GL_EXTENSIONS),
286 eglQueryString(display, EGL_VENDOR),
287 eglQueryString(display, EGL_VERSION),
288 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700289
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 EGLint w, h;
291 eglQuerySurface(display, surface, EGL_WIDTH, &w);
292 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700293
Mathias Agopiana4912602012-07-12 14:25:33 -0700294 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
295 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700298 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 glShadeModel(GL_FLAT);
301 glDisable(GL_DITHER);
302 glDisable(GL_CULL_FACE);
303
Mathias Agopiana4912602012-07-12 14:25:33 -0700304 struct pack565 {
305 inline uint16_t operator() (int r, int g, int b) const {
306 return (r<<11)|(g<<5)|b;
307 }
308 } pack565;
309
Jamie Gennis9575f602011-10-07 14:51:16 -0700310 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
311 glGenTextures(1, &mProtectedTexName);
312 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
313 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
314 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
315 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
316 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
317 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
318 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319
320 glViewport(0, 0, w, h);
321 glMatrixMode(GL_PROJECTION);
322 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700323 // put the origin in the left-bottom corner
324 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 -0800325
Mathias Agopiana4912602012-07-12 14:25:33 -0700326 // print some debugging info
327 EGLint r,g,b,a;
328 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
329 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
330 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
331 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
332 ALOGI("EGL informations:");
333 ALOGI("vendor : %s", extensions.getEglVendor());
334 ALOGI("version : %s", extensions.getEglVersion());
335 ALOGI("extensions: %s", extensions.getEglExtension());
336 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
337 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
338 ALOGI("OpenGL ES informations:");
339 ALOGI("vendor : %s", extensions.getVendor());
340 ALOGI("renderer : %s", extensions.getRenderer());
341 ALOGI("version : %s", extensions.getVersion());
342 ALOGI("extensions: %s", extensions.getExtension());
343 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
344 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
345}
346
Mathias Agopiana4912602012-07-12 14:25:33 -0700347status_t SurfaceFlinger::readyToRun()
348{
349 ALOGI( "SurfaceFlinger's main thread ready to run. "
350 "Initializing graphics H/W...");
351
Mathias Agopiana4912602012-07-12 14:25:33 -0700352 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700353 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
354 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700355
356 // Initialize the main display
357 // create native window to main display
358 sp<FramebufferSurface> anw = FramebufferSurface::create();
359 ANativeWindow* const window = anw.get();
360 if (!window) {
361 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
362 exit(0);
363 }
364
365 // initialize the config and context
366 int format;
367 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700368 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
369 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700370
371 // initialize our main display hardware
Mathias Agopian92a979a2012-08-02 18:32:23 -0700372 mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState());
373 DisplayDevice hw(this, DisplayDevice::DISPLAY_ID_MAIN, anw, mEGLConfig);
374 mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700375
376 // initialize OpenGL ES
Mathias Agopian92a979a2012-08-02 18:32:23 -0700377 EGLSurface surface = hw.getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700378 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800379
Mathias Agopian028508c2012-07-25 21:12:12 -0700380 // start the EventThread
381 mEventThread = new EventThread(this);
382 mEventQueue.setEventThread(mEventThread);
383
Mathias Agopian86303202012-07-24 22:46:10 -0700384 // initialize the H/W composer
385 mHwc = new HWComposer(this,
386 *static_cast<HWComposer::EventHandler *>(this),
Mathias Agopian92a979a2012-08-02 18:32:23 -0700387 hw.getRefreshPeriod());
388
389 // initialize our drawing state
390 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700391
Mathias Agopiana4912602012-07-12 14:25:33 -0700392 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800393 mReadyToRunBarrier.open();
394
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700395 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700396 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 return NO_ERROR;
399}
400
Mathias Agopiana67e4182012-06-19 17:26:12 -0700401void SurfaceFlinger::startBootAnim() {
402 // start boot animation
403 property_set("service.bootanim.exit", "0");
404 property_set("ctl.start", "bootanim");
405}
406
Mathias Agopiana4912602012-07-12 14:25:33 -0700407uint32_t SurfaceFlinger::getMaxTextureSize() const {
408 return mMaxTextureSize;
409}
410
411uint32_t SurfaceFlinger::getMaxViewportDims() const {
412 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
413 mMaxViewportDims[0] : mMaxViewportDims[1];
414}
415
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800417
Jamie Gennis582270d2011-08-17 18:19:00 -0700418bool SurfaceFlinger::authenticateSurfaceTexture(
419 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800420 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700421 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800422
423 // Check the visible layer list for the ISurface
424 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
425 size_t count = currentLayers.size();
426 for (size_t i=0 ; i<count ; i++) {
427 const sp<LayerBase>& layer(currentLayers[i]);
428 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700429 if (lbc != NULL) {
430 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
431 if (lbcBinder == surfaceTextureBinder) {
432 return true;
433 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800434 }
435 }
436
437 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700438 // SurfaceTexture gets destroyed before all the clients are done using it,
439 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800440 // will instead fail later on when the client tries to use the surface,
441 // which should be reported as "surface XYZ returned an -ENODEV". The
442 // purgatorized layers are no less authentic than the visible ones, so this
443 // should not cause any harm.
444 size_t purgatorySize = mLayerPurgatory.size();
445 for (size_t i=0 ; i<purgatorySize ; i++) {
446 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
447 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700448 if (lbc != NULL) {
449 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
450 if (lbcBinder == surfaceTextureBinder) {
451 return true;
452 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800453 }
454 }
455
456 return false;
457}
458
Mathias Agopianc666cae2012-07-25 18:56:13 -0700459status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
460 if (uint32_t(dpy) >= 2) {
461 return BAD_INDEX;
462 }
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700463 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopianc666cae2012-07-25 18:56:13 -0700464 return hw.getInfo(info);
465}
466
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800467// ----------------------------------------------------------------------------
468
469sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800470 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700471}
472
Mathias Agopian3094df32012-06-18 18:06:45 -0700473void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700474 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian3094df32012-06-18 18:06:45 -0700475 EGLSurface result = EGL_NO_SURFACE;
476 EGLSurface old_surface = EGL_NO_SURFACE;
477 sp<SurfaceTextureClient> stc;
478
479 if (display != NULL) {
480 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700481 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700482 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700483 ALOGE_IF(result == EGL_NO_SURFACE,
484 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
485 display.get());
486 }
487
488 { // scope for the lock
489 Mutex::Autolock _l(mStateLock);
490 old_surface = mExternalDisplaySurface;
491 mExternalDisplayNativeWindow = stc;
492 mExternalDisplaySurface = result;
493 ALOGD("mExternalDisplaySurface = %p", result);
494 }
495
496 if (old_surface != EGL_NO_SURFACE) {
497 // Note: EGL allows to destroy an object while its current
498 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700499 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700500 }
501}
502
503EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
504 Mutex::Autolock _l(mStateLock);
505 return mExternalDisplaySurface;
506}
507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800509
510void SurfaceFlinger::waitForEvent() {
511 mEventQueue.waitMessage();
512}
513
514void SurfaceFlinger::signalTransaction() {
515 mEventQueue.invalidate();
516}
517
518void SurfaceFlinger::signalLayerUpdate() {
519 mEventQueue.invalidate();
520}
521
522void SurfaceFlinger::signalRefresh() {
523 mEventQueue.refresh();
524}
525
526status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
527 nsecs_t reltime, uint32_t flags) {
528 return mEventQueue.postMessage(msg, reltime);
529}
530
531status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
532 nsecs_t reltime, uint32_t flags) {
533 status_t res = mEventQueue.postMessage(msg, reltime);
534 if (res == NO_ERROR) {
535 msg->wait();
536 }
537 return res;
538}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539
Mathias Agopian4fec8732012-06-29 14:12:52 -0700540bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 return true;
543}
544
Mathias Agopian86303202012-07-24 22:46:10 -0700545void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700546 mEventThread->onVSyncReceived(dpy, timestamp);
547}
548
549void SurfaceFlinger::eventControl(int event, int enabled) {
550 getHwComposer().eventControl(event, enabled);
551}
552
Mathias Agopian4fec8732012-06-29 14:12:52 -0700553void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800554 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800555 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700556 case MessageQueue::INVALIDATE:
557 handleMessageTransaction();
558 handleMessageInvalidate();
559 signalRefresh();
560 break;
561 case MessageQueue::REFRESH:
562 handleMessageRefresh();
563 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800564 }
565}
566
Mathias Agopian4fec8732012-06-29 14:12:52 -0700567void SurfaceFlinger::handleMessageTransaction() {
568 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
569 uint32_t transactionFlags = peekTransactionFlags(mask);
570 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700571 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700572 }
573}
574
575void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700576 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700577}
578
579void SurfaceFlinger::handleMessageRefresh() {
580 handleRefresh();
581
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700582 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700583 mVisibleRegionsDirty = false;
584 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700585
586 /*
587 * rebuild the visible layer list per screen
588 */
589
Mathias Agopian87baae12012-07-31 12:38:26 -0700590 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700591 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
592 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian87baae12012-07-31 12:38:26 -0700593 Region opaqueRegion;
594 Region dirtyRegion;
595 computeVisibleRegions(currentLayers,
596 hw.getLayerStack(), dirtyRegion, opaqueRegion);
597 hw.dirtyRegion.orSelf(dirtyRegion);
598
599 Vector< sp<LayerBase> > layersSortedByZ;
600 const size_t count = currentLayers.size();
601 for (size_t i=0 ; i<count ; i++) {
602 const Layer::State& s(currentLayers[i]->drawingState());
603 if (s.layerStack == hw.getLayerStack()) {
604 if (!currentLayers[i]->visibleRegion.isEmpty()) {
605 layersSortedByZ.add(currentLayers[i]);
606 }
607 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700608 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700609 hw.setVisibleLayersSortedByZ(layersSortedByZ);
610 hw.undefinedRegion.set(hw.getBounds());
611 hw.undefinedRegion.subtractSelf(hw.getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700612 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700613 }
614
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700615 HWComposer& hwc(getHwComposer());
616 if (hwc.initCheck() == NO_ERROR) {
617 // build the h/w work list
618 const bool workListsDirty = mHwWorkListDirty;
619 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700620 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
621 const DisplayDevice& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700622 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
623 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700624
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700625 hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
626
627 HWComposer::LayerListIterator cur = hwc.begin();
628 const HWComposer::LayerListIterator end = hwc.end();
629 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
630 const sp<LayerBase>& layer(currentLayers[i]);
631
632 if (CC_UNLIKELY(workListsDirty)) {
633 layer->setGeometry(hw, *cur);
634 if (mDebugDisableHWC || mDebugRegion) {
635 cur->setSkip(true);
636 }
637 }
638
639 /*
640 * update the per-frame h/w composer data for each layer
641 * and build the transparent region of the FB
642 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700643 layer->setPerFrameData(hw, *cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700644 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700645 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700646 status_t err = hwc.prepare();
647 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
648 }
649
650 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700651 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
652 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653
Mathias Agopian87baae12012-07-31 12:38:26 -0700654 // transform the dirty region into this screen's coordinate space
655 const Transform& planeTransform(hw.getTransform());
656 Region dirtyRegion;
657 if (repaintEverything) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700658 dirtyRegion.set(hw.bounds());
659 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -0700660 dirtyRegion = planeTransform.transform(hw.dirtyRegion);
661 dirtyRegion.andSelf(hw.bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700662 }
663 hw.dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700664
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700665 if (!dirtyRegion.isEmpty()) {
666 if (hw.canDraw()) {
667 // repaint the framebuffer (if needed)
668 handleRepaint(hw, dirtyRegion);
669 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700670 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700671 // inform the h/w that we're done compositing
672 hw.compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700673 }
674
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700675 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700676
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700677
678#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679 // render to the external display if we have one
680 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
681 if (externalDisplaySurface != EGL_NO_SURFACE) {
682 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
683 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
684 externalDisplaySurface, externalDisplaySurface,
685 eglGetCurrentContext());
686
687 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
688
689 if (success) {
690 // redraw the screen entirely...
691 glDisable(GL_TEXTURE_EXTERNAL_OES);
692 glDisable(GL_TEXTURE_2D);
693 glClearColor(0,0,0,1);
694 glClear(GL_COLOR_BUFFER_BIT);
695 glMatrixMode(GL_MODELVIEW);
696 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700697
Mathias Agopian92a979a2012-08-02 18:32:23 -0700698 DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice()));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700699 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700 const size_t count = layers.size();
701 for (size_t i=0 ; i<count ; ++i) {
702 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700703 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700704 }
705
706 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
707 ALOGE_IF(!success, "external display eglSwapBuffers failed");
708
709 hw.compositionComplete();
710 }
711
712 success = eglMakeCurrent(eglGetCurrentDisplay(),
713 cur, cur, eglGetCurrentContext());
714
715 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
716 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700717#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700718
719}
720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721void SurfaceFlinger::postFramebuffer()
722{
Mathias Agopian841cde52012-03-01 15:44:37 -0800723 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800724
Mathias Agopiana44b0412011-10-16 18:46:35 -0700725 const nsecs_t now = systemTime();
726 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700727
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700728 HWComposer& hwc(getHwComposer());
729
Mathias Agopian92a979a2012-08-02 18:32:23 -0700730 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
731 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700732 if (hwc.initCheck() == NO_ERROR) {
733 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
734 const size_t count = currentLayers.size();
735 HWComposer::LayerListIterator cur = hwc.begin();
736 const HWComposer::LayerListIterator end = hwc.end();
737 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
738 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700739 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700740 }
741 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700742 hw.flip(hw.swapRegion);
743 hw.swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700744 }
745
Jesse Hallef194142012-06-14 14:45:17 -0700746 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700747 // FIXME: eventually commit() won't take arguments
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700748 hwc.commit(mEGLDisplay, getDefaultDisplayDevice().getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700749 }
750
Mathias Agopian92a979a2012-08-02 18:32:23 -0700751 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
752 const DisplayDevice& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700753 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
754 const size_t count = currentLayers.size();
755 if (hwc.initCheck() == NO_ERROR) {
756 HWComposer::LayerListIterator cur = hwc.begin();
757 const HWComposer::LayerListIterator end = hwc.end();
758 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700759 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700760 }
761 } else {
762 eglSwapBuffers(mEGLDisplay, hw.getEGLSurface());
763 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700764 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700765 }
Jesse Hallef194142012-06-14 14:45:17 -0700766 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700767
768 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800769 }
770
Mathias Agopiana44b0412011-10-16 18:46:35 -0700771 mLastSwapBufferTime = systemTime() - now;
772 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773}
774
Mathias Agopian87baae12012-07-31 12:38:26 -0700775void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776{
Mathias Agopian841cde52012-03-01 15:44:37 -0800777 ATRACE_CALL();
778
Mathias Agopianca4d3602011-05-19 15:38:14 -0700779 Mutex::Autolock _l(mStateLock);
780 const nsecs_t now = systemTime();
781 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782
Mathias Agopianca4d3602011-05-19 15:38:14 -0700783 // Here we're guaranteed that some transaction flags are set
784 // so we can call handleTransactionLocked() unconditionally.
785 // We call getTransactionFlags(), which will also clear the flags,
786 // with mStateLock held to guarantee that mCurrentState won't change
787 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700788
Mathias Agopianca4d3602011-05-19 15:38:14 -0700789 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
790 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700791 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700792
Mathias Agopianca4d3602011-05-19 15:38:14 -0700793 mLastTransactionTime = systemTime() - now;
794 mDebugInTransaction = 0;
795 invalidateHwcGeometry();
796 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700797}
798
Mathias Agopian87baae12012-07-31 12:38:26 -0700799void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700800{
801 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 const size_t count = currentLayers.size();
803
804 /*
805 * Traversal of the children
806 * (perform the transaction for each of them if needed)
807 */
808
809 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
810 if (layersNeedTransaction) {
811 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700812 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
814 if (!trFlags) continue;
815
816 const uint32_t flags = layer->doTransaction(0);
817 if (flags & Layer::eVisibleRegion)
818 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 }
820 }
821
822 /*
823 * Perform our own transaction if needed
824 */
825
826 if (transactionFlags & eTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700827 // here we take advantage of Vector's copy-on-write semantics to
828 // improve performance by skipping the transaction entirely when
829 // know that the lists are identical
830 const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
831 const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
832 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700834 const size_t cc = curr.size();
835 const size_t dc = draw.size();
836
837 // find the displays that were removed
838 // (ie: in drawing state but not in current state)
839 // also handle displays that changed
840 // (ie: displays that are in both lists)
841 for (size_t i=0 ; i<dc ; i++) {
842 if (curr.indexOfKey(draw[i].id) < 0) {
843 // in drawing state but not in current state
844 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
845 mDisplays.editValueFor(draw[i].id).terminate();
846 mDisplays.removeItem(draw[i].id);
847 } else {
848 ALOGW("trying to remove the main display");
849 }
850 } else {
851 // this display is in both lists. see if something changed.
852 const DisplayDeviceState& state(curr[i]);
853 if (state.layerStack != draw[i].layerStack) {
854 DisplayDevice& disp(mDisplays.editValueFor(state.id));
855 //disp.setLayerStack(state.layerStack);
856 }
857 if (curr[i].orientation != draw[i].orientation) {
858 DisplayDevice& disp(mDisplays.editValueFor(state.id));
859 disp.setOrientation(state.orientation);
860 }
861 }
862 }
863
864 // find displays that were added
865 // (ie: in current state but not in drawing state)
866 for (size_t i=0 ; i<cc ; i++) {
867 if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
868 // FIXME: we need to pass the surface here
869 DisplayDevice disp(this, curr[i].id, 0, mEGLConfig);
870 mDisplays.add(curr[i].id, disp);
871 }
872 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 }
874
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700875 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
876 // layers have been added
877 mVisibleRegionsDirty = true;
878 }
879
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 // some layers might have been removed, so
881 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700882 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700883 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700885 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700886 const size_t count = previousLayers.size();
887 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700888 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700889 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700890 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700891 // TODO: we could traverse the tree from front to back and
892 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700893 // TODO: we could cache the transformed region
894 Layer::State front(layer->drawingState());
895 Region visibleReg = front.transform.transform(
896 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700897 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700898 }
899 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 }
902
903 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700904}
905
906void SurfaceFlinger::commitTransaction()
907{
908 if (!mLayersPendingRemoval.isEmpty()) {
909 // Notify removed layers now that they can't be drawn from
910 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
911 mLayersPendingRemoval[i]->onRemoved();
912 }
913 mLayersPendingRemoval.clear();
914 }
915
916 mDrawingState = mCurrentState;
917 mTransationPending = false;
918 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919}
920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700922 const LayerVector& currentLayers, uint32_t layerStack,
923 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924{
Mathias Agopian841cde52012-03-01 15:44:37 -0800925 ATRACE_CALL();
926
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 Region aboveOpaqueLayers;
928 Region aboveCoveredLayers;
929 Region dirty;
930
Mathias Agopian87baae12012-07-31 12:38:26 -0700931 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932
933 size_t i = currentLayers.size();
934 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700935 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936
937 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700938 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939
Mathias Agopian87baae12012-07-31 12:38:26 -0700940 // only consider the layers on the given later stack
941 if (s.layerStack != layerStack)
942 continue;
943
Mathias Agopianab028732010-03-16 16:41:46 -0700944 /*
945 * opaqueRegion: area of a surface that is fully opaque.
946 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700948
949 /*
950 * visibleRegion: area of a surface that is visible on screen
951 * and not fully transparent. This is essentially the layer's
952 * footprint minus the opaque regions above it.
953 * Areas covered by a translucent surface are considered visible.
954 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700956
957 /*
958 * coveredRegion: area of a surface that is covered by all
959 * visible regions above it (which includes the translucent areas).
960 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700962
963
964 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800965 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700966 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700967 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700969 if (!visibleRegion.isEmpty()) {
970 // Remove the transparent area from the visible region
971 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700972 Region transparentRegionScreen;
973 const Transform tr(s.transform);
974 if (tr.transformed()) {
975 if (tr.preserveRects()) {
976 // transform the transparent region
977 transparentRegionScreen = tr.transform(s.transparentRegion);
978 } else {
979 // transformation too complex, can't do the
980 // transparent region optimization.
981 transparentRegionScreen.clear();
982 }
983 } else {
984 transparentRegionScreen = s.transparentRegion;
985 }
986 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700987 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988
Mathias Agopianab028732010-03-16 16:41:46 -0700989 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700990 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700991 if (s.alpha==255 && !translucent &&
992 ((layerOrientation & Transform::ROT_INVALID) == false)) {
993 // the opaque region is the layer's footprint
994 opaqueRegion = visibleRegion;
995 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 }
997 }
998
Mathias Agopianab028732010-03-16 16:41:46 -0700999 // Clip the covered region to the visible region
1000 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1001
1002 // Update aboveCoveredLayers for next (lower) layer
1003 aboveCoveredLayers.orSelf(visibleRegion);
1004
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 // subtract the opaque region covered by the layers above us
1006 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007
1008 // compute this layer's dirty region
1009 if (layer->contentDirty) {
1010 // we need to invalidate the whole region
1011 dirty = visibleRegion;
1012 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001013 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 layer->contentDirty = false;
1015 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001016 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001017 * the exposed region consists of two components:
1018 * 1) what's VISIBLE now and was COVERED before
1019 * 2) what's EXPOSED now less what was EXPOSED before
1020 *
1021 * note that (1) is conservative, we start with the whole
1022 * visible region but only keep what used to be covered by
1023 * something -- which mean it may have been exposed.
1024 *
1025 * (2) handles areas that were not covered by anything but got
1026 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001027 */
Mathias Agopianab028732010-03-16 16:41:46 -07001028 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001029 const Region oldVisibleRegion = layer->visibleRegion;
1030 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001031 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1032 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 }
1034 dirty.subtractSelf(aboveOpaqueLayers);
1035
1036 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001037 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038
Mathias Agopianab028732010-03-16 16:41:46 -07001039 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 // Store the visible region is screen space
1043 layer->setVisibleRegion(visibleRegion);
1044 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 }
1046
Mathias Agopian87baae12012-07-31 12:38:26 -07001047 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048}
1049
Mathias Agopian87baae12012-07-31 12:38:26 -07001050void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1051 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001052 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1053 DisplayDevice& hw(mDisplays.editValueAt(dpy));
1054 if (hw.getLayerStack() == layerStack) {
1055 hw.dirtyRegion.orSelf(dirty);
1056 }
1057 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001058}
1059
1060void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001062 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001063 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001065 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001066
Mathias Agopian4fec8732012-06-29 14:12:52 -07001067 bool visibleRegions = false;
1068 const size_t count = currentLayers.size();
1069 sp<LayerBase> const* layers = currentLayers.array();
1070 for (size_t i=0 ; i<count ; i++) {
1071 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001072 const Region dirty(layer->latchBuffer(visibleRegions));
1073 Layer::State s(layer->drawingState());
1074 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001075 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001076
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001077 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078}
1079
Mathias Agopianad456f92011-01-13 17:53:01 -08001080void SurfaceFlinger::invalidateHwcGeometry()
1081{
1082 mHwWorkListDirty = true;
1083}
1084
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001085void SurfaceFlinger::handleRefresh()
1086{
1087 bool needInvalidate = false;
1088 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1089 const size_t count = currentLayers.size();
1090 for (size_t i=0 ; i<count ; i++) {
1091 const sp<LayerBase>& layer(currentLayers[i]);
1092 if (layer->onPreComposition()) {
1093 needInvalidate = true;
1094 }
1095 }
1096 if (needInvalidate) {
1097 signalLayerUpdate();
1098 }
1099}
1100
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001101void SurfaceFlinger::handleRepaint(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001102 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103{
Mathias Agopian841cde52012-03-01 15:44:37 -08001104 ATRACE_CALL();
1105
Mathias Agopian87baae12012-07-31 12:38:26 -07001106 Region dirtyRegion(inDirtyRegion);
1107
Mathias Agopianb8a55602009-06-26 19:06:36 -07001108 // compute the invalid region
Mathias Agopian87baae12012-07-31 12:38:26 -07001109 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110
Glenn Kasten99ed2242011-12-15 09:51:17 -08001111 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001112 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 }
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 uint32_t flags = hw.getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001116 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001117 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1118 // takes a rectangle, we must make sure to update that whole
1119 // rectangle in that case
Mathias Agopian87baae12012-07-31 12:38:26 -07001120 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001121 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001122 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001123 // We need to redraw the rectangle that will be updated
1124 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001125 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001126 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian87baae12012-07-31 12:38:26 -07001127 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001128 } else {
1129 // we need to redraw everything (the whole screen)
Mathias Agopian87baae12012-07-31 12:38:26 -07001130 dirtyRegion.set(hw.bounds());
1131 hw.swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 }
1133 }
1134
Mathias Agopian87baae12012-07-31 12:38:26 -07001135 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001137 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1138 const size_t count = currentLayers.size();
1139 for (size_t i=0 ; i<count ; i++) {
1140 currentLayers[i]->onPostComposition();
1141 }
1142
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001143 // update the swap region and clear the dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001144 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145}
1146
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001147void SurfaceFlinger::composeSurfaces(const DisplayDevice& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001148{
Mathias Agopian86303202012-07-24 22:46:10 -07001149 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001150 HWComposer::LayerListIterator cur = hwc.begin();
1151 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001152
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001153 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001154 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001155
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001156 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001157
1158 // set the frame buffer
1159 glMatrixMode(GL_MODELVIEW);
1160 glLoadIdentity();
1161
1162 // Never touch the framebuffer if we don't have any framebuffer layers
1163 if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
Mathias Agopianb9494d52012-04-18 02:28:45 -07001164 // when using overlays, we assume a fully transparent framebuffer
1165 // NOTE: we could reduce how much we need to clear, for instance
1166 // remove where there are opaque FB layers. however, on some
1167 // GPUs doing a "clean slate" glClear might be more efficient.
1168 // We'll revisit later if needed.
1169 glClearColor(0, 0, 0, 0);
1170 glClear(GL_COLOR_BUFFER_BIT);
1171 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -07001172 const Region region(hw.undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001173 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001174 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001175 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001176 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001177 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001178 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001179
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001180 /*
1181 * and then, render the layers targeted at the framebuffer
1182 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001183
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001184 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001185 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001186 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001187 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001188 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001189 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001190 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001191 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001192 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001193 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001194 // never clear the very first layer since we're
1195 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001196 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001197 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001198 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001199 continue;
1200 }
1201 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001202 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001203 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001204 if (cur != end) {
1205 ++cur;
1206 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001207 }
1208 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209}
1210
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001211void SurfaceFlinger::debugFlashRegions(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001212 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213{
Mathias Agopian0a917752010-06-14 21:20:00 -07001214 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001215 const int32_t height = hw.getHeight();
Mathias Agopian87baae12012-07-31 12:38:26 -07001216 if (hw.swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001217 return;
1218 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001219
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001220 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1221 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian87baae12012-07-31 12:38:26 -07001222 dirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001223 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001224 }
1225
Mathias Agopianc492e672011-10-18 14:49:27 -07001226 glDisable(GL_TEXTURE_EXTERNAL_OES);
1227 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229
Mathias Agopian0926f502009-05-04 14:17:04 -07001230 static int toggle = 0;
1231 toggle = 1 - toggle;
1232 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001233 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001234 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001235 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001236 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237
Mathias Agopian87baae12012-07-31 12:38:26 -07001238 Region::const_iterator it = dirtyRegion.begin();
1239 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001240 while (it != end) {
1241 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001243 { r.left, height - r.top },
1244 { r.left, height - r.bottom },
1245 { r.right, height - r.bottom },
1246 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 };
1248 glVertexPointer(2, GL_FLOAT, 0, vertices);
1249 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1250 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001251
Mathias Agopian87baae12012-07-31 12:38:26 -07001252 hw.flip(hw.swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253
1254 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001255 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256}
1257
Mathias Agopian87baae12012-07-31 12:38:26 -07001258void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001260 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001261 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001262 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001263 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001264
1265 GLfloat vertices[4][2];
1266 glVertexPointer(2, GL_FLOAT, 0, vertices);
1267 Region::const_iterator it = region.begin();
1268 Region::const_iterator const end = region.end();
1269 while (it != end) {
1270 const Rect& r = *it++;
1271 vertices[0][0] = r.left;
1272 vertices[0][1] = r.top;
1273 vertices[1][0] = r.right;
1274 vertices[1][1] = r.top;
1275 vertices[2][0] = r.right;
1276 vertices[2][1] = r.bottom;
1277 vertices[3][0] = r.left;
1278 vertices[3][1] = r.bottom;
1279 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1280 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281}
1282
Mathias Agopian96f08192010-06-02 23:28:45 -07001283ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1284 const sp<LayerBaseClient>& lbc)
1285{
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001287 size_t name = client->attachLayer(lbc);
1288
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001290 Mutex::Autolock _l(mStateLock);
1291 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001292
Mathias Agopian4f113742011-05-03 16:21:41 -07001293 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001294}
1295
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001296status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297{
1298 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001299 status_t err = purgatorizeLayer_l(layer);
1300 if (err == NO_ERROR)
1301 setTransactionFlags(eTransactionNeeded);
1302 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303}
1304
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001305status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
1307 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1308 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001309 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310 return NO_ERROR;
1311 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001312 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313}
1314
Mathias Agopian9a112062009-04-17 19:36:26 -07001315status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1316{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001317 // First add the layer to the purgatory list, which makes sure it won't
1318 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001319 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001320 if (err >= 0) {
1321 mLayerPurgatory.add(layerBase);
1322 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001323
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001324 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001325
Mathias Agopian3d579642009-06-04 18:46:21 -07001326 // it's possible that we don't find a layer, because it might
1327 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001328 // from the user because there is a race between Client::destroySurface(),
1329 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001330 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1331}
1332
Mathias Agopiandea20b12011-05-03 17:04:02 -07001333uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1334{
1335 return android_atomic_release_load(&mTransactionFlags);
1336}
1337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1339{
1340 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1341}
1342
Mathias Agopianbb641242010-05-18 17:06:55 -07001343uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344{
1345 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1346 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001347 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 }
1349 return old;
1350}
1351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352
Mathias Agopian8b33f032012-07-24 20:43:54 -07001353void SurfaceFlinger::setTransactionState(
1354 const Vector<ComposerState>& state,
1355 const Vector<DisplayState>& displays,
1356 uint32_t flags)
1357{
Mathias Agopian698c0872011-06-28 19:09:31 -07001358 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001359
Mathias Agopian8b33f032012-07-24 20:43:54 -07001360 int orientation = eOrientationUnchanged;
1361 if (displays.size()) {
1362 // TODO: handle all displays
1363 orientation = displays[0].orientation;
1364 }
1365
Jamie Gennis28378392011-10-12 17:39:00 -07001366 uint32_t transactionFlags = 0;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001367 // FIXME: don't hardcode display id here
1368 if (mCurrentState.displays.valueFor(0).orientation != orientation) {
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001369 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001370 mCurrentState.displays.editValueFor(0).orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001371 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001372 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001373 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001374 orientation);
1375 }
1376 }
1377
Mathias Agopian698c0872011-06-28 19:09:31 -07001378 const size_t count = state.size();
1379 for (size_t i=0 ; i<count ; i++) {
1380 const ComposerState& s(state[i]);
1381 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001382 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001383 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001384
Mathias Agopian386aa982011-11-07 21:58:03 -08001385 if (transactionFlags) {
1386 // this triggers the transaction
1387 setTransactionFlags(transactionFlags);
1388
1389 // if this is a synchronous transaction, wait for it to take effect
1390 // before returning.
1391 if (flags & eSynchronous) {
1392 mTransationPending = true;
1393 }
1394 while (mTransationPending) {
1395 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1396 if (CC_UNLIKELY(err != NO_ERROR)) {
1397 // just in case something goes wrong in SF, return to the
1398 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001399 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001400 mTransationPending = false;
1401 break;
1402 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001403 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404 }
1405}
1406
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001407sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001408 ISurfaceComposerClient::surface_data_t* params,
1409 const String8& name,
1410 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1412 uint32_t flags)
1413{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001414 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001415 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001416
1417 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001418 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001419 int(w), int(h));
1420 return surfaceHandle;
1421 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001422
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001423 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 switch (flags & eFXSurfaceMask) {
1425 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001426 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 break;
1428 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001429 // for now we treat Blur as Dim, until we can implement it
1430 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001432 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001434 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001435 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001436 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 }
1438
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001439 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001440 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001441 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001442 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001444 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001445 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001446 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001447 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001448 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 }
1450
1451 return surfaceHandle;
1452}
1453
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001454sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001455 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001456 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001457 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458{
1459 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001460 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 case PIXEL_FORMAT_TRANSPARENT:
1462 case PIXEL_FORMAT_TRANSLUCENT:
1463 format = PIXEL_FORMAT_RGBA_8888;
1464 break;
1465 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001466#ifdef NO_RGBX_8888
1467 format = PIXEL_FORMAT_RGB_565;
1468#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001469 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001470#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 break;
1472 }
1473
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001474#ifdef NO_RGBX_8888
1475 if (format == PIXEL_FORMAT_RGBX_8888)
1476 format = PIXEL_FORMAT_RGBA_8888;
1477#endif
1478
Mathias Agopian96f08192010-06-02 23:28:45 -07001479 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001480 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001481 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001482 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001483 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 }
1485 return layer;
1486}
1487
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001488sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001489 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001490 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491{
Mathias Agopian96f08192010-06-02 23:28:45 -07001492 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001493 return layer;
1494}
1495
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001496sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001497 const sp<Client>& client, DisplayID display,
1498 uint32_t w, uint32_t h, uint32_t flags)
1499{
1500 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 return layer;
1502}
1503
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001504status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505{
Mathias Agopian9a112062009-04-17 19:36:26 -07001506 /*
1507 * called by the window manager, when a surface should be marked for
1508 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001509 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001510 * The surface is removed from the current and drawing lists, but placed
1511 * in the purgatory queue, so it's not destroyed right-away (we need
1512 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001513 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001514
Mathias Agopian48d819a2009-09-10 19:41:18 -07001515 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001516 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001517 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001518
Mathias Agopian48d819a2009-09-10 19:41:18 -07001519 if (layer != 0) {
1520 err = purgatorizeLayer_l(layer);
1521 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001522 setTransactionFlags(eTransactionNeeded);
1523 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001524 }
1525 return err;
1526}
1527
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001528status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001529{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001530 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001531 status_t err = NO_ERROR;
1532 sp<LayerBaseClient> l(layer.promote());
1533 if (l != NULL) {
1534 Mutex::Autolock _l(mStateLock);
1535 err = removeLayer_l(l);
1536 if (err == NAME_NOT_FOUND) {
1537 // The surface wasn't in the current list, which means it was
1538 // removed already, which means it is in the purgatory,
1539 // and need to be removed from there.
1540 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001541 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001542 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001543 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001544 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001545 "error removing layer=%p (%s)", l.get(), strerror(-err));
1546 }
1547 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548}
1549
Mathias Agopian698c0872011-06-28 19:09:31 -07001550uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001551 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001552 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001555 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1556 if (layer != 0) {
1557 const uint32_t what = s.what;
1558 if (what & ePositionChanged) {
1559 if (layer->setPosition(s.x, s.y))
1560 flags |= eTraversalNeeded;
1561 }
1562 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001563 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001564 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1565 if (layer->setLayer(s.z)) {
1566 mCurrentState.layersSortedByZ.removeAt(idx);
1567 mCurrentState.layersSortedByZ.add(layer);
1568 // we need traversal (state changed)
1569 // AND transaction (list changed)
1570 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 }
1572 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001573 if (what & eSizeChanged) {
1574 if (layer->setSize(s.w, s.h)) {
1575 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001576 }
1577 }
1578 if (what & eAlphaChanged) {
1579 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1580 flags |= eTraversalNeeded;
1581 }
1582 if (what & eMatrixChanged) {
1583 if (layer->setMatrix(s.matrix))
1584 flags |= eTraversalNeeded;
1585 }
1586 if (what & eTransparentRegionChanged) {
1587 if (layer->setTransparentRegionHint(s.transparentRegion))
1588 flags |= eTraversalNeeded;
1589 }
1590 if (what & eVisibilityChanged) {
1591 if (layer->setFlags(s.flags, s.mask))
1592 flags |= eTraversalNeeded;
1593 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001594 if (what & eCropChanged) {
1595 if (layer->setCrop(s.crop))
1596 flags |= eTraversalNeeded;
1597 }
Mathias Agopian87855782012-07-24 21:41:09 -07001598 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001599 // NOTE: index needs to be calculated before we update the state
1600 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1601 if (layer->setLayerStack(s.layerStack)) {
1602 mCurrentState.layersSortedByZ.removeAt(idx);
1603 mCurrentState.layersSortedByZ.add(layer);
1604 // we need traversal (state changed)
1605 // AND transaction (list changed)
1606 flags |= eTransactionNeeded|eTraversalNeeded;
1607 }
Mathias Agopian87855782012-07-24 21:41:09 -07001608 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001610 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611}
1612
Mathias Agopianb60314a2012-04-10 22:09:54 -07001613// ---------------------------------------------------------------------------
1614
1615void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001616 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001617 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001618 getHwComposer().acquire();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001619 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001620 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
1622
Mathias Agopianb60314a2012-04-10 22:09:54 -07001623void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001624 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001625 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopianb60314a2012-04-10 22:09:54 -07001626 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001627 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001628 hw.releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001629 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001630 // from this point on, SF will stop drawing
1631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632}
1633
Colin Cross8e533062012-06-07 13:17:52 -07001634void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001635 class MessageScreenAcquired : public MessageBase {
1636 SurfaceFlinger* flinger;
1637 public:
1638 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1639 virtual bool handler() {
1640 flinger->onScreenAcquired();
1641 return true;
1642 }
1643 };
1644 sp<MessageBase> msg = new MessageScreenAcquired(this);
1645 postMessageSync(msg);
1646}
1647
Colin Cross8e533062012-06-07 13:17:52 -07001648void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001649 class MessageScreenReleased : public MessageBase {
1650 SurfaceFlinger* flinger;
1651 public:
1652 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1653 virtual bool handler() {
1654 flinger->onScreenReleased();
1655 return true;
1656 }
1657 };
1658 sp<MessageBase> msg = new MessageScreenReleased(this);
1659 postMessageSync(msg);
1660}
1661
1662// ---------------------------------------------------------------------------
1663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1665{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001666 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 char buffer[SIZE];
1668 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001669
1670 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 snprintf(buffer, SIZE, "Permission Denial: "
1672 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1673 IPCThreadState::self()->getCallingPid(),
1674 IPCThreadState::self()->getCallingUid());
1675 result.append(buffer);
1676 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001677 // Try to get the main lock, but don't insist if we can't
1678 // (this would indicate SF is stuck, but we want to be able to
1679 // print something in dumpsys).
1680 int retry = 3;
1681 while (mStateLock.tryLock()<0 && --retry>=0) {
1682 usleep(1000000);
1683 }
1684 const bool locked(retry >= 0);
1685 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001686 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001687 "SurfaceFlinger appears to be unresponsive, "
1688 "dumping anyways (no locks held)\n");
1689 result.append(buffer);
1690 }
1691
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001692 bool dumpAll = true;
1693 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001694 size_t numArgs = args.size();
1695 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001696 if ((index < numArgs) &&
1697 (args[index] == String16("--list"))) {
1698 index++;
1699 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001700 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001701 }
1702
1703 if ((index < numArgs) &&
1704 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001705 index++;
1706 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001707 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001708 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001709
1710 if ((index < numArgs) &&
1711 (args[index] == String16("--latency-clear"))) {
1712 index++;
1713 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001714 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001715 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001717
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001718 if (dumpAll) {
1719 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001720 }
1721
Mathias Agopian9795c422009-08-26 16:36:26 -07001722 if (locked) {
1723 mStateLock.unlock();
1724 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 }
1726 write(fd, result.string(), result.size());
1727 return NO_ERROR;
1728}
1729
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001730void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1731 String8& result, char* buffer, size_t SIZE) const
1732{
1733 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1734 const size_t count = currentLayers.size();
1735 for (size_t i=0 ; i<count ; i++) {
1736 const sp<LayerBase>& layer(currentLayers[i]);
1737 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1738 result.append(buffer);
1739 }
1740}
1741
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001742void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1743 String8& result, char* buffer, size_t SIZE) const
1744{
1745 String8 name;
1746 if (index < args.size()) {
1747 name = String8(args[index]);
1748 index++;
1749 }
1750
1751 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1752 const size_t count = currentLayers.size();
1753 for (size_t i=0 ; i<count ; i++) {
1754 const sp<LayerBase>& layer(currentLayers[i]);
1755 if (name.isEmpty()) {
1756 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1757 result.append(buffer);
1758 }
1759 if (name.isEmpty() || (name == layer->getName())) {
1760 layer->dumpStats(result, buffer, SIZE);
1761 }
1762 }
1763}
1764
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001765void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1766 String8& result, char* buffer, size_t SIZE) const
1767{
1768 String8 name;
1769 if (index < args.size()) {
1770 name = String8(args[index]);
1771 index++;
1772 }
1773
1774 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1775 const size_t count = currentLayers.size();
1776 for (size_t i=0 ; i<count ; i++) {
1777 const sp<LayerBase>& layer(currentLayers[i]);
1778 if (name.isEmpty() || (name == layer->getName())) {
1779 layer->clearStats();
1780 }
1781 }
1782}
1783
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001784void SurfaceFlinger::dumpAllLocked(
1785 String8& result, char* buffer, size_t SIZE) const
1786{
1787 // figure out if we're stuck somewhere
1788 const nsecs_t now = systemTime();
1789 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1790 const nsecs_t inTransaction(mDebugInTransaction);
1791 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1792 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1793
1794 /*
1795 * Dump the visible layer list
1796 */
1797 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1798 const size_t count = currentLayers.size();
1799 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1800 result.append(buffer);
1801 for (size_t i=0 ; i<count ; i++) {
1802 const sp<LayerBase>& layer(currentLayers[i]);
1803 layer->dump(result, buffer, SIZE);
1804 }
1805
1806 /*
1807 * Dump the layers in the purgatory
1808 */
1809
1810 const size_t purgatorySize = mLayerPurgatory.size();
1811 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1812 result.append(buffer);
1813 for (size_t i=0 ; i<purgatorySize ; i++) {
1814 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1815 layer->shortDump(result, buffer, SIZE);
1816 }
1817
1818 /*
1819 * Dump SurfaceFlinger global state
1820 */
1821
1822 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1823 result.append(buffer);
1824
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001825 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001826 const GLExtensions& extensions(GLExtensions::getInstance());
1827 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1828 extensions.getVendor(),
1829 extensions.getRenderer(),
1830 extensions.getVersion());
1831 result.append(buffer);
1832
1833 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001834 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001835 result.append(buffer);
1836
1837 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1838 result.append(buffer);
1839
Mathias Agopian87baae12012-07-31 12:38:26 -07001840 hw.undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001841 snprintf(buffer, SIZE,
1842 " orientation=%d, canDraw=%d\n",
Mathias Agopian92a979a2012-08-02 18:32:23 -07001843 hw.getOrientation(), hw.canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001844 result.append(buffer);
1845 snprintf(buffer, SIZE,
1846 " last eglSwapBuffers() time: %f us\n"
1847 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001848 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001849 " refresh-rate : %f fps\n"
1850 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001851 " y-dpi : %f\n"
1852 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001853 mLastSwapBufferTime/1000.0,
1854 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001855 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001856 hw.getRefreshRate(),
1857 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001858 hw.getDpiY(),
1859 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001860 result.append(buffer);
1861
1862 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1863 inSwapBuffersDuration/1000.0);
1864 result.append(buffer);
1865
1866 snprintf(buffer, SIZE, " transaction time: %f us\n",
1867 inTransactionDuration/1000.0);
1868 result.append(buffer);
1869
1870 /*
1871 * VSYNC state
1872 */
1873 mEventThread->dump(result, buffer, SIZE);
1874
1875 /*
1876 * Dump HWComposer state
1877 */
Mathias Agopian86303202012-07-24 22:46:10 -07001878 HWComposer& hwc(getHwComposer());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001879 snprintf(buffer, SIZE, "h/w composer state:\n");
1880 result.append(buffer);
1881 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1882 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1883 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1884 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001885 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001886
1887 /*
1888 * Dump gralloc state
1889 */
1890 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1891 alloc.dump(result);
1892 hw.dump(result);
1893}
1894
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895status_t SurfaceFlinger::onTransact(
1896 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1897{
1898 switch (code) {
1899 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001900 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001901 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001903 case BLANK:
1904 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 {
1906 // codes that require permission check
1907 IPCThreadState* ipc = IPCThreadState::self();
1908 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001909 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001910 if ((uid != AID_GRAPHICS) &&
1911 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001912 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001913 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1914 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001916 break;
1917 }
1918 case CAPTURE_SCREEN:
1919 {
1920 // codes that require permission check
1921 IPCThreadState* ipc = IPCThreadState::self();
1922 const int pid = ipc->getCallingPid();
1923 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001924 if ((uid != AID_GRAPHICS) &&
1925 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001926 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001927 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1928 return PERMISSION_DENIED;
1929 }
1930 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931 }
1932 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001933
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1935 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001936 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001937 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001938 IPCThreadState* ipc = IPCThreadState::self();
1939 const int pid = ipc->getCallingPid();
1940 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001941 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001942 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 return PERMISSION_DENIED;
1944 }
1945 int n;
1946 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001947 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001948 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949 return NO_ERROR;
1950 case 1002: // SHOW_UPDATES
1951 n = data.readInt32();
1952 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001953 invalidateHwcGeometry();
1954 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001957 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001958 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001960 case 1005:{ // force transaction
1961 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1962 return NO_ERROR;
1963 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001964 case 1006:{ // send empty update
1965 signalRefresh();
1966 return NO_ERROR;
1967 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001968 case 1008: // toggle use of hw composer
1969 n = data.readInt32();
1970 mDebugDisableHWC = n ? 1 : 0;
1971 invalidateHwcGeometry();
1972 repaintEverything();
1973 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001974 case 1009: // toggle use of transform hint
1975 n = data.readInt32();
1976 mDebugDisableTransformHint = n ? 1 : 0;
1977 invalidateHwcGeometry();
1978 repaintEverything();
1979 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001980 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001981 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982 reply->writeInt32(0);
1983 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001984 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001985 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986 return NO_ERROR;
1987 case 1013: {
1988 Mutex::Autolock _l(mStateLock);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001989 const DisplayDevice& hw(getDefaultDisplayDevice());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001990 reply->writeInt32(hw.getPageFlipCount());
1991 }
1992 return NO_ERROR;
1993 }
1994 }
1995 return err;
1996}
1997
Mathias Agopian53331da2011-08-22 21:44:41 -07001998void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07001999 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002000 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002001}
2002
Mathias Agopian59119e62010-10-11 12:37:43 -07002003// ---------------------------------------------------------------------------
2004
Mathias Agopian118d0242011-10-13 16:02:48 -07002005status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2006 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2007{
2008 Mutex::Autolock _l(mStateLock);
2009 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2010}
2011
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002012status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2013 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002014{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002015 ATRACE_CALL();
2016
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 if (!GLExtensions::getInstance().haveFramebufferObject())
2018 return INVALID_OPERATION;
2019
2020 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002021 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002022 const uint32_t hw_w = hw.getWidth();
2023 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002024 GLfloat u = 1;
2025 GLfloat v = 1;
2026
2027 // make sure to clear all GL error flags
2028 while ( glGetError() != GL_NO_ERROR ) ;
2029
2030 // create a FBO
2031 GLuint name, tname;
2032 glGenTextures(1, &tname);
2033 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002034 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2035 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002036 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2037 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002038 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002039 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002040 GLint tw = (2 << (31 - clz(hw_w)));
2041 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002042 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2043 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002044 u = GLfloat(hw_w) / tw;
2045 v = GLfloat(hw_h) / th;
2046 }
2047 glGenFramebuffersOES(1, &name);
2048 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002049 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2050 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002051
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002052 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002053 glDisable(GL_TEXTURE_EXTERNAL_OES);
2054 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002055 glClearColor(0,0,0,1);
2056 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002057 glMatrixMode(GL_MODELVIEW);
2058 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002059 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002060 const size_t count = layers.size();
2061 for (size_t i=0 ; i<count ; ++i) {
2062 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002063 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 }
2065
Mathias Agopian118d0242011-10-13 16:02:48 -07002066 hw.compositionComplete();
2067
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002068 // back to main framebuffer
2069 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070 glDeleteFramebuffersOES(1, &name);
2071
2072 *textureName = tname;
2073 *uOut = u;
2074 *vOut = v;
2075 return NO_ERROR;
2076}
2077
2078// ---------------------------------------------------------------------------
2079
Mathias Agopian74c40c02010-09-29 13:02:36 -07002080status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2081 sp<IMemoryHeap>* heap,
2082 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002083 uint32_t sw, uint32_t sh,
2084 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002085{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002086 ATRACE_CALL();
2087
Mathias Agopian74c40c02010-09-29 13:02:36 -07002088 status_t result = PERMISSION_DENIED;
2089
2090 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002091 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002092 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002093 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002094
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002095 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002096 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002097 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002098
2099 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002100 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002101 const uint32_t hw_w = hw.getWidth();
2102 const uint32_t hw_h = hw.getHeight();
2103
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002104 // if we have secure windows on this display, never allow the screen capture
2105 if (hw.getSecureLayerVisible()) {
2106 return PERMISSION_DENIED;
2107 }
2108
2109 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002110 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002111 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002112
2113 sw = (!sw) ? hw_w : sw;
2114 sh = (!sh) ? hw_h : sh;
2115 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002116 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002117
Steve Block9d453682011-12-20 16:23:08 +00002118 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002119 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002120
Mathias Agopian74c40c02010-09-29 13:02:36 -07002121 // make sure to clear all GL error flags
2122 while ( glGetError() != GL_NO_ERROR ) ;
2123
2124 // create a FBO
2125 GLuint name, tname;
2126 glGenRenderbuffersOES(1, &tname);
2127 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2128 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002129
Mathias Agopian74c40c02010-09-29 13:02:36 -07002130 glGenFramebuffersOES(1, &name);
2131 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2132 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2133 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2134
2135 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002136
Mathias Agopian74c40c02010-09-29 13:02:36 -07002137 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2138
2139 // invert everything, b/c glReadPixel() below will invert the FB
2140 glViewport(0, 0, sw, sh);
2141 glMatrixMode(GL_PROJECTION);
2142 glPushMatrix();
2143 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002144 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002145 glMatrixMode(GL_MODELVIEW);
2146
2147 // redraw the screen entirely...
2148 glClearColor(0,0,0,1);
2149 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002150
Jamie Gennis9575f602011-10-07 14:51:16 -07002151 const LayerVector& layers(mDrawingState.layersSortedByZ);
2152 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002153 for (size_t i=0 ; i<count ; ++i) {
2154 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002155 const uint32_t flags = layer->drawingState().flags;
2156 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2157 const uint32_t z = layer->drawingState().z;
2158 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002159 if (filtering) layer->setFiltering(true);
2160 layer->draw(hw);
2161 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002162 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002163 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002164 }
2165
Mathias Agopian74c40c02010-09-29 13:02:36 -07002166 // check for errors and return screen capture
2167 if (glGetError() != GL_NO_ERROR) {
2168 // error while rendering
2169 result = INVALID_OPERATION;
2170 } else {
2171 // allocate shared memory large enough to hold the
2172 // screen capture
2173 sp<MemoryHeapBase> base(
2174 new MemoryHeapBase(size, 0, "screen-capture") );
2175 void* const ptr = base->getBase();
2176 if (ptr) {
2177 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002178 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002179 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2180 if (glGetError() == GL_NO_ERROR) {
2181 *heap = base;
2182 *w = sw;
2183 *h = sh;
2184 *f = PIXEL_FORMAT_RGBA_8888;
2185 result = NO_ERROR;
2186 }
2187 } else {
2188 result = NO_MEMORY;
2189 }
2190 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002191 glViewport(0, 0, hw_w, hw_h);
2192 glMatrixMode(GL_PROJECTION);
2193 glPopMatrix();
2194 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002195 } else {
2196 result = BAD_VALUE;
2197 }
2198
2199 // release FBO resources
2200 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2201 glDeleteRenderbuffersOES(1, &tname);
2202 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002203
2204 hw.compositionComplete();
2205
Steve Block9d453682011-12-20 16:23:08 +00002206 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002207
Mathias Agopian74c40c02010-09-29 13:02:36 -07002208 return result;
2209}
2210
2211
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002212status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2213 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002214 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002215 uint32_t sw, uint32_t sh,
2216 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002217{
2218 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002219 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002220 return BAD_VALUE;
2221
2222 if (!GLExtensions::getInstance().haveFramebufferObject())
2223 return INVALID_OPERATION;
2224
2225 class MessageCaptureScreen : public MessageBase {
2226 SurfaceFlinger* flinger;
2227 DisplayID dpy;
2228 sp<IMemoryHeap>* heap;
2229 uint32_t* w;
2230 uint32_t* h;
2231 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002232 uint32_t sw;
2233 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002234 uint32_t minLayerZ;
2235 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002236 status_t result;
2237 public:
2238 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002239 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002240 uint32_t sw, uint32_t sh,
2241 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002242 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002243 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2244 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2245 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002246 {
2247 }
2248 status_t getResult() const {
2249 return result;
2250 }
2251 virtual bool handler() {
2252 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002253 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002254 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002255 return true;
2256 }
2257 };
2258
2259 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002260 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002261 status_t res = postMessageSync(msg);
2262 if (res == NO_ERROR) {
2263 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2264 }
2265 return res;
2266}
2267
2268// ---------------------------------------------------------------------------
2269
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002270SurfaceFlinger::LayerVector::LayerVector() {
2271}
2272
2273SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2274 : SortedVector<sp<LayerBase> >(rhs) {
2275}
2276
2277int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2278 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002279{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002280 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002281 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2282 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002283
2284 uint32_t ls = l->currentState().layerStack;
2285 uint32_t rs = r->currentState().layerStack;
2286 if (ls != rs)
2287 return ls - rs;
2288
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002289 uint32_t lz = l->currentState().z;
2290 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002291 if (lz != rz)
2292 return lz - rz;
2293
2294 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002295}
2296
2297// ---------------------------------------------------------------------------
2298
Mathias Agopian92a979a2012-08-02 18:32:23 -07002299SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2300 : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002301}
2302
2303// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002304
Jamie Gennis9a78c902011-01-12 18:30:40 -08002305GraphicBufferAlloc::GraphicBufferAlloc() {}
2306
2307GraphicBufferAlloc::~GraphicBufferAlloc() {}
2308
2309sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002310 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002311 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2312 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002313 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002314 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002315 if (err == NO_MEMORY) {
2316 GraphicBuffer::dumpAllocationsToSystemLog();
2317 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002318 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002319 "failed (%s), handle=%p",
2320 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002321 return 0;
2322 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002323 return graphicBuffer;
2324}
2325
Jamie Gennis9a78c902011-01-12 18:30:40 -08002326// ---------------------------------------------------------------------------
2327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002328}; // namespace android