blob: aba701ca873809cd299249c76fb439f0e4eccdaf [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 McFadden3f395622012-08-20 15:39:23 -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,
405 DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
406 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700407
408 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700409 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700410 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800411
Mathias Agopian028508c2012-07-25 21:12:12 -0700412 // start the EventThread
413 mEventThread = new EventThread(this);
414 mEventQueue.setEventThread(mEventThread);
415
Mathias Agopian86303202012-07-24 22:46:10 -0700416 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700417 mHwc = new HWComposer(this,
418 *static_cast<HWComposer::EventHandler *>(this),
419 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700420
421 // initialize our drawing state
422 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700423
Mathias Agopiana4912602012-07-12 14:25:33 -0700424 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800425 mReadyToRunBarrier.open();
426
Andy McFadden3f395622012-08-20 15:39:23 -0700427 // set initial conditions (e.g. unblank default device)
428 initializeDisplays();
429
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700430 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700431 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 return NO_ERROR;
434}
435
Mathias Agopiana67e4182012-06-19 17:26:12 -0700436void SurfaceFlinger::startBootAnim() {
437 // start boot animation
438 property_set("service.bootanim.exit", "0");
439 property_set("ctl.start", "bootanim");
440}
441
Mathias Agopiana4912602012-07-12 14:25:33 -0700442uint32_t SurfaceFlinger::getMaxTextureSize() const {
443 return mMaxTextureSize;
444}
445
446uint32_t SurfaceFlinger::getMaxViewportDims() const {
447 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
448 mMaxViewportDims[0] : mMaxViewportDims[1];
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800452
Jamie Gennis582270d2011-08-17 18:19:00 -0700453bool SurfaceFlinger::authenticateSurfaceTexture(
454 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800455 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700456 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800457
458 // Check the visible layer list for the ISurface
459 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
460 size_t count = currentLayers.size();
461 for (size_t i=0 ; i<count ; i++) {
462 const sp<LayerBase>& layer(currentLayers[i]);
463 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700464 if (lbc != NULL) {
465 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
466 if (lbcBinder == surfaceTextureBinder) {
467 return true;
468 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800469 }
470 }
471
472 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700473 // SurfaceTexture gets destroyed before all the clients are done using it,
474 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800475 // will instead fail later on when the client tries to use the surface,
476 // which should be reported as "surface XYZ returned an -ENODEV". The
477 // purgatorized layers are no less authentic than the visible ones, so this
478 // should not cause any harm.
479 size_t purgatorySize = mLayerPurgatory.size();
480 for (size_t i=0 ; i<purgatorySize ; i++) {
481 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
482 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700483 if (lbc != NULL) {
484 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
485 if (lbcBinder == surfaceTextureBinder) {
486 return true;
487 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800488 }
489 }
490
491 return false;
492}
493
Mathias Agopianc666cae2012-07-25 18:56:13 -0700494status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700495 // TODO: this is here only for compatibility -- should go away eventually.
496 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700497 return BAD_INDEX;
498 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700499
500 const HWComposer& hwc(getHwComposer());
501 float xdpi = hwc.getDpiX();
502 float ydpi = hwc.getDpiY();
503
504 // TODO: Not sure if display density should handled by SF any longer
505 class Density {
506 static int getDensityFromProperty(char const* propName) {
507 char property[PROPERTY_VALUE_MAX];
508 int density = 0;
509 if (property_get(propName, property, NULL) > 0) {
510 density = atoi(property);
511 }
512 return density;
513 }
514 public:
515 static int getEmuDensity() {
516 return getDensityFromProperty("qemu.sf.lcd_density"); }
517 static int getBuildDensity() {
518 return getDensityFromProperty("ro.sf.lcd_density"); }
519 };
520 // The density of the device is provided by a build property
521 float density = Density::getBuildDensity() / 160.0f;
522 if (density == 0) {
523 // the build doesn't provide a density -- this is wrong!
524 // use xdpi instead
525 ALOGE("ro.sf.lcd_density must be defined as a build property");
526 density = xdpi / 160.0f;
527 }
528 if (Density::getEmuDensity()) {
529 // if "qemu.sf.lcd_density" is specified, it overrides everything
530 xdpi = ydpi = density = Density::getEmuDensity();
531 density /= 160.0f;
532 }
533
Mathias Agopian42977342012-08-05 00:40:46 -0700534 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
535 info->w = hw->getWidth();
536 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700537 info->xdpi = xdpi;
538 info->ydpi = ydpi;
539 info->fps = float(1e9 / hwc.getRefreshPeriod());
540 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700541 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700542 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700543 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700544 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700545}
546
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800547// ----------------------------------------------------------------------------
548
549sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800550 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700551}
552
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700553void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700554
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700555 sp<IBinder> token;
556 { // scope for the lock
557 Mutex::Autolock _l(mStateLock);
558 token = mExtDisplayToken;
559 }
560
561 if (token == 0) {
562 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700563 }
564
565 { // scope for the lock
566 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700567 if (surface == 0) {
568 // release our current display. we're guarantee to have
569 // a reference to it (token), while we hold the lock
570 mExtDisplayToken = 0;
571 } else {
572 mExtDisplayToken = token;
573 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700574
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700575 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
576 info.surface = surface;
577 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700578 }
579}
580
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800582
583void SurfaceFlinger::waitForEvent() {
584 mEventQueue.waitMessage();
585}
586
587void SurfaceFlinger::signalTransaction() {
588 mEventQueue.invalidate();
589}
590
591void SurfaceFlinger::signalLayerUpdate() {
592 mEventQueue.invalidate();
593}
594
595void SurfaceFlinger::signalRefresh() {
596 mEventQueue.refresh();
597}
598
599status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
600 nsecs_t reltime, uint32_t flags) {
601 return mEventQueue.postMessage(msg, reltime);
602}
603
604status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
605 nsecs_t reltime, uint32_t flags) {
606 status_t res = mEventQueue.postMessage(msg, reltime);
607 if (res == NO_ERROR) {
608 msg->wait();
609 }
610 return res;
611}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612
Mathias Agopian4fec8732012-06-29 14:12:52 -0700613bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 return true;
616}
617
Mathias Agopian86303202012-07-24 22:46:10 -0700618void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700619 mEventThread->onVSyncReceived(dpy, timestamp);
620}
621
622void SurfaceFlinger::eventControl(int event, int enabled) {
623 getHwComposer().eventControl(event, enabled);
624}
625
Mathias Agopian4fec8732012-06-29 14:12:52 -0700626void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800627 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800628 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700629 case MessageQueue::INVALIDATE:
630 handleMessageTransaction();
631 handleMessageInvalidate();
632 signalRefresh();
633 break;
634 case MessageQueue::REFRESH:
635 handleMessageRefresh();
636 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800637 }
638}
639
Mathias Agopian4fec8732012-06-29 14:12:52 -0700640void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700641 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700642 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700643 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700644 }
645}
646
647void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700648 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700649 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700650}
651
652void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700653 ATRACE_CALL();
654 preComposition();
655 rebuildLayerStacks();
656 setUpHWComposer();
657 doDebugFlashRegions();
658 doComposition();
659 postComposition();
660}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700661
Mathias Agopiancd60f992012-08-16 16:28:27 -0700662void SurfaceFlinger::doDebugFlashRegions()
663{
664 // is debugging enabled
665 if (CC_LIKELY(!mDebugRegion))
666 return;
667
668 const bool repaintEverything = mRepaintEverything;
669 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
670 const sp<DisplayDevice>& hw(mDisplays[dpy]);
671 if (hw->canDraw()) {
672 // transform the dirty region into this screen's coordinate space
673 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
674 if (!dirtyRegion.isEmpty()) {
675 // redraw the whole screen
676 doComposeSurfaces(hw, Region(hw->bounds()));
677
678 // and draw the dirty region
679 glDisable(GL_TEXTURE_EXTERNAL_OES);
680 glDisable(GL_TEXTURE_2D);
681 glDisable(GL_BLEND);
682 glColor4f(1, 0, 1, 1);
683 const int32_t height = hw->getHeight();
684 Region::const_iterator it = dirtyRegion.begin();
685 Region::const_iterator const end = dirtyRegion.end();
686 while (it != end) {
687 const Rect& r = *it++;
688 GLfloat vertices[][2] = {
689 { r.left, height - r.top },
690 { r.left, height - r.bottom },
691 { r.right, height - r.bottom },
692 { r.right, height - r.top }
693 };
694 glVertexPointer(2, GL_FLOAT, 0, vertices);
695 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
696 }
697 hw->compositionComplete();
698 // FIXME
699 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
700 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
701 }
702 }
703 }
704 }
705
706 postFramebuffer();
707
708 if (mDebugRegion > 1) {
709 usleep(mDebugRegion * 1000);
710 }
711}
712
713void SurfaceFlinger::preComposition()
714{
715 bool needExtraInvalidate = false;
716 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
717 const size_t count = currentLayers.size();
718 for (size_t i=0 ; i<count ; i++) {
719 if (currentLayers[i]->onPreComposition()) {
720 needExtraInvalidate = true;
721 }
722 }
723 if (needExtraInvalidate) {
724 signalLayerUpdate();
725 }
726}
727
728void SurfaceFlinger::postComposition()
729{
730 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
731 const size_t count = currentLayers.size();
732 for (size_t i=0 ; i<count ; i++) {
733 currentLayers[i]->onPostComposition();
734 }
735}
736
737void SurfaceFlinger::rebuildLayerStacks() {
738 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700739 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700740 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700741 mVisibleRegionsDirty = false;
742 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700743 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700744 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700745 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700746 Region opaqueRegion;
747 Region dirtyRegion;
748 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700749 hw->getLayerStack(), dirtyRegion, opaqueRegion);
750 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700751
752 Vector< sp<LayerBase> > layersSortedByZ;
753 const size_t count = currentLayers.size();
754 for (size_t i=0 ; i<count ; i++) {
755 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700756 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700757 if (!currentLayers[i]->visibleRegion.isEmpty()) {
758 layersSortedByZ.add(currentLayers[i]);
759 }
760 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700761 }
Mathias Agopian42977342012-08-05 00:40:46 -0700762 hw->setVisibleLayersSortedByZ(layersSortedByZ);
763 hw->undefinedRegion.set(hw->getBounds());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764 hw->undefinedRegion.subtractSelf(
765 hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700766 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700767 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700768}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700769
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700771 HWComposer& hwc(getHwComposer());
772 if (hwc.initCheck() == NO_ERROR) {
773 // build the h/w work list
774 const bool workListsDirty = mHwWorkListDirty;
775 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700776 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700777 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700778 const Vector< sp<LayerBase> >& currentLayers(
779 hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700780 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700781
Mathias Agopian1e260872012-08-08 18:35:12 -0700782 const int32_t id = hw->getDisplayId();
783 if (hwc.createWorkList(id, count) >= 0) {
784 HWComposer::LayerListIterator cur = hwc.begin(id);
785 const HWComposer::LayerListIterator end = hwc.end(id);
786 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
787 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700788
Mathias Agopian1e260872012-08-08 18:35:12 -0700789 if (CC_UNLIKELY(workListsDirty)) {
790 layer->setGeometry(hw, *cur);
791 if (mDebugDisableHWC || mDebugRegion) {
792 cur->setSkip(true);
793 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700794 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700795
Mathias Agopian1e260872012-08-08 18:35:12 -0700796 /*
797 * update the per-frame h/w composer data for each layer
798 * and build the transparent region of the FB
799 */
800 layer->setPerFrameData(hw, *cur);
801 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700802 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700803 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700804 status_t err = hwc.prepare();
805 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
806 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700808
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809void SurfaceFlinger::doComposition() {
810 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700811 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700812 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700813 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700814 if (hw->canDraw()) {
815 // transform the dirty region into this screen's coordinate space
816 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
817 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700818 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700819 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700820 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700821 hw->dirtyRegion.clear();
822 hw->flip(hw->swapRegion);
823 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700824 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700825 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700826 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700827 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700828 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700829}
830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831void SurfaceFlinger::postFramebuffer()
832{
Mathias Agopian841cde52012-03-01 15:44:37 -0800833 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800834
Mathias Agopiana44b0412011-10-16 18:46:35 -0700835 const nsecs_t now = systemTime();
836 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700837
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700838 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700839 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700840 // FIXME: eventually commit() won't take arguments
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700841 // FIXME: EGL spec says:
842 // "surface must be bound to the calling thread's current context,
843 // for the current rendering API."
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 DisplayDevice::makeCurrent(
845 getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopian42977342012-08-05 00:40:46 -0700846 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700847 }
848
Mathias Agopian92a979a2012-08-02 18:32:23 -0700849 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700850 sp<const DisplayDevice> hw(mDisplays[dpy]);
851 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700852 const size_t count = currentLayers.size();
853 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700854 int32_t id = hw->getDisplayId();
855 HWComposer::LayerListIterator cur = hwc.begin(id);
856 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700857 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700858 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700859 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700860 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700861 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700862 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700863 }
Jesse Hallef194142012-06-14 14:45:17 -0700864 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800865 }
866
Mathias Agopiana44b0412011-10-16 18:46:35 -0700867 mLastSwapBufferTime = systemTime() - now;
868 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869}
870
Mathias Agopian87baae12012-07-31 12:38:26 -0700871void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872{
Mathias Agopian841cde52012-03-01 15:44:37 -0800873 ATRACE_CALL();
874
Mathias Agopianca4d3602011-05-19 15:38:14 -0700875 Mutex::Autolock _l(mStateLock);
876 const nsecs_t now = systemTime();
877 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878
Mathias Agopianca4d3602011-05-19 15:38:14 -0700879 // Here we're guaranteed that some transaction flags are set
880 // so we can call handleTransactionLocked() unconditionally.
881 // We call getTransactionFlags(), which will also clear the flags,
882 // with mStateLock held to guarantee that mCurrentState won't change
883 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700884
Mathias Agopiane57f2922012-08-09 16:29:12 -0700885 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700886 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700887
Mathias Agopianca4d3602011-05-19 15:38:14 -0700888 mLastTransactionTime = systemTime() - now;
889 mDebugInTransaction = 0;
890 invalidateHwcGeometry();
891 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700892}
893
Mathias Agopian87baae12012-07-31 12:38:26 -0700894void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700895{
896 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 const size_t count = currentLayers.size();
898
899 /*
900 * Traversal of the children
901 * (perform the transaction for each of them if needed)
902 */
903
Mathias Agopian3559b072012-08-15 13:46:03 -0700904 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700906 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
908 if (!trFlags) continue;
909
910 const uint32_t flags = layer->doTransaction(0);
911 if (flags & Layer::eVisibleRegion)
912 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913 }
914 }
915
916 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700917 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 */
919
Mathias Agopiane57f2922012-08-09 16:29:12 -0700920 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700921 // here we take advantage of Vector's copy-on-write semantics to
922 // improve performance by skipping the transaction entirely when
923 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700924 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
925 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700926 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700928 const size_t cc = curr.size();
929 const size_t dc = draw.size();
930
931 // find the displays that were removed
932 // (ie: in drawing state but not in current state)
933 // also handle displays that changed
934 // (ie: displays that are in both lists)
935 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700936 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
937 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700938 // in drawing state but not in current state
939 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700940 mDisplays.removeItem(draw[i].id);
941 } else {
942 ALOGW("trying to remove the main display");
943 }
944 } else {
945 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700946 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700947 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700948 // changing the surface is like destroying and
949 // recreating the DisplayDevice
950
951 sp<SurfaceTextureClient> stc(
952 new SurfaceTextureClient(state.surface));
953
954 sp<DisplayDevice> disp = new DisplayDevice(this,
955 state.id, stc, 0, mEGLConfig);
956
957 disp->setLayerStack(state.layerStack);
958 disp->setOrientation(state.orientation);
959 // TODO: take viewport and frame into account
960 mDisplays.replaceValueFor(state.id, disp);
961 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700962 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700963 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700964 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700965 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700966 if (state.orientation != draw[i].orientation ||
967 state.viewport != draw[i].viewport ||
968 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700969 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
970 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700971 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 }
973 }
974 }
975
976 // find displays that were added
977 // (ie: in current state but not in drawing state)
978 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700979 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700980 const DisplayDeviceState& state(curr[i]);
981 sp<SurfaceTextureClient> stc(
982 new SurfaceTextureClient(state.surface));
983 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
984 stc, 0, mEGLConfig);
985 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700986 }
987 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 }
Mathias Agopian3559b072012-08-15 13:46:03 -0700989 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990
Mathias Agopian3559b072012-08-15 13:46:03 -0700991 /*
992 * Perform our own transaction if needed
993 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700994
Mathias Agopiancd60f992012-08-16 16:28:27 -0700995 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
996 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -0700997 // layers have been added
998 mVisibleRegionsDirty = true;
999 }
1000
1001 // some layers might have been removed, so
1002 // we need to update the regions they're exposing.
1003 if (mLayersRemoved) {
1004 mLayersRemoved = false;
1005 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001006 const size_t count = previousLayers.size();
1007 for (size_t i=0 ; i<count ; i++) {
1008 const sp<LayerBase>& layer(previousLayers[i]);
1009 if (currentLayers.indexOf(layer) < 0) {
1010 // this layer is not visible anymore
1011 // TODO: we could traverse the tree from front to back and
1012 // compute the actual visible region
1013 // TODO: we could cache the transformed region
1014 Layer::State front(layer->drawingState());
1015 Region visibleReg = front.transform.transform(
1016 Region(Rect(front.active.w, front.active.h)));
1017 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 }
1021
1022 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001023}
1024
1025void SurfaceFlinger::commitTransaction()
1026{
1027 if (!mLayersPendingRemoval.isEmpty()) {
1028 // Notify removed layers now that they can't be drawn from
1029 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1030 mLayersPendingRemoval[i]->onRemoved();
1031 }
1032 mLayersPendingRemoval.clear();
1033 }
1034
1035 mDrawingState = mCurrentState;
1036 mTransationPending = false;
1037 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001041 const LayerVector& currentLayers, uint32_t layerStack,
1042 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043{
Mathias Agopian841cde52012-03-01 15:44:37 -08001044 ATRACE_CALL();
1045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 Region aboveOpaqueLayers;
1047 Region aboveCoveredLayers;
1048 Region dirty;
1049
Mathias Agopian87baae12012-07-31 12:38:26 -07001050 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051
1052 size_t i = currentLayers.size();
1053 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001054 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055
1056 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001057 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058
Mathias Agopian87baae12012-07-31 12:38:26 -07001059 // only consider the layers on the given later stack
1060 if (s.layerStack != layerStack)
1061 continue;
1062
Mathias Agopianab028732010-03-16 16:41:46 -07001063 /*
1064 * opaqueRegion: area of a surface that is fully opaque.
1065 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001067
1068 /*
1069 * visibleRegion: area of a surface that is visible on screen
1070 * and not fully transparent. This is essentially the layer's
1071 * footprint minus the opaque regions above it.
1072 * Areas covered by a translucent surface are considered visible.
1073 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001075
1076 /*
1077 * coveredRegion: area of a surface that is covered by all
1078 * visible regions above it (which includes the translucent areas).
1079 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001081
1082
1083 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001084 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001085 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001086 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001088 if (!visibleRegion.isEmpty()) {
1089 // Remove the transparent area from the visible region
1090 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001091 Region transparentRegionScreen;
1092 const Transform tr(s.transform);
1093 if (tr.transformed()) {
1094 if (tr.preserveRects()) {
1095 // transform the transparent region
1096 transparentRegionScreen = tr.transform(s.transparentRegion);
1097 } else {
1098 // transformation too complex, can't do the
1099 // transparent region optimization.
1100 transparentRegionScreen.clear();
1101 }
1102 } else {
1103 transparentRegionScreen = s.transparentRegion;
1104 }
1105 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001106 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107
Mathias Agopianab028732010-03-16 16:41:46 -07001108 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001109 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001110 if (s.alpha==255 && !translucent &&
1111 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1112 // the opaque region is the layer's footprint
1113 opaqueRegion = visibleRegion;
1114 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 }
1116 }
1117
Mathias Agopianab028732010-03-16 16:41:46 -07001118 // Clip the covered region to the visible region
1119 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1120
1121 // Update aboveCoveredLayers for next (lower) layer
1122 aboveCoveredLayers.orSelf(visibleRegion);
1123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 // subtract the opaque region covered by the layers above us
1125 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126
1127 // compute this layer's dirty region
1128 if (layer->contentDirty) {
1129 // we need to invalidate the whole region
1130 dirty = visibleRegion;
1131 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001132 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133 layer->contentDirty = false;
1134 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001135 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001136 * the exposed region consists of two components:
1137 * 1) what's VISIBLE now and was COVERED before
1138 * 2) what's EXPOSED now less what was EXPOSED before
1139 *
1140 * note that (1) is conservative, we start with the whole
1141 * visible region but only keep what used to be covered by
1142 * something -- which mean it may have been exposed.
1143 *
1144 * (2) handles areas that were not covered by anything but got
1145 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001146 */
Mathias Agopianab028732010-03-16 16:41:46 -07001147 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001148 const Region oldVisibleRegion = layer->visibleRegion;
1149 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001150 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1151 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 }
1153 dirty.subtractSelf(aboveOpaqueLayers);
1154
1155 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001156 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157
Mathias Agopianab028732010-03-16 16:41:46 -07001158 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 // Store the visible region is screen space
1162 layer->setVisibleRegion(visibleRegion);
1163 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 }
1165
Mathias Agopian87baae12012-07-31 12:38:26 -07001166 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167}
1168
Mathias Agopian87baae12012-07-31 12:38:26 -07001169void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1170 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001171 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001172 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1173 if (hw->getLayerStack() == layerStack) {
1174 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001175 }
1176 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001177}
1178
1179void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001181 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182
Mathias Agopian4fec8732012-06-29 14:12:52 -07001183 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001184 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001185 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001186 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001187 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001188 const Region dirty(layer->latchBuffer(visibleRegions));
1189 Layer::State s(layer->drawingState());
1190 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001191 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001192
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001193 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194}
1195
Mathias Agopianad456f92011-01-13 17:53:01 -08001196void SurfaceFlinger::invalidateHwcGeometry()
1197{
1198 mHwWorkListDirty = true;
1199}
1200
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001201
Mathias Agopiancd60f992012-08-16 16:28:27 -07001202void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001203 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204{
Mathias Agopian87baae12012-07-31 12:38:26 -07001205 Region dirtyRegion(inDirtyRegion);
1206
Mathias Agopianb8a55602009-06-26 19:06:36 -07001207 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001208 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209
Mathias Agopian42977342012-08-05 00:40:46 -07001210 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001211 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001212 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1213 // takes a rectangle, we must make sure to update that whole
1214 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001215 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001216 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001217 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001218 // We need to redraw the rectangle that will be updated
1219 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001220 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001221 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001222 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001223 } else {
1224 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001225 dirtyRegion.set(hw->bounds());
1226 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 }
1228 }
1229
Mathias Agopiancd60f992012-08-16 16:28:27 -07001230 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231
Mathias Agopiancd60f992012-08-16 16:28:27 -07001232 // FIXME: we need to call eglSwapBuffers() on displays that have
1233 // GL composition and only on those.
1234 // however, currently hwc.commit() already does that for the main
1235 // display and never for the other ones
1236 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
1237 // FIXME: EGL spec says:
1238 // "surface must be bound to the calling thread's current context,
1239 // for the current rendering API."
1240 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001241 }
1242
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001243 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001244 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245}
1246
Mathias Agopiancd60f992012-08-16 16:28:27 -07001247void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001248{
Mathias Agopian86303202012-07-24 22:46:10 -07001249 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001250 int32_t id = hw->getDisplayId();
1251 HWComposer::LayerListIterator cur = hwc.begin(id);
1252 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001253
Mathias Agopian1e260872012-08-08 18:35:12 -07001254 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001255 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001256
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001257 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001258
1259 // set the frame buffer
1260 glMatrixMode(GL_MODELVIEW);
1261 glLoadIdentity();
1262
1263 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001264 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001265 // when using overlays, we assume a fully transparent framebuffer
1266 // NOTE: we could reduce how much we need to clear, for instance
1267 // remove where there are opaque FB layers. however, on some
1268 // GPUs doing a "clean slate" glClear might be more efficient.
1269 // We'll revisit later if needed.
1270 glClearColor(0, 0, 0, 0);
1271 glClear(GL_COLOR_BUFFER_BIT);
1272 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001273 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001274 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001275 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001276 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001277 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001278 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001279 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001280
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001281 /*
1282 * and then, render the layers targeted at the framebuffer
1283 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001284
Mathias Agopian42977342012-08-05 00:40:46 -07001285 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001286 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001287 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001288 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001289 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001290 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Jesse Halla6b32db2012-07-19 16:44:38 -07001291 if (cur != end) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001292 // we're using h/w composer
1293 if (!clip.isEmpty()) {
1294 if (cur->getCompositionType() == HWC_OVERLAY) {
1295 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
1296 && layer->isOpaque()) {
1297 // never clear the very first layer since we're
1298 // guaranteed the FB is already cleared
1299 layer->clearWithOpenGL(hw, clip);
1300 }
1301 } else {
1302 layer->draw(hw, clip);
1303 }
1304 layer->setAcquireFence(hw, *cur);
1305 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001306 ++cur;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001307 } else {
1308 // we're not using h/w composer
1309 if (!clip.isEmpty()) {
1310 layer->draw(hw, clip);
1311 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001312 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001313 }
1314 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315}
1316
Mathias Agopian87baae12012-07-31 12:38:26 -07001317void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001319 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001320 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001321 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001322 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001323
1324 GLfloat vertices[4][2];
1325 glVertexPointer(2, GL_FLOAT, 0, vertices);
1326 Region::const_iterator it = region.begin();
1327 Region::const_iterator const end = region.end();
1328 while (it != end) {
1329 const Rect& r = *it++;
1330 vertices[0][0] = r.left;
1331 vertices[0][1] = r.top;
1332 vertices[1][0] = r.right;
1333 vertices[1][1] = r.top;
1334 vertices[2][0] = r.right;
1335 vertices[2][1] = r.bottom;
1336 vertices[3][0] = r.left;
1337 vertices[3][1] = r.bottom;
1338 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1339 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340}
1341
Mathias Agopian96f08192010-06-02 23:28:45 -07001342ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1343 const sp<LayerBaseClient>& lbc)
1344{
Mathias Agopian96f08192010-06-02 23:28:45 -07001345 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001346 size_t name = client->attachLayer(lbc);
1347
Mathias Agopian96f08192010-06-02 23:28:45 -07001348 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001349 Mutex::Autolock _l(mStateLock);
1350 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001351
Mathias Agopian4f113742011-05-03 16:21:41 -07001352 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001353}
1354
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001355status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356{
1357 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001358 status_t err = purgatorizeLayer_l(layer);
1359 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001360 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001361 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362}
1363
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001364status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365{
1366 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1367 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001368 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 return NO_ERROR;
1370 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001371 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372}
1373
Mathias Agopian9a112062009-04-17 19:36:26 -07001374status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1375{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001376 // First add the layer to the purgatory list, which makes sure it won't
1377 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001378 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001379 if (err >= 0) {
1380 mLayerPurgatory.add(layerBase);
1381 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001382
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001383 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001384
Mathias Agopian3d579642009-06-04 18:46:21 -07001385 // it's possible that we don't find a layer, because it might
1386 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001387 // from the user because there is a race between Client::destroySurface(),
1388 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001389 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1390}
1391
Mathias Agopiandea20b12011-05-03 17:04:02 -07001392uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1393{
1394 return android_atomic_release_load(&mTransactionFlags);
1395}
1396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1398{
1399 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1400}
1401
Mathias Agopianbb641242010-05-18 17:06:55 -07001402uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403{
1404 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1405 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001406 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 }
1408 return old;
1409}
1410
Mathias Agopian8b33f032012-07-24 20:43:54 -07001411void SurfaceFlinger::setTransactionState(
1412 const Vector<ComposerState>& state,
1413 const Vector<DisplayState>& displays,
1414 uint32_t flags)
1415{
Mathias Agopian698c0872011-06-28 19:09:31 -07001416 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001417 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001418
1419 size_t count = displays.size();
1420 for (size_t i=0 ; i<count ; i++) {
1421 const DisplayState& s(displays[i]);
1422 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001423 }
1424
Mathias Agopiane57f2922012-08-09 16:29:12 -07001425 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001426 for (size_t i=0 ; i<count ; i++) {
1427 const ComposerState& s(state[i]);
1428 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001429 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001430 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001431
Mathias Agopian386aa982011-11-07 21:58:03 -08001432 if (transactionFlags) {
1433 // this triggers the transaction
1434 setTransactionFlags(transactionFlags);
1435
1436 // if this is a synchronous transaction, wait for it to take effect
1437 // before returning.
1438 if (flags & eSynchronous) {
1439 mTransationPending = true;
1440 }
1441 while (mTransationPending) {
1442 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1443 if (CC_UNLIKELY(err != NO_ERROR)) {
1444 // just in case something goes wrong in SF, return to the
1445 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001446 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001447 mTransationPending = false;
1448 break;
1449 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001450 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 }
1452}
1453
Mathias Agopiane57f2922012-08-09 16:29:12 -07001454uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1455{
1456 uint32_t flags = 0;
1457 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1458 if (disp.id >= 0) {
1459 const uint32_t what = s.what;
1460 if (what & DisplayState::eSurfaceChanged) {
1461 if (disp.surface->asBinder() != s.surface->asBinder()) {
1462 disp.surface = s.surface;
1463 flags |= eDisplayTransactionNeeded;
1464 }
1465 }
1466 if (what & DisplayState::eLayerStackChanged) {
1467 if (disp.layerStack != s.layerStack) {
1468 disp.layerStack = s.layerStack;
1469 flags |= eDisplayTransactionNeeded;
1470 }
1471 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001472 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001473 if (disp.orientation != s.orientation) {
1474 disp.orientation = s.orientation;
1475 flags |= eDisplayTransactionNeeded;
1476 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001477 }
1478 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001479 if (disp.frame != s.frame) {
1480 disp.frame = s.frame;
1481 flags |= eDisplayTransactionNeeded;
1482 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001483 }
1484 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001485 if (disp.viewport != s.viewport) {
1486 disp.viewport = s.viewport;
1487 flags |= eDisplayTransactionNeeded;
1488 }
1489 }
1490 }
1491 return flags;
1492}
1493
1494uint32_t SurfaceFlinger::setClientStateLocked(
1495 const sp<Client>& client,
1496 const layer_state_t& s)
1497{
1498 uint32_t flags = 0;
1499 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1500 if (layer != 0) {
1501 const uint32_t what = s.what;
1502 if (what & layer_state_t::ePositionChanged) {
1503 if (layer->setPosition(s.x, s.y))
1504 flags |= eTraversalNeeded;
1505 }
1506 if (what & layer_state_t::eLayerChanged) {
1507 // NOTE: index needs to be calculated before we update the state
1508 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1509 if (layer->setLayer(s.z)) {
1510 mCurrentState.layersSortedByZ.removeAt(idx);
1511 mCurrentState.layersSortedByZ.add(layer);
1512 // we need traversal (state changed)
1513 // AND transaction (list changed)
1514 flags |= eTransactionNeeded|eTraversalNeeded;
1515 }
1516 }
1517 if (what & layer_state_t::eSizeChanged) {
1518 if (layer->setSize(s.w, s.h)) {
1519 flags |= eTraversalNeeded;
1520 }
1521 }
1522 if (what & layer_state_t::eAlphaChanged) {
1523 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1524 flags |= eTraversalNeeded;
1525 }
1526 if (what & layer_state_t::eMatrixChanged) {
1527 if (layer->setMatrix(s.matrix))
1528 flags |= eTraversalNeeded;
1529 }
1530 if (what & layer_state_t::eTransparentRegionChanged) {
1531 if (layer->setTransparentRegionHint(s.transparentRegion))
1532 flags |= eTraversalNeeded;
1533 }
1534 if (what & layer_state_t::eVisibilityChanged) {
1535 if (layer->setFlags(s.flags, s.mask))
1536 flags |= eTraversalNeeded;
1537 }
1538 if (what & layer_state_t::eCropChanged) {
1539 if (layer->setCrop(s.crop))
1540 flags |= eTraversalNeeded;
1541 }
1542 if (what & layer_state_t::eLayerStackChanged) {
1543 // NOTE: index needs to be calculated before we update the state
1544 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1545 if (layer->setLayerStack(s.layerStack)) {
1546 mCurrentState.layersSortedByZ.removeAt(idx);
1547 mCurrentState.layersSortedByZ.add(layer);
1548 // we need traversal (state changed)
1549 // AND transaction (list changed)
1550 flags |= eTransactionNeeded|eTraversalNeeded;
1551 }
1552 }
1553 }
1554 return flags;
1555}
1556
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001557sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001558 ISurfaceComposerClient::surface_data_t* params,
1559 const String8& name,
1560 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1562 uint32_t flags)
1563{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001564 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001565 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001566
1567 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001568 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001569 int(w), int(h));
1570 return surfaceHandle;
1571 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001572
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001573 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001574 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1575 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001576 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001578 case ISurfaceComposerClient::eFXSurfaceBlur:
1579 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001580 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001582 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001583 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001584 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 }
1586
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001587 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001588 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001589 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001590 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001592 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001593 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001594 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001595 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001596 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 }
1598
1599 return surfaceHandle;
1600}
1601
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001602sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001603 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001604 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001605 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606{
1607 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001608 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 case PIXEL_FORMAT_TRANSPARENT:
1610 case PIXEL_FORMAT_TRANSLUCENT:
1611 format = PIXEL_FORMAT_RGBA_8888;
1612 break;
1613 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001614#ifdef NO_RGBX_8888
1615 format = PIXEL_FORMAT_RGB_565;
1616#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001617 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001618#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 break;
1620 }
1621
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001622#ifdef NO_RGBX_8888
1623 if (format == PIXEL_FORMAT_RGBX_8888)
1624 format = PIXEL_FORMAT_RGBA_8888;
1625#endif
1626
Mathias Agopian96f08192010-06-02 23:28:45 -07001627 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001628 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001629 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001630 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001631 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 }
1633 return layer;
1634}
1635
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001636sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001637 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001638 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639{
Mathias Agopian96f08192010-06-02 23:28:45 -07001640 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001641 return layer;
1642}
1643
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001644sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001645 const sp<Client>& client, DisplayID display,
1646 uint32_t w, uint32_t h, uint32_t flags)
1647{
1648 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 return layer;
1650}
1651
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001652status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653{
Mathias Agopian9a112062009-04-17 19:36:26 -07001654 /*
1655 * called by the window manager, when a surface should be marked for
1656 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001657 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001658 * The surface is removed from the current and drawing lists, but placed
1659 * in the purgatory queue, so it's not destroyed right-away (we need
1660 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001661 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001662
Mathias Agopian48d819a2009-09-10 19:41:18 -07001663 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001664 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001665 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001666
Mathias Agopian48d819a2009-09-10 19:41:18 -07001667 if (layer != 0) {
1668 err = purgatorizeLayer_l(layer);
1669 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001670 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001671 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001672 }
1673 return err;
1674}
1675
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001676status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001677{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001678 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001679 status_t err = NO_ERROR;
1680 sp<LayerBaseClient> l(layer.promote());
1681 if (l != NULL) {
1682 Mutex::Autolock _l(mStateLock);
1683 err = removeLayer_l(l);
1684 if (err == NAME_NOT_FOUND) {
1685 // The surface wasn't in the current list, which means it was
1686 // removed already, which means it is in the purgatory,
1687 // and need to be removed from there.
1688 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001689 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001690 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001691 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001692 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001693 "error removing layer=%p (%s)", l.get(), strerror(-err));
1694 }
1695 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696}
1697
Mathias Agopianb60314a2012-04-10 22:09:54 -07001698// ---------------------------------------------------------------------------
1699
Andy McFadden3f395622012-08-20 15:39:23 -07001700void SurfaceFlinger::onInitializeDisplays() {
1701 // reset screen orientation
1702 Vector<ComposerState> state;
1703 Vector<DisplayState> displays;
1704 DisplayState d;
1705 d.what = DisplayState::eOrientationChanged;
1706 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
1707 d.orientation = DisplayState::eOrientationDefault;
1708 displays.add(d);
1709 setTransactionState(state, displays, 0);
1710
1711 // XXX: this should init default device to "unblank" and all other devices to "blank"
1712 onScreenAcquired();
1713}
1714
1715void SurfaceFlinger::initializeDisplays() {
1716 class MessageScreenInitialized : public MessageBase {
1717 SurfaceFlinger* flinger;
1718 public:
1719 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1720 virtual bool handler() {
1721 flinger->onInitializeDisplays();
1722 return true;
1723 }
1724 };
1725 sp<MessageBase> msg = new MessageScreenInitialized(this);
1726 postMessageAsync(msg); // we may be called from main thread, use async message
1727}
1728
1729
Mathias Agopianb60314a2012-04-10 22:09:54 -07001730void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001731 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001732 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001733 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001734 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001735 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001736 mVisibleRegionsDirty = true;
1737 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738}
1739
Mathias Agopianb60314a2012-04-10 22:09:54 -07001740void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001741 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001742 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1743 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001744 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001745 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001746 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001747 // from this point on, SF will stop drawing
1748 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749}
1750
Colin Cross8e533062012-06-07 13:17:52 -07001751void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001752 class MessageScreenAcquired : public MessageBase {
1753 SurfaceFlinger* flinger;
1754 public:
1755 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1756 virtual bool handler() {
1757 flinger->onScreenAcquired();
1758 return true;
1759 }
1760 };
1761 sp<MessageBase> msg = new MessageScreenAcquired(this);
1762 postMessageSync(msg);
1763}
1764
Colin Cross8e533062012-06-07 13:17:52 -07001765void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001766 class MessageScreenReleased : public MessageBase {
1767 SurfaceFlinger* flinger;
1768 public:
1769 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1770 virtual bool handler() {
1771 flinger->onScreenReleased();
1772 return true;
1773 }
1774 };
1775 sp<MessageBase> msg = new MessageScreenReleased(this);
1776 postMessageSync(msg);
1777}
1778
1779// ---------------------------------------------------------------------------
1780
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1782{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001783 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784 char buffer[SIZE];
1785 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001786
1787 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 snprintf(buffer, SIZE, "Permission Denial: "
1789 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1790 IPCThreadState::self()->getCallingPid(),
1791 IPCThreadState::self()->getCallingUid());
1792 result.append(buffer);
1793 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001794 // Try to get the main lock, but don't insist if we can't
1795 // (this would indicate SF is stuck, but we want to be able to
1796 // print something in dumpsys).
1797 int retry = 3;
1798 while (mStateLock.tryLock()<0 && --retry>=0) {
1799 usleep(1000000);
1800 }
1801 const bool locked(retry >= 0);
1802 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001803 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001804 "SurfaceFlinger appears to be unresponsive, "
1805 "dumping anyways (no locks held)\n");
1806 result.append(buffer);
1807 }
1808
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001809 bool dumpAll = true;
1810 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001811 size_t numArgs = args.size();
1812 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001813 if ((index < numArgs) &&
1814 (args[index] == String16("--list"))) {
1815 index++;
1816 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001817 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001818 }
1819
1820 if ((index < numArgs) &&
1821 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001822 index++;
1823 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001824 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001825 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001826
1827 if ((index < numArgs) &&
1828 (args[index] == String16("--latency-clear"))) {
1829 index++;
1830 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001831 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001832 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001834
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001835 if (dumpAll) {
1836 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001837 }
1838
Mathias Agopian9795c422009-08-26 16:36:26 -07001839 if (locked) {
1840 mStateLock.unlock();
1841 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 }
1843 write(fd, result.string(), result.size());
1844 return NO_ERROR;
1845}
1846
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001847void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1848 String8& result, char* buffer, size_t SIZE) const
1849{
1850 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1851 const size_t count = currentLayers.size();
1852 for (size_t i=0 ; i<count ; i++) {
1853 const sp<LayerBase>& layer(currentLayers[i]);
1854 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1855 result.append(buffer);
1856 }
1857}
1858
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001859void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1860 String8& result, char* buffer, size_t SIZE) const
1861{
1862 String8 name;
1863 if (index < args.size()) {
1864 name = String8(args[index]);
1865 index++;
1866 }
1867
1868 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1869 const size_t count = currentLayers.size();
1870 for (size_t i=0 ; i<count ; i++) {
1871 const sp<LayerBase>& layer(currentLayers[i]);
1872 if (name.isEmpty()) {
1873 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1874 result.append(buffer);
1875 }
1876 if (name.isEmpty() || (name == layer->getName())) {
1877 layer->dumpStats(result, buffer, SIZE);
1878 }
1879 }
1880}
1881
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001882void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1883 String8& result, char* buffer, size_t SIZE) const
1884{
1885 String8 name;
1886 if (index < args.size()) {
1887 name = String8(args[index]);
1888 index++;
1889 }
1890
1891 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1892 const size_t count = currentLayers.size();
1893 for (size_t i=0 ; i<count ; i++) {
1894 const sp<LayerBase>& layer(currentLayers[i]);
1895 if (name.isEmpty() || (name == layer->getName())) {
1896 layer->clearStats();
1897 }
1898 }
1899}
1900
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001901void SurfaceFlinger::dumpAllLocked(
1902 String8& result, char* buffer, size_t SIZE) const
1903{
1904 // figure out if we're stuck somewhere
1905 const nsecs_t now = systemTime();
1906 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1907 const nsecs_t inTransaction(mDebugInTransaction);
1908 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1909 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1910
1911 /*
1912 * Dump the visible layer list
1913 */
1914 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1915 const size_t count = currentLayers.size();
1916 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1917 result.append(buffer);
1918 for (size_t i=0 ; i<count ; i++) {
1919 const sp<LayerBase>& layer(currentLayers[i]);
1920 layer->dump(result, buffer, SIZE);
1921 }
1922
1923 /*
1924 * Dump the layers in the purgatory
1925 */
1926
1927 const size_t purgatorySize = mLayerPurgatory.size();
1928 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1929 result.append(buffer);
1930 for (size_t i=0 ; i<purgatorySize ; i++) {
1931 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1932 layer->shortDump(result, buffer, SIZE);
1933 }
1934
1935 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001936 * Dump Display state
1937 */
1938
1939 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1940 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1941 snprintf(buffer, SIZE,
1942 "+ DisplayDevice[%u]\n"
1943 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1944 "flips=%u, secure=%d, numLayers=%u\n",
1945 dpy,
1946 hw->getDisplayId(), hw->getLayerStack(),
1947 hw->getWidth(), hw->getHeight(),
1948 hw->getOrientation(), hw->getTransform().getType(),
1949 hw->getPageFlipCount(),
1950 hw->getSecureLayerVisible(),
1951 hw->getVisibleLayersSortedByZ().size());
1952 result.append(buffer);
1953 }
1954
1955 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001956 * Dump SurfaceFlinger global state
1957 */
1958
1959 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1960 result.append(buffer);
1961
Mathias Agopian888c8222012-08-04 21:10:38 -07001962 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001963 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001964 const GLExtensions& extensions(GLExtensions::getInstance());
1965 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1966 extensions.getVendor(),
1967 extensions.getRenderer(),
1968 extensions.getVersion());
1969 result.append(buffer);
1970
1971 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001972 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001973 result.append(buffer);
1974
1975 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1976 result.append(buffer);
1977
Mathias Agopian42977342012-08-05 00:40:46 -07001978 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001979 snprintf(buffer, SIZE,
1980 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001981 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001982 result.append(buffer);
1983 snprintf(buffer, SIZE,
1984 " last eglSwapBuffers() time: %f us\n"
1985 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001986 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001987 " refresh-rate : %f fps\n"
1988 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001989 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001990 mLastSwapBufferTime/1000.0,
1991 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001992 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001993 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001994 hwc.getDpiX(),
1995 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001996 result.append(buffer);
1997
1998 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1999 inSwapBuffersDuration/1000.0);
2000 result.append(buffer);
2001
2002 snprintf(buffer, SIZE, " transaction time: %f us\n",
2003 inTransactionDuration/1000.0);
2004 result.append(buffer);
2005
2006 /*
2007 * VSYNC state
2008 */
2009 mEventThread->dump(result, buffer, SIZE);
2010
2011 /*
2012 * Dump HWComposer state
2013 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002014 snprintf(buffer, SIZE, "h/w composer state:\n");
2015 result.append(buffer);
2016 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2017 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2018 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2019 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002020 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002021
2022 /*
2023 * Dump gralloc state
2024 */
2025 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2026 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002027 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002028}
2029
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002030status_t SurfaceFlinger::onTransact(
2031 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2032{
2033 switch (code) {
2034 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002035 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002036 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002037 case BLANK:
2038 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039 {
2040 // codes that require permission check
2041 IPCThreadState* ipc = IPCThreadState::self();
2042 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002043 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002044 if ((uid != AID_GRAPHICS) &&
2045 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002046 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002047 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2048 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002049 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002050 break;
2051 }
2052 case CAPTURE_SCREEN:
2053 {
2054 // codes that require permission check
2055 IPCThreadState* ipc = IPCThreadState::self();
2056 const int pid = ipc->getCallingPid();
2057 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002058 if ((uid != AID_GRAPHICS) &&
2059 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002060 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002061 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2062 return PERMISSION_DENIED;
2063 }
2064 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002065 }
2066 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2069 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002070 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002071 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002072 IPCThreadState* ipc = IPCThreadState::self();
2073 const int pid = ipc->getCallingPid();
2074 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002075 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002076 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002077 return PERMISSION_DENIED;
2078 }
2079 int n;
2080 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002081 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002082 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002083 return NO_ERROR;
2084 case 1002: // SHOW_UPDATES
2085 n = data.readInt32();
2086 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002087 invalidateHwcGeometry();
2088 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002089 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002090 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002091 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002092 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002094 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002095 setTransactionFlags(
2096 eTransactionNeeded|
2097 eDisplayTransactionNeeded|
2098 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002099 return NO_ERROR;
2100 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002101 case 1006:{ // send empty update
2102 signalRefresh();
2103 return NO_ERROR;
2104 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002105 case 1008: // toggle use of hw composer
2106 n = data.readInt32();
2107 mDebugDisableHWC = n ? 1 : 0;
2108 invalidateHwcGeometry();
2109 repaintEverything();
2110 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002111 case 1009: // toggle use of transform hint
2112 n = data.readInt32();
2113 mDebugDisableTransformHint = n ? 1 : 0;
2114 invalidateHwcGeometry();
2115 repaintEverything();
2116 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002117 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002118 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119 reply->writeInt32(0);
2120 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002121 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002122 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 return NO_ERROR;
2124 case 1013: {
2125 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002126 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2127 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002128 }
2129 return NO_ERROR;
2130 }
2131 }
2132 return err;
2133}
2134
Mathias Agopian53331da2011-08-22 21:44:41 -07002135void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002136 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002137 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002138}
2139
Mathias Agopian59119e62010-10-11 12:37:43 -07002140// ---------------------------------------------------------------------------
2141
Mathias Agopian118d0242011-10-13 16:02:48 -07002142status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2143 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2144{
2145 Mutex::Autolock _l(mStateLock);
2146 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2147}
2148
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002149status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2150 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002151{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002152 ATRACE_CALL();
2153
Mathias Agopian59119e62010-10-11 12:37:43 -07002154 if (!GLExtensions::getInstance().haveFramebufferObject())
2155 return INVALID_OPERATION;
2156
2157 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002158 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2159 const uint32_t hw_w = hw->getWidth();
2160 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002161 GLfloat u = 1;
2162 GLfloat v = 1;
2163
2164 // make sure to clear all GL error flags
2165 while ( glGetError() != GL_NO_ERROR ) ;
2166
2167 // create a FBO
2168 GLuint name, tname;
2169 glGenTextures(1, &tname);
2170 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002171 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2172 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2174 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002175 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002176 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002177 GLint tw = (2 << (31 - clz(hw_w)));
2178 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2180 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002181 u = GLfloat(hw_w) / tw;
2182 v = GLfloat(hw_h) / th;
2183 }
2184 glGenFramebuffersOES(1, &name);
2185 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002186 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2187 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002188
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002189 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002190 glDisable(GL_TEXTURE_EXTERNAL_OES);
2191 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002192 glClearColor(0,0,0,1);
2193 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002194 glMatrixMode(GL_MODELVIEW);
2195 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002196 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002197 const size_t count = layers.size();
2198 for (size_t i=0 ; i<count ; ++i) {
2199 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002200 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002201 }
2202
Mathias Agopian42977342012-08-05 00:40:46 -07002203 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002204
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 // back to main framebuffer
2206 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 glDeleteFramebuffersOES(1, &name);
2208
2209 *textureName = tname;
2210 *uOut = u;
2211 *vOut = v;
2212 return NO_ERROR;
2213}
2214
2215// ---------------------------------------------------------------------------
2216
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2218 sp<IMemoryHeap>* heap,
2219 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002220 uint32_t sw, uint32_t sh,
2221 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002222{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002223 ATRACE_CALL();
2224
Mathias Agopian74c40c02010-09-29 13:02:36 -07002225 status_t result = PERMISSION_DENIED;
2226
2227 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002228 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002229 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002230 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002231 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002232
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002233 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002235 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236
2237 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002238 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2239 const uint32_t hw_w = hw->getWidth();
2240 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002242 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002243 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002244 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002245 return PERMISSION_DENIED;
2246 }
2247
2248 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002249 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002250 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002251 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252
2253 sw = (!sw) ? hw_w : sw;
2254 sh = (!sh) ? hw_h : sh;
2255 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002256 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002257
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002258// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2259// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002260
Mathias Agopian74c40c02010-09-29 13:02:36 -07002261 // make sure to clear all GL error flags
2262 while ( glGetError() != GL_NO_ERROR ) ;
2263
2264 // create a FBO
2265 GLuint name, tname;
2266 glGenRenderbuffersOES(1, &tname);
2267 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2268 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002269
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270 glGenFramebuffersOES(1, &name);
2271 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2272 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2273 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2274
2275 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002276
Mathias Agopian74c40c02010-09-29 13:02:36 -07002277 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2278
2279 // invert everything, b/c glReadPixel() below will invert the FB
2280 glViewport(0, 0, sw, sh);
2281 glMatrixMode(GL_PROJECTION);
2282 glPushMatrix();
2283 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002284 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002285 glMatrixMode(GL_MODELVIEW);
2286
2287 // redraw the screen entirely...
2288 glClearColor(0,0,0,1);
2289 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002290
Jamie Gennis9575f602011-10-07 14:51:16 -07002291 const LayerVector& layers(mDrawingState.layersSortedByZ);
2292 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002293 for (size_t i=0 ; i<count ; ++i) {
2294 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002295 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002296 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002297 const uint32_t z = layer->drawingState().z;
2298 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002299 if (filtering) layer->setFiltering(true);
2300 layer->draw(hw);
2301 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002302 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002303 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 }
2305
Mathias Agopian74c40c02010-09-29 13:02:36 -07002306 // check for errors and return screen capture
2307 if (glGetError() != GL_NO_ERROR) {
2308 // error while rendering
2309 result = INVALID_OPERATION;
2310 } else {
2311 // allocate shared memory large enough to hold the
2312 // screen capture
2313 sp<MemoryHeapBase> base(
2314 new MemoryHeapBase(size, 0, "screen-capture") );
2315 void* const ptr = base->getBase();
2316 if (ptr) {
2317 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002318 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2320 if (glGetError() == GL_NO_ERROR) {
2321 *heap = base;
2322 *w = sw;
2323 *h = sh;
2324 *f = PIXEL_FORMAT_RGBA_8888;
2325 result = NO_ERROR;
2326 }
2327 } else {
2328 result = NO_MEMORY;
2329 }
2330 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 glViewport(0, 0, hw_w, hw_h);
2332 glMatrixMode(GL_PROJECTION);
2333 glPopMatrix();
2334 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 } else {
2336 result = BAD_VALUE;
2337 }
2338
2339 // release FBO resources
2340 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2341 glDeleteRenderbuffersOES(1, &tname);
2342 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002343
Mathias Agopian42977342012-08-05 00:40:46 -07002344 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002345
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002346// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002347
Mathias Agopian74c40c02010-09-29 13:02:36 -07002348 return result;
2349}
2350
2351
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002352status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2353 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002354 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002355 uint32_t sw, uint32_t sh,
2356 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002357{
2358 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002359 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002360 return BAD_VALUE;
2361
2362 if (!GLExtensions::getInstance().haveFramebufferObject())
2363 return INVALID_OPERATION;
2364
2365 class MessageCaptureScreen : public MessageBase {
2366 SurfaceFlinger* flinger;
2367 DisplayID dpy;
2368 sp<IMemoryHeap>* heap;
2369 uint32_t* w;
2370 uint32_t* h;
2371 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002372 uint32_t sw;
2373 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002374 uint32_t minLayerZ;
2375 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002376 status_t result;
2377 public:
2378 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002380 uint32_t sw, uint32_t sh,
2381 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002382 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002383 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2384 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2385 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002386 {
2387 }
2388 status_t getResult() const {
2389 return result;
2390 }
2391 virtual bool handler() {
2392 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002393 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002394 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002395 return true;
2396 }
2397 };
2398
2399 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002400 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002401 status_t res = postMessageSync(msg);
2402 if (res == NO_ERROR) {
2403 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2404 }
2405 return res;
2406}
2407
2408// ---------------------------------------------------------------------------
2409
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002410SurfaceFlinger::LayerVector::LayerVector() {
2411}
2412
2413SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2414 : SortedVector<sp<LayerBase> >(rhs) {
2415}
2416
2417int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2418 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002419{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002420 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002421 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2422 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002423
2424 uint32_t ls = l->currentState().layerStack;
2425 uint32_t rs = r->currentState().layerStack;
2426 if (ls != rs)
2427 return ls - rs;
2428
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002429 uint32_t lz = l->currentState().z;
2430 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002431 if (lz != rz)
2432 return lz - rz;
2433
2434 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002435}
2436
2437// ---------------------------------------------------------------------------
2438
Mathias Agopiane57f2922012-08-09 16:29:12 -07002439SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2440}
2441
2442SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2443 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002444}
2445
2446// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447
Jamie Gennis9a78c902011-01-12 18:30:40 -08002448GraphicBufferAlloc::GraphicBufferAlloc() {}
2449
2450GraphicBufferAlloc::~GraphicBufferAlloc() {}
2451
2452sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002453 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002454 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2455 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002456 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002457 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002458 if (err == NO_MEMORY) {
2459 GraphicBuffer::dumpAllocationsToSystemLog();
2460 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002461 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002462 "failed (%s), handle=%p",
2463 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002464 return 0;
2465 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002466 return graphicBuffer;
2467}
2468
Jamie Gennis9a78c902011-01-12 18:30:40 -08002469// ---------------------------------------------------------------------------
2470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002471}; // namespace android