blob: 37757346754b768ecd634e3f25bf1571e8da00d0 [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),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070099 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Steve Blocka19954a2012-01-04 20:05:49 +0000101 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108
Colin Cross3854ed52012-03-23 14:17:18 -0700109#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110 property_get("debug.sf.ddms", value, "0");
111 mDebugDDMS = atoi(value);
112 if (mDebugDDMS) {
113 DdmConnection::start(getServiceName());
114 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700115#else
116#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700117#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118
Steve Blocka19954a2012-01-04 20:05:49 +0000119 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000120 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800123void SurfaceFlinger::onFirstRef()
124{
125 mEventQueue.init(this);
126
127 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
128
129 // Wait for the main thread to be done with its initialization
130 mReadyToRunBarrier.wait();
131}
132
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134SurfaceFlinger::~SurfaceFlinger()
135{
Mathias Agopiana4912602012-07-12 14:25:33 -0700136 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
137 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141void SurfaceFlinger::binderDied(const wp<IBinder>& who)
142{
143 // the window manager died on us. prepare its eulogy.
144
145 // reset screen orientation
146 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700147 Vector<DisplayState> displays;
148 DisplayState d;
149 d.orientation = eOrientationDefault;
150 displays.add(d);
151 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152
153 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700154 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800155}
156
Mathias Agopian7e27f052010-05-28 14:22:23 -0700157sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Mathias Agopian96f08192010-06-02 23:28:45 -0700159 sp<ISurfaceComposerClient> bclient;
160 sp<Client> client(new Client(this));
161 status_t err = client->initCheck();
162 if (err == NO_ERROR) {
163 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 return bclient;
166}
167
Jamie Gennis9a78c902011-01-12 18:30:40 -0800168sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
169{
170 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
171 return gba;
172}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174void SurfaceFlinger::bootFinished()
175{
176 const nsecs_t now = systemTime();
177 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000178 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700180
181 // wait patiently for the window manager death
182 const String16 name("window");
183 sp<IBinder> window(defaultServiceManager()->getService(name));
184 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700185 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700186 }
187
188 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700189 // formerly we would just kill the process, but we now ask it to exit so it
190 // can choose where to stop the animation.
191 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700194void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
195 class MessageDestroyGLTexture : public MessageBase {
196 GLuint texture;
197 public:
198 MessageDestroyGLTexture(GLuint texture)
199 : texture(texture) {
200 }
201 virtual bool handler() {
202 glDeleteTextures(1, &texture);
203 return true;
204 }
205 };
206 postMessageAsync(new MessageDestroyGLTexture(texture));
207}
208
Mathias Agopiana4912602012-07-12 14:25:33 -0700209status_t SurfaceFlinger::selectConfigForPixelFormat(
210 EGLDisplay dpy,
211 EGLint const* attrs,
212 PixelFormat format,
213 EGLConfig* outConfig)
214{
215 EGLConfig config = NULL;
216 EGLint numConfigs = -1, n=0;
217 eglGetConfigs(dpy, NULL, 0, &numConfigs);
218 EGLConfig* const configs = new EGLConfig[numConfigs];
219 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
220 for (int i=0 ; i<n ; i++) {
221 EGLint nativeVisualId = 0;
222 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
223 if (nativeVisualId>0 && format == nativeVisualId) {
224 *outConfig = configs[i];
225 delete [] configs;
226 return NO_ERROR;
227 }
228 }
229 delete [] configs;
230 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
Mathias Agopiana4912602012-07-12 14:25:33 -0700233EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
234 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
235 // it is to be used with WIFI displays
236 EGLConfig config;
237 EGLint dummy;
238 status_t err;
239 EGLint attribs[] = {
240 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
241 EGL_RECORDABLE_ANDROID, EGL_TRUE,
242 EGL_NONE
243 };
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 if (err) {
246 // maybe we failed because of EGL_RECORDABLE_ANDROID
247 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
248 attribs[2] = EGL_NONE;
249 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
250 }
251 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
252 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
253 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
254 }
255 return config;
256}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
Mathias Agopiana4912602012-07-12 14:25:33 -0700258EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
259 // Also create our EGLContext
260 EGLint contextAttributes[] = {
261#ifdef EGL_IMG_context_priority
262#ifdef HAS_CONTEXT_PRIORITY
263#warning "using EGL_IMG_context_priority"
264 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
265#endif
266#endif
267 EGL_NONE, EGL_NONE
268 };
269 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
270 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
271 return ctxt;
272}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
Mathias Agopiana4912602012-07-12 14:25:33 -0700274void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
275 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
276 if (!result) {
277 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
278 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 }
280
Mathias Agopiana4912602012-07-12 14:25:33 -0700281 GLExtensions& extensions(GLExtensions::getInstance());
282 extensions.initWithGLStrings(
283 glGetString(GL_VENDOR),
284 glGetString(GL_RENDERER),
285 glGetString(GL_VERSION),
286 glGetString(GL_EXTENSIONS),
287 eglQueryString(display, EGL_VENDOR),
288 eglQueryString(display, EGL_VERSION),
289 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700290
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 EGLint w, h;
292 eglQuerySurface(display, surface, EGL_WIDTH, &w);
293 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294
Mathias Agopiana4912602012-07-12 14:25:33 -0700295 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
296 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700299 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 glShadeModel(GL_FLAT);
302 glDisable(GL_DITHER);
303 glDisable(GL_CULL_FACE);
304
Mathias Agopiana4912602012-07-12 14:25:33 -0700305 struct pack565 {
306 inline uint16_t operator() (int r, int g, int b) const {
307 return (r<<11)|(g<<5)|b;
308 }
309 } pack565;
310
Jamie Gennis9575f602011-10-07 14:51:16 -0700311 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
312 glGenTextures(1, &mProtectedTexName);
313 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
314 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
315 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
316 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
317 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
318 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
319 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320
321 glViewport(0, 0, w, h);
322 glMatrixMode(GL_PROJECTION);
323 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700324 // put the origin in the left-bottom corner
325 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 -0800326
Mathias Agopiana4912602012-07-12 14:25:33 -0700327 // print some debugging info
328 EGLint r,g,b,a;
329 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
330 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
331 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
332 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
333 ALOGI("EGL informations:");
334 ALOGI("vendor : %s", extensions.getEglVendor());
335 ALOGI("version : %s", extensions.getEglVersion());
336 ALOGI("extensions: %s", extensions.getEglExtension());
337 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
338 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
339 ALOGI("OpenGL ES informations:");
340 ALOGI("vendor : %s", extensions.getVendor());
341 ALOGI("renderer : %s", extensions.getRenderer());
342 ALOGI("version : %s", extensions.getVersion());
343 ALOGI("extensions: %s", extensions.getExtension());
344 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
345 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
346}
347
Mathias Agopiana4912602012-07-12 14:25:33 -0700348status_t SurfaceFlinger::readyToRun()
349{
350 ALOGI( "SurfaceFlinger's main thread ready to run. "
351 "Initializing graphics H/W...");
352
Mathias Agopiana4912602012-07-12 14:25:33 -0700353 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700354 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
355 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700356
357 // Initialize the main display
358 // create native window to main display
359 sp<FramebufferSurface> anw = FramebufferSurface::create();
360 ANativeWindow* const window = anw.get();
361 if (!window) {
362 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
363 exit(0);
364 }
365
366 // initialize the config and context
367 int format;
368 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700369 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
370 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700371
372 // initialize our main display hardware
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700373 DisplayDevice* const hw = new DisplayDevice(this, 0, anw, mEGLConfig);
374 mDisplayDevices[0] = hw;
Mathias Agopiana4912602012-07-12 14:25:33 -0700375
376 // initialize OpenGL ES
377 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700378 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800379
Mathias Agopian028508c2012-07-25 21:12:12 -0700380 // start the EventThread
381 mEventThread = new EventThread(this);
382 mEventQueue.setEventThread(mEventThread);
383
Mathias Agopian86303202012-07-24 22:46:10 -0700384 // initialize the H/W composer
385 mHwc = new HWComposer(this,
386 *static_cast<HWComposer::EventHandler *>(this),
387 hw->getRefreshPeriod());
Mathias Agopian86303202012-07-24 22:46:10 -0700388
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800390 mReadyToRunBarrier.open();
391
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700392 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700393 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 return NO_ERROR;
396}
397
Mathias Agopiana67e4182012-06-19 17:26:12 -0700398void SurfaceFlinger::startBootAnim() {
399 // start boot animation
400 property_set("service.bootanim.exit", "0");
401 property_set("ctl.start", "bootanim");
402}
403
Mathias Agopiana4912602012-07-12 14:25:33 -0700404uint32_t SurfaceFlinger::getMaxTextureSize() const {
405 return mMaxTextureSize;
406}
407
408uint32_t SurfaceFlinger::getMaxViewportDims() const {
409 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
410 mMaxViewportDims[0] : mMaxViewportDims[1];
411}
412
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800414
Jamie Gennis582270d2011-08-17 18:19:00 -0700415bool SurfaceFlinger::authenticateSurfaceTexture(
416 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800417 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700418 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800419
420 // Check the visible layer list for the ISurface
421 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
422 size_t count = currentLayers.size();
423 for (size_t i=0 ; i<count ; i++) {
424 const sp<LayerBase>& layer(currentLayers[i]);
425 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700426 if (lbc != NULL) {
427 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
428 if (lbcBinder == surfaceTextureBinder) {
429 return true;
430 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800431 }
432 }
433
434 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700435 // SurfaceTexture gets destroyed before all the clients are done using it,
436 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800437 // will instead fail later on when the client tries to use the surface,
438 // which should be reported as "surface XYZ returned an -ENODEV". The
439 // purgatorized layers are no less authentic than the visible ones, so this
440 // should not cause any harm.
441 size_t purgatorySize = mLayerPurgatory.size();
442 for (size_t i=0 ; i<purgatorySize ; i++) {
443 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
444 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700445 if (lbc != NULL) {
446 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
447 if (lbcBinder == surfaceTextureBinder) {
448 return true;
449 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800450 }
451 }
452
453 return false;
454}
455
Mathias Agopianc666cae2012-07-25 18:56:13 -0700456status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
457 if (uint32_t(dpy) >= 2) {
458 return BAD_INDEX;
459 }
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700460 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopianc666cae2012-07-25 18:56:13 -0700461 return hw.getInfo(info);
462}
463
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800464// ----------------------------------------------------------------------------
465
466sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800467 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700468}
469
Mathias Agopian3094df32012-06-18 18:06:45 -0700470void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700471 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian3094df32012-06-18 18:06:45 -0700472 EGLSurface result = EGL_NO_SURFACE;
473 EGLSurface old_surface = EGL_NO_SURFACE;
474 sp<SurfaceTextureClient> stc;
475
476 if (display != NULL) {
477 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700478 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700479 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700480 ALOGE_IF(result == EGL_NO_SURFACE,
481 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
482 display.get());
483 }
484
485 { // scope for the lock
486 Mutex::Autolock _l(mStateLock);
487 old_surface = mExternalDisplaySurface;
488 mExternalDisplayNativeWindow = stc;
489 mExternalDisplaySurface = result;
490 ALOGD("mExternalDisplaySurface = %p", result);
491 }
492
493 if (old_surface != EGL_NO_SURFACE) {
494 // Note: EGL allows to destroy an object while its current
495 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700496 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700497 }
498}
499
500EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
501 Mutex::Autolock _l(mStateLock);
502 return mExternalDisplaySurface;
503}
504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800506
507void SurfaceFlinger::waitForEvent() {
508 mEventQueue.waitMessage();
509}
510
511void SurfaceFlinger::signalTransaction() {
512 mEventQueue.invalidate();
513}
514
515void SurfaceFlinger::signalLayerUpdate() {
516 mEventQueue.invalidate();
517}
518
519void SurfaceFlinger::signalRefresh() {
520 mEventQueue.refresh();
521}
522
523status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
524 nsecs_t reltime, uint32_t flags) {
525 return mEventQueue.postMessage(msg, reltime);
526}
527
528status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
529 nsecs_t reltime, uint32_t flags) {
530 status_t res = mEventQueue.postMessage(msg, reltime);
531 if (res == NO_ERROR) {
532 msg->wait();
533 }
534 return res;
535}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536
Mathias Agopian4fec8732012-06-29 14:12:52 -0700537bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 return true;
540}
541
Mathias Agopian86303202012-07-24 22:46:10 -0700542void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700543 mEventThread->onVSyncReceived(dpy, timestamp);
544}
545
546void SurfaceFlinger::eventControl(int event, int enabled) {
547 getHwComposer().eventControl(event, enabled);
548}
549
Mathias Agopian4fec8732012-06-29 14:12:52 -0700550void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800551 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800552 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700553 case MessageQueue::INVALIDATE:
554 handleMessageTransaction();
555 handleMessageInvalidate();
556 signalRefresh();
557 break;
558 case MessageQueue::REFRESH:
559 handleMessageRefresh();
560 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800561 }
562}
563
Mathias Agopian4fec8732012-06-29 14:12:52 -0700564void SurfaceFlinger::handleMessageTransaction() {
565 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
566 uint32_t transactionFlags = peekTransactionFlags(mask);
567 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700568 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700569 }
570}
571
572void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700573 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700574}
575
576void SurfaceFlinger::handleMessageRefresh() {
577 handleRefresh();
578
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700579 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700580 mVisibleRegionsDirty = false;
581 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700582
583 /*
584 * rebuild the visible layer list per screen
585 */
586
Mathias Agopian87baae12012-07-31 12:38:26 -0700587 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700588 for (int dpy=0 ; dpy<1 ; dpy++) { // TODO: iterate through all displays
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700589 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(dpy)));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700590
Mathias Agopian87baae12012-07-31 12:38:26 -0700591 Region opaqueRegion;
592 Region dirtyRegion;
593 computeVisibleRegions(currentLayers,
594 hw.getLayerStack(), dirtyRegion, opaqueRegion);
595 hw.dirtyRegion.orSelf(dirtyRegion);
596
597 Vector< sp<LayerBase> > layersSortedByZ;
598 const size_t count = currentLayers.size();
599 for (size_t i=0 ; i<count ; i++) {
600 const Layer::State& s(currentLayers[i]->drawingState());
601 if (s.layerStack == hw.getLayerStack()) {
602 if (!currentLayers[i]->visibleRegion.isEmpty()) {
603 layersSortedByZ.add(currentLayers[i]);
604 }
605 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700606 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700607 hw.setVisibleLayersSortedByZ(layersSortedByZ);
608 hw.undefinedRegion.set(hw.getBounds());
609 hw.undefinedRegion.subtractSelf(hw.getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700610 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700611 }
612
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700613 HWComposer& hwc(getHwComposer());
614 if (hwc.initCheck() == NO_ERROR) {
615 // build the h/w work list
616 const bool workListsDirty = mHwWorkListDirty;
617 mHwWorkListDirty = false;
618 for (int dpy=0 ; dpy<1 ; dpy++) { // TODO: iterate through all displays
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700619 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(dpy)));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700620 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
621 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700622
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700623 hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
624
625 HWComposer::LayerListIterator cur = hwc.begin();
626 const HWComposer::LayerListIterator end = hwc.end();
627 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
628 const sp<LayerBase>& layer(currentLayers[i]);
629
630 if (CC_UNLIKELY(workListsDirty)) {
631 layer->setGeometry(hw, *cur);
632 if (mDebugDisableHWC || mDebugRegion) {
633 cur->setSkip(true);
634 }
635 }
636
637 /*
638 * update the per-frame h/w composer data for each layer
639 * and build the transparent region of the FB
640 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700641 layer->setPerFrameData(hw, *cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700642 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700643 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700644 status_t err = hwc.prepare();
645 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
646 }
647
648 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
649 for (int dpy=0 ; dpy<1 ; dpy++) { // TODO: iterate through all displays
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700650 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(dpy)));
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651
Mathias Agopian87baae12012-07-31 12:38:26 -0700652 // transform the dirty region into this screen's coordinate space
653 const Transform& planeTransform(hw.getTransform());
654 Region dirtyRegion;
655 if (repaintEverything) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700656 dirtyRegion.set(hw.bounds());
657 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -0700658 dirtyRegion = planeTransform.transform(hw.dirtyRegion);
659 dirtyRegion.andSelf(hw.bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700660 }
661 hw.dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700662
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700663 if (!dirtyRegion.isEmpty()) {
664 if (hw.canDraw()) {
665 // repaint the framebuffer (if needed)
666 handleRepaint(hw, dirtyRegion);
667 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700668 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700669 // inform the h/w that we're done compositing
670 hw.compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700671 }
672
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700673 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700674
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700675
676#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700677 // render to the external display if we have one
678 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
679 if (externalDisplaySurface != EGL_NO_SURFACE) {
680 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
681 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
682 externalDisplaySurface, externalDisplaySurface,
683 eglGetCurrentContext());
684
685 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
686
687 if (success) {
688 // redraw the screen entirely...
689 glDisable(GL_TEXTURE_EXTERNAL_OES);
690 glDisable(GL_TEXTURE_2D);
691 glClearColor(0,0,0,1);
692 glClear(GL_COLOR_BUFFER_BIT);
693 glMatrixMode(GL_MODELVIEW);
694 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700695
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700696 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(0)));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700697 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700698 const size_t count = layers.size();
699 for (size_t i=0 ; i<count ; ++i) {
700 const sp<LayerBase>& layer(layers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700701 layer->drawForScreenShot(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700702 }
703
704 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
705 ALOGE_IF(!success, "external display eglSwapBuffers failed");
706
707 hw.compositionComplete();
708 }
709
710 success = eglMakeCurrent(eglGetCurrentDisplay(),
711 cur, cur, eglGetCurrentContext());
712
713 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
714 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700715#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700716
717}
718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719void SurfaceFlinger::postFramebuffer()
720{
Mathias Agopian841cde52012-03-01 15:44:37 -0800721 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800722
Mathias Agopiana44b0412011-10-16 18:46:35 -0700723 const nsecs_t now = systemTime();
724 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700725
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700726 HWComposer& hwc(getHwComposer());
727
728 for (int dpy=0 ; dpy<1 ; dpy++) { // TODO: iterate through all displays
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700729 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(dpy)));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700730 if (hwc.initCheck() == NO_ERROR) {
731 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
732 const size_t count = currentLayers.size();
733 HWComposer::LayerListIterator cur = hwc.begin();
734 const HWComposer::LayerListIterator end = hwc.end();
735 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
736 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700737 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700738 }
739 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700740 hw.flip(hw.swapRegion);
741 hw.swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700742 }
743
Jesse Hallef194142012-06-14 14:45:17 -0700744 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700745 // FIXME: eventually commit() won't take arguments
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700746 hwc.commit(mEGLDisplay, getDefaultDisplayDevice().getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700747 }
748
749 for (int dpy=0 ; dpy<1 ; dpy++) { // TODO: iterate through all displays
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700750 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(dpy)));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700751 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
752 const size_t count = currentLayers.size();
753 if (hwc.initCheck() == NO_ERROR) {
754 HWComposer::LayerListIterator cur = hwc.begin();
755 const HWComposer::LayerListIterator end = hwc.end();
756 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700757 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700758 }
759 } else {
760 eglSwapBuffers(mEGLDisplay, hw.getEGLSurface());
761 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700762 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700763 }
Jesse Hallef194142012-06-14 14:45:17 -0700764 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700765
766 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800767 }
768
Mathias Agopiana44b0412011-10-16 18:46:35 -0700769 mLastSwapBufferTime = systemTime() - now;
770 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771}
772
Mathias Agopian87baae12012-07-31 12:38:26 -0700773void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774{
Mathias Agopian841cde52012-03-01 15:44:37 -0800775 ATRACE_CALL();
776
Mathias Agopianca4d3602011-05-19 15:38:14 -0700777 Mutex::Autolock _l(mStateLock);
778 const nsecs_t now = systemTime();
779 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780
Mathias Agopianca4d3602011-05-19 15:38:14 -0700781 // Here we're guaranteed that some transaction flags are set
782 // so we can call handleTransactionLocked() unconditionally.
783 // We call getTransactionFlags(), which will also clear the flags,
784 // with mStateLock held to guarantee that mCurrentState won't change
785 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700786
Mathias Agopianca4d3602011-05-19 15:38:14 -0700787 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
788 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700789 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700790
Mathias Agopianca4d3602011-05-19 15:38:14 -0700791 mLastTransactionTime = systemTime() - now;
792 mDebugInTransaction = 0;
793 invalidateHwcGeometry();
794 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700795}
796
Mathias Agopian87baae12012-07-31 12:38:26 -0700797void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700798{
799 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 const size_t count = currentLayers.size();
801
802 /*
803 * Traversal of the children
804 * (perform the transaction for each of them if needed)
805 */
806
807 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
808 if (layersNeedTransaction) {
809 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700810 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
812 if (!trFlags) continue;
813
814 const uint32_t flags = layer->doTransaction(0);
815 if (flags & Layer::eVisibleRegion)
816 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 }
818 }
819
820 /*
821 * Perform our own transaction if needed
822 */
823
824 if (transactionFlags & eTransactionNeeded) {
825 if (mCurrentState.orientation != mDrawingState.orientation) {
826 // the orientation has changed, recompute all visible regions
827 // and invalidate everything.
828
Mathias Agopian87baae12012-07-31 12:38:26 -0700829 const int dpy = 0; // FIXME: should be a parameter
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -0700830 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(dpy)));
Mathias Agopian98a121a2012-07-24 21:08:59 -0700831 hw.setOrientation(mCurrentState.orientation);
Mathias Agopian87baae12012-07-31 12:38:26 -0700832 hw.dirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833
834 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835 }
836
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700837 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
838 // layers have been added
839 mVisibleRegionsDirty = true;
840 }
841
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 // some layers might have been removed, so
843 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700844 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700845 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700847 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700848 const size_t count = previousLayers.size();
849 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700850 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700851 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700852 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700853 // TODO: we could traverse the tree from front to back and
854 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700855 // TODO: we could cache the transformed region
856 Layer::State front(layer->drawingState());
857 Region visibleReg = front.transform.transform(
858 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700859 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700860 }
861 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 }
864
865 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700866}
867
868void SurfaceFlinger::commitTransaction()
869{
870 if (!mLayersPendingRemoval.isEmpty()) {
871 // Notify removed layers now that they can't be drawn from
872 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
873 mLayersPendingRemoval[i]->onRemoved();
874 }
875 mLayersPendingRemoval.clear();
876 }
877
878 mDrawingState = mCurrentState;
879 mTransationPending = false;
880 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881}
882
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700884 const LayerVector& currentLayers, uint32_t layerStack,
885 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886{
Mathias Agopian841cde52012-03-01 15:44:37 -0800887 ATRACE_CALL();
888
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 Region aboveOpaqueLayers;
890 Region aboveCoveredLayers;
891 Region dirty;
892
Mathias Agopian87baae12012-07-31 12:38:26 -0700893 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894
895 size_t i = currentLayers.size();
896 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700897 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898
899 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700900 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901
Mathias Agopian87baae12012-07-31 12:38:26 -0700902 // only consider the layers on the given later stack
903 if (s.layerStack != layerStack)
904 continue;
905
Mathias Agopianab028732010-03-16 16:41:46 -0700906 /*
907 * opaqueRegion: area of a surface that is fully opaque.
908 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700910
911 /*
912 * visibleRegion: area of a surface that is visible on screen
913 * and not fully transparent. This is essentially the layer's
914 * footprint minus the opaque regions above it.
915 * Areas covered by a translucent surface are considered visible.
916 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700918
919 /*
920 * coveredRegion: area of a surface that is covered by all
921 * visible regions above it (which includes the translucent areas).
922 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700924
925
926 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800927 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700928 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700929 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700931 if (!visibleRegion.isEmpty()) {
932 // Remove the transparent area from the visible region
933 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700934 Region transparentRegionScreen;
935 const Transform tr(s.transform);
936 if (tr.transformed()) {
937 if (tr.preserveRects()) {
938 // transform the transparent region
939 transparentRegionScreen = tr.transform(s.transparentRegion);
940 } else {
941 // transformation too complex, can't do the
942 // transparent region optimization.
943 transparentRegionScreen.clear();
944 }
945 } else {
946 transparentRegionScreen = s.transparentRegion;
947 }
948 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700949 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950
Mathias Agopianab028732010-03-16 16:41:46 -0700951 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700952 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700953 if (s.alpha==255 && !translucent &&
954 ((layerOrientation & Transform::ROT_INVALID) == false)) {
955 // the opaque region is the layer's footprint
956 opaqueRegion = visibleRegion;
957 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 }
959 }
960
Mathias Agopianab028732010-03-16 16:41:46 -0700961 // Clip the covered region to the visible region
962 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
963
964 // Update aboveCoveredLayers for next (lower) layer
965 aboveCoveredLayers.orSelf(visibleRegion);
966
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 // subtract the opaque region covered by the layers above us
968 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969
970 // compute this layer's dirty region
971 if (layer->contentDirty) {
972 // we need to invalidate the whole region
973 dirty = visibleRegion;
974 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700975 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 layer->contentDirty = false;
977 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700978 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700979 * the exposed region consists of two components:
980 * 1) what's VISIBLE now and was COVERED before
981 * 2) what's EXPOSED now less what was EXPOSED before
982 *
983 * note that (1) is conservative, we start with the whole
984 * visible region but only keep what used to be covered by
985 * something -- which mean it may have been exposed.
986 *
987 * (2) handles areas that were not covered by anything but got
988 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700989 */
Mathias Agopianab028732010-03-16 16:41:46 -0700990 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700991 const Region oldVisibleRegion = layer->visibleRegion;
992 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700993 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
994 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 }
996 dirty.subtractSelf(aboveOpaqueLayers);
997
998 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -0700999 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000
Mathias Agopianab028732010-03-16 16:41:46 -07001001 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001003
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 // Store the visible region is screen space
1005 layer->setVisibleRegion(visibleRegion);
1006 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 }
1008
Mathias Agopian87baae12012-07-31 12:38:26 -07001009 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010}
1011
Mathias Agopian87baae12012-07-31 12:38:26 -07001012void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1013 const Region& dirty) {
1014 // FIXME: update the dirty region of all displays
1015 // presenting this layer's layer stack.
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001016 DisplayDevice& hw(const_cast<DisplayDevice&>(getDisplayDevice(0)));
Mathias Agopian87baae12012-07-31 12:38:26 -07001017 hw.dirtyRegion.orSelf(dirty);
1018}
1019
1020void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001022 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001023 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001025 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001026
Mathias Agopian4fec8732012-06-29 14:12:52 -07001027 bool visibleRegions = false;
1028 const size_t count = currentLayers.size();
1029 sp<LayerBase> const* layers = currentLayers.array();
1030 for (size_t i=0 ; i<count ; i++) {
1031 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001032 const Region dirty(layer->latchBuffer(visibleRegions));
1033 Layer::State s(layer->drawingState());
1034 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001035 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001036
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001037 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
Mathias Agopianad456f92011-01-13 17:53:01 -08001040void SurfaceFlinger::invalidateHwcGeometry()
1041{
1042 mHwWorkListDirty = true;
1043}
1044
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001045void SurfaceFlinger::handleRefresh()
1046{
1047 bool needInvalidate = false;
1048 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1049 const size_t count = currentLayers.size();
1050 for (size_t i=0 ; i<count ; i++) {
1051 const sp<LayerBase>& layer(currentLayers[i]);
1052 if (layer->onPreComposition()) {
1053 needInvalidate = true;
1054 }
1055 }
1056 if (needInvalidate) {
1057 signalLayerUpdate();
1058 }
1059}
1060
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001061void SurfaceFlinger::handleRepaint(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001062 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063{
Mathias Agopian841cde52012-03-01 15:44:37 -08001064 ATRACE_CALL();
1065
Mathias Agopian87baae12012-07-31 12:38:26 -07001066 Region dirtyRegion(inDirtyRegion);
1067
Mathias Agopianb8a55602009-06-26 19:06:36 -07001068 // compute the invalid region
Mathias Agopian87baae12012-07-31 12:38:26 -07001069 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070
Glenn Kasten99ed2242011-12-15 09:51:17 -08001071 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001072 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 }
1074
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 uint32_t flags = hw.getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001076 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001077 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1078 // takes a rectangle, we must make sure to update that whole
1079 // rectangle in that case
Mathias Agopian87baae12012-07-31 12:38:26 -07001080 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001081 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001082 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001083 // We need to redraw the rectangle that will be updated
1084 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001085 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001086 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian87baae12012-07-31 12:38:26 -07001087 dirtyRegion.set(hw.swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001088 } else {
1089 // we need to redraw everything (the whole screen)
Mathias Agopian87baae12012-07-31 12:38:26 -07001090 dirtyRegion.set(hw.bounds());
1091 hw.swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 }
1093 }
1094
Mathias Agopian87baae12012-07-31 12:38:26 -07001095 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001097 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1098 const size_t count = currentLayers.size();
1099 for (size_t i=0 ; i<count ; i++) {
1100 currentLayers[i]->onPostComposition();
1101 }
1102
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001103 // update the swap region and clear the dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001104 hw.swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105}
1106
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001107void SurfaceFlinger::composeSurfaces(const DisplayDevice& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001108{
Mathias Agopian86303202012-07-24 22:46:10 -07001109 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001110 HWComposer::LayerListIterator cur = hwc.begin();
1111 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001112
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001113 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001114 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001115
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001116 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001117
1118 // set the frame buffer
1119 glMatrixMode(GL_MODELVIEW);
1120 glLoadIdentity();
1121
1122 // Never touch the framebuffer if we don't have any framebuffer layers
1123 if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
Mathias Agopianb9494d52012-04-18 02:28:45 -07001124 // when using overlays, we assume a fully transparent framebuffer
1125 // NOTE: we could reduce how much we need to clear, for instance
1126 // remove where there are opaque FB layers. however, on some
1127 // GPUs doing a "clean slate" glClear might be more efficient.
1128 // We'll revisit later if needed.
1129 glClearColor(0, 0, 0, 0);
1130 glClear(GL_COLOR_BUFFER_BIT);
1131 } else {
Mathias Agopian87baae12012-07-31 12:38:26 -07001132 const Region region(hw.undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001133 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001134 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001135 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001136 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001137 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001138 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001139
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001140 /*
1141 * and then, render the layers targeted at the framebuffer
1142 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001143
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001144 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001145 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001146 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001147 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001148 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001149 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001150 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001151 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001152 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001153 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001154 // never clear the very first layer since we're
1155 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001156 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001157 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001158 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001159 continue;
1160 }
1161 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001162 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001163 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001164 if (cur != end) {
1165 ++cur;
1166 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001167 }
1168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169}
1170
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001171void SurfaceFlinger::debugFlashRegions(const DisplayDevice& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001172 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173{
Mathias Agopian0a917752010-06-14 21:20:00 -07001174 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001175 const int32_t height = hw.getHeight();
Mathias Agopian87baae12012-07-31 12:38:26 -07001176 if (hw.swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001177 return;
1178 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001179
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001180 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1181 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian87baae12012-07-31 12:38:26 -07001182 dirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001183 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001184 }
1185
Mathias Agopianc492e672011-10-18 14:49:27 -07001186 glDisable(GL_TEXTURE_EXTERNAL_OES);
1187 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
Mathias Agopian0926f502009-05-04 14:17:04 -07001190 static int toggle = 0;
1191 toggle = 1 - toggle;
1192 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001193 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001194 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001195 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001196 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197
Mathias Agopian87baae12012-07-31 12:38:26 -07001198 Region::const_iterator it = dirtyRegion.begin();
1199 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001200 while (it != end) {
1201 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001203 { r.left, height - r.top },
1204 { r.left, height - r.bottom },
1205 { r.right, height - r.bottom },
1206 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 };
1208 glVertexPointer(2, GL_FLOAT, 0, vertices);
1209 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1210 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001211
Mathias Agopian87baae12012-07-31 12:38:26 -07001212 hw.flip(hw.swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213
1214 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001215 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216}
1217
Mathias Agopian87baae12012-07-31 12:38:26 -07001218void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001220 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001221 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001222 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001223 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001224
1225 GLfloat vertices[4][2];
1226 glVertexPointer(2, GL_FLOAT, 0, vertices);
1227 Region::const_iterator it = region.begin();
1228 Region::const_iterator const end = region.end();
1229 while (it != end) {
1230 const Rect& r = *it++;
1231 vertices[0][0] = r.left;
1232 vertices[0][1] = r.top;
1233 vertices[1][0] = r.right;
1234 vertices[1][1] = r.top;
1235 vertices[2][0] = r.right;
1236 vertices[2][1] = r.bottom;
1237 vertices[3][0] = r.left;
1238 vertices[3][1] = r.bottom;
1239 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241}
1242
Mathias Agopian96f08192010-06-02 23:28:45 -07001243ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1244 const sp<LayerBaseClient>& lbc)
1245{
Mathias Agopian96f08192010-06-02 23:28:45 -07001246 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001247 size_t name = client->attachLayer(lbc);
1248
Mathias Agopian96f08192010-06-02 23:28:45 -07001249 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001250 Mutex::Autolock _l(mStateLock);
1251 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001252
Mathias Agopian4f113742011-05-03 16:21:41 -07001253 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001254}
1255
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001256status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257{
1258 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001259 status_t err = purgatorizeLayer_l(layer);
1260 if (err == NO_ERROR)
1261 setTransactionFlags(eTransactionNeeded);
1262 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263}
1264
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001265status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266{
1267 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1268 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001269 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 return NO_ERROR;
1271 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001272 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273}
1274
Mathias Agopian9a112062009-04-17 19:36:26 -07001275status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1276{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001277 // First add the layer to the purgatory list, which makes sure it won't
1278 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001279 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001280 if (err >= 0) {
1281 mLayerPurgatory.add(layerBase);
1282 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001283
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001284 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001285
Mathias Agopian3d579642009-06-04 18:46:21 -07001286 // it's possible that we don't find a layer, because it might
1287 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001288 // from the user because there is a race between Client::destroySurface(),
1289 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001290 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1291}
1292
Mathias Agopiandea20b12011-05-03 17:04:02 -07001293uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1294{
1295 return android_atomic_release_load(&mTransactionFlags);
1296}
1297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1299{
1300 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1301}
1302
Mathias Agopianbb641242010-05-18 17:06:55 -07001303uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304{
1305 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1306 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001307 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 }
1309 return old;
1310}
1311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312
Mathias Agopian8b33f032012-07-24 20:43:54 -07001313void SurfaceFlinger::setTransactionState(
1314 const Vector<ComposerState>& state,
1315 const Vector<DisplayState>& displays,
1316 uint32_t flags)
1317{
Mathias Agopian698c0872011-06-28 19:09:31 -07001318 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001319
Mathias Agopian8b33f032012-07-24 20:43:54 -07001320 int orientation = eOrientationUnchanged;
1321 if (displays.size()) {
1322 // TODO: handle all displays
1323 orientation = displays[0].orientation;
1324 }
1325
Jamie Gennis28378392011-10-12 17:39:00 -07001326 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001327 if (mCurrentState.orientation != orientation) {
1328 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1329 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001330 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001331 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001332 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001333 orientation);
1334 }
1335 }
1336
Mathias Agopian698c0872011-06-28 19:09:31 -07001337 const size_t count = state.size();
1338 for (size_t i=0 ; i<count ; i++) {
1339 const ComposerState& s(state[i]);
1340 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001341 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001342 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001343
Mathias Agopian386aa982011-11-07 21:58:03 -08001344 if (transactionFlags) {
1345 // this triggers the transaction
1346 setTransactionFlags(transactionFlags);
1347
1348 // if this is a synchronous transaction, wait for it to take effect
1349 // before returning.
1350 if (flags & eSynchronous) {
1351 mTransationPending = true;
1352 }
1353 while (mTransationPending) {
1354 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1355 if (CC_UNLIKELY(err != NO_ERROR)) {
1356 // just in case something goes wrong in SF, return to the
1357 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001358 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001359 mTransationPending = false;
1360 break;
1361 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001362 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 }
1364}
1365
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001366sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001367 ISurfaceComposerClient::surface_data_t* params,
1368 const String8& name,
1369 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1371 uint32_t flags)
1372{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001373 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001374 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001375
1376 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001377 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001378 int(w), int(h));
1379 return surfaceHandle;
1380 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001381
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001382 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 switch (flags & eFXSurfaceMask) {
1384 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001385 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 break;
1387 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001388 // for now we treat Blur as Dim, until we can implement it
1389 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001391 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001393 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001394 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001395 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 }
1397
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001398 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001399 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001400 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001401 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001403 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001404 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001405 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001406 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001407 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 }
1409
1410 return surfaceHandle;
1411}
1412
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001413sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001414 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001415 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001416 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417{
1418 // initialize the surfaces
1419 switch (format) { // TODO: take h/w into account
1420 case PIXEL_FORMAT_TRANSPARENT:
1421 case PIXEL_FORMAT_TRANSLUCENT:
1422 format = PIXEL_FORMAT_RGBA_8888;
1423 break;
1424 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001425#ifdef NO_RGBX_8888
1426 format = PIXEL_FORMAT_RGB_565;
1427#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001428 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001429#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 break;
1431 }
1432
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001433#ifdef NO_RGBX_8888
1434 if (format == PIXEL_FORMAT_RGBX_8888)
1435 format = PIXEL_FORMAT_RGBA_8888;
1436#endif
1437
Mathias Agopian96f08192010-06-02 23:28:45 -07001438 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001439 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001440 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001441 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001442 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 return layer;
1445}
1446
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001447sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001448 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001449 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450{
Mathias Agopian96f08192010-06-02 23:28:45 -07001451 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001452 return layer;
1453}
1454
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001455sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001456 const sp<Client>& client, DisplayID display,
1457 uint32_t w, uint32_t h, uint32_t flags)
1458{
1459 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 return layer;
1461}
1462
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001463status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464{
Mathias Agopian9a112062009-04-17 19:36:26 -07001465 /*
1466 * called by the window manager, when a surface should be marked for
1467 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001468 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001469 * The surface is removed from the current and drawing lists, but placed
1470 * in the purgatory queue, so it's not destroyed right-away (we need
1471 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001472 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001473
Mathias Agopian48d819a2009-09-10 19:41:18 -07001474 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001475 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001476 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001477
Mathias Agopian48d819a2009-09-10 19:41:18 -07001478 if (layer != 0) {
1479 err = purgatorizeLayer_l(layer);
1480 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001481 setTransactionFlags(eTransactionNeeded);
1482 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001483 }
1484 return err;
1485}
1486
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001487status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001488{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001489 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001490 status_t err = NO_ERROR;
1491 sp<LayerBaseClient> l(layer.promote());
1492 if (l != NULL) {
1493 Mutex::Autolock _l(mStateLock);
1494 err = removeLayer_l(l);
1495 if (err == NAME_NOT_FOUND) {
1496 // The surface wasn't in the current list, which means it was
1497 // removed already, which means it is in the purgatory,
1498 // and need to be removed from there.
1499 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001500 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001501 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001502 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001503 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001504 "error removing layer=%p (%s)", l.get(), strerror(-err));
1505 }
1506 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507}
1508
Mathias Agopian698c0872011-06-28 19:09:31 -07001509uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001510 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001511 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001514 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1515 if (layer != 0) {
1516 const uint32_t what = s.what;
1517 if (what & ePositionChanged) {
1518 if (layer->setPosition(s.x, s.y))
1519 flags |= eTraversalNeeded;
1520 }
1521 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001522 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001523 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1524 if (layer->setLayer(s.z)) {
1525 mCurrentState.layersSortedByZ.removeAt(idx);
1526 mCurrentState.layersSortedByZ.add(layer);
1527 // we need traversal (state changed)
1528 // AND transaction (list changed)
1529 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 }
1531 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001532 if (what & eSizeChanged) {
1533 if (layer->setSize(s.w, s.h)) {
1534 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001535 }
1536 }
1537 if (what & eAlphaChanged) {
1538 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1539 flags |= eTraversalNeeded;
1540 }
1541 if (what & eMatrixChanged) {
1542 if (layer->setMatrix(s.matrix))
1543 flags |= eTraversalNeeded;
1544 }
1545 if (what & eTransparentRegionChanged) {
1546 if (layer->setTransparentRegionHint(s.transparentRegion))
1547 flags |= eTraversalNeeded;
1548 }
1549 if (what & eVisibilityChanged) {
1550 if (layer->setFlags(s.flags, s.mask))
1551 flags |= eTraversalNeeded;
1552 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001553 if (what & eCropChanged) {
1554 if (layer->setCrop(s.crop))
1555 flags |= eTraversalNeeded;
1556 }
Mathias Agopian87855782012-07-24 21:41:09 -07001557 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001558 // NOTE: index needs to be calculated before we update the state
1559 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1560 if (layer->setLayerStack(s.layerStack)) {
1561 mCurrentState.layersSortedByZ.removeAt(idx);
1562 mCurrentState.layersSortedByZ.add(layer);
1563 // we need traversal (state changed)
1564 // AND transaction (list changed)
1565 flags |= eTransactionNeeded|eTraversalNeeded;
1566 }
Mathias Agopian87855782012-07-24 21:41:09 -07001567 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001568 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001569 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570}
1571
Mathias Agopianb60314a2012-04-10 22:09:54 -07001572// ---------------------------------------------------------------------------
1573
1574void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001575 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001576 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001577 getHwComposer().acquire();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001578 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001579 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001580 // this is a temporary work-around, eventually this should be called
1581 // by the power-manager
1582 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001583 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001584 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585}
1586
Mathias Agopianb60314a2012-04-10 22:09:54 -07001587void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001588 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001589 const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopianb60314a2012-04-10 22:09:54 -07001590 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001591 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001592 hw.releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001593 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001594 // from this point on, SF will stop drawing
1595 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596}
1597
Colin Cross8e533062012-06-07 13:17:52 -07001598void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001599 class MessageScreenAcquired : public MessageBase {
1600 SurfaceFlinger* flinger;
1601 public:
1602 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1603 virtual bool handler() {
1604 flinger->onScreenAcquired();
1605 return true;
1606 }
1607 };
1608 sp<MessageBase> msg = new MessageScreenAcquired(this);
1609 postMessageSync(msg);
1610}
1611
Colin Cross8e533062012-06-07 13:17:52 -07001612void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001613 class MessageScreenReleased : public MessageBase {
1614 SurfaceFlinger* flinger;
1615 public:
1616 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1617 virtual bool handler() {
1618 flinger->onScreenReleased();
1619 return true;
1620 }
1621 };
1622 sp<MessageBase> msg = new MessageScreenReleased(this);
1623 postMessageSync(msg);
1624}
1625
1626// ---------------------------------------------------------------------------
1627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1629{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001630 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631 char buffer[SIZE];
1632 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001633
1634 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 snprintf(buffer, SIZE, "Permission Denial: "
1636 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1637 IPCThreadState::self()->getCallingPid(),
1638 IPCThreadState::self()->getCallingUid());
1639 result.append(buffer);
1640 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001641 // Try to get the main lock, but don't insist if we can't
1642 // (this would indicate SF is stuck, but we want to be able to
1643 // print something in dumpsys).
1644 int retry = 3;
1645 while (mStateLock.tryLock()<0 && --retry>=0) {
1646 usleep(1000000);
1647 }
1648 const bool locked(retry >= 0);
1649 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001650 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001651 "SurfaceFlinger appears to be unresponsive, "
1652 "dumping anyways (no locks held)\n");
1653 result.append(buffer);
1654 }
1655
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001656 bool dumpAll = true;
1657 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001658 size_t numArgs = args.size();
1659 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001660 if ((index < numArgs) &&
1661 (args[index] == String16("--list"))) {
1662 index++;
1663 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001664 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001665 }
1666
1667 if ((index < numArgs) &&
1668 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001669 index++;
1670 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001671 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001672 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001673
1674 if ((index < numArgs) &&
1675 (args[index] == String16("--latency-clear"))) {
1676 index++;
1677 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001678 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001679 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001681
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001682 if (dumpAll) {
1683 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001684 }
1685
Mathias Agopian9795c422009-08-26 16:36:26 -07001686 if (locked) {
1687 mStateLock.unlock();
1688 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 }
1690 write(fd, result.string(), result.size());
1691 return NO_ERROR;
1692}
1693
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001694void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1695 String8& result, char* buffer, size_t SIZE) const
1696{
1697 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1698 const size_t count = currentLayers.size();
1699 for (size_t i=0 ; i<count ; i++) {
1700 const sp<LayerBase>& layer(currentLayers[i]);
1701 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1702 result.append(buffer);
1703 }
1704}
1705
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001706void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1707 String8& result, char* buffer, size_t SIZE) const
1708{
1709 String8 name;
1710 if (index < args.size()) {
1711 name = String8(args[index]);
1712 index++;
1713 }
1714
1715 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1716 const size_t count = currentLayers.size();
1717 for (size_t i=0 ; i<count ; i++) {
1718 const sp<LayerBase>& layer(currentLayers[i]);
1719 if (name.isEmpty()) {
1720 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1721 result.append(buffer);
1722 }
1723 if (name.isEmpty() || (name == layer->getName())) {
1724 layer->dumpStats(result, buffer, SIZE);
1725 }
1726 }
1727}
1728
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001729void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1730 String8& result, char* buffer, size_t SIZE) const
1731{
1732 String8 name;
1733 if (index < args.size()) {
1734 name = String8(args[index]);
1735 index++;
1736 }
1737
1738 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1739 const size_t count = currentLayers.size();
1740 for (size_t i=0 ; i<count ; i++) {
1741 const sp<LayerBase>& layer(currentLayers[i]);
1742 if (name.isEmpty() || (name == layer->getName())) {
1743 layer->clearStats();
1744 }
1745 }
1746}
1747
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001748void SurfaceFlinger::dumpAllLocked(
1749 String8& result, char* buffer, size_t SIZE) const
1750{
1751 // figure out if we're stuck somewhere
1752 const nsecs_t now = systemTime();
1753 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1754 const nsecs_t inTransaction(mDebugInTransaction);
1755 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1756 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1757
1758 /*
1759 * Dump the visible layer list
1760 */
1761 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1762 const size_t count = currentLayers.size();
1763 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1764 result.append(buffer);
1765 for (size_t i=0 ; i<count ; i++) {
1766 const sp<LayerBase>& layer(currentLayers[i]);
1767 layer->dump(result, buffer, SIZE);
1768 }
1769
1770 /*
1771 * Dump the layers in the purgatory
1772 */
1773
1774 const size_t purgatorySize = mLayerPurgatory.size();
1775 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1776 result.append(buffer);
1777 for (size_t i=0 ; i<purgatorySize ; i++) {
1778 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1779 layer->shortDump(result, buffer, SIZE);
1780 }
1781
1782 /*
1783 * Dump SurfaceFlinger global state
1784 */
1785
1786 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1787 result.append(buffer);
1788
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001789 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001790 const GLExtensions& extensions(GLExtensions::getInstance());
1791 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1792 extensions.getVendor(),
1793 extensions.getRenderer(),
1794 extensions.getVersion());
1795 result.append(buffer);
1796
1797 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001798 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001799 result.append(buffer);
1800
1801 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1802 result.append(buffer);
1803
Mathias Agopian87baae12012-07-31 12:38:26 -07001804 hw.undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001805 snprintf(buffer, SIZE,
1806 " orientation=%d, canDraw=%d\n",
1807 mCurrentState.orientation, hw.canDraw());
1808 result.append(buffer);
1809 snprintf(buffer, SIZE,
1810 " last eglSwapBuffers() time: %f us\n"
1811 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001812 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001813 " refresh-rate : %f fps\n"
1814 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001815 " y-dpi : %f\n"
1816 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001817 mLastSwapBufferTime/1000.0,
1818 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001819 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001820 hw.getRefreshRate(),
1821 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001822 hw.getDpiY(),
1823 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001824 result.append(buffer);
1825
1826 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1827 inSwapBuffersDuration/1000.0);
1828 result.append(buffer);
1829
1830 snprintf(buffer, SIZE, " transaction time: %f us\n",
1831 inTransactionDuration/1000.0);
1832 result.append(buffer);
1833
1834 /*
1835 * VSYNC state
1836 */
1837 mEventThread->dump(result, buffer, SIZE);
1838
1839 /*
1840 * Dump HWComposer state
1841 */
Mathias Agopian86303202012-07-24 22:46:10 -07001842 HWComposer& hwc(getHwComposer());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001843 snprintf(buffer, SIZE, "h/w composer state:\n");
1844 result.append(buffer);
1845 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1846 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1847 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1848 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001849 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001850
1851 /*
1852 * Dump gralloc state
1853 */
1854 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1855 alloc.dump(result);
1856 hw.dump(result);
1857}
1858
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001859status_t SurfaceFlinger::onTransact(
1860 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1861{
1862 switch (code) {
1863 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001864 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001867 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001868 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001869 case BLANK:
1870 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 {
1872 // codes that require permission check
1873 IPCThreadState* ipc = IPCThreadState::self();
1874 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001875 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001876 if ((uid != AID_GRAPHICS) &&
1877 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001878 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001879 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1880 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001882 break;
1883 }
1884 case CAPTURE_SCREEN:
1885 {
1886 // codes that require permission check
1887 IPCThreadState* ipc = IPCThreadState::self();
1888 const int pid = ipc->getCallingPid();
1889 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001890 if ((uid != AID_GRAPHICS) &&
1891 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001892 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001893 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1894 return PERMISSION_DENIED;
1895 }
1896 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 }
1898 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001899
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1901 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001902 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001903 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001904 IPCThreadState* ipc = IPCThreadState::self();
1905 const int pid = ipc->getCallingPid();
1906 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001907 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001908 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909 return PERMISSION_DENIED;
1910 }
1911 int n;
1912 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001913 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001914 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915 return NO_ERROR;
1916 case 1002: // SHOW_UPDATES
1917 n = data.readInt32();
1918 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001919 invalidateHwcGeometry();
1920 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001923 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001924 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001926 case 1005:{ // force transaction
1927 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1928 return NO_ERROR;
1929 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001930 case 1006:{ // send empty update
1931 signalRefresh();
1932 return NO_ERROR;
1933 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001934 case 1008: // toggle use of hw composer
1935 n = data.readInt32();
1936 mDebugDisableHWC = n ? 1 : 0;
1937 invalidateHwcGeometry();
1938 repaintEverything();
1939 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001940 case 1009: // toggle use of transform hint
1941 n = data.readInt32();
1942 mDebugDisableTransformHint = n ? 1 : 0;
1943 invalidateHwcGeometry();
1944 repaintEverything();
1945 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001947 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001948 reply->writeInt32(0);
1949 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001950 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001951 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 return NO_ERROR;
1953 case 1013: {
1954 Mutex::Autolock _l(mStateLock);
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001955 const DisplayDevice& hw(getDefaultDisplayDevice());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956 reply->writeInt32(hw.getPageFlipCount());
1957 }
1958 return NO_ERROR;
1959 }
1960 }
1961 return err;
1962}
1963
Mathias Agopian53331da2011-08-22 21:44:41 -07001964void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07001965 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001966 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001967}
1968
Mathias Agopian59119e62010-10-11 12:37:43 -07001969// ---------------------------------------------------------------------------
1970
Mathias Agopian118d0242011-10-13 16:02:48 -07001971status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1972 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1973{
1974 Mutex::Autolock _l(mStateLock);
1975 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1976}
1977
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001978status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1979 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001980{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001981 ATRACE_CALL();
1982
Mathias Agopian59119e62010-10-11 12:37:43 -07001983 if (!GLExtensions::getInstance().haveFramebufferObject())
1984 return INVALID_OPERATION;
1985
1986 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001987 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07001988 const uint32_t hw_w = hw.getWidth();
1989 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001990 GLfloat u = 1;
1991 GLfloat v = 1;
1992
1993 // make sure to clear all GL error flags
1994 while ( glGetError() != GL_NO_ERROR ) ;
1995
1996 // create a FBO
1997 GLuint name, tname;
1998 glGenTextures(1, &tname);
1999 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002000 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2001 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2003 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002004 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002005 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002006 GLint tw = (2 << (31 - clz(hw_w)));
2007 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002008 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2009 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002010 u = GLfloat(hw_w) / tw;
2011 v = GLfloat(hw_h) / th;
2012 }
2013 glGenFramebuffersOES(1, &name);
2014 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002015 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2016 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002017
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002018 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002019 glDisable(GL_TEXTURE_EXTERNAL_OES);
2020 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021 glClearColor(0,0,0,1);
2022 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002023 glMatrixMode(GL_MODELVIEW);
2024 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002025 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 const size_t count = layers.size();
2027 for (size_t i=0 ; i<count ; ++i) {
2028 const sp<LayerBase>& layer(layers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002029 layer->drawForScreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002030 }
2031
Mathias Agopian118d0242011-10-13 16:02:48 -07002032 hw.compositionComplete();
2033
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034 // back to main framebuffer
2035 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002036 glDeleteFramebuffersOES(1, &name);
2037
2038 *textureName = tname;
2039 *uOut = u;
2040 *vOut = v;
2041 return NO_ERROR;
2042}
2043
2044// ---------------------------------------------------------------------------
2045
Mathias Agopianed9807b2012-05-18 14:18:08 -07002046class VSyncWaiter {
2047 DisplayEventReceiver::Event buffer[4];
2048 sp<Looper> looper;
2049 sp<IDisplayEventConnection> events;
2050 sp<BitTube> eventTube;
2051public:
2052 VSyncWaiter(const sp<EventThread>& eventThread) {
2053 looper = new Looper(true);
2054 events = eventThread->createEventConnection();
2055 eventTube = events->getDataChannel();
2056 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
2057 events->requestNextVsync();
2058 }
2059
2060 void wait() {
2061 ssize_t n;
2062
2063 looper->pollOnce(-1);
2064 // we don't handle any errors here, it doesn't matter
2065 // and we don't want to take the risk to get stuck.
2066
2067 // drain the events...
2068 while ((n = DisplayEventReceiver::getEvents(
2069 eventTube, buffer, 4)) > 0) ;
2070
2071 events->requestNextVsync();
2072 }
2073};
2074
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002075status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
2076{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002077 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002078 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002079 const uint32_t hw_w = hw.getWidth();
2080 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07002081 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082
2083 GLfloat u, v;
2084 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002085 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002086 if (result != NO_ERROR) {
2087 return result;
2088 }
2089
2090 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002091 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002092 glBindTexture(GL_TEXTURE_2D, tname);
2093 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2094 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2095 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002096 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2097 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2099 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2100 glVertexPointer(2, GL_FLOAT, 0, vtx);
2101
Mathias Agopianffcf4652011-07-07 17:30:31 -07002102 /*
2103 * Texture coordinate mapping
2104 *
2105 * u
2106 * 1 +----------+---+
2107 * | | | | image is inverted
2108 * | V | | w.r.t. the texture
2109 * 1-v +----------+ | coordinates
2110 * | |
2111 * | |
2112 * | |
2113 * 0 +--------------+
2114 * 0 1
2115 *
2116 */
2117
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118 class s_curve_interpolator {
2119 const float nbFrames, s, v;
2120 public:
2121 s_curve_interpolator(int nbFrames, float s)
2122 : nbFrames(1.0f / (nbFrames-1)), s(s),
2123 v(1.0f + expf(-s + 0.5f*s)) {
2124 }
2125 float operator()(int f) {
2126 const float x = f * nbFrames;
2127 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2128 }
2129 };
2130
2131 class v_stretch {
2132 const GLfloat hw_w, hw_h;
2133 public:
2134 v_stretch(uint32_t hw_w, uint32_t hw_h)
2135 : hw_w(hw_w), hw_h(hw_h) {
2136 }
2137 void operator()(GLfloat* vtx, float v) {
2138 const GLfloat w = hw_w + (hw_w * v);
2139 const GLfloat h = hw_h - (hw_h * v);
2140 const GLfloat x = (hw_w - w) * 0.5f;
2141 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002142 vtx[0] = x; vtx[1] = y;
2143 vtx[2] = x; vtx[3] = y + h;
2144 vtx[4] = x + w; vtx[5] = y + h;
2145 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002146 }
2147 };
2148
2149 class h_stretch {
2150 const GLfloat hw_w, hw_h;
2151 public:
2152 h_stretch(uint32_t hw_w, uint32_t hw_h)
2153 : hw_w(hw_w), hw_h(hw_h) {
2154 }
2155 void operator()(GLfloat* vtx, float v) {
2156 const GLfloat w = hw_w - (hw_w * v);
2157 const GLfloat h = 1.0f;
2158 const GLfloat x = (hw_w - w) * 0.5f;
2159 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002160 vtx[0] = x; vtx[1] = y;
2161 vtx[2] = x; vtx[3] = y + h;
2162 vtx[4] = x + w; vtx[5] = y + h;
2163 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002164 }
2165 };
2166
Mathias Agopianed9807b2012-05-18 14:18:08 -07002167 VSyncWaiter vsync(mEventThread);
2168
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002170 char value[PROPERTY_VALUE_MAX];
2171 property_get("debug.sf.electron_frames", value, "24");
2172 int nbFrames = (atoi(value) + 1) >> 1;
2173 if (nbFrames <= 0) // just in case
2174 nbFrames = 24;
2175
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002176 s_curve_interpolator itr(nbFrames, 7.5f);
2177 s_curve_interpolator itg(nbFrames, 8.0f);
2178 s_curve_interpolator itb(nbFrames, 8.5f);
2179
2180 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002181
2182 glMatrixMode(GL_TEXTURE);
2183 glLoadIdentity();
2184 glMatrixMode(GL_MODELVIEW);
2185 glLoadIdentity();
2186
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002187 glEnable(GL_BLEND);
2188 glBlendFunc(GL_ONE, GL_ONE);
2189 for (int i=0 ; i<nbFrames ; i++) {
2190 float x, y, w, h;
2191 const float vr = itr(i);
2192 const float vg = itg(i);
2193 const float vb = itb(i);
2194
Mathias Agopianed9807b2012-05-18 14:18:08 -07002195 // wait for vsync
2196 vsync.wait();
2197
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 // clear screen
2199 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002200 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002201 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002202
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 // draw the red plane
2204 vverts(vtx, vr);
2205 glColorMask(1,0,0,1);
2206 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002207
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002208 // draw the green plane
2209 vverts(vtx, vg);
2210 glColorMask(0,1,0,1);
2211 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002212
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002213 // draw the blue plane
2214 vverts(vtx, vb);
2215 glColorMask(0,0,1,1);
2216 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002217
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002218 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002219 glDisable(GL_TEXTURE_2D);
2220 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002221 glColor4f(vg, vg, vg, 1);
2222 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2223 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002224 }
2225
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226 h_stretch hverts(hw_w, hw_h);
2227 glDisable(GL_BLEND);
2228 glDisable(GL_TEXTURE_2D);
2229 glColorMask(1,1,1,1);
2230 for (int i=0 ; i<nbFrames ; i++) {
2231 const float v = itg(i);
2232 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002233
2234 // wait for vsync
2235 vsync.wait();
2236
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237 glClear(GL_COLOR_BUFFER_BIT);
2238 glColor4f(1-v, 1-v, 1-v, 1);
2239 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2240 hw.flip(screenBounds);
2241 }
2242
2243 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2245 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002246 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002247 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002248 return NO_ERROR;
2249}
2250
2251status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2252{
2253 status_t result = PERMISSION_DENIED;
2254
2255 if (!GLExtensions::getInstance().haveFramebufferObject())
2256 return INVALID_OPERATION;
2257
2258
2259 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002260 const DisplayDevice& hw(getDefaultDisplayDevice());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002261 const uint32_t hw_w = hw.getWidth();
2262 const uint32_t hw_h = hw.getHeight();
2263 const Region screenBounds(hw.bounds());
2264
2265 GLfloat u, v;
2266 GLuint tname;
2267 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2268 if (result != NO_ERROR) {
2269 return result;
2270 }
2271
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002272 GLfloat vtx[8];
2273 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002274 glBindTexture(GL_TEXTURE_2D, tname);
2275 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002280 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2281 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2282 glVertexPointer(2, GL_FLOAT, 0, vtx);
2283
2284 class s_curve_interpolator {
2285 const float nbFrames, s, v;
2286 public:
2287 s_curve_interpolator(int nbFrames, float s)
2288 : nbFrames(1.0f / (nbFrames-1)), s(s),
2289 v(1.0f + expf(-s + 0.5f*s)) {
2290 }
2291 float operator()(int f) {
2292 const float x = f * nbFrames;
2293 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2294 }
2295 };
2296
2297 class v_stretch {
2298 const GLfloat hw_w, hw_h;
2299 public:
2300 v_stretch(uint32_t hw_w, uint32_t hw_h)
2301 : hw_w(hw_w), hw_h(hw_h) {
2302 }
2303 void operator()(GLfloat* vtx, float v) {
2304 const GLfloat w = hw_w + (hw_w * v);
2305 const GLfloat h = hw_h - (hw_h * v);
2306 const GLfloat x = (hw_w - w) * 0.5f;
2307 const GLfloat y = (hw_h - h) * 0.5f;
2308 vtx[0] = x; vtx[1] = y;
2309 vtx[2] = x; vtx[3] = y + h;
2310 vtx[4] = x + w; vtx[5] = y + h;
2311 vtx[6] = x + w; vtx[7] = y;
2312 }
2313 };
2314
2315 class h_stretch {
2316 const GLfloat hw_w, hw_h;
2317 public:
2318 h_stretch(uint32_t hw_w, uint32_t hw_h)
2319 : hw_w(hw_w), hw_h(hw_h) {
2320 }
2321 void operator()(GLfloat* vtx, float v) {
2322 const GLfloat w = hw_w - (hw_w * v);
2323 const GLfloat h = 1.0f;
2324 const GLfloat x = (hw_w - w) * 0.5f;
2325 const GLfloat y = (hw_h - h) * 0.5f;
2326 vtx[0] = x; vtx[1] = y;
2327 vtx[2] = x; vtx[3] = y + h;
2328 vtx[4] = x + w; vtx[5] = y + h;
2329 vtx[6] = x + w; vtx[7] = y;
2330 }
2331 };
2332
Mathias Agopianed9807b2012-05-18 14:18:08 -07002333 VSyncWaiter vsync(mEventThread);
2334
Mathias Agopiana6546e52010-10-14 12:33:07 -07002335 // the full animation is 12 frames
2336 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002337 s_curve_interpolator itr(nbFrames, 7.5f);
2338 s_curve_interpolator itg(nbFrames, 8.0f);
2339 s_curve_interpolator itb(nbFrames, 8.5f);
2340
2341 h_stretch hverts(hw_w, hw_h);
2342 glDisable(GL_BLEND);
2343 glDisable(GL_TEXTURE_2D);
2344 glColorMask(1,1,1,1);
2345 for (int i=nbFrames-1 ; i>=0 ; i--) {
2346 const float v = itg(i);
2347 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002348
2349 // wait for vsync
2350 vsync.wait();
2351
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002352 glClear(GL_COLOR_BUFFER_BIT);
2353 glColor4f(1-v, 1-v, 1-v, 1);
2354 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2355 hw.flip(screenBounds);
2356 }
2357
Mathias Agopiana6546e52010-10-14 12:33:07 -07002358 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002359 v_stretch vverts(hw_w, hw_h);
2360 glEnable(GL_BLEND);
2361 glBlendFunc(GL_ONE, GL_ONE);
2362 for (int i=nbFrames-1 ; i>=0 ; i--) {
2363 float x, y, w, h;
2364 const float vr = itr(i);
2365 const float vg = itg(i);
2366 const float vb = itb(i);
2367
Mathias Agopianed9807b2012-05-18 14:18:08 -07002368 // wait for vsync
2369 vsync.wait();
2370
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002371 // clear screen
2372 glColorMask(1,1,1,1);
2373 glClear(GL_COLOR_BUFFER_BIT);
2374 glEnable(GL_TEXTURE_2D);
2375
2376 // draw the red plane
2377 vverts(vtx, vr);
2378 glColorMask(1,0,0,1);
2379 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2380
2381 // draw the green plane
2382 vverts(vtx, vg);
2383 glColorMask(0,1,0,1);
2384 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2385
2386 // draw the blue plane
2387 vverts(vtx, vb);
2388 glColorMask(0,0,1,1);
2389 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2390
2391 hw.flip(screenBounds);
2392 }
2393
2394 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002395 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002396 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002397 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002398 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002399
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002400 return NO_ERROR;
2401}
2402
2403// ---------------------------------------------------------------------------
2404
Mathias Agopianabd671a2010-10-14 14:54:06 -07002405status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002406{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002407 ATRACE_CALL();
2408
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002409 DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002410 if (!hw.canDraw()) {
2411 // we're already off
2412 return NO_ERROR;
2413 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002414
2415 // turn off hwc while we're doing the animation
Mathias Agopian86303202012-07-24 22:46:10 -07002416 getHwComposer().disable();
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002417 // and make sure to turn it back on (if needed) next time we compose
2418 invalidateHwcGeometry();
2419
Mathias Agopianabd671a2010-10-14 14:54:06 -07002420 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2421 electronBeamOffAnimationImplLocked();
2422 }
2423
2424 // always clear the whole screen at the end of the animation
2425 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002426 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002427 hw.flip( Region(hw.bounds()) );
2428
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002429 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002430}
2431
2432status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2433{
Mathias Agopian59119e62010-10-11 12:37:43 -07002434 class MessageTurnElectronBeamOff : public MessageBase {
2435 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002436 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002437 status_t result;
2438 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002439 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2440 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002441 }
2442 status_t getResult() const {
2443 return result;
2444 }
2445 virtual bool handler() {
2446 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002447 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002448 return true;
2449 }
2450 };
2451
Mathias Agopianabd671a2010-10-14 14:54:06 -07002452 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002453 status_t res = postMessageSync(msg);
2454 if (res == NO_ERROR) {
2455 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002456
2457 // work-around: when the power-manager calls us we activate the
2458 // animation. eventually, the "on" animation will be called
2459 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002460 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002461 }
2462 return res;
2463}
2464
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002465// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002466
Mathias Agopianabd671a2010-10-14 14:54:06 -07002467status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002468{
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002469 DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002470 if (hw.canDraw()) {
2471 // we're already on
2472 return NO_ERROR;
2473 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002474 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2475 electronBeamOnAnimationImplLocked();
2476 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002477
2478 // make sure to redraw the whole screen when the animation is done
Mathias Agopian87baae12012-07-31 12:38:26 -07002479 hw.dirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002480 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002481
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002482 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002483}
2484
2485status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2486{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002487 class MessageTurnElectronBeamOn : public MessageBase {
2488 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002489 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002490 status_t result;
2491 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002492 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2493 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002494 }
2495 status_t getResult() const {
2496 return result;
2497 }
2498 virtual bool handler() {
2499 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002500 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002501 return true;
2502 }
2503 };
2504
Mathias Agopianabd671a2010-10-14 14:54:06 -07002505 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002506 return NO_ERROR;
2507}
2508
2509// ---------------------------------------------------------------------------
2510
Mathias Agopian74c40c02010-09-29 13:02:36 -07002511status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2512 sp<IMemoryHeap>* heap,
2513 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002514 uint32_t sw, uint32_t sh,
2515 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002516{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002517 ATRACE_CALL();
2518
Mathias Agopian74c40c02010-09-29 13:02:36 -07002519 status_t result = PERMISSION_DENIED;
2520
2521 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002522 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002523 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002524 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002525
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002526 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002527 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002528 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002529
2530 // get screen geometry
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002531 const DisplayDevice& hw(getDisplayDevice(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002532 const uint32_t hw_w = hw.getWidth();
2533 const uint32_t hw_h = hw.getHeight();
2534
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002535 // if we have secure windows on this display, never allow the screen capture
2536 if (hw.getSecureLayerVisible()) {
2537 return PERMISSION_DENIED;
2538 }
2539
2540 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002541 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002542 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002543
2544 sw = (!sw) ? hw_w : sw;
2545 sh = (!sh) ? hw_h : sh;
2546 const size_t size = sw * sh * 4;
2547
Steve Block9d453682011-12-20 16:23:08 +00002548 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002549 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002550
Mathias Agopian74c40c02010-09-29 13:02:36 -07002551 // make sure to clear all GL error flags
2552 while ( glGetError() != GL_NO_ERROR ) ;
2553
2554 // create a FBO
2555 GLuint name, tname;
2556 glGenRenderbuffersOES(1, &tname);
2557 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2558 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002559
Mathias Agopian74c40c02010-09-29 13:02:36 -07002560 glGenFramebuffersOES(1, &name);
2561 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2562 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2563 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2564
2565 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002566
Mathias Agopian74c40c02010-09-29 13:02:36 -07002567 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2568
2569 // invert everything, b/c glReadPixel() below will invert the FB
2570 glViewport(0, 0, sw, sh);
2571 glMatrixMode(GL_PROJECTION);
2572 glPushMatrix();
2573 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002574 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002575 glMatrixMode(GL_MODELVIEW);
2576
2577 // redraw the screen entirely...
2578 glClearColor(0,0,0,1);
2579 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002580
Jamie Gennis9575f602011-10-07 14:51:16 -07002581 const LayerVector& layers(mDrawingState.layersSortedByZ);
2582 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002583 for (size_t i=0 ; i<count ; ++i) {
2584 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002585 const uint32_t flags = layer->drawingState().flags;
2586 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2587 const uint32_t z = layer->drawingState().z;
2588 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002589 layer->drawForScreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002590 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002591 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002592 }
2593
Mathias Agopian74c40c02010-09-29 13:02:36 -07002594 // check for errors and return screen capture
2595 if (glGetError() != GL_NO_ERROR) {
2596 // error while rendering
2597 result = INVALID_OPERATION;
2598 } else {
2599 // allocate shared memory large enough to hold the
2600 // screen capture
2601 sp<MemoryHeapBase> base(
2602 new MemoryHeapBase(size, 0, "screen-capture") );
2603 void* const ptr = base->getBase();
2604 if (ptr) {
2605 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002606 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002607 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2608 if (glGetError() == GL_NO_ERROR) {
2609 *heap = base;
2610 *w = sw;
2611 *h = sh;
2612 *f = PIXEL_FORMAT_RGBA_8888;
2613 result = NO_ERROR;
2614 }
2615 } else {
2616 result = NO_MEMORY;
2617 }
2618 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002619 glViewport(0, 0, hw_w, hw_h);
2620 glMatrixMode(GL_PROJECTION);
2621 glPopMatrix();
2622 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002623 } else {
2624 result = BAD_VALUE;
2625 }
2626
2627 // release FBO resources
2628 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2629 glDeleteRenderbuffersOES(1, &tname);
2630 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002631
2632 hw.compositionComplete();
2633
Steve Block9d453682011-12-20 16:23:08 +00002634 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002635
Mathias Agopian74c40c02010-09-29 13:02:36 -07002636 return result;
2637}
2638
2639
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002640status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2641 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002642 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002643 uint32_t sw, uint32_t sh,
2644 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002645{
2646 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002647 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002648 return BAD_VALUE;
2649
2650 if (!GLExtensions::getInstance().haveFramebufferObject())
2651 return INVALID_OPERATION;
2652
2653 class MessageCaptureScreen : public MessageBase {
2654 SurfaceFlinger* flinger;
2655 DisplayID dpy;
2656 sp<IMemoryHeap>* heap;
2657 uint32_t* w;
2658 uint32_t* h;
2659 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002660 uint32_t sw;
2661 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002662 uint32_t minLayerZ;
2663 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002664 status_t result;
2665 public:
2666 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002667 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002668 uint32_t sw, uint32_t sh,
2669 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002670 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002671 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2672 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2673 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002674 {
2675 }
2676 status_t getResult() const {
2677 return result;
2678 }
2679 virtual bool handler() {
2680 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002681 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002682 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002683 return true;
2684 }
2685 };
2686
2687 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002688 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002689 status_t res = postMessageSync(msg);
2690 if (res == NO_ERROR) {
2691 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2692 }
2693 return res;
2694}
2695
2696// ---------------------------------------------------------------------------
2697
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002698SurfaceFlinger::LayerVector::LayerVector() {
2699}
2700
2701SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2702 : SortedVector<sp<LayerBase> >(rhs) {
2703}
2704
2705int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2706 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002707{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002708 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002709 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2710 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002711
2712 uint32_t ls = l->currentState().layerStack;
2713 uint32_t rs = r->currentState().layerStack;
2714 if (ls != rs)
2715 return ls - rs;
2716
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002717 uint32_t lz = l->currentState().z;
2718 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002719 if (lz != rz)
2720 return lz - rz;
2721
2722 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002723}
2724
2725// ---------------------------------------------------------------------------
2726
2727SurfaceFlinger::State::State()
2728 : orientation(ISurfaceComposer::eOrientationDefault),
2729 orientationFlags(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002730}
2731
2732// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733
Jamie Gennis9a78c902011-01-12 18:30:40 -08002734GraphicBufferAlloc::GraphicBufferAlloc() {}
2735
2736GraphicBufferAlloc::~GraphicBufferAlloc() {}
2737
2738sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002739 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002740 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2741 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002742 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002743 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002744 if (err == NO_MEMORY) {
2745 GraphicBuffer::dumpAllocationsToSystemLog();
2746 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002747 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002748 "failed (%s), handle=%p",
2749 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002750 return 0;
2751 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002752 return graphicBuffer;
2753}
2754
Jamie Gennis9a78c902011-01-12 18:30:40 -08002755// ---------------------------------------------------------------------------
2756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002757}; // namespace android