blob: ca3c216418c84d28f0ab6db8b4fe512ac01971f9 [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();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749}
1750
Mathias Agopianb60314a2012-04-10 22:09:54 -07001751void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001752 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001753 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1754 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001755 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001756 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001757 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001758 // from this point on, SF will stop drawing
1759 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760}
1761
Colin Cross8e533062012-06-07 13:17:52 -07001762void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001763 class MessageScreenAcquired : public MessageBase {
1764 SurfaceFlinger* flinger;
1765 public:
1766 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1767 virtual bool handler() {
1768 flinger->onScreenAcquired();
1769 return true;
1770 }
1771 };
1772 sp<MessageBase> msg = new MessageScreenAcquired(this);
1773 postMessageSync(msg);
1774}
1775
Colin Cross8e533062012-06-07 13:17:52 -07001776void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001777 class MessageScreenReleased : public MessageBase {
1778 SurfaceFlinger* flinger;
1779 public:
1780 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1781 virtual bool handler() {
1782 flinger->onScreenReleased();
1783 return true;
1784 }
1785 };
1786 sp<MessageBase> msg = new MessageScreenReleased(this);
1787 postMessageSync(msg);
1788}
1789
1790// ---------------------------------------------------------------------------
1791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001792status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1793{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001794 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001795 char buffer[SIZE];
1796 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001797
1798 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 snprintf(buffer, SIZE, "Permission Denial: "
1800 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1801 IPCThreadState::self()->getCallingPid(),
1802 IPCThreadState::self()->getCallingUid());
1803 result.append(buffer);
1804 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001805 // Try to get the main lock, but don't insist if we can't
1806 // (this would indicate SF is stuck, but we want to be able to
1807 // print something in dumpsys).
1808 int retry = 3;
1809 while (mStateLock.tryLock()<0 && --retry>=0) {
1810 usleep(1000000);
1811 }
1812 const bool locked(retry >= 0);
1813 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001814 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001815 "SurfaceFlinger appears to be unresponsive, "
1816 "dumping anyways (no locks held)\n");
1817 result.append(buffer);
1818 }
1819
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001820 bool dumpAll = true;
1821 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001822 size_t numArgs = args.size();
1823 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001824 if ((index < numArgs) &&
1825 (args[index] == String16("--list"))) {
1826 index++;
1827 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001828 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001829 }
1830
1831 if ((index < numArgs) &&
1832 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001833 index++;
1834 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001835 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001836 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001837
1838 if ((index < numArgs) &&
1839 (args[index] == String16("--latency-clear"))) {
1840 index++;
1841 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001842 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001843 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001845
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001846 if (dumpAll) {
1847 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001848 }
1849
Mathias Agopian9795c422009-08-26 16:36:26 -07001850 if (locked) {
1851 mStateLock.unlock();
1852 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853 }
1854 write(fd, result.string(), result.size());
1855 return NO_ERROR;
1856}
1857
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001858void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1859 String8& result, char* buffer, size_t SIZE) const
1860{
1861 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1862 const size_t count = currentLayers.size();
1863 for (size_t i=0 ; i<count ; i++) {
1864 const sp<LayerBase>& layer(currentLayers[i]);
1865 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1866 result.append(buffer);
1867 }
1868}
1869
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001870void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1871 String8& result, char* buffer, size_t SIZE) const
1872{
1873 String8 name;
1874 if (index < args.size()) {
1875 name = String8(args[index]);
1876 index++;
1877 }
1878
1879 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1880 const size_t count = currentLayers.size();
1881 for (size_t i=0 ; i<count ; i++) {
1882 const sp<LayerBase>& layer(currentLayers[i]);
1883 if (name.isEmpty()) {
1884 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1885 result.append(buffer);
1886 }
1887 if (name.isEmpty() || (name == layer->getName())) {
1888 layer->dumpStats(result, buffer, SIZE);
1889 }
1890 }
1891}
1892
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001893void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1894 String8& result, char* buffer, size_t SIZE) const
1895{
1896 String8 name;
1897 if (index < args.size()) {
1898 name = String8(args[index]);
1899 index++;
1900 }
1901
1902 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1903 const size_t count = currentLayers.size();
1904 for (size_t i=0 ; i<count ; i++) {
1905 const sp<LayerBase>& layer(currentLayers[i]);
1906 if (name.isEmpty() || (name == layer->getName())) {
1907 layer->clearStats();
1908 }
1909 }
1910}
1911
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001912void SurfaceFlinger::dumpAllLocked(
1913 String8& result, char* buffer, size_t SIZE) const
1914{
1915 // figure out if we're stuck somewhere
1916 const nsecs_t now = systemTime();
1917 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1918 const nsecs_t inTransaction(mDebugInTransaction);
1919 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1920 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1921
1922 /*
1923 * Dump the visible layer list
1924 */
1925 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1926 const size_t count = currentLayers.size();
1927 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1928 result.append(buffer);
1929 for (size_t i=0 ; i<count ; i++) {
1930 const sp<LayerBase>& layer(currentLayers[i]);
1931 layer->dump(result, buffer, SIZE);
1932 }
1933
1934 /*
1935 * Dump the layers in the purgatory
1936 */
1937
1938 const size_t purgatorySize = mLayerPurgatory.size();
1939 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1940 result.append(buffer);
1941 for (size_t i=0 ; i<purgatorySize ; i++) {
1942 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1943 layer->shortDump(result, buffer, SIZE);
1944 }
1945
1946 /*
1947 * Dump SurfaceFlinger global state
1948 */
1949
1950 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1951 result.append(buffer);
1952
Mathias Agopian888c8222012-08-04 21:10:38 -07001953 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001954 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001955 const GLExtensions& extensions(GLExtensions::getInstance());
1956 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1957 extensions.getVendor(),
1958 extensions.getRenderer(),
1959 extensions.getVersion());
1960 result.append(buffer);
1961
1962 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001963 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001964 result.append(buffer);
1965
1966 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1967 result.append(buffer);
1968
Mathias Agopian42977342012-08-05 00:40:46 -07001969 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001970 snprintf(buffer, SIZE,
1971 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001972 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001973 result.append(buffer);
1974 snprintf(buffer, SIZE,
1975 " last eglSwapBuffers() time: %f us\n"
1976 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001977 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001978 " refresh-rate : %f fps\n"
1979 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001980 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001981 mLastSwapBufferTime/1000.0,
1982 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001983 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001984 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001985 hwc.getDpiX(),
1986 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001987 result.append(buffer);
1988
1989 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1990 inSwapBuffersDuration/1000.0);
1991 result.append(buffer);
1992
1993 snprintf(buffer, SIZE, " transaction time: %f us\n",
1994 inTransactionDuration/1000.0);
1995 result.append(buffer);
1996
1997 /*
1998 * VSYNC state
1999 */
2000 mEventThread->dump(result, buffer, SIZE);
2001
2002 /*
2003 * Dump HWComposer state
2004 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002005 snprintf(buffer, SIZE, "h/w composer state:\n");
2006 result.append(buffer);
2007 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2008 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2009 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2010 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002011 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002012
2013 /*
2014 * Dump gralloc state
2015 */
2016 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2017 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002018 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002019}
2020
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002021status_t SurfaceFlinger::onTransact(
2022 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2023{
2024 switch (code) {
2025 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002026 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002027 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002028 case BLANK:
2029 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002030 {
2031 // codes that require permission check
2032 IPCThreadState* ipc = IPCThreadState::self();
2033 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002034 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002035 if ((uid != AID_GRAPHICS) &&
2036 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002037 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002038 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2039 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002040 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002041 break;
2042 }
2043 case CAPTURE_SCREEN:
2044 {
2045 // codes that require permission check
2046 IPCThreadState* ipc = IPCThreadState::self();
2047 const int pid = ipc->getCallingPid();
2048 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002049 if ((uid != AID_GRAPHICS) &&
2050 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002051 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002052 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2053 return PERMISSION_DENIED;
2054 }
2055 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002056 }
2057 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002059 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2060 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002061 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002062 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002063 IPCThreadState* ipc = IPCThreadState::self();
2064 const int pid = ipc->getCallingPid();
2065 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002066 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002067 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068 return PERMISSION_DENIED;
2069 }
2070 int n;
2071 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002072 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002073 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074 return NO_ERROR;
2075 case 1002: // SHOW_UPDATES
2076 n = data.readInt32();
2077 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002078 invalidateHwcGeometry();
2079 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002081 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002082 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002083 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002085 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002086 setTransactionFlags(
2087 eTransactionNeeded|
2088 eDisplayTransactionNeeded|
2089 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002090 return NO_ERROR;
2091 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002092 case 1006:{ // send empty update
2093 signalRefresh();
2094 return NO_ERROR;
2095 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002096 case 1008: // toggle use of hw composer
2097 n = data.readInt32();
2098 mDebugDisableHWC = n ? 1 : 0;
2099 invalidateHwcGeometry();
2100 repaintEverything();
2101 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002102 case 1009: // toggle use of transform hint
2103 n = data.readInt32();
2104 mDebugDisableTransformHint = n ? 1 : 0;
2105 invalidateHwcGeometry();
2106 repaintEverything();
2107 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002108 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002109 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002110 reply->writeInt32(0);
2111 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002112 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002113 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114 return NO_ERROR;
2115 case 1013: {
2116 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002117 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2118 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119 }
2120 return NO_ERROR;
2121 }
2122 }
2123 return err;
2124}
2125
Mathias Agopian53331da2011-08-22 21:44:41 -07002126void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002127 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002128 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002129}
2130
Mathias Agopian59119e62010-10-11 12:37:43 -07002131// ---------------------------------------------------------------------------
2132
Mathias Agopian118d0242011-10-13 16:02:48 -07002133status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2134 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2135{
2136 Mutex::Autolock _l(mStateLock);
2137 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2138}
2139
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002140status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2141 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002142{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002143 ATRACE_CALL();
2144
Mathias Agopian59119e62010-10-11 12:37:43 -07002145 if (!GLExtensions::getInstance().haveFramebufferObject())
2146 return INVALID_OPERATION;
2147
2148 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002149 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2150 const uint32_t hw_w = hw->getWidth();
2151 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002152 GLfloat u = 1;
2153 GLfloat v = 1;
2154
2155 // make sure to clear all GL error flags
2156 while ( glGetError() != GL_NO_ERROR ) ;
2157
2158 // create a FBO
2159 GLuint name, tname;
2160 glGenTextures(1, &tname);
2161 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002162 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2163 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002164 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2165 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002166 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002167 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002168 GLint tw = (2 << (31 - clz(hw_w)));
2169 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2171 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002172 u = GLfloat(hw_w) / tw;
2173 v = GLfloat(hw_h) / th;
2174 }
2175 glGenFramebuffersOES(1, &name);
2176 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002177 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2178 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002179
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002180 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002181 glDisable(GL_TEXTURE_EXTERNAL_OES);
2182 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002183 glClearColor(0,0,0,1);
2184 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002185 glMatrixMode(GL_MODELVIEW);
2186 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002187 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 const size_t count = layers.size();
2189 for (size_t i=0 ; i<count ; ++i) {
2190 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002191 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002192 }
2193
Mathias Agopian42977342012-08-05 00:40:46 -07002194 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002195
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002196 // back to main framebuffer
2197 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 glDeleteFramebuffersOES(1, &name);
2199
2200 *textureName = tname;
2201 *uOut = u;
2202 *vOut = v;
2203 return NO_ERROR;
2204}
2205
2206// ---------------------------------------------------------------------------
2207
Mathias Agopian74c40c02010-09-29 13:02:36 -07002208status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2209 sp<IMemoryHeap>* heap,
2210 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002211 uint32_t sw, uint32_t sh,
2212 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002213{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002214 ATRACE_CALL();
2215
Mathias Agopian74c40c02010-09-29 13:02:36 -07002216 status_t result = PERMISSION_DENIED;
2217
2218 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002219 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002220 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002221 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002222 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002223
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002224 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002225 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002226 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002227
2228 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002229 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2230 const uint32_t hw_w = hw->getWidth();
2231 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002232
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002233 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002234 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002235 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002236 return PERMISSION_DENIED;
2237 }
2238
2239 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002240 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002242 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243
2244 sw = (!sw) ? hw_w : sw;
2245 sh = (!sh) ? hw_h : sh;
2246 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002247 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002248
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002249// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2250// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002251
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252 // make sure to clear all GL error flags
2253 while ( glGetError() != GL_NO_ERROR ) ;
2254
2255 // create a FBO
2256 GLuint name, tname;
2257 glGenRenderbuffersOES(1, &tname);
2258 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2259 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002260
Mathias Agopian74c40c02010-09-29 13:02:36 -07002261 glGenFramebuffersOES(1, &name);
2262 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2263 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2264 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2265
2266 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002267
Mathias Agopian74c40c02010-09-29 13:02:36 -07002268 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2269
2270 // invert everything, b/c glReadPixel() below will invert the FB
2271 glViewport(0, 0, sw, sh);
2272 glMatrixMode(GL_PROJECTION);
2273 glPushMatrix();
2274 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002275 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002276 glMatrixMode(GL_MODELVIEW);
2277
2278 // redraw the screen entirely...
2279 glClearColor(0,0,0,1);
2280 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002281
Jamie Gennis9575f602011-10-07 14:51:16 -07002282 const LayerVector& layers(mDrawingState.layersSortedByZ);
2283 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002284 for (size_t i=0 ; i<count ; ++i) {
2285 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002286 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002287 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002288 const uint32_t z = layer->drawingState().z;
2289 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002290 if (filtering) layer->setFiltering(true);
2291 layer->draw(hw);
2292 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002293 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002294 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002295 }
2296
Mathias Agopian74c40c02010-09-29 13:02:36 -07002297 // check for errors and return screen capture
2298 if (glGetError() != GL_NO_ERROR) {
2299 // error while rendering
2300 result = INVALID_OPERATION;
2301 } else {
2302 // allocate shared memory large enough to hold the
2303 // screen capture
2304 sp<MemoryHeapBase> base(
2305 new MemoryHeapBase(size, 0, "screen-capture") );
2306 void* const ptr = base->getBase();
2307 if (ptr) {
2308 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002309 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002310 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2311 if (glGetError() == GL_NO_ERROR) {
2312 *heap = base;
2313 *w = sw;
2314 *h = sh;
2315 *f = PIXEL_FORMAT_RGBA_8888;
2316 result = NO_ERROR;
2317 }
2318 } else {
2319 result = NO_MEMORY;
2320 }
2321 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 glViewport(0, 0, hw_w, hw_h);
2323 glMatrixMode(GL_PROJECTION);
2324 glPopMatrix();
2325 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002326 } else {
2327 result = BAD_VALUE;
2328 }
2329
2330 // release FBO resources
2331 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2332 glDeleteRenderbuffersOES(1, &tname);
2333 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002334
Mathias Agopian42977342012-08-05 00:40:46 -07002335 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002336
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002337// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002338
Mathias Agopian74c40c02010-09-29 13:02:36 -07002339 return result;
2340}
2341
2342
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002343status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2344 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002345 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002346 uint32_t sw, uint32_t sh,
2347 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002348{
2349 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002350 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002351 return BAD_VALUE;
2352
2353 if (!GLExtensions::getInstance().haveFramebufferObject())
2354 return INVALID_OPERATION;
2355
2356 class MessageCaptureScreen : public MessageBase {
2357 SurfaceFlinger* flinger;
2358 DisplayID dpy;
2359 sp<IMemoryHeap>* heap;
2360 uint32_t* w;
2361 uint32_t* h;
2362 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002363 uint32_t sw;
2364 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002365 uint32_t minLayerZ;
2366 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002367 status_t result;
2368 public:
2369 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002370 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002371 uint32_t sw, uint32_t sh,
2372 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002373 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002374 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2375 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2376 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002377 {
2378 }
2379 status_t getResult() const {
2380 return result;
2381 }
2382 virtual bool handler() {
2383 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002385 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002386 return true;
2387 }
2388 };
2389
2390 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002391 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002392 status_t res = postMessageSync(msg);
2393 if (res == NO_ERROR) {
2394 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2395 }
2396 return res;
2397}
2398
2399// ---------------------------------------------------------------------------
2400
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002401SurfaceFlinger::LayerVector::LayerVector() {
2402}
2403
2404SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2405 : SortedVector<sp<LayerBase> >(rhs) {
2406}
2407
2408int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2409 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002410{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002411 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002412 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2413 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002414
2415 uint32_t ls = l->currentState().layerStack;
2416 uint32_t rs = r->currentState().layerStack;
2417 if (ls != rs)
2418 return ls - rs;
2419
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002420 uint32_t lz = l->currentState().z;
2421 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002422 if (lz != rz)
2423 return lz - rz;
2424
2425 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002426}
2427
2428// ---------------------------------------------------------------------------
2429
Mathias Agopiane57f2922012-08-09 16:29:12 -07002430SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2431}
2432
2433SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2434 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002435}
2436
2437// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002438
Jamie Gennis9a78c902011-01-12 18:30:40 -08002439GraphicBufferAlloc::GraphicBufferAlloc() {}
2440
2441GraphicBufferAlloc::~GraphicBufferAlloc() {}
2442
2443sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002444 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002445 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2446 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002447 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002448 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002449 if (err == NO_MEMORY) {
2450 GraphicBuffer::dumpAllocationsToSystemLog();
2451 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002452 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002453 "failed (%s), handle=%p",
2454 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002455 return 0;
2456 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002457 return graphicBuffer;
2458}
2459
Jamie Gennis9a78c902011-01-12 18:30:40 -08002460// ---------------------------------------------------------------------------
2461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002462}; // namespace android