blob: 63f3d91f076553a3a782056bc1894ced6e285da0 [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 Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
39#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/SurfaceTextureClient.h>
41
42#include <ui/GraphicBufferAllocator.h>
43#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/String8.h>
46#include <utils/String16.h>
47#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080048#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian921e6ac2012-07-23 23:11:29 -070050#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080053#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070055#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070060#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana4912602012-07-12 14:25:33 -070063#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070064#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070086 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -070098 mBootFinished(false)
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;
Mathias Agopian818b4602012-08-16 20:57:39 -0700144 d.what = DisplayState::eOrientationChanged;
Jesse Hallea599df2012-08-13 14:47:01 -0700145 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
Mathias Agopian3165cc22012-08-08 19:42:09 -0700146 d.orientation = DisplayState::eOrientationDefault;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700147 displays.add(d);
148 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149
150 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700151 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152}
153
Mathias Agopian7e27f052010-05-28 14:22:23 -0700154sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian96f08192010-06-02 23:28:45 -0700156 sp<ISurfaceComposerClient> bclient;
157 sp<Client> client(new Client(this));
158 status_t err = client->initCheck();
159 if (err == NO_ERROR) {
160 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return bclient;
163}
164
Mathias Agopiane57f2922012-08-09 16:29:12 -0700165sp<IBinder> SurfaceFlinger::createDisplay()
166{
167 class DisplayToken : public BBinder {
168 sp<SurfaceFlinger> flinger;
169 virtual ~DisplayToken() {
170 // no more references, this display must be terminated
171 Mutex::Autolock _l(flinger->mStateLock);
172 flinger->mCurrentState.displays.removeItem(this);
173 flinger->setTransactionFlags(eDisplayTransactionNeeded);
174 }
175 public:
176 DisplayToken(const sp<SurfaceFlinger>& flinger)
177 : flinger(flinger) {
178 }
179 };
180
181 sp<BBinder> token = new DisplayToken(this);
182
183 Mutex::Autolock _l(mStateLock);
184 DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
185 mCurrentState.displays.add(token, info);
186
187 return token;
188}
189
190sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
191 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
192 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
193 return NULL;
194 }
195 return mDefaultDisplays[id];
196}
197
Jamie Gennis9a78c902011-01-12 18:30:40 -0800198sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
199{
200 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
201 return gba;
202}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204void SurfaceFlinger::bootFinished()
205{
206 const nsecs_t now = systemTime();
207 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000208 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700209 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700210
211 // wait patiently for the window manager death
212 const String16 name("window");
213 sp<IBinder> window(defaultServiceManager()->getService(name));
214 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700215 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700216 }
217
218 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700219 // formerly we would just kill the process, but we now ask it to exit so it
220 // can choose where to stop the animation.
221 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222}
223
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700224void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
225 class MessageDestroyGLTexture : public MessageBase {
226 GLuint texture;
227 public:
228 MessageDestroyGLTexture(GLuint texture)
229 : texture(texture) {
230 }
231 virtual bool handler() {
232 glDeleteTextures(1, &texture);
233 return true;
234 }
235 };
236 postMessageAsync(new MessageDestroyGLTexture(texture));
237}
238
Mathias Agopiana4912602012-07-12 14:25:33 -0700239status_t SurfaceFlinger::selectConfigForPixelFormat(
240 EGLDisplay dpy,
241 EGLint const* attrs,
242 PixelFormat format,
243 EGLConfig* outConfig)
244{
245 EGLConfig config = NULL;
246 EGLint numConfigs = -1, n=0;
247 eglGetConfigs(dpy, NULL, 0, &numConfigs);
248 EGLConfig* const configs = new EGLConfig[numConfigs];
249 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
250 for (int i=0 ; i<n ; i++) {
251 EGLint nativeVisualId = 0;
252 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
253 if (nativeVisualId>0 && format == nativeVisualId) {
254 *outConfig = configs[i];
255 delete [] configs;
256 return NO_ERROR;
257 }
258 }
259 delete [] configs;
260 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261}
262
Mathias Agopiana4912602012-07-12 14:25:33 -0700263EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
264 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
265 // it is to be used with WIFI displays
266 EGLConfig config;
267 EGLint dummy;
268 status_t err;
269 EGLint attribs[] = {
270 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
271 EGL_RECORDABLE_ANDROID, EGL_TRUE,
272 EGL_NONE
273 };
274 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
275 if (err) {
276 // maybe we failed because of EGL_RECORDABLE_ANDROID
277 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
278 attribs[2] = EGL_NONE;
279 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
280 }
281 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
282 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
283 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
284 }
285 return config;
286}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
Mathias Agopiana4912602012-07-12 14:25:33 -0700288EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
289 // Also create our EGLContext
290 EGLint contextAttributes[] = {
291#ifdef EGL_IMG_context_priority
292#ifdef HAS_CONTEXT_PRIORITY
293#warning "using EGL_IMG_context_priority"
294 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
295#endif
296#endif
297 EGL_NONE, EGL_NONE
298 };
299 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
300 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
301 return ctxt;
302}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
Mathias Agopiana4912602012-07-12 14:25:33 -0700304void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
305 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
306 if (!result) {
307 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
308 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 }
310
Mathias Agopiana4912602012-07-12 14:25:33 -0700311 GLExtensions& extensions(GLExtensions::getInstance());
312 extensions.initWithGLStrings(
313 glGetString(GL_VENDOR),
314 glGetString(GL_RENDERER),
315 glGetString(GL_VERSION),
316 glGetString(GL_EXTENSIONS),
317 eglQueryString(display, EGL_VENDOR),
318 eglQueryString(display, EGL_VERSION),
319 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700320
Mathias Agopiana4912602012-07-12 14:25:33 -0700321 EGLint w, h;
322 eglQuerySurface(display, surface, EGL_WIDTH, &w);
323 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700324
Mathias Agopiana4912602012-07-12 14:25:33 -0700325 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
326 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700329 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 glShadeModel(GL_FLAT);
332 glDisable(GL_DITHER);
333 glDisable(GL_CULL_FACE);
334
Mathias Agopiana4912602012-07-12 14:25:33 -0700335 struct pack565 {
336 inline uint16_t operator() (int r, int g, int b) const {
337 return (r<<11)|(g<<5)|b;
338 }
339 } pack565;
340
Jamie Gennis9575f602011-10-07 14:51:16 -0700341 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
342 glGenTextures(1, &mProtectedTexName);
343 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
344 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
348 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
349 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
351 glViewport(0, 0, w, h);
352 glMatrixMode(GL_PROJECTION);
353 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700354 // put the origin in the left-bottom corner
355 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 -0800356
Mathias Agopiana4912602012-07-12 14:25:33 -0700357 // print some debugging info
358 EGLint r,g,b,a;
359 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
360 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
361 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
362 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
363 ALOGI("EGL informations:");
364 ALOGI("vendor : %s", extensions.getEglVendor());
365 ALOGI("version : %s", extensions.getEglVersion());
366 ALOGI("extensions: %s", extensions.getEglExtension());
367 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
368 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
369 ALOGI("OpenGL ES informations:");
370 ALOGI("vendor : %s", extensions.getVendor());
371 ALOGI("renderer : %s", extensions.getRenderer());
372 ALOGI("version : %s", extensions.getVersion());
373 ALOGI("extensions: %s", extensions.getExtension());
374 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
375 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
376}
377
Mathias Agopiana4912602012-07-12 14:25:33 -0700378status_t SurfaceFlinger::readyToRun()
379{
380 ALOGI( "SurfaceFlinger's main thread ready to run. "
381 "Initializing graphics H/W...");
382
Mathias Agopiana4912602012-07-12 14:25:33 -0700383 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700384 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
385 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700386
387 // Initialize the main display
388 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700389 sp<FramebufferSurface> fbs = FramebufferSurface::create();
390 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700391 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
392 exit(0);
393 }
394
Mathias Agopiane57f2922012-08-09 16:29:12 -0700395 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
396 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700397
Mathias Agopiana4912602012-07-12 14:25:33 -0700398 // initialize the config and context
399 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700400 ANativeWindow* const anw = stc.get();
401 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700402 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
403 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700404
405 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700406
407 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
408 mDefaultDisplays[i] = new BBinder();
409 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
410 }
411 sp<DisplayDevice> hw = new DisplayDevice(this,
412 DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
413 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700414
415 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700416 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700417 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800418
Mathias Agopian028508c2012-07-25 21:12:12 -0700419 // start the EventThread
420 mEventThread = new EventThread(this);
421 mEventQueue.setEventThread(mEventThread);
422
Mathias Agopian86303202012-07-24 22:46:10 -0700423 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700424 mHwc = new HWComposer(this,
425 *static_cast<HWComposer::EventHandler *>(this),
426 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700427
428 // initialize our drawing state
429 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700430
Mathias Agopiana4912602012-07-12 14:25:33 -0700431 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800432 mReadyToRunBarrier.open();
433
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700434 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700435 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 return NO_ERROR;
438}
439
Mathias Agopiana67e4182012-06-19 17:26:12 -0700440void SurfaceFlinger::startBootAnim() {
441 // start boot animation
442 property_set("service.bootanim.exit", "0");
443 property_set("ctl.start", "bootanim");
444}
445
Mathias Agopiana4912602012-07-12 14:25:33 -0700446uint32_t SurfaceFlinger::getMaxTextureSize() const {
447 return mMaxTextureSize;
448}
449
450uint32_t SurfaceFlinger::getMaxViewportDims() const {
451 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
452 mMaxViewportDims[0] : mMaxViewportDims[1];
453}
454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800456
Jamie Gennis582270d2011-08-17 18:19:00 -0700457bool SurfaceFlinger::authenticateSurfaceTexture(
458 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800459 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700460 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800461
462 // Check the visible layer list for the ISurface
463 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
464 size_t count = currentLayers.size();
465 for (size_t i=0 ; i<count ; i++) {
466 const sp<LayerBase>& layer(currentLayers[i]);
467 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700468 if (lbc != NULL) {
469 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
470 if (lbcBinder == surfaceTextureBinder) {
471 return true;
472 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800473 }
474 }
475
476 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700477 // SurfaceTexture gets destroyed before all the clients are done using it,
478 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800479 // will instead fail later on when the client tries to use the surface,
480 // which should be reported as "surface XYZ returned an -ENODEV". The
481 // purgatorized layers are no less authentic than the visible ones, so this
482 // should not cause any harm.
483 size_t purgatorySize = mLayerPurgatory.size();
484 for (size_t i=0 ; i<purgatorySize ; i++) {
485 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
486 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700487 if (lbc != NULL) {
488 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
489 if (lbcBinder == surfaceTextureBinder) {
490 return true;
491 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800492 }
493 }
494
495 return false;
496}
497
Mathias Agopianc666cae2012-07-25 18:56:13 -0700498status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700499 // TODO: this is here only for compatibility -- should go away eventually.
500 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700501 return BAD_INDEX;
502 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700503
504 const HWComposer& hwc(getHwComposer());
505 float xdpi = hwc.getDpiX();
506 float ydpi = hwc.getDpiY();
507
508 // TODO: Not sure if display density should handled by SF any longer
509 class Density {
510 static int getDensityFromProperty(char const* propName) {
511 char property[PROPERTY_VALUE_MAX];
512 int density = 0;
513 if (property_get(propName, property, NULL) > 0) {
514 density = atoi(property);
515 }
516 return density;
517 }
518 public:
519 static int getEmuDensity() {
520 return getDensityFromProperty("qemu.sf.lcd_density"); }
521 static int getBuildDensity() {
522 return getDensityFromProperty("ro.sf.lcd_density"); }
523 };
524 // The density of the device is provided by a build property
525 float density = Density::getBuildDensity() / 160.0f;
526 if (density == 0) {
527 // the build doesn't provide a density -- this is wrong!
528 // use xdpi instead
529 ALOGE("ro.sf.lcd_density must be defined as a build property");
530 density = xdpi / 160.0f;
531 }
532 if (Density::getEmuDensity()) {
533 // if "qemu.sf.lcd_density" is specified, it overrides everything
534 xdpi = ydpi = density = Density::getEmuDensity();
535 density /= 160.0f;
536 }
537
Mathias Agopian42977342012-08-05 00:40:46 -0700538 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
539 info->w = hw->getWidth();
540 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700541 info->xdpi = xdpi;
542 info->ydpi = ydpi;
543 info->fps = float(1e9 / hwc.getRefreshPeriod());
544 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700545 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700546 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700547 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700548 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700549}
550
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800551// ----------------------------------------------------------------------------
552
553sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800554 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700555}
556
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700557void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700558
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700559 sp<IBinder> token;
560 { // scope for the lock
561 Mutex::Autolock _l(mStateLock);
562 token = mExtDisplayToken;
563 }
564
565 if (token == 0) {
566 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700567 }
568
569 { // scope for the lock
570 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700571 if (surface == 0) {
572 // release our current display. we're guarantee to have
573 // a reference to it (token), while we hold the lock
574 mExtDisplayToken = 0;
575 } else {
576 mExtDisplayToken = token;
577 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700578
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700579 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
580 info.surface = surface;
581 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700582 }
583}
584
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800586
587void SurfaceFlinger::waitForEvent() {
588 mEventQueue.waitMessage();
589}
590
591void SurfaceFlinger::signalTransaction() {
592 mEventQueue.invalidate();
593}
594
595void SurfaceFlinger::signalLayerUpdate() {
596 mEventQueue.invalidate();
597}
598
599void SurfaceFlinger::signalRefresh() {
600 mEventQueue.refresh();
601}
602
603status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
604 nsecs_t reltime, uint32_t flags) {
605 return mEventQueue.postMessage(msg, reltime);
606}
607
608status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
609 nsecs_t reltime, uint32_t flags) {
610 status_t res = mEventQueue.postMessage(msg, reltime);
611 if (res == NO_ERROR) {
612 msg->wait();
613 }
614 return res;
615}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616
Mathias Agopian4fec8732012-06-29 14:12:52 -0700617bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 return true;
620}
621
Mathias Agopian86303202012-07-24 22:46:10 -0700622void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700623 mEventThread->onVSyncReceived(dpy, timestamp);
624}
625
626void SurfaceFlinger::eventControl(int event, int enabled) {
627 getHwComposer().eventControl(event, enabled);
628}
629
Mathias Agopian4fec8732012-06-29 14:12:52 -0700630void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800631 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800632 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700633 case MessageQueue::INVALIDATE:
634 handleMessageTransaction();
635 handleMessageInvalidate();
636 signalRefresh();
637 break;
638 case MessageQueue::REFRESH:
639 handleMessageRefresh();
640 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800641 }
642}
643
Mathias Agopian4fec8732012-06-29 14:12:52 -0700644void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700645 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700646 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700647 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700648 }
649}
650
651void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700652 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653}
654
655void SurfaceFlinger::handleMessageRefresh() {
656 handleRefresh();
657
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700658 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700659 mVisibleRegionsDirty = false;
660 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700661
662 /*
663 * rebuild the visible layer list per screen
664 */
665
Mathias Agopian87baae12012-07-31 12:38:26 -0700666 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700667 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700668 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700669 Region opaqueRegion;
670 Region dirtyRegion;
671 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700672 hw->getLayerStack(), dirtyRegion, opaqueRegion);
673 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700674
675 Vector< sp<LayerBase> > layersSortedByZ;
676 const size_t count = currentLayers.size();
677 for (size_t i=0 ; i<count ; i++) {
678 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700679 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700680 if (!currentLayers[i]->visibleRegion.isEmpty()) {
681 layersSortedByZ.add(currentLayers[i]);
682 }
683 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700684 }
Mathias Agopian42977342012-08-05 00:40:46 -0700685 hw->setVisibleLayersSortedByZ(layersSortedByZ);
686 hw->undefinedRegion.set(hw->getBounds());
687 hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700688 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700689 }
690
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700691 HWComposer& hwc(getHwComposer());
692 if (hwc.initCheck() == NO_ERROR) {
693 // build the h/w work list
694 const bool workListsDirty = mHwWorkListDirty;
695 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700696 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700697 sp<const DisplayDevice> hw(mDisplays[dpy]);
698 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700699 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700
Mathias Agopian1e260872012-08-08 18:35:12 -0700701 const int32_t id = hw->getDisplayId();
702 if (hwc.createWorkList(id, count) >= 0) {
703 HWComposer::LayerListIterator cur = hwc.begin(id);
704 const HWComposer::LayerListIterator end = hwc.end(id);
705 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
706 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700707
Mathias Agopian1e260872012-08-08 18:35:12 -0700708 if (CC_UNLIKELY(workListsDirty)) {
709 layer->setGeometry(hw, *cur);
710 if (mDebugDisableHWC || mDebugRegion) {
711 cur->setSkip(true);
712 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700713 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700714
Mathias Agopian1e260872012-08-08 18:35:12 -0700715 /*
716 * update the per-frame h/w composer data for each layer
717 * and build the transparent region of the FB
718 */
719 layer->setPerFrameData(hw, *cur);
720 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700721 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700722 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700723 status_t err = hwc.prepare();
724 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
725 }
726
727 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700728 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700729 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700730
Mathias Agopian87baae12012-07-31 12:38:26 -0700731 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700732 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700733 Region dirtyRegion;
734 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700735 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700736 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700737 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
738 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700739 }
Mathias Agopian42977342012-08-05 00:40:46 -0700740 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700741
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700742 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700743 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700744 // repaint the framebuffer (if needed)
745 handleRepaint(hw, dirtyRegion);
746 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700747 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700748 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700749 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700750 }
751
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700752 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755void SurfaceFlinger::postFramebuffer()
756{
Mathias Agopian841cde52012-03-01 15:44:37 -0800757 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800758
Mathias Agopiana44b0412011-10-16 18:46:35 -0700759 const nsecs_t now = systemTime();
760 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700761
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700762 HWComposer& hwc(getHwComposer());
763
Mathias Agopian92a979a2012-08-02 18:32:23 -0700764 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700765 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700766 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700767 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700768 const size_t count = currentLayers.size();
Mathias Agopian1e260872012-08-08 18:35:12 -0700769 const int32_t id = hw->getDisplayId();
770 HWComposer::LayerListIterator cur = hwc.begin(id);
771 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700772 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
773 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700774 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700775 }
776 }
Mathias Agopian42977342012-08-05 00:40:46 -0700777 hw->flip(hw->swapRegion);
778 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700779 }
780
Jesse Hallef194142012-06-14 14:45:17 -0700781 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700782 // FIXME: eventually commit() won't take arguments
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700783 // FIXME: EGL spec says:
784 // "surface must be bound to the calling thread's current context,
785 // for the current rendering API."
786 DisplayDevice::makeCurrent(getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopian42977342012-08-05 00:40:46 -0700787 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700788 }
789
Mathias Agopian92a979a2012-08-02 18:32:23 -0700790 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700791 sp<const DisplayDevice> hw(mDisplays[dpy]);
792 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700793 const size_t count = currentLayers.size();
794 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700795 int32_t id = hw->getDisplayId();
796 HWComposer::LayerListIterator cur = hwc.begin(id);
797 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700798 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700799 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700800 }
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700801 }
802
803 // FIXME: we need to call eglSwapBuffers() on displays that have
804 // GL composition and only on those.
805 // however, currently hwc.commit() already does that for the main
806 // display and never for the other ones
807 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
808 // FIXME: EGL spec says:
809 // "surface must be bound to the calling thread's current context,
810 // for the current rendering API."
811 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian42977342012-08-05 00:40:46 -0700812 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700813 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700814 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700815 }
Jesse Hallef194142012-06-14 14:45:17 -0700816 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800817 }
818
Mathias Agopiana44b0412011-10-16 18:46:35 -0700819 mLastSwapBufferTime = systemTime() - now;
820 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821}
822
Mathias Agopian87baae12012-07-31 12:38:26 -0700823void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824{
Mathias Agopian841cde52012-03-01 15:44:37 -0800825 ATRACE_CALL();
826
Mathias Agopianca4d3602011-05-19 15:38:14 -0700827 Mutex::Autolock _l(mStateLock);
828 const nsecs_t now = systemTime();
829 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
Mathias Agopianca4d3602011-05-19 15:38:14 -0700831 // Here we're guaranteed that some transaction flags are set
832 // so we can call handleTransactionLocked() unconditionally.
833 // We call getTransactionFlags(), which will also clear the flags,
834 // with mStateLock held to guarantee that mCurrentState won't change
835 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700836
Mathias Agopiane57f2922012-08-09 16:29:12 -0700837 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700838 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700839
Mathias Agopianca4d3602011-05-19 15:38:14 -0700840 mLastTransactionTime = systemTime() - now;
841 mDebugInTransaction = 0;
842 invalidateHwcGeometry();
843 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700844}
845
Mathias Agopian87baae12012-07-31 12:38:26 -0700846void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700847{
848 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 const size_t count = currentLayers.size();
850
851 /*
852 * Traversal of the children
853 * (perform the transaction for each of them if needed)
854 */
855
Mathias Agopian3559b072012-08-15 13:46:03 -0700856 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700858 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
860 if (!trFlags) continue;
861
862 const uint32_t flags = layer->doTransaction(0);
863 if (flags & Layer::eVisibleRegion)
864 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865 }
866 }
867
868 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700869 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870 */
871
Mathias Agopiane57f2922012-08-09 16:29:12 -0700872 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700873 // here we take advantage of Vector's copy-on-write semantics to
874 // improve performance by skipping the transaction entirely when
875 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700876 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
877 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700878 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700880 const size_t cc = curr.size();
881 const size_t dc = draw.size();
882
883 // find the displays that were removed
884 // (ie: in drawing state but not in current state)
885 // also handle displays that changed
886 // (ie: displays that are in both lists)
887 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700888 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
889 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700890 // in drawing state but not in current state
891 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700892 mDisplays.removeItem(draw[i].id);
893 } else {
894 ALOGW("trying to remove the main display");
895 }
896 } else {
897 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700898 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700899 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700900 // changing the surface is like destroying and
901 // recreating the DisplayDevice
902
903 sp<SurfaceTextureClient> stc(
904 new SurfaceTextureClient(state.surface));
905
906 sp<DisplayDevice> disp = new DisplayDevice(this,
907 state.id, stc, 0, mEGLConfig);
908
909 disp->setLayerStack(state.layerStack);
910 disp->setOrientation(state.orientation);
911 // TODO: take viewport and frame into account
912 mDisplays.replaceValueFor(state.id, disp);
913 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700914 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700915 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700916 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700917 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700918 if (state.orientation != draw[i].orientation ||
919 state.viewport != draw[i].viewport ||
920 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700921 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
922 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700923 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700924 }
925 }
926 }
927
928 // find displays that were added
929 // (ie: in current state but not in drawing state)
930 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700931 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700932 const DisplayDeviceState& state(curr[i]);
933 sp<SurfaceTextureClient> stc(
934 new SurfaceTextureClient(state.surface));
935 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
936 stc, 0, mEGLConfig);
937 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700938 }
939 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940 }
Mathias Agopian3559b072012-08-15 13:46:03 -0700941 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942
Mathias Agopian3559b072012-08-15 13:46:03 -0700943 /*
944 * Perform our own transaction if needed
945 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700946
Mathias Agopian3559b072012-08-15 13:46:03 -0700947 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
948 // layers have been added
949 mVisibleRegionsDirty = true;
950 }
951
952 // some layers might have been removed, so
953 // we need to update the regions they're exposing.
954 if (mLayersRemoved) {
955 mLayersRemoved = false;
956 mVisibleRegionsDirty = true;
957 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
958 const size_t count = previousLayers.size();
959 for (size_t i=0 ; i<count ; i++) {
960 const sp<LayerBase>& layer(previousLayers[i]);
961 if (currentLayers.indexOf(layer) < 0) {
962 // this layer is not visible anymore
963 // TODO: we could traverse the tree from front to back and
964 // compute the actual visible region
965 // TODO: we could cache the transformed region
966 Layer::State front(layer->drawingState());
967 Region visibleReg = front.transform.transform(
968 Region(Rect(front.active.w, front.active.h)));
969 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700970 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 }
973
974 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700975}
976
977void SurfaceFlinger::commitTransaction()
978{
979 if (!mLayersPendingRemoval.isEmpty()) {
980 // Notify removed layers now that they can't be drawn from
981 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
982 mLayersPendingRemoval[i]->onRemoved();
983 }
984 mLayersPendingRemoval.clear();
985 }
986
987 mDrawingState = mCurrentState;
988 mTransationPending = false;
989 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990}
991
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700993 const LayerVector& currentLayers, uint32_t layerStack,
994 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995{
Mathias Agopian841cde52012-03-01 15:44:37 -0800996 ATRACE_CALL();
997
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 Region aboveOpaqueLayers;
999 Region aboveCoveredLayers;
1000 Region dirty;
1001
Mathias Agopian87baae12012-07-31 12:38:26 -07001002 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003
1004 size_t i = currentLayers.size();
1005 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001006 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007
1008 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001009 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010
Mathias Agopian87baae12012-07-31 12:38:26 -07001011 // only consider the layers on the given later stack
1012 if (s.layerStack != layerStack)
1013 continue;
1014
Mathias Agopianab028732010-03-16 16:41:46 -07001015 /*
1016 * opaqueRegion: area of a surface that is fully opaque.
1017 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001019
1020 /*
1021 * visibleRegion: area of a surface that is visible on screen
1022 * and not fully transparent. This is essentially the layer's
1023 * footprint minus the opaque regions above it.
1024 * Areas covered by a translucent surface are considered visible.
1025 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001027
1028 /*
1029 * coveredRegion: area of a surface that is covered by all
1030 * visible regions above it (which includes the translucent areas).
1031 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001033
1034
1035 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001036 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001037 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001038 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001040 if (!visibleRegion.isEmpty()) {
1041 // Remove the transparent area from the visible region
1042 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001043 Region transparentRegionScreen;
1044 const Transform tr(s.transform);
1045 if (tr.transformed()) {
1046 if (tr.preserveRects()) {
1047 // transform the transparent region
1048 transparentRegionScreen = tr.transform(s.transparentRegion);
1049 } else {
1050 // transformation too complex, can't do the
1051 // transparent region optimization.
1052 transparentRegionScreen.clear();
1053 }
1054 } else {
1055 transparentRegionScreen = s.transparentRegion;
1056 }
1057 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001058 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059
Mathias Agopianab028732010-03-16 16:41:46 -07001060 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001061 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001062 if (s.alpha==255 && !translucent &&
1063 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1064 // the opaque region is the layer's footprint
1065 opaqueRegion = visibleRegion;
1066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 }
1068 }
1069
Mathias Agopianab028732010-03-16 16:41:46 -07001070 // Clip the covered region to the visible region
1071 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1072
1073 // Update aboveCoveredLayers for next (lower) layer
1074 aboveCoveredLayers.orSelf(visibleRegion);
1075
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 // subtract the opaque region covered by the layers above us
1077 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078
1079 // compute this layer's dirty region
1080 if (layer->contentDirty) {
1081 // we need to invalidate the whole region
1082 dirty = visibleRegion;
1083 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001084 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 layer->contentDirty = false;
1086 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001087 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001088 * the exposed region consists of two components:
1089 * 1) what's VISIBLE now and was COVERED before
1090 * 2) what's EXPOSED now less what was EXPOSED before
1091 *
1092 * note that (1) is conservative, we start with the whole
1093 * visible region but only keep what used to be covered by
1094 * something -- which mean it may have been exposed.
1095 *
1096 * (2) handles areas that were not covered by anything but got
1097 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001098 */
Mathias Agopianab028732010-03-16 16:41:46 -07001099 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001100 const Region oldVisibleRegion = layer->visibleRegion;
1101 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001102 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1103 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 }
1105 dirty.subtractSelf(aboveOpaqueLayers);
1106
1107 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001108 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109
Mathias Agopianab028732010-03-16 16:41:46 -07001110 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 // Store the visible region is screen space
1114 layer->setVisibleRegion(visibleRegion);
1115 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
1117
Mathias Agopian87baae12012-07-31 12:38:26 -07001118 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119}
1120
Mathias Agopian87baae12012-07-31 12:38:26 -07001121void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1122 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001123 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001124 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1125 if (hw->getLayerStack() == layerStack) {
1126 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001127 }
1128 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001129}
1130
1131void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001133 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001134 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001136 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001137
Mathias Agopian4fec8732012-06-29 14:12:52 -07001138 bool visibleRegions = false;
1139 const size_t count = currentLayers.size();
1140 sp<LayerBase> const* layers = currentLayers.array();
1141 for (size_t i=0 ; i<count ; i++) {
1142 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001143 const Region dirty(layer->latchBuffer(visibleRegions));
1144 Layer::State s(layer->drawingState());
1145 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001146 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001147
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001148 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149}
1150
Mathias Agopianad456f92011-01-13 17:53:01 -08001151void SurfaceFlinger::invalidateHwcGeometry()
1152{
1153 mHwWorkListDirty = true;
1154}
1155
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001156void SurfaceFlinger::handleRefresh()
1157{
1158 bool needInvalidate = false;
1159 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1160 const size_t count = currentLayers.size();
1161 for (size_t i=0 ; i<count ; i++) {
1162 const sp<LayerBase>& layer(currentLayers[i]);
1163 if (layer->onPreComposition()) {
1164 needInvalidate = true;
1165 }
1166 }
1167 if (needInvalidate) {
1168 signalLayerUpdate();
1169 }
1170}
1171
Mathias Agopian42977342012-08-05 00:40:46 -07001172void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001173 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174{
Mathias Agopian841cde52012-03-01 15:44:37 -08001175 ATRACE_CALL();
1176
Mathias Agopian87baae12012-07-31 12:38:26 -07001177 Region dirtyRegion(inDirtyRegion);
1178
Mathias Agopianb8a55602009-06-26 19:06:36 -07001179 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001180 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181
Glenn Kasten99ed2242011-12-15 09:51:17 -08001182 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001183 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 }
1185
Mathias Agopian42977342012-08-05 00:40:46 -07001186 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001187 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001188 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1189 // takes a rectangle, we must make sure to update that whole
1190 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001191 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001192 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001193 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001194 // We need to redraw the rectangle that will be updated
1195 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001196 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001197 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001198 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001199 } else {
1200 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001201 dirtyRegion.set(hw->bounds());
1202 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 }
1204 }
1205
Mathias Agopian87baae12012-07-31 12:38:26 -07001206 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001208 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1209 const size_t count = currentLayers.size();
1210 for (size_t i=0 ; i<count ; i++) {
1211 currentLayers[i]->onPostComposition();
1212 }
1213
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001214 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001215 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216}
1217
Mathias Agopian42977342012-08-05 00:40:46 -07001218void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001219{
Mathias Agopian86303202012-07-24 22:46:10 -07001220 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001221 int32_t id = hw->getDisplayId();
1222 HWComposer::LayerListIterator cur = hwc.begin(id);
1223 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001224
Mathias Agopian1e260872012-08-08 18:35:12 -07001225 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001226 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001227
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001228 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001229
1230 // set the frame buffer
1231 glMatrixMode(GL_MODELVIEW);
1232 glLoadIdentity();
1233
1234 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001235 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001236 // when using overlays, we assume a fully transparent framebuffer
1237 // NOTE: we could reduce how much we need to clear, for instance
1238 // remove where there are opaque FB layers. however, on some
1239 // GPUs doing a "clean slate" glClear might be more efficient.
1240 // We'll revisit later if needed.
1241 glClearColor(0, 0, 0, 0);
1242 glClear(GL_COLOR_BUFFER_BIT);
1243 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001244 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001245 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001246 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001247 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001248 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001249 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001250 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001251
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001252 /*
1253 * and then, render the layers targeted at the framebuffer
1254 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001255
Mathias Agopian42977342012-08-05 00:40:46 -07001256 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001257 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001258 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001259 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001260 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001261 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001262 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001263 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001264 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001265 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001266 // never clear the very first layer since we're
1267 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001268 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001269 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001270 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001271 continue;
1272 }
1273 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001274 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001275 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001276 if (cur != end) {
1277 ++cur;
1278 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001279 }
1280 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281}
1282
Mathias Agopian42977342012-08-05 00:40:46 -07001283void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001284 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285{
Mathias Agopian42977342012-08-05 00:40:46 -07001286 const uint32_t flags = hw->getFlags();
1287 const int32_t height = hw->getHeight();
1288 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001289 return;
1290 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001291
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001292 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1293 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001294 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001295 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001296 }
1297
Mathias Agopianc492e672011-10-18 14:49:27 -07001298 glDisable(GL_TEXTURE_EXTERNAL_OES);
1299 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301
Mathias Agopian0926f502009-05-04 14:17:04 -07001302 static int toggle = 0;
1303 toggle = 1 - toggle;
1304 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001305 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001306 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001307 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001308 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309
Mathias Agopian87baae12012-07-31 12:38:26 -07001310 Region::const_iterator it = dirtyRegion.begin();
1311 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001312 while (it != end) {
1313 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001315 { r.left, height - r.top },
1316 { r.left, height - r.bottom },
1317 { r.right, height - r.bottom },
1318 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 };
1320 glVertexPointer(2, GL_FLOAT, 0, vertices);
1321 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1322 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001323
Mathias Agopian42977342012-08-05 00:40:46 -07001324 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325
1326 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001327 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328}
1329
Mathias Agopian87baae12012-07-31 12:38:26 -07001330void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001332 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001333 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001334 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001335 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001336
1337 GLfloat vertices[4][2];
1338 glVertexPointer(2, GL_FLOAT, 0, vertices);
1339 Region::const_iterator it = region.begin();
1340 Region::const_iterator const end = region.end();
1341 while (it != end) {
1342 const Rect& r = *it++;
1343 vertices[0][0] = r.left;
1344 vertices[0][1] = r.top;
1345 vertices[1][0] = r.right;
1346 vertices[1][1] = r.top;
1347 vertices[2][0] = r.right;
1348 vertices[2][1] = r.bottom;
1349 vertices[3][0] = r.left;
1350 vertices[3][1] = r.bottom;
1351 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1352 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353}
1354
Mathias Agopian96f08192010-06-02 23:28:45 -07001355ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1356 const sp<LayerBaseClient>& lbc)
1357{
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001359 size_t name = client->attachLayer(lbc);
1360
Mathias Agopian96f08192010-06-02 23:28:45 -07001361 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001362 Mutex::Autolock _l(mStateLock);
1363 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001364
Mathias Agopian4f113742011-05-03 16:21:41 -07001365 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001366}
1367
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001368status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369{
1370 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001371 status_t err = purgatorizeLayer_l(layer);
1372 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001373 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001374 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375}
1376
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378{
1379 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1380 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001381 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 return NO_ERROR;
1383 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001384 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385}
1386
Mathias Agopian9a112062009-04-17 19:36:26 -07001387status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1388{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001389 // First add the layer to the purgatory list, which makes sure it won't
1390 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001391 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001392 if (err >= 0) {
1393 mLayerPurgatory.add(layerBase);
1394 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001395
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001396 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001397
Mathias Agopian3d579642009-06-04 18:46:21 -07001398 // it's possible that we don't find a layer, because it might
1399 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001400 // from the user because there is a race between Client::destroySurface(),
1401 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001402 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1403}
1404
Mathias Agopiandea20b12011-05-03 17:04:02 -07001405uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1406{
1407 return android_atomic_release_load(&mTransactionFlags);
1408}
1409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1411{
1412 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1413}
1414
Mathias Agopianbb641242010-05-18 17:06:55 -07001415uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416{
1417 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1418 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001419 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 }
1421 return old;
1422}
1423
Mathias Agopian8b33f032012-07-24 20:43:54 -07001424void SurfaceFlinger::setTransactionState(
1425 const Vector<ComposerState>& state,
1426 const Vector<DisplayState>& displays,
1427 uint32_t flags)
1428{
Mathias Agopian698c0872011-06-28 19:09:31 -07001429 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001430 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001431
1432 size_t count = displays.size();
1433 for (size_t i=0 ; i<count ; i++) {
1434 const DisplayState& s(displays[i]);
1435 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001436 }
1437
Mathias Agopiane57f2922012-08-09 16:29:12 -07001438 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001439 for (size_t i=0 ; i<count ; i++) {
1440 const ComposerState& s(state[i]);
1441 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001442 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001443 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001444
Mathias Agopian386aa982011-11-07 21:58:03 -08001445 if (transactionFlags) {
1446 // this triggers the transaction
1447 setTransactionFlags(transactionFlags);
1448
1449 // if this is a synchronous transaction, wait for it to take effect
1450 // before returning.
1451 if (flags & eSynchronous) {
1452 mTransationPending = true;
1453 }
1454 while (mTransationPending) {
1455 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1456 if (CC_UNLIKELY(err != NO_ERROR)) {
1457 // just in case something goes wrong in SF, return to the
1458 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001459 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001460 mTransationPending = false;
1461 break;
1462 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001463 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 }
1465}
1466
Mathias Agopiane57f2922012-08-09 16:29:12 -07001467uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1468{
1469 uint32_t flags = 0;
1470 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1471 if (disp.id >= 0) {
1472 const uint32_t what = s.what;
1473 if (what & DisplayState::eSurfaceChanged) {
1474 if (disp.surface->asBinder() != s.surface->asBinder()) {
1475 disp.surface = s.surface;
1476 flags |= eDisplayTransactionNeeded;
1477 }
1478 }
1479 if (what & DisplayState::eLayerStackChanged) {
1480 if (disp.layerStack != s.layerStack) {
1481 disp.layerStack = s.layerStack;
1482 flags |= eDisplayTransactionNeeded;
1483 }
1484 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001485 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001486 if (disp.orientation != s.orientation) {
1487 disp.orientation = s.orientation;
1488 flags |= eDisplayTransactionNeeded;
1489 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001490 }
1491 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001492 if (disp.frame != s.frame) {
1493 disp.frame = s.frame;
1494 flags |= eDisplayTransactionNeeded;
1495 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001496 }
1497 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001498 if (disp.viewport != s.viewport) {
1499 disp.viewport = s.viewport;
1500 flags |= eDisplayTransactionNeeded;
1501 }
1502 }
1503 }
1504 return flags;
1505}
1506
1507uint32_t SurfaceFlinger::setClientStateLocked(
1508 const sp<Client>& client,
1509 const layer_state_t& s)
1510{
1511 uint32_t flags = 0;
1512 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1513 if (layer != 0) {
1514 const uint32_t what = s.what;
1515 if (what & layer_state_t::ePositionChanged) {
1516 if (layer->setPosition(s.x, s.y))
1517 flags |= eTraversalNeeded;
1518 }
1519 if (what & layer_state_t::eLayerChanged) {
1520 // NOTE: index needs to be calculated before we update the state
1521 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1522 if (layer->setLayer(s.z)) {
1523 mCurrentState.layersSortedByZ.removeAt(idx);
1524 mCurrentState.layersSortedByZ.add(layer);
1525 // we need traversal (state changed)
1526 // AND transaction (list changed)
1527 flags |= eTransactionNeeded|eTraversalNeeded;
1528 }
1529 }
1530 if (what & layer_state_t::eSizeChanged) {
1531 if (layer->setSize(s.w, s.h)) {
1532 flags |= eTraversalNeeded;
1533 }
1534 }
1535 if (what & layer_state_t::eAlphaChanged) {
1536 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1537 flags |= eTraversalNeeded;
1538 }
1539 if (what & layer_state_t::eMatrixChanged) {
1540 if (layer->setMatrix(s.matrix))
1541 flags |= eTraversalNeeded;
1542 }
1543 if (what & layer_state_t::eTransparentRegionChanged) {
1544 if (layer->setTransparentRegionHint(s.transparentRegion))
1545 flags |= eTraversalNeeded;
1546 }
1547 if (what & layer_state_t::eVisibilityChanged) {
1548 if (layer->setFlags(s.flags, s.mask))
1549 flags |= eTraversalNeeded;
1550 }
1551 if (what & layer_state_t::eCropChanged) {
1552 if (layer->setCrop(s.crop))
1553 flags |= eTraversalNeeded;
1554 }
1555 if (what & layer_state_t::eLayerStackChanged) {
1556 // NOTE: index needs to be calculated before we update the state
1557 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1558 if (layer->setLayerStack(s.layerStack)) {
1559 mCurrentState.layersSortedByZ.removeAt(idx);
1560 mCurrentState.layersSortedByZ.add(layer);
1561 // we need traversal (state changed)
1562 // AND transaction (list changed)
1563 flags |= eTransactionNeeded|eTraversalNeeded;
1564 }
1565 }
1566 }
1567 return flags;
1568}
1569
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001570sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001571 ISurfaceComposerClient::surface_data_t* params,
1572 const String8& name,
1573 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1575 uint32_t flags)
1576{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001577 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001578 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001579
1580 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001581 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001582 int(w), int(h));
1583 return surfaceHandle;
1584 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001585
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001586 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001587 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1588 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001589 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001591 case ISurfaceComposerClient::eFXSurfaceBlur:
1592 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001593 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001595 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001596 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001597 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 }
1599
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001600 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001601 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001602 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001603 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001605 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001606 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001607 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001608 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001609 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 }
1611
1612 return surfaceHandle;
1613}
1614
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001615sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001616 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001617 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001618 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619{
1620 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001621 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 case PIXEL_FORMAT_TRANSPARENT:
1623 case PIXEL_FORMAT_TRANSLUCENT:
1624 format = PIXEL_FORMAT_RGBA_8888;
1625 break;
1626 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001627#ifdef NO_RGBX_8888
1628 format = PIXEL_FORMAT_RGB_565;
1629#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001630 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001631#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 break;
1633 }
1634
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001635#ifdef NO_RGBX_8888
1636 if (format == PIXEL_FORMAT_RGBX_8888)
1637 format = PIXEL_FORMAT_RGBA_8888;
1638#endif
1639
Mathias Agopian96f08192010-06-02 23:28:45 -07001640 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001641 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001642 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001643 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 }
1646 return layer;
1647}
1648
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001649sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001650 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001651 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652{
Mathias Agopian96f08192010-06-02 23:28:45 -07001653 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001654 return layer;
1655}
1656
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001657sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001658 const sp<Client>& client, DisplayID display,
1659 uint32_t w, uint32_t h, uint32_t flags)
1660{
1661 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 return layer;
1663}
1664
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001665status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666{
Mathias Agopian9a112062009-04-17 19:36:26 -07001667 /*
1668 * called by the window manager, when a surface should be marked for
1669 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001670 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001671 * The surface is removed from the current and drawing lists, but placed
1672 * in the purgatory queue, so it's not destroyed right-away (we need
1673 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001674 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001675
Mathias Agopian48d819a2009-09-10 19:41:18 -07001676 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001677 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001678 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001679
Mathias Agopian48d819a2009-09-10 19:41:18 -07001680 if (layer != 0) {
1681 err = purgatorizeLayer_l(layer);
1682 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001683 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001684 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001685 }
1686 return err;
1687}
1688
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001689status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001690{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001691 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001692 status_t err = NO_ERROR;
1693 sp<LayerBaseClient> l(layer.promote());
1694 if (l != NULL) {
1695 Mutex::Autolock _l(mStateLock);
1696 err = removeLayer_l(l);
1697 if (err == NAME_NOT_FOUND) {
1698 // The surface wasn't in the current list, which means it was
1699 // removed already, which means it is in the purgatory,
1700 // and need to be removed from there.
1701 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001702 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001703 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001704 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001705 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001706 "error removing layer=%p (%s)", l.get(), strerror(-err));
1707 }
1708 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709}
1710
Mathias Agopianb60314a2012-04-10 22:09:54 -07001711// ---------------------------------------------------------------------------
1712
1713void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001714 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001715 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001716 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001717 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001718 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001719 mVisibleRegionsDirty = true;
1720 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721}
1722
Mathias Agopianb60314a2012-04-10 22:09:54 -07001723void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001724 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001725 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1726 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001727 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001728 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001729 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001730 // from this point on, SF will stop drawing
1731 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732}
1733
Colin Cross8e533062012-06-07 13:17:52 -07001734void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001735 class MessageScreenAcquired : public MessageBase {
1736 SurfaceFlinger* flinger;
1737 public:
1738 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1739 virtual bool handler() {
1740 flinger->onScreenAcquired();
1741 return true;
1742 }
1743 };
1744 sp<MessageBase> msg = new MessageScreenAcquired(this);
1745 postMessageSync(msg);
1746}
1747
Colin Cross8e533062012-06-07 13:17:52 -07001748void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001749 class MessageScreenReleased : public MessageBase {
1750 SurfaceFlinger* flinger;
1751 public:
1752 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1753 virtual bool handler() {
1754 flinger->onScreenReleased();
1755 return true;
1756 }
1757 };
1758 sp<MessageBase> msg = new MessageScreenReleased(this);
1759 postMessageSync(msg);
1760}
1761
1762// ---------------------------------------------------------------------------
1763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1765{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001766 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 char buffer[SIZE];
1768 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001769
1770 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 snprintf(buffer, SIZE, "Permission Denial: "
1772 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1773 IPCThreadState::self()->getCallingPid(),
1774 IPCThreadState::self()->getCallingUid());
1775 result.append(buffer);
1776 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001777 // Try to get the main lock, but don't insist if we can't
1778 // (this would indicate SF is stuck, but we want to be able to
1779 // print something in dumpsys).
1780 int retry = 3;
1781 while (mStateLock.tryLock()<0 && --retry>=0) {
1782 usleep(1000000);
1783 }
1784 const bool locked(retry >= 0);
1785 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001786 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001787 "SurfaceFlinger appears to be unresponsive, "
1788 "dumping anyways (no locks held)\n");
1789 result.append(buffer);
1790 }
1791
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001792 bool dumpAll = true;
1793 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001794 size_t numArgs = args.size();
1795 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001796 if ((index < numArgs) &&
1797 (args[index] == String16("--list"))) {
1798 index++;
1799 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001800 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001801 }
1802
1803 if ((index < numArgs) &&
1804 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001805 index++;
1806 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001807 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001808 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001809
1810 if ((index < numArgs) &&
1811 (args[index] == String16("--latency-clear"))) {
1812 index++;
1813 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001814 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001815 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001817
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001818 if (dumpAll) {
1819 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001820 }
1821
Mathias Agopian9795c422009-08-26 16:36:26 -07001822 if (locked) {
1823 mStateLock.unlock();
1824 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 }
1826 write(fd, result.string(), result.size());
1827 return NO_ERROR;
1828}
1829
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001830void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1831 String8& result, char* buffer, size_t SIZE) const
1832{
1833 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1834 const size_t count = currentLayers.size();
1835 for (size_t i=0 ; i<count ; i++) {
1836 const sp<LayerBase>& layer(currentLayers[i]);
1837 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1838 result.append(buffer);
1839 }
1840}
1841
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001842void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1843 String8& result, char* buffer, size_t SIZE) const
1844{
1845 String8 name;
1846 if (index < args.size()) {
1847 name = String8(args[index]);
1848 index++;
1849 }
1850
1851 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1852 const size_t count = currentLayers.size();
1853 for (size_t i=0 ; i<count ; i++) {
1854 const sp<LayerBase>& layer(currentLayers[i]);
1855 if (name.isEmpty()) {
1856 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1857 result.append(buffer);
1858 }
1859 if (name.isEmpty() || (name == layer->getName())) {
1860 layer->dumpStats(result, buffer, SIZE);
1861 }
1862 }
1863}
1864
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001865void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1866 String8& result, char* buffer, size_t SIZE) const
1867{
1868 String8 name;
1869 if (index < args.size()) {
1870 name = String8(args[index]);
1871 index++;
1872 }
1873
1874 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1875 const size_t count = currentLayers.size();
1876 for (size_t i=0 ; i<count ; i++) {
1877 const sp<LayerBase>& layer(currentLayers[i]);
1878 if (name.isEmpty() || (name == layer->getName())) {
1879 layer->clearStats();
1880 }
1881 }
1882}
1883
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001884void SurfaceFlinger::dumpAllLocked(
1885 String8& result, char* buffer, size_t SIZE) const
1886{
1887 // figure out if we're stuck somewhere
1888 const nsecs_t now = systemTime();
1889 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1890 const nsecs_t inTransaction(mDebugInTransaction);
1891 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1892 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1893
1894 /*
1895 * Dump the visible layer list
1896 */
1897 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1898 const size_t count = currentLayers.size();
1899 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1900 result.append(buffer);
1901 for (size_t i=0 ; i<count ; i++) {
1902 const sp<LayerBase>& layer(currentLayers[i]);
1903 layer->dump(result, buffer, SIZE);
1904 }
1905
1906 /*
1907 * Dump the layers in the purgatory
1908 */
1909
1910 const size_t purgatorySize = mLayerPurgatory.size();
1911 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1912 result.append(buffer);
1913 for (size_t i=0 ; i<purgatorySize ; i++) {
1914 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1915 layer->shortDump(result, buffer, SIZE);
1916 }
1917
1918 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001919 * Dump Display state
1920 */
1921
1922 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1923 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1924 snprintf(buffer, SIZE,
1925 "+ DisplayDevice[%u]\n"
1926 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1927 "flips=%u, secure=%d, numLayers=%u\n",
1928 dpy,
1929 hw->getDisplayId(), hw->getLayerStack(),
1930 hw->getWidth(), hw->getHeight(),
1931 hw->getOrientation(), hw->getTransform().getType(),
1932 hw->getPageFlipCount(),
1933 hw->getSecureLayerVisible(),
1934 hw->getVisibleLayersSortedByZ().size());
1935 result.append(buffer);
1936 }
1937
1938 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001939 * Dump SurfaceFlinger global state
1940 */
1941
1942 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1943 result.append(buffer);
1944
Mathias Agopian888c8222012-08-04 21:10:38 -07001945 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001946 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001947 const GLExtensions& extensions(GLExtensions::getInstance());
1948 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1949 extensions.getVendor(),
1950 extensions.getRenderer(),
1951 extensions.getVersion());
1952 result.append(buffer);
1953
1954 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001955 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001956 result.append(buffer);
1957
1958 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1959 result.append(buffer);
1960
Mathias Agopian42977342012-08-05 00:40:46 -07001961 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001962 snprintf(buffer, SIZE,
1963 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001964 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001965 result.append(buffer);
1966 snprintf(buffer, SIZE,
1967 " last eglSwapBuffers() time: %f us\n"
1968 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001969 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001970 " refresh-rate : %f fps\n"
1971 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001972 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001973 mLastSwapBufferTime/1000.0,
1974 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001975 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001976 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001977 hwc.getDpiX(),
1978 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001979 result.append(buffer);
1980
1981 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1982 inSwapBuffersDuration/1000.0);
1983 result.append(buffer);
1984
1985 snprintf(buffer, SIZE, " transaction time: %f us\n",
1986 inTransactionDuration/1000.0);
1987 result.append(buffer);
1988
1989 /*
1990 * VSYNC state
1991 */
1992 mEventThread->dump(result, buffer, SIZE);
1993
1994 /*
1995 * Dump HWComposer state
1996 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001997 snprintf(buffer, SIZE, "h/w composer state:\n");
1998 result.append(buffer);
1999 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2000 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2001 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2002 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002003 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002004
2005 /*
2006 * Dump gralloc state
2007 */
2008 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2009 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002010 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002011}
2012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013status_t SurfaceFlinger::onTransact(
2014 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2015{
2016 switch (code) {
2017 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002018 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002020 case BLANK:
2021 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002022 {
2023 // codes that require permission check
2024 IPCThreadState* ipc = IPCThreadState::self();
2025 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002026 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002027 if ((uid != AID_GRAPHICS) &&
2028 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002029 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002030 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2031 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002032 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002033 break;
2034 }
2035 case CAPTURE_SCREEN:
2036 {
2037 // codes that require permission check
2038 IPCThreadState* ipc = IPCThreadState::self();
2039 const int pid = ipc->getCallingPid();
2040 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002041 if ((uid != AID_GRAPHICS) &&
2042 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002043 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002044 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2045 return PERMISSION_DENIED;
2046 }
2047 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048 }
2049 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2052 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002053 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002054 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002055 IPCThreadState* ipc = IPCThreadState::self();
2056 const int pid = ipc->getCallingPid();
2057 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002058 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002059 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060 return PERMISSION_DENIED;
2061 }
2062 int n;
2063 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002064 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002065 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 return NO_ERROR;
2067 case 1002: // SHOW_UPDATES
2068 n = data.readInt32();
2069 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002070 invalidateHwcGeometry();
2071 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002073 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002074 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002075 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002077 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002078 setTransactionFlags(
2079 eTransactionNeeded|
2080 eDisplayTransactionNeeded|
2081 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002082 return NO_ERROR;
2083 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002084 case 1006:{ // send empty update
2085 signalRefresh();
2086 return NO_ERROR;
2087 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002088 case 1008: // toggle use of hw composer
2089 n = data.readInt32();
2090 mDebugDisableHWC = n ? 1 : 0;
2091 invalidateHwcGeometry();
2092 repaintEverything();
2093 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002094 case 1009: // toggle use of transform hint
2095 n = data.readInt32();
2096 mDebugDisableTransformHint = n ? 1 : 0;
2097 invalidateHwcGeometry();
2098 repaintEverything();
2099 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002101 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102 reply->writeInt32(0);
2103 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002104 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002105 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002106 return NO_ERROR;
2107 case 1013: {
2108 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002109 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2110 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111 }
2112 return NO_ERROR;
2113 }
2114 }
2115 return err;
2116}
2117
Mathias Agopian53331da2011-08-22 21:44:41 -07002118void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002119 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002120 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002121}
2122
Mathias Agopian59119e62010-10-11 12:37:43 -07002123// ---------------------------------------------------------------------------
2124
Mathias Agopian118d0242011-10-13 16:02:48 -07002125status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2126 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2127{
2128 Mutex::Autolock _l(mStateLock);
2129 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2130}
2131
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002132status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2133 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002134{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002135 ATRACE_CALL();
2136
Mathias Agopian59119e62010-10-11 12:37:43 -07002137 if (!GLExtensions::getInstance().haveFramebufferObject())
2138 return INVALID_OPERATION;
2139
2140 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002141 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2142 const uint32_t hw_w = hw->getWidth();
2143 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002144 GLfloat u = 1;
2145 GLfloat v = 1;
2146
2147 // make sure to clear all GL error flags
2148 while ( glGetError() != GL_NO_ERROR ) ;
2149
2150 // create a FBO
2151 GLuint name, tname;
2152 glGenTextures(1, &tname);
2153 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002154 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2155 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002156 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2157 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002158 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002159 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002160 GLint tw = (2 << (31 - clz(hw_w)));
2161 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2163 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002164 u = GLfloat(hw_w) / tw;
2165 v = GLfloat(hw_h) / th;
2166 }
2167 glGenFramebuffersOES(1, &name);
2168 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2170 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002171
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002172 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002173 glDisable(GL_TEXTURE_EXTERNAL_OES);
2174 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002175 glClearColor(0,0,0,1);
2176 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002177 glMatrixMode(GL_MODELVIEW);
2178 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002179 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002180 const size_t count = layers.size();
2181 for (size_t i=0 ; i<count ; ++i) {
2182 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002183 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002184 }
2185
Mathias Agopian42977342012-08-05 00:40:46 -07002186 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002187
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 // back to main framebuffer
2189 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002190 glDeleteFramebuffersOES(1, &name);
2191
2192 *textureName = tname;
2193 *uOut = u;
2194 *vOut = v;
2195 return NO_ERROR;
2196}
2197
2198// ---------------------------------------------------------------------------
2199
Mathias Agopian74c40c02010-09-29 13:02:36 -07002200status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2201 sp<IMemoryHeap>* heap,
2202 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002203 uint32_t sw, uint32_t sh,
2204 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002205{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002206 ATRACE_CALL();
2207
Mathias Agopian74c40c02010-09-29 13:02:36 -07002208 status_t result = PERMISSION_DENIED;
2209
2210 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002211 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002212 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002213 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002214 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002215
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002216 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002218 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219
2220 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002221 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2222 const uint32_t hw_w = hw->getWidth();
2223 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002224
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002225 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002226 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002227 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002228 return PERMISSION_DENIED;
2229 }
2230
2231 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002232 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002233 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002234 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002235
2236 sw = (!sw) ? hw_w : sw;
2237 sh = (!sh) ? hw_h : sh;
2238 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002239 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002240
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002241// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2242// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002243
Mathias Agopian74c40c02010-09-29 13:02:36 -07002244 // make sure to clear all GL error flags
2245 while ( glGetError() != GL_NO_ERROR ) ;
2246
2247 // create a FBO
2248 GLuint name, tname;
2249 glGenRenderbuffersOES(1, &tname);
2250 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2251 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002252
Mathias Agopian74c40c02010-09-29 13:02:36 -07002253 glGenFramebuffersOES(1, &name);
2254 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2255 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2256 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2257
2258 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002259
Mathias Agopian74c40c02010-09-29 13:02:36 -07002260 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2261
2262 // invert everything, b/c glReadPixel() below will invert the FB
2263 glViewport(0, 0, sw, sh);
2264 glMatrixMode(GL_PROJECTION);
2265 glPushMatrix();
2266 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002267 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002268 glMatrixMode(GL_MODELVIEW);
2269
2270 // redraw the screen entirely...
2271 glClearColor(0,0,0,1);
2272 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002273
Jamie Gennis9575f602011-10-07 14:51:16 -07002274 const LayerVector& layers(mDrawingState.layersSortedByZ);
2275 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002276 for (size_t i=0 ; i<count ; ++i) {
2277 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002278 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002279 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002280 const uint32_t z = layer->drawingState().z;
2281 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002282 if (filtering) layer->setFiltering(true);
2283 layer->draw(hw);
2284 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002285 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002286 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287 }
2288
Mathias Agopian74c40c02010-09-29 13:02:36 -07002289 // check for errors and return screen capture
2290 if (glGetError() != GL_NO_ERROR) {
2291 // error while rendering
2292 result = INVALID_OPERATION;
2293 } else {
2294 // allocate shared memory large enough to hold the
2295 // screen capture
2296 sp<MemoryHeapBase> base(
2297 new MemoryHeapBase(size, 0, "screen-capture") );
2298 void* const ptr = base->getBase();
2299 if (ptr) {
2300 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002301 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2303 if (glGetError() == GL_NO_ERROR) {
2304 *heap = base;
2305 *w = sw;
2306 *h = sh;
2307 *f = PIXEL_FORMAT_RGBA_8888;
2308 result = NO_ERROR;
2309 }
2310 } else {
2311 result = NO_MEMORY;
2312 }
2313 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314 glViewport(0, 0, hw_w, hw_h);
2315 glMatrixMode(GL_PROJECTION);
2316 glPopMatrix();
2317 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002318 } else {
2319 result = BAD_VALUE;
2320 }
2321
2322 // release FBO resources
2323 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2324 glDeleteRenderbuffersOES(1, &tname);
2325 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002326
Mathias Agopian42977342012-08-05 00:40:46 -07002327 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002328
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002329// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002330
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 return result;
2332}
2333
2334
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002335status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2336 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002338 uint32_t sw, uint32_t sh,
2339 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002340{
2341 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002342 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002343 return BAD_VALUE;
2344
2345 if (!GLExtensions::getInstance().haveFramebufferObject())
2346 return INVALID_OPERATION;
2347
2348 class MessageCaptureScreen : public MessageBase {
2349 SurfaceFlinger* flinger;
2350 DisplayID dpy;
2351 sp<IMemoryHeap>* heap;
2352 uint32_t* w;
2353 uint32_t* h;
2354 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355 uint32_t sw;
2356 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002357 uint32_t minLayerZ;
2358 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002359 status_t result;
2360 public:
2361 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002363 uint32_t sw, uint32_t sh,
2364 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002365 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002366 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2367 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2368 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002369 {
2370 }
2371 status_t getResult() const {
2372 return result;
2373 }
2374 virtual bool handler() {
2375 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002377 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002378 return true;
2379 }
2380 };
2381
2382 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002383 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002384 status_t res = postMessageSync(msg);
2385 if (res == NO_ERROR) {
2386 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2387 }
2388 return res;
2389}
2390
2391// ---------------------------------------------------------------------------
2392
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002393SurfaceFlinger::LayerVector::LayerVector() {
2394}
2395
2396SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2397 : SortedVector<sp<LayerBase> >(rhs) {
2398}
2399
2400int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2401 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002402{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002403 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002404 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2405 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002406
2407 uint32_t ls = l->currentState().layerStack;
2408 uint32_t rs = r->currentState().layerStack;
2409 if (ls != rs)
2410 return ls - rs;
2411
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002412 uint32_t lz = l->currentState().z;
2413 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002414 if (lz != rz)
2415 return lz - rz;
2416
2417 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002418}
2419
2420// ---------------------------------------------------------------------------
2421
Mathias Agopiane57f2922012-08-09 16:29:12 -07002422SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2423}
2424
2425SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2426 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002427}
2428
2429// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430
Jamie Gennis9a78c902011-01-12 18:30:40 -08002431GraphicBufferAlloc::GraphicBufferAlloc() {}
2432
2433GraphicBufferAlloc::~GraphicBufferAlloc() {}
2434
2435sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002436 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002437 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2438 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002439 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002440 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002441 if (err == NO_MEMORY) {
2442 GraphicBuffer::dumpAllocationsToSystemLog();
2443 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002444 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002445 "failed (%s), handle=%p",
2446 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002447 return 0;
2448 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002449 return graphicBuffer;
2450}
2451
Jamie Gennis9a78c902011-01-12 18:30:40 -08002452// ---------------------------------------------------------------------------
2453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454}; // namespace android