blob: 9de3649e96f30b3eee7f5a38d02f48cbff0cd745 [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
Andy McFadden53ade082012-08-23 17:36:41 -0700140 // restore initial conditions (default device unblank, etc)
141 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142
143 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700144 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145}
146
Mathias Agopian7e27f052010-05-28 14:22:23 -0700147sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148{
Mathias Agopian96f08192010-06-02 23:28:45 -0700149 sp<ISurfaceComposerClient> bclient;
150 sp<Client> client(new Client(this));
151 status_t err = client->initCheck();
152 if (err == NO_ERROR) {
153 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 return bclient;
156}
157
Mathias Agopiane57f2922012-08-09 16:29:12 -0700158sp<IBinder> SurfaceFlinger::createDisplay()
159{
160 class DisplayToken : public BBinder {
161 sp<SurfaceFlinger> flinger;
162 virtual ~DisplayToken() {
163 // no more references, this display must be terminated
164 Mutex::Autolock _l(flinger->mStateLock);
165 flinger->mCurrentState.displays.removeItem(this);
166 flinger->setTransactionFlags(eDisplayTransactionNeeded);
167 }
168 public:
169 DisplayToken(const sp<SurfaceFlinger>& flinger)
170 : flinger(flinger) {
171 }
172 };
173
174 sp<BBinder> token = new DisplayToken(this);
175
176 Mutex::Autolock _l(mStateLock);
177 DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
178 mCurrentState.displays.add(token, info);
179
180 return token;
181}
182
183sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
184 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
185 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
186 return NULL;
187 }
188 return mDefaultDisplays[id];
189}
190
Jamie Gennis9a78c902011-01-12 18:30:40 -0800191sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
192{
193 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
194 return gba;
195}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197void SurfaceFlinger::bootFinished()
198{
199 const nsecs_t now = systemTime();
200 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000201 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700202 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700203
204 // wait patiently for the window manager death
205 const String16 name("window");
206 sp<IBinder> window(defaultServiceManager()->getService(name));
207 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700208 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700209 }
210
211 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700212 // formerly we would just kill the process, but we now ask it to exit so it
213 // can choose where to stop the animation.
214 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700217void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
218 class MessageDestroyGLTexture : public MessageBase {
219 GLuint texture;
220 public:
221 MessageDestroyGLTexture(GLuint texture)
222 : texture(texture) {
223 }
224 virtual bool handler() {
225 glDeleteTextures(1, &texture);
226 return true;
227 }
228 };
229 postMessageAsync(new MessageDestroyGLTexture(texture));
230}
231
Mathias Agopiana4912602012-07-12 14:25:33 -0700232status_t SurfaceFlinger::selectConfigForPixelFormat(
233 EGLDisplay dpy,
234 EGLint const* attrs,
235 PixelFormat format,
236 EGLConfig* outConfig)
237{
238 EGLConfig config = NULL;
239 EGLint numConfigs = -1, n=0;
240 eglGetConfigs(dpy, NULL, 0, &numConfigs);
241 EGLConfig* const configs = new EGLConfig[numConfigs];
242 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
243 for (int i=0 ; i<n ; i++) {
244 EGLint nativeVisualId = 0;
245 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
246 if (nativeVisualId>0 && format == nativeVisualId) {
247 *outConfig = configs[i];
248 delete [] configs;
249 return NO_ERROR;
250 }
251 }
252 delete [] configs;
253 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254}
255
Mathias Agopiana4912602012-07-12 14:25:33 -0700256EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
257 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
258 // it is to be used with WIFI displays
259 EGLConfig config;
260 EGLint dummy;
261 status_t err;
262 EGLint attribs[] = {
263 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
264 EGL_RECORDABLE_ANDROID, EGL_TRUE,
265 EGL_NONE
266 };
267 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
268 if (err) {
269 // maybe we failed because of EGL_RECORDABLE_ANDROID
270 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
271 attribs[2] = EGL_NONE;
272 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
273 }
274 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
275 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
276 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
277 }
278 return config;
279}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280
Mathias Agopiana4912602012-07-12 14:25:33 -0700281EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
282 // Also create our EGLContext
283 EGLint contextAttributes[] = {
284#ifdef EGL_IMG_context_priority
285#ifdef HAS_CONTEXT_PRIORITY
286#warning "using EGL_IMG_context_priority"
287 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
288#endif
289#endif
290 EGL_NONE, EGL_NONE
291 };
292 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
293 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
294 return ctxt;
295}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
Mathias Agopiana4912602012-07-12 14:25:33 -0700297void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
298 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
299 if (!result) {
300 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
301 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 }
303
Mathias Agopiana4912602012-07-12 14:25:33 -0700304 GLExtensions& extensions(GLExtensions::getInstance());
305 extensions.initWithGLStrings(
306 glGetString(GL_VENDOR),
307 glGetString(GL_RENDERER),
308 glGetString(GL_VERSION),
309 glGetString(GL_EXTENSIONS),
310 eglQueryString(display, EGL_VENDOR),
311 eglQueryString(display, EGL_VERSION),
312 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700313
Mathias Agopiana4912602012-07-12 14:25:33 -0700314 EGLint w, h;
315 eglQuerySurface(display, surface, EGL_WIDTH, &w);
316 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700317
Mathias Agopiana4912602012-07-12 14:25:33 -0700318 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
319 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700322 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 glShadeModel(GL_FLAT);
325 glDisable(GL_DITHER);
326 glDisable(GL_CULL_FACE);
327
Mathias Agopiana4912602012-07-12 14:25:33 -0700328 struct pack565 {
329 inline uint16_t operator() (int r, int g, int b) const {
330 return (r<<11)|(g<<5)|b;
331 }
332 } pack565;
333
Jamie Gennis9575f602011-10-07 14:51:16 -0700334 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
335 glGenTextures(1, &mProtectedTexName);
336 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
337 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
338 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
340 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
341 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
342 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343
344 glViewport(0, 0, w, h);
345 glMatrixMode(GL_PROJECTION);
346 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700347 // put the origin in the left-bottom corner
348 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 -0800349
Mathias Agopiana4912602012-07-12 14:25:33 -0700350 // print some debugging info
351 EGLint r,g,b,a;
352 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
353 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
354 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
355 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
356 ALOGI("EGL informations:");
357 ALOGI("vendor : %s", extensions.getEglVendor());
358 ALOGI("version : %s", extensions.getEglVersion());
359 ALOGI("extensions: %s", extensions.getEglExtension());
360 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
361 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
362 ALOGI("OpenGL ES informations:");
363 ALOGI("vendor : %s", extensions.getVendor());
364 ALOGI("renderer : %s", extensions.getRenderer());
365 ALOGI("version : %s", extensions.getVersion());
366 ALOGI("extensions: %s", extensions.getExtension());
367 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
368 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
369}
370
Mathias Agopiana4912602012-07-12 14:25:33 -0700371status_t SurfaceFlinger::readyToRun()
372{
373 ALOGI( "SurfaceFlinger's main thread ready to run. "
374 "Initializing graphics H/W...");
375
Mathias Agopiana4912602012-07-12 14:25:33 -0700376 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700377 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
378 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700379
380 // Initialize the main display
381 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700382 sp<FramebufferSurface> fbs = FramebufferSurface::create();
383 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700384 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
385 exit(0);
386 }
387
Mathias Agopiane57f2922012-08-09 16:29:12 -0700388 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
389 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700390
Mathias Agopiana4912602012-07-12 14:25:33 -0700391 // initialize the config and context
392 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700393 ANativeWindow* const anw = stc.get();
394 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700395 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
396 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700397
398 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700399
400 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
401 mDefaultDisplays[i] = new BBinder();
402 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
403 }
404 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700405 DisplayDevice::DISPLAY_ID_MAIN, HWC_DISPLAY_PRIMARY,
406 anw, fbs, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700407 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700408
409 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700410 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700411 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800412
Mathias Agopian028508c2012-07-25 21:12:12 -0700413 // start the EventThread
414 mEventThread = new EventThread(this);
415 mEventQueue.setEventThread(mEventThread);
416
Mathias Agopian86303202012-07-24 22:46:10 -0700417 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700418 mHwc = new HWComposer(this,
419 *static_cast<HWComposer::EventHandler *>(this),
420 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700421
422 // initialize our drawing state
423 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700424
Mathias Agopiana4912602012-07-12 14:25:33 -0700425 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800426 mReadyToRunBarrier.open();
427
Andy McFadden53ade082012-08-23 17:36:41 -0700428 // set initial conditions (e.g. unblank default device)
429 initializeDisplays();
430
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700431 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700432 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 return NO_ERROR;
435}
436
Mathias Agopiana67e4182012-06-19 17:26:12 -0700437void SurfaceFlinger::startBootAnim() {
438 // start boot animation
439 property_set("service.bootanim.exit", "0");
440 property_set("ctl.start", "bootanim");
441}
442
Mathias Agopiana4912602012-07-12 14:25:33 -0700443uint32_t SurfaceFlinger::getMaxTextureSize() const {
444 return mMaxTextureSize;
445}
446
447uint32_t SurfaceFlinger::getMaxViewportDims() const {
448 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
449 mMaxViewportDims[0] : mMaxViewportDims[1];
450}
451
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800453
Jamie Gennis582270d2011-08-17 18:19:00 -0700454bool SurfaceFlinger::authenticateSurfaceTexture(
455 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800456 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700457 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800458
459 // Check the visible layer list for the ISurface
460 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
461 size_t count = currentLayers.size();
462 for (size_t i=0 ; i<count ; i++) {
463 const sp<LayerBase>& layer(currentLayers[i]);
464 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700465 if (lbc != NULL) {
466 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
467 if (lbcBinder == surfaceTextureBinder) {
468 return true;
469 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800470 }
471 }
472
473 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700474 // SurfaceTexture gets destroyed before all the clients are done using it,
475 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800476 // will instead fail later on when the client tries to use the surface,
477 // which should be reported as "surface XYZ returned an -ENODEV". The
478 // purgatorized layers are no less authentic than the visible ones, so this
479 // should not cause any harm.
480 size_t purgatorySize = mLayerPurgatory.size();
481 for (size_t i=0 ; i<purgatorySize ; i++) {
482 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
483 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700484 if (lbc != NULL) {
485 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
486 if (lbcBinder == surfaceTextureBinder) {
487 return true;
488 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800489 }
490 }
491
492 return false;
493}
494
Mathias Agopianc666cae2012-07-25 18:56:13 -0700495status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700496 // TODO: this is here only for compatibility -- should go away eventually.
497 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700498 return BAD_INDEX;
499 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700500
501 const HWComposer& hwc(getHwComposer());
502 float xdpi = hwc.getDpiX();
503 float ydpi = hwc.getDpiY();
504
505 // TODO: Not sure if display density should handled by SF any longer
506 class Density {
507 static int getDensityFromProperty(char const* propName) {
508 char property[PROPERTY_VALUE_MAX];
509 int density = 0;
510 if (property_get(propName, property, NULL) > 0) {
511 density = atoi(property);
512 }
513 return density;
514 }
515 public:
516 static int getEmuDensity() {
517 return getDensityFromProperty("qemu.sf.lcd_density"); }
518 static int getBuildDensity() {
519 return getDensityFromProperty("ro.sf.lcd_density"); }
520 };
521 // The density of the device is provided by a build property
522 float density = Density::getBuildDensity() / 160.0f;
523 if (density == 0) {
524 // the build doesn't provide a density -- this is wrong!
525 // use xdpi instead
526 ALOGE("ro.sf.lcd_density must be defined as a build property");
527 density = xdpi / 160.0f;
528 }
529 if (Density::getEmuDensity()) {
530 // if "qemu.sf.lcd_density" is specified, it overrides everything
531 xdpi = ydpi = density = Density::getEmuDensity();
532 density /= 160.0f;
533 }
534
Mathias Agopian42977342012-08-05 00:40:46 -0700535 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
536 info->w = hw->getWidth();
537 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700538 info->xdpi = xdpi;
539 info->ydpi = ydpi;
540 info->fps = float(1e9 / hwc.getRefreshPeriod());
541 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700542 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700543 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700544 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700545 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700546}
547
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800548// ----------------------------------------------------------------------------
549
550sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800551 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700552}
553
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700554void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700555
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700556 sp<IBinder> token;
557 { // scope for the lock
558 Mutex::Autolock _l(mStateLock);
559 token = mExtDisplayToken;
560 }
561
562 if (token == 0) {
563 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700564 }
565
566 { // scope for the lock
567 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700568 if (surface == 0) {
569 // release our current display. we're guarantee to have
570 // a reference to it (token), while we hold the lock
571 mExtDisplayToken = 0;
572 } else {
573 mExtDisplayToken = token;
574 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700575
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700576 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
577 info.surface = surface;
578 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700579 }
580}
581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800583
584void SurfaceFlinger::waitForEvent() {
585 mEventQueue.waitMessage();
586}
587
588void SurfaceFlinger::signalTransaction() {
589 mEventQueue.invalidate();
590}
591
592void SurfaceFlinger::signalLayerUpdate() {
593 mEventQueue.invalidate();
594}
595
596void SurfaceFlinger::signalRefresh() {
597 mEventQueue.refresh();
598}
599
600status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
601 nsecs_t reltime, uint32_t flags) {
602 return mEventQueue.postMessage(msg, reltime);
603}
604
605status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
606 nsecs_t reltime, uint32_t flags) {
607 status_t res = mEventQueue.postMessage(msg, reltime);
608 if (res == NO_ERROR) {
609 msg->wait();
610 }
611 return res;
612}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613
Mathias Agopian4fec8732012-06-29 14:12:52 -0700614bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 return true;
617}
618
Mathias Agopian86303202012-07-24 22:46:10 -0700619void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700620 mEventThread->onVSyncReceived(dpy, timestamp);
621}
622
623void SurfaceFlinger::eventControl(int event, int enabled) {
624 getHwComposer().eventControl(event, enabled);
625}
626
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800628 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800629 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700630 case MessageQueue::INVALIDATE:
631 handleMessageTransaction();
632 handleMessageInvalidate();
633 signalRefresh();
634 break;
635 case MessageQueue::REFRESH:
636 handleMessageRefresh();
637 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800638 }
639}
640
Mathias Agopian4fec8732012-06-29 14:12:52 -0700641void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700642 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700643 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700644 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700645 }
646}
647
648void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700649 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700650 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651}
652
653void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700654 ATRACE_CALL();
655 preComposition();
656 rebuildLayerStacks();
657 setUpHWComposer();
658 doDebugFlashRegions();
659 doComposition();
660 postComposition();
661}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700662
Mathias Agopiancd60f992012-08-16 16:28:27 -0700663void SurfaceFlinger::doDebugFlashRegions()
664{
665 // is debugging enabled
666 if (CC_LIKELY(!mDebugRegion))
667 return;
668
669 const bool repaintEverything = mRepaintEverything;
670 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
671 const sp<DisplayDevice>& hw(mDisplays[dpy]);
672 if (hw->canDraw()) {
673 // transform the dirty region into this screen's coordinate space
674 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
675 if (!dirtyRegion.isEmpty()) {
676 // redraw the whole screen
677 doComposeSurfaces(hw, Region(hw->bounds()));
678
679 // and draw the dirty region
680 glDisable(GL_TEXTURE_EXTERNAL_OES);
681 glDisable(GL_TEXTURE_2D);
682 glDisable(GL_BLEND);
683 glColor4f(1, 0, 1, 1);
684 const int32_t height = hw->getHeight();
685 Region::const_iterator it = dirtyRegion.begin();
686 Region::const_iterator const end = dirtyRegion.end();
687 while (it != end) {
688 const Rect& r = *it++;
689 GLfloat vertices[][2] = {
690 { r.left, height - r.top },
691 { r.left, height - r.bottom },
692 { r.right, height - r.bottom },
693 { r.right, height - r.top }
694 };
695 glVertexPointer(2, GL_FLOAT, 0, vertices);
696 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
697 }
698 hw->compositionComplete();
699 // FIXME
700 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
701 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
702 }
703 }
704 }
705 }
706
707 postFramebuffer();
708
709 if (mDebugRegion > 1) {
710 usleep(mDebugRegion * 1000);
711 }
712}
713
714void SurfaceFlinger::preComposition()
715{
716 bool needExtraInvalidate = false;
717 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
718 const size_t count = currentLayers.size();
719 for (size_t i=0 ; i<count ; i++) {
720 if (currentLayers[i]->onPreComposition()) {
721 needExtraInvalidate = true;
722 }
723 }
724 if (needExtraInvalidate) {
725 signalLayerUpdate();
726 }
727}
728
729void SurfaceFlinger::postComposition()
730{
731 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
732 const size_t count = currentLayers.size();
733 for (size_t i=0 ; i<count ; i++) {
734 currentLayers[i]->onPostComposition();
735 }
736}
737
738void SurfaceFlinger::rebuildLayerStacks() {
739 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700740 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700741 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700742 mVisibleRegionsDirty = false;
743 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700744 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700745 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700746 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700747 Region opaqueRegion;
748 Region dirtyRegion;
749 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700750 hw->getLayerStack(), dirtyRegion, opaqueRegion);
751 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700752
753 Vector< sp<LayerBase> > layersSortedByZ;
754 const size_t count = currentLayers.size();
755 for (size_t i=0 ; i<count ; i++) {
756 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700757 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700758 if (!currentLayers[i]->visibleRegion.isEmpty()) {
759 layersSortedByZ.add(currentLayers[i]);
760 }
761 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700762 }
Mathias Agopian42977342012-08-05 00:40:46 -0700763 hw->setVisibleLayersSortedByZ(layersSortedByZ);
764 hw->undefinedRegion.set(hw->getBounds());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700765 hw->undefinedRegion.subtractSelf(
766 hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700767 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700768 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700770
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700772 HWComposer& hwc(getHwComposer());
773 if (hwc.initCheck() == NO_ERROR) {
774 // build the h/w work list
775 const bool workListsDirty = mHwWorkListDirty;
776 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700777 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700778 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700779 const int32_t id = hw->getHwcDisplayId();
780 if (id >= 0) {
781 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700782 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700783 const size_t count = currentLayers.size();
784 if (hwc.createWorkList(id, count) >= 0) {
785 HWComposer::LayerListIterator cur = hwc.begin(id);
786 const HWComposer::LayerListIterator end = hwc.end(id);
787 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
788 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700789
Mathias Agopiane60b0682012-08-21 23:34:09 -0700790 if (CC_UNLIKELY(workListsDirty)) {
791 layer->setGeometry(hw, *cur);
792 if (mDebugDisableHWC || mDebugRegion) {
793 cur->setSkip(true);
794 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700795 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700796
Mathias Agopiane60b0682012-08-21 23:34:09 -0700797 /*
798 * update the per-frame h/w composer data for each layer
799 * and build the transparent region of the FB
800 */
801 layer->setPerFrameData(hw, *cur);
802 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700803 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700804 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700805 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700806 status_t err = hwc.prepare();
807 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
808 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700810
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811void SurfaceFlinger::doComposition() {
812 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700813 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700814 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700815 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700816 if (hw->canDraw()) {
817 // transform the dirty region into this screen's coordinate space
818 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
819 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700820 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700821 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700822 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700823 hw->dirtyRegion.clear();
824 hw->flip(hw->swapRegion);
825 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700826 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700827 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700828 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700829 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700830 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700831}
832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833void SurfaceFlinger::postFramebuffer()
834{
Mathias Agopian841cde52012-03-01 15:44:37 -0800835 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800836
Mathias Agopiana44b0412011-10-16 18:46:35 -0700837 const nsecs_t now = systemTime();
838 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700839
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700840 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700841 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700842 // FIXME: EGL spec says:
843 // "surface must be bound to the calling thread's current context,
844 // for the current rendering API."
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 DisplayDevice::makeCurrent(
846 getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700847 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 }
849
Mathias Agopian92a979a2012-08-02 18:32:23 -0700850 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700851 sp<const DisplayDevice> hw(mDisplays[dpy]);
852 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700853 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700854 int32_t id = hw->getHwcDisplayId();
855 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700856 HWComposer::LayerListIterator cur = hwc.begin(id);
857 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700858 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700859 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700860 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700862 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700863 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700864 }
Jesse Hallef194142012-06-14 14:45:17 -0700865 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800866 }
867
Mathias Agopiana44b0412011-10-16 18:46:35 -0700868 mLastSwapBufferTime = systemTime() - now;
869 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870}
871
Mathias Agopian87baae12012-07-31 12:38:26 -0700872void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873{
Mathias Agopian841cde52012-03-01 15:44:37 -0800874 ATRACE_CALL();
875
Mathias Agopianca4d3602011-05-19 15:38:14 -0700876 Mutex::Autolock _l(mStateLock);
877 const nsecs_t now = systemTime();
878 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879
Mathias Agopianca4d3602011-05-19 15:38:14 -0700880 // Here we're guaranteed that some transaction flags are set
881 // so we can call handleTransactionLocked() unconditionally.
882 // We call getTransactionFlags(), which will also clear the flags,
883 // with mStateLock held to guarantee that mCurrentState won't change
884 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700885
Mathias Agopiane57f2922012-08-09 16:29:12 -0700886 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700887 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700888
Mathias Agopianca4d3602011-05-19 15:38:14 -0700889 mLastTransactionTime = systemTime() - now;
890 mDebugInTransaction = 0;
891 invalidateHwcGeometry();
892 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700893}
894
Mathias Agopian87baae12012-07-31 12:38:26 -0700895void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700896{
897 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 const size_t count = currentLayers.size();
899
900 /*
901 * Traversal of the children
902 * (perform the transaction for each of them if needed)
903 */
904
Mathias Agopian3559b072012-08-15 13:46:03 -0700905 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700907 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
909 if (!trFlags) continue;
910
911 const uint32_t flags = layer->doTransaction(0);
912 if (flags & Layer::eVisibleRegion)
913 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914 }
915 }
916
917 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700918 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 */
920
Mathias Agopiane57f2922012-08-09 16:29:12 -0700921 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700922 // here we take advantage of Vector's copy-on-write semantics to
923 // improve performance by skipping the transaction entirely when
924 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700925 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
926 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700927 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700929 const size_t cc = curr.size();
930 const size_t dc = draw.size();
931
932 // find the displays that were removed
933 // (ie: in drawing state but not in current state)
934 // also handle displays that changed
935 // (ie: displays that are in both lists)
936 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700937 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
938 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700939 // in drawing state but not in current state
940 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700941 mDisplays.removeItem(draw[i].id);
942 } else {
943 ALOGW("trying to remove the main display");
944 }
945 } else {
946 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700947 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700948 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700949 // changing the surface is like destroying and
950 // recreating the DisplayDevice
Mathias Agopiane60b0682012-08-21 23:34:09 -0700951 const int32_t hwcDisplayId =
952 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
953 state.id : getHwComposer().allocateDisplayId();
Mathias Agopiane57f2922012-08-09 16:29:12 -0700954
955 sp<SurfaceTextureClient> stc(
956 new SurfaceTextureClient(state.surface));
957
958 sp<DisplayDevice> disp = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700959 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700960
961 disp->setLayerStack(state.layerStack);
962 disp->setOrientation(state.orientation);
963 // TODO: take viewport and frame into account
964 mDisplays.replaceValueFor(state.id, disp);
965 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700966 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700967 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700968 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 if (state.orientation != draw[i].orientation ||
971 state.viewport != draw[i].viewport ||
972 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700973 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
974 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700975 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700976 }
977 }
978 }
979
980 // find displays that were added
981 // (ie: in current state but not in drawing state)
982 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700983 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700984 const DisplayDeviceState& state(curr[i]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700985 const int32_t hwcDisplayId =
986 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
987 state.id : getHwComposer().allocateDisplayId();
988
Mathias Agopiane57f2922012-08-09 16:29:12 -0700989 sp<SurfaceTextureClient> stc(
990 new SurfaceTextureClient(state.surface));
Mathias Agopiane60b0682012-08-21 23:34:09 -0700991 sp<DisplayDevice> disp = new DisplayDevice(this,
992 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700993 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700994 }
995 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 }
Mathias Agopian3559b072012-08-15 13:46:03 -0700997 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998
Mathias Agopian3559b072012-08-15 13:46:03 -0700999 /*
1000 * Perform our own transaction if needed
1001 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001002
Mathias Agopiancd60f992012-08-16 16:28:27 -07001003 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1004 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001005 // layers have been added
1006 mVisibleRegionsDirty = true;
1007 }
1008
1009 // some layers might have been removed, so
1010 // we need to update the regions they're exposing.
1011 if (mLayersRemoved) {
1012 mLayersRemoved = false;
1013 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001014 const size_t count = previousLayers.size();
1015 for (size_t i=0 ; i<count ; i++) {
1016 const sp<LayerBase>& layer(previousLayers[i]);
1017 if (currentLayers.indexOf(layer) < 0) {
1018 // this layer is not visible anymore
1019 // TODO: we could traverse the tree from front to back and
1020 // compute the actual visible region
1021 // TODO: we could cache the transformed region
1022 Layer::State front(layer->drawingState());
1023 Region visibleReg = front.transform.transform(
1024 Region(Rect(front.active.w, front.active.h)));
1025 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001026 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 }
1029
1030 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001031}
1032
1033void SurfaceFlinger::commitTransaction()
1034{
1035 if (!mLayersPendingRemoval.isEmpty()) {
1036 // Notify removed layers now that they can't be drawn from
1037 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1038 mLayersPendingRemoval[i]->onRemoved();
1039 }
1040 mLayersPendingRemoval.clear();
1041 }
1042
1043 mDrawingState = mCurrentState;
1044 mTransationPending = false;
1045 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046}
1047
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001049 const LayerVector& currentLayers, uint32_t layerStack,
1050 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051{
Mathias Agopian841cde52012-03-01 15:44:37 -08001052 ATRACE_CALL();
1053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 Region aboveOpaqueLayers;
1055 Region aboveCoveredLayers;
1056 Region dirty;
1057
Mathias Agopian87baae12012-07-31 12:38:26 -07001058 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059
1060 size_t i = currentLayers.size();
1061 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063
1064 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001065 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066
Mathias Agopian87baae12012-07-31 12:38:26 -07001067 // only consider the layers on the given later stack
1068 if (s.layerStack != layerStack)
1069 continue;
1070
Mathias Agopianab028732010-03-16 16:41:46 -07001071 /*
1072 * opaqueRegion: area of a surface that is fully opaque.
1073 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001075
1076 /*
1077 * visibleRegion: area of a surface that is visible on screen
1078 * and not fully transparent. This is essentially the layer's
1079 * footprint minus the opaque regions above it.
1080 * Areas covered by a translucent surface are considered visible.
1081 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001083
1084 /*
1085 * coveredRegion: area of a surface that is covered by all
1086 * visible regions above it (which includes the translucent areas).
1087 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001089
1090
1091 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001092 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001093 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001094 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001096 if (!visibleRegion.isEmpty()) {
1097 // Remove the transparent area from the visible region
1098 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001099 Region transparentRegionScreen;
1100 const Transform tr(s.transform);
1101 if (tr.transformed()) {
1102 if (tr.preserveRects()) {
1103 // transform the transparent region
1104 transparentRegionScreen = tr.transform(s.transparentRegion);
1105 } else {
1106 // transformation too complex, can't do the
1107 // transparent region optimization.
1108 transparentRegionScreen.clear();
1109 }
1110 } else {
1111 transparentRegionScreen = s.transparentRegion;
1112 }
1113 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001114 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115
Mathias Agopianab028732010-03-16 16:41:46 -07001116 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001117 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001118 if (s.alpha==255 && !translucent &&
1119 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1120 // the opaque region is the layer's footprint
1121 opaqueRegion = visibleRegion;
1122 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 }
1124 }
1125
Mathias Agopianab028732010-03-16 16:41:46 -07001126 // Clip the covered region to the visible region
1127 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1128
1129 // Update aboveCoveredLayers for next (lower) layer
1130 aboveCoveredLayers.orSelf(visibleRegion);
1131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 // subtract the opaque region covered by the layers above us
1133 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134
1135 // compute this layer's dirty region
1136 if (layer->contentDirty) {
1137 // we need to invalidate the whole region
1138 dirty = visibleRegion;
1139 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001140 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 layer->contentDirty = false;
1142 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001143 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001144 * the exposed region consists of two components:
1145 * 1) what's VISIBLE now and was COVERED before
1146 * 2) what's EXPOSED now less what was EXPOSED before
1147 *
1148 * note that (1) is conservative, we start with the whole
1149 * visible region but only keep what used to be covered by
1150 * something -- which mean it may have been exposed.
1151 *
1152 * (2) handles areas that were not covered by anything but got
1153 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001154 */
Mathias Agopianab028732010-03-16 16:41:46 -07001155 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001156 const Region oldVisibleRegion = layer->visibleRegion;
1157 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001158 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1159 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 }
1161 dirty.subtractSelf(aboveOpaqueLayers);
1162
1163 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001164 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165
Mathias Agopianab028732010-03-16 16:41:46 -07001166 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 // Store the visible region is screen space
1170 layer->setVisibleRegion(visibleRegion);
1171 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172 }
1173
Mathias Agopian87baae12012-07-31 12:38:26 -07001174 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175}
1176
Mathias Agopian87baae12012-07-31 12:38:26 -07001177void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1178 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001179 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001180 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1181 if (hw->getLayerStack() == layerStack) {
1182 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001183 }
1184 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001185}
1186
1187void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001189 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190
Mathias Agopian4fec8732012-06-29 14:12:52 -07001191 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001192 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001193 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001194 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001195 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001196 const Region dirty(layer->latchBuffer(visibleRegions));
1197 Layer::State s(layer->drawingState());
1198 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001199 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001200
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001201 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202}
1203
Mathias Agopianad456f92011-01-13 17:53:01 -08001204void SurfaceFlinger::invalidateHwcGeometry()
1205{
1206 mHwWorkListDirty = true;
1207}
1208
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001209
Mathias Agopiancd60f992012-08-16 16:28:27 -07001210void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001211 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212{
Mathias Agopian87baae12012-07-31 12:38:26 -07001213 Region dirtyRegion(inDirtyRegion);
1214
Mathias Agopianb8a55602009-06-26 19:06:36 -07001215 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001216 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217
Mathias Agopian42977342012-08-05 00:40:46 -07001218 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001219 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001220 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1221 // takes a rectangle, we must make sure to update that whole
1222 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001223 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001224 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001225 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001226 // We need to redraw the rectangle that will be updated
1227 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001228 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001229 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001230 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001231 } else {
1232 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001233 dirtyRegion.set(hw->bounds());
1234 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 }
1236 }
1237
Mathias Agopiancd60f992012-08-16 16:28:27 -07001238 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239
Mathias Agopiancd60f992012-08-16 16:28:27 -07001240 // FIXME: we need to call eglSwapBuffers() on displays that have
1241 // GL composition and only on those.
1242 // however, currently hwc.commit() already does that for the main
1243 // display and never for the other ones
1244 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
1245 // FIXME: EGL spec says:
1246 // "surface must be bound to the calling thread's current context,
1247 // for the current rendering API."
1248 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001249 }
1250
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001251 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001252 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253}
1254
Mathias Agopiancd60f992012-08-16 16:28:27 -07001255void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001256{
Mathias Agopian86303202012-07-24 22:46:10 -07001257 HWComposer& hwc(getHwComposer());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001258 int32_t id = hw->getHwcDisplayId();
Mathias Agopian1e260872012-08-08 18:35:12 -07001259 HWComposer::LayerListIterator cur = hwc.begin(id);
1260 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001261
Mathias Agopiane60b0682012-08-21 23:34:09 -07001262 const bool hasGlesComposition = hwc.hasGlesComposition(id);
1263 const bool hasHwcComposition = hwc.hasHwcComposition(id);
1264 if (cur==end || hasGlesComposition) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001265
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001266 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001267
1268 // set the frame buffer
1269 glMatrixMode(GL_MODELVIEW);
1270 glLoadIdentity();
1271
1272 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07001273 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001274 // when using overlays, we assume a fully transparent framebuffer
1275 // NOTE: we could reduce how much we need to clear, for instance
1276 // remove where there are opaque FB layers. however, on some
1277 // GPUs doing a "clean slate" glClear might be more efficient.
1278 // We'll revisit later if needed.
1279 glClearColor(0, 0, 0, 0);
1280 glClear(GL_COLOR_BUFFER_BIT);
1281 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001282 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001283 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001284 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001285 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001286 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001287 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001288 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001289
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001290 /*
1291 * and then, render the layers targeted at the framebuffer
1292 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001293
Mathias Agopian42977342012-08-05 00:40:46 -07001294 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001295 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001296 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001297 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001298 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001299 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Jesse Halla6b32db2012-07-19 16:44:38 -07001300 if (cur != end) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001301 // we're using h/w composer
1302 if (!clip.isEmpty()) {
1303 if (cur->getCompositionType() == HWC_OVERLAY) {
1304 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
1305 && layer->isOpaque()) {
1306 // never clear the very first layer since we're
1307 // guaranteed the FB is already cleared
1308 layer->clearWithOpenGL(hw, clip);
1309 }
1310 } else {
1311 layer->draw(hw, clip);
1312 }
1313 layer->setAcquireFence(hw, *cur);
1314 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001315 ++cur;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001316 } else {
1317 // we're not using h/w composer
1318 if (!clip.isEmpty()) {
1319 layer->draw(hw, clip);
1320 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001321 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001322 }
1323 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324}
1325
Mathias Agopian87baae12012-07-31 12:38:26 -07001326void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001328 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001329 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001330 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001331 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001332
1333 GLfloat vertices[4][2];
1334 glVertexPointer(2, GL_FLOAT, 0, vertices);
1335 Region::const_iterator it = region.begin();
1336 Region::const_iterator const end = region.end();
1337 while (it != end) {
1338 const Rect& r = *it++;
1339 vertices[0][0] = r.left;
1340 vertices[0][1] = r.top;
1341 vertices[1][0] = r.right;
1342 vertices[1][1] = r.top;
1343 vertices[2][0] = r.right;
1344 vertices[2][1] = r.bottom;
1345 vertices[3][0] = r.left;
1346 vertices[3][1] = r.bottom;
1347 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1348 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349}
1350
Mathias Agopian96f08192010-06-02 23:28:45 -07001351ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1352 const sp<LayerBaseClient>& lbc)
1353{
Mathias Agopian96f08192010-06-02 23:28:45 -07001354 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001355 size_t name = client->attachLayer(lbc);
1356
Mathias Agopian96f08192010-06-02 23:28:45 -07001357 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001358 Mutex::Autolock _l(mStateLock);
1359 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001360
Mathias Agopian4f113742011-05-03 16:21:41 -07001361 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001362}
1363
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001364status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365{
1366 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001367 status_t err = purgatorizeLayer_l(layer);
1368 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001369 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001370 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371}
1372
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001373status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374{
1375 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1376 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001377 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 return NO_ERROR;
1379 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001380 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381}
1382
Mathias Agopian9a112062009-04-17 19:36:26 -07001383status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1384{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001385 // First add the layer to the purgatory list, which makes sure it won't
1386 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001387 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001388 if (err >= 0) {
1389 mLayerPurgatory.add(layerBase);
1390 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001391
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001392 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001393
Mathias Agopian3d579642009-06-04 18:46:21 -07001394 // it's possible that we don't find a layer, because it might
1395 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001396 // from the user because there is a race between Client::destroySurface(),
1397 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001398 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1399}
1400
Mathias Agopiandea20b12011-05-03 17:04:02 -07001401uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1402{
1403 return android_atomic_release_load(&mTransactionFlags);
1404}
1405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1407{
1408 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1409}
1410
Mathias Agopianbb641242010-05-18 17:06:55 -07001411uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412{
1413 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1414 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001415 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 }
1417 return old;
1418}
1419
Mathias Agopian8b33f032012-07-24 20:43:54 -07001420void SurfaceFlinger::setTransactionState(
1421 const Vector<ComposerState>& state,
1422 const Vector<DisplayState>& displays,
1423 uint32_t flags)
1424{
Mathias Agopian698c0872011-06-28 19:09:31 -07001425 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001426 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001427
1428 size_t count = displays.size();
1429 for (size_t i=0 ; i<count ; i++) {
1430 const DisplayState& s(displays[i]);
1431 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001432 }
1433
Mathias Agopiane57f2922012-08-09 16:29:12 -07001434 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001435 for (size_t i=0 ; i<count ; i++) {
1436 const ComposerState& s(state[i]);
1437 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001438 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001439 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001440
Mathias Agopian386aa982011-11-07 21:58:03 -08001441 if (transactionFlags) {
1442 // this triggers the transaction
1443 setTransactionFlags(transactionFlags);
1444
1445 // if this is a synchronous transaction, wait for it to take effect
1446 // before returning.
1447 if (flags & eSynchronous) {
1448 mTransationPending = true;
1449 }
1450 while (mTransationPending) {
1451 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1452 if (CC_UNLIKELY(err != NO_ERROR)) {
1453 // just in case something goes wrong in SF, return to the
1454 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001455 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001456 mTransationPending = false;
1457 break;
1458 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001459 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 }
1461}
1462
Mathias Agopiane57f2922012-08-09 16:29:12 -07001463uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1464{
1465 uint32_t flags = 0;
1466 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1467 if (disp.id >= 0) {
1468 const uint32_t what = s.what;
1469 if (what & DisplayState::eSurfaceChanged) {
1470 if (disp.surface->asBinder() != s.surface->asBinder()) {
1471 disp.surface = s.surface;
1472 flags |= eDisplayTransactionNeeded;
1473 }
1474 }
1475 if (what & DisplayState::eLayerStackChanged) {
1476 if (disp.layerStack != s.layerStack) {
1477 disp.layerStack = s.layerStack;
1478 flags |= eDisplayTransactionNeeded;
1479 }
1480 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001481 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001482 if (disp.orientation != s.orientation) {
1483 disp.orientation = s.orientation;
1484 flags |= eDisplayTransactionNeeded;
1485 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001486 }
1487 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001488 if (disp.frame != s.frame) {
1489 disp.frame = s.frame;
1490 flags |= eDisplayTransactionNeeded;
1491 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001492 }
1493 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001494 if (disp.viewport != s.viewport) {
1495 disp.viewport = s.viewport;
1496 flags |= eDisplayTransactionNeeded;
1497 }
1498 }
1499 }
1500 return flags;
1501}
1502
1503uint32_t SurfaceFlinger::setClientStateLocked(
1504 const sp<Client>& client,
1505 const layer_state_t& s)
1506{
1507 uint32_t flags = 0;
1508 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1509 if (layer != 0) {
1510 const uint32_t what = s.what;
1511 if (what & layer_state_t::ePositionChanged) {
1512 if (layer->setPosition(s.x, s.y))
1513 flags |= eTraversalNeeded;
1514 }
1515 if (what & layer_state_t::eLayerChanged) {
1516 // NOTE: index needs to be calculated before we update the state
1517 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1518 if (layer->setLayer(s.z)) {
1519 mCurrentState.layersSortedByZ.removeAt(idx);
1520 mCurrentState.layersSortedByZ.add(layer);
1521 // we need traversal (state changed)
1522 // AND transaction (list changed)
1523 flags |= eTransactionNeeded|eTraversalNeeded;
1524 }
1525 }
1526 if (what & layer_state_t::eSizeChanged) {
1527 if (layer->setSize(s.w, s.h)) {
1528 flags |= eTraversalNeeded;
1529 }
1530 }
1531 if (what & layer_state_t::eAlphaChanged) {
1532 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1533 flags |= eTraversalNeeded;
1534 }
1535 if (what & layer_state_t::eMatrixChanged) {
1536 if (layer->setMatrix(s.matrix))
1537 flags |= eTraversalNeeded;
1538 }
1539 if (what & layer_state_t::eTransparentRegionChanged) {
1540 if (layer->setTransparentRegionHint(s.transparentRegion))
1541 flags |= eTraversalNeeded;
1542 }
1543 if (what & layer_state_t::eVisibilityChanged) {
1544 if (layer->setFlags(s.flags, s.mask))
1545 flags |= eTraversalNeeded;
1546 }
1547 if (what & layer_state_t::eCropChanged) {
1548 if (layer->setCrop(s.crop))
1549 flags |= eTraversalNeeded;
1550 }
1551 if (what & layer_state_t::eLayerStackChanged) {
1552 // NOTE: index needs to be calculated before we update the state
1553 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1554 if (layer->setLayerStack(s.layerStack)) {
1555 mCurrentState.layersSortedByZ.removeAt(idx);
1556 mCurrentState.layersSortedByZ.add(layer);
1557 // we need traversal (state changed)
1558 // AND transaction (list changed)
1559 flags |= eTransactionNeeded|eTraversalNeeded;
1560 }
1561 }
1562 }
1563 return flags;
1564}
1565
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001566sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001567 ISurfaceComposerClient::surface_data_t* params,
1568 const String8& name,
1569 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1571 uint32_t flags)
1572{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001573 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001574 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001575
1576 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001577 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001578 int(w), int(h));
1579 return surfaceHandle;
1580 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001581
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001582 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001583 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1584 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001585 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001587 case ISurfaceComposerClient::eFXSurfaceBlur:
1588 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001589 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001591 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001592 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001593 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
1595
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001596 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001597 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001598 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001599 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001601 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001602 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001603 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001604 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001605 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 }
1607
1608 return surfaceHandle;
1609}
1610
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001611sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001612 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001613 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001614 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615{
1616 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001617 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 case PIXEL_FORMAT_TRANSPARENT:
1619 case PIXEL_FORMAT_TRANSLUCENT:
1620 format = PIXEL_FORMAT_RGBA_8888;
1621 break;
1622 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001623#ifdef NO_RGBX_8888
1624 format = PIXEL_FORMAT_RGB_565;
1625#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001626 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001627#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 break;
1629 }
1630
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001631#ifdef NO_RGBX_8888
1632 if (format == PIXEL_FORMAT_RGBX_8888)
1633 format = PIXEL_FORMAT_RGBA_8888;
1634#endif
1635
Mathias Agopian96f08192010-06-02 23:28:45 -07001636 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001637 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001638 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001639 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001640 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 }
1642 return layer;
1643}
1644
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001645sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001646 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001647 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648{
Mathias Agopian96f08192010-06-02 23:28:45 -07001649 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001650 return layer;
1651}
1652
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001653sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001654 const sp<Client>& client, DisplayID display,
1655 uint32_t w, uint32_t h, uint32_t flags)
1656{
1657 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 return layer;
1659}
1660
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001661status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662{
Mathias Agopian9a112062009-04-17 19:36:26 -07001663 /*
1664 * called by the window manager, when a surface should be marked for
1665 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001666 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001667 * The surface is removed from the current and drawing lists, but placed
1668 * in the purgatory queue, so it's not destroyed right-away (we need
1669 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001670 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001671
Mathias Agopian48d819a2009-09-10 19:41:18 -07001672 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001673 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001674 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001675
Mathias Agopian48d819a2009-09-10 19:41:18 -07001676 if (layer != 0) {
1677 err = purgatorizeLayer_l(layer);
1678 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001679 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001680 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001681 }
1682 return err;
1683}
1684
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001685status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001686{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001687 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001688 status_t err = NO_ERROR;
1689 sp<LayerBaseClient> l(layer.promote());
1690 if (l != NULL) {
1691 Mutex::Autolock _l(mStateLock);
1692 err = removeLayer_l(l);
1693 if (err == NAME_NOT_FOUND) {
1694 // The surface wasn't in the current list, which means it was
1695 // removed already, which means it is in the purgatory,
1696 // and need to be removed from there.
1697 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001698 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001699 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001700 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001701 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001702 "error removing layer=%p (%s)", l.get(), strerror(-err));
1703 }
1704 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705}
1706
Mathias Agopianb60314a2012-04-10 22:09:54 -07001707// ---------------------------------------------------------------------------
1708
Andy McFadden53ade082012-08-23 17:36:41 -07001709void SurfaceFlinger::onInitializeDisplays() {
1710 // reset screen orientation
1711 Vector<ComposerState> state;
1712 Vector<DisplayState> displays;
1713 DisplayState d;
1714 d.what = DisplayState::eOrientationChanged;
1715 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
1716 d.orientation = DisplayState::eOrientationDefault;
1717 displays.add(d);
1718 setTransactionState(state, displays, 0);
1719
1720 // XXX: this should init default device to "unblank" and all other devices to "blank"
1721 onScreenAcquired();
1722}
1723
1724void SurfaceFlinger::initializeDisplays() {
1725 class MessageScreenInitialized : public MessageBase {
1726 SurfaceFlinger* flinger;
1727 public:
1728 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1729 virtual bool handler() {
1730 flinger->onInitializeDisplays();
1731 return true;
1732 }
1733 };
1734 sp<MessageBase> msg = new MessageScreenInitialized(this);
1735 postMessageAsync(msg); // we may be called from main thread, use async message
1736}
1737
1738
Mathias Agopianb60314a2012-04-10 22:09:54 -07001739void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001740 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001741 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001742 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001743 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001744 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001745 mVisibleRegionsDirty = true;
1746 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747}
1748
Mathias Agopianb60314a2012-04-10 22:09:54 -07001749void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001750 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001751 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1752 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001753 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001754 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001755 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001756 // from this point on, SF will stop drawing
1757 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758}
1759
Colin Cross8e533062012-06-07 13:17:52 -07001760void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001761 class MessageScreenAcquired : public MessageBase {
1762 SurfaceFlinger* flinger;
1763 public:
1764 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1765 virtual bool handler() {
1766 flinger->onScreenAcquired();
1767 return true;
1768 }
1769 };
1770 sp<MessageBase> msg = new MessageScreenAcquired(this);
1771 postMessageSync(msg);
1772}
1773
Colin Cross8e533062012-06-07 13:17:52 -07001774void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001775 class MessageScreenReleased : public MessageBase {
1776 SurfaceFlinger* flinger;
1777 public:
1778 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1779 virtual bool handler() {
1780 flinger->onScreenReleased();
1781 return true;
1782 }
1783 };
1784 sp<MessageBase> msg = new MessageScreenReleased(this);
1785 postMessageSync(msg);
1786}
1787
1788// ---------------------------------------------------------------------------
1789
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1791{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001792 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 char buffer[SIZE];
1794 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001795
1796 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797 snprintf(buffer, SIZE, "Permission Denial: "
1798 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1799 IPCThreadState::self()->getCallingPid(),
1800 IPCThreadState::self()->getCallingUid());
1801 result.append(buffer);
1802 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001803 // Try to get the main lock, but don't insist if we can't
1804 // (this would indicate SF is stuck, but we want to be able to
1805 // print something in dumpsys).
1806 int retry = 3;
1807 while (mStateLock.tryLock()<0 && --retry>=0) {
1808 usleep(1000000);
1809 }
1810 const bool locked(retry >= 0);
1811 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001812 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001813 "SurfaceFlinger appears to be unresponsive, "
1814 "dumping anyways (no locks held)\n");
1815 result.append(buffer);
1816 }
1817
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001818 bool dumpAll = true;
1819 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001820 size_t numArgs = args.size();
1821 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001822 if ((index < numArgs) &&
1823 (args[index] == String16("--list"))) {
1824 index++;
1825 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001826 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001827 }
1828
1829 if ((index < numArgs) &&
1830 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001831 index++;
1832 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001833 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001834 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001835
1836 if ((index < numArgs) &&
1837 (args[index] == String16("--latency-clear"))) {
1838 index++;
1839 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001840 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001841 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001843
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001844 if (dumpAll) {
1845 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001846 }
1847
Mathias Agopian9795c422009-08-26 16:36:26 -07001848 if (locked) {
1849 mStateLock.unlock();
1850 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 }
1852 write(fd, result.string(), result.size());
1853 return NO_ERROR;
1854}
1855
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001856void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1857 String8& result, char* buffer, size_t SIZE) const
1858{
1859 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1860 const size_t count = currentLayers.size();
1861 for (size_t i=0 ; i<count ; i++) {
1862 const sp<LayerBase>& layer(currentLayers[i]);
1863 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1864 result.append(buffer);
1865 }
1866}
1867
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001868void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1869 String8& result, char* buffer, size_t SIZE) const
1870{
1871 String8 name;
1872 if (index < args.size()) {
1873 name = String8(args[index]);
1874 index++;
1875 }
1876
1877 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1878 const size_t count = currentLayers.size();
1879 for (size_t i=0 ; i<count ; i++) {
1880 const sp<LayerBase>& layer(currentLayers[i]);
1881 if (name.isEmpty()) {
1882 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1883 result.append(buffer);
1884 }
1885 if (name.isEmpty() || (name == layer->getName())) {
1886 layer->dumpStats(result, buffer, SIZE);
1887 }
1888 }
1889}
1890
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001891void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1892 String8& result, char* buffer, size_t SIZE) const
1893{
1894 String8 name;
1895 if (index < args.size()) {
1896 name = String8(args[index]);
1897 index++;
1898 }
1899
1900 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1901 const size_t count = currentLayers.size();
1902 for (size_t i=0 ; i<count ; i++) {
1903 const sp<LayerBase>& layer(currentLayers[i]);
1904 if (name.isEmpty() || (name == layer->getName())) {
1905 layer->clearStats();
1906 }
1907 }
1908}
1909
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001910void SurfaceFlinger::dumpAllLocked(
1911 String8& result, char* buffer, size_t SIZE) const
1912{
1913 // figure out if we're stuck somewhere
1914 const nsecs_t now = systemTime();
1915 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1916 const nsecs_t inTransaction(mDebugInTransaction);
1917 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1918 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1919
1920 /*
1921 * Dump the visible layer list
1922 */
1923 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1924 const size_t count = currentLayers.size();
1925 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1926 result.append(buffer);
1927 for (size_t i=0 ; i<count ; i++) {
1928 const sp<LayerBase>& layer(currentLayers[i]);
1929 layer->dump(result, buffer, SIZE);
1930 }
1931
1932 /*
1933 * Dump the layers in the purgatory
1934 */
1935
1936 const size_t purgatorySize = mLayerPurgatory.size();
1937 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1938 result.append(buffer);
1939 for (size_t i=0 ; i<purgatorySize ; i++) {
1940 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1941 layer->shortDump(result, buffer, SIZE);
1942 }
1943
1944 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001945 * Dump Display state
1946 */
1947
1948 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1949 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1950 snprintf(buffer, SIZE,
1951 "+ DisplayDevice[%u]\n"
1952 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1953 "flips=%u, secure=%d, numLayers=%u\n",
1954 dpy,
1955 hw->getDisplayId(), hw->getLayerStack(),
1956 hw->getWidth(), hw->getHeight(),
1957 hw->getOrientation(), hw->getTransform().getType(),
1958 hw->getPageFlipCount(),
1959 hw->getSecureLayerVisible(),
1960 hw->getVisibleLayersSortedByZ().size());
1961 result.append(buffer);
1962 }
1963
1964 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001965 * Dump SurfaceFlinger global state
1966 */
1967
1968 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1969 result.append(buffer);
1970
Mathias Agopian888c8222012-08-04 21:10:38 -07001971 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001972 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001973 const GLExtensions& extensions(GLExtensions::getInstance());
1974 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1975 extensions.getVendor(),
1976 extensions.getRenderer(),
1977 extensions.getVersion());
1978 result.append(buffer);
1979
1980 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001981 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001982 result.append(buffer);
1983
1984 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1985 result.append(buffer);
1986
Mathias Agopian42977342012-08-05 00:40:46 -07001987 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001988 snprintf(buffer, SIZE,
1989 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001990 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001991 result.append(buffer);
1992 snprintf(buffer, SIZE,
1993 " last eglSwapBuffers() time: %f us\n"
1994 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001995 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001996 " refresh-rate : %f fps\n"
1997 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001998 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001999 mLastSwapBufferTime/1000.0,
2000 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002001 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07002002 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07002003 hwc.getDpiX(),
2004 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002005 result.append(buffer);
2006
2007 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2008 inSwapBuffersDuration/1000.0);
2009 result.append(buffer);
2010
2011 snprintf(buffer, SIZE, " transaction time: %f us\n",
2012 inTransactionDuration/1000.0);
2013 result.append(buffer);
2014
2015 /*
2016 * VSYNC state
2017 */
2018 mEventThread->dump(result, buffer, SIZE);
2019
2020 /*
2021 * Dump HWComposer state
2022 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002023 snprintf(buffer, SIZE, "h/w composer state:\n");
2024 result.append(buffer);
2025 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2026 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2027 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2028 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002029 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002030
2031 /*
2032 * Dump gralloc state
2033 */
2034 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2035 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002036 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002037}
2038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039status_t SurfaceFlinger::onTransact(
2040 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2041{
2042 switch (code) {
2043 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002044 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002045 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002046 case BLANK:
2047 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048 {
2049 // codes that require permission check
2050 IPCThreadState* ipc = IPCThreadState::self();
2051 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002052 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002053 if ((uid != AID_GRAPHICS) &&
2054 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002055 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002056 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2057 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002059 break;
2060 }
2061 case CAPTURE_SCREEN:
2062 {
2063 // codes that require permission check
2064 IPCThreadState* ipc = IPCThreadState::self();
2065 const int pid = ipc->getCallingPid();
2066 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002067 if ((uid != AID_GRAPHICS) &&
2068 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002069 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002070 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2071 return PERMISSION_DENIED;
2072 }
2073 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074 }
2075 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002077 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2078 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002079 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002080 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002081 IPCThreadState* ipc = IPCThreadState::self();
2082 const int pid = ipc->getCallingPid();
2083 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002084 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002085 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086 return PERMISSION_DENIED;
2087 }
2088 int n;
2089 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002090 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002091 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092 return NO_ERROR;
2093 case 1002: // SHOW_UPDATES
2094 n = data.readInt32();
2095 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002096 invalidateHwcGeometry();
2097 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002098 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002099 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002100 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002101 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002103 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002104 setTransactionFlags(
2105 eTransactionNeeded|
2106 eDisplayTransactionNeeded|
2107 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002108 return NO_ERROR;
2109 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002110 case 1006:{ // send empty update
2111 signalRefresh();
2112 return NO_ERROR;
2113 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002114 case 1008: // toggle use of hw composer
2115 n = data.readInt32();
2116 mDebugDisableHWC = n ? 1 : 0;
2117 invalidateHwcGeometry();
2118 repaintEverything();
2119 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002120 case 1009: // toggle use of transform hint
2121 n = data.readInt32();
2122 mDebugDisableTransformHint = n ? 1 : 0;
2123 invalidateHwcGeometry();
2124 repaintEverything();
2125 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002126 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002127 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002128 reply->writeInt32(0);
2129 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002130 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002131 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002132 return NO_ERROR;
2133 case 1013: {
2134 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002135 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2136 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002137 }
2138 return NO_ERROR;
2139 }
2140 }
2141 return err;
2142}
2143
Mathias Agopian53331da2011-08-22 21:44:41 -07002144void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002145 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002146 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002147}
2148
Mathias Agopian59119e62010-10-11 12:37:43 -07002149// ---------------------------------------------------------------------------
2150
Mathias Agopian118d0242011-10-13 16:02:48 -07002151status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2152 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2153{
2154 Mutex::Autolock _l(mStateLock);
2155 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2156}
2157
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002158status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2159 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002160{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002161 ATRACE_CALL();
2162
Mathias Agopian59119e62010-10-11 12:37:43 -07002163 if (!GLExtensions::getInstance().haveFramebufferObject())
2164 return INVALID_OPERATION;
2165
2166 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002167 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2168 const uint32_t hw_w = hw->getWidth();
2169 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002170 GLfloat u = 1;
2171 GLfloat v = 1;
2172
2173 // make sure to clear all GL error flags
2174 while ( glGetError() != GL_NO_ERROR ) ;
2175
2176 // create a FBO
2177 GLuint name, tname;
2178 glGenTextures(1, &tname);
2179 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002180 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2181 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002182 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2183 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002184 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002185 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002186 GLint tw = (2 << (31 - clz(hw_w)));
2187 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2189 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002190 u = GLfloat(hw_w) / tw;
2191 v = GLfloat(hw_h) / th;
2192 }
2193 glGenFramebuffersOES(1, &name);
2194 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002195 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2196 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002197
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002199 glDisable(GL_TEXTURE_EXTERNAL_OES);
2200 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002201 glClearColor(0,0,0,1);
2202 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002203 glMatrixMode(GL_MODELVIEW);
2204 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002205 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206 const size_t count = layers.size();
2207 for (size_t i=0 ; i<count ; ++i) {
2208 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002209 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002210 }
2211
Mathias Agopian42977342012-08-05 00:40:46 -07002212 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002213
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002214 // back to main framebuffer
2215 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002216 glDeleteFramebuffersOES(1, &name);
2217
2218 *textureName = tname;
2219 *uOut = u;
2220 *vOut = v;
2221 return NO_ERROR;
2222}
2223
2224// ---------------------------------------------------------------------------
2225
Mathias Agopian74c40c02010-09-29 13:02:36 -07002226status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2227 sp<IMemoryHeap>* heap,
2228 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002229 uint32_t sw, uint32_t sh,
2230 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002231{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002232 ATRACE_CALL();
2233
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234 status_t result = PERMISSION_DENIED;
2235
2236 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002237 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002238 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002239 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002240 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002242 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002244 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002245
2246 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002247 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2248 const uint32_t hw_w = hw->getWidth();
2249 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002250
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002251 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002252 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002253 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002254 return PERMISSION_DENIED;
2255 }
2256
2257 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002258 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002260 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002261
2262 sw = (!sw) ? hw_w : sw;
2263 sh = (!sh) ? hw_h : sh;
2264 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002265 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002266
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002267// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2268// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002269
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270 // make sure to clear all GL error flags
2271 while ( glGetError() != GL_NO_ERROR ) ;
2272
2273 // create a FBO
2274 GLuint name, tname;
2275 glGenRenderbuffersOES(1, &tname);
2276 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2277 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002278
Mathias Agopian74c40c02010-09-29 13:02:36 -07002279 glGenFramebuffersOES(1, &name);
2280 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2281 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2282 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2283
2284 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002285
Mathias Agopian74c40c02010-09-29 13:02:36 -07002286 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2287
2288 // invert everything, b/c glReadPixel() below will invert the FB
2289 glViewport(0, 0, sw, sh);
2290 glMatrixMode(GL_PROJECTION);
2291 glPushMatrix();
2292 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002293 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002294 glMatrixMode(GL_MODELVIEW);
2295
2296 // redraw the screen entirely...
2297 glClearColor(0,0,0,1);
2298 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002299
Jamie Gennis9575f602011-10-07 14:51:16 -07002300 const LayerVector& layers(mDrawingState.layersSortedByZ);
2301 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302 for (size_t i=0 ; i<count ; ++i) {
2303 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002304 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002305 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002306 const uint32_t z = layer->drawingState().z;
2307 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002308 if (filtering) layer->setFiltering(true);
2309 layer->draw(hw);
2310 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002311 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002312 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002313 }
2314
Mathias Agopian74c40c02010-09-29 13:02:36 -07002315 // check for errors and return screen capture
2316 if (glGetError() != GL_NO_ERROR) {
2317 // error while rendering
2318 result = INVALID_OPERATION;
2319 } else {
2320 // allocate shared memory large enough to hold the
2321 // screen capture
2322 sp<MemoryHeapBase> base(
2323 new MemoryHeapBase(size, 0, "screen-capture") );
2324 void* const ptr = base->getBase();
2325 if (ptr) {
2326 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002327 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2329 if (glGetError() == GL_NO_ERROR) {
2330 *heap = base;
2331 *w = sw;
2332 *h = sh;
2333 *f = PIXEL_FORMAT_RGBA_8888;
2334 result = NO_ERROR;
2335 }
2336 } else {
2337 result = NO_MEMORY;
2338 }
2339 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002340 glViewport(0, 0, hw_w, hw_h);
2341 glMatrixMode(GL_PROJECTION);
2342 glPopMatrix();
2343 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344 } else {
2345 result = BAD_VALUE;
2346 }
2347
2348 // release FBO resources
2349 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2350 glDeleteRenderbuffersOES(1, &tname);
2351 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002352
Mathias Agopian42977342012-08-05 00:40:46 -07002353 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002354
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002355// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002356
Mathias Agopian74c40c02010-09-29 13:02:36 -07002357 return result;
2358}
2359
2360
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002361status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2362 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002363 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002364 uint32_t sw, uint32_t sh,
2365 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002366{
2367 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002368 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002369 return BAD_VALUE;
2370
2371 if (!GLExtensions::getInstance().haveFramebufferObject())
2372 return INVALID_OPERATION;
2373
2374 class MessageCaptureScreen : public MessageBase {
2375 SurfaceFlinger* flinger;
2376 DisplayID dpy;
2377 sp<IMemoryHeap>* heap;
2378 uint32_t* w;
2379 uint32_t* h;
2380 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 uint32_t sw;
2382 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002383 uint32_t minLayerZ;
2384 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002385 status_t result;
2386 public:
2387 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002389 uint32_t sw, uint32_t sh,
2390 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002391 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002392 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2393 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2394 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002395 {
2396 }
2397 status_t getResult() const {
2398 return result;
2399 }
2400 virtual bool handler() {
2401 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002402 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002403 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002404 return true;
2405 }
2406 };
2407
2408 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002409 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002410 status_t res = postMessageSync(msg);
2411 if (res == NO_ERROR) {
2412 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2413 }
2414 return res;
2415}
2416
2417// ---------------------------------------------------------------------------
2418
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002419SurfaceFlinger::LayerVector::LayerVector() {
2420}
2421
2422SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2423 : SortedVector<sp<LayerBase> >(rhs) {
2424}
2425
2426int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2427 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002428{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002429 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002430 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2431 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002432
2433 uint32_t ls = l->currentState().layerStack;
2434 uint32_t rs = r->currentState().layerStack;
2435 if (ls != rs)
2436 return ls - rs;
2437
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002438 uint32_t lz = l->currentState().z;
2439 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002440 if (lz != rz)
2441 return lz - rz;
2442
2443 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002444}
2445
2446// ---------------------------------------------------------------------------
2447
Mathias Agopiane57f2922012-08-09 16:29:12 -07002448SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2449}
2450
2451SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2452 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002453}
2454
2455// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456
Jamie Gennis9a78c902011-01-12 18:30:40 -08002457GraphicBufferAlloc::GraphicBufferAlloc() {}
2458
2459GraphicBufferAlloc::~GraphicBufferAlloc() {}
2460
2461sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002462 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002463 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2464 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002465 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002466 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002467 if (err == NO_MEMORY) {
2468 GraphicBuffer::dumpAllocationsToSystemLog();
2469 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002470 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002471 "failed (%s), handle=%p",
2472 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002473 return 0;
2474 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002475 return graphicBuffer;
2476}
2477
Jamie Gennis9a78c902011-01-12 18:30:40 -08002478// ---------------------------------------------------------------------------
2479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480}; // namespace android