blob: cd1b3368454330eaa31c9a435d1d20b1470893c6 [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 McFadden13a082e2012-08-24 10:16:42 -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);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700177 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700178 mCurrentState.displays.add(token, info);
179
180 return token;
181}
182
183sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700184 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700185 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
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700400 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700401 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700402 mCurrentState.displays.add(mDefaultDisplays[i],
403 DisplayDeviceState((DisplayDevice::DisplayType)i));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700404 }
405 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700406 DisplayDevice::DISPLAY_PRIMARY,
407 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiane60b0682012-08-21 23:34:09 -0700408 anw, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700409 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700410
411 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700412 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700413 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800414
Mathias Agopian028508c2012-07-25 21:12:12 -0700415 // start the EventThread
416 mEventThread = new EventThread(this);
417 mEventQueue.setEventThread(mEventThread);
418
Mathias Agopian86303202012-07-24 22:46:10 -0700419 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700420 mHwc = new HWComposer(this,
421 *static_cast<HWComposer::EventHandler *>(this),
422 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700423
424 // initialize our drawing state
425 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700426
Mathias Agopiana4912602012-07-12 14:25:33 -0700427 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800428 mReadyToRunBarrier.open();
429
Andy McFadden13a082e2012-08-24 10:16:42 -0700430 // set initial conditions (e.g. unblank default device)
431 initializeDisplays();
432
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700433 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700434 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 return NO_ERROR;
437}
438
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700439int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
440 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
441 type : mHwc->allocateDisplayId();
442}
443
Mathias Agopiana67e4182012-06-19 17:26:12 -0700444void SurfaceFlinger::startBootAnim() {
445 // start boot animation
446 property_set("service.bootanim.exit", "0");
447 property_set("ctl.start", "bootanim");
448}
449
Mathias Agopiana4912602012-07-12 14:25:33 -0700450uint32_t SurfaceFlinger::getMaxTextureSize() const {
451 return mMaxTextureSize;
452}
453
454uint32_t SurfaceFlinger::getMaxViewportDims() const {
455 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
456 mMaxViewportDims[0] : mMaxViewportDims[1];
457}
458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800460
Jamie Gennis582270d2011-08-17 18:19:00 -0700461bool SurfaceFlinger::authenticateSurfaceTexture(
462 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800463 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700464 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800465
466 // Check the visible layer list for the ISurface
467 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
468 size_t count = currentLayers.size();
469 for (size_t i=0 ; i<count ; i++) {
470 const sp<LayerBase>& layer(currentLayers[i]);
471 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700472 if (lbc != NULL) {
473 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
474 if (lbcBinder == surfaceTextureBinder) {
475 return true;
476 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800477 }
478 }
479
480 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700481 // SurfaceTexture gets destroyed before all the clients are done using it,
482 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800483 // will instead fail later on when the client tries to use the surface,
484 // which should be reported as "surface XYZ returned an -ENODEV". The
485 // purgatorized layers are no less authentic than the visible ones, so this
486 // should not cause any harm.
487 size_t purgatorySize = mLayerPurgatory.size();
488 for (size_t i=0 ; i<purgatorySize ; i++) {
489 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
490 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700491 if (lbc != NULL) {
492 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
493 if (lbcBinder == surfaceTextureBinder) {
494 return true;
495 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800496 }
497 }
498
499 return false;
500}
501
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700502status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
503 // TODO: this is mostly here only for compatibility
504 // the display size is needed but the display metrics should come from elsewhere
505 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
506 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700507 return BAD_INDEX;
508 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700509
510 const HWComposer& hwc(getHwComposer());
511 float xdpi = hwc.getDpiX();
512 float ydpi = hwc.getDpiY();
513
514 // TODO: Not sure if display density should handled by SF any longer
515 class Density {
516 static int getDensityFromProperty(char const* propName) {
517 char property[PROPERTY_VALUE_MAX];
518 int density = 0;
519 if (property_get(propName, property, NULL) > 0) {
520 density = atoi(property);
521 }
522 return density;
523 }
524 public:
525 static int getEmuDensity() {
526 return getDensityFromProperty("qemu.sf.lcd_density"); }
527 static int getBuildDensity() {
528 return getDensityFromProperty("ro.sf.lcd_density"); }
529 };
530 // The density of the device is provided by a build property
531 float density = Density::getBuildDensity() / 160.0f;
532 if (density == 0) {
533 // the build doesn't provide a density -- this is wrong!
534 // use xdpi instead
535 ALOGE("ro.sf.lcd_density must be defined as a build property");
536 density = xdpi / 160.0f;
537 }
538 if (Density::getEmuDensity()) {
539 // if "qemu.sf.lcd_density" is specified, it overrides everything
540 xdpi = ydpi = density = Density::getEmuDensity();
541 density /= 160.0f;
542 }
543
Mathias Agopian42977342012-08-05 00:40:46 -0700544 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
545 info->w = hw->getWidth();
546 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700547 info->xdpi = xdpi;
548 info->ydpi = ydpi;
549 info->fps = float(1e9 / hwc.getRefreshPeriod());
550 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700551 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700552 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700553 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700554 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700555}
556
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800557// ----------------------------------------------------------------------------
558
559sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800560 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700561}
562
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700563void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700564
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700565 sp<IBinder> token;
566 { // scope for the lock
567 Mutex::Autolock _l(mStateLock);
568 token = mExtDisplayToken;
569 }
570
571 if (token == 0) {
572 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700573 }
574
575 { // scope for the lock
576 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700577 if (surface == 0) {
578 // release our current display. we're guarantee to have
579 // a reference to it (token), while we hold the lock
580 mExtDisplayToken = 0;
581 } else {
582 mExtDisplayToken = token;
583 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700584
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700585 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
586 info.surface = surface;
587 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700588 }
589}
590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800592
593void SurfaceFlinger::waitForEvent() {
594 mEventQueue.waitMessage();
595}
596
597void SurfaceFlinger::signalTransaction() {
598 mEventQueue.invalidate();
599}
600
601void SurfaceFlinger::signalLayerUpdate() {
602 mEventQueue.invalidate();
603}
604
605void SurfaceFlinger::signalRefresh() {
606 mEventQueue.refresh();
607}
608
609status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
610 nsecs_t reltime, uint32_t flags) {
611 return mEventQueue.postMessage(msg, reltime);
612}
613
614status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
615 nsecs_t reltime, uint32_t flags) {
616 status_t res = mEventQueue.postMessage(msg, reltime);
617 if (res == NO_ERROR) {
618 msg->wait();
619 }
620 return res;
621}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622
Mathias Agopian4fec8732012-06-29 14:12:52 -0700623bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 return true;
626}
627
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700628void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
629 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
630 // we should only receive DisplayDevice::DisplayType from the vsync callback
631 const wp<IBinder>& token(mDefaultDisplays[type]);
632 mEventThread->onVSyncReceived(token, timestamp);
633 }
Mathias Agopian86303202012-07-24 22:46:10 -0700634}
635
636void SurfaceFlinger::eventControl(int event, int enabled) {
637 getHwComposer().eventControl(event, enabled);
638}
639
Mathias Agopian4fec8732012-06-29 14:12:52 -0700640void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800641 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800642 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700643 case MessageQueue::INVALIDATE:
644 handleMessageTransaction();
645 handleMessageInvalidate();
646 signalRefresh();
647 break;
648 case MessageQueue::REFRESH:
649 handleMessageRefresh();
650 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800651 }
652}
653
Mathias Agopian4fec8732012-06-29 14:12:52 -0700654void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700655 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700656 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700657 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700658 }
659}
660
661void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700662 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700663 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700664}
665
666void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700667 ATRACE_CALL();
668 preComposition();
669 rebuildLayerStacks();
670 setUpHWComposer();
671 doDebugFlashRegions();
672 doComposition();
673 postComposition();
674}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700675
Mathias Agopiancd60f992012-08-16 16:28:27 -0700676void SurfaceFlinger::doDebugFlashRegions()
677{
678 // is debugging enabled
679 if (CC_LIKELY(!mDebugRegion))
680 return;
681
682 const bool repaintEverything = mRepaintEverything;
683 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
684 const sp<DisplayDevice>& hw(mDisplays[dpy]);
685 if (hw->canDraw()) {
686 // transform the dirty region into this screen's coordinate space
687 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
688 if (!dirtyRegion.isEmpty()) {
689 // redraw the whole screen
690 doComposeSurfaces(hw, Region(hw->bounds()));
691
692 // and draw the dirty region
693 glDisable(GL_TEXTURE_EXTERNAL_OES);
694 glDisable(GL_TEXTURE_2D);
695 glDisable(GL_BLEND);
696 glColor4f(1, 0, 1, 1);
697 const int32_t height = hw->getHeight();
698 Region::const_iterator it = dirtyRegion.begin();
699 Region::const_iterator const end = dirtyRegion.end();
700 while (it != end) {
701 const Rect& r = *it++;
702 GLfloat vertices[][2] = {
703 { r.left, height - r.top },
704 { r.left, height - r.bottom },
705 { r.right, height - r.bottom },
706 { r.right, height - r.top }
707 };
708 glVertexPointer(2, GL_FLOAT, 0, vertices);
709 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
710 }
711 hw->compositionComplete();
712 // FIXME
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700713 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700714 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
715 }
716 }
717 }
718 }
719
720 postFramebuffer();
721
722 if (mDebugRegion > 1) {
723 usleep(mDebugRegion * 1000);
724 }
725}
726
727void SurfaceFlinger::preComposition()
728{
729 bool needExtraInvalidate = false;
730 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
731 const size_t count = currentLayers.size();
732 for (size_t i=0 ; i<count ; i++) {
733 if (currentLayers[i]->onPreComposition()) {
734 needExtraInvalidate = true;
735 }
736 }
737 if (needExtraInvalidate) {
738 signalLayerUpdate();
739 }
740}
741
742void SurfaceFlinger::postComposition()
743{
744 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
745 const size_t count = currentLayers.size();
746 for (size_t i=0 ; i<count ; i++) {
747 currentLayers[i]->onPostComposition();
748 }
749}
750
751void SurfaceFlinger::rebuildLayerStacks() {
752 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700753 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700754 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700755 mVisibleRegionsDirty = false;
756 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700757 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700758 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700759 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700760 const Transform& tr(hw->getTransform());
761 const Rect bounds(hw->getBounds());
762
Mathias Agopian87baae12012-07-31 12:38:26 -0700763 Region opaqueRegion;
764 Region dirtyRegion;
765 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700766 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700767
768 Vector< sp<LayerBase> > layersSortedByZ;
769 const size_t count = currentLayers.size();
770 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700771 const sp<LayerBase>& layer(currentLayers[i]);
772 const Layer::State& s(layer->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700773 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700774 Region visibleRegion(tr.transform(layer->visibleRegion));
775 visibleRegion.andSelf(bounds);
776 if (!visibleRegion.isEmpty()) {
777 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -0700778 }
779 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700780 }
Mathias Agopian42977342012-08-05 00:40:46 -0700781 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700782 hw->undefinedRegion.set(bounds);
783 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
784 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700785 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700786 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700788
Mathias Agopiancd60f992012-08-16 16:28:27 -0700789void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700790 HWComposer& hwc(getHwComposer());
791 if (hwc.initCheck() == NO_ERROR) {
792 // build the h/w work list
793 const bool workListsDirty = mHwWorkListDirty;
794 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700795 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700796 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700797 const int32_t id = hw->getHwcDisplayId();
798 if (id >= 0) {
799 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700800 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700801 const size_t count = currentLayers.size();
802 if (hwc.createWorkList(id, count) >= 0) {
803 HWComposer::LayerListIterator cur = hwc.begin(id);
804 const HWComposer::LayerListIterator end = hwc.end(id);
805 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
806 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700807
Mathias Agopiane60b0682012-08-21 23:34:09 -0700808 if (CC_UNLIKELY(workListsDirty)) {
809 layer->setGeometry(hw, *cur);
810 if (mDebugDisableHWC || mDebugRegion) {
811 cur->setSkip(true);
812 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700813 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700814
Mathias Agopiane60b0682012-08-21 23:34:09 -0700815 /*
816 * update the per-frame h/w composer data for each layer
817 * and build the transparent region of the FB
818 */
819 layer->setPerFrameData(hw, *cur);
820 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700821 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700822 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700823 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700824 status_t err = hwc.prepare();
825 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
826 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700827}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700828
Mathias Agopiancd60f992012-08-16 16:28:27 -0700829void SurfaceFlinger::doComposition() {
830 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700831 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700832 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700833 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700834 if (hw->canDraw()) {
835 // transform the dirty region into this screen's coordinate space
836 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
837 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700838 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700839 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700840 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700841 hw->dirtyRegion.clear();
842 hw->flip(hw->swapRegion);
843 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700844 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700845 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700846 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700847 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700849}
850
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851void SurfaceFlinger::postFramebuffer()
852{
Mathias Agopian841cde52012-03-01 15:44:37 -0800853 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800854
Mathias Agopiana44b0412011-10-16 18:46:35 -0700855 const nsecs_t now = systemTime();
856 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700857
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700858 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700859 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700860 // FIXME: EGL spec says:
861 // "surface must be bound to the calling thread's current context,
862 // for the current rendering API."
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700863 DisplayDevice::makeCurrent(getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700864 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700865 }
866
Mathias Agopian92a979a2012-08-02 18:32:23 -0700867 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700868 sp<const DisplayDevice> hw(mDisplays[dpy]);
869 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700870 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700871 int32_t id = hw->getHwcDisplayId();
872 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700873 HWComposer::LayerListIterator cur = hwc.begin(id);
874 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700875 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700876 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700877 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700878 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700879 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700880 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700881 }
Jesse Hallef194142012-06-14 14:45:17 -0700882 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800883 }
884
Mathias Agopiana44b0412011-10-16 18:46:35 -0700885 mLastSwapBufferTime = systemTime() - now;
886 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887}
888
Mathias Agopian87baae12012-07-31 12:38:26 -0700889void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890{
Mathias Agopian841cde52012-03-01 15:44:37 -0800891 ATRACE_CALL();
892
Mathias Agopianca4d3602011-05-19 15:38:14 -0700893 Mutex::Autolock _l(mStateLock);
894 const nsecs_t now = systemTime();
895 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896
Mathias Agopianca4d3602011-05-19 15:38:14 -0700897 // Here we're guaranteed that some transaction flags are set
898 // so we can call handleTransactionLocked() unconditionally.
899 // We call getTransactionFlags(), which will also clear the flags,
900 // with mStateLock held to guarantee that mCurrentState won't change
901 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700902
Mathias Agopiane57f2922012-08-09 16:29:12 -0700903 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700904 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700905
Mathias Agopianca4d3602011-05-19 15:38:14 -0700906 mLastTransactionTime = systemTime() - now;
907 mDebugInTransaction = 0;
908 invalidateHwcGeometry();
909 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700910}
911
Mathias Agopian87baae12012-07-31 12:38:26 -0700912void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700913{
914 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 const size_t count = currentLayers.size();
916
917 /*
918 * Traversal of the children
919 * (perform the transaction for each of them if needed)
920 */
921
Mathias Agopian3559b072012-08-15 13:46:03 -0700922 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800923 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700924 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
926 if (!trFlags) continue;
927
928 const uint32_t flags = layer->doTransaction(0);
929 if (flags & Layer::eVisibleRegion)
930 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 }
932 }
933
934 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700935 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 */
937
Mathias Agopiane57f2922012-08-09 16:29:12 -0700938 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700939 // here we take advantage of Vector's copy-on-write semantics to
940 // improve performance by skipping the transaction entirely when
941 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700942 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
943 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700944 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700946 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700947 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948
949 // find the displays that were removed
950 // (ie: in drawing state but not in current state)
951 // also handle displays that changed
952 // (ie: displays that are in both lists)
953 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700954 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
955 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700956 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700957 if (!draw[i].isMainDisplay()) {
958 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700959 } else {
960 ALOGW("trying to remove the main display");
961 }
962 } else {
963 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700964 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700965 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700966 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700967 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700968 // recreating the DisplayDevice, so we just remove it
969 // from the drawing state, so that it get re-added
970 // below.
971 mDisplays.removeItem(display);
972 mDrawingState.displays.removeItemsAt(i);
973 dc--; i--;
974 // at this point we must loop to the next item
975 continue;
976 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700977
Mathias Agopian93997a82012-08-29 17:30:36 -0700978 const sp<DisplayDevice>& disp(getDisplayDevice(display));
979 if (disp != NULL) {
980 if (state.layerStack != draw[i].layerStack) {
981 disp->setLayerStack(state.layerStack);
982 }
983 if (state.orientation != draw[i].orientation ||
984 state.viewport != draw[i].viewport ||
985 state.frame != draw[i].frame) {
986 disp->setOrientation(state.orientation);
987 // TODO: take viewport and frame into account
988 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700989 }
990 }
991 }
992
993 // find displays that were added
994 // (ie: in current state but not in drawing state)
995 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700996 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700997 const DisplayDeviceState& state(curr[i]);
Mathias Agopian93997a82012-08-29 17:30:36 -0700998 if (state.surface != NULL) {
999 sp<SurfaceTextureClient> stc(
1000 new SurfaceTextureClient(state.surface));
1001 const wp<IBinder>& display(curr.keyAt(i));
1002 sp<DisplayDevice> disp = new DisplayDevice(this,
1003 state.type, display, stc, 0, mEGLConfig);
1004 disp->setLayerStack(state.layerStack);
1005 disp->setOrientation(state.orientation);
1006 // TODO: take viewport and frame into account
1007 mDisplays.add(display, disp);
1008 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001009 }
1010 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001012 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013
Mathias Agopian3559b072012-08-15 13:46:03 -07001014 /*
1015 * Perform our own transaction if needed
1016 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001017
Mathias Agopiancd60f992012-08-16 16:28:27 -07001018 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1019 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001020 // layers have been added
1021 mVisibleRegionsDirty = true;
1022 }
1023
1024 // some layers might have been removed, so
1025 // we need to update the regions they're exposing.
1026 if (mLayersRemoved) {
1027 mLayersRemoved = false;
1028 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001029 const size_t count = previousLayers.size();
1030 for (size_t i=0 ; i<count ; i++) {
1031 const sp<LayerBase>& layer(previousLayers[i]);
1032 if (currentLayers.indexOf(layer) < 0) {
1033 // this layer is not visible anymore
1034 // TODO: we could traverse the tree from front to back and
1035 // compute the actual visible region
1036 // TODO: we could cache the transformed region
1037 Layer::State front(layer->drawingState());
1038 Region visibleReg = front.transform.transform(
1039 Region(Rect(front.active.w, front.active.h)));
1040 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001041 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 }
1044
1045 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001046}
1047
1048void SurfaceFlinger::commitTransaction()
1049{
1050 if (!mLayersPendingRemoval.isEmpty()) {
1051 // Notify removed layers now that they can't be drawn from
1052 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1053 mLayersPendingRemoval[i]->onRemoved();
1054 }
1055 mLayersPendingRemoval.clear();
1056 }
1057
1058 mDrawingState = mCurrentState;
1059 mTransationPending = false;
1060 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061}
1062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001064 const LayerVector& currentLayers, uint32_t layerStack,
1065 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066{
Mathias Agopian841cde52012-03-01 15:44:37 -08001067 ATRACE_CALL();
1068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 Region aboveOpaqueLayers;
1070 Region aboveCoveredLayers;
1071 Region dirty;
1072
Mathias Agopian87baae12012-07-31 12:38:26 -07001073 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074
1075 size_t i = currentLayers.size();
1076 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001077 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078
1079 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001080 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081
Mathias Agopian87baae12012-07-31 12:38:26 -07001082 // only consider the layers on the given later stack
1083 if (s.layerStack != layerStack)
1084 continue;
1085
Mathias Agopianab028732010-03-16 16:41:46 -07001086 /*
1087 * opaqueRegion: area of a surface that is fully opaque.
1088 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001090
1091 /*
1092 * visibleRegion: area of a surface that is visible on screen
1093 * and not fully transparent. This is essentially the layer's
1094 * footprint minus the opaque regions above it.
1095 * Areas covered by a translucent surface are considered visible.
1096 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001098
1099 /*
1100 * coveredRegion: area of a surface that is covered by all
1101 * visible regions above it (which includes the translucent areas).
1102 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001104
1105
1106 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001107 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001108 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001109 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001111 if (!visibleRegion.isEmpty()) {
1112 // Remove the transparent area from the visible region
1113 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001114 Region transparentRegionScreen;
1115 const Transform tr(s.transform);
1116 if (tr.transformed()) {
1117 if (tr.preserveRects()) {
1118 // transform the transparent region
1119 transparentRegionScreen = tr.transform(s.transparentRegion);
1120 } else {
1121 // transformation too complex, can't do the
1122 // transparent region optimization.
1123 transparentRegionScreen.clear();
1124 }
1125 } else {
1126 transparentRegionScreen = s.transparentRegion;
1127 }
1128 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001129 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130
Mathias Agopianab028732010-03-16 16:41:46 -07001131 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001132 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001133 if (s.alpha==255 && !translucent &&
1134 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1135 // the opaque region is the layer's footprint
1136 opaqueRegion = visibleRegion;
1137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 }
1139 }
1140
Mathias Agopianab028732010-03-16 16:41:46 -07001141 // Clip the covered region to the visible region
1142 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1143
1144 // Update aboveCoveredLayers for next (lower) layer
1145 aboveCoveredLayers.orSelf(visibleRegion);
1146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 // subtract the opaque region covered by the layers above us
1148 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149
1150 // compute this layer's dirty region
1151 if (layer->contentDirty) {
1152 // we need to invalidate the whole region
1153 dirty = visibleRegion;
1154 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001155 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 layer->contentDirty = false;
1157 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001158 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001159 * the exposed region consists of two components:
1160 * 1) what's VISIBLE now and was COVERED before
1161 * 2) what's EXPOSED now less what was EXPOSED before
1162 *
1163 * note that (1) is conservative, we start with the whole
1164 * visible region but only keep what used to be covered by
1165 * something -- which mean it may have been exposed.
1166 *
1167 * (2) handles areas that were not covered by anything but got
1168 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001169 */
Mathias Agopianab028732010-03-16 16:41:46 -07001170 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001171 const Region oldVisibleRegion = layer->visibleRegion;
1172 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001173 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1174 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 }
1176 dirty.subtractSelf(aboveOpaqueLayers);
1177
1178 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001179 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180
Mathias Agopianab028732010-03-16 16:41:46 -07001181 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 // Store the visible region is screen space
1185 layer->setVisibleRegion(visibleRegion);
1186 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 }
1188
Mathias Agopian87baae12012-07-31 12:38:26 -07001189 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190}
1191
Mathias Agopian87baae12012-07-31 12:38:26 -07001192void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1193 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001194 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001195 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1196 if (hw->getLayerStack() == layerStack) {
1197 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001198 }
1199 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001200}
1201
1202void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001204 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205
Mathias Agopian4fec8732012-06-29 14:12:52 -07001206 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001207 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001208 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001209 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001210 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001211 const Region dirty(layer->latchBuffer(visibleRegions));
1212 Layer::State s(layer->drawingState());
1213 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001214 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001215
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001216 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217}
1218
Mathias Agopianad456f92011-01-13 17:53:01 -08001219void SurfaceFlinger::invalidateHwcGeometry()
1220{
1221 mHwWorkListDirty = true;
1222}
1223
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001224
Mathias Agopiancd60f992012-08-16 16:28:27 -07001225void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001226 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227{
Mathias Agopian87baae12012-07-31 12:38:26 -07001228 Region dirtyRegion(inDirtyRegion);
1229
Mathias Agopianb8a55602009-06-26 19:06:36 -07001230 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001231 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232
Mathias Agopian42977342012-08-05 00:40:46 -07001233 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001234 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001235 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1236 // takes a rectangle, we must make sure to update that whole
1237 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001238 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001239 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001240 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001241 // We need to redraw the rectangle that will be updated
1242 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001243 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001244 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001245 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001246 } else {
1247 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001248 dirtyRegion.set(hw->bounds());
1249 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 }
1251 }
1252
Mathias Agopiancd60f992012-08-16 16:28:27 -07001253 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254
Mathias Agopiancd60f992012-08-16 16:28:27 -07001255 // FIXME: we need to call eglSwapBuffers() on displays that have
1256 // GL composition and only on those.
1257 // however, currently hwc.commit() already does that for the main
1258 // display and never for the other ones
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001259 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001260 // FIXME: EGL spec says:
1261 // "surface must be bound to the calling thread's current context,
1262 // for the current rendering API."
1263 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001264 }
1265
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001266 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001267 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268}
1269
Mathias Agopiancd60f992012-08-16 16:28:27 -07001270void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001271{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001272 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001273 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001274 HWComposer::LayerListIterator cur = hwc.begin(id);
1275 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001276
Mathias Agopian85d751c2012-08-29 16:59:24 -07001277 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1278 if (hasGlesComposition) {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001279 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001280
1281 // set the frame buffer
1282 glMatrixMode(GL_MODELVIEW);
1283 glLoadIdentity();
1284
1285 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001286 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001287 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001288 // when using overlays, we assume a fully transparent framebuffer
1289 // NOTE: we could reduce how much we need to clear, for instance
1290 // remove where there are opaque FB layers. however, on some
1291 // GPUs doing a "clean slate" glClear might be more efficient.
1292 // We'll revisit later if needed.
1293 glClearColor(0, 0, 0, 0);
1294 glClear(GL_COLOR_BUFFER_BIT);
1295 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001296 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001297 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001298 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001299 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001300 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001301 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001302 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001303 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001304
Mathias Agopian85d751c2012-08-29 16:59:24 -07001305 /*
1306 * and then, render the layers targeted at the framebuffer
1307 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001308
Mathias Agopian85d751c2012-08-29 16:59:24 -07001309 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1310 const size_t count = layers.size();
1311 const Transform& tr = hw->getTransform();
1312 if (cur != end) {
1313 // we're using h/w composer
1314 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001315 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001316 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001317 if (!clip.isEmpty()) {
1318 switch (cur->getCompositionType()) {
1319 case HWC_OVERLAY: {
1320 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1321 && i
1322 && layer->isOpaque()
1323 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001324 // never clear the very first layer since we're
1325 // guaranteed the FB is already cleared
1326 layer->clearWithOpenGL(hw, clip);
1327 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001328 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001329 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001330 case HWC_FRAMEBUFFER: {
1331 layer->draw(hw, clip);
1332 break;
1333 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001334 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001335 }
1336 layer->setAcquireFence(hw, *cur);
1337 }
1338 } else {
1339 // we're not using h/w composer
1340 for (size_t i=0 ; i<count ; ++i) {
1341 const sp<LayerBase>& layer(layers[i]);
1342 const Region clip(dirty.intersect(
1343 tr.transform(layer->visibleRegion)));
1344 if (!clip.isEmpty()) {
1345 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001346 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001347 }
1348 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349}
1350
Mathias Agopian55801e42012-08-27 18:54:24 -07001351void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1352 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001354 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001355 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001356 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001357 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001358
Mathias Agopian55801e42012-08-27 18:54:24 -07001359 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001360 Region::const_iterator it = region.begin();
1361 Region::const_iterator const end = region.end();
1362 while (it != end) {
1363 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001364 GLfloat vertices[][2] = {
1365 { r.left, height - r.top },
1366 { r.left, height - r.bottom },
1367 { r.right, height - r.bottom },
1368 { r.right, height - r.top }
1369 };
1370 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001371 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1372 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373}
1374
Mathias Agopian96f08192010-06-02 23:28:45 -07001375ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1376 const sp<LayerBaseClient>& lbc)
1377{
Mathias Agopian96f08192010-06-02 23:28:45 -07001378 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001379 size_t name = client->attachLayer(lbc);
1380
Mathias Agopian96f08192010-06-02 23:28:45 -07001381 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001382 Mutex::Autolock _l(mStateLock);
1383 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001384
Mathias Agopian4f113742011-05-03 16:21:41 -07001385 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001386}
1387
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001388status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389{
1390 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001391 status_t err = purgatorizeLayer_l(layer);
1392 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001393 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001394 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395}
1396
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001397status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398{
1399 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1400 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001401 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 return NO_ERROR;
1403 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001404 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405}
1406
Mathias Agopian9a112062009-04-17 19:36:26 -07001407status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1408{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001409 // First add the layer to the purgatory list, which makes sure it won't
1410 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001411 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001412 if (err >= 0) {
1413 mLayerPurgatory.add(layerBase);
1414 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001415
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001416 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001417
Mathias Agopian3d579642009-06-04 18:46:21 -07001418 // it's possible that we don't find a layer, because it might
1419 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001420 // from the user because there is a race between Client::destroySurface(),
1421 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001422 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1423}
1424
Mathias Agopiandea20b12011-05-03 17:04:02 -07001425uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1426{
1427 return android_atomic_release_load(&mTransactionFlags);
1428}
1429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1431{
1432 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1433}
1434
Mathias Agopianbb641242010-05-18 17:06:55 -07001435uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436{
1437 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1438 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001439 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 }
1441 return old;
1442}
1443
Mathias Agopian8b33f032012-07-24 20:43:54 -07001444void SurfaceFlinger::setTransactionState(
1445 const Vector<ComposerState>& state,
1446 const Vector<DisplayState>& displays,
1447 uint32_t flags)
1448{
Mathias Agopian698c0872011-06-28 19:09:31 -07001449 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001450 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001451
1452 size_t count = displays.size();
1453 for (size_t i=0 ; i<count ; i++) {
1454 const DisplayState& s(displays[i]);
1455 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001456 }
1457
Mathias Agopiane57f2922012-08-09 16:29:12 -07001458 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001459 for (size_t i=0 ; i<count ; i++) {
1460 const ComposerState& s(state[i]);
1461 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001462 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001463 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001464
Mathias Agopian386aa982011-11-07 21:58:03 -08001465 if (transactionFlags) {
1466 // this triggers the transaction
1467 setTransactionFlags(transactionFlags);
1468
1469 // if this is a synchronous transaction, wait for it to take effect
1470 // before returning.
1471 if (flags & eSynchronous) {
1472 mTransationPending = true;
1473 }
1474 while (mTransationPending) {
1475 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1476 if (CC_UNLIKELY(err != NO_ERROR)) {
1477 // just in case something goes wrong in SF, return to the
1478 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001479 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001480 mTransationPending = false;
1481 break;
1482 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001483 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 }
1485}
1486
Mathias Agopiane57f2922012-08-09 16:29:12 -07001487uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1488{
1489 uint32_t flags = 0;
1490 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001491 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001492 const uint32_t what = s.what;
1493 if (what & DisplayState::eSurfaceChanged) {
1494 if (disp.surface->asBinder() != s.surface->asBinder()) {
1495 disp.surface = s.surface;
1496 flags |= eDisplayTransactionNeeded;
1497 }
1498 }
1499 if (what & DisplayState::eLayerStackChanged) {
1500 if (disp.layerStack != s.layerStack) {
1501 disp.layerStack = s.layerStack;
1502 flags |= eDisplayTransactionNeeded;
1503 }
1504 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001505 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001506 if (disp.orientation != s.orientation) {
1507 disp.orientation = s.orientation;
1508 flags |= eDisplayTransactionNeeded;
1509 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001510 }
1511 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001512 if (disp.frame != s.frame) {
1513 disp.frame = s.frame;
1514 flags |= eDisplayTransactionNeeded;
1515 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001516 }
1517 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001518 if (disp.viewport != s.viewport) {
1519 disp.viewport = s.viewport;
1520 flags |= eDisplayTransactionNeeded;
1521 }
1522 }
1523 }
1524 return flags;
1525}
1526
1527uint32_t SurfaceFlinger::setClientStateLocked(
1528 const sp<Client>& client,
1529 const layer_state_t& s)
1530{
1531 uint32_t flags = 0;
1532 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1533 if (layer != 0) {
1534 const uint32_t what = s.what;
1535 if (what & layer_state_t::ePositionChanged) {
1536 if (layer->setPosition(s.x, s.y))
1537 flags |= eTraversalNeeded;
1538 }
1539 if (what & layer_state_t::eLayerChanged) {
1540 // NOTE: index needs to be calculated before we update the state
1541 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1542 if (layer->setLayer(s.z)) {
1543 mCurrentState.layersSortedByZ.removeAt(idx);
1544 mCurrentState.layersSortedByZ.add(layer);
1545 // we need traversal (state changed)
1546 // AND transaction (list changed)
1547 flags |= eTransactionNeeded|eTraversalNeeded;
1548 }
1549 }
1550 if (what & layer_state_t::eSizeChanged) {
1551 if (layer->setSize(s.w, s.h)) {
1552 flags |= eTraversalNeeded;
1553 }
1554 }
1555 if (what & layer_state_t::eAlphaChanged) {
1556 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1557 flags |= eTraversalNeeded;
1558 }
1559 if (what & layer_state_t::eMatrixChanged) {
1560 if (layer->setMatrix(s.matrix))
1561 flags |= eTraversalNeeded;
1562 }
1563 if (what & layer_state_t::eTransparentRegionChanged) {
1564 if (layer->setTransparentRegionHint(s.transparentRegion))
1565 flags |= eTraversalNeeded;
1566 }
1567 if (what & layer_state_t::eVisibilityChanged) {
1568 if (layer->setFlags(s.flags, s.mask))
1569 flags |= eTraversalNeeded;
1570 }
1571 if (what & layer_state_t::eCropChanged) {
1572 if (layer->setCrop(s.crop))
1573 flags |= eTraversalNeeded;
1574 }
1575 if (what & layer_state_t::eLayerStackChanged) {
1576 // NOTE: index needs to be calculated before we update the state
1577 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1578 if (layer->setLayerStack(s.layerStack)) {
1579 mCurrentState.layersSortedByZ.removeAt(idx);
1580 mCurrentState.layersSortedByZ.add(layer);
1581 // we need traversal (state changed)
1582 // AND transaction (list changed)
1583 flags |= eTransactionNeeded|eTraversalNeeded;
1584 }
1585 }
1586 }
1587 return flags;
1588}
1589
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001590sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001591 ISurfaceComposerClient::surface_data_t* params,
1592 const String8& name,
1593 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001594 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595 uint32_t flags)
1596{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001597 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001598 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001599
1600 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001601 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001602 int(w), int(h));
1603 return surfaceHandle;
1604 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001605
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001606 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001607 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1608 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001609 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001611 case ISurfaceComposerClient::eFXSurfaceBlur:
1612 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001613 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001615 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001616 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001617 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 }
1619
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001620 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001621 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001622 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001623 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001625 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001627 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001628 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001629 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 }
1631
1632 return surfaceHandle;
1633}
1634
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001635sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001636 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001637 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001638 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639{
1640 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001641 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 case PIXEL_FORMAT_TRANSPARENT:
1643 case PIXEL_FORMAT_TRANSLUCENT:
1644 format = PIXEL_FORMAT_RGBA_8888;
1645 break;
1646 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001647#ifdef NO_RGBX_8888
1648 format = PIXEL_FORMAT_RGB_565;
1649#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001650 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001651#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 break;
1653 }
1654
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001655#ifdef NO_RGBX_8888
1656 if (format == PIXEL_FORMAT_RGBX_8888)
1657 format = PIXEL_FORMAT_RGBA_8888;
1658#endif
1659
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001660 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001661 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001662 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001663 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001664 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665 }
1666 return layer;
1667}
1668
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001669sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001670 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001671 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001673 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001674 return layer;
1675}
1676
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001677sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001678 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001679 uint32_t w, uint32_t h, uint32_t flags)
1680{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001681 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 return layer;
1683}
1684
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001685status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686{
Mathias Agopian9a112062009-04-17 19:36:26 -07001687 /*
1688 * called by the window manager, when a surface should be marked for
1689 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001690 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001691 * The surface is removed from the current and drawing lists, but placed
1692 * in the purgatory queue, so it's not destroyed right-away (we need
1693 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001694 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001695
Mathias Agopian48d819a2009-09-10 19:41:18 -07001696 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001697 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001698 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001699
Mathias Agopian48d819a2009-09-10 19:41:18 -07001700 if (layer != 0) {
1701 err = purgatorizeLayer_l(layer);
1702 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001703 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001704 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001705 }
1706 return err;
1707}
1708
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001709status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001710{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001711 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001712 status_t err = NO_ERROR;
1713 sp<LayerBaseClient> l(layer.promote());
1714 if (l != NULL) {
1715 Mutex::Autolock _l(mStateLock);
1716 err = removeLayer_l(l);
1717 if (err == NAME_NOT_FOUND) {
1718 // The surface wasn't in the current list, which means it was
1719 // removed already, which means it is in the purgatory,
1720 // and need to be removed from there.
1721 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001722 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001723 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001724 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001725 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001726 "error removing layer=%p (%s)", l.get(), strerror(-err));
1727 }
1728 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729}
1730
Mathias Agopianb60314a2012-04-10 22:09:54 -07001731// ---------------------------------------------------------------------------
1732
Andy McFadden13a082e2012-08-24 10:16:42 -07001733void SurfaceFlinger::onInitializeDisplays() {
1734 // reset screen orientation
1735 Vector<ComposerState> state;
1736 Vector<DisplayState> displays;
1737 DisplayState d;
1738 d.what = DisplayState::eOrientationChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001739 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001740 d.orientation = DisplayState::eOrientationDefault;
1741 displays.add(d);
1742 setTransactionState(state, displays, 0);
1743
1744 // XXX: this should init default device to "unblank" and all other devices to "blank"
1745 onScreenAcquired();
1746}
1747
1748void SurfaceFlinger::initializeDisplays() {
1749 class MessageScreenInitialized : public MessageBase {
1750 SurfaceFlinger* flinger;
1751 public:
1752 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1753 virtual bool handler() {
1754 flinger->onInitializeDisplays();
1755 return true;
1756 }
1757 };
1758 sp<MessageBase> msg = new MessageScreenInitialized(this);
1759 postMessageAsync(msg); // we may be called from main thread, use async message
1760}
1761
1762
Mathias Agopianb60314a2012-04-10 22:09:54 -07001763void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001764 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001765 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001766 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001767 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001768 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001769 mVisibleRegionsDirty = true;
1770 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771}
1772
Mathias Agopianb60314a2012-04-10 22:09:54 -07001773void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001774 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001775 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1776 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001777 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001778 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001779 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001780 // from this point on, SF will stop drawing
1781 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782}
1783
Colin Cross8e533062012-06-07 13:17:52 -07001784void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001785 class MessageScreenAcquired : public MessageBase {
1786 SurfaceFlinger* flinger;
1787 public:
1788 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1789 virtual bool handler() {
1790 flinger->onScreenAcquired();
1791 return true;
1792 }
1793 };
1794 sp<MessageBase> msg = new MessageScreenAcquired(this);
1795 postMessageSync(msg);
1796}
1797
Colin Cross8e533062012-06-07 13:17:52 -07001798void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001799 class MessageScreenReleased : public MessageBase {
1800 SurfaceFlinger* flinger;
1801 public:
1802 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1803 virtual bool handler() {
1804 flinger->onScreenReleased();
1805 return true;
1806 }
1807 };
1808 sp<MessageBase> msg = new MessageScreenReleased(this);
1809 postMessageSync(msg);
1810}
1811
1812// ---------------------------------------------------------------------------
1813
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1815{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001816 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001817 char buffer[SIZE];
1818 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001819
1820 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 snprintf(buffer, SIZE, "Permission Denial: "
1822 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1823 IPCThreadState::self()->getCallingPid(),
1824 IPCThreadState::self()->getCallingUid());
1825 result.append(buffer);
1826 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001827 // Try to get the main lock, but don't insist if we can't
1828 // (this would indicate SF is stuck, but we want to be able to
1829 // print something in dumpsys).
1830 int retry = 3;
1831 while (mStateLock.tryLock()<0 && --retry>=0) {
1832 usleep(1000000);
1833 }
1834 const bool locked(retry >= 0);
1835 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001836 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001837 "SurfaceFlinger appears to be unresponsive, "
1838 "dumping anyways (no locks held)\n");
1839 result.append(buffer);
1840 }
1841
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001842 bool dumpAll = true;
1843 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001844 size_t numArgs = args.size();
1845 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001846 if ((index < numArgs) &&
1847 (args[index] == String16("--list"))) {
1848 index++;
1849 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001850 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001851 }
1852
1853 if ((index < numArgs) &&
1854 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001855 index++;
1856 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001857 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001858 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001859
1860 if ((index < numArgs) &&
1861 (args[index] == String16("--latency-clear"))) {
1862 index++;
1863 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001864 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001865 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001867
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001868 if (dumpAll) {
1869 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001870 }
1871
Mathias Agopian9795c422009-08-26 16:36:26 -07001872 if (locked) {
1873 mStateLock.unlock();
1874 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 }
1876 write(fd, result.string(), result.size());
1877 return NO_ERROR;
1878}
1879
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001880void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1881 String8& result, char* buffer, size_t SIZE) const
1882{
1883 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1884 const size_t count = currentLayers.size();
1885 for (size_t i=0 ; i<count ; i++) {
1886 const sp<LayerBase>& layer(currentLayers[i]);
1887 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1888 result.append(buffer);
1889 }
1890}
1891
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001892void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1893 String8& result, char* buffer, size_t SIZE) const
1894{
1895 String8 name;
1896 if (index < args.size()) {
1897 name = String8(args[index]);
1898 index++;
1899 }
1900
1901 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1902 const size_t count = currentLayers.size();
1903 for (size_t i=0 ; i<count ; i++) {
1904 const sp<LayerBase>& layer(currentLayers[i]);
1905 if (name.isEmpty()) {
1906 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1907 result.append(buffer);
1908 }
1909 if (name.isEmpty() || (name == layer->getName())) {
1910 layer->dumpStats(result, buffer, SIZE);
1911 }
1912 }
1913}
1914
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001915void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1916 String8& result, char* buffer, size_t SIZE) const
1917{
1918 String8 name;
1919 if (index < args.size()) {
1920 name = String8(args[index]);
1921 index++;
1922 }
1923
1924 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1925 const size_t count = currentLayers.size();
1926 for (size_t i=0 ; i<count ; i++) {
1927 const sp<LayerBase>& layer(currentLayers[i]);
1928 if (name.isEmpty() || (name == layer->getName())) {
1929 layer->clearStats();
1930 }
1931 }
1932}
1933
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001934void SurfaceFlinger::dumpAllLocked(
1935 String8& result, char* buffer, size_t SIZE) const
1936{
1937 // figure out if we're stuck somewhere
1938 const nsecs_t now = systemTime();
1939 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1940 const nsecs_t inTransaction(mDebugInTransaction);
1941 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1942 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1943
1944 /*
1945 * Dump the visible layer list
1946 */
1947 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1948 const size_t count = currentLayers.size();
1949 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1950 result.append(buffer);
1951 for (size_t i=0 ; i<count ; i++) {
1952 const sp<LayerBase>& layer(currentLayers[i]);
1953 layer->dump(result, buffer, SIZE);
1954 }
1955
1956 /*
1957 * Dump the layers in the purgatory
1958 */
1959
1960 const size_t purgatorySize = mLayerPurgatory.size();
1961 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1962 result.append(buffer);
1963 for (size_t i=0 ; i<purgatorySize ; i++) {
1964 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1965 layer->shortDump(result, buffer, SIZE);
1966 }
1967
1968 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001969 * Dump Display state
1970 */
1971
1972 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1973 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1974 snprintf(buffer, SIZE,
1975 "+ DisplayDevice[%u]\n"
1976 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1977 "flips=%u, secure=%d, numLayers=%u\n",
1978 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001979 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001980 hw->getWidth(), hw->getHeight(),
1981 hw->getOrientation(), hw->getTransform().getType(),
1982 hw->getPageFlipCount(),
1983 hw->getSecureLayerVisible(),
1984 hw->getVisibleLayersSortedByZ().size());
1985 result.append(buffer);
1986 }
1987
1988 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001989 * Dump SurfaceFlinger global state
1990 */
1991
1992 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1993 result.append(buffer);
1994
Mathias Agopian888c8222012-08-04 21:10:38 -07001995 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001996 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001997 const GLExtensions& extensions(GLExtensions::getInstance());
1998 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1999 extensions.getVendor(),
2000 extensions.getRenderer(),
2001 extensions.getVersion());
2002 result.append(buffer);
2003
2004 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002005 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002006 result.append(buffer);
2007
2008 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2009 result.append(buffer);
2010
Mathias Agopian42977342012-08-05 00:40:46 -07002011 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002012 snprintf(buffer, SIZE,
2013 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002014 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002015 result.append(buffer);
2016 snprintf(buffer, SIZE,
2017 " last eglSwapBuffers() time: %f us\n"
2018 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002019 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002020 " refresh-rate : %f fps\n"
2021 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002022 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002023 mLastSwapBufferTime/1000.0,
2024 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002025 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07002026 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07002027 hwc.getDpiX(),
2028 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002029 result.append(buffer);
2030
2031 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2032 inSwapBuffersDuration/1000.0);
2033 result.append(buffer);
2034
2035 snprintf(buffer, SIZE, " transaction time: %f us\n",
2036 inTransactionDuration/1000.0);
2037 result.append(buffer);
2038
2039 /*
2040 * VSYNC state
2041 */
2042 mEventThread->dump(result, buffer, SIZE);
2043
2044 /*
2045 * Dump HWComposer state
2046 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002047 snprintf(buffer, SIZE, "h/w composer state:\n");
2048 result.append(buffer);
2049 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2050 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2051 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2052 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002053 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002054
2055 /*
2056 * Dump gralloc state
2057 */
2058 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2059 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002060 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002061}
2062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002063status_t SurfaceFlinger::onTransact(
2064 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2065{
2066 switch (code) {
2067 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002068 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002069 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002070 case BLANK:
2071 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072 {
2073 // codes that require permission check
2074 IPCThreadState* ipc = IPCThreadState::self();
2075 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002076 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002077 if ((uid != AID_GRAPHICS) &&
2078 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002079 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002080 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2081 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002083 break;
2084 }
2085 case CAPTURE_SCREEN:
2086 {
2087 // codes that require permission check
2088 IPCThreadState* ipc = IPCThreadState::self();
2089 const int pid = ipc->getCallingPid();
2090 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002091 if ((uid != AID_GRAPHICS) &&
2092 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002093 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002094 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2095 return PERMISSION_DENIED;
2096 }
2097 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002098 }
2099 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002101 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2102 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002103 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002104 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002105 IPCThreadState* ipc = IPCThreadState::self();
2106 const int pid = ipc->getCallingPid();
2107 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002108 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002109 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002110 return PERMISSION_DENIED;
2111 }
2112 int n;
2113 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002114 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002115 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002116 return NO_ERROR;
2117 case 1002: // SHOW_UPDATES
2118 n = data.readInt32();
2119 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002120 invalidateHwcGeometry();
2121 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002122 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002124 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002125 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002126 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002127 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002128 setTransactionFlags(
2129 eTransactionNeeded|
2130 eDisplayTransactionNeeded|
2131 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002132 return NO_ERROR;
2133 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002134 case 1006:{ // send empty update
2135 signalRefresh();
2136 return NO_ERROR;
2137 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002138 case 1008: // toggle use of hw composer
2139 n = data.readInt32();
2140 mDebugDisableHWC = n ? 1 : 0;
2141 invalidateHwcGeometry();
2142 repaintEverything();
2143 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002144 case 1009: // toggle use of transform hint
2145 n = data.readInt32();
2146 mDebugDisableTransformHint = n ? 1 : 0;
2147 invalidateHwcGeometry();
2148 repaintEverything();
2149 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002151 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002152 reply->writeInt32(0);
2153 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002154 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002155 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002156 return NO_ERROR;
2157 case 1013: {
2158 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002159 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2160 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002161 }
2162 return NO_ERROR;
2163 }
2164 }
2165 return err;
2166}
2167
Mathias Agopian53331da2011-08-22 21:44:41 -07002168void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002169 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002170 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002171}
2172
Mathias Agopian59119e62010-10-11 12:37:43 -07002173// ---------------------------------------------------------------------------
2174
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002175status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002176 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2177{
2178 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002179 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002180}
2181
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002182status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002183 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002184{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002185 ATRACE_CALL();
2186
Mathias Agopian59119e62010-10-11 12:37:43 -07002187 if (!GLExtensions::getInstance().haveFramebufferObject())
2188 return INVALID_OPERATION;
2189
2190 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002191 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2192 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002193 const uint32_t hw_w = hw->getWidth();
2194 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002195 GLfloat u = 1;
2196 GLfloat v = 1;
2197
2198 // make sure to clear all GL error flags
2199 while ( glGetError() != GL_NO_ERROR ) ;
2200
2201 // create a FBO
2202 GLuint name, tname;
2203 glGenTextures(1, &tname);
2204 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002205 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2206 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2208 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002209 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002210 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002211 GLint tw = (2 << (31 - clz(hw_w)));
2212 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002213 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2214 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002215 u = GLfloat(hw_w) / tw;
2216 v = GLfloat(hw_h) / th;
2217 }
2218 glGenFramebuffersOES(1, &name);
2219 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002220 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2221 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002222
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002223 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002224 glDisable(GL_TEXTURE_EXTERNAL_OES);
2225 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226 glClearColor(0,0,0,1);
2227 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002228 glMatrixMode(GL_MODELVIEW);
2229 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002230 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002231 const size_t count = layers.size();
2232 for (size_t i=0 ; i<count ; ++i) {
2233 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002234 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002235 }
2236
Mathias Agopian42977342012-08-05 00:40:46 -07002237 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002238
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002239 // back to main framebuffer
2240 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002241 glDeleteFramebuffersOES(1, &name);
2242
2243 *textureName = tname;
2244 *uOut = u;
2245 *vOut = v;
2246 return NO_ERROR;
2247}
2248
2249// ---------------------------------------------------------------------------
2250
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002251status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252 sp<IMemoryHeap>* heap,
2253 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002254 uint32_t sw, uint32_t sh,
2255 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002256{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002257 ATRACE_CALL();
2258
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259 status_t result = PERMISSION_DENIED;
2260
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002261 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002262 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002263 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002264
2265 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002266 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002267 const uint32_t hw_w = hw->getWidth();
2268 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002269
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002270 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002271 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002272 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002273 return PERMISSION_DENIED;
2274 }
2275
2276 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002277 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002279 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280
2281 sw = (!sw) ? hw_w : sw;
2282 sh = (!sh) ? hw_h : sh;
2283 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002284 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002285
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002286// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2287// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002288
Mathias Agopian74c40c02010-09-29 13:02:36 -07002289 // make sure to clear all GL error flags
2290 while ( glGetError() != GL_NO_ERROR ) ;
2291
2292 // create a FBO
2293 GLuint name, tname;
2294 glGenRenderbuffersOES(1, &tname);
2295 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2296 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002297
Mathias Agopian74c40c02010-09-29 13:02:36 -07002298 glGenFramebuffersOES(1, &name);
2299 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2300 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2301 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2302
2303 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002304
Mathias Agopian74c40c02010-09-29 13:02:36 -07002305 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2306
2307 // invert everything, b/c glReadPixel() below will invert the FB
2308 glViewport(0, 0, sw, sh);
2309 glMatrixMode(GL_PROJECTION);
2310 glPushMatrix();
2311 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002312 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002313 glMatrixMode(GL_MODELVIEW);
2314
2315 // redraw the screen entirely...
2316 glClearColor(0,0,0,1);
2317 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002318
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002319 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002320 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002321 for (size_t i=0 ; i<count ; ++i) {
2322 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002323 const uint32_t z = layer->drawingState().z;
2324 if (z >= minLayerZ && z <= maxLayerZ) {
2325 if (filtering) layer->setFiltering(true);
2326 layer->draw(hw);
2327 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002328 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 }
2330
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 // check for errors and return screen capture
2332 if (glGetError() != GL_NO_ERROR) {
2333 // error while rendering
2334 result = INVALID_OPERATION;
2335 } else {
2336 // allocate shared memory large enough to hold the
2337 // screen capture
2338 sp<MemoryHeapBase> base(
2339 new MemoryHeapBase(size, 0, "screen-capture") );
2340 void* const ptr = base->getBase();
2341 if (ptr) {
2342 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002343 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2345 if (glGetError() == GL_NO_ERROR) {
2346 *heap = base;
2347 *w = sw;
2348 *h = sh;
2349 *f = PIXEL_FORMAT_RGBA_8888;
2350 result = NO_ERROR;
2351 }
2352 } else {
2353 result = NO_MEMORY;
2354 }
2355 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356 glViewport(0, 0, hw_w, hw_h);
2357 glMatrixMode(GL_PROJECTION);
2358 glPopMatrix();
2359 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360 } else {
2361 result = BAD_VALUE;
2362 }
2363
2364 // release FBO resources
2365 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2366 glDeleteRenderbuffersOES(1, &tname);
2367 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002368
Mathias Agopian42977342012-08-05 00:40:46 -07002369 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002370
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002371// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002372
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 return result;
2374}
2375
2376
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002377status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002378 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 uint32_t* width, uint32_t* height, PixelFormat* format,
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{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002383 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002384 return BAD_VALUE;
2385
2386 if (!GLExtensions::getInstance().haveFramebufferObject())
2387 return INVALID_OPERATION;
2388
2389 class MessageCaptureScreen : public MessageBase {
2390 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002391 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002392 sp<IMemoryHeap>* heap;
2393 uint32_t* w;
2394 uint32_t* h;
2395 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002396 uint32_t sw;
2397 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002398 uint32_t minLayerZ;
2399 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002400 status_t result;
2401 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002402 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002403 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002404 uint32_t sw, uint32_t sh,
2405 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002406 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2408 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2409 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002410 {
2411 }
2412 status_t getResult() const {
2413 return result;
2414 }
2415 virtual bool handler() {
2416 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002417 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002418 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002419 return true;
2420 }
2421 };
2422
2423 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002424 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002425 status_t res = postMessageSync(msg);
2426 if (res == NO_ERROR) {
2427 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2428 }
2429 return res;
2430}
2431
2432// ---------------------------------------------------------------------------
2433
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002434SurfaceFlinger::LayerVector::LayerVector() {
2435}
2436
2437SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2438 : SortedVector<sp<LayerBase> >(rhs) {
2439}
2440
2441int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2442 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002443{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002444 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002445 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2446 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002447
2448 uint32_t ls = l->currentState().layerStack;
2449 uint32_t rs = r->currentState().layerStack;
2450 if (ls != rs)
2451 return ls - rs;
2452
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002453 uint32_t lz = l->currentState().z;
2454 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002455 if (lz != rz)
2456 return lz - rz;
2457
2458 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002459}
2460
2461// ---------------------------------------------------------------------------
2462
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002463SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2464 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002465}
2466
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002467SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2468 : type(type), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002469}
2470
2471// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472
Jamie Gennis9a78c902011-01-12 18:30:40 -08002473GraphicBufferAlloc::GraphicBufferAlloc() {}
2474
2475GraphicBufferAlloc::~GraphicBufferAlloc() {}
2476
2477sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002478 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002479 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2480 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002481 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002482 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002483 if (err == NO_MEMORY) {
2484 GraphicBuffer::dumpAllocationsToSystemLog();
2485 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002486 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002487 "failed (%s), handle=%p",
2488 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002489 return 0;
2490 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002491 return graphicBuffer;
2492}
2493
Jamie Gennis9a78c902011-01-12 18:30:40 -08002494// ---------------------------------------------------------------------------
2495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496}; // namespace android