blob: 1269f3b2edeb6823abb5275ee8cf95df2f965f2b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
39#include <gui/SurfaceTextureClient.h>
40
41#include <ui/GraphicBufferAllocator.h>
42#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <utils/String8.h>
45#include <utils/String16.h>
46#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Mathias Agopian921e6ac2012-07-23 23:11:29 -070049#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070053#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070054#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070056#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070059#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopiana4912602012-07-12 14:25:33 -070062#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070063#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070085 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070098 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
113
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 ALOGI_IF(mDebugRegion, "showupdates enabled");
115 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800118void SurfaceFlinger::onFirstRef()
119{
120 mEventQueue.init(this);
121
122 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
123
124 // Wait for the main thread to be done with its initialization
125 mReadyToRunBarrier.wait();
126}
127
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::~SurfaceFlinger()
130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void SurfaceFlinger::binderDied(const wp<IBinder>& who)
137{
138 // the window manager died on us. prepare its eulogy.
139
140 // reset screen orientation
141 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700142 Vector<DisplayState> displays;
143 DisplayState d;
144 d.orientation = eOrientationDefault;
145 displays.add(d);
146 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800147
148 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700149 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150}
151
Mathias Agopian7e27f052010-05-28 14:22:23 -0700152sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopian96f08192010-06-02 23:28:45 -0700154 sp<ISurfaceComposerClient> bclient;
155 sp<Client> client(new Client(this));
156 status_t err = client->initCheck();
157 if (err == NO_ERROR) {
158 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return bclient;
161}
162
Jamie Gennis9a78c902011-01-12 18:30:40 -0800163sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
164{
165 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
166 return gba;
167}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000173 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700175
176 // wait patiently for the window manager death
177 const String16 name("window");
178 sp<IBinder> window(defaultServiceManager()->getService(name));
179 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700180 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700181 }
182
183 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700184 // formerly we would just kill the process, but we now ask it to exit so it
185 // can choose where to stop the animation.
186 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700189void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
190 class MessageDestroyGLTexture : public MessageBase {
191 GLuint texture;
192 public:
193 MessageDestroyGLTexture(GLuint texture)
194 : texture(texture) {
195 }
196 virtual bool handler() {
197 glDeleteTextures(1, &texture);
198 return true;
199 }
200 };
201 postMessageAsync(new MessageDestroyGLTexture(texture));
202}
203
Mathias Agopiana4912602012-07-12 14:25:33 -0700204status_t SurfaceFlinger::selectConfigForPixelFormat(
205 EGLDisplay dpy,
206 EGLint const* attrs,
207 PixelFormat format,
208 EGLConfig* outConfig)
209{
210 EGLConfig config = NULL;
211 EGLint numConfigs = -1, n=0;
212 eglGetConfigs(dpy, NULL, 0, &numConfigs);
213 EGLConfig* const configs = new EGLConfig[numConfigs];
214 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
215 for (int i=0 ; i<n ; i++) {
216 EGLint nativeVisualId = 0;
217 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
218 if (nativeVisualId>0 && format == nativeVisualId) {
219 *outConfig = configs[i];
220 delete [] configs;
221 return NO_ERROR;
222 }
223 }
224 delete [] configs;
225 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
Mathias Agopiana4912602012-07-12 14:25:33 -0700228EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
229 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
230 // it is to be used with WIFI displays
231 EGLConfig config;
232 EGLint dummy;
233 status_t err;
234 EGLint attribs[] = {
235 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
236 EGL_RECORDABLE_ANDROID, EGL_TRUE,
237 EGL_NONE
238 };
239 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
240 if (err) {
241 // maybe we failed because of EGL_RECORDABLE_ANDROID
242 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
243 attribs[2] = EGL_NONE;
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 }
246 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
247 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
248 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
249 }
250 return config;
251}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
Mathias Agopiana4912602012-07-12 14:25:33 -0700253EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
254 // Also create our EGLContext
255 EGLint contextAttributes[] = {
256#ifdef EGL_IMG_context_priority
257#ifdef HAS_CONTEXT_PRIORITY
258#warning "using EGL_IMG_context_priority"
259 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
260#endif
261#endif
262 EGL_NONE, EGL_NONE
263 };
264 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
265 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
266 return ctxt;
267}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268
Mathias Agopiana4912602012-07-12 14:25:33 -0700269void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
270 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
271 if (!result) {
272 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
273 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 }
275
Mathias Agopiana4912602012-07-12 14:25:33 -0700276 GLExtensions& extensions(GLExtensions::getInstance());
277 extensions.initWithGLStrings(
278 glGetString(GL_VENDOR),
279 glGetString(GL_RENDERER),
280 glGetString(GL_VERSION),
281 glGetString(GL_EXTENSIONS),
282 eglQueryString(display, EGL_VENDOR),
283 eglQueryString(display, EGL_VERSION),
284 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700285
Mathias Agopiana4912602012-07-12 14:25:33 -0700286 EGLint w, h;
287 eglQuerySurface(display, surface, EGL_WIDTH, &w);
288 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700289
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
291 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700292
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 glShadeModel(GL_FLAT);
297 glDisable(GL_DITHER);
298 glDisable(GL_CULL_FACE);
299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300 struct pack565 {
301 inline uint16_t operator() (int r, int g, int b) const {
302 return (r<<11)|(g<<5)|b;
303 }
304 } pack565;
305
Jamie Gennis9575f602011-10-07 14:51:16 -0700306 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
307 glGenTextures(1, &mProtectedTexName);
308 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
309 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
310 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
311 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
312 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
313 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
314 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316 glViewport(0, 0, w, h);
317 glMatrixMode(GL_PROJECTION);
318 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700319 // put the origin in the left-bottom corner
320 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
Mathias Agopiana4912602012-07-12 14:25:33 -0700322 // print some debugging info
323 EGLint r,g,b,a;
324 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
325 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
326 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
327 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
328 ALOGI("EGL informations:");
329 ALOGI("vendor : %s", extensions.getEglVendor());
330 ALOGI("version : %s", extensions.getEglVersion());
331 ALOGI("extensions: %s", extensions.getEglExtension());
332 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
333 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
334 ALOGI("OpenGL ES informations:");
335 ALOGI("vendor : %s", extensions.getVendor());
336 ALOGI("renderer : %s", extensions.getRenderer());
337 ALOGI("version : %s", extensions.getVersion());
338 ALOGI("extensions: %s", extensions.getExtension());
339 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
340 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
341}
342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343status_t SurfaceFlinger::readyToRun()
344{
345 ALOGI( "SurfaceFlinger's main thread ready to run. "
346 "Initializing graphics H/W...");
347
Mathias Agopiana4912602012-07-12 14:25:33 -0700348 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700349 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
350 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700351
352 // Initialize the main display
353 // create native window to main display
354 sp<FramebufferSurface> anw = FramebufferSurface::create();
355 ANativeWindow* const window = anw.get();
356 if (!window) {
357 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
358 exit(0);
359 }
360
361 // initialize the config and context
362 int format;
363 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700364 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
365 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700366
367 // initialize our main display hardware
Mathias Agopian92a979a2012-08-02 18:32:23 -0700368 mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState());
Mathias Agopian42977342012-08-05 00:40:46 -0700369 sp<DisplayDevice> hw = new DisplayDevice(this, DisplayDevice::DISPLAY_ID_MAIN, anw, mEGLConfig);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700370 mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700371
372 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700373 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700374 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800375
Mathias Agopian028508c2012-07-25 21:12:12 -0700376 // start the EventThread
377 mEventThread = new EventThread(this);
378 mEventQueue.setEventThread(mEventThread);
379
Mathias Agopian86303202012-07-24 22:46:10 -0700380 // initialize the H/W composer
Mathias Agopian888c8222012-08-04 21:10:38 -0700381 mHwc = new HWComposer(this, *static_cast<HWComposer::EventHandler *>(this));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700382
383 // initialize our drawing state
384 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700385
Mathias Agopiana4912602012-07-12 14:25:33 -0700386 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800387 mReadyToRunBarrier.open();
388
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700389 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700390 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700391
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 return NO_ERROR;
393}
394
Mathias Agopiana67e4182012-06-19 17:26:12 -0700395void SurfaceFlinger::startBootAnim() {
396 // start boot animation
397 property_set("service.bootanim.exit", "0");
398 property_set("ctl.start", "bootanim");
399}
400
Mathias Agopiana4912602012-07-12 14:25:33 -0700401uint32_t SurfaceFlinger::getMaxTextureSize() const {
402 return mMaxTextureSize;
403}
404
405uint32_t SurfaceFlinger::getMaxViewportDims() const {
406 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
407 mMaxViewportDims[0] : mMaxViewportDims[1];
408}
409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800411
Jamie Gennis582270d2011-08-17 18:19:00 -0700412bool SurfaceFlinger::authenticateSurfaceTexture(
413 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800414 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700415 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800416
417 // Check the visible layer list for the ISurface
418 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
419 size_t count = currentLayers.size();
420 for (size_t i=0 ; i<count ; i++) {
421 const sp<LayerBase>& layer(currentLayers[i]);
422 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700423 if (lbc != NULL) {
424 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
425 if (lbcBinder == surfaceTextureBinder) {
426 return true;
427 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800428 }
429 }
430
431 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700432 // SurfaceTexture gets destroyed before all the clients are done using it,
433 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800434 // will instead fail later on when the client tries to use the surface,
435 // which should be reported as "surface XYZ returned an -ENODEV". The
436 // purgatorized layers are no less authentic than the visible ones, so this
437 // should not cause any harm.
438 size_t purgatorySize = mLayerPurgatory.size();
439 for (size_t i=0 ; i<purgatorySize ; i++) {
440 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
441 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700442 if (lbc != NULL) {
443 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
444 if (lbcBinder == surfaceTextureBinder) {
445 return true;
446 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800447 }
448 }
449
450 return false;
451}
452
Mathias Agopianc666cae2012-07-25 18:56:13 -0700453status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700454 // TODO: this is here only for compatibility -- show go away eventually.
Mathias Agopianc666cae2012-07-25 18:56:13 -0700455 if (uint32_t(dpy) >= 2) {
456 return BAD_INDEX;
457 }
Mathias Agopian42977342012-08-05 00:40:46 -0700458 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
459 info->w = hw->getWidth();
460 info->h = hw->getHeight();
461 info->xdpi = hw->getDpiX();
462 info->ydpi = hw->getDpiY();
Mathias Agopian888c8222012-08-04 21:10:38 -0700463 info->fps = float(1e9 / getHwComposer().getRefreshPeriod());
Mathias Agopian42977342012-08-05 00:40:46 -0700464 info->density = hw->getDensity();
465 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700466 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700467 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700468 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700469}
470
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800471// ----------------------------------------------------------------------------
472
473sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800474 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700475}
476
Mathias Agopian3094df32012-06-18 18:06:45 -0700477void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700478 EGLSurface result = EGL_NO_SURFACE;
479 EGLSurface old_surface = EGL_NO_SURFACE;
480 sp<SurfaceTextureClient> stc;
481
482 if (display != NULL) {
483 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700484 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700485 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700486 ALOGE_IF(result == EGL_NO_SURFACE,
487 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
488 display.get());
489 }
490
491 { // scope for the lock
492 Mutex::Autolock _l(mStateLock);
493 old_surface = mExternalDisplaySurface;
494 mExternalDisplayNativeWindow = stc;
495 mExternalDisplaySurface = result;
496 ALOGD("mExternalDisplaySurface = %p", result);
497 }
498
499 if (old_surface != EGL_NO_SURFACE) {
500 // Note: EGL allows to destroy an object while its current
501 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700502 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700503 }
504}
505
506EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
507 Mutex::Autolock _l(mStateLock);
508 return mExternalDisplaySurface;
509}
510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800512
513void SurfaceFlinger::waitForEvent() {
514 mEventQueue.waitMessage();
515}
516
517void SurfaceFlinger::signalTransaction() {
518 mEventQueue.invalidate();
519}
520
521void SurfaceFlinger::signalLayerUpdate() {
522 mEventQueue.invalidate();
523}
524
525void SurfaceFlinger::signalRefresh() {
526 mEventQueue.refresh();
527}
528
529status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
530 nsecs_t reltime, uint32_t flags) {
531 return mEventQueue.postMessage(msg, reltime);
532}
533
534status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
535 nsecs_t reltime, uint32_t flags) {
536 status_t res = mEventQueue.postMessage(msg, reltime);
537 if (res == NO_ERROR) {
538 msg->wait();
539 }
540 return res;
541}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
Mathias Agopian4fec8732012-06-29 14:12:52 -0700543bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 return true;
546}
547
Mathias Agopian86303202012-07-24 22:46:10 -0700548void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700549 mEventThread->onVSyncReceived(dpy, timestamp);
550}
551
552void SurfaceFlinger::eventControl(int event, int enabled) {
553 getHwComposer().eventControl(event, enabled);
554}
555
Mathias Agopian4fec8732012-06-29 14:12:52 -0700556void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800557 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800558 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700559 case MessageQueue::INVALIDATE:
560 handleMessageTransaction();
561 handleMessageInvalidate();
562 signalRefresh();
563 break;
564 case MessageQueue::REFRESH:
565 handleMessageRefresh();
566 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800567 }
568}
569
Mathias Agopian4fec8732012-06-29 14:12:52 -0700570void SurfaceFlinger::handleMessageTransaction() {
571 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
572 uint32_t transactionFlags = peekTransactionFlags(mask);
573 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700574 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700575 }
576}
577
578void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700579 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700580}
581
582void SurfaceFlinger::handleMessageRefresh() {
583 handleRefresh();
584
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700585 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700586 mVisibleRegionsDirty = false;
587 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700588
589 /*
590 * rebuild the visible layer list per screen
591 */
592
Mathias Agopian87baae12012-07-31 12:38:26 -0700593 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700594 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700595 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700596 Region opaqueRegion;
597 Region dirtyRegion;
598 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700599 hw->getLayerStack(), dirtyRegion, opaqueRegion);
600 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700601
602 Vector< sp<LayerBase> > layersSortedByZ;
603 const size_t count = currentLayers.size();
604 for (size_t i=0 ; i<count ; i++) {
605 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700606 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700607 if (!currentLayers[i]->visibleRegion.isEmpty()) {
608 layersSortedByZ.add(currentLayers[i]);
609 }
610 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700611 }
Mathias Agopian42977342012-08-05 00:40:46 -0700612 hw->setVisibleLayersSortedByZ(layersSortedByZ);
613 hw->undefinedRegion.set(hw->getBounds());
614 hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700615 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700616 }
617
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700618 HWComposer& hwc(getHwComposer());
619 if (hwc.initCheck() == NO_ERROR) {
620 // build the h/w work list
621 const bool workListsDirty = mHwWorkListDirty;
622 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700623 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700624 sp<const DisplayDevice> hw(mDisplays[dpy]);
625 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700626 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700628 hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
629
630 HWComposer::LayerListIterator cur = hwc.begin();
631 const HWComposer::LayerListIterator end = hwc.end();
632 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
633 const sp<LayerBase>& layer(currentLayers[i]);
634
635 if (CC_UNLIKELY(workListsDirty)) {
636 layer->setGeometry(hw, *cur);
637 if (mDebugDisableHWC || mDebugRegion) {
638 cur->setSkip(true);
639 }
640 }
641
642 /*
643 * update the per-frame h/w composer data for each layer
644 * and build the transparent region of the FB
645 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700646 layer->setPerFrameData(hw, *cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700647 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700648 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700649 status_t err = hwc.prepare();
650 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
651 }
652
653 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700654 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700655 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700656
Mathias Agopian87baae12012-07-31 12:38:26 -0700657 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700658 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700659 Region dirtyRegion;
660 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700661 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700662 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700663 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
664 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700665 }
Mathias Agopian42977342012-08-05 00:40:46 -0700666 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700667
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700668 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700669 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700670 // repaint the framebuffer (if needed)
671 handleRepaint(hw, dirtyRegion);
672 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700673 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700674 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700675 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700676 }
677
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700678 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700680
681#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700682 // render to the external display if we have one
683 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
684 if (externalDisplaySurface != EGL_NO_SURFACE) {
685 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
686 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
687 externalDisplaySurface, externalDisplaySurface,
688 eglGetCurrentContext());
689
690 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
691
692 if (success) {
693 // redraw the screen entirely...
694 glDisable(GL_TEXTURE_EXTERNAL_OES);
695 glDisable(GL_TEXTURE_2D);
696 glClearColor(0,0,0,1);
697 glClear(GL_COLOR_BUFFER_BIT);
698 glMatrixMode(GL_MODELVIEW);
699 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700700
Mathias Agopian42977342012-08-05 00:40:46 -0700701 const sp<DisplayDevice>& hw(getDisplayDevice(0));
702 const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700703 const size_t count = layers.size();
704 for (size_t i=0 ; i<count ; ++i) {
705 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700706 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700707 }
708
709 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
710 ALOGE_IF(!success, "external display eglSwapBuffers failed");
711
Mathias Agopian42977342012-08-05 00:40:46 -0700712 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700713 }
714
715 success = eglMakeCurrent(eglGetCurrentDisplay(),
716 cur, cur, eglGetCurrentContext());
717
718 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
719 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700720#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700721
722}
723
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724void SurfaceFlinger::postFramebuffer()
725{
Mathias Agopian841cde52012-03-01 15:44:37 -0800726 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800727
Mathias Agopiana44b0412011-10-16 18:46:35 -0700728 const nsecs_t now = systemTime();
729 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700730
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700731 HWComposer& hwc(getHwComposer());
732
Mathias Agopian92a979a2012-08-02 18:32:23 -0700733 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700734 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700735 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700736 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700737 const size_t count = currentLayers.size();
738 HWComposer::LayerListIterator cur = hwc.begin();
739 const HWComposer::LayerListIterator end = hwc.end();
740 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
741 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700742 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700743 }
744 }
Mathias Agopian42977342012-08-05 00:40:46 -0700745 hw->flip(hw->swapRegion);
746 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700747 }
748
Jesse Hallef194142012-06-14 14:45:17 -0700749 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700750 // FIXME: eventually commit() won't take arguments
Mathias Agopian42977342012-08-05 00:40:46 -0700751 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700752 }
753
Mathias Agopian92a979a2012-08-02 18:32:23 -0700754 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700755 sp<const DisplayDevice> hw(mDisplays[dpy]);
756 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700757 const size_t count = currentLayers.size();
758 if (hwc.initCheck() == NO_ERROR) {
759 HWComposer::LayerListIterator cur = hwc.begin();
760 const HWComposer::LayerListIterator end = hwc.end();
761 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700762 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700763 }
764 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700765 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700766 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700767 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700768 }
Jesse Hallef194142012-06-14 14:45:17 -0700769 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700770
771 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800772 }
773
Mathias Agopiana44b0412011-10-16 18:46:35 -0700774 mLastSwapBufferTime = systemTime() - now;
775 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776}
777
Mathias Agopian87baae12012-07-31 12:38:26 -0700778void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779{
Mathias Agopian841cde52012-03-01 15:44:37 -0800780 ATRACE_CALL();
781
Mathias Agopianca4d3602011-05-19 15:38:14 -0700782 Mutex::Autolock _l(mStateLock);
783 const nsecs_t now = systemTime();
784 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785
Mathias Agopianca4d3602011-05-19 15:38:14 -0700786 // Here we're guaranteed that some transaction flags are set
787 // so we can call handleTransactionLocked() unconditionally.
788 // We call getTransactionFlags(), which will also clear the flags,
789 // with mStateLock held to guarantee that mCurrentState won't change
790 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700791
Mathias Agopianca4d3602011-05-19 15:38:14 -0700792 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
793 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700794 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700795
Mathias Agopianca4d3602011-05-19 15:38:14 -0700796 mLastTransactionTime = systemTime() - now;
797 mDebugInTransaction = 0;
798 invalidateHwcGeometry();
799 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700800}
801
Mathias Agopian87baae12012-07-31 12:38:26 -0700802void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700803{
804 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 const size_t count = currentLayers.size();
806
807 /*
808 * Traversal of the children
809 * (perform the transaction for each of them if needed)
810 */
811
812 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
813 if (layersNeedTransaction) {
814 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700815 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
817 if (!trFlags) continue;
818
819 const uint32_t flags = layer->doTransaction(0);
820 if (flags & Layer::eVisibleRegion)
821 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 }
823 }
824
825 /*
826 * Perform our own transaction if needed
827 */
828
829 if (transactionFlags & eTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700830 // here we take advantage of Vector's copy-on-write semantics to
831 // improve performance by skipping the transaction entirely when
832 // know that the lists are identical
833 const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
834 const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
835 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700837 const size_t cc = curr.size();
838 const size_t dc = draw.size();
839
840 // find the displays that were removed
841 // (ie: in drawing state but not in current state)
842 // also handle displays that changed
843 // (ie: displays that are in both lists)
844 for (size_t i=0 ; i<dc ; i++) {
845 if (curr.indexOfKey(draw[i].id) < 0) {
846 // in drawing state but not in current state
847 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700848 mDisplays.removeItem(draw[i].id);
849 } else {
850 ALOGW("trying to remove the main display");
851 }
852 } else {
853 // this display is in both lists. see if something changed.
854 const DisplayDeviceState& state(curr[i]);
855 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700856 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
857 //disp->setLayerStack(state.layerStack); // FIXME: set layer stack
Mathias Agopian92a979a2012-08-02 18:32:23 -0700858 }
859 if (curr[i].orientation != draw[i].orientation) {
Mathias Agopian42977342012-08-05 00:40:46 -0700860 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
861 disp->setOrientation(state.orientation);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700862 }
863 }
864 }
865
866 // find displays that were added
867 // (ie: in current state but not in drawing state)
868 for (size_t i=0 ; i<cc ; i++) {
869 if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
870 // FIXME: we need to pass the surface here
Mathias Agopian42977342012-08-05 00:40:46 -0700871 sp<DisplayDevice> disp = new DisplayDevice(this, curr[i].id, 0, mEGLConfig);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700872 mDisplays.add(curr[i].id, disp);
873 }
874 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 }
876
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700877 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
878 // layers have been added
879 mVisibleRegionsDirty = true;
880 }
881
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 // some layers might have been removed, so
883 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700884 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700885 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700887 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700888 const size_t count = previousLayers.size();
889 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700890 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700891 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700892 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700893 // TODO: we could traverse the tree from front to back and
894 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700895 // TODO: we could cache the transformed region
896 Layer::State front(layer->drawingState());
897 Region visibleReg = front.transform.transform(
898 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700899 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700900 }
901 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 }
904
905 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700906}
907
908void SurfaceFlinger::commitTransaction()
909{
910 if (!mLayersPendingRemoval.isEmpty()) {
911 // Notify removed layers now that they can't be drawn from
912 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
913 mLayersPendingRemoval[i]->onRemoved();
914 }
915 mLayersPendingRemoval.clear();
916 }
917
918 mDrawingState = mCurrentState;
919 mTransationPending = false;
920 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921}
922
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700924 const LayerVector& currentLayers, uint32_t layerStack,
925 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926{
Mathias Agopian841cde52012-03-01 15:44:37 -0800927 ATRACE_CALL();
928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 Region aboveOpaqueLayers;
930 Region aboveCoveredLayers;
931 Region dirty;
932
Mathias Agopian87baae12012-07-31 12:38:26 -0700933 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934
935 size_t i = currentLayers.size();
936 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700937 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938
939 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700940 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941
Mathias Agopian87baae12012-07-31 12:38:26 -0700942 // only consider the layers on the given later stack
943 if (s.layerStack != layerStack)
944 continue;
945
Mathias Agopianab028732010-03-16 16:41:46 -0700946 /*
947 * opaqueRegion: area of a surface that is fully opaque.
948 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700950
951 /*
952 * visibleRegion: area of a surface that is visible on screen
953 * and not fully transparent. This is essentially the layer's
954 * footprint minus the opaque regions above it.
955 * Areas covered by a translucent surface are considered visible.
956 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700958
959 /*
960 * coveredRegion: area of a surface that is covered by all
961 * visible regions above it (which includes the translucent areas).
962 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700964
965
966 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800967 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700968 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700969 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700971 if (!visibleRegion.isEmpty()) {
972 // Remove the transparent area from the visible region
973 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700974 Region transparentRegionScreen;
975 const Transform tr(s.transform);
976 if (tr.transformed()) {
977 if (tr.preserveRects()) {
978 // transform the transparent region
979 transparentRegionScreen = tr.transform(s.transparentRegion);
980 } else {
981 // transformation too complex, can't do the
982 // transparent region optimization.
983 transparentRegionScreen.clear();
984 }
985 } else {
986 transparentRegionScreen = s.transparentRegion;
987 }
988 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700989 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990
Mathias Agopianab028732010-03-16 16:41:46 -0700991 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700992 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700993 if (s.alpha==255 && !translucent &&
994 ((layerOrientation & Transform::ROT_INVALID) == false)) {
995 // the opaque region is the layer's footprint
996 opaqueRegion = visibleRegion;
997 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 }
999 }
1000
Mathias Agopianab028732010-03-16 16:41:46 -07001001 // Clip the covered region to the visible region
1002 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1003
1004 // Update aboveCoveredLayers for next (lower) layer
1005 aboveCoveredLayers.orSelf(visibleRegion);
1006
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 // subtract the opaque region covered by the layers above us
1008 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009
1010 // compute this layer's dirty region
1011 if (layer->contentDirty) {
1012 // we need to invalidate the whole region
1013 dirty = visibleRegion;
1014 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001015 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 layer->contentDirty = false;
1017 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001018 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001019 * the exposed region consists of two components:
1020 * 1) what's VISIBLE now and was COVERED before
1021 * 2) what's EXPOSED now less what was EXPOSED before
1022 *
1023 * note that (1) is conservative, we start with the whole
1024 * visible region but only keep what used to be covered by
1025 * something -- which mean it may have been exposed.
1026 *
1027 * (2) handles areas that were not covered by anything but got
1028 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001029 */
Mathias Agopianab028732010-03-16 16:41:46 -07001030 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001031 const Region oldVisibleRegion = layer->visibleRegion;
1032 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001033 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1034 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 }
1036 dirty.subtractSelf(aboveOpaqueLayers);
1037
1038 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001039 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040
Mathias Agopianab028732010-03-16 16:41:46 -07001041 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 // Store the visible region is screen space
1045 layer->setVisibleRegion(visibleRegion);
1046 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 }
1048
Mathias Agopian87baae12012-07-31 12:38:26 -07001049 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050}
1051
Mathias Agopian87baae12012-07-31 12:38:26 -07001052void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1053 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001054 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001055 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1056 if (hw->getLayerStack() == layerStack) {
1057 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001058 }
1059 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001060}
1061
1062void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001064 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001065 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001067 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001068
Mathias Agopian4fec8732012-06-29 14:12:52 -07001069 bool visibleRegions = false;
1070 const size_t count = currentLayers.size();
1071 sp<LayerBase> const* layers = currentLayers.array();
1072 for (size_t i=0 ; i<count ; i++) {
1073 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001074 const Region dirty(layer->latchBuffer(visibleRegions));
1075 Layer::State s(layer->drawingState());
1076 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001077 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001078
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001079 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080}
1081
Mathias Agopianad456f92011-01-13 17:53:01 -08001082void SurfaceFlinger::invalidateHwcGeometry()
1083{
1084 mHwWorkListDirty = true;
1085}
1086
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001087void SurfaceFlinger::handleRefresh()
1088{
1089 bool needInvalidate = false;
1090 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1091 const size_t count = currentLayers.size();
1092 for (size_t i=0 ; i<count ; i++) {
1093 const sp<LayerBase>& layer(currentLayers[i]);
1094 if (layer->onPreComposition()) {
1095 needInvalidate = true;
1096 }
1097 }
1098 if (needInvalidate) {
1099 signalLayerUpdate();
1100 }
1101}
1102
Mathias Agopian42977342012-08-05 00:40:46 -07001103void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001104 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105{
Mathias Agopian841cde52012-03-01 15:44:37 -08001106 ATRACE_CALL();
1107
Mathias Agopian87baae12012-07-31 12:38:26 -07001108 Region dirtyRegion(inDirtyRegion);
1109
Mathias Agopianb8a55602009-06-26 19:06:36 -07001110 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001111 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112
Glenn Kasten99ed2242011-12-15 09:51:17 -08001113 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001114 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 }
1116
Mathias Agopian42977342012-08-05 00:40:46 -07001117 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001118 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001119 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1120 // takes a rectangle, we must make sure to update that whole
1121 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001122 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001123 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001124 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001125 // We need to redraw the rectangle that will be updated
1126 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001127 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001128 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001129 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001130 } else {
1131 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001132 dirtyRegion.set(hw->bounds());
1133 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 }
1135 }
1136
Mathias Agopian87baae12012-07-31 12:38:26 -07001137 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001139 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1140 const size_t count = currentLayers.size();
1141 for (size_t i=0 ; i<count ; i++) {
1142 currentLayers[i]->onPostComposition();
1143 }
1144
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001145 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001146 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147}
1148
Mathias Agopian42977342012-08-05 00:40:46 -07001149void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001150{
Mathias Agopian86303202012-07-24 22:46:10 -07001151 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001152 HWComposer::LayerListIterator cur = hwc.begin();
1153 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001154
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001155 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001156 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001157
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001158 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001159
1160 // set the frame buffer
1161 glMatrixMode(GL_MODELVIEW);
1162 glLoadIdentity();
1163
1164 // Never touch the framebuffer if we don't have any framebuffer layers
1165 if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
Mathias Agopianb9494d52012-04-18 02:28:45 -07001166 // when using overlays, we assume a fully transparent framebuffer
1167 // NOTE: we could reduce how much we need to clear, for instance
1168 // remove where there are opaque FB layers. however, on some
1169 // GPUs doing a "clean slate" glClear might be more efficient.
1170 // We'll revisit later if needed.
1171 glClearColor(0, 0, 0, 0);
1172 glClear(GL_COLOR_BUFFER_BIT);
1173 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001174 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001175 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001176 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001177 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001178 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001179 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001180 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001181
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001182 /*
1183 * and then, render the layers targeted at the framebuffer
1184 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001185
Mathias Agopian42977342012-08-05 00:40:46 -07001186 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001187 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001188 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001189 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001190 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001191 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001192 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001193 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001194 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001195 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001196 // never clear the very first layer since we're
1197 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001198 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001199 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001200 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001201 continue;
1202 }
1203 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001204 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001205 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001206 if (cur != end) {
1207 ++cur;
1208 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001209 }
1210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211}
1212
Mathias Agopian42977342012-08-05 00:40:46 -07001213void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001214 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215{
Mathias Agopian42977342012-08-05 00:40:46 -07001216 const uint32_t flags = hw->getFlags();
1217 const int32_t height = hw->getHeight();
1218 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001219 return;
1220 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001221
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001222 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1223 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001224 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001225 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001226 }
1227
Mathias Agopianc492e672011-10-18 14:49:27 -07001228 glDisable(GL_TEXTURE_EXTERNAL_OES);
1229 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231
Mathias Agopian0926f502009-05-04 14:17:04 -07001232 static int toggle = 0;
1233 toggle = 1 - toggle;
1234 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001235 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001236 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001237 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001238 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239
Mathias Agopian87baae12012-07-31 12:38:26 -07001240 Region::const_iterator it = dirtyRegion.begin();
1241 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001242 while (it != end) {
1243 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001245 { r.left, height - r.top },
1246 { r.left, height - r.bottom },
1247 { r.right, height - r.bottom },
1248 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 };
1250 glVertexPointer(2, GL_FLOAT, 0, vertices);
1251 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1252 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001253
Mathias Agopian42977342012-08-05 00:40:46 -07001254 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255
1256 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001257 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258}
1259
Mathias Agopian87baae12012-07-31 12:38:26 -07001260void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001262 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001263 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001264 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001265 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001266
1267 GLfloat vertices[4][2];
1268 glVertexPointer(2, GL_FLOAT, 0, vertices);
1269 Region::const_iterator it = region.begin();
1270 Region::const_iterator const end = region.end();
1271 while (it != end) {
1272 const Rect& r = *it++;
1273 vertices[0][0] = r.left;
1274 vertices[0][1] = r.top;
1275 vertices[1][0] = r.right;
1276 vertices[1][1] = r.top;
1277 vertices[2][0] = r.right;
1278 vertices[2][1] = r.bottom;
1279 vertices[3][0] = r.left;
1280 vertices[3][1] = r.bottom;
1281 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1282 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283}
1284
Mathias Agopian96f08192010-06-02 23:28:45 -07001285ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1286 const sp<LayerBaseClient>& lbc)
1287{
Mathias Agopian96f08192010-06-02 23:28:45 -07001288 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001289 size_t name = client->attachLayer(lbc);
1290
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001292 Mutex::Autolock _l(mStateLock);
1293 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001294
Mathias Agopian4f113742011-05-03 16:21:41 -07001295 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001296}
1297
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001298status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299{
1300 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001301 status_t err = purgatorizeLayer_l(layer);
1302 if (err == NO_ERROR)
1303 setTransactionFlags(eTransactionNeeded);
1304 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305}
1306
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001307status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308{
1309 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1310 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001311 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 return NO_ERROR;
1313 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001314 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315}
1316
Mathias Agopian9a112062009-04-17 19:36:26 -07001317status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1318{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001319 // First add the layer to the purgatory list, which makes sure it won't
1320 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001321 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001322 if (err >= 0) {
1323 mLayerPurgatory.add(layerBase);
1324 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001325
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001326 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001327
Mathias Agopian3d579642009-06-04 18:46:21 -07001328 // it's possible that we don't find a layer, because it might
1329 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001330 // from the user because there is a race between Client::destroySurface(),
1331 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001332 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1333}
1334
Mathias Agopiandea20b12011-05-03 17:04:02 -07001335uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1336{
1337 return android_atomic_release_load(&mTransactionFlags);
1338}
1339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1341{
1342 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1343}
1344
Mathias Agopianbb641242010-05-18 17:06:55 -07001345uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346{
1347 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1348 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001349 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 }
1351 return old;
1352}
1353
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354
Mathias Agopian8b33f032012-07-24 20:43:54 -07001355void SurfaceFlinger::setTransactionState(
1356 const Vector<ComposerState>& state,
1357 const Vector<DisplayState>& displays,
1358 uint32_t flags)
1359{
Mathias Agopian698c0872011-06-28 19:09:31 -07001360 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001361
Mathias Agopian8b33f032012-07-24 20:43:54 -07001362 int orientation = eOrientationUnchanged;
1363 if (displays.size()) {
1364 // TODO: handle all displays
1365 orientation = displays[0].orientation;
1366 }
1367
Jamie Gennis28378392011-10-12 17:39:00 -07001368 uint32_t transactionFlags = 0;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001369 // FIXME: don't hardcode display id here
1370 if (mCurrentState.displays.valueFor(0).orientation != orientation) {
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001371 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001372 mCurrentState.displays.editValueFor(0).orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001373 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001374 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001375 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001376 orientation);
1377 }
1378 }
1379
Mathias Agopian698c0872011-06-28 19:09:31 -07001380 const size_t count = state.size();
1381 for (size_t i=0 ; i<count ; i++) {
1382 const ComposerState& s(state[i]);
1383 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001384 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001385 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001386
Mathias Agopian386aa982011-11-07 21:58:03 -08001387 if (transactionFlags) {
1388 // this triggers the transaction
1389 setTransactionFlags(transactionFlags);
1390
1391 // if this is a synchronous transaction, wait for it to take effect
1392 // before returning.
1393 if (flags & eSynchronous) {
1394 mTransationPending = true;
1395 }
1396 while (mTransationPending) {
1397 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1398 if (CC_UNLIKELY(err != NO_ERROR)) {
1399 // just in case something goes wrong in SF, return to the
1400 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001401 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001402 mTransationPending = false;
1403 break;
1404 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 }
1407}
1408
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001409sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001410 ISurfaceComposerClient::surface_data_t* params,
1411 const String8& name,
1412 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1414 uint32_t flags)
1415{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001416 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001417 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001418
1419 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001420 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001421 int(w), int(h));
1422 return surfaceHandle;
1423 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001424
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001425 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 switch (flags & eFXSurfaceMask) {
1427 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001428 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 break;
1430 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001431 // for now we treat Blur as Dim, until we can implement it
1432 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001434 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001436 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001437 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001438 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439 }
1440
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001441 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001442 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001443 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001444 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001446 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001447 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001448 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001449 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001450 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 }
1452
1453 return surfaceHandle;
1454}
1455
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001456sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001457 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001458 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001459 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460{
1461 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001462 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 case PIXEL_FORMAT_TRANSPARENT:
1464 case PIXEL_FORMAT_TRANSLUCENT:
1465 format = PIXEL_FORMAT_RGBA_8888;
1466 break;
1467 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001468#ifdef NO_RGBX_8888
1469 format = PIXEL_FORMAT_RGB_565;
1470#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001471 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001472#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 break;
1474 }
1475
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001476#ifdef NO_RGBX_8888
1477 if (format == PIXEL_FORMAT_RGBX_8888)
1478 format = PIXEL_FORMAT_RGBA_8888;
1479#endif
1480
Mathias Agopian96f08192010-06-02 23:28:45 -07001481 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001482 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001483 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001484 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001485 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 }
1487 return layer;
1488}
1489
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001490sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001491 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001492 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493{
Mathias Agopian96f08192010-06-02 23:28:45 -07001494 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001495 return layer;
1496}
1497
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001498sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001499 const sp<Client>& client, DisplayID display,
1500 uint32_t w, uint32_t h, uint32_t flags)
1501{
1502 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 return layer;
1504}
1505
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001506status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507{
Mathias Agopian9a112062009-04-17 19:36:26 -07001508 /*
1509 * called by the window manager, when a surface should be marked for
1510 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001511 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001512 * The surface is removed from the current and drawing lists, but placed
1513 * in the purgatory queue, so it's not destroyed right-away (we need
1514 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001515 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001516
Mathias Agopian48d819a2009-09-10 19:41:18 -07001517 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001518 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001519 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001520
Mathias Agopian48d819a2009-09-10 19:41:18 -07001521 if (layer != 0) {
1522 err = purgatorizeLayer_l(layer);
1523 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001524 setTransactionFlags(eTransactionNeeded);
1525 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001526 }
1527 return err;
1528}
1529
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001530status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001531{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001532 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001533 status_t err = NO_ERROR;
1534 sp<LayerBaseClient> l(layer.promote());
1535 if (l != NULL) {
1536 Mutex::Autolock _l(mStateLock);
1537 err = removeLayer_l(l);
1538 if (err == NAME_NOT_FOUND) {
1539 // The surface wasn't in the current list, which means it was
1540 // removed already, which means it is in the purgatory,
1541 // and need to be removed from there.
1542 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001543 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001544 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001545 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001546 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001547 "error removing layer=%p (%s)", l.get(), strerror(-err));
1548 }
1549 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550}
1551
Mathias Agopian698c0872011-06-28 19:09:31 -07001552uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001553 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001554 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001557 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1558 if (layer != 0) {
1559 const uint32_t what = s.what;
1560 if (what & ePositionChanged) {
1561 if (layer->setPosition(s.x, s.y))
1562 flags |= eTraversalNeeded;
1563 }
1564 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001565 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001566 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1567 if (layer->setLayer(s.z)) {
1568 mCurrentState.layersSortedByZ.removeAt(idx);
1569 mCurrentState.layersSortedByZ.add(layer);
1570 // we need traversal (state changed)
1571 // AND transaction (list changed)
1572 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 }
1574 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001575 if (what & eSizeChanged) {
1576 if (layer->setSize(s.w, s.h)) {
1577 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001578 }
1579 }
1580 if (what & eAlphaChanged) {
1581 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1582 flags |= eTraversalNeeded;
1583 }
1584 if (what & eMatrixChanged) {
1585 if (layer->setMatrix(s.matrix))
1586 flags |= eTraversalNeeded;
1587 }
1588 if (what & eTransparentRegionChanged) {
1589 if (layer->setTransparentRegionHint(s.transparentRegion))
1590 flags |= eTraversalNeeded;
1591 }
1592 if (what & eVisibilityChanged) {
1593 if (layer->setFlags(s.flags, s.mask))
1594 flags |= eTraversalNeeded;
1595 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001596 if (what & eCropChanged) {
1597 if (layer->setCrop(s.crop))
1598 flags |= eTraversalNeeded;
1599 }
Mathias Agopian87855782012-07-24 21:41:09 -07001600 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001601 // NOTE: index needs to be calculated before we update the state
1602 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1603 if (layer->setLayerStack(s.layerStack)) {
1604 mCurrentState.layersSortedByZ.removeAt(idx);
1605 mCurrentState.layersSortedByZ.add(layer);
1606 // we need traversal (state changed)
1607 // AND transaction (list changed)
1608 flags |= eTransactionNeeded|eTraversalNeeded;
1609 }
Mathias Agopian87855782012-07-24 21:41:09 -07001610 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001612 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613}
1614
Mathias Agopianb60314a2012-04-10 22:09:54 -07001615// ---------------------------------------------------------------------------
1616
1617void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001618 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001619 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001620 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001621 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001622 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623}
1624
Mathias Agopianb60314a2012-04-10 22:09:54 -07001625void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001626 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001627 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1628 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001629 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001630 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001631 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001632 // from this point on, SF will stop drawing
1633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634}
1635
Colin Cross8e533062012-06-07 13:17:52 -07001636void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001637 class MessageScreenAcquired : public MessageBase {
1638 SurfaceFlinger* flinger;
1639 public:
1640 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1641 virtual bool handler() {
1642 flinger->onScreenAcquired();
1643 return true;
1644 }
1645 };
1646 sp<MessageBase> msg = new MessageScreenAcquired(this);
1647 postMessageSync(msg);
1648}
1649
Colin Cross8e533062012-06-07 13:17:52 -07001650void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001651 class MessageScreenReleased : public MessageBase {
1652 SurfaceFlinger* flinger;
1653 public:
1654 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1655 virtual bool handler() {
1656 flinger->onScreenReleased();
1657 return true;
1658 }
1659 };
1660 sp<MessageBase> msg = new MessageScreenReleased(this);
1661 postMessageSync(msg);
1662}
1663
1664// ---------------------------------------------------------------------------
1665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1667{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001668 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 char buffer[SIZE];
1670 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001671
1672 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 snprintf(buffer, SIZE, "Permission Denial: "
1674 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1675 IPCThreadState::self()->getCallingPid(),
1676 IPCThreadState::self()->getCallingUid());
1677 result.append(buffer);
1678 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001679 // Try to get the main lock, but don't insist if we can't
1680 // (this would indicate SF is stuck, but we want to be able to
1681 // print something in dumpsys).
1682 int retry = 3;
1683 while (mStateLock.tryLock()<0 && --retry>=0) {
1684 usleep(1000000);
1685 }
1686 const bool locked(retry >= 0);
1687 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001688 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001689 "SurfaceFlinger appears to be unresponsive, "
1690 "dumping anyways (no locks held)\n");
1691 result.append(buffer);
1692 }
1693
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001694 bool dumpAll = true;
1695 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001696 size_t numArgs = args.size();
1697 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001698 if ((index < numArgs) &&
1699 (args[index] == String16("--list"))) {
1700 index++;
1701 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001702 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001703 }
1704
1705 if ((index < numArgs) &&
1706 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001707 index++;
1708 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001709 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001710 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001711
1712 if ((index < numArgs) &&
1713 (args[index] == String16("--latency-clear"))) {
1714 index++;
1715 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001716 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001717 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001719
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001720 if (dumpAll) {
1721 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001722 }
1723
Mathias Agopian9795c422009-08-26 16:36:26 -07001724 if (locked) {
1725 mStateLock.unlock();
1726 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 }
1728 write(fd, result.string(), result.size());
1729 return NO_ERROR;
1730}
1731
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001732void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1733 String8& result, char* buffer, size_t SIZE) const
1734{
1735 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1736 const size_t count = currentLayers.size();
1737 for (size_t i=0 ; i<count ; i++) {
1738 const sp<LayerBase>& layer(currentLayers[i]);
1739 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1740 result.append(buffer);
1741 }
1742}
1743
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001744void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1745 String8& result, char* buffer, size_t SIZE) const
1746{
1747 String8 name;
1748 if (index < args.size()) {
1749 name = String8(args[index]);
1750 index++;
1751 }
1752
1753 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1754 const size_t count = currentLayers.size();
1755 for (size_t i=0 ; i<count ; i++) {
1756 const sp<LayerBase>& layer(currentLayers[i]);
1757 if (name.isEmpty()) {
1758 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1759 result.append(buffer);
1760 }
1761 if (name.isEmpty() || (name == layer->getName())) {
1762 layer->dumpStats(result, buffer, SIZE);
1763 }
1764 }
1765}
1766
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001767void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1768 String8& result, char* buffer, size_t SIZE) const
1769{
1770 String8 name;
1771 if (index < args.size()) {
1772 name = String8(args[index]);
1773 index++;
1774 }
1775
1776 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1777 const size_t count = currentLayers.size();
1778 for (size_t i=0 ; i<count ; i++) {
1779 const sp<LayerBase>& layer(currentLayers[i]);
1780 if (name.isEmpty() || (name == layer->getName())) {
1781 layer->clearStats();
1782 }
1783 }
1784}
1785
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001786void SurfaceFlinger::dumpAllLocked(
1787 String8& result, char* buffer, size_t SIZE) const
1788{
1789 // figure out if we're stuck somewhere
1790 const nsecs_t now = systemTime();
1791 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1792 const nsecs_t inTransaction(mDebugInTransaction);
1793 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1794 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1795
1796 /*
1797 * Dump the visible layer list
1798 */
1799 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1800 const size_t count = currentLayers.size();
1801 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1802 result.append(buffer);
1803 for (size_t i=0 ; i<count ; i++) {
1804 const sp<LayerBase>& layer(currentLayers[i]);
1805 layer->dump(result, buffer, SIZE);
1806 }
1807
1808 /*
1809 * Dump the layers in the purgatory
1810 */
1811
1812 const size_t purgatorySize = mLayerPurgatory.size();
1813 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1814 result.append(buffer);
1815 for (size_t i=0 ; i<purgatorySize ; i++) {
1816 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1817 layer->shortDump(result, buffer, SIZE);
1818 }
1819
1820 /*
1821 * Dump SurfaceFlinger global state
1822 */
1823
1824 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1825 result.append(buffer);
1826
Mathias Agopian888c8222012-08-04 21:10:38 -07001827 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001828 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001829 const GLExtensions& extensions(GLExtensions::getInstance());
1830 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1831 extensions.getVendor(),
1832 extensions.getRenderer(),
1833 extensions.getVersion());
1834 result.append(buffer);
1835
1836 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001837 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001838 result.append(buffer);
1839
1840 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1841 result.append(buffer);
1842
Mathias Agopian42977342012-08-05 00:40:46 -07001843 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001844 snprintf(buffer, SIZE,
1845 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001846 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001847 result.append(buffer);
1848 snprintf(buffer, SIZE,
1849 " last eglSwapBuffers() time: %f us\n"
1850 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001851 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001852 " refresh-rate : %f fps\n"
1853 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001854 " y-dpi : %f\n"
1855 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001856 mLastSwapBufferTime/1000.0,
1857 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001858 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001859 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian42977342012-08-05 00:40:46 -07001860 hw->getDpiX(),
1861 hw->getDpiY(),
1862 hw->getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001863 result.append(buffer);
1864
1865 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1866 inSwapBuffersDuration/1000.0);
1867 result.append(buffer);
1868
1869 snprintf(buffer, SIZE, " transaction time: %f us\n",
1870 inTransactionDuration/1000.0);
1871 result.append(buffer);
1872
1873 /*
1874 * VSYNC state
1875 */
1876 mEventThread->dump(result, buffer, SIZE);
1877
1878 /*
1879 * Dump HWComposer state
1880 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001881 snprintf(buffer, SIZE, "h/w composer state:\n");
1882 result.append(buffer);
1883 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1884 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1885 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1886 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07001887 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001888
1889 /*
1890 * Dump gralloc state
1891 */
1892 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1893 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07001894 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001895}
1896
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897status_t SurfaceFlinger::onTransact(
1898 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1899{
1900 switch (code) {
1901 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001902 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001903 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001905 case BLANK:
1906 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 {
1908 // codes that require permission check
1909 IPCThreadState* ipc = IPCThreadState::self();
1910 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001911 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001912 if ((uid != AID_GRAPHICS) &&
1913 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001914 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001915 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1916 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001918 break;
1919 }
1920 case CAPTURE_SCREEN:
1921 {
1922 // codes that require permission check
1923 IPCThreadState* ipc = IPCThreadState::self();
1924 const int pid = ipc->getCallingPid();
1925 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001926 if ((uid != AID_GRAPHICS) &&
1927 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001928 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001929 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1930 return PERMISSION_DENIED;
1931 }
1932 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933 }
1934 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1937 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001938 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001939 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001940 IPCThreadState* ipc = IPCThreadState::self();
1941 const int pid = ipc->getCallingPid();
1942 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001943 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001944 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 return PERMISSION_DENIED;
1946 }
1947 int n;
1948 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001949 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001950 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951 return NO_ERROR;
1952 case 1002: // SHOW_UPDATES
1953 n = data.readInt32();
1954 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001955 invalidateHwcGeometry();
1956 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001957 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001959 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001960 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001962 case 1005:{ // force transaction
1963 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1964 return NO_ERROR;
1965 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001966 case 1006:{ // send empty update
1967 signalRefresh();
1968 return NO_ERROR;
1969 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001970 case 1008: // toggle use of hw composer
1971 n = data.readInt32();
1972 mDebugDisableHWC = n ? 1 : 0;
1973 invalidateHwcGeometry();
1974 repaintEverything();
1975 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001976 case 1009: // toggle use of transform hint
1977 n = data.readInt32();
1978 mDebugDisableTransformHint = n ? 1 : 0;
1979 invalidateHwcGeometry();
1980 repaintEverything();
1981 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001983 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 reply->writeInt32(0);
1985 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001986 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001987 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001988 return NO_ERROR;
1989 case 1013: {
1990 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07001991 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
1992 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001993 }
1994 return NO_ERROR;
1995 }
1996 }
1997 return err;
1998}
1999
Mathias Agopian53331da2011-08-22 21:44:41 -07002000void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002001 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002002 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002003}
2004
Mathias Agopian59119e62010-10-11 12:37:43 -07002005// ---------------------------------------------------------------------------
2006
Mathias Agopian118d0242011-10-13 16:02:48 -07002007status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2008 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2009{
2010 Mutex::Autolock _l(mStateLock);
2011 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2012}
2013
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002014status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2015 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002016{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002017 ATRACE_CALL();
2018
Mathias Agopian59119e62010-10-11 12:37:43 -07002019 if (!GLExtensions::getInstance().haveFramebufferObject())
2020 return INVALID_OPERATION;
2021
2022 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002023 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2024 const uint32_t hw_w = hw->getWidth();
2025 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002026 GLfloat u = 1;
2027 GLfloat v = 1;
2028
2029 // make sure to clear all GL error flags
2030 while ( glGetError() != GL_NO_ERROR ) ;
2031
2032 // create a FBO
2033 GLuint name, tname;
2034 glGenTextures(1, &tname);
2035 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002036 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2037 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2039 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002040 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002041 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002042 GLint tw = (2 << (31 - clz(hw_w)));
2043 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2045 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002046 u = GLfloat(hw_w) / tw;
2047 v = GLfloat(hw_h) / th;
2048 }
2049 glGenFramebuffersOES(1, &name);
2050 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2052 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002053
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002055 glDisable(GL_TEXTURE_EXTERNAL_OES);
2056 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 glClearColor(0,0,0,1);
2058 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002059 glMatrixMode(GL_MODELVIEW);
2060 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002061 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002062 const size_t count = layers.size();
2063 for (size_t i=0 ; i<count ; ++i) {
2064 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002065 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002066 }
2067
Mathias Agopian42977342012-08-05 00:40:46 -07002068 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002069
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070 // back to main framebuffer
2071 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 glDeleteFramebuffersOES(1, &name);
2073
2074 *textureName = tname;
2075 *uOut = u;
2076 *vOut = v;
2077 return NO_ERROR;
2078}
2079
2080// ---------------------------------------------------------------------------
2081
Mathias Agopian74c40c02010-09-29 13:02:36 -07002082status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2083 sp<IMemoryHeap>* heap,
2084 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002085 uint32_t sw, uint32_t sh,
2086 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002087{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002088 ATRACE_CALL();
2089
Mathias Agopian74c40c02010-09-29 13:02:36 -07002090 status_t result = PERMISSION_DENIED;
2091
2092 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002093 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002094 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002095 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002096
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002097 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002098 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002099 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002100
2101 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002102 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2103 const uint32_t hw_w = hw->getWidth();
2104 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002105
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002106 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002107 if (hw->getSecureLayerVisible()) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002108 return PERMISSION_DENIED;
2109 }
2110
2111 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002112 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002113 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002114
2115 sw = (!sw) ? hw_w : sw;
2116 sh = (!sh) ? hw_h : sh;
2117 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002118 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002119
Steve Block9d453682011-12-20 16:23:08 +00002120 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002121 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002122
Mathias Agopian74c40c02010-09-29 13:02:36 -07002123 // make sure to clear all GL error flags
2124 while ( glGetError() != GL_NO_ERROR ) ;
2125
2126 // create a FBO
2127 GLuint name, tname;
2128 glGenRenderbuffersOES(1, &tname);
2129 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2130 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002131
Mathias Agopian74c40c02010-09-29 13:02:36 -07002132 glGenFramebuffersOES(1, &name);
2133 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2134 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2135 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2136
2137 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002138
Mathias Agopian74c40c02010-09-29 13:02:36 -07002139 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2140
2141 // invert everything, b/c glReadPixel() below will invert the FB
2142 glViewport(0, 0, sw, sh);
2143 glMatrixMode(GL_PROJECTION);
2144 glPushMatrix();
2145 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002146 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002147 glMatrixMode(GL_MODELVIEW);
2148
2149 // redraw the screen entirely...
2150 glClearColor(0,0,0,1);
2151 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002152
Jamie Gennis9575f602011-10-07 14:51:16 -07002153 const LayerVector& layers(mDrawingState.layersSortedByZ);
2154 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002155 for (size_t i=0 ; i<count ; ++i) {
2156 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002157 const uint32_t flags = layer->drawingState().flags;
2158 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2159 const uint32_t z = layer->drawingState().z;
2160 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002161 if (filtering) layer->setFiltering(true);
2162 layer->draw(hw);
2163 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002164 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002165 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002166 }
2167
Mathias Agopian74c40c02010-09-29 13:02:36 -07002168 // check for errors and return screen capture
2169 if (glGetError() != GL_NO_ERROR) {
2170 // error while rendering
2171 result = INVALID_OPERATION;
2172 } else {
2173 // allocate shared memory large enough to hold the
2174 // screen capture
2175 sp<MemoryHeapBase> base(
2176 new MemoryHeapBase(size, 0, "screen-capture") );
2177 void* const ptr = base->getBase();
2178 if (ptr) {
2179 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002180 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002181 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2182 if (glGetError() == GL_NO_ERROR) {
2183 *heap = base;
2184 *w = sw;
2185 *h = sh;
2186 *f = PIXEL_FORMAT_RGBA_8888;
2187 result = NO_ERROR;
2188 }
2189 } else {
2190 result = NO_MEMORY;
2191 }
2192 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002193 glViewport(0, 0, hw_w, hw_h);
2194 glMatrixMode(GL_PROJECTION);
2195 glPopMatrix();
2196 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002197 } else {
2198 result = BAD_VALUE;
2199 }
2200
2201 // release FBO resources
2202 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2203 glDeleteRenderbuffersOES(1, &tname);
2204 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002205
Mathias Agopian42977342012-08-05 00:40:46 -07002206 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002207
Steve Block9d453682011-12-20 16:23:08 +00002208 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002209
Mathias Agopian74c40c02010-09-29 13:02:36 -07002210 return result;
2211}
2212
2213
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002214status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2215 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002216 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002217 uint32_t sw, uint32_t sh,
2218 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002219{
2220 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002221 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002222 return BAD_VALUE;
2223
2224 if (!GLExtensions::getInstance().haveFramebufferObject())
2225 return INVALID_OPERATION;
2226
2227 class MessageCaptureScreen : public MessageBase {
2228 SurfaceFlinger* flinger;
2229 DisplayID dpy;
2230 sp<IMemoryHeap>* heap;
2231 uint32_t* w;
2232 uint32_t* h;
2233 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234 uint32_t sw;
2235 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002236 uint32_t minLayerZ;
2237 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002238 status_t result;
2239 public:
2240 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002242 uint32_t sw, uint32_t sh,
2243 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002244 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002245 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2246 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2247 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002248 {
2249 }
2250 status_t getResult() const {
2251 return result;
2252 }
2253 virtual bool handler() {
2254 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002255 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002256 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002257 return true;
2258 }
2259 };
2260
2261 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002262 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002263 status_t res = postMessageSync(msg);
2264 if (res == NO_ERROR) {
2265 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2266 }
2267 return res;
2268}
2269
2270// ---------------------------------------------------------------------------
2271
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002272SurfaceFlinger::LayerVector::LayerVector() {
2273}
2274
2275SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2276 : SortedVector<sp<LayerBase> >(rhs) {
2277}
2278
2279int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2280 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002281{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002282 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002283 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2284 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002285
2286 uint32_t ls = l->currentState().layerStack;
2287 uint32_t rs = r->currentState().layerStack;
2288 if (ls != rs)
2289 return ls - rs;
2290
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002291 uint32_t lz = l->currentState().z;
2292 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002293 if (lz != rz)
2294 return lz - rz;
2295
2296 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002297}
2298
2299// ---------------------------------------------------------------------------
2300
Mathias Agopian92a979a2012-08-02 18:32:23 -07002301SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2302 : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002303}
2304
2305// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002306
Jamie Gennis9a78c902011-01-12 18:30:40 -08002307GraphicBufferAlloc::GraphicBufferAlloc() {}
2308
2309GraphicBufferAlloc::~GraphicBufferAlloc() {}
2310
2311sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002312 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002313 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2314 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002315 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002316 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002317 if (err == NO_MEMORY) {
2318 GraphicBuffer::dumpAllocationsToSystemLog();
2319 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002320 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002321 "failed (%s), handle=%p",
2322 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002323 return 0;
2324 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002325 return graphicBuffer;
2326}
2327
Jamie Gennis9a78c902011-01-12 18:30:40 -08002328// ---------------------------------------------------------------------------
2329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002330}; // namespace android