blob: 97b02ce6006f0c5d4a877e4d287f594ff2629164 [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;
Jesse Hallea599df2012-08-13 14:47:01 -0700145 d.what = DisplayState::eTransformChanged;
146 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
Mathias Agopian3165cc22012-08-08 19:42:09 -0700147 d.orientation = DisplayState::eOrientationDefault;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700148 displays.add(d);
149 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150
151 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700152 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800153}
154
Mathias Agopian7e27f052010-05-28 14:22:23 -0700155sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Mathias Agopian96f08192010-06-02 23:28:45 -0700157 sp<ISurfaceComposerClient> bclient;
158 sp<Client> client(new Client(this));
159 status_t err = client->initCheck();
160 if (err == NO_ERROR) {
161 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 return bclient;
164}
165
Mathias Agopiane57f2922012-08-09 16:29:12 -0700166sp<IBinder> SurfaceFlinger::createDisplay()
167{
168 class DisplayToken : public BBinder {
169 sp<SurfaceFlinger> flinger;
170 virtual ~DisplayToken() {
171 // no more references, this display must be terminated
172 Mutex::Autolock _l(flinger->mStateLock);
173 flinger->mCurrentState.displays.removeItem(this);
174 flinger->setTransactionFlags(eDisplayTransactionNeeded);
175 }
176 public:
177 DisplayToken(const sp<SurfaceFlinger>& flinger)
178 : flinger(flinger) {
179 }
180 };
181
182 sp<BBinder> token = new DisplayToken(this);
183
184 Mutex::Autolock _l(mStateLock);
185 DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
186 mCurrentState.displays.add(token, info);
187
188 return token;
189}
190
191sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
192 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
193 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
194 return NULL;
195 }
196 return mDefaultDisplays[id];
197}
198
Jamie Gennis9a78c902011-01-12 18:30:40 -0800199sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
200{
201 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
202 return gba;
203}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205void SurfaceFlinger::bootFinished()
206{
207 const nsecs_t now = systemTime();
208 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000209 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700210 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700211
212 // wait patiently for the window manager death
213 const String16 name("window");
214 sp<IBinder> window(defaultServiceManager()->getService(name));
215 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700216 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700217 }
218
219 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700220 // formerly we would just kill the process, but we now ask it to exit so it
221 // can choose where to stop the animation.
222 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}
224
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700225void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
226 class MessageDestroyGLTexture : public MessageBase {
227 GLuint texture;
228 public:
229 MessageDestroyGLTexture(GLuint texture)
230 : texture(texture) {
231 }
232 virtual bool handler() {
233 glDeleteTextures(1, &texture);
234 return true;
235 }
236 };
237 postMessageAsync(new MessageDestroyGLTexture(texture));
238}
239
Mathias Agopiana4912602012-07-12 14:25:33 -0700240status_t SurfaceFlinger::selectConfigForPixelFormat(
241 EGLDisplay dpy,
242 EGLint const* attrs,
243 PixelFormat format,
244 EGLConfig* outConfig)
245{
246 EGLConfig config = NULL;
247 EGLint numConfigs = -1, n=0;
248 eglGetConfigs(dpy, NULL, 0, &numConfigs);
249 EGLConfig* const configs = new EGLConfig[numConfigs];
250 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
251 for (int i=0 ; i<n ; i++) {
252 EGLint nativeVisualId = 0;
253 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
254 if (nativeVisualId>0 && format == nativeVisualId) {
255 *outConfig = configs[i];
256 delete [] configs;
257 return NO_ERROR;
258 }
259 }
260 delete [] configs;
261 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
Mathias Agopiana4912602012-07-12 14:25:33 -0700264EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
265 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
266 // it is to be used with WIFI displays
267 EGLConfig config;
268 EGLint dummy;
269 status_t err;
270 EGLint attribs[] = {
271 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
272 EGL_RECORDABLE_ANDROID, EGL_TRUE,
273 EGL_NONE
274 };
275 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
276 if (err) {
277 // maybe we failed because of EGL_RECORDABLE_ANDROID
278 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
279 attribs[2] = EGL_NONE;
280 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
281 }
282 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
283 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
284 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
285 }
286 return config;
287}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
Mathias Agopiana4912602012-07-12 14:25:33 -0700289EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
290 // Also create our EGLContext
291 EGLint contextAttributes[] = {
292#ifdef EGL_IMG_context_priority
293#ifdef HAS_CONTEXT_PRIORITY
294#warning "using EGL_IMG_context_priority"
295 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
296#endif
297#endif
298 EGL_NONE, EGL_NONE
299 };
300 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
301 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
302 return ctxt;
303}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304
Mathias Agopiana4912602012-07-12 14:25:33 -0700305void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
306 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
307 if (!result) {
308 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
309 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 }
311
Mathias Agopiana4912602012-07-12 14:25:33 -0700312 GLExtensions& extensions(GLExtensions::getInstance());
313 extensions.initWithGLStrings(
314 glGetString(GL_VENDOR),
315 glGetString(GL_RENDERER),
316 glGetString(GL_VERSION),
317 glGetString(GL_EXTENSIONS),
318 eglQueryString(display, EGL_VENDOR),
319 eglQueryString(display, EGL_VERSION),
320 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700321
Mathias Agopiana4912602012-07-12 14:25:33 -0700322 EGLint w, h;
323 eglQuerySurface(display, surface, EGL_WIDTH, &w);
324 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700325
Mathias Agopiana4912602012-07-12 14:25:33 -0700326 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
327 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700328
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700330 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 glShadeModel(GL_FLAT);
333 glDisable(GL_DITHER);
334 glDisable(GL_CULL_FACE);
335
Mathias Agopiana4912602012-07-12 14:25:33 -0700336 struct pack565 {
337 inline uint16_t operator() (int r, int g, int b) const {
338 return (r<<11)|(g<<5)|b;
339 }
340 } pack565;
341
Jamie Gennis9575f602011-10-07 14:51:16 -0700342 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
343 glGenTextures(1, &mProtectedTexName);
344 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
347 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
348 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
349 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
350 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351
352 glViewport(0, 0, w, h);
353 glMatrixMode(GL_PROJECTION);
354 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700355 // put the origin in the left-bottom corner
356 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 -0800357
Mathias Agopiana4912602012-07-12 14:25:33 -0700358 // print some debugging info
359 EGLint r,g,b,a;
360 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
361 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
362 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
363 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
364 ALOGI("EGL informations:");
365 ALOGI("vendor : %s", extensions.getEglVendor());
366 ALOGI("version : %s", extensions.getEglVersion());
367 ALOGI("extensions: %s", extensions.getEglExtension());
368 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
369 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
370 ALOGI("OpenGL ES informations:");
371 ALOGI("vendor : %s", extensions.getVendor());
372 ALOGI("renderer : %s", extensions.getRenderer());
373 ALOGI("version : %s", extensions.getVersion());
374 ALOGI("extensions: %s", extensions.getExtension());
375 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
376 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
377}
378
Mathias Agopiana4912602012-07-12 14:25:33 -0700379status_t SurfaceFlinger::readyToRun()
380{
381 ALOGI( "SurfaceFlinger's main thread ready to run. "
382 "Initializing graphics H/W...");
383
Mathias Agopiana4912602012-07-12 14:25:33 -0700384 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700385 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
386 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700387
388 // Initialize the main display
389 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700390 sp<FramebufferSurface> fbs = FramebufferSurface::create();
391 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700392 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
393 exit(0);
394 }
395
Mathias Agopiane57f2922012-08-09 16:29:12 -0700396 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
397 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700398
Mathias Agopiana4912602012-07-12 14:25:33 -0700399 // initialize the config and context
400 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700401 ANativeWindow* const anw = stc.get();
402 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700403 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
404 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700405
406 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700407
408 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
409 mDefaultDisplays[i] = new BBinder();
410 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
411 }
412 sp<DisplayDevice> hw = new DisplayDevice(this,
413 DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
414 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700415
416 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700417 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700418 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800419
Mathias Agopian028508c2012-07-25 21:12:12 -0700420 // start the EventThread
421 mEventThread = new EventThread(this);
422 mEventQueue.setEventThread(mEventThread);
423
Mathias Agopian86303202012-07-24 22:46:10 -0700424 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700425 mHwc = new HWComposer(this,
426 *static_cast<HWComposer::EventHandler *>(this),
427 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700428
429 // initialize our drawing state
430 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700431
Mathias Agopiana4912602012-07-12 14:25:33 -0700432 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800433 mReadyToRunBarrier.open();
434
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700435 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700436 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700437
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 return NO_ERROR;
439}
440
Mathias Agopiana67e4182012-06-19 17:26:12 -0700441void SurfaceFlinger::startBootAnim() {
442 // start boot animation
443 property_set("service.bootanim.exit", "0");
444 property_set("ctl.start", "bootanim");
445}
446
Mathias Agopiana4912602012-07-12 14:25:33 -0700447uint32_t SurfaceFlinger::getMaxTextureSize() const {
448 return mMaxTextureSize;
449}
450
451uint32_t SurfaceFlinger::getMaxViewportDims() const {
452 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
453 mMaxViewportDims[0] : mMaxViewportDims[1];
454}
455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800457
Jamie Gennis582270d2011-08-17 18:19:00 -0700458bool SurfaceFlinger::authenticateSurfaceTexture(
459 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800460 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700461 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800462
463 // Check the visible layer list for the ISurface
464 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
465 size_t count = currentLayers.size();
466 for (size_t i=0 ; i<count ; i++) {
467 const sp<LayerBase>& layer(currentLayers[i]);
468 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700469 if (lbc != NULL) {
470 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
471 if (lbcBinder == surfaceTextureBinder) {
472 return true;
473 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800474 }
475 }
476
477 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700478 // SurfaceTexture gets destroyed before all the clients are done using it,
479 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800480 // will instead fail later on when the client tries to use the surface,
481 // which should be reported as "surface XYZ returned an -ENODEV". The
482 // purgatorized layers are no less authentic than the visible ones, so this
483 // should not cause any harm.
484 size_t purgatorySize = mLayerPurgatory.size();
485 for (size_t i=0 ; i<purgatorySize ; i++) {
486 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
487 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700488 if (lbc != NULL) {
489 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
490 if (lbcBinder == surfaceTextureBinder) {
491 return true;
492 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800493 }
494 }
495
496 return false;
497}
498
Mathias Agopianc666cae2012-07-25 18:56:13 -0700499status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700500 // TODO: this is here only for compatibility -- should go away eventually.
501 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700502 return BAD_INDEX;
503 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700504
505 const HWComposer& hwc(getHwComposer());
506 float xdpi = hwc.getDpiX();
507 float ydpi = hwc.getDpiY();
508
509 // TODO: Not sure if display density should handled by SF any longer
510 class Density {
511 static int getDensityFromProperty(char const* propName) {
512 char property[PROPERTY_VALUE_MAX];
513 int density = 0;
514 if (property_get(propName, property, NULL) > 0) {
515 density = atoi(property);
516 }
517 return density;
518 }
519 public:
520 static int getEmuDensity() {
521 return getDensityFromProperty("qemu.sf.lcd_density"); }
522 static int getBuildDensity() {
523 return getDensityFromProperty("ro.sf.lcd_density"); }
524 };
525 // The density of the device is provided by a build property
526 float density = Density::getBuildDensity() / 160.0f;
527 if (density == 0) {
528 // the build doesn't provide a density -- this is wrong!
529 // use xdpi instead
530 ALOGE("ro.sf.lcd_density must be defined as a build property");
531 density = xdpi / 160.0f;
532 }
533 if (Density::getEmuDensity()) {
534 // if "qemu.sf.lcd_density" is specified, it overrides everything
535 xdpi = ydpi = density = Density::getEmuDensity();
536 density /= 160.0f;
537 }
538
Mathias Agopian42977342012-08-05 00:40:46 -0700539 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
540 info->w = hw->getWidth();
541 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700542 info->xdpi = xdpi;
543 info->ydpi = ydpi;
544 info->fps = float(1e9 / hwc.getRefreshPeriod());
545 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700546 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700547 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700548 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700549 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700550}
551
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800552// ----------------------------------------------------------------------------
553
554sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800555 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700556}
557
Mathias Agopian3094df32012-06-18 18:06:45 -0700558void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700559 EGLSurface result = EGL_NO_SURFACE;
560 EGLSurface old_surface = EGL_NO_SURFACE;
561 sp<SurfaceTextureClient> stc;
562
563 if (display != NULL) {
564 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700565 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700566 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700567 ALOGE_IF(result == EGL_NO_SURFACE,
568 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
569 display.get());
570 }
571
572 { // scope for the lock
573 Mutex::Autolock _l(mStateLock);
574 old_surface = mExternalDisplaySurface;
575 mExternalDisplayNativeWindow = stc;
576 mExternalDisplaySurface = result;
577 ALOGD("mExternalDisplaySurface = %p", result);
578 }
579
580 if (old_surface != EGL_NO_SURFACE) {
581 // Note: EGL allows to destroy an object while its current
582 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700583 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700584 }
585}
586
587EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
588 Mutex::Autolock _l(mStateLock);
589 return mExternalDisplaySurface;
590}
591
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800593
594void SurfaceFlinger::waitForEvent() {
595 mEventQueue.waitMessage();
596}
597
598void SurfaceFlinger::signalTransaction() {
599 mEventQueue.invalidate();
600}
601
602void SurfaceFlinger::signalLayerUpdate() {
603 mEventQueue.invalidate();
604}
605
606void SurfaceFlinger::signalRefresh() {
607 mEventQueue.refresh();
608}
609
610status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
611 nsecs_t reltime, uint32_t flags) {
612 return mEventQueue.postMessage(msg, reltime);
613}
614
615status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
616 nsecs_t reltime, uint32_t flags) {
617 status_t res = mEventQueue.postMessage(msg, reltime);
618 if (res == NO_ERROR) {
619 msg->wait();
620 }
621 return res;
622}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623
Mathias Agopian4fec8732012-06-29 14:12:52 -0700624bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 return true;
627}
628
Mathias Agopian86303202012-07-24 22:46:10 -0700629void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700630 mEventThread->onVSyncReceived(dpy, timestamp);
631}
632
633void SurfaceFlinger::eventControl(int event, int enabled) {
634 getHwComposer().eventControl(event, enabled);
635}
636
Mathias Agopian4fec8732012-06-29 14:12:52 -0700637void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800638 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800639 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700640 case MessageQueue::INVALIDATE:
641 handleMessageTransaction();
642 handleMessageInvalidate();
643 signalRefresh();
644 break;
645 case MessageQueue::REFRESH:
646 handleMessageRefresh();
647 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800648 }
649}
650
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700652 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700654 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700655 }
656}
657
658void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700659 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700660}
661
662void SurfaceFlinger::handleMessageRefresh() {
663 handleRefresh();
664
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700665 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700666 mVisibleRegionsDirty = false;
667 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700668
669 /*
670 * rebuild the visible layer list per screen
671 */
672
Mathias Agopian87baae12012-07-31 12:38:26 -0700673 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700674 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700675 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700676 Region opaqueRegion;
677 Region dirtyRegion;
678 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700679 hw->getLayerStack(), dirtyRegion, opaqueRegion);
680 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700681
682 Vector< sp<LayerBase> > layersSortedByZ;
683 const size_t count = currentLayers.size();
684 for (size_t i=0 ; i<count ; i++) {
685 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700686 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700687 if (!currentLayers[i]->visibleRegion.isEmpty()) {
688 layersSortedByZ.add(currentLayers[i]);
689 }
690 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700691 }
Mathias Agopian42977342012-08-05 00:40:46 -0700692 hw->setVisibleLayersSortedByZ(layersSortedByZ);
693 hw->undefinedRegion.set(hw->getBounds());
694 hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700695 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700696 }
697
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700698 HWComposer& hwc(getHwComposer());
699 if (hwc.initCheck() == NO_ERROR) {
700 // build the h/w work list
701 const bool workListsDirty = mHwWorkListDirty;
702 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700703 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700704 sp<const DisplayDevice> hw(mDisplays[dpy]);
705 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700706 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700707
Mathias Agopian1e260872012-08-08 18:35:12 -0700708 const int32_t id = hw->getDisplayId();
709 if (hwc.createWorkList(id, count) >= 0) {
710 HWComposer::LayerListIterator cur = hwc.begin(id);
711 const HWComposer::LayerListIterator end = hwc.end(id);
712 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
713 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700714
Mathias Agopian1e260872012-08-08 18:35:12 -0700715 if (CC_UNLIKELY(workListsDirty)) {
716 layer->setGeometry(hw, *cur);
717 if (mDebugDisableHWC || mDebugRegion) {
718 cur->setSkip(true);
719 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700720 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700721
Mathias Agopian1e260872012-08-08 18:35:12 -0700722 /*
723 * update the per-frame h/w composer data for each layer
724 * and build the transparent region of the FB
725 */
726 layer->setPerFrameData(hw, *cur);
727 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700728 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700729 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700730 status_t err = hwc.prepare();
731 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
732 }
733
734 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700735 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700736 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700737
Mathias Agopian87baae12012-07-31 12:38:26 -0700738 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700739 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700740 Region dirtyRegion;
741 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700742 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700743 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700744 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
745 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700746 }
Mathias Agopian42977342012-08-05 00:40:46 -0700747 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700748
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700749 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700750 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700751 // repaint the framebuffer (if needed)
752 handleRepaint(hw, dirtyRegion);
753 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700754 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700755 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700756 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700757 }
758
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700759 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700760
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700761
762#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700763 // render to the external display if we have one
764 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
765 if (externalDisplaySurface != EGL_NO_SURFACE) {
766 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
767 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
768 externalDisplaySurface, externalDisplaySurface,
769 eglGetCurrentContext());
770
771 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
772
773 if (success) {
774 // redraw the screen entirely...
775 glDisable(GL_TEXTURE_EXTERNAL_OES);
776 glDisable(GL_TEXTURE_2D);
777 glClearColor(0,0,0,1);
778 glClear(GL_COLOR_BUFFER_BIT);
779 glMatrixMode(GL_MODELVIEW);
780 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700781
Mathias Agopian42977342012-08-05 00:40:46 -0700782 const sp<DisplayDevice>& hw(getDisplayDevice(0));
783 const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700784 const size_t count = layers.size();
785 for (size_t i=0 ; i<count ; ++i) {
786 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700787 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700788 }
789
790 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
791 ALOGE_IF(!success, "external display eglSwapBuffers failed");
792
Mathias Agopian42977342012-08-05 00:40:46 -0700793 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700794 }
795
796 success = eglMakeCurrent(eglGetCurrentDisplay(),
797 cur, cur, eglGetCurrentContext());
798
799 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
800 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700801#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700802
803}
804
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805void SurfaceFlinger::postFramebuffer()
806{
Mathias Agopian841cde52012-03-01 15:44:37 -0800807 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800808
Mathias Agopiana44b0412011-10-16 18:46:35 -0700809 const nsecs_t now = systemTime();
810 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700811
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700812 HWComposer& hwc(getHwComposer());
813
Mathias Agopian92a979a2012-08-02 18:32:23 -0700814 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700815 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700816 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700817 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700818 const size_t count = currentLayers.size();
Mathias Agopian1e260872012-08-08 18:35:12 -0700819 const int32_t id = hw->getDisplayId();
820 HWComposer::LayerListIterator cur = hwc.begin(id);
821 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700822 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
823 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700824 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700825 }
826 }
Mathias Agopian42977342012-08-05 00:40:46 -0700827 hw->flip(hw->swapRegion);
828 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700829 }
830
Jesse Hallef194142012-06-14 14:45:17 -0700831 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 // FIXME: eventually commit() won't take arguments
Mathias Agopian42977342012-08-05 00:40:46 -0700833 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700834 }
835
Mathias Agopian92a979a2012-08-02 18:32:23 -0700836 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700837 sp<const DisplayDevice> hw(mDisplays[dpy]);
838 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700839 const size_t count = currentLayers.size();
840 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700841 int32_t id = hw->getDisplayId();
842 HWComposer::LayerListIterator cur = hwc.begin(id);
843 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700844 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700845 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700846 }
847 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700848 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700850 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851 }
Jesse Hallef194142012-06-14 14:45:17 -0700852 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700853
854 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800855 }
856
Mathias Agopiana44b0412011-10-16 18:46:35 -0700857 mLastSwapBufferTime = systemTime() - now;
858 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859}
860
Mathias Agopian87baae12012-07-31 12:38:26 -0700861void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862{
Mathias Agopian841cde52012-03-01 15:44:37 -0800863 ATRACE_CALL();
864
Mathias Agopianca4d3602011-05-19 15:38:14 -0700865 Mutex::Autolock _l(mStateLock);
866 const nsecs_t now = systemTime();
867 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868
Mathias Agopianca4d3602011-05-19 15:38:14 -0700869 // Here we're guaranteed that some transaction flags are set
870 // so we can call handleTransactionLocked() unconditionally.
871 // We call getTransactionFlags(), which will also clear the flags,
872 // with mStateLock held to guarantee that mCurrentState won't change
873 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700874
Mathias Agopiane57f2922012-08-09 16:29:12 -0700875 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700876 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700877
Mathias Agopianca4d3602011-05-19 15:38:14 -0700878 mLastTransactionTime = systemTime() - now;
879 mDebugInTransaction = 0;
880 invalidateHwcGeometry();
881 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700882}
883
Mathias Agopian87baae12012-07-31 12:38:26 -0700884void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700885{
886 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 const size_t count = currentLayers.size();
888
889 /*
890 * Traversal of the children
891 * (perform the transaction for each of them if needed)
892 */
893
894 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
895 if (layersNeedTransaction) {
896 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700897 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
899 if (!trFlags) continue;
900
901 const uint32_t flags = layer->doTransaction(0);
902 if (flags & Layer::eVisibleRegion)
903 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 }
905 }
906
907 /*
908 * Perform our own transaction if needed
909 */
910
Mathias Agopiane57f2922012-08-09 16:29:12 -0700911 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700912 // here we take advantage of Vector's copy-on-write semantics to
913 // improve performance by skipping the transaction entirely when
914 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700915 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
916 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700917 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700919 const size_t cc = curr.size();
920 const size_t dc = draw.size();
921
922 // find the displays that were removed
923 // (ie: in drawing state but not in current state)
924 // also handle displays that changed
925 // (ie: displays that are in both lists)
926 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700927 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
928 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700929 // in drawing state but not in current state
930 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700931 mDisplays.removeItem(draw[i].id);
932 } else {
933 ALOGW("trying to remove the main display");
934 }
935 } else {
936 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700937 const DisplayDeviceState& state(curr[j]);
938 if (state.surface != draw[i].surface) {
939 // changing the surface is like destroying and
940 // recreating the DisplayDevice
941
942 sp<SurfaceTextureClient> stc(
943 new SurfaceTextureClient(state.surface));
944
945 sp<DisplayDevice> disp = new DisplayDevice(this,
946 state.id, stc, 0, mEGLConfig);
947
948 disp->setLayerStack(state.layerStack);
949 disp->setOrientation(state.orientation);
950 // TODO: take viewport and frame into account
951 mDisplays.replaceValueFor(state.id, disp);
952 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700953 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700954 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700955 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700956 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700957 if (state.orientation != draw[i].orientation ||
958 state.viewport != draw[i].viewport ||
959 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700960 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
961 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700962 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700963 }
964 }
965 }
966
967 // find displays that were added
968 // (ie: in current state but not in drawing state)
969 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700971 // FIXME: we need to pass the surface here
Mathias Agopiane57f2922012-08-09 16:29:12 -0700972 const DisplayDeviceState& state(curr[i]);
973 sp<SurfaceTextureClient> stc(
974 new SurfaceTextureClient(state.surface));
975 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
976 stc, 0, mEGLConfig);
977 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700978 }
979 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 }
981
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700982 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
983 // layers have been added
984 mVisibleRegionsDirty = true;
985 }
986
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 // some layers might have been removed, so
988 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700989 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700990 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700992 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700993 const size_t count = previousLayers.size();
994 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700995 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700996 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700997 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700998 // TODO: we could traverse the tree from front to back and
999 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001000 // TODO: we could cache the transformed region
1001 Layer::State front(layer->drawingState());
1002 Region visibleReg = front.transform.transform(
1003 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -07001004 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001005 }
1006 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 }
1009
1010 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001011}
1012
1013void SurfaceFlinger::commitTransaction()
1014{
1015 if (!mLayersPendingRemoval.isEmpty()) {
1016 // Notify removed layers now that they can't be drawn from
1017 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1018 mLayersPendingRemoval[i]->onRemoved();
1019 }
1020 mLayersPendingRemoval.clear();
1021 }
1022
1023 mDrawingState = mCurrentState;
1024 mTransationPending = false;
1025 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026}
1027
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001029 const LayerVector& currentLayers, uint32_t layerStack,
1030 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031{
Mathias Agopian841cde52012-03-01 15:44:37 -08001032 ATRACE_CALL();
1033
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 Region aboveOpaqueLayers;
1035 Region aboveCoveredLayers;
1036 Region dirty;
1037
Mathias Agopian87baae12012-07-31 12:38:26 -07001038 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039
1040 size_t i = currentLayers.size();
1041 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001042 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043
1044 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001045 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046
Mathias Agopian87baae12012-07-31 12:38:26 -07001047 // only consider the layers on the given later stack
1048 if (s.layerStack != layerStack)
1049 continue;
1050
Mathias Agopianab028732010-03-16 16:41:46 -07001051 /*
1052 * opaqueRegion: area of a surface that is fully opaque.
1053 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001055
1056 /*
1057 * visibleRegion: area of a surface that is visible on screen
1058 * and not fully transparent. This is essentially the layer's
1059 * footprint minus the opaque regions above it.
1060 * Areas covered by a translucent surface are considered visible.
1061 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001063
1064 /*
1065 * coveredRegion: area of a surface that is covered by all
1066 * visible regions above it (which includes the translucent areas).
1067 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001069
1070
1071 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001072 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001073 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001074 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001076 if (!visibleRegion.isEmpty()) {
1077 // Remove the transparent area from the visible region
1078 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001079 Region transparentRegionScreen;
1080 const Transform tr(s.transform);
1081 if (tr.transformed()) {
1082 if (tr.preserveRects()) {
1083 // transform the transparent region
1084 transparentRegionScreen = tr.transform(s.transparentRegion);
1085 } else {
1086 // transformation too complex, can't do the
1087 // transparent region optimization.
1088 transparentRegionScreen.clear();
1089 }
1090 } else {
1091 transparentRegionScreen = s.transparentRegion;
1092 }
1093 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001094 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095
Mathias Agopianab028732010-03-16 16:41:46 -07001096 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001097 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001098 if (s.alpha==255 && !translucent &&
1099 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1100 // the opaque region is the layer's footprint
1101 opaqueRegion = visibleRegion;
1102 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 }
1104 }
1105
Mathias Agopianab028732010-03-16 16:41:46 -07001106 // Clip the covered region to the visible region
1107 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1108
1109 // Update aboveCoveredLayers for next (lower) layer
1110 aboveCoveredLayers.orSelf(visibleRegion);
1111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 // subtract the opaque region covered by the layers above us
1113 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114
1115 // compute this layer's dirty region
1116 if (layer->contentDirty) {
1117 // we need to invalidate the whole region
1118 dirty = visibleRegion;
1119 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001120 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 layer->contentDirty = false;
1122 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001123 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001124 * the exposed region consists of two components:
1125 * 1) what's VISIBLE now and was COVERED before
1126 * 2) what's EXPOSED now less what was EXPOSED before
1127 *
1128 * note that (1) is conservative, we start with the whole
1129 * visible region but only keep what used to be covered by
1130 * something -- which mean it may have been exposed.
1131 *
1132 * (2) handles areas that were not covered by anything but got
1133 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001134 */
Mathias Agopianab028732010-03-16 16:41:46 -07001135 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001136 const Region oldVisibleRegion = layer->visibleRegion;
1137 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001138 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1139 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 }
1141 dirty.subtractSelf(aboveOpaqueLayers);
1142
1143 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001144 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145
Mathias Agopianab028732010-03-16 16:41:46 -07001146 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 // Store the visible region is screen space
1150 layer->setVisibleRegion(visibleRegion);
1151 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 }
1153
Mathias Agopian87baae12012-07-31 12:38:26 -07001154 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155}
1156
Mathias Agopian87baae12012-07-31 12:38:26 -07001157void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1158 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001159 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001160 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1161 if (hw->getLayerStack() == layerStack) {
1162 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001163 }
1164 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001165}
1166
1167void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001169 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001170 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001172 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001173
Mathias Agopian4fec8732012-06-29 14:12:52 -07001174 bool visibleRegions = false;
1175 const size_t count = currentLayers.size();
1176 sp<LayerBase> const* layers = currentLayers.array();
1177 for (size_t i=0 ; i<count ; i++) {
1178 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001179 const Region dirty(layer->latchBuffer(visibleRegions));
1180 Layer::State s(layer->drawingState());
1181 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001182 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001183
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001184 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185}
1186
Mathias Agopianad456f92011-01-13 17:53:01 -08001187void SurfaceFlinger::invalidateHwcGeometry()
1188{
1189 mHwWorkListDirty = true;
1190}
1191
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001192void SurfaceFlinger::handleRefresh()
1193{
1194 bool needInvalidate = false;
1195 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1196 const size_t count = currentLayers.size();
1197 for (size_t i=0 ; i<count ; i++) {
1198 const sp<LayerBase>& layer(currentLayers[i]);
1199 if (layer->onPreComposition()) {
1200 needInvalidate = true;
1201 }
1202 }
1203 if (needInvalidate) {
1204 signalLayerUpdate();
1205 }
1206}
1207
Mathias Agopian42977342012-08-05 00:40:46 -07001208void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001209 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210{
Mathias Agopian841cde52012-03-01 15:44:37 -08001211 ATRACE_CALL();
1212
Mathias Agopian87baae12012-07-31 12:38:26 -07001213 Region dirtyRegion(inDirtyRegion);
1214
Mathias Agopianb8a55602009-06-26 19:06:36 -07001215 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001216 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217
Glenn Kasten99ed2242011-12-15 09:51:17 -08001218 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001219 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 }
1221
Mathias Agopian42977342012-08-05 00:40:46 -07001222 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001223 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001224 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1225 // takes a rectangle, we must make sure to update that whole
1226 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001227 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001228 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001229 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001230 // We need to redraw the rectangle that will be updated
1231 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001232 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001233 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001234 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001235 } else {
1236 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001237 dirtyRegion.set(hw->bounds());
1238 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 }
1240 }
1241
Mathias Agopian87baae12012-07-31 12:38:26 -07001242 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001244 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1245 const size_t count = currentLayers.size();
1246 for (size_t i=0 ; i<count ; i++) {
1247 currentLayers[i]->onPostComposition();
1248 }
1249
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001250 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001251 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252}
1253
Mathias Agopian42977342012-08-05 00:40:46 -07001254void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001255{
Mathias Agopian86303202012-07-24 22:46:10 -07001256 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001257 int32_t id = hw->getDisplayId();
1258 HWComposer::LayerListIterator cur = hwc.begin(id);
1259 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001260
Mathias Agopian1e260872012-08-08 18:35:12 -07001261 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001262 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001263
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001264 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001265
1266 // set the frame buffer
1267 glMatrixMode(GL_MODELVIEW);
1268 glLoadIdentity();
1269
1270 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001271 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001272 // when using overlays, we assume a fully transparent framebuffer
1273 // NOTE: we could reduce how much we need to clear, for instance
1274 // remove where there are opaque FB layers. however, on some
1275 // GPUs doing a "clean slate" glClear might be more efficient.
1276 // We'll revisit later if needed.
1277 glClearColor(0, 0, 0, 0);
1278 glClear(GL_COLOR_BUFFER_BIT);
1279 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001280 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001281 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001282 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001283 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001284 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001285 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001286 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001287
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001288 /*
1289 * and then, render the layers targeted at the framebuffer
1290 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001291
Mathias Agopian42977342012-08-05 00:40:46 -07001292 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001293 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001294 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001295 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001296 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001297 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001298 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001299 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001300 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001301 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001302 // never clear the very first layer since we're
1303 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001304 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001305 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001306 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001307 continue;
1308 }
1309 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001310 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001311 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001312 if (cur != end) {
1313 ++cur;
1314 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001315 }
1316 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317}
1318
Mathias Agopian42977342012-08-05 00:40:46 -07001319void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001320 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321{
Mathias Agopian42977342012-08-05 00:40:46 -07001322 const uint32_t flags = hw->getFlags();
1323 const int32_t height = hw->getHeight();
1324 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001325 return;
1326 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001327
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001328 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1329 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001330 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001331 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001332 }
1333
Mathias Agopianc492e672011-10-18 14:49:27 -07001334 glDisable(GL_TEXTURE_EXTERNAL_OES);
1335 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337
Mathias Agopian0926f502009-05-04 14:17:04 -07001338 static int toggle = 0;
1339 toggle = 1 - toggle;
1340 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001341 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001342 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001343 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001344 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345
Mathias Agopian87baae12012-07-31 12:38:26 -07001346 Region::const_iterator it = dirtyRegion.begin();
1347 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001348 while (it != end) {
1349 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001351 { r.left, height - r.top },
1352 { r.left, height - r.bottom },
1353 { r.right, height - r.bottom },
1354 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 };
1356 glVertexPointer(2, GL_FLOAT, 0, vertices);
1357 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1358 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001359
Mathias Agopian42977342012-08-05 00:40:46 -07001360 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361
1362 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001363 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364}
1365
Mathias Agopian87baae12012-07-31 12:38:26 -07001366void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001368 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001369 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001370 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001371 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001372
1373 GLfloat vertices[4][2];
1374 glVertexPointer(2, GL_FLOAT, 0, vertices);
1375 Region::const_iterator it = region.begin();
1376 Region::const_iterator const end = region.end();
1377 while (it != end) {
1378 const Rect& r = *it++;
1379 vertices[0][0] = r.left;
1380 vertices[0][1] = r.top;
1381 vertices[1][0] = r.right;
1382 vertices[1][1] = r.top;
1383 vertices[2][0] = r.right;
1384 vertices[2][1] = r.bottom;
1385 vertices[3][0] = r.left;
1386 vertices[3][1] = r.bottom;
1387 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1388 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389}
1390
Mathias Agopian96f08192010-06-02 23:28:45 -07001391ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1392 const sp<LayerBaseClient>& lbc)
1393{
Mathias Agopian96f08192010-06-02 23:28:45 -07001394 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001395 size_t name = client->attachLayer(lbc);
1396
Mathias Agopian96f08192010-06-02 23:28:45 -07001397 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001398 Mutex::Autolock _l(mStateLock);
1399 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001400
Mathias Agopian4f113742011-05-03 16:21:41 -07001401 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001402}
1403
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001404status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405{
1406 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001407 status_t err = purgatorizeLayer_l(layer);
1408 if (err == NO_ERROR)
Jeff Brown01eb9792012-08-15 01:08:54 -07001409 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001410 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411}
1412
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001413status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414{
1415 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1416 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001417 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 return NO_ERROR;
1419 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001420 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421}
1422
Mathias Agopian9a112062009-04-17 19:36:26 -07001423status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1424{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001425 // First add the layer to the purgatory list, which makes sure it won't
1426 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001427 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001428 if (err >= 0) {
1429 mLayerPurgatory.add(layerBase);
1430 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001431
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001432 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001433
Mathias Agopian3d579642009-06-04 18:46:21 -07001434 // it's possible that we don't find a layer, because it might
1435 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001436 // from the user because there is a race between Client::destroySurface(),
1437 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001438 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1439}
1440
Mathias Agopiandea20b12011-05-03 17:04:02 -07001441uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1442{
1443 return android_atomic_release_load(&mTransactionFlags);
1444}
1445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1447{
1448 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1449}
1450
Mathias Agopianbb641242010-05-18 17:06:55 -07001451uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452{
1453 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1454 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001455 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 }
1457 return old;
1458}
1459
Mathias Agopian8b33f032012-07-24 20:43:54 -07001460void SurfaceFlinger::setTransactionState(
1461 const Vector<ComposerState>& state,
1462 const Vector<DisplayState>& displays,
1463 uint32_t flags)
1464{
Mathias Agopian698c0872011-06-28 19:09:31 -07001465 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001466 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001467
1468 size_t count = displays.size();
1469 for (size_t i=0 ; i<count ; i++) {
1470 const DisplayState& s(displays[i]);
1471 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001472 }
1473
Mathias Agopiane57f2922012-08-09 16:29:12 -07001474 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001475 for (size_t i=0 ; i<count ; i++) {
1476 const ComposerState& s(state[i]);
1477 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001478 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001479 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001480
Mathias Agopian386aa982011-11-07 21:58:03 -08001481 if (transactionFlags) {
1482 // this triggers the transaction
1483 setTransactionFlags(transactionFlags);
1484
1485 // if this is a synchronous transaction, wait for it to take effect
1486 // before returning.
1487 if (flags & eSynchronous) {
1488 mTransationPending = true;
1489 }
1490 while (mTransationPending) {
1491 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1492 if (CC_UNLIKELY(err != NO_ERROR)) {
1493 // just in case something goes wrong in SF, return to the
1494 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001495 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001496 mTransationPending = false;
1497 break;
1498 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001499 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 }
1501}
1502
Mathias Agopiane57f2922012-08-09 16:29:12 -07001503uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1504{
1505 uint32_t flags = 0;
1506 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1507 if (disp.id >= 0) {
1508 const uint32_t what = s.what;
1509 if (what & DisplayState::eSurfaceChanged) {
1510 if (disp.surface->asBinder() != s.surface->asBinder()) {
1511 disp.surface = s.surface;
1512 flags |= eDisplayTransactionNeeded;
1513 }
1514 }
1515 if (what & DisplayState::eLayerStackChanged) {
1516 if (disp.layerStack != s.layerStack) {
1517 disp.layerStack = s.layerStack;
1518 flags |= eDisplayTransactionNeeded;
1519 }
1520 }
1521 if (what & DisplayState::eTransformChanged) {
1522 if (disp.orientation != s.orientation) {
1523 disp.orientation = s.orientation;
1524 flags |= eDisplayTransactionNeeded;
1525 }
1526 if (disp.frame != s.frame) {
1527 disp.frame = s.frame;
1528 flags |= eDisplayTransactionNeeded;
1529 }
1530 if (disp.viewport != s.viewport) {
1531 disp.viewport = s.viewport;
1532 flags |= eDisplayTransactionNeeded;
1533 }
1534 }
1535 }
1536 return flags;
1537}
1538
1539uint32_t SurfaceFlinger::setClientStateLocked(
1540 const sp<Client>& client,
1541 const layer_state_t& s)
1542{
1543 uint32_t flags = 0;
1544 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1545 if (layer != 0) {
1546 const uint32_t what = s.what;
1547 if (what & layer_state_t::ePositionChanged) {
1548 if (layer->setPosition(s.x, s.y))
1549 flags |= eTraversalNeeded;
1550 }
1551 if (what & layer_state_t::eLayerChanged) {
1552 // NOTE: index needs to be calculated before we update the state
1553 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1554 if (layer->setLayer(s.z)) {
1555 mCurrentState.layersSortedByZ.removeAt(idx);
1556 mCurrentState.layersSortedByZ.add(layer);
1557 // we need traversal (state changed)
1558 // AND transaction (list changed)
1559 flags |= eTransactionNeeded|eTraversalNeeded;
1560 }
1561 }
1562 if (what & layer_state_t::eSizeChanged) {
1563 if (layer->setSize(s.w, s.h)) {
1564 flags |= eTraversalNeeded;
1565 }
1566 }
1567 if (what & layer_state_t::eAlphaChanged) {
1568 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1569 flags |= eTraversalNeeded;
1570 }
1571 if (what & layer_state_t::eMatrixChanged) {
1572 if (layer->setMatrix(s.matrix))
1573 flags |= eTraversalNeeded;
1574 }
1575 if (what & layer_state_t::eTransparentRegionChanged) {
1576 if (layer->setTransparentRegionHint(s.transparentRegion))
1577 flags |= eTraversalNeeded;
1578 }
1579 if (what & layer_state_t::eVisibilityChanged) {
1580 if (layer->setFlags(s.flags, s.mask))
1581 flags |= eTraversalNeeded;
1582 }
1583 if (what & layer_state_t::eCropChanged) {
1584 if (layer->setCrop(s.crop))
1585 flags |= eTraversalNeeded;
1586 }
1587 if (what & layer_state_t::eLayerStackChanged) {
1588 // NOTE: index needs to be calculated before we update the state
1589 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1590 if (layer->setLayerStack(s.layerStack)) {
1591 mCurrentState.layersSortedByZ.removeAt(idx);
1592 mCurrentState.layersSortedByZ.add(layer);
1593 // we need traversal (state changed)
1594 // AND transaction (list changed)
1595 flags |= eTransactionNeeded|eTraversalNeeded;
1596 }
1597 }
1598 }
1599 return flags;
1600}
1601
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001602sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001603 ISurfaceComposerClient::surface_data_t* params,
1604 const String8& name,
1605 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1607 uint32_t flags)
1608{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001609 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001610 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001611
1612 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001613 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001614 int(w), int(h));
1615 return surfaceHandle;
1616 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001617
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001618 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001619 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1620 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001621 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001623 case ISurfaceComposerClient::eFXSurfaceBlur:
1624 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001625 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001627 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001628 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001629 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 }
1631
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001632 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001633 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001634 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001635 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001637 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001638 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001639 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001640 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001641 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 }
1643
1644 return surfaceHandle;
1645}
1646
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001647sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001648 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001649 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001650 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651{
1652 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001653 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 case PIXEL_FORMAT_TRANSPARENT:
1655 case PIXEL_FORMAT_TRANSLUCENT:
1656 format = PIXEL_FORMAT_RGBA_8888;
1657 break;
1658 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001659#ifdef NO_RGBX_8888
1660 format = PIXEL_FORMAT_RGB_565;
1661#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001662 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001663#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 break;
1665 }
1666
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001667#ifdef NO_RGBX_8888
1668 if (format == PIXEL_FORMAT_RGBX_8888)
1669 format = PIXEL_FORMAT_RGBA_8888;
1670#endif
1671
Mathias Agopian96f08192010-06-02 23:28:45 -07001672 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001673 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001674 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001675 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001676 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 }
1678 return layer;
1679}
1680
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001681sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001682 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001683 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684{
Mathias Agopian96f08192010-06-02 23:28:45 -07001685 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001686 return layer;
1687}
1688
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001689sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001690 const sp<Client>& client, DisplayID display,
1691 uint32_t w, uint32_t h, uint32_t flags)
1692{
1693 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 return layer;
1695}
1696
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001697status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698{
Mathias Agopian9a112062009-04-17 19:36:26 -07001699 /*
1700 * called by the window manager, when a surface should be marked for
1701 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001702 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001703 * The surface is removed from the current and drawing lists, but placed
1704 * in the purgatory queue, so it's not destroyed right-away (we need
1705 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001706 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001707
Mathias Agopian48d819a2009-09-10 19:41:18 -07001708 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001709 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001710 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001711
Mathias Agopian48d819a2009-09-10 19:41:18 -07001712 if (layer != 0) {
1713 err = purgatorizeLayer_l(layer);
1714 if (err == NO_ERROR) {
Jeff Brown01eb9792012-08-15 01:08:54 -07001715 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001716 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001717 }
1718 return err;
1719}
1720
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001721status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001722{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001723 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001724 status_t err = NO_ERROR;
1725 sp<LayerBaseClient> l(layer.promote());
1726 if (l != NULL) {
1727 Mutex::Autolock _l(mStateLock);
1728 err = removeLayer_l(l);
1729 if (err == NAME_NOT_FOUND) {
1730 // The surface wasn't in the current list, which means it was
1731 // removed already, which means it is in the purgatory,
1732 // and need to be removed from there.
1733 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001734 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001735 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001736 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001737 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001738 "error removing layer=%p (%s)", l.get(), strerror(-err));
1739 }
1740 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741}
1742
Mathias Agopianb60314a2012-04-10 22:09:54 -07001743// ---------------------------------------------------------------------------
1744
1745void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001746 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001747 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001748 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001749 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001750 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001751 mVisibleRegionsDirty = true;
1752 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753}
1754
Mathias Agopianb60314a2012-04-10 22:09:54 -07001755void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001756 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001757 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1758 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001759 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001760 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001761 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001762 // from this point on, SF will stop drawing
1763 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764}
1765
Colin Cross8e533062012-06-07 13:17:52 -07001766void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001767 class MessageScreenAcquired : public MessageBase {
1768 SurfaceFlinger* flinger;
1769 public:
1770 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1771 virtual bool handler() {
1772 flinger->onScreenAcquired();
1773 return true;
1774 }
1775 };
1776 sp<MessageBase> msg = new MessageScreenAcquired(this);
1777 postMessageSync(msg);
1778}
1779
Colin Cross8e533062012-06-07 13:17:52 -07001780void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001781 class MessageScreenReleased : public MessageBase {
1782 SurfaceFlinger* flinger;
1783 public:
1784 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1785 virtual bool handler() {
1786 flinger->onScreenReleased();
1787 return true;
1788 }
1789 };
1790 sp<MessageBase> msg = new MessageScreenReleased(this);
1791 postMessageSync(msg);
1792}
1793
1794// ---------------------------------------------------------------------------
1795
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1797{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001798 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 char buffer[SIZE];
1800 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001801
1802 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001803 snprintf(buffer, SIZE, "Permission Denial: "
1804 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1805 IPCThreadState::self()->getCallingPid(),
1806 IPCThreadState::self()->getCallingUid());
1807 result.append(buffer);
1808 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001809 // Try to get the main lock, but don't insist if we can't
1810 // (this would indicate SF is stuck, but we want to be able to
1811 // print something in dumpsys).
1812 int retry = 3;
1813 while (mStateLock.tryLock()<0 && --retry>=0) {
1814 usleep(1000000);
1815 }
1816 const bool locked(retry >= 0);
1817 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001818 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001819 "SurfaceFlinger appears to be unresponsive, "
1820 "dumping anyways (no locks held)\n");
1821 result.append(buffer);
1822 }
1823
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001824 bool dumpAll = true;
1825 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001826 size_t numArgs = args.size();
1827 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001828 if ((index < numArgs) &&
1829 (args[index] == String16("--list"))) {
1830 index++;
1831 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001832 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001833 }
1834
1835 if ((index < numArgs) &&
1836 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001837 index++;
1838 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001839 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001840 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001841
1842 if ((index < numArgs) &&
1843 (args[index] == String16("--latency-clear"))) {
1844 index++;
1845 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001846 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001847 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001849
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001850 if (dumpAll) {
1851 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001852 }
1853
Mathias Agopian9795c422009-08-26 16:36:26 -07001854 if (locked) {
1855 mStateLock.unlock();
1856 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857 }
1858 write(fd, result.string(), result.size());
1859 return NO_ERROR;
1860}
1861
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001862void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1863 String8& result, char* buffer, size_t SIZE) const
1864{
1865 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1866 const size_t count = currentLayers.size();
1867 for (size_t i=0 ; i<count ; i++) {
1868 const sp<LayerBase>& layer(currentLayers[i]);
1869 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1870 result.append(buffer);
1871 }
1872}
1873
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001874void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1875 String8& result, char* buffer, size_t SIZE) const
1876{
1877 String8 name;
1878 if (index < args.size()) {
1879 name = String8(args[index]);
1880 index++;
1881 }
1882
1883 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1884 const size_t count = currentLayers.size();
1885 for (size_t i=0 ; i<count ; i++) {
1886 const sp<LayerBase>& layer(currentLayers[i]);
1887 if (name.isEmpty()) {
1888 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1889 result.append(buffer);
1890 }
1891 if (name.isEmpty() || (name == layer->getName())) {
1892 layer->dumpStats(result, buffer, SIZE);
1893 }
1894 }
1895}
1896
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001897void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1898 String8& result, char* buffer, size_t SIZE) const
1899{
1900 String8 name;
1901 if (index < args.size()) {
1902 name = String8(args[index]);
1903 index++;
1904 }
1905
1906 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1907 const size_t count = currentLayers.size();
1908 for (size_t i=0 ; i<count ; i++) {
1909 const sp<LayerBase>& layer(currentLayers[i]);
1910 if (name.isEmpty() || (name == layer->getName())) {
1911 layer->clearStats();
1912 }
1913 }
1914}
1915
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001916void SurfaceFlinger::dumpAllLocked(
1917 String8& result, char* buffer, size_t SIZE) const
1918{
1919 // figure out if we're stuck somewhere
1920 const nsecs_t now = systemTime();
1921 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1922 const nsecs_t inTransaction(mDebugInTransaction);
1923 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1924 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1925
1926 /*
1927 * Dump the visible layer list
1928 */
1929 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1930 const size_t count = currentLayers.size();
1931 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1932 result.append(buffer);
1933 for (size_t i=0 ; i<count ; i++) {
1934 const sp<LayerBase>& layer(currentLayers[i]);
1935 layer->dump(result, buffer, SIZE);
1936 }
1937
1938 /*
1939 * Dump the layers in the purgatory
1940 */
1941
1942 const size_t purgatorySize = mLayerPurgatory.size();
1943 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1944 result.append(buffer);
1945 for (size_t i=0 ; i<purgatorySize ; i++) {
1946 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1947 layer->shortDump(result, buffer, SIZE);
1948 }
1949
1950 /*
1951 * Dump SurfaceFlinger global state
1952 */
1953
1954 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1955 result.append(buffer);
1956
Mathias Agopian888c8222012-08-04 21:10:38 -07001957 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001958 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001959 const GLExtensions& extensions(GLExtensions::getInstance());
1960 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1961 extensions.getVendor(),
1962 extensions.getRenderer(),
1963 extensions.getVersion());
1964 result.append(buffer);
1965
1966 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001967 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001968 result.append(buffer);
1969
1970 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1971 result.append(buffer);
1972
Mathias Agopian42977342012-08-05 00:40:46 -07001973 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001974 snprintf(buffer, SIZE,
1975 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001976 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001977 result.append(buffer);
1978 snprintf(buffer, SIZE,
1979 " last eglSwapBuffers() time: %f us\n"
1980 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001981 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001982 " refresh-rate : %f fps\n"
1983 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001984 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001985 mLastSwapBufferTime/1000.0,
1986 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001987 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001988 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001989 hwc.getDpiX(),
1990 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001991 result.append(buffer);
1992
1993 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1994 inSwapBuffersDuration/1000.0);
1995 result.append(buffer);
1996
1997 snprintf(buffer, SIZE, " transaction time: %f us\n",
1998 inTransactionDuration/1000.0);
1999 result.append(buffer);
2000
2001 /*
2002 * VSYNC state
2003 */
2004 mEventThread->dump(result, buffer, SIZE);
2005
2006 /*
2007 * Dump HWComposer state
2008 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002009 snprintf(buffer, SIZE, "h/w composer state:\n");
2010 result.append(buffer);
2011 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2012 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2013 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2014 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002015 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002016
2017 /*
2018 * Dump gralloc state
2019 */
2020 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2021 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002022 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002023}
2024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025status_t SurfaceFlinger::onTransact(
2026 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2027{
2028 switch (code) {
2029 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002030 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002031 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002032 case BLANK:
2033 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002034 {
2035 // codes that require permission check
2036 IPCThreadState* ipc = IPCThreadState::self();
2037 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002038 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002039 if ((uid != AID_GRAPHICS) &&
2040 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002041 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002042 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2043 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002044 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002045 break;
2046 }
2047 case CAPTURE_SCREEN:
2048 {
2049 // codes that require permission check
2050 IPCThreadState* ipc = IPCThreadState::self();
2051 const int pid = ipc->getCallingPid();
2052 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002053 if ((uid != AID_GRAPHICS) &&
2054 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002055 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002056 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2057 return PERMISSION_DENIED;
2058 }
2059 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060 }
2061 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002063 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2064 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002065 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002066 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002067 IPCThreadState* ipc = IPCThreadState::self();
2068 const int pid = ipc->getCallingPid();
2069 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002070 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002071 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072 return PERMISSION_DENIED;
2073 }
2074 int n;
2075 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002076 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002077 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002078 return NO_ERROR;
2079 case 1002: // SHOW_UPDATES
2080 n = data.readInt32();
2081 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002082 invalidateHwcGeometry();
2083 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002086 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002087 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002088 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002089 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002090 setTransactionFlags(
2091 eTransactionNeeded|
2092 eDisplayTransactionNeeded|
2093 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002094 return NO_ERROR;
2095 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002096 case 1006:{ // send empty update
2097 signalRefresh();
2098 return NO_ERROR;
2099 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002100 case 1008: // toggle use of hw composer
2101 n = data.readInt32();
2102 mDebugDisableHWC = n ? 1 : 0;
2103 invalidateHwcGeometry();
2104 repaintEverything();
2105 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002106 case 1009: // toggle use of transform hint
2107 n = data.readInt32();
2108 mDebugDisableTransformHint = n ? 1 : 0;
2109 invalidateHwcGeometry();
2110 repaintEverything();
2111 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002112 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002113 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114 reply->writeInt32(0);
2115 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002116 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002117 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002118 return NO_ERROR;
2119 case 1013: {
2120 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002121 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2122 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 }
2124 return NO_ERROR;
2125 }
2126 }
2127 return err;
2128}
2129
Mathias Agopian53331da2011-08-22 21:44:41 -07002130void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002131 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002132 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002133}
2134
Mathias Agopian59119e62010-10-11 12:37:43 -07002135// ---------------------------------------------------------------------------
2136
Mathias Agopian118d0242011-10-13 16:02:48 -07002137status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2138 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2139{
2140 Mutex::Autolock _l(mStateLock);
2141 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2142}
2143
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002144status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2145 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002146{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002147 ATRACE_CALL();
2148
Mathias Agopian59119e62010-10-11 12:37:43 -07002149 if (!GLExtensions::getInstance().haveFramebufferObject())
2150 return INVALID_OPERATION;
2151
2152 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002153 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2154 const uint32_t hw_w = hw->getWidth();
2155 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002156 GLfloat u = 1;
2157 GLfloat v = 1;
2158
2159 // make sure to clear all GL error flags
2160 while ( glGetError() != GL_NO_ERROR ) ;
2161
2162 // create a FBO
2163 GLuint name, tname;
2164 glGenTextures(1, &tname);
2165 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002166 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2167 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2169 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002170 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002171 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002172 GLint tw = (2 << (31 - clz(hw_w)));
2173 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002174 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2175 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002176 u = GLfloat(hw_w) / tw;
2177 v = GLfloat(hw_h) / th;
2178 }
2179 glGenFramebuffersOES(1, &name);
2180 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002181 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2182 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002183
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002184 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002185 glDisable(GL_TEXTURE_EXTERNAL_OES);
2186 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002187 glClearColor(0,0,0,1);
2188 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002189 glMatrixMode(GL_MODELVIEW);
2190 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002191 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002192 const size_t count = layers.size();
2193 for (size_t i=0 ; i<count ; ++i) {
2194 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002195 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002196 }
2197
Mathias Agopian42977342012-08-05 00:40:46 -07002198 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002199
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 // back to main framebuffer
2201 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002202 glDeleteFramebuffersOES(1, &name);
2203
2204 *textureName = tname;
2205 *uOut = u;
2206 *vOut = v;
2207 return NO_ERROR;
2208}
2209
2210// ---------------------------------------------------------------------------
2211
Mathias Agopian74c40c02010-09-29 13:02:36 -07002212status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2213 sp<IMemoryHeap>* heap,
2214 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002215 uint32_t sw, uint32_t sh,
2216 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002218 ATRACE_CALL();
2219
Mathias Agopian74c40c02010-09-29 13:02:36 -07002220 status_t result = PERMISSION_DENIED;
2221
2222 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002223 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002224 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002225 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002226 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002227
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002228 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002229 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002230 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002231
2232 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002233 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2234 const uint32_t hw_w = hw->getWidth();
2235 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002237 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002238 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002239 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002240 return PERMISSION_DENIED;
2241 }
2242
2243 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002244 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002245 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002246 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002247
2248 sw = (!sw) ? hw_w : sw;
2249 sh = (!sh) ? hw_h : sh;
2250 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002251 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002253// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2254// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002255
Mathias Agopian74c40c02010-09-29 13:02:36 -07002256 // make sure to clear all GL error flags
2257 while ( glGetError() != GL_NO_ERROR ) ;
2258
2259 // create a FBO
2260 GLuint name, tname;
2261 glGenRenderbuffersOES(1, &tname);
2262 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2263 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002264
Mathias Agopian74c40c02010-09-29 13:02:36 -07002265 glGenFramebuffersOES(1, &name);
2266 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2267 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2268 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2269
2270 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002271
Mathias Agopian74c40c02010-09-29 13:02:36 -07002272 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2273
2274 // invert everything, b/c glReadPixel() below will invert the FB
2275 glViewport(0, 0, sw, sh);
2276 glMatrixMode(GL_PROJECTION);
2277 glPushMatrix();
2278 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002279 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280 glMatrixMode(GL_MODELVIEW);
2281
2282 // redraw the screen entirely...
2283 glClearColor(0,0,0,1);
2284 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002285
Jamie Gennis9575f602011-10-07 14:51:16 -07002286 const LayerVector& layers(mDrawingState.layersSortedByZ);
2287 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002288 for (size_t i=0 ; i<count ; ++i) {
2289 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002290 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002291 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002292 const uint32_t z = layer->drawingState().z;
2293 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002294 if (filtering) layer->setFiltering(true);
2295 layer->draw(hw);
2296 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002297 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002298 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002299 }
2300
Mathias Agopian74c40c02010-09-29 13:02:36 -07002301 // check for errors and return screen capture
2302 if (glGetError() != GL_NO_ERROR) {
2303 // error while rendering
2304 result = INVALID_OPERATION;
2305 } else {
2306 // allocate shared memory large enough to hold the
2307 // screen capture
2308 sp<MemoryHeapBase> base(
2309 new MemoryHeapBase(size, 0, "screen-capture") );
2310 void* const ptr = base->getBase();
2311 if (ptr) {
2312 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002313 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2315 if (glGetError() == GL_NO_ERROR) {
2316 *heap = base;
2317 *w = sw;
2318 *h = sh;
2319 *f = PIXEL_FORMAT_RGBA_8888;
2320 result = NO_ERROR;
2321 }
2322 } else {
2323 result = NO_MEMORY;
2324 }
2325 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002326 glViewport(0, 0, hw_w, hw_h);
2327 glMatrixMode(GL_PROJECTION);
2328 glPopMatrix();
2329 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002330 } else {
2331 result = BAD_VALUE;
2332 }
2333
2334 // release FBO resources
2335 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2336 glDeleteRenderbuffersOES(1, &tname);
2337 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002338
Mathias Agopian42977342012-08-05 00:40:46 -07002339 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002340
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002341// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002342
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 return result;
2344}
2345
2346
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002347status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2348 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002350 uint32_t sw, uint32_t sh,
2351 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002352{
2353 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002354 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002355 return BAD_VALUE;
2356
2357 if (!GLExtensions::getInstance().haveFramebufferObject())
2358 return INVALID_OPERATION;
2359
2360 class MessageCaptureScreen : public MessageBase {
2361 SurfaceFlinger* flinger;
2362 DisplayID dpy;
2363 sp<IMemoryHeap>* heap;
2364 uint32_t* w;
2365 uint32_t* h;
2366 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002367 uint32_t sw;
2368 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002369 uint32_t minLayerZ;
2370 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002371 status_t result;
2372 public:
2373 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002375 uint32_t sw, uint32_t sh,
2376 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002377 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002378 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2379 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2380 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002381 {
2382 }
2383 status_t getResult() const {
2384 return result;
2385 }
2386 virtual bool handler() {
2387 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002389 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002390 return true;
2391 }
2392 };
2393
2394 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002395 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002396 status_t res = postMessageSync(msg);
2397 if (res == NO_ERROR) {
2398 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2399 }
2400 return res;
2401}
2402
2403// ---------------------------------------------------------------------------
2404
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002405SurfaceFlinger::LayerVector::LayerVector() {
2406}
2407
2408SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2409 : SortedVector<sp<LayerBase> >(rhs) {
2410}
2411
2412int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2413 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002414{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002415 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002416 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2417 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002418
2419 uint32_t ls = l->currentState().layerStack;
2420 uint32_t rs = r->currentState().layerStack;
2421 if (ls != rs)
2422 return ls - rs;
2423
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002424 uint32_t lz = l->currentState().z;
2425 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002426 if (lz != rz)
2427 return lz - rz;
2428
2429 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002430}
2431
2432// ---------------------------------------------------------------------------
2433
Mathias Agopiane57f2922012-08-09 16:29:12 -07002434SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2435}
2436
2437SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2438 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002439}
2440
2441// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002442
Jamie Gennis9a78c902011-01-12 18:30:40 -08002443GraphicBufferAlloc::GraphicBufferAlloc() {}
2444
2445GraphicBufferAlloc::~GraphicBufferAlloc() {}
2446
2447sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002448 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002449 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2450 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002451 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002452 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002453 if (err == NO_MEMORY) {
2454 GraphicBuffer::dumpAllocationsToSystemLog();
2455 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002456 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002457 "failed (%s), handle=%p",
2458 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002459 return 0;
2460 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002461 return graphicBuffer;
2462}
2463
Jamie Gennis9a78c902011-01-12 18:30:40 -08002464// ---------------------------------------------------------------------------
2465
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002466}; // namespace android