blob: fbf2873038bef37b07e5bdf4a4fb3c756528aa91 [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 Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070099 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Steve Blocka19954a2012-01-04 20:05:49 +0000101 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
112 DdmConnection::start(getServiceName());
113 }
114
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700115 ALOGI_IF(mDebugRegion, "showupdates enabled");
116 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800119void SurfaceFlinger::onFirstRef()
120{
121 mEventQueue.init(this);
122
123 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
124
125 // Wait for the main thread to be done with its initialization
126 mReadyToRunBarrier.wait();
127}
128
129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130SurfaceFlinger::~SurfaceFlinger()
131{
Mathias Agopiana4912602012-07-12 14:25:33 -0700132 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
133 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
134 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800137void SurfaceFlinger::binderDied(const wp<IBinder>& who)
138{
139 // the window manager died on us. prepare its eulogy.
140
141 // reset screen orientation
142 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700143 Vector<DisplayState> displays;
144 DisplayState d;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700145 d.orientation = DisplayState::eOrientationDefault;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700146 displays.add(d);
147 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148
149 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700150 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800151}
152
Mathias Agopian7e27f052010-05-28 14:22:23 -0700153sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154{
Mathias Agopian96f08192010-06-02 23:28:45 -0700155 sp<ISurfaceComposerClient> bclient;
156 sp<Client> client(new Client(this));
157 status_t err = client->initCheck();
158 if (err == NO_ERROR) {
159 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 return bclient;
162}
163
Mathias Agopiane57f2922012-08-09 16:29:12 -0700164sp<IBinder> SurfaceFlinger::createDisplay()
165{
166 class DisplayToken : public BBinder {
167 sp<SurfaceFlinger> flinger;
168 virtual ~DisplayToken() {
169 // no more references, this display must be terminated
170 Mutex::Autolock _l(flinger->mStateLock);
171 flinger->mCurrentState.displays.removeItem(this);
172 flinger->setTransactionFlags(eDisplayTransactionNeeded);
173 }
174 public:
175 DisplayToken(const sp<SurfaceFlinger>& flinger)
176 : flinger(flinger) {
177 }
178 };
179
180 sp<BBinder> token = new DisplayToken(this);
181
182 Mutex::Autolock _l(mStateLock);
183 DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
184 mCurrentState.displays.add(token, info);
185
186 return token;
187}
188
189sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
190 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
191 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
192 return NULL;
193 }
194 return mDefaultDisplays[id];
195}
196
Jamie Gennis9a78c902011-01-12 18:30:40 -0800197sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
198{
199 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
200 return gba;
201}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203void SurfaceFlinger::bootFinished()
204{
205 const nsecs_t now = systemTime();
206 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000207 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700208 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700209
210 // wait patiently for the window manager death
211 const String16 name("window");
212 sp<IBinder> window(defaultServiceManager()->getService(name));
213 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700214 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700215 }
216
217 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700218 // formerly we would just kill the process, but we now ask it to exit so it
219 // can choose where to stop the animation.
220 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700223void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
224 class MessageDestroyGLTexture : public MessageBase {
225 GLuint texture;
226 public:
227 MessageDestroyGLTexture(GLuint texture)
228 : texture(texture) {
229 }
230 virtual bool handler() {
231 glDeleteTextures(1, &texture);
232 return true;
233 }
234 };
235 postMessageAsync(new MessageDestroyGLTexture(texture));
236}
237
Mathias Agopiana4912602012-07-12 14:25:33 -0700238status_t SurfaceFlinger::selectConfigForPixelFormat(
239 EGLDisplay dpy,
240 EGLint const* attrs,
241 PixelFormat format,
242 EGLConfig* outConfig)
243{
244 EGLConfig config = NULL;
245 EGLint numConfigs = -1, n=0;
246 eglGetConfigs(dpy, NULL, 0, &numConfigs);
247 EGLConfig* const configs = new EGLConfig[numConfigs];
248 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
249 for (int i=0 ; i<n ; i++) {
250 EGLint nativeVisualId = 0;
251 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
252 if (nativeVisualId>0 && format == nativeVisualId) {
253 *outConfig = configs[i];
254 delete [] configs;
255 return NO_ERROR;
256 }
257 }
258 delete [] configs;
259 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260}
261
Mathias Agopiana4912602012-07-12 14:25:33 -0700262EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
263 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
264 // it is to be used with WIFI displays
265 EGLConfig config;
266 EGLint dummy;
267 status_t err;
268 EGLint attribs[] = {
269 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
270 EGL_RECORDABLE_ANDROID, EGL_TRUE,
271 EGL_NONE
272 };
273 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
274 if (err) {
275 // maybe we failed because of EGL_RECORDABLE_ANDROID
276 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
277 attribs[2] = EGL_NONE;
278 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
279 }
280 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
281 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
282 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
283 }
284 return config;
285}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
Mathias Agopiana4912602012-07-12 14:25:33 -0700287EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
288 // Also create our EGLContext
289 EGLint contextAttributes[] = {
290#ifdef EGL_IMG_context_priority
291#ifdef HAS_CONTEXT_PRIORITY
292#warning "using EGL_IMG_context_priority"
293 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
294#endif
295#endif
296 EGL_NONE, EGL_NONE
297 };
298 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
299 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
300 return ctxt;
301}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopiana4912602012-07-12 14:25:33 -0700303void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
304 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
305 if (!result) {
306 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
307 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 }
309
Mathias Agopiana4912602012-07-12 14:25:33 -0700310 GLExtensions& extensions(GLExtensions::getInstance());
311 extensions.initWithGLStrings(
312 glGetString(GL_VENDOR),
313 glGetString(GL_RENDERER),
314 glGetString(GL_VERSION),
315 glGetString(GL_EXTENSIONS),
316 eglQueryString(display, EGL_VENDOR),
317 eglQueryString(display, EGL_VERSION),
318 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700319
Mathias Agopiana4912602012-07-12 14:25:33 -0700320 EGLint w, h;
321 eglQuerySurface(display, surface, EGL_WIDTH, &w);
322 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700323
Mathias Agopiana4912602012-07-12 14:25:33 -0700324 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
325 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700328 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 glShadeModel(GL_FLAT);
331 glDisable(GL_DITHER);
332 glDisable(GL_CULL_FACE);
333
Mathias Agopiana4912602012-07-12 14:25:33 -0700334 struct pack565 {
335 inline uint16_t operator() (int r, int g, int b) const {
336 return (r<<11)|(g<<5)|b;
337 }
338 } pack565;
339
Jamie Gennis9575f602011-10-07 14:51:16 -0700340 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
341 glGenTextures(1, &mProtectedTexName);
342 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
343 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
344 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
347 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
348 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
350 glViewport(0, 0, w, h);
351 glMatrixMode(GL_PROJECTION);
352 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700353 // put the origin in the left-bottom corner
354 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 -0800355
Mathias Agopiana4912602012-07-12 14:25:33 -0700356 // print some debugging info
357 EGLint r,g,b,a;
358 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
359 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
360 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
361 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
362 ALOGI("EGL informations:");
363 ALOGI("vendor : %s", extensions.getEglVendor());
364 ALOGI("version : %s", extensions.getEglVersion());
365 ALOGI("extensions: %s", extensions.getEglExtension());
366 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
367 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
368 ALOGI("OpenGL ES informations:");
369 ALOGI("vendor : %s", extensions.getVendor());
370 ALOGI("renderer : %s", extensions.getRenderer());
371 ALOGI("version : %s", extensions.getVersion());
372 ALOGI("extensions: %s", extensions.getExtension());
373 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
374 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
375}
376
Mathias Agopiana4912602012-07-12 14:25:33 -0700377status_t SurfaceFlinger::readyToRun()
378{
379 ALOGI( "SurfaceFlinger's main thread ready to run. "
380 "Initializing graphics H/W...");
381
Mathias Agopiana4912602012-07-12 14:25:33 -0700382 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700383 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
384 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700385
386 // Initialize the main display
387 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700388 sp<FramebufferSurface> fbs = FramebufferSurface::create();
389 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700390 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
391 exit(0);
392 }
393
Mathias Agopiane57f2922012-08-09 16:29:12 -0700394 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
395 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700396
Mathias Agopiana4912602012-07-12 14:25:33 -0700397 // initialize the config and context
398 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700399 ANativeWindow* const anw = stc.get();
400 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700401 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
402 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700403
404 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700405
406 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
407 mDefaultDisplays[i] = new BBinder();
408 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
409 }
410 sp<DisplayDevice> hw = new DisplayDevice(this,
411 DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
412 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700413
414 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700415 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700416 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800417
Mathias Agopian028508c2012-07-25 21:12:12 -0700418 // start the EventThread
419 mEventThread = new EventThread(this);
420 mEventQueue.setEventThread(mEventThread);
421
Mathias Agopian86303202012-07-24 22:46:10 -0700422 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700423 mHwc = new HWComposer(this,
424 *static_cast<HWComposer::EventHandler *>(this),
425 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700426
427 // initialize our drawing state
428 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700429
Mathias Agopiana4912602012-07-12 14:25:33 -0700430 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800431 mReadyToRunBarrier.open();
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 Agopiana67e4182012-06-19 17:26:12 -0700439void SurfaceFlinger::startBootAnim() {
440 // start boot animation
441 property_set("service.bootanim.exit", "0");
442 property_set("ctl.start", "bootanim");
443}
444
Mathias Agopiana4912602012-07-12 14:25:33 -0700445uint32_t SurfaceFlinger::getMaxTextureSize() const {
446 return mMaxTextureSize;
447}
448
449uint32_t SurfaceFlinger::getMaxViewportDims() const {
450 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
451 mMaxViewportDims[0] : mMaxViewportDims[1];
452}
453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800455
Jamie Gennis582270d2011-08-17 18:19:00 -0700456bool SurfaceFlinger::authenticateSurfaceTexture(
457 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800458 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700459 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800460
461 // Check the visible layer list for the ISurface
462 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
463 size_t count = currentLayers.size();
464 for (size_t i=0 ; i<count ; i++) {
465 const sp<LayerBase>& layer(currentLayers[i]);
466 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700467 if (lbc != NULL) {
468 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
469 if (lbcBinder == surfaceTextureBinder) {
470 return true;
471 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800472 }
473 }
474
475 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700476 // SurfaceTexture gets destroyed before all the clients are done using it,
477 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800478 // will instead fail later on when the client tries to use the surface,
479 // which should be reported as "surface XYZ returned an -ENODEV". The
480 // purgatorized layers are no less authentic than the visible ones, so this
481 // should not cause any harm.
482 size_t purgatorySize = mLayerPurgatory.size();
483 for (size_t i=0 ; i<purgatorySize ; i++) {
484 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
485 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700486 if (lbc != NULL) {
487 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
488 if (lbcBinder == surfaceTextureBinder) {
489 return true;
490 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800491 }
492 }
493
494 return false;
495}
496
Mathias Agopianc666cae2012-07-25 18:56:13 -0700497status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700498 // TODO: this is here only for compatibility -- should go away eventually.
499 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700500 return BAD_INDEX;
501 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700502
503 const HWComposer& hwc(getHwComposer());
504 float xdpi = hwc.getDpiX();
505 float ydpi = hwc.getDpiY();
506
507 // TODO: Not sure if display density should handled by SF any longer
508 class Density {
509 static int getDensityFromProperty(char const* propName) {
510 char property[PROPERTY_VALUE_MAX];
511 int density = 0;
512 if (property_get(propName, property, NULL) > 0) {
513 density = atoi(property);
514 }
515 return density;
516 }
517 public:
518 static int getEmuDensity() {
519 return getDensityFromProperty("qemu.sf.lcd_density"); }
520 static int getBuildDensity() {
521 return getDensityFromProperty("ro.sf.lcd_density"); }
522 };
523 // The density of the device is provided by a build property
524 float density = Density::getBuildDensity() / 160.0f;
525 if (density == 0) {
526 // the build doesn't provide a density -- this is wrong!
527 // use xdpi instead
528 ALOGE("ro.sf.lcd_density must be defined as a build property");
529 density = xdpi / 160.0f;
530 }
531 if (Density::getEmuDensity()) {
532 // if "qemu.sf.lcd_density" is specified, it overrides everything
533 xdpi = ydpi = density = Density::getEmuDensity();
534 density /= 160.0f;
535 }
536
Mathias Agopian42977342012-08-05 00:40:46 -0700537 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
538 info->w = hw->getWidth();
539 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700540 info->xdpi = xdpi;
541 info->ydpi = ydpi;
542 info->fps = float(1e9 / hwc.getRefreshPeriod());
543 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700544 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700545 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700546 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700547 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700548}
549
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800550// ----------------------------------------------------------------------------
551
552sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800553 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700554}
555
Mathias Agopian3094df32012-06-18 18:06:45 -0700556void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700557 EGLSurface result = EGL_NO_SURFACE;
558 EGLSurface old_surface = EGL_NO_SURFACE;
559 sp<SurfaceTextureClient> stc;
560
561 if (display != NULL) {
562 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700563 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700564 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700565 ALOGE_IF(result == EGL_NO_SURFACE,
566 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
567 display.get());
568 }
569
570 { // scope for the lock
571 Mutex::Autolock _l(mStateLock);
572 old_surface = mExternalDisplaySurface;
573 mExternalDisplayNativeWindow = stc;
574 mExternalDisplaySurface = result;
575 ALOGD("mExternalDisplaySurface = %p", result);
576 }
577
578 if (old_surface != EGL_NO_SURFACE) {
579 // Note: EGL allows to destroy an object while its current
580 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700581 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700582 }
583}
584
585EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
586 Mutex::Autolock _l(mStateLock);
587 return mExternalDisplaySurface;
588}
589
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800591
592void SurfaceFlinger::waitForEvent() {
593 mEventQueue.waitMessage();
594}
595
596void SurfaceFlinger::signalTransaction() {
597 mEventQueue.invalidate();
598}
599
600void SurfaceFlinger::signalLayerUpdate() {
601 mEventQueue.invalidate();
602}
603
604void SurfaceFlinger::signalRefresh() {
605 mEventQueue.refresh();
606}
607
608status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
609 nsecs_t reltime, uint32_t flags) {
610 return mEventQueue.postMessage(msg, reltime);
611}
612
613status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
614 nsecs_t reltime, uint32_t flags) {
615 status_t res = mEventQueue.postMessage(msg, reltime);
616 if (res == NO_ERROR) {
617 msg->wait();
618 }
619 return res;
620}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621
Mathias Agopian4fec8732012-06-29 14:12:52 -0700622bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 return true;
625}
626
Mathias Agopian86303202012-07-24 22:46:10 -0700627void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700628 mEventThread->onVSyncReceived(dpy, timestamp);
629}
630
631void SurfaceFlinger::eventControl(int event, int enabled) {
632 getHwComposer().eventControl(event, enabled);
633}
634
Mathias Agopian4fec8732012-06-29 14:12:52 -0700635void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800636 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800637 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700638 case MessageQueue::INVALIDATE:
639 handleMessageTransaction();
640 handleMessageInvalidate();
641 signalRefresh();
642 break;
643 case MessageQueue::REFRESH:
644 handleMessageRefresh();
645 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800646 }
647}
648
Mathias Agopian4fec8732012-06-29 14:12:52 -0700649void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700650 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700652 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653 }
654}
655
656void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700657 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700658}
659
660void SurfaceFlinger::handleMessageRefresh() {
661 handleRefresh();
662
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700663 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700664 mVisibleRegionsDirty = false;
665 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700666
667 /*
668 * rebuild the visible layer list per screen
669 */
670
Mathias Agopian87baae12012-07-31 12:38:26 -0700671 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700672 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700673 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700674 Region opaqueRegion;
675 Region dirtyRegion;
676 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700677 hw->getLayerStack(), dirtyRegion, opaqueRegion);
678 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700679
680 Vector< sp<LayerBase> > layersSortedByZ;
681 const size_t count = currentLayers.size();
682 for (size_t i=0 ; i<count ; i++) {
683 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700684 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700685 if (!currentLayers[i]->visibleRegion.isEmpty()) {
686 layersSortedByZ.add(currentLayers[i]);
687 }
688 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700689 }
Mathias Agopian42977342012-08-05 00:40:46 -0700690 hw->setVisibleLayersSortedByZ(layersSortedByZ);
691 hw->undefinedRegion.set(hw->getBounds());
692 hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700693 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700694 }
695
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700696 HWComposer& hwc(getHwComposer());
697 if (hwc.initCheck() == NO_ERROR) {
698 // build the h/w work list
699 const bool workListsDirty = mHwWorkListDirty;
700 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700701 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700702 sp<const DisplayDevice> hw(mDisplays[dpy]);
703 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700704 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700705
Mathias Agopian1e260872012-08-08 18:35:12 -0700706 const int32_t id = hw->getDisplayId();
707 if (hwc.createWorkList(id, count) >= 0) {
708 HWComposer::LayerListIterator cur = hwc.begin(id);
709 const HWComposer::LayerListIterator end = hwc.end(id);
710 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
711 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700712
Mathias Agopian1e260872012-08-08 18:35:12 -0700713 if (CC_UNLIKELY(workListsDirty)) {
714 layer->setGeometry(hw, *cur);
715 if (mDebugDisableHWC || mDebugRegion) {
716 cur->setSkip(true);
717 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700718 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700719
Mathias Agopian1e260872012-08-08 18:35:12 -0700720 /*
721 * update the per-frame h/w composer data for each layer
722 * and build the transparent region of the FB
723 */
724 layer->setPerFrameData(hw, *cur);
725 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700726 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700727 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700728 status_t err = hwc.prepare();
729 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
730 }
731
732 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700733 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700734 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700735
Mathias Agopian87baae12012-07-31 12:38:26 -0700736 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700737 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700738 Region dirtyRegion;
739 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700740 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700741 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700742 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
743 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700744 }
Mathias Agopian42977342012-08-05 00:40:46 -0700745 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700746
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700747 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700748 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700749 // repaint the framebuffer (if needed)
750 handleRepaint(hw, dirtyRegion);
751 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700752 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700753 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700754 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700755 }
756
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700757 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700758
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700759
760#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700761 // render to the external display if we have one
762 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
763 if (externalDisplaySurface != EGL_NO_SURFACE) {
764 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
765 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
766 externalDisplaySurface, externalDisplaySurface,
767 eglGetCurrentContext());
768
769 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
770
771 if (success) {
772 // redraw the screen entirely...
773 glDisable(GL_TEXTURE_EXTERNAL_OES);
774 glDisable(GL_TEXTURE_2D);
775 glClearColor(0,0,0,1);
776 glClear(GL_COLOR_BUFFER_BIT);
777 glMatrixMode(GL_MODELVIEW);
778 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700779
Mathias Agopian42977342012-08-05 00:40:46 -0700780 const sp<DisplayDevice>& hw(getDisplayDevice(0));
781 const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700782 const size_t count = layers.size();
783 for (size_t i=0 ; i<count ; ++i) {
784 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700785 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700786 }
787
788 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
789 ALOGE_IF(!success, "external display eglSwapBuffers failed");
790
Mathias Agopian42977342012-08-05 00:40:46 -0700791 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700792 }
793
794 success = eglMakeCurrent(eglGetCurrentDisplay(),
795 cur, cur, eglGetCurrentContext());
796
797 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
798 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700799#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700800
801}
802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803void SurfaceFlinger::postFramebuffer()
804{
Mathias Agopian841cde52012-03-01 15:44:37 -0800805 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800806
Mathias Agopiana44b0412011-10-16 18:46:35 -0700807 const nsecs_t now = systemTime();
808 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700809
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700810 HWComposer& hwc(getHwComposer());
811
Mathias Agopian92a979a2012-08-02 18:32:23 -0700812 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700813 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700814 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700815 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700816 const size_t count = currentLayers.size();
Mathias Agopian1e260872012-08-08 18:35:12 -0700817 const int32_t id = hw->getDisplayId();
818 HWComposer::LayerListIterator cur = hwc.begin(id);
819 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700820 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
821 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700822 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700823 }
824 }
Mathias Agopian42977342012-08-05 00:40:46 -0700825 hw->flip(hw->swapRegion);
826 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700827 }
828
Jesse Hallef194142012-06-14 14:45:17 -0700829 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700830 // FIXME: eventually commit() won't take arguments
Mathias Agopian42977342012-08-05 00:40:46 -0700831 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 }
833
Mathias Agopian92a979a2012-08-02 18:32:23 -0700834 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700835 sp<const DisplayDevice> hw(mDisplays[dpy]);
836 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700837 const size_t count = currentLayers.size();
838 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700839 int32_t id = hw->getDisplayId();
840 HWComposer::LayerListIterator cur = hwc.begin(id);
841 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700842 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700843 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700844 }
845 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700846 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700847 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700848 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 }
Jesse Hallef194142012-06-14 14:45:17 -0700850 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851
852 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800853 }
854
Mathias Agopiana44b0412011-10-16 18:46:35 -0700855 mLastSwapBufferTime = systemTime() - now;
856 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857}
858
Mathias Agopian87baae12012-07-31 12:38:26 -0700859void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860{
Mathias Agopian841cde52012-03-01 15:44:37 -0800861 ATRACE_CALL();
862
Mathias Agopianca4d3602011-05-19 15:38:14 -0700863 Mutex::Autolock _l(mStateLock);
864 const nsecs_t now = systemTime();
865 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866
Mathias Agopianca4d3602011-05-19 15:38:14 -0700867 // Here we're guaranteed that some transaction flags are set
868 // so we can call handleTransactionLocked() unconditionally.
869 // We call getTransactionFlags(), which will also clear the flags,
870 // with mStateLock held to guarantee that mCurrentState won't change
871 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700872
Mathias Agopiane57f2922012-08-09 16:29:12 -0700873 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700874 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700875
Mathias Agopianca4d3602011-05-19 15:38:14 -0700876 mLastTransactionTime = systemTime() - now;
877 mDebugInTransaction = 0;
878 invalidateHwcGeometry();
879 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700880}
881
Mathias Agopian87baae12012-07-31 12:38:26 -0700882void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700883{
884 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 const size_t count = currentLayers.size();
886
887 /*
888 * Traversal of the children
889 * (perform the transaction for each of them if needed)
890 */
891
892 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
893 if (layersNeedTransaction) {
894 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700895 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
897 if (!trFlags) continue;
898
899 const uint32_t flags = layer->doTransaction(0);
900 if (flags & Layer::eVisibleRegion)
901 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 }
903 }
904
905 /*
906 * Perform our own transaction if needed
907 */
908
Mathias Agopiane57f2922012-08-09 16:29:12 -0700909 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700910 // here we take advantage of Vector's copy-on-write semantics to
911 // improve performance by skipping the transaction entirely when
912 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700913 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
914 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700915 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700917 const size_t cc = curr.size();
918 const size_t dc = draw.size();
919
920 // find the displays that were removed
921 // (ie: in drawing state but not in current state)
922 // also handle displays that changed
923 // (ie: displays that are in both lists)
924 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700925 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
926 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700927 // in drawing state but not in current state
928 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700929 mDisplays.removeItem(draw[i].id);
930 } else {
931 ALOGW("trying to remove the main display");
932 }
933 } else {
934 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700935 const DisplayDeviceState& state(curr[j]);
936 if (state.surface != draw[i].surface) {
937 // changing the surface is like destroying and
938 // recreating the DisplayDevice
939
940 sp<SurfaceTextureClient> stc(
941 new SurfaceTextureClient(state.surface));
942
943 sp<DisplayDevice> disp = new DisplayDevice(this,
944 state.id, stc, 0, mEGLConfig);
945
946 disp->setLayerStack(state.layerStack);
947 disp->setOrientation(state.orientation);
948 // TODO: take viewport and frame into account
949 mDisplays.replaceValueFor(state.id, disp);
950 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700951 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700952 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700953 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700954 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700955 if (state.orientation != draw[i].orientation ||
956 state.viewport != draw[i].viewport ||
957 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700958 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
959 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700960 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700961 }
962 }
963 }
964
965 // find displays that were added
966 // (ie: in current state but not in drawing state)
967 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700968 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 // FIXME: we need to pass the surface here
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 const DisplayDeviceState& state(curr[i]);
971 sp<SurfaceTextureClient> stc(
972 new SurfaceTextureClient(state.surface));
973 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
974 stc, 0, mEGLConfig);
975 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700976 }
977 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 }
979
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700980 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
981 // layers have been added
982 mVisibleRegionsDirty = true;
983 }
984
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 // some layers might have been removed, so
986 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700987 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700988 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700990 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700991 const size_t count = previousLayers.size();
992 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700993 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700994 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700995 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700996 // TODO: we could traverse the tree from front to back and
997 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700998 // TODO: we could cache the transformed region
999 Layer::State front(layer->drawingState());
1000 Region visibleReg = front.transform.transform(
1001 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -07001002 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001003 }
1004 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 }
1007
1008 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001009}
1010
1011void SurfaceFlinger::commitTransaction()
1012{
1013 if (!mLayersPendingRemoval.isEmpty()) {
1014 // Notify removed layers now that they can't be drawn from
1015 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1016 mLayersPendingRemoval[i]->onRemoved();
1017 }
1018 mLayersPendingRemoval.clear();
1019 }
1020
1021 mDrawingState = mCurrentState;
1022 mTransationPending = false;
1023 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024}
1025
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001027 const LayerVector& currentLayers, uint32_t layerStack,
1028 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029{
Mathias Agopian841cde52012-03-01 15:44:37 -08001030 ATRACE_CALL();
1031
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 Region aboveOpaqueLayers;
1033 Region aboveCoveredLayers;
1034 Region dirty;
1035
Mathias Agopian87baae12012-07-31 12:38:26 -07001036 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037
1038 size_t i = currentLayers.size();
1039 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001040 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041
1042 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001043 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044
Mathias Agopian87baae12012-07-31 12:38:26 -07001045 // only consider the layers on the given later stack
1046 if (s.layerStack != layerStack)
1047 continue;
1048
Mathias Agopianab028732010-03-16 16:41:46 -07001049 /*
1050 * opaqueRegion: area of a surface that is fully opaque.
1051 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001053
1054 /*
1055 * visibleRegion: area of a surface that is visible on screen
1056 * and not fully transparent. This is essentially the layer's
1057 * footprint minus the opaque regions above it.
1058 * Areas covered by a translucent surface are considered visible.
1059 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001061
1062 /*
1063 * coveredRegion: area of a surface that is covered by all
1064 * visible regions above it (which includes the translucent areas).
1065 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001067
1068
1069 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001070 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001071 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001072 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001074 if (!visibleRegion.isEmpty()) {
1075 // Remove the transparent area from the visible region
1076 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001077 Region transparentRegionScreen;
1078 const Transform tr(s.transform);
1079 if (tr.transformed()) {
1080 if (tr.preserveRects()) {
1081 // transform the transparent region
1082 transparentRegionScreen = tr.transform(s.transparentRegion);
1083 } else {
1084 // transformation too complex, can't do the
1085 // transparent region optimization.
1086 transparentRegionScreen.clear();
1087 }
1088 } else {
1089 transparentRegionScreen = s.transparentRegion;
1090 }
1091 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001092 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093
Mathias Agopianab028732010-03-16 16:41:46 -07001094 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001095 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001096 if (s.alpha==255 && !translucent &&
1097 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1098 // the opaque region is the layer's footprint
1099 opaqueRegion = visibleRegion;
1100 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 }
1102 }
1103
Mathias Agopianab028732010-03-16 16:41:46 -07001104 // Clip the covered region to the visible region
1105 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1106
1107 // Update aboveCoveredLayers for next (lower) layer
1108 aboveCoveredLayers.orSelf(visibleRegion);
1109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 // subtract the opaque region covered by the layers above us
1111 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112
1113 // compute this layer's dirty region
1114 if (layer->contentDirty) {
1115 // we need to invalidate the whole region
1116 dirty = visibleRegion;
1117 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001118 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 layer->contentDirty = false;
1120 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001121 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001122 * the exposed region consists of two components:
1123 * 1) what's VISIBLE now and was COVERED before
1124 * 2) what's EXPOSED now less what was EXPOSED before
1125 *
1126 * note that (1) is conservative, we start with the whole
1127 * visible region but only keep what used to be covered by
1128 * something -- which mean it may have been exposed.
1129 *
1130 * (2) handles areas that were not covered by anything but got
1131 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001132 */
Mathias Agopianab028732010-03-16 16:41:46 -07001133 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001134 const Region oldVisibleRegion = layer->visibleRegion;
1135 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001136 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1137 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 }
1139 dirty.subtractSelf(aboveOpaqueLayers);
1140
1141 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001142 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143
Mathias Agopianab028732010-03-16 16:41:46 -07001144 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 // Store the visible region is screen space
1148 layer->setVisibleRegion(visibleRegion);
1149 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 }
1151
Mathias Agopian87baae12012-07-31 12:38:26 -07001152 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153}
1154
Mathias Agopian87baae12012-07-31 12:38:26 -07001155void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1156 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001157 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001158 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1159 if (hw->getLayerStack() == layerStack) {
1160 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001161 }
1162 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001163}
1164
1165void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001167 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001168 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001170 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001171
Mathias Agopian4fec8732012-06-29 14:12:52 -07001172 bool visibleRegions = false;
1173 const size_t count = currentLayers.size();
1174 sp<LayerBase> const* layers = currentLayers.array();
1175 for (size_t i=0 ; i<count ; i++) {
1176 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001177 const Region dirty(layer->latchBuffer(visibleRegions));
1178 Layer::State s(layer->drawingState());
1179 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001180 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001181
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001182 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183}
1184
Mathias Agopianad456f92011-01-13 17:53:01 -08001185void SurfaceFlinger::invalidateHwcGeometry()
1186{
1187 mHwWorkListDirty = true;
1188}
1189
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001190void SurfaceFlinger::handleRefresh()
1191{
1192 bool needInvalidate = false;
1193 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1194 const size_t count = currentLayers.size();
1195 for (size_t i=0 ; i<count ; i++) {
1196 const sp<LayerBase>& layer(currentLayers[i]);
1197 if (layer->onPreComposition()) {
1198 needInvalidate = true;
1199 }
1200 }
1201 if (needInvalidate) {
1202 signalLayerUpdate();
1203 }
1204}
1205
Mathias Agopian42977342012-08-05 00:40:46 -07001206void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001207 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208{
Mathias Agopian841cde52012-03-01 15:44:37 -08001209 ATRACE_CALL();
1210
Mathias Agopian87baae12012-07-31 12:38:26 -07001211 Region dirtyRegion(inDirtyRegion);
1212
Mathias Agopianb8a55602009-06-26 19:06:36 -07001213 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001214 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215
Glenn Kasten99ed2242011-12-15 09:51:17 -08001216 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001217 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 }
1219
Mathias Agopian42977342012-08-05 00:40:46 -07001220 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001221 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001222 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1223 // takes a rectangle, we must make sure to update that whole
1224 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001225 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001226 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001227 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001228 // We need to redraw the rectangle that will be updated
1229 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001230 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001231 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001232 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001233 } else {
1234 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001235 dirtyRegion.set(hw->bounds());
1236 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 }
1238 }
1239
Mathias Agopian87baae12012-07-31 12:38:26 -07001240 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001242 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1243 const size_t count = currentLayers.size();
1244 for (size_t i=0 ; i<count ; i++) {
1245 currentLayers[i]->onPostComposition();
1246 }
1247
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001248 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001249 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250}
1251
Mathias Agopian42977342012-08-05 00:40:46 -07001252void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001253{
Mathias Agopian86303202012-07-24 22:46:10 -07001254 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001255 int32_t id = hw->getDisplayId();
1256 HWComposer::LayerListIterator cur = hwc.begin(id);
1257 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001258
Mathias Agopian1e260872012-08-08 18:35:12 -07001259 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001260 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001261
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001262 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001263
1264 // set the frame buffer
1265 glMatrixMode(GL_MODELVIEW);
1266 glLoadIdentity();
1267
1268 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001269 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001270 // when using overlays, we assume a fully transparent framebuffer
1271 // NOTE: we could reduce how much we need to clear, for instance
1272 // remove where there are opaque FB layers. however, on some
1273 // GPUs doing a "clean slate" glClear might be more efficient.
1274 // We'll revisit later if needed.
1275 glClearColor(0, 0, 0, 0);
1276 glClear(GL_COLOR_BUFFER_BIT);
1277 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001278 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001279 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001280 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001281 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001282 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001283 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001284 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001285
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001286 /*
1287 * and then, render the layers targeted at the framebuffer
1288 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001289
Mathias Agopian42977342012-08-05 00:40:46 -07001290 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001291 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001292 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001293 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001294 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001295 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001296 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001297 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001298 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001299 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001300 // never clear the very first layer since we're
1301 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001302 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001303 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001304 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001305 continue;
1306 }
1307 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001308 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001309 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001310 if (cur != end) {
1311 ++cur;
1312 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001313 }
1314 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315}
1316
Mathias Agopian42977342012-08-05 00:40:46 -07001317void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001318 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319{
Mathias Agopian42977342012-08-05 00:40:46 -07001320 const uint32_t flags = hw->getFlags();
1321 const int32_t height = hw->getHeight();
1322 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001323 return;
1324 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001325
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001326 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1327 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001328 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001329 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001330 }
1331
Mathias Agopianc492e672011-10-18 14:49:27 -07001332 glDisable(GL_TEXTURE_EXTERNAL_OES);
1333 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335
Mathias Agopian0926f502009-05-04 14:17:04 -07001336 static int toggle = 0;
1337 toggle = 1 - toggle;
1338 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001339 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001340 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001341 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001342 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343
Mathias Agopian87baae12012-07-31 12:38:26 -07001344 Region::const_iterator it = dirtyRegion.begin();
1345 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001346 while (it != end) {
1347 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001349 { r.left, height - r.top },
1350 { r.left, height - r.bottom },
1351 { r.right, height - r.bottom },
1352 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 };
1354 glVertexPointer(2, GL_FLOAT, 0, vertices);
1355 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1356 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001357
Mathias Agopian42977342012-08-05 00:40:46 -07001358 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359
1360 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001361 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362}
1363
Mathias Agopian87baae12012-07-31 12:38:26 -07001364void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001366 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001367 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001368 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001369 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001370
1371 GLfloat vertices[4][2];
1372 glVertexPointer(2, GL_FLOAT, 0, vertices);
1373 Region::const_iterator it = region.begin();
1374 Region::const_iterator const end = region.end();
1375 while (it != end) {
1376 const Rect& r = *it++;
1377 vertices[0][0] = r.left;
1378 vertices[0][1] = r.top;
1379 vertices[1][0] = r.right;
1380 vertices[1][1] = r.top;
1381 vertices[2][0] = r.right;
1382 vertices[2][1] = r.bottom;
1383 vertices[3][0] = r.left;
1384 vertices[3][1] = r.bottom;
1385 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1386 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387}
1388
Mathias Agopian96f08192010-06-02 23:28:45 -07001389ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1390 const sp<LayerBaseClient>& lbc)
1391{
Mathias Agopian96f08192010-06-02 23:28:45 -07001392 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001393 size_t name = client->attachLayer(lbc);
1394
Mathias Agopian96f08192010-06-02 23:28:45 -07001395 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001396 Mutex::Autolock _l(mStateLock);
1397 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001398
Mathias Agopian4f113742011-05-03 16:21:41 -07001399 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001400}
1401
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001402status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403{
1404 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001405 status_t err = purgatorizeLayer_l(layer);
1406 if (err == NO_ERROR)
1407 setTransactionFlags(eTransactionNeeded);
1408 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409}
1410
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001411status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412{
1413 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1414 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001415 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 return NO_ERROR;
1417 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001418 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419}
1420
Mathias Agopian9a112062009-04-17 19:36:26 -07001421status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1422{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001423 // First add the layer to the purgatory list, which makes sure it won't
1424 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001425 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001426 if (err >= 0) {
1427 mLayerPurgatory.add(layerBase);
1428 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001429
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001430 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001431
Mathias Agopian3d579642009-06-04 18:46:21 -07001432 // it's possible that we don't find a layer, because it might
1433 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001434 // from the user because there is a race between Client::destroySurface(),
1435 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001436 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1437}
1438
Mathias Agopiandea20b12011-05-03 17:04:02 -07001439uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1440{
1441 return android_atomic_release_load(&mTransactionFlags);
1442}
1443
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1445{
1446 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1447}
1448
Mathias Agopianbb641242010-05-18 17:06:55 -07001449uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450{
1451 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1452 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001453 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 }
1455 return old;
1456}
1457
Mathias Agopian8b33f032012-07-24 20:43:54 -07001458void SurfaceFlinger::setTransactionState(
1459 const Vector<ComposerState>& state,
1460 const Vector<DisplayState>& displays,
1461 uint32_t flags)
1462{
Mathias Agopian698c0872011-06-28 19:09:31 -07001463 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001464 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001465
1466 size_t count = displays.size();
1467 for (size_t i=0 ; i<count ; i++) {
1468 const DisplayState& s(displays[i]);
1469 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001470 }
1471
Mathias Agopiane57f2922012-08-09 16:29:12 -07001472 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001473 for (size_t i=0 ; i<count ; i++) {
1474 const ComposerState& s(state[i]);
1475 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001476 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001477 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001478
Mathias Agopian386aa982011-11-07 21:58:03 -08001479 if (transactionFlags) {
1480 // this triggers the transaction
1481 setTransactionFlags(transactionFlags);
1482
1483 // if this is a synchronous transaction, wait for it to take effect
1484 // before returning.
1485 if (flags & eSynchronous) {
1486 mTransationPending = true;
1487 }
1488 while (mTransationPending) {
1489 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1490 if (CC_UNLIKELY(err != NO_ERROR)) {
1491 // just in case something goes wrong in SF, return to the
1492 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001493 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001494 mTransationPending = false;
1495 break;
1496 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001497 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 }
1499}
1500
Mathias Agopiane57f2922012-08-09 16:29:12 -07001501uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1502{
1503 uint32_t flags = 0;
1504 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1505 if (disp.id >= 0) {
1506 const uint32_t what = s.what;
1507 if (what & DisplayState::eSurfaceChanged) {
1508 if (disp.surface->asBinder() != s.surface->asBinder()) {
1509 disp.surface = s.surface;
1510 flags |= eDisplayTransactionNeeded;
1511 }
1512 }
1513 if (what & DisplayState::eLayerStackChanged) {
1514 if (disp.layerStack != s.layerStack) {
1515 disp.layerStack = s.layerStack;
1516 flags |= eDisplayTransactionNeeded;
1517 }
1518 }
1519 if (what & DisplayState::eTransformChanged) {
1520 if (disp.orientation != s.orientation) {
1521 disp.orientation = s.orientation;
1522 flags |= eDisplayTransactionNeeded;
1523 }
1524 if (disp.frame != s.frame) {
1525 disp.frame = s.frame;
1526 flags |= eDisplayTransactionNeeded;
1527 }
1528 if (disp.viewport != s.viewport) {
1529 disp.viewport = s.viewport;
1530 flags |= eDisplayTransactionNeeded;
1531 }
1532 }
1533 }
1534 return flags;
1535}
1536
1537uint32_t SurfaceFlinger::setClientStateLocked(
1538 const sp<Client>& client,
1539 const layer_state_t& s)
1540{
1541 uint32_t flags = 0;
1542 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1543 if (layer != 0) {
1544 const uint32_t what = s.what;
1545 if (what & layer_state_t::ePositionChanged) {
1546 if (layer->setPosition(s.x, s.y))
1547 flags |= eTraversalNeeded;
1548 }
1549 if (what & layer_state_t::eLayerChanged) {
1550 // NOTE: index needs to be calculated before we update the state
1551 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1552 if (layer->setLayer(s.z)) {
1553 mCurrentState.layersSortedByZ.removeAt(idx);
1554 mCurrentState.layersSortedByZ.add(layer);
1555 // we need traversal (state changed)
1556 // AND transaction (list changed)
1557 flags |= eTransactionNeeded|eTraversalNeeded;
1558 }
1559 }
1560 if (what & layer_state_t::eSizeChanged) {
1561 if (layer->setSize(s.w, s.h)) {
1562 flags |= eTraversalNeeded;
1563 }
1564 }
1565 if (what & layer_state_t::eAlphaChanged) {
1566 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1567 flags |= eTraversalNeeded;
1568 }
1569 if (what & layer_state_t::eMatrixChanged) {
1570 if (layer->setMatrix(s.matrix))
1571 flags |= eTraversalNeeded;
1572 }
1573 if (what & layer_state_t::eTransparentRegionChanged) {
1574 if (layer->setTransparentRegionHint(s.transparentRegion))
1575 flags |= eTraversalNeeded;
1576 }
1577 if (what & layer_state_t::eVisibilityChanged) {
1578 if (layer->setFlags(s.flags, s.mask))
1579 flags |= eTraversalNeeded;
1580 }
1581 if (what & layer_state_t::eCropChanged) {
1582 if (layer->setCrop(s.crop))
1583 flags |= eTraversalNeeded;
1584 }
1585 if (what & layer_state_t::eLayerStackChanged) {
1586 // NOTE: index needs to be calculated before we update the state
1587 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1588 if (layer->setLayerStack(s.layerStack)) {
1589 mCurrentState.layersSortedByZ.removeAt(idx);
1590 mCurrentState.layersSortedByZ.add(layer);
1591 // we need traversal (state changed)
1592 // AND transaction (list changed)
1593 flags |= eTransactionNeeded|eTraversalNeeded;
1594 }
1595 }
1596 }
1597 return flags;
1598}
1599
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001600sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001601 ISurfaceComposerClient::surface_data_t* params,
1602 const String8& name,
1603 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1605 uint32_t flags)
1606{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001607 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001608 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001609
1610 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001611 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001612 int(w), int(h));
1613 return surfaceHandle;
1614 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001615
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001616 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001617 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1618 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001619 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001621 case ISurfaceComposerClient::eFXSurfaceBlur:
1622 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001623 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001625 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001626 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001627 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 }
1629
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001630 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001631 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001632 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001633 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001635 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001636 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001637 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001638 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001639 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640 }
1641
1642 return surfaceHandle;
1643}
1644
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001645sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001646 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001647 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001648 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649{
1650 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001651 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 case PIXEL_FORMAT_TRANSPARENT:
1653 case PIXEL_FORMAT_TRANSLUCENT:
1654 format = PIXEL_FORMAT_RGBA_8888;
1655 break;
1656 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001657#ifdef NO_RGBX_8888
1658 format = PIXEL_FORMAT_RGB_565;
1659#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001660 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001661#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 break;
1663 }
1664
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001665#ifdef NO_RGBX_8888
1666 if (format == PIXEL_FORMAT_RGBX_8888)
1667 format = PIXEL_FORMAT_RGBA_8888;
1668#endif
1669
Mathias Agopian96f08192010-06-02 23:28:45 -07001670 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001671 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001672 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001673 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001674 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 }
1676 return layer;
1677}
1678
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001679sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001680 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001681 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682{
Mathias Agopian96f08192010-06-02 23:28:45 -07001683 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001684 return layer;
1685}
1686
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001687sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001688 const sp<Client>& client, DisplayID display,
1689 uint32_t w, uint32_t h, uint32_t flags)
1690{
1691 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 return layer;
1693}
1694
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001695status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696{
Mathias Agopian9a112062009-04-17 19:36:26 -07001697 /*
1698 * called by the window manager, when a surface should be marked for
1699 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001700 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001701 * The surface is removed from the current and drawing lists, but placed
1702 * in the purgatory queue, so it's not destroyed right-away (we need
1703 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001704 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001705
Mathias Agopian48d819a2009-09-10 19:41:18 -07001706 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001707 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001708 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001709
Mathias Agopian48d819a2009-09-10 19:41:18 -07001710 if (layer != 0) {
1711 err = purgatorizeLayer_l(layer);
1712 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001713 setTransactionFlags(eTransactionNeeded);
1714 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001715 }
1716 return err;
1717}
1718
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001719status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001720{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001721 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001722 status_t err = NO_ERROR;
1723 sp<LayerBaseClient> l(layer.promote());
1724 if (l != NULL) {
1725 Mutex::Autolock _l(mStateLock);
1726 err = removeLayer_l(l);
1727 if (err == NAME_NOT_FOUND) {
1728 // The surface wasn't in the current list, which means it was
1729 // removed already, which means it is in the purgatory,
1730 // and need to be removed from there.
1731 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001732 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001733 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001734 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001735 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001736 "error removing layer=%p (%s)", l.get(), strerror(-err));
1737 }
1738 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739}
1740
Mathias Agopianb60314a2012-04-10 22:09:54 -07001741// ---------------------------------------------------------------------------
1742
1743void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001744 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001745 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001746 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001747 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001748 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001749 mVisibleRegionsDirty = true;
1750 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751}
1752
Mathias Agopianb60314a2012-04-10 22:09:54 -07001753void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001754 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001755 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1756 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001757 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001758 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001759 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001760 // from this point on, SF will stop drawing
1761 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762}
1763
Colin Cross8e533062012-06-07 13:17:52 -07001764void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001765 class MessageScreenAcquired : public MessageBase {
1766 SurfaceFlinger* flinger;
1767 public:
1768 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1769 virtual bool handler() {
1770 flinger->onScreenAcquired();
1771 return true;
1772 }
1773 };
1774 sp<MessageBase> msg = new MessageScreenAcquired(this);
1775 postMessageSync(msg);
1776}
1777
Colin Cross8e533062012-06-07 13:17:52 -07001778void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001779 class MessageScreenReleased : public MessageBase {
1780 SurfaceFlinger* flinger;
1781 public:
1782 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1783 virtual bool handler() {
1784 flinger->onScreenReleased();
1785 return true;
1786 }
1787 };
1788 sp<MessageBase> msg = new MessageScreenReleased(this);
1789 postMessageSync(msg);
1790}
1791
1792// ---------------------------------------------------------------------------
1793
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1795{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001796 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797 char buffer[SIZE];
1798 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001799
1800 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 snprintf(buffer, SIZE, "Permission Denial: "
1802 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1803 IPCThreadState::self()->getCallingPid(),
1804 IPCThreadState::self()->getCallingUid());
1805 result.append(buffer);
1806 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001807 // Try to get the main lock, but don't insist if we can't
1808 // (this would indicate SF is stuck, but we want to be able to
1809 // print something in dumpsys).
1810 int retry = 3;
1811 while (mStateLock.tryLock()<0 && --retry>=0) {
1812 usleep(1000000);
1813 }
1814 const bool locked(retry >= 0);
1815 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001816 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001817 "SurfaceFlinger appears to be unresponsive, "
1818 "dumping anyways (no locks held)\n");
1819 result.append(buffer);
1820 }
1821
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001822 bool dumpAll = true;
1823 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001824 size_t numArgs = args.size();
1825 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001826 if ((index < numArgs) &&
1827 (args[index] == String16("--list"))) {
1828 index++;
1829 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001830 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001831 }
1832
1833 if ((index < numArgs) &&
1834 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001835 index++;
1836 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001837 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001838 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001839
1840 if ((index < numArgs) &&
1841 (args[index] == String16("--latency-clear"))) {
1842 index++;
1843 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001844 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001845 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001847
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001848 if (dumpAll) {
1849 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001850 }
1851
Mathias Agopian9795c422009-08-26 16:36:26 -07001852 if (locked) {
1853 mStateLock.unlock();
1854 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855 }
1856 write(fd, result.string(), result.size());
1857 return NO_ERROR;
1858}
1859
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001860void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1861 String8& result, char* buffer, size_t SIZE) const
1862{
1863 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1864 const size_t count = currentLayers.size();
1865 for (size_t i=0 ; i<count ; i++) {
1866 const sp<LayerBase>& layer(currentLayers[i]);
1867 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1868 result.append(buffer);
1869 }
1870}
1871
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001872void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1873 String8& result, char* buffer, size_t SIZE) const
1874{
1875 String8 name;
1876 if (index < args.size()) {
1877 name = String8(args[index]);
1878 index++;
1879 }
1880
1881 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1882 const size_t count = currentLayers.size();
1883 for (size_t i=0 ; i<count ; i++) {
1884 const sp<LayerBase>& layer(currentLayers[i]);
1885 if (name.isEmpty()) {
1886 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1887 result.append(buffer);
1888 }
1889 if (name.isEmpty() || (name == layer->getName())) {
1890 layer->dumpStats(result, buffer, SIZE);
1891 }
1892 }
1893}
1894
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001895void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1896 String8& result, char* buffer, size_t SIZE) const
1897{
1898 String8 name;
1899 if (index < args.size()) {
1900 name = String8(args[index]);
1901 index++;
1902 }
1903
1904 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1905 const size_t count = currentLayers.size();
1906 for (size_t i=0 ; i<count ; i++) {
1907 const sp<LayerBase>& layer(currentLayers[i]);
1908 if (name.isEmpty() || (name == layer->getName())) {
1909 layer->clearStats();
1910 }
1911 }
1912}
1913
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001914void SurfaceFlinger::dumpAllLocked(
1915 String8& result, char* buffer, size_t SIZE) const
1916{
1917 // figure out if we're stuck somewhere
1918 const nsecs_t now = systemTime();
1919 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1920 const nsecs_t inTransaction(mDebugInTransaction);
1921 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1922 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1923
1924 /*
1925 * Dump the visible layer list
1926 */
1927 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1928 const size_t count = currentLayers.size();
1929 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1930 result.append(buffer);
1931 for (size_t i=0 ; i<count ; i++) {
1932 const sp<LayerBase>& layer(currentLayers[i]);
1933 layer->dump(result, buffer, SIZE);
1934 }
1935
1936 /*
1937 * Dump the layers in the purgatory
1938 */
1939
1940 const size_t purgatorySize = mLayerPurgatory.size();
1941 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1942 result.append(buffer);
1943 for (size_t i=0 ; i<purgatorySize ; i++) {
1944 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1945 layer->shortDump(result, buffer, SIZE);
1946 }
1947
1948 /*
1949 * Dump SurfaceFlinger global state
1950 */
1951
1952 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1953 result.append(buffer);
1954
Mathias Agopian888c8222012-08-04 21:10:38 -07001955 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001956 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001957 const GLExtensions& extensions(GLExtensions::getInstance());
1958 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1959 extensions.getVendor(),
1960 extensions.getRenderer(),
1961 extensions.getVersion());
1962 result.append(buffer);
1963
1964 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001965 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001966 result.append(buffer);
1967
1968 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1969 result.append(buffer);
1970
Mathias Agopian42977342012-08-05 00:40:46 -07001971 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001972 snprintf(buffer, SIZE,
1973 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001974 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001975 result.append(buffer);
1976 snprintf(buffer, SIZE,
1977 " last eglSwapBuffers() time: %f us\n"
1978 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001979 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001980 " refresh-rate : %f fps\n"
1981 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001982 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001983 mLastSwapBufferTime/1000.0,
1984 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001985 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001986 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001987 hwc.getDpiX(),
1988 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001989 result.append(buffer);
1990
1991 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1992 inSwapBuffersDuration/1000.0);
1993 result.append(buffer);
1994
1995 snprintf(buffer, SIZE, " transaction time: %f us\n",
1996 inTransactionDuration/1000.0);
1997 result.append(buffer);
1998
1999 /*
2000 * VSYNC state
2001 */
2002 mEventThread->dump(result, buffer, SIZE);
2003
2004 /*
2005 * Dump HWComposer state
2006 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002007 snprintf(buffer, SIZE, "h/w composer state:\n");
2008 result.append(buffer);
2009 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2010 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2011 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2012 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002013 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002014
2015 /*
2016 * Dump gralloc state
2017 */
2018 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2019 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002020 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002021}
2022
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023status_t SurfaceFlinger::onTransact(
2024 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2025{
2026 switch (code) {
2027 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002028 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002029 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002030 case BLANK:
2031 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002032 {
2033 // codes that require permission check
2034 IPCThreadState* ipc = IPCThreadState::self();
2035 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002036 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002037 if ((uid != AID_GRAPHICS) &&
2038 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002039 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002040 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2041 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002042 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002043 break;
2044 }
2045 case CAPTURE_SCREEN:
2046 {
2047 // codes that require permission check
2048 IPCThreadState* ipc = IPCThreadState::self();
2049 const int pid = ipc->getCallingPid();
2050 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002051 if ((uid != AID_GRAPHICS) &&
2052 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002053 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002054 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2055 return PERMISSION_DENIED;
2056 }
2057 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058 }
2059 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002061 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2062 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002063 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002064 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002065 IPCThreadState* ipc = IPCThreadState::self();
2066 const int pid = ipc->getCallingPid();
2067 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002068 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002069 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002070 return PERMISSION_DENIED;
2071 }
2072 int n;
2073 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002074 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002075 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076 return NO_ERROR;
2077 case 1002: // SHOW_UPDATES
2078 n = data.readInt32();
2079 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002080 invalidateHwcGeometry();
2081 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002083 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002084 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002085 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002087 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002088 setTransactionFlags(
2089 eTransactionNeeded|
2090 eDisplayTransactionNeeded|
2091 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002092 return NO_ERROR;
2093 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002094 case 1006:{ // send empty update
2095 signalRefresh();
2096 return NO_ERROR;
2097 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002098 case 1008: // toggle use of hw composer
2099 n = data.readInt32();
2100 mDebugDisableHWC = n ? 1 : 0;
2101 invalidateHwcGeometry();
2102 repaintEverything();
2103 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002104 case 1009: // toggle use of transform hint
2105 n = data.readInt32();
2106 mDebugDisableTransformHint = n ? 1 : 0;
2107 invalidateHwcGeometry();
2108 repaintEverything();
2109 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002110 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002111 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002112 reply->writeInt32(0);
2113 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002114 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002115 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002116 return NO_ERROR;
2117 case 1013: {
2118 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002119 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2120 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121 }
2122 return NO_ERROR;
2123 }
2124 }
2125 return err;
2126}
2127
Mathias Agopian53331da2011-08-22 21:44:41 -07002128void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002129 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002130 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002131}
2132
Mathias Agopian59119e62010-10-11 12:37:43 -07002133// ---------------------------------------------------------------------------
2134
Mathias Agopian118d0242011-10-13 16:02:48 -07002135status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2136 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2137{
2138 Mutex::Autolock _l(mStateLock);
2139 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2140}
2141
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002142status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2143 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002144{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002145 ATRACE_CALL();
2146
Mathias Agopian59119e62010-10-11 12:37:43 -07002147 if (!GLExtensions::getInstance().haveFramebufferObject())
2148 return INVALID_OPERATION;
2149
2150 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002151 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2152 const uint32_t hw_w = hw->getWidth();
2153 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002154 GLfloat u = 1;
2155 GLfloat v = 1;
2156
2157 // make sure to clear all GL error flags
2158 while ( glGetError() != GL_NO_ERROR ) ;
2159
2160 // create a FBO
2161 GLuint name, tname;
2162 glGenTextures(1, &tname);
2163 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002164 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2165 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002166 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2167 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002168 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002169 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002170 GLint tw = (2 << (31 - clz(hw_w)));
2171 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2173 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002174 u = GLfloat(hw_w) / tw;
2175 v = GLfloat(hw_h) / th;
2176 }
2177 glGenFramebuffersOES(1, &name);
2178 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2180 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002181
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002182 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002183 glDisable(GL_TEXTURE_EXTERNAL_OES);
2184 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002185 glClearColor(0,0,0,1);
2186 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002187 glMatrixMode(GL_MODELVIEW);
2188 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002189 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002190 const size_t count = layers.size();
2191 for (size_t i=0 ; i<count ; ++i) {
2192 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002193 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002194 }
2195
Mathias Agopian42977342012-08-05 00:40:46 -07002196 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002197
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 // back to main framebuffer
2199 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 glDeleteFramebuffersOES(1, &name);
2201
2202 *textureName = tname;
2203 *uOut = u;
2204 *vOut = v;
2205 return NO_ERROR;
2206}
2207
2208// ---------------------------------------------------------------------------
2209
Mathias Agopian74c40c02010-09-29 13:02:36 -07002210status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2211 sp<IMemoryHeap>* heap,
2212 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002213 uint32_t sw, uint32_t sh,
2214 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002215{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002216 ATRACE_CALL();
2217
Mathias Agopian74c40c02010-09-29 13:02:36 -07002218 status_t result = PERMISSION_DENIED;
2219
2220 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002221 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002222 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002223 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002224 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002225
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002226 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002227 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002228 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002229
2230 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002231 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2232 const uint32_t hw_w = hw->getWidth();
2233 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002235 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002236 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002237 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002238 return PERMISSION_DENIED;
2239 }
2240
2241 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002242 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002244 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002245
2246 sw = (!sw) ? hw_w : sw;
2247 sh = (!sh) ? hw_h : sh;
2248 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002249 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002250
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002251// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2252// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002253
Mathias Agopian74c40c02010-09-29 13:02:36 -07002254 // make sure to clear all GL error flags
2255 while ( glGetError() != GL_NO_ERROR ) ;
2256
2257 // create a FBO
2258 GLuint name, tname;
2259 glGenRenderbuffersOES(1, &tname);
2260 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2261 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002262
Mathias Agopian74c40c02010-09-29 13:02:36 -07002263 glGenFramebuffersOES(1, &name);
2264 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2265 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2266 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2267
2268 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002269
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2271
2272 // invert everything, b/c glReadPixel() below will invert the FB
2273 glViewport(0, 0, sw, sh);
2274 glMatrixMode(GL_PROJECTION);
2275 glPushMatrix();
2276 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002277 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278 glMatrixMode(GL_MODELVIEW);
2279
2280 // redraw the screen entirely...
2281 glClearColor(0,0,0,1);
2282 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002283
Jamie Gennis9575f602011-10-07 14:51:16 -07002284 const LayerVector& layers(mDrawingState.layersSortedByZ);
2285 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002286 for (size_t i=0 ; i<count ; ++i) {
2287 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002288 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002289 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002290 const uint32_t z = layer->drawingState().z;
2291 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002292 if (filtering) layer->setFiltering(true);
2293 layer->draw(hw);
2294 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002295 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002296 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002297 }
2298
Mathias Agopian74c40c02010-09-29 13:02:36 -07002299 // check for errors and return screen capture
2300 if (glGetError() != GL_NO_ERROR) {
2301 // error while rendering
2302 result = INVALID_OPERATION;
2303 } else {
2304 // allocate shared memory large enough to hold the
2305 // screen capture
2306 sp<MemoryHeapBase> base(
2307 new MemoryHeapBase(size, 0, "screen-capture") );
2308 void* const ptr = base->getBase();
2309 if (ptr) {
2310 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002311 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002312 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2313 if (glGetError() == GL_NO_ERROR) {
2314 *heap = base;
2315 *w = sw;
2316 *h = sh;
2317 *f = PIXEL_FORMAT_RGBA_8888;
2318 result = NO_ERROR;
2319 }
2320 } else {
2321 result = NO_MEMORY;
2322 }
2323 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324 glViewport(0, 0, hw_w, hw_h);
2325 glMatrixMode(GL_PROJECTION);
2326 glPopMatrix();
2327 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 } else {
2329 result = BAD_VALUE;
2330 }
2331
2332 // release FBO resources
2333 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2334 glDeleteRenderbuffersOES(1, &tname);
2335 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002336
Mathias Agopian42977342012-08-05 00:40:46 -07002337 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002338
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002339// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002340
Mathias Agopian74c40c02010-09-29 13:02:36 -07002341 return result;
2342}
2343
2344
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002345status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2346 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002347 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002348 uint32_t sw, uint32_t sh,
2349 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002350{
2351 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002352 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002353 return BAD_VALUE;
2354
2355 if (!GLExtensions::getInstance().haveFramebufferObject())
2356 return INVALID_OPERATION;
2357
2358 class MessageCaptureScreen : public MessageBase {
2359 SurfaceFlinger* flinger;
2360 DisplayID dpy;
2361 sp<IMemoryHeap>* heap;
2362 uint32_t* w;
2363 uint32_t* h;
2364 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002365 uint32_t sw;
2366 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002367 uint32_t minLayerZ;
2368 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002369 status_t result;
2370 public:
2371 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002372 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002373 uint32_t sw, uint32_t sh,
2374 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002375 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002376 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2377 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2378 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002379 {
2380 }
2381 status_t getResult() const {
2382 return result;
2383 }
2384 virtual bool handler() {
2385 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002386 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002387 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002388 return true;
2389 }
2390 };
2391
2392 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002393 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002394 status_t res = postMessageSync(msg);
2395 if (res == NO_ERROR) {
2396 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2397 }
2398 return res;
2399}
2400
2401// ---------------------------------------------------------------------------
2402
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002403SurfaceFlinger::LayerVector::LayerVector() {
2404}
2405
2406SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2407 : SortedVector<sp<LayerBase> >(rhs) {
2408}
2409
2410int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2411 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002412{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002413 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002414 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2415 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002416
2417 uint32_t ls = l->currentState().layerStack;
2418 uint32_t rs = r->currentState().layerStack;
2419 if (ls != rs)
2420 return ls - rs;
2421
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002422 uint32_t lz = l->currentState().z;
2423 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002424 if (lz != rz)
2425 return lz - rz;
2426
2427 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002428}
2429
2430// ---------------------------------------------------------------------------
2431
Mathias Agopiane57f2922012-08-09 16:29:12 -07002432SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2433}
2434
2435SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2436 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002437}
2438
2439// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440
Jamie Gennis9a78c902011-01-12 18:30:40 -08002441GraphicBufferAlloc::GraphicBufferAlloc() {}
2442
2443GraphicBufferAlloc::~GraphicBufferAlloc() {}
2444
2445sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002446 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002447 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2448 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002449 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002450 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002451 if (err == NO_MEMORY) {
2452 GraphicBuffer::dumpAllocationsToSystemLog();
2453 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002454 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002455 "failed (%s), handle=%p",
2456 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002457 return 0;
2458 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002459 return graphicBuffer;
2460}
2461
Jamie Gennis9a78c902011-01-12 18:30:40 -08002462// ---------------------------------------------------------------------------
2463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464}; // namespace android