blob: 3aba3dbb80249788166e192c18e86ed5720be1fc [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
39#include <gui/SurfaceTextureClient.h>
40
41#include <ui/GraphicBufferAllocator.h>
42#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <utils/String8.h>
45#include <utils/String16.h>
46#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Mathias Agopian921e6ac2012-07-23 23:11:29 -070049#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070053#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070054#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070056#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070059#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopiana4912602012-07-12 14:25:33 -070062#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070063#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070085 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070098 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
113
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 ALOGI_IF(mDebugRegion, "showupdates enabled");
115 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800118void SurfaceFlinger::onFirstRef()
119{
120 mEventQueue.init(this);
121
122 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
123
124 // Wait for the main thread to be done with its initialization
125 mReadyToRunBarrier.wait();
126}
127
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::~SurfaceFlinger()
130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void SurfaceFlinger::binderDied(const wp<IBinder>& who)
137{
138 // the window manager died on us. prepare its eulogy.
139
140 // reset screen orientation
141 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700142 Vector<DisplayState> displays;
143 DisplayState d;
144 d.orientation = eOrientationDefault;
145 displays.add(d);
146 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800147
148 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700149 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150}
151
Mathias Agopian7e27f052010-05-28 14:22:23 -0700152sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopian96f08192010-06-02 23:28:45 -0700154 sp<ISurfaceComposerClient> bclient;
155 sp<Client> client(new Client(this));
156 status_t err = client->initCheck();
157 if (err == NO_ERROR) {
158 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return bclient;
161}
162
Jamie Gennis9a78c902011-01-12 18:30:40 -0800163sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
164{
165 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
166 return gba;
167}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000173 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700175
176 // wait patiently for the window manager death
177 const String16 name("window");
178 sp<IBinder> window(defaultServiceManager()->getService(name));
179 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700180 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700181 }
182
183 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700184 // formerly we would just kill the process, but we now ask it to exit so it
185 // can choose where to stop the animation.
186 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700189void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
190 class MessageDestroyGLTexture : public MessageBase {
191 GLuint texture;
192 public:
193 MessageDestroyGLTexture(GLuint texture)
194 : texture(texture) {
195 }
196 virtual bool handler() {
197 glDeleteTextures(1, &texture);
198 return true;
199 }
200 };
201 postMessageAsync(new MessageDestroyGLTexture(texture));
202}
203
Mathias Agopiana4912602012-07-12 14:25:33 -0700204status_t SurfaceFlinger::selectConfigForPixelFormat(
205 EGLDisplay dpy,
206 EGLint const* attrs,
207 PixelFormat format,
208 EGLConfig* outConfig)
209{
210 EGLConfig config = NULL;
211 EGLint numConfigs = -1, n=0;
212 eglGetConfigs(dpy, NULL, 0, &numConfigs);
213 EGLConfig* const configs = new EGLConfig[numConfigs];
214 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
215 for (int i=0 ; i<n ; i++) {
216 EGLint nativeVisualId = 0;
217 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
218 if (nativeVisualId>0 && format == nativeVisualId) {
219 *outConfig = configs[i];
220 delete [] configs;
221 return NO_ERROR;
222 }
223 }
224 delete [] configs;
225 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
Mathias Agopiana4912602012-07-12 14:25:33 -0700228EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
229 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
230 // it is to be used with WIFI displays
231 EGLConfig config;
232 EGLint dummy;
233 status_t err;
234 EGLint attribs[] = {
235 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
236 EGL_RECORDABLE_ANDROID, EGL_TRUE,
237 EGL_NONE
238 };
239 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
240 if (err) {
241 // maybe we failed because of EGL_RECORDABLE_ANDROID
242 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
243 attribs[2] = EGL_NONE;
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 }
246 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
247 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
248 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
249 }
250 return config;
251}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
Mathias Agopiana4912602012-07-12 14:25:33 -0700253EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
254 // Also create our EGLContext
255 EGLint contextAttributes[] = {
256#ifdef EGL_IMG_context_priority
257#ifdef HAS_CONTEXT_PRIORITY
258#warning "using EGL_IMG_context_priority"
259 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
260#endif
261#endif
262 EGL_NONE, EGL_NONE
263 };
264 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
265 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
266 return ctxt;
267}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268
Mathias Agopiana4912602012-07-12 14:25:33 -0700269void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
270 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
271 if (!result) {
272 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
273 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 }
275
Mathias Agopiana4912602012-07-12 14:25:33 -0700276 GLExtensions& extensions(GLExtensions::getInstance());
277 extensions.initWithGLStrings(
278 glGetString(GL_VENDOR),
279 glGetString(GL_RENDERER),
280 glGetString(GL_VERSION),
281 glGetString(GL_EXTENSIONS),
282 eglQueryString(display, EGL_VENDOR),
283 eglQueryString(display, EGL_VERSION),
284 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700285
Mathias Agopiana4912602012-07-12 14:25:33 -0700286 EGLint w, h;
287 eglQuerySurface(display, surface, EGL_WIDTH, &w);
288 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700289
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
291 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700292
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 glShadeModel(GL_FLAT);
297 glDisable(GL_DITHER);
298 glDisable(GL_CULL_FACE);
299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300 struct pack565 {
301 inline uint16_t operator() (int r, int g, int b) const {
302 return (r<<11)|(g<<5)|b;
303 }
304 } pack565;
305
Jamie Gennis9575f602011-10-07 14:51:16 -0700306 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
307 glGenTextures(1, &mProtectedTexName);
308 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
309 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
310 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
311 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
312 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
313 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
314 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316 glViewport(0, 0, w, h);
317 glMatrixMode(GL_PROJECTION);
318 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700319 // put the origin in the left-bottom corner
320 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
Mathias Agopiana4912602012-07-12 14:25:33 -0700322 // print some debugging info
323 EGLint r,g,b,a;
324 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
325 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
326 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
327 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
328 ALOGI("EGL informations:");
329 ALOGI("vendor : %s", extensions.getEglVendor());
330 ALOGI("version : %s", extensions.getEglVersion());
331 ALOGI("extensions: %s", extensions.getEglExtension());
332 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
333 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
334 ALOGI("OpenGL ES informations:");
335 ALOGI("vendor : %s", extensions.getVendor());
336 ALOGI("renderer : %s", extensions.getRenderer());
337 ALOGI("version : %s", extensions.getVersion());
338 ALOGI("extensions: %s", extensions.getExtension());
339 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
340 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
341}
342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343status_t SurfaceFlinger::readyToRun()
344{
345 ALOGI( "SurfaceFlinger's main thread ready to run. "
346 "Initializing graphics H/W...");
347
Mathias Agopiana4912602012-07-12 14:25:33 -0700348 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700349 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
350 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700351
352 // Initialize the main display
353 // create native window to main display
354 sp<FramebufferSurface> anw = FramebufferSurface::create();
355 ANativeWindow* const window = anw.get();
356 if (!window) {
357 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
358 exit(0);
359 }
360
361 // initialize the config and context
362 int format;
363 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700364 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
365 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700366
367 // initialize our main display hardware
Mathias Agopian92a979a2012-08-02 18:32:23 -0700368 mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState());
369 DisplayDevice hw(this, DisplayDevice::DISPLAY_ID_MAIN, anw, mEGLConfig);
370 mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700371
372 // initialize OpenGL ES
Mathias Agopian92a979a2012-08-02 18:32:23 -0700373 EGLSurface surface = hw.getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700374 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800375
Mathias Agopian028508c2012-07-25 21:12:12 -0700376 // start the EventThread
377 mEventThread = new EventThread(this);
378 mEventQueue.setEventThread(mEventThread);
379
Mathias Agopian86303202012-07-24 22:46:10 -0700380 // initialize the H/W composer
Mathias Agopian888c8222012-08-04 21:10:38 -0700381 mHwc = new HWComposer(this, *static_cast<HWComposer::EventHandler *>(this));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700382
383 // initialize our drawing state
384 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700385
Mathias Agopiana4912602012-07-12 14:25:33 -0700386 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800387 mReadyToRunBarrier.open();
388
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700389 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700390 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 return NO_ERROR;
393}
394
Mathias Agopiana67e4182012-06-19 17:26:12 -0700395void SurfaceFlinger::startBootAnim() {
396 // start boot animation
397 property_set("service.bootanim.exit", "0");
398 property_set("ctl.start", "bootanim");
399}
400
Mathias Agopiana4912602012-07-12 14:25:33 -0700401uint32_t SurfaceFlinger::getMaxTextureSize() const {
402 return mMaxTextureSize;
403}
404
405uint32_t SurfaceFlinger::getMaxViewportDims() const {
406 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
407 mMaxViewportDims[0] : mMaxViewportDims[1];
408}
409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800411
Jamie Gennis582270d2011-08-17 18:19:00 -0700412bool SurfaceFlinger::authenticateSurfaceTexture(
413 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800414 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700415 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800416
417 // Check the visible layer list for the ISurface
418 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
419 size_t count = currentLayers.size();
420 for (size_t i=0 ; i<count ; i++) {
421 const sp<LayerBase>& layer(currentLayers[i]);
422 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700423 if (lbc != NULL) {
424 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
425 if (lbcBinder == surfaceTextureBinder) {
426 return true;
427 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800428 }
429 }
430
431 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700432 // SurfaceTexture gets destroyed before all the clients are done using it,
433 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800434 // will instead fail later on when the client tries to use the surface,
435 // which should be reported as "surface XYZ returned an -ENODEV". The
436 // purgatorized layers are no less authentic than the visible ones, so this
437 // should not cause any harm.
438 size_t purgatorySize = mLayerPurgatory.size();
439 for (size_t i=0 ; i<purgatorySize ; i++) {
440 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
441 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700442 if (lbc != NULL) {
443 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
444 if (lbcBinder == surfaceTextureBinder) {
445 return true;
446 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800447 }
448 }
449
450 return false;
451}
452
Mathias Agopianc666cae2012-07-25 18:56:13 -0700453status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700454 // TODO: this is here only for compatibility -- show go away eventually.
Mathias Agopianc666cae2012-07-25 18:56:13 -0700455 if (uint32_t(dpy) >= 2) {
456 return BAD_INDEX;
457 }
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700458 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian888c8222012-08-04 21:10:38 -0700459 info->w = hw.getWidth();
460 info->h = hw.getHeight();
461 info->xdpi = hw.getDpiX();
462 info->ydpi = hw.getDpiY();
463 info->fps = float(1e9 / getHwComposer().getRefreshPeriod());
464 info->density = hw.getDensity();
465 info->orientation = hw.getOrientation();
466 // TODO: this needs to go away (currently needed only by webkit)
467 getPixelFormatInfo(hw.getFormat(), &info->pixelFormatInfo);
468 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700469}
470
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800471// ----------------------------------------------------------------------------
472
473sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800474 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700475}
476
Mathias Agopian3094df32012-06-18 18:06:45 -0700477void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700478 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian3094df32012-06-18 18:06:45 -0700479 EGLSurface result = EGL_NO_SURFACE;
480 EGLSurface old_surface = EGL_NO_SURFACE;
481 sp<SurfaceTextureClient> stc;
482
483 if (display != NULL) {
484 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700485 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700486 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700487 ALOGE_IF(result == EGL_NO_SURFACE,
488 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
489 display.get());
490 }
491
492 { // scope for the lock
493 Mutex::Autolock _l(mStateLock);
494 old_surface = mExternalDisplaySurface;
495 mExternalDisplayNativeWindow = stc;
496 mExternalDisplaySurface = result;
497 ALOGD("mExternalDisplaySurface = %p", result);
498 }
499
500 if (old_surface != EGL_NO_SURFACE) {
501 // Note: EGL allows to destroy an object while its current
502 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700503 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700504 }
505}
506
507EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
508 Mutex::Autolock _l(mStateLock);
509 return mExternalDisplaySurface;
510}
511
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800513
514void SurfaceFlinger::waitForEvent() {
515 mEventQueue.waitMessage();
516}
517
518void SurfaceFlinger::signalTransaction() {
519 mEventQueue.invalidate();
520}
521
522void SurfaceFlinger::signalLayerUpdate() {
523 mEventQueue.invalidate();
524}
525
526void SurfaceFlinger::signalRefresh() {
527 mEventQueue.refresh();
528}
529
530status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
531 nsecs_t reltime, uint32_t flags) {
532 return mEventQueue.postMessage(msg, reltime);
533}
534
535status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
536 nsecs_t reltime, uint32_t flags) {
537 status_t res = mEventQueue.postMessage(msg, reltime);
538 if (res == NO_ERROR) {
539 msg->wait();
540 }
541 return res;
542}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
Mathias Agopian4fec8732012-06-29 14:12:52 -0700544bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 return true;
547}
548
Mathias Agopian86303202012-07-24 22:46:10 -0700549void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700550 mEventThread->onVSyncReceived(dpy, timestamp);
551}
552
553void SurfaceFlinger::eventControl(int event, int enabled) {
554 getHwComposer().eventControl(event, enabled);
555}
556
Mathias Agopian4fec8732012-06-29 14:12:52 -0700557void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800558 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800559 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700560 case MessageQueue::INVALIDATE:
561 handleMessageTransaction();
562 handleMessageInvalidate();
563 signalRefresh();
564 break;
565 case MessageQueue::REFRESH:
566 handleMessageRefresh();
567 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800568 }
569}
570
Mathias Agopian4fec8732012-06-29 14:12:52 -0700571void SurfaceFlinger::handleMessageTransaction() {
572 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
573 uint32_t transactionFlags = peekTransactionFlags(mask);
574 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700575 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700576 }
577}
578
579void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700580 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700581}
582
583void SurfaceFlinger::handleMessageRefresh() {
584 handleRefresh();
585
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700586 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700587 mVisibleRegionsDirty = false;
588 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700589
590 /*
591 * rebuild the visible layer list per screen
592 */
593
Mathias Agopian87baae12012-07-31 12:38:26 -0700594 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700595 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
596 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian87baae12012-07-31 12:38:26 -0700597 Region opaqueRegion;
598 Region dirtyRegion;
599 computeVisibleRegions(currentLayers,
600 hw.getLayerStack(), dirtyRegion, opaqueRegion);
601 hw.dirtyRegion.orSelf(dirtyRegion);
602
603 Vector< sp<LayerBase> > layersSortedByZ;
604 const size_t count = currentLayers.size();
605 for (size_t i=0 ; i<count ; i++) {
606 const Layer::State& s(currentLayers[i]->drawingState());
607 if (s.layerStack == hw.getLayerStack()) {
608 if (!currentLayers[i]->visibleRegion.isEmpty()) {
609 layersSortedByZ.add(currentLayers[i]);
610 }
611 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700612 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700613 hw.setVisibleLayersSortedByZ(layersSortedByZ);
614 hw.undefinedRegion.set(hw.getBounds());
615 hw.undefinedRegion.subtractSelf(hw.getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700616 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700617 }
618
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700619 HWComposer& hwc(getHwComposer());
620 if (hwc.initCheck() == NO_ERROR) {
621 // build the h/w work list
622 const bool workListsDirty = mHwWorkListDirty;
623 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700624 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
625 const DisplayDevice& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700626 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
627 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700628
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700629 hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
630
631 HWComposer::LayerListIterator cur = hwc.begin();
632 const HWComposer::LayerListIterator end = hwc.end();
633 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
634 const sp<LayerBase>& layer(currentLayers[i]);
635
636 if (CC_UNLIKELY(workListsDirty)) {
637 layer->setGeometry(hw, *cur);
638 if (mDebugDisableHWC || mDebugRegion) {
639 cur->setSkip(true);
640 }
641 }
642
643 /*
644 * update the per-frame h/w composer data for each layer
645 * and build the transparent region of the FB
646 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700647 layer->setPerFrameData(hw, *cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700648 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700649 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700650 status_t err = hwc.prepare();
651 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
652 }
653
654 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700655 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
656 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian4fec8732012-06-29 14:12:52 -0700657
Mathias Agopian87baae12012-07-31 12:38:26 -0700658 // transform the dirty region into this screen's coordinate space
659 const Transform& planeTransform(hw.getTransform());
660 Region dirtyRegion;
661 if (repaintEverything) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700662 dirtyRegion.set(hw.bounds());
663 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -0700664 dirtyRegion = planeTransform.transform(hw.dirtyRegion);
665 dirtyRegion.andSelf(hw.bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700666 }
667 hw.dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700668
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700669 if (!dirtyRegion.isEmpty()) {
670 if (hw.canDraw()) {
671 // repaint the framebuffer (if needed)
672 handleRepaint(hw, dirtyRegion);
673 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700674 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700675 // inform the h/w that we're done compositing
676 hw.compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700677 }
678
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700679 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700680
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700681
682#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700683 // render to the external display if we have one
684 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
685 if (externalDisplaySurface != EGL_NO_SURFACE) {
686 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
687 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
688 externalDisplaySurface, externalDisplaySurface,
689 eglGetCurrentContext());
690
691 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
692
693 if (success) {
694 // redraw the screen entirely...
695 glDisable(GL_TEXTURE_EXTERNAL_OES);
696 glDisable(GL_TEXTURE_2D);
697 glClearColor(0,0,0,1);
698 glClear(GL_COLOR_BUFFER_BIT);
699 glMatrixMode(GL_MODELVIEW);
700 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700701
Mathias Agopian92a979a2012-08-02 18:32:23 -0700702 DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice()));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700703 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700704 const size_t count = layers.size();
705 for (size_t i=0 ; i<count ; ++i) {
706 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700707 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700708 }
709
710 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
711 ALOGE_IF(!success, "external display eglSwapBuffers failed");
712
713 hw.compositionComplete();
714 }
715
716 success = eglMakeCurrent(eglGetCurrentDisplay(),
717 cur, cur, eglGetCurrentContext());
718
719 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
720 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700721#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700722
723}
724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725void SurfaceFlinger::postFramebuffer()
726{
Mathias Agopian841cde52012-03-01 15:44:37 -0800727 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800728
Mathias Agopiana44b0412011-10-16 18:46:35 -0700729 const nsecs_t now = systemTime();
730 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700731
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700732 HWComposer& hwc(getHwComposer());
733
Mathias Agopian92a979a2012-08-02 18:32:23 -0700734 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
735 DisplayDevice& hw(mDisplays.editValueAt(dpy));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700736 if (hwc.initCheck() == NO_ERROR) {
737 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
738 const size_t count = currentLayers.size();
739 HWComposer::LayerListIterator cur = hwc.begin();
740 const HWComposer::LayerListIterator end = hwc.end();
741 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
742 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700743 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700744 }
745 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700746 hw.flip(hw.swapRegion);
747 hw.swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700748 }
749
Jesse Hallef194142012-06-14 14:45:17 -0700750 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700751 // FIXME: eventually commit() won't take arguments
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700752 hwc.commit(mEGLDisplay, getDefaultDisplayDevice().getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700753 }
754
Mathias Agopian92a979a2012-08-02 18:32:23 -0700755 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
756 const DisplayDevice& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700757 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
758 const size_t count = currentLayers.size();
759 if (hwc.initCheck() == NO_ERROR) {
760 HWComposer::LayerListIterator cur = hwc.begin();
761 const HWComposer::LayerListIterator end = hwc.end();
762 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700763 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700764 }
765 } else {
766 eglSwapBuffers(mEGLDisplay, hw.getEGLSurface());
767 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700768 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700769 }
Jesse Hallef194142012-06-14 14:45:17 -0700770 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700771
772 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800773 }
774
Mathias Agopiana44b0412011-10-16 18:46:35 -0700775 mLastSwapBufferTime = systemTime() - now;
776 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777}
778
Mathias Agopian87baae12012-07-31 12:38:26 -0700779void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780{
Mathias Agopian841cde52012-03-01 15:44:37 -0800781 ATRACE_CALL();
782
Mathias Agopianca4d3602011-05-19 15:38:14 -0700783 Mutex::Autolock _l(mStateLock);
784 const nsecs_t now = systemTime();
785 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786
Mathias Agopianca4d3602011-05-19 15:38:14 -0700787 // Here we're guaranteed that some transaction flags are set
788 // so we can call handleTransactionLocked() unconditionally.
789 // We call getTransactionFlags(), which will also clear the flags,
790 // with mStateLock held to guarantee that mCurrentState won't change
791 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700792
Mathias Agopianca4d3602011-05-19 15:38:14 -0700793 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
794 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700795 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700796
Mathias Agopianca4d3602011-05-19 15:38:14 -0700797 mLastTransactionTime = systemTime() - now;
798 mDebugInTransaction = 0;
799 invalidateHwcGeometry();
800 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700801}
802
Mathias Agopian87baae12012-07-31 12:38:26 -0700803void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700804{
805 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 const size_t count = currentLayers.size();
807
808 /*
809 * Traversal of the children
810 * (perform the transaction for each of them if needed)
811 */
812
813 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
814 if (layersNeedTransaction) {
815 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700816 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
818 if (!trFlags) continue;
819
820 const uint32_t flags = layer->doTransaction(0);
821 if (flags & Layer::eVisibleRegion)
822 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 }
824 }
825
826 /*
827 * Perform our own transaction if needed
828 */
829
830 if (transactionFlags & eTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700831 // here we take advantage of Vector's copy-on-write semantics to
832 // improve performance by skipping the transaction entirely when
833 // know that the lists are identical
834 const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
835 const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
836 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700838 const size_t cc = curr.size();
839 const size_t dc = draw.size();
840
841 // find the displays that were removed
842 // (ie: in drawing state but not in current state)
843 // also handle displays that changed
844 // (ie: displays that are in both lists)
845 for (size_t i=0 ; i<dc ; i++) {
846 if (curr.indexOfKey(draw[i].id) < 0) {
847 // in drawing state but not in current state
848 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
849 mDisplays.editValueFor(draw[i].id).terminate();
850 mDisplays.removeItem(draw[i].id);
851 } else {
852 ALOGW("trying to remove the main display");
853 }
854 } else {
855 // this display is in both lists. see if something changed.
856 const DisplayDeviceState& state(curr[i]);
857 if (state.layerStack != draw[i].layerStack) {
858 DisplayDevice& disp(mDisplays.editValueFor(state.id));
859 //disp.setLayerStack(state.layerStack);
860 }
861 if (curr[i].orientation != draw[i].orientation) {
862 DisplayDevice& disp(mDisplays.editValueFor(state.id));
863 disp.setOrientation(state.orientation);
864 }
865 }
866 }
867
868 // find displays that were added
869 // (ie: in current state but not in drawing state)
870 for (size_t i=0 ; i<cc ; i++) {
871 if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
872 // FIXME: we need to pass the surface here
873 DisplayDevice disp(this, curr[i].id, 0, mEGLConfig);
874 mDisplays.add(curr[i].id, disp);
875 }
876 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 }
878
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700879 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
880 // layers have been added
881 mVisibleRegionsDirty = true;
882 }
883
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 // some layers might have been removed, so
885 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700886 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700887 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700889 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700890 const size_t count = previousLayers.size();
891 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700892 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700893 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700894 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700895 // TODO: we could traverse the tree from front to back and
896 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700897 // TODO: we could cache the transformed region
898 Layer::State front(layer->drawingState());
899 Region visibleReg = front.transform.transform(
900 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700901 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700902 }
903 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 }
906
907 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700908}
909
910void SurfaceFlinger::commitTransaction()
911{
912 if (!mLayersPendingRemoval.isEmpty()) {
913 // Notify removed layers now that they can't be drawn from
914 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
915 mLayersPendingRemoval[i]->onRemoved();
916 }
917 mLayersPendingRemoval.clear();
918 }
919
920 mDrawingState = mCurrentState;
921 mTransationPending = false;
922 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923}
924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700926 const LayerVector& currentLayers, uint32_t layerStack,
927 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928{
Mathias Agopian841cde52012-03-01 15:44:37 -0800929 ATRACE_CALL();
930
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 Region aboveOpaqueLayers;
932 Region aboveCoveredLayers;
933 Region dirty;
934
Mathias Agopian87baae12012-07-31 12:38:26 -0700935 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936
937 size_t i = currentLayers.size();
938 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700939 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940
941 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700942 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943
Mathias Agopian87baae12012-07-31 12:38:26 -0700944 // only consider the layers on the given later stack
945 if (s.layerStack != layerStack)
946 continue;
947
Mathias Agopianab028732010-03-16 16:41:46 -0700948 /*
949 * opaqueRegion: area of a surface that is fully opaque.
950 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700952
953 /*
954 * visibleRegion: area of a surface that is visible on screen
955 * and not fully transparent. This is essentially the layer's
956 * footprint minus the opaque regions above it.
957 * Areas covered by a translucent surface are considered visible.
958 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700960
961 /*
962 * coveredRegion: area of a surface that is covered by all
963 * visible regions above it (which includes the translucent areas).
964 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700966
967
968 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800969 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700970 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700971 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700973 if (!visibleRegion.isEmpty()) {
974 // Remove the transparent area from the visible region
975 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700976 Region transparentRegionScreen;
977 const Transform tr(s.transform);
978 if (tr.transformed()) {
979 if (tr.preserveRects()) {
980 // transform the transparent region
981 transparentRegionScreen = tr.transform(s.transparentRegion);
982 } else {
983 // transformation too complex, can't do the
984 // transparent region optimization.
985 transparentRegionScreen.clear();
986 }
987 } else {
988 transparentRegionScreen = s.transparentRegion;
989 }
990 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700991 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992
Mathias Agopianab028732010-03-16 16:41:46 -0700993 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700994 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700995 if (s.alpha==255 && !translucent &&
996 ((layerOrientation & Transform::ROT_INVALID) == false)) {
997 // the opaque region is the layer's footprint
998 opaqueRegion = visibleRegion;
999 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 }
1001 }
1002
Mathias Agopianab028732010-03-16 16:41:46 -07001003 // Clip the covered region to the visible region
1004 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1005
1006 // Update aboveCoveredLayers for next (lower) layer
1007 aboveCoveredLayers.orSelf(visibleRegion);
1008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 // subtract the opaque region covered by the layers above us
1010 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
1012 // compute this layer's dirty region
1013 if (layer->contentDirty) {
1014 // we need to invalidate the whole region
1015 dirty = visibleRegion;
1016 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001017 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 layer->contentDirty = false;
1019 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001020 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001021 * the exposed region consists of two components:
1022 * 1) what's VISIBLE now and was COVERED before
1023 * 2) what's EXPOSED now less what was EXPOSED before
1024 *
1025 * note that (1) is conservative, we start with the whole
1026 * visible region but only keep what used to be covered by
1027 * something -- which mean it may have been exposed.
1028 *
1029 * (2) handles areas that were not covered by anything but got
1030 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001031 */
Mathias Agopianab028732010-03-16 16:41:46 -07001032 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001033 const Region oldVisibleRegion = layer->visibleRegion;
1034 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001035 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1036 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 }
1038 dirty.subtractSelf(aboveOpaqueLayers);
1039
1040 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001041 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042
Mathias Agopianab028732010-03-16 16:41:46 -07001043 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 // Store the visible region is screen space
1047 layer->setVisibleRegion(visibleRegion);
1048 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 }
1050
Mathias Agopian87baae12012-07-31 12:38:26 -07001051 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052}
1053
Mathias Agopian87baae12012-07-31 12:38:26 -07001054void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1055 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001056 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1057 DisplayDevice& hw(mDisplays.editValueAt(dpy));
1058 if (hw.getLayerStack() == layerStack) {
1059 hw.dirtyRegion.orSelf(dirty);
1060 }
1061 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001062}
1063
1064void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001066 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001067 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001069 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001070
Mathias Agopian4fec8732012-06-29 14:12:52 -07001071 bool visibleRegions = false;
1072 const size_t count = currentLayers.size();
1073 sp<LayerBase> const* layers = currentLayers.array();
1074 for (size_t i=0 ; i<count ; i++) {
1075 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001076 const Region dirty(layer->latchBuffer(visibleRegions));
1077 Layer::State s(layer->drawingState());
1078 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001079 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001080
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001081 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082}
1083
Mathias Agopianad456f92011-01-13 17:53:01 -08001084void SurfaceFlinger::invalidateHwcGeometry()
1085{
1086 mHwWorkListDirty = true;
1087}
1088
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001089void SurfaceFlinger::handleRefresh()
1090{
1091 bool needInvalidate = false;
1092 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1093 const size_t count = currentLayers.size();
1094 for (size_t i=0 ; i<count ; i++) {
1095 const sp<LayerBase>& layer(currentLayers[i]);
1096 if (layer->onPreComposition()) {
1097 needInvalidate = true;
1098 }
1099 }
1100 if (needInvalidate) {
1101 signalLayerUpdate();
1102 }
1103}
1104
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001105void SurfaceFlinger::handleRepaint(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001106 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107{
Mathias Agopian841cde52012-03-01 15:44:37 -08001108 ATRACE_CALL();
1109
Mathias Agopian87baae12012-07-31 12:38:26 -07001110 Region dirtyRegion(inDirtyRegion);
1111
Mathias Agopianb8a55602009-06-26 19:06:36 -07001112 // compute the invalid region
Mathias Agopian87baae12012-07-31 12:38:26 -07001113 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114
Glenn Kasten99ed2242011-12-15 09:51:17 -08001115 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001116 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 }
1118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 uint32_t flags = hw.getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001120 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001121 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1122 // takes a rectangle, we must make sure to update that whole
1123 // rectangle in that case
Mathias Agopian87baae12012-07-31 12:38:26 -07001124 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001125 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001126 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001127 // We need to redraw the rectangle that will be updated
1128 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001129 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001130 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian87baae12012-07-31 12:38:26 -07001131 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001132 } else {
1133 // we need to redraw everything (the whole screen)
Mathias Agopian87baae12012-07-31 12:38:26 -07001134 dirtyRegion.set(hw.bounds());
1135 hw.swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 }
1137 }
1138
Mathias Agopian87baae12012-07-31 12:38:26 -07001139 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001141 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1142 const size_t count = currentLayers.size();
1143 for (size_t i=0 ; i<count ; i++) {
1144 currentLayers[i]->onPostComposition();
1145 }
1146
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001147 // update the swap region and clear the dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001148 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149}
1150
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001151void SurfaceFlinger::composeSurfaces(const DisplayDevice& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001152{
Mathias Agopian86303202012-07-24 22:46:10 -07001153 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001154 HWComposer::LayerListIterator cur = hwc.begin();
1155 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001156
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001157 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001158 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001159
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001160 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001161
1162 // set the frame buffer
1163 glMatrixMode(GL_MODELVIEW);
1164 glLoadIdentity();
1165
1166 // Never touch the framebuffer if we don't have any framebuffer layers
1167 if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
Mathias Agopianb9494d52012-04-18 02:28:45 -07001168 // when using overlays, we assume a fully transparent framebuffer
1169 // NOTE: we could reduce how much we need to clear, for instance
1170 // remove where there are opaque FB layers. however, on some
1171 // GPUs doing a "clean slate" glClear might be more efficient.
1172 // We'll revisit later if needed.
1173 glClearColor(0, 0, 0, 0);
1174 glClear(GL_COLOR_BUFFER_BIT);
1175 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -07001176 const Region region(hw.undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001177 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001178 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001179 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001180 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001181 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001182 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001183
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001184 /*
1185 * and then, render the layers targeted at the framebuffer
1186 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001187
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001188 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001189 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001190 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001191 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001192 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001193 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001194 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001195 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001196 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001197 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001198 // never clear the very first layer since we're
1199 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001200 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001201 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001202 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001203 continue;
1204 }
1205 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001206 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001207 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001208 if (cur != end) {
1209 ++cur;
1210 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001211 }
1212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213}
1214
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001215void SurfaceFlinger::debugFlashRegions(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001216 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217{
Mathias Agopian0a917752010-06-14 21:20:00 -07001218 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001219 const int32_t height = hw.getHeight();
Mathias Agopian87baae12012-07-31 12:38:26 -07001220 if (hw.swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001221 return;
1222 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001223
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001224 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1225 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian87baae12012-07-31 12:38:26 -07001226 dirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001227 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001228 }
1229
Mathias Agopianc492e672011-10-18 14:49:27 -07001230 glDisable(GL_TEXTURE_EXTERNAL_OES);
1231 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233
Mathias Agopian0926f502009-05-04 14:17:04 -07001234 static int toggle = 0;
1235 toggle = 1 - toggle;
1236 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001237 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001238 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001239 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241
Mathias Agopian87baae12012-07-31 12:38:26 -07001242 Region::const_iterator it = dirtyRegion.begin();
1243 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001244 while (it != end) {
1245 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001247 { r.left, height - r.top },
1248 { r.left, height - r.bottom },
1249 { r.right, height - r.bottom },
1250 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 };
1252 glVertexPointer(2, GL_FLOAT, 0, vertices);
1253 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1254 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001255
Mathias Agopian87baae12012-07-31 12:38:26 -07001256 hw.flip(hw.swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257
1258 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001259 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260}
1261
Mathias Agopian87baae12012-07-31 12:38:26 -07001262void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001264 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001265 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001266 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001267 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001268
1269 GLfloat vertices[4][2];
1270 glVertexPointer(2, GL_FLOAT, 0, vertices);
1271 Region::const_iterator it = region.begin();
1272 Region::const_iterator const end = region.end();
1273 while (it != end) {
1274 const Rect& r = *it++;
1275 vertices[0][0] = r.left;
1276 vertices[0][1] = r.top;
1277 vertices[1][0] = r.right;
1278 vertices[1][1] = r.top;
1279 vertices[2][0] = r.right;
1280 vertices[2][1] = r.bottom;
1281 vertices[3][0] = r.left;
1282 vertices[3][1] = r.bottom;
1283 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1284 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285}
1286
Mathias Agopian96f08192010-06-02 23:28:45 -07001287ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1288 const sp<LayerBaseClient>& lbc)
1289{
Mathias Agopian96f08192010-06-02 23:28:45 -07001290 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001291 size_t name = client->attachLayer(lbc);
1292
Mathias Agopian96f08192010-06-02 23:28:45 -07001293 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001294 Mutex::Autolock _l(mStateLock);
1295 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001296
Mathias Agopian4f113742011-05-03 16:21:41 -07001297 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001298}
1299
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001300status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301{
1302 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001303 status_t err = purgatorizeLayer_l(layer);
1304 if (err == NO_ERROR)
1305 setTransactionFlags(eTransactionNeeded);
1306 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307}
1308
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001309status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
1311 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1312 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001313 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 return NO_ERROR;
1315 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001316 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317}
1318
Mathias Agopian9a112062009-04-17 19:36:26 -07001319status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1320{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001321 // First add the layer to the purgatory list, which makes sure it won't
1322 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001323 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001324 if (err >= 0) {
1325 mLayerPurgatory.add(layerBase);
1326 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001327
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001328 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001329
Mathias Agopian3d579642009-06-04 18:46:21 -07001330 // it's possible that we don't find a layer, because it might
1331 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001332 // from the user because there is a race between Client::destroySurface(),
1333 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001334 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1335}
1336
Mathias Agopiandea20b12011-05-03 17:04:02 -07001337uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1338{
1339 return android_atomic_release_load(&mTransactionFlags);
1340}
1341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1343{
1344 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1345}
1346
Mathias Agopianbb641242010-05-18 17:06:55 -07001347uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348{
1349 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1350 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001351 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 }
1353 return old;
1354}
1355
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356
Mathias Agopian8b33f032012-07-24 20:43:54 -07001357void SurfaceFlinger::setTransactionState(
1358 const Vector<ComposerState>& state,
1359 const Vector<DisplayState>& displays,
1360 uint32_t flags)
1361{
Mathias Agopian698c0872011-06-28 19:09:31 -07001362 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001363
Mathias Agopian8b33f032012-07-24 20:43:54 -07001364 int orientation = eOrientationUnchanged;
1365 if (displays.size()) {
1366 // TODO: handle all displays
1367 orientation = displays[0].orientation;
1368 }
1369
Jamie Gennis28378392011-10-12 17:39:00 -07001370 uint32_t transactionFlags = 0;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001371 // FIXME: don't hardcode display id here
1372 if (mCurrentState.displays.valueFor(0).orientation != orientation) {
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001373 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001374 mCurrentState.displays.editValueFor(0).orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001375 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001376 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001377 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001378 orientation);
1379 }
1380 }
1381
Mathias Agopian698c0872011-06-28 19:09:31 -07001382 const size_t count = state.size();
1383 for (size_t i=0 ; i<count ; i++) {
1384 const ComposerState& s(state[i]);
1385 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001386 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001387 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001388
Mathias Agopian386aa982011-11-07 21:58:03 -08001389 if (transactionFlags) {
1390 // this triggers the transaction
1391 setTransactionFlags(transactionFlags);
1392
1393 // if this is a synchronous transaction, wait for it to take effect
1394 // before returning.
1395 if (flags & eSynchronous) {
1396 mTransationPending = true;
1397 }
1398 while (mTransationPending) {
1399 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1400 if (CC_UNLIKELY(err != NO_ERROR)) {
1401 // just in case something goes wrong in SF, return to the
1402 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001403 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001404 mTransationPending = false;
1405 break;
1406 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001407 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 }
1409}
1410
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001411sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001412 ISurfaceComposerClient::surface_data_t* params,
1413 const String8& name,
1414 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1416 uint32_t flags)
1417{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001418 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001419 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001420
1421 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001422 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001423 int(w), int(h));
1424 return surfaceHandle;
1425 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001426
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001427 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 switch (flags & eFXSurfaceMask) {
1429 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001430 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 break;
1432 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001433 // for now we treat Blur as Dim, until we can implement it
1434 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001436 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001438 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001439 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001440 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 }
1442
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001443 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001444 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001445 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001446 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001448 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001449 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001450 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001451 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001452 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 }
1454
1455 return surfaceHandle;
1456}
1457
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001458sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001459 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001460 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001461 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462{
1463 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001464 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 case PIXEL_FORMAT_TRANSPARENT:
1466 case PIXEL_FORMAT_TRANSLUCENT:
1467 format = PIXEL_FORMAT_RGBA_8888;
1468 break;
1469 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001470#ifdef NO_RGBX_8888
1471 format = PIXEL_FORMAT_RGB_565;
1472#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001473 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001474#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 break;
1476 }
1477
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001478#ifdef NO_RGBX_8888
1479 if (format == PIXEL_FORMAT_RGBX_8888)
1480 format = PIXEL_FORMAT_RGBA_8888;
1481#endif
1482
Mathias Agopian96f08192010-06-02 23:28:45 -07001483 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001484 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001485 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001486 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001487 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 }
1489 return layer;
1490}
1491
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001492sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001493 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001494 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495{
Mathias Agopian96f08192010-06-02 23:28:45 -07001496 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001497 return layer;
1498}
1499
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001500sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001501 const sp<Client>& client, DisplayID display,
1502 uint32_t w, uint32_t h, uint32_t flags)
1503{
1504 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 return layer;
1506}
1507
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001508status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509{
Mathias Agopian9a112062009-04-17 19:36:26 -07001510 /*
1511 * called by the window manager, when a surface should be marked for
1512 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001513 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001514 * The surface is removed from the current and drawing lists, but placed
1515 * in the purgatory queue, so it's not destroyed right-away (we need
1516 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001517 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001518
Mathias Agopian48d819a2009-09-10 19:41:18 -07001519 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001520 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001521 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001522
Mathias Agopian48d819a2009-09-10 19:41:18 -07001523 if (layer != 0) {
1524 err = purgatorizeLayer_l(layer);
1525 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001526 setTransactionFlags(eTransactionNeeded);
1527 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001528 }
1529 return err;
1530}
1531
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001532status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001533{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001534 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001535 status_t err = NO_ERROR;
1536 sp<LayerBaseClient> l(layer.promote());
1537 if (l != NULL) {
1538 Mutex::Autolock _l(mStateLock);
1539 err = removeLayer_l(l);
1540 if (err == NAME_NOT_FOUND) {
1541 // The surface wasn't in the current list, which means it was
1542 // removed already, which means it is in the purgatory,
1543 // and need to be removed from there.
1544 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001545 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001546 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001547 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001548 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001549 "error removing layer=%p (%s)", l.get(), strerror(-err));
1550 }
1551 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552}
1553
Mathias Agopian698c0872011-06-28 19:09:31 -07001554uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001555 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001556 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001559 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1560 if (layer != 0) {
1561 const uint32_t what = s.what;
1562 if (what & ePositionChanged) {
1563 if (layer->setPosition(s.x, s.y))
1564 flags |= eTraversalNeeded;
1565 }
1566 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001567 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001568 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1569 if (layer->setLayer(s.z)) {
1570 mCurrentState.layersSortedByZ.removeAt(idx);
1571 mCurrentState.layersSortedByZ.add(layer);
1572 // we need traversal (state changed)
1573 // AND transaction (list changed)
1574 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575 }
1576 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001577 if (what & eSizeChanged) {
1578 if (layer->setSize(s.w, s.h)) {
1579 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001580 }
1581 }
1582 if (what & eAlphaChanged) {
1583 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1584 flags |= eTraversalNeeded;
1585 }
1586 if (what & eMatrixChanged) {
1587 if (layer->setMatrix(s.matrix))
1588 flags |= eTraversalNeeded;
1589 }
1590 if (what & eTransparentRegionChanged) {
1591 if (layer->setTransparentRegionHint(s.transparentRegion))
1592 flags |= eTraversalNeeded;
1593 }
1594 if (what & eVisibilityChanged) {
1595 if (layer->setFlags(s.flags, s.mask))
1596 flags |= eTraversalNeeded;
1597 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001598 if (what & eCropChanged) {
1599 if (layer->setCrop(s.crop))
1600 flags |= eTraversalNeeded;
1601 }
Mathias Agopian87855782012-07-24 21:41:09 -07001602 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001603 // NOTE: index needs to be calculated before we update the state
1604 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1605 if (layer->setLayerStack(s.layerStack)) {
1606 mCurrentState.layersSortedByZ.removeAt(idx);
1607 mCurrentState.layersSortedByZ.add(layer);
1608 // we need traversal (state changed)
1609 // AND transaction (list changed)
1610 flags |= eTransactionNeeded|eTraversalNeeded;
1611 }
Mathias Agopian87855782012-07-24 21:41:09 -07001612 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001614 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615}
1616
Mathias Agopianb60314a2012-04-10 22:09:54 -07001617// ---------------------------------------------------------------------------
1618
1619void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001620 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001621 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001622 getHwComposer().acquire();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001623 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001624 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625}
1626
Mathias Agopianb60314a2012-04-10 22:09:54 -07001627void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001628 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001629 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopianb60314a2012-04-10 22:09:54 -07001630 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001631 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001632 hw.releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001633 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001634 // from this point on, SF will stop drawing
1635 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636}
1637
Colin Cross8e533062012-06-07 13:17:52 -07001638void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001639 class MessageScreenAcquired : public MessageBase {
1640 SurfaceFlinger* flinger;
1641 public:
1642 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1643 virtual bool handler() {
1644 flinger->onScreenAcquired();
1645 return true;
1646 }
1647 };
1648 sp<MessageBase> msg = new MessageScreenAcquired(this);
1649 postMessageSync(msg);
1650}
1651
Colin Cross8e533062012-06-07 13:17:52 -07001652void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001653 class MessageScreenReleased : public MessageBase {
1654 SurfaceFlinger* flinger;
1655 public:
1656 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1657 virtual bool handler() {
1658 flinger->onScreenReleased();
1659 return true;
1660 }
1661 };
1662 sp<MessageBase> msg = new MessageScreenReleased(this);
1663 postMessageSync(msg);
1664}
1665
1666// ---------------------------------------------------------------------------
1667
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1669{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001670 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 char buffer[SIZE];
1672 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001673
1674 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 snprintf(buffer, SIZE, "Permission Denial: "
1676 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1677 IPCThreadState::self()->getCallingPid(),
1678 IPCThreadState::self()->getCallingUid());
1679 result.append(buffer);
1680 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001681 // Try to get the main lock, but don't insist if we can't
1682 // (this would indicate SF is stuck, but we want to be able to
1683 // print something in dumpsys).
1684 int retry = 3;
1685 while (mStateLock.tryLock()<0 && --retry>=0) {
1686 usleep(1000000);
1687 }
1688 const bool locked(retry >= 0);
1689 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001690 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001691 "SurfaceFlinger appears to be unresponsive, "
1692 "dumping anyways (no locks held)\n");
1693 result.append(buffer);
1694 }
1695
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001696 bool dumpAll = true;
1697 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001698 size_t numArgs = args.size();
1699 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001700 if ((index < numArgs) &&
1701 (args[index] == String16("--list"))) {
1702 index++;
1703 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001704 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001705 }
1706
1707 if ((index < numArgs) &&
1708 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001709 index++;
1710 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001711 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001712 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001713
1714 if ((index < numArgs) &&
1715 (args[index] == String16("--latency-clear"))) {
1716 index++;
1717 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001718 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001719 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001721
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001722 if (dumpAll) {
1723 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001724 }
1725
Mathias Agopian9795c422009-08-26 16:36:26 -07001726 if (locked) {
1727 mStateLock.unlock();
1728 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 }
1730 write(fd, result.string(), result.size());
1731 return NO_ERROR;
1732}
1733
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001734void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1735 String8& result, char* buffer, size_t SIZE) const
1736{
1737 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1738 const size_t count = currentLayers.size();
1739 for (size_t i=0 ; i<count ; i++) {
1740 const sp<LayerBase>& layer(currentLayers[i]);
1741 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1742 result.append(buffer);
1743 }
1744}
1745
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001746void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1747 String8& result, char* buffer, size_t SIZE) const
1748{
1749 String8 name;
1750 if (index < args.size()) {
1751 name = String8(args[index]);
1752 index++;
1753 }
1754
1755 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1756 const size_t count = currentLayers.size();
1757 for (size_t i=0 ; i<count ; i++) {
1758 const sp<LayerBase>& layer(currentLayers[i]);
1759 if (name.isEmpty()) {
1760 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1761 result.append(buffer);
1762 }
1763 if (name.isEmpty() || (name == layer->getName())) {
1764 layer->dumpStats(result, buffer, SIZE);
1765 }
1766 }
1767}
1768
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001769void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1770 String8& result, char* buffer, size_t SIZE) const
1771{
1772 String8 name;
1773 if (index < args.size()) {
1774 name = String8(args[index]);
1775 index++;
1776 }
1777
1778 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1779 const size_t count = currentLayers.size();
1780 for (size_t i=0 ; i<count ; i++) {
1781 const sp<LayerBase>& layer(currentLayers[i]);
1782 if (name.isEmpty() || (name == layer->getName())) {
1783 layer->clearStats();
1784 }
1785 }
1786}
1787
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001788void SurfaceFlinger::dumpAllLocked(
1789 String8& result, char* buffer, size_t SIZE) const
1790{
1791 // figure out if we're stuck somewhere
1792 const nsecs_t now = systemTime();
1793 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1794 const nsecs_t inTransaction(mDebugInTransaction);
1795 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1796 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1797
1798 /*
1799 * Dump the visible layer list
1800 */
1801 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1802 const size_t count = currentLayers.size();
1803 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1804 result.append(buffer);
1805 for (size_t i=0 ; i<count ; i++) {
1806 const sp<LayerBase>& layer(currentLayers[i]);
1807 layer->dump(result, buffer, SIZE);
1808 }
1809
1810 /*
1811 * Dump the layers in the purgatory
1812 */
1813
1814 const size_t purgatorySize = mLayerPurgatory.size();
1815 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1816 result.append(buffer);
1817 for (size_t i=0 ; i<purgatorySize ; i++) {
1818 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1819 layer->shortDump(result, buffer, SIZE);
1820 }
1821
1822 /*
1823 * Dump SurfaceFlinger global state
1824 */
1825
1826 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1827 result.append(buffer);
1828
Mathias Agopian888c8222012-08-04 21:10:38 -07001829 HWComposer& hwc(getHwComposer());
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001830 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001831 const GLExtensions& extensions(GLExtensions::getInstance());
1832 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1833 extensions.getVendor(),
1834 extensions.getRenderer(),
1835 extensions.getVersion());
1836 result.append(buffer);
1837
1838 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001839 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001840 result.append(buffer);
1841
1842 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1843 result.append(buffer);
1844
Mathias Agopian87baae12012-07-31 12:38:26 -07001845 hw.undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001846 snprintf(buffer, SIZE,
1847 " orientation=%d, canDraw=%d\n",
Mathias Agopian92a979a2012-08-02 18:32:23 -07001848 hw.getOrientation(), hw.canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001849 result.append(buffer);
1850 snprintf(buffer, SIZE,
1851 " last eglSwapBuffers() time: %f us\n"
1852 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001853 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001854 " refresh-rate : %f fps\n"
1855 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001856 " y-dpi : %f\n"
1857 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001858 mLastSwapBufferTime/1000.0,
1859 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001860 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001861 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001862 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001863 hw.getDpiY(),
1864 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001865 result.append(buffer);
1866
1867 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1868 inSwapBuffersDuration/1000.0);
1869 result.append(buffer);
1870
1871 snprintf(buffer, SIZE, " transaction time: %f us\n",
1872 inTransactionDuration/1000.0);
1873 result.append(buffer);
1874
1875 /*
1876 * VSYNC state
1877 */
1878 mEventThread->dump(result, buffer, SIZE);
1879
1880 /*
1881 * Dump HWComposer state
1882 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001883 snprintf(buffer, SIZE, "h/w composer state:\n");
1884 result.append(buffer);
1885 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1886 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1887 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1888 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001889 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001890
1891 /*
1892 * Dump gralloc state
1893 */
1894 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1895 alloc.dump(result);
1896 hw.dump(result);
1897}
1898
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899status_t SurfaceFlinger::onTransact(
1900 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1901{
1902 switch (code) {
1903 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001904 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001906 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001907 case BLANK:
1908 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909 {
1910 // codes that require permission check
1911 IPCThreadState* ipc = IPCThreadState::self();
1912 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001913 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001914 if ((uid != AID_GRAPHICS) &&
1915 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001916 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001917 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1918 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001920 break;
1921 }
1922 case CAPTURE_SCREEN:
1923 {
1924 // codes that require permission check
1925 IPCThreadState* ipc = IPCThreadState::self();
1926 const int pid = ipc->getCallingPid();
1927 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001928 if ((uid != AID_GRAPHICS) &&
1929 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001930 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001931 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1932 return PERMISSION_DENIED;
1933 }
1934 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 }
1936 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1939 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001940 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001941 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001942 IPCThreadState* ipc = IPCThreadState::self();
1943 const int pid = ipc->getCallingPid();
1944 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001945 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001946 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001947 return PERMISSION_DENIED;
1948 }
1949 int n;
1950 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001951 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001952 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953 return NO_ERROR;
1954 case 1002: // SHOW_UPDATES
1955 n = data.readInt32();
1956 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001957 invalidateHwcGeometry();
1958 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001961 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001962 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001964 case 1005:{ // force transaction
1965 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1966 return NO_ERROR;
1967 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001968 case 1006:{ // send empty update
1969 signalRefresh();
1970 return NO_ERROR;
1971 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001972 case 1008: // toggle use of hw composer
1973 n = data.readInt32();
1974 mDebugDisableHWC = n ? 1 : 0;
1975 invalidateHwcGeometry();
1976 repaintEverything();
1977 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001978 case 1009: // toggle use of transform hint
1979 n = data.readInt32();
1980 mDebugDisableTransformHint = n ? 1 : 0;
1981 invalidateHwcGeometry();
1982 repaintEverything();
1983 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001985 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986 reply->writeInt32(0);
1987 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001988 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001989 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001990 return NO_ERROR;
1991 case 1013: {
1992 Mutex::Autolock _l(mStateLock);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001993 const DisplayDevice& hw(getDefaultDisplayDevice());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001994 reply->writeInt32(hw.getPageFlipCount());
1995 }
1996 return NO_ERROR;
1997 }
1998 }
1999 return err;
2000}
2001
Mathias Agopian53331da2011-08-22 21:44:41 -07002002void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002003 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002004 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002005}
2006
Mathias Agopian59119e62010-10-11 12:37:43 -07002007// ---------------------------------------------------------------------------
2008
Mathias Agopian118d0242011-10-13 16:02:48 -07002009status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2010 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2011{
2012 Mutex::Autolock _l(mStateLock);
2013 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2014}
2015
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002016status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2017 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002018{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002019 ATRACE_CALL();
2020
Mathias Agopian59119e62010-10-11 12:37:43 -07002021 if (!GLExtensions::getInstance().haveFramebufferObject())
2022 return INVALID_OPERATION;
2023
2024 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002025 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002026 const uint32_t hw_w = hw.getWidth();
2027 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002028 GLfloat u = 1;
2029 GLfloat v = 1;
2030
2031 // make sure to clear all GL error flags
2032 while ( glGetError() != GL_NO_ERROR ) ;
2033
2034 // create a FBO
2035 GLuint name, tname;
2036 glGenTextures(1, &tname);
2037 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002038 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2039 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002040 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2041 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002042 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002043 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002044 GLint tw = (2 << (31 - clz(hw_w)));
2045 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002046 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2047 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002048 u = GLfloat(hw_w) / tw;
2049 v = GLfloat(hw_h) / th;
2050 }
2051 glGenFramebuffersOES(1, &name);
2052 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2054 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002055
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002056 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002057 glDisable(GL_TEXTURE_EXTERNAL_OES);
2058 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002059 glClearColor(0,0,0,1);
2060 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002061 glMatrixMode(GL_MODELVIEW);
2062 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002063 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 const size_t count = layers.size();
2065 for (size_t i=0 ; i<count ; ++i) {
2066 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002067 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002068 }
2069
Mathias Agopian118d0242011-10-13 16:02:48 -07002070 hw.compositionComplete();
2071
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 // back to main framebuffer
2073 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002074 glDeleteFramebuffersOES(1, &name);
2075
2076 *textureName = tname;
2077 *uOut = u;
2078 *vOut = v;
2079 return NO_ERROR;
2080}
2081
2082// ---------------------------------------------------------------------------
2083
Mathias Agopian74c40c02010-09-29 13:02:36 -07002084status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2085 sp<IMemoryHeap>* heap,
2086 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002087 uint32_t sw, uint32_t sh,
2088 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002089{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002090 ATRACE_CALL();
2091
Mathias Agopian74c40c02010-09-29 13:02:36 -07002092 status_t result = PERMISSION_DENIED;
2093
2094 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002095 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002096 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002097 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002098
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002099 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002100 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002101 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002102
2103 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002104 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002105 const uint32_t hw_w = hw.getWidth();
2106 const uint32_t hw_h = hw.getHeight();
2107
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002108 // if we have secure windows on this display, never allow the screen capture
2109 if (hw.getSecureLayerVisible()) {
2110 return PERMISSION_DENIED;
2111 }
2112
2113 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002114 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002115 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002116
2117 sw = (!sw) ? hw_w : sw;
2118 sh = (!sh) ? hw_h : sh;
2119 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002120 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002121
Steve Block9d453682011-12-20 16:23:08 +00002122 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002123 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002124
Mathias Agopian74c40c02010-09-29 13:02:36 -07002125 // make sure to clear all GL error flags
2126 while ( glGetError() != GL_NO_ERROR ) ;
2127
2128 // create a FBO
2129 GLuint name, tname;
2130 glGenRenderbuffersOES(1, &tname);
2131 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2132 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002133
Mathias Agopian74c40c02010-09-29 13:02:36 -07002134 glGenFramebuffersOES(1, &name);
2135 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2136 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2137 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2138
2139 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002140
Mathias Agopian74c40c02010-09-29 13:02:36 -07002141 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2142
2143 // invert everything, b/c glReadPixel() below will invert the FB
2144 glViewport(0, 0, sw, sh);
2145 glMatrixMode(GL_PROJECTION);
2146 glPushMatrix();
2147 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002148 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002149 glMatrixMode(GL_MODELVIEW);
2150
2151 // redraw the screen entirely...
2152 glClearColor(0,0,0,1);
2153 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002154
Jamie Gennis9575f602011-10-07 14:51:16 -07002155 const LayerVector& layers(mDrawingState.layersSortedByZ);
2156 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002157 for (size_t i=0 ; i<count ; ++i) {
2158 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002159 const uint32_t flags = layer->drawingState().flags;
2160 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2161 const uint32_t z = layer->drawingState().z;
2162 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002163 if (filtering) layer->setFiltering(true);
2164 layer->draw(hw);
2165 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002166 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002167 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002168 }
2169
Mathias Agopian74c40c02010-09-29 13:02:36 -07002170 // check for errors and return screen capture
2171 if (glGetError() != GL_NO_ERROR) {
2172 // error while rendering
2173 result = INVALID_OPERATION;
2174 } else {
2175 // allocate shared memory large enough to hold the
2176 // screen capture
2177 sp<MemoryHeapBase> base(
2178 new MemoryHeapBase(size, 0, "screen-capture") );
2179 void* const ptr = base->getBase();
2180 if (ptr) {
2181 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002182 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002183 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2184 if (glGetError() == GL_NO_ERROR) {
2185 *heap = base;
2186 *w = sw;
2187 *h = sh;
2188 *f = PIXEL_FORMAT_RGBA_8888;
2189 result = NO_ERROR;
2190 }
2191 } else {
2192 result = NO_MEMORY;
2193 }
2194 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002195 glViewport(0, 0, hw_w, hw_h);
2196 glMatrixMode(GL_PROJECTION);
2197 glPopMatrix();
2198 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002199 } else {
2200 result = BAD_VALUE;
2201 }
2202
2203 // release FBO resources
2204 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2205 glDeleteRenderbuffersOES(1, &tname);
2206 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002207
2208 hw.compositionComplete();
2209
Steve Block9d453682011-12-20 16:23:08 +00002210 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002211
Mathias Agopian74c40c02010-09-29 13:02:36 -07002212 return result;
2213}
2214
2215
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002216status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2217 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002218 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002219 uint32_t sw, uint32_t sh,
2220 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002221{
2222 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002223 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002224 return BAD_VALUE;
2225
2226 if (!GLExtensions::getInstance().haveFramebufferObject())
2227 return INVALID_OPERATION;
2228
2229 class MessageCaptureScreen : public MessageBase {
2230 SurfaceFlinger* flinger;
2231 DisplayID dpy;
2232 sp<IMemoryHeap>* heap;
2233 uint32_t* w;
2234 uint32_t* h;
2235 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236 uint32_t sw;
2237 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002238 uint32_t minLayerZ;
2239 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002240 status_t result;
2241 public:
2242 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002244 uint32_t sw, uint32_t sh,
2245 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002246 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002247 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2248 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2249 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002250 {
2251 }
2252 status_t getResult() const {
2253 return result;
2254 }
2255 virtual bool handler() {
2256 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002257 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002258 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002259 return true;
2260 }
2261 };
2262
2263 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002264 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002265 status_t res = postMessageSync(msg);
2266 if (res == NO_ERROR) {
2267 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2268 }
2269 return res;
2270}
2271
2272// ---------------------------------------------------------------------------
2273
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002274SurfaceFlinger::LayerVector::LayerVector() {
2275}
2276
2277SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2278 : SortedVector<sp<LayerBase> >(rhs) {
2279}
2280
2281int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2282 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002283{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002284 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002285 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2286 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002287
2288 uint32_t ls = l->currentState().layerStack;
2289 uint32_t rs = r->currentState().layerStack;
2290 if (ls != rs)
2291 return ls - rs;
2292
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002293 uint32_t lz = l->currentState().z;
2294 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002295 if (lz != rz)
2296 return lz - rz;
2297
2298 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002299}
2300
2301// ---------------------------------------------------------------------------
2302
Mathias Agopian92a979a2012-08-02 18:32:23 -07002303SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2304 : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002305}
2306
2307// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308
Jamie Gennis9a78c902011-01-12 18:30:40 -08002309GraphicBufferAlloc::GraphicBufferAlloc() {}
2310
2311GraphicBufferAlloc::~GraphicBufferAlloc() {}
2312
2313sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002314 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002315 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2316 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002317 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002318 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002319 if (err == NO_MEMORY) {
2320 GraphicBuffer::dumpAllocationsToSystemLog();
2321 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002322 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002323 "failed (%s), handle=%p",
2324 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002325 return 0;
2326 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002327 return graphicBuffer;
2328}
2329
Jamie Gennis9a78c902011-01-12 18:30:40 -08002330// ---------------------------------------------------------------------------
2331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332}; // namespace android