blob: 2b9ae06f2a54627617b888fcb7eadbcb949a8a1d [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
Mathias Agopian3559b072012-08-15 13:46:03 -0700894 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700896 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
898 if (!trFlags) continue;
899
900 const uint32_t flags = layer->doTransaction(0);
901 if (flags & Layer::eVisibleRegion)
902 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 }
904 }
905
906 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700907 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 */
909
Mathias Agopiane57f2922012-08-09 16:29:12 -0700910 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700911 // here we take advantage of Vector's copy-on-write semantics to
912 // improve performance by skipping the transaction entirely when
913 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700914 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
915 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700916 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700918 const size_t cc = curr.size();
919 const size_t dc = draw.size();
920
921 // find the displays that were removed
922 // (ie: in drawing state but not in current state)
923 // also handle displays that changed
924 // (ie: displays that are in both lists)
925 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700926 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
927 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700928 // in drawing state but not in current state
929 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700930 mDisplays.removeItem(draw[i].id);
931 } else {
932 ALOGW("trying to remove the main display");
933 }
934 } else {
935 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700936 const DisplayDeviceState& state(curr[j]);
937 if (state.surface != draw[i].surface) {
938 // changing the surface is like destroying and
939 // recreating the DisplayDevice
940
941 sp<SurfaceTextureClient> stc(
942 new SurfaceTextureClient(state.surface));
943
944 sp<DisplayDevice> disp = new DisplayDevice(this,
945 state.id, stc, 0, mEGLConfig);
946
947 disp->setLayerStack(state.layerStack);
948 disp->setOrientation(state.orientation);
949 // TODO: take viewport and frame into account
950 mDisplays.replaceValueFor(state.id, disp);
951 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700952 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700953 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700954 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700955 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700956 if (state.orientation != draw[i].orientation ||
957 state.viewport != draw[i].viewport ||
958 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700959 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
960 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700961 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700962 }
963 }
964 }
965
966 // find displays that were added
967 // (ie: in current state but not in drawing state)
968 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700969 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 // FIXME: we need to pass the surface here
Mathias Agopiane57f2922012-08-09 16:29:12 -0700971 const DisplayDeviceState& state(curr[i]);
972 sp<SurfaceTextureClient> stc(
973 new SurfaceTextureClient(state.surface));
974 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
975 stc, 0, mEGLConfig);
976 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700977 }
978 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 }
Mathias Agopian3559b072012-08-15 13:46:03 -0700980 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981
Mathias Agopian3559b072012-08-15 13:46:03 -0700982 /*
983 * Perform our own transaction if needed
984 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700985
Mathias Agopian3559b072012-08-15 13:46:03 -0700986 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
987 // layers have been added
988 mVisibleRegionsDirty = true;
989 }
990
991 // some layers might have been removed, so
992 // we need to update the regions they're exposing.
993 if (mLayersRemoved) {
994 mLayersRemoved = false;
995 mVisibleRegionsDirty = true;
996 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
997 const size_t count = previousLayers.size();
998 for (size_t i=0 ; i<count ; i++) {
999 const sp<LayerBase>& layer(previousLayers[i]);
1000 if (currentLayers.indexOf(layer) < 0) {
1001 // this layer is not visible anymore
1002 // TODO: we could traverse the tree from front to back and
1003 // compute the actual visible region
1004 // TODO: we could cache the transformed region
1005 Layer::State front(layer->drawingState());
1006 Region visibleReg = front.transform.transform(
1007 Region(Rect(front.active.w, front.active.h)));
1008 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001009 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 }
1012
1013 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001014}
1015
1016void SurfaceFlinger::commitTransaction()
1017{
1018 if (!mLayersPendingRemoval.isEmpty()) {
1019 // Notify removed layers now that they can't be drawn from
1020 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1021 mLayersPendingRemoval[i]->onRemoved();
1022 }
1023 mLayersPendingRemoval.clear();
1024 }
1025
1026 mDrawingState = mCurrentState;
1027 mTransationPending = false;
1028 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029}
1030
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001032 const LayerVector& currentLayers, uint32_t layerStack,
1033 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034{
Mathias Agopian841cde52012-03-01 15:44:37 -08001035 ATRACE_CALL();
1036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 Region aboveOpaqueLayers;
1038 Region aboveCoveredLayers;
1039 Region dirty;
1040
Mathias Agopian87baae12012-07-31 12:38:26 -07001041 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042
1043 size_t i = currentLayers.size();
1044 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001045 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046
1047 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001048 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049
Mathias Agopian87baae12012-07-31 12:38:26 -07001050 // only consider the layers on the given later stack
1051 if (s.layerStack != layerStack)
1052 continue;
1053
Mathias Agopianab028732010-03-16 16:41:46 -07001054 /*
1055 * opaqueRegion: area of a surface that is fully opaque.
1056 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001058
1059 /*
1060 * visibleRegion: area of a surface that is visible on screen
1061 * and not fully transparent. This is essentially the layer's
1062 * footprint minus the opaque regions above it.
1063 * Areas covered by a translucent surface are considered visible.
1064 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001066
1067 /*
1068 * coveredRegion: area of a surface that is covered by all
1069 * visible regions above it (which includes the translucent areas).
1070 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001072
1073
1074 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001075 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001076 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001077 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001079 if (!visibleRegion.isEmpty()) {
1080 // Remove the transparent area from the visible region
1081 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001082 Region transparentRegionScreen;
1083 const Transform tr(s.transform);
1084 if (tr.transformed()) {
1085 if (tr.preserveRects()) {
1086 // transform the transparent region
1087 transparentRegionScreen = tr.transform(s.transparentRegion);
1088 } else {
1089 // transformation too complex, can't do the
1090 // transparent region optimization.
1091 transparentRegionScreen.clear();
1092 }
1093 } else {
1094 transparentRegionScreen = s.transparentRegion;
1095 }
1096 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001097 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098
Mathias Agopianab028732010-03-16 16:41:46 -07001099 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001100 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001101 if (s.alpha==255 && !translucent &&
1102 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1103 // the opaque region is the layer's footprint
1104 opaqueRegion = visibleRegion;
1105 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 }
1107 }
1108
Mathias Agopianab028732010-03-16 16:41:46 -07001109 // Clip the covered region to the visible region
1110 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1111
1112 // Update aboveCoveredLayers for next (lower) layer
1113 aboveCoveredLayers.orSelf(visibleRegion);
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 // subtract the opaque region covered by the layers above us
1116 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117
1118 // compute this layer's dirty region
1119 if (layer->contentDirty) {
1120 // we need to invalidate the whole region
1121 dirty = visibleRegion;
1122 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001123 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 layer->contentDirty = false;
1125 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001126 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001127 * the exposed region consists of two components:
1128 * 1) what's VISIBLE now and was COVERED before
1129 * 2) what's EXPOSED now less what was EXPOSED before
1130 *
1131 * note that (1) is conservative, we start with the whole
1132 * visible region but only keep what used to be covered by
1133 * something -- which mean it may have been exposed.
1134 *
1135 * (2) handles areas that were not covered by anything but got
1136 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001137 */
Mathias Agopianab028732010-03-16 16:41:46 -07001138 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001139 const Region oldVisibleRegion = layer->visibleRegion;
1140 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001141 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1142 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 }
1144 dirty.subtractSelf(aboveOpaqueLayers);
1145
1146 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001147 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148
Mathias Agopianab028732010-03-16 16:41:46 -07001149 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 // Store the visible region is screen space
1153 layer->setVisibleRegion(visibleRegion);
1154 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155 }
1156
Mathias Agopian87baae12012-07-31 12:38:26 -07001157 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158}
1159
Mathias Agopian87baae12012-07-31 12:38:26 -07001160void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1161 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001162 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001163 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1164 if (hw->getLayerStack() == layerStack) {
1165 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001166 }
1167 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001168}
1169
1170void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001172 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001173 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001175 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001176
Mathias Agopian4fec8732012-06-29 14:12:52 -07001177 bool visibleRegions = false;
1178 const size_t count = currentLayers.size();
1179 sp<LayerBase> const* layers = currentLayers.array();
1180 for (size_t i=0 ; i<count ; i++) {
1181 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001182 const Region dirty(layer->latchBuffer(visibleRegions));
1183 Layer::State s(layer->drawingState());
1184 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001185 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001186
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001187 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188}
1189
Mathias Agopianad456f92011-01-13 17:53:01 -08001190void SurfaceFlinger::invalidateHwcGeometry()
1191{
1192 mHwWorkListDirty = true;
1193}
1194
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001195void SurfaceFlinger::handleRefresh()
1196{
1197 bool needInvalidate = false;
1198 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1199 const size_t count = currentLayers.size();
1200 for (size_t i=0 ; i<count ; i++) {
1201 const sp<LayerBase>& layer(currentLayers[i]);
1202 if (layer->onPreComposition()) {
1203 needInvalidate = true;
1204 }
1205 }
1206 if (needInvalidate) {
1207 signalLayerUpdate();
1208 }
1209}
1210
Mathias Agopian42977342012-08-05 00:40:46 -07001211void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001212 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213{
Mathias Agopian841cde52012-03-01 15:44:37 -08001214 ATRACE_CALL();
1215
Mathias Agopian87baae12012-07-31 12:38:26 -07001216 Region dirtyRegion(inDirtyRegion);
1217
Mathias Agopianb8a55602009-06-26 19:06:36 -07001218 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001219 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220
Glenn Kasten99ed2242011-12-15 09:51:17 -08001221 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001222 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 }
1224
Mathias Agopian42977342012-08-05 00:40:46 -07001225 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001226 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001227 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1228 // takes a rectangle, we must make sure to update that whole
1229 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001230 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001231 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001232 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001233 // We need to redraw the rectangle that will be updated
1234 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001235 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001236 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001237 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001238 } else {
1239 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001240 dirtyRegion.set(hw->bounds());
1241 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 }
1243 }
1244
Mathias Agopian87baae12012-07-31 12:38:26 -07001245 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001247 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1248 const size_t count = currentLayers.size();
1249 for (size_t i=0 ; i<count ; i++) {
1250 currentLayers[i]->onPostComposition();
1251 }
1252
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001253 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001254 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255}
1256
Mathias Agopian42977342012-08-05 00:40:46 -07001257void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001258{
Mathias Agopian86303202012-07-24 22:46:10 -07001259 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001260 int32_t id = hw->getDisplayId();
1261 HWComposer::LayerListIterator cur = hwc.begin(id);
1262 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001263
Mathias Agopian1e260872012-08-08 18:35:12 -07001264 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001265 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001266
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001267 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001268
1269 // set the frame buffer
1270 glMatrixMode(GL_MODELVIEW);
1271 glLoadIdentity();
1272
1273 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001274 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001275 // when using overlays, we assume a fully transparent framebuffer
1276 // NOTE: we could reduce how much we need to clear, for instance
1277 // remove where there are opaque FB layers. however, on some
1278 // GPUs doing a "clean slate" glClear might be more efficient.
1279 // We'll revisit later if needed.
1280 glClearColor(0, 0, 0, 0);
1281 glClear(GL_COLOR_BUFFER_BIT);
1282 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001283 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001284 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001285 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001286 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001287 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001288 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001289 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001290
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001291 /*
1292 * and then, render the layers targeted at the framebuffer
1293 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001294
Mathias Agopian42977342012-08-05 00:40:46 -07001295 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001296 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001297 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001298 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001299 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001300 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001301 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001302 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001303 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001304 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001305 // never clear the very first layer since we're
1306 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001307 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001308 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001309 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001310 continue;
1311 }
1312 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001313 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001314 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001315 if (cur != end) {
1316 ++cur;
1317 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001318 }
1319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320}
1321
Mathias Agopian42977342012-08-05 00:40:46 -07001322void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001323 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324{
Mathias Agopian42977342012-08-05 00:40:46 -07001325 const uint32_t flags = hw->getFlags();
1326 const int32_t height = hw->getHeight();
1327 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001328 return;
1329 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001330
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001331 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1332 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001333 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001334 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001335 }
1336
Mathias Agopianc492e672011-10-18 14:49:27 -07001337 glDisable(GL_TEXTURE_EXTERNAL_OES);
1338 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340
Mathias Agopian0926f502009-05-04 14:17:04 -07001341 static int toggle = 0;
1342 toggle = 1 - toggle;
1343 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001344 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001345 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001346 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348
Mathias Agopian87baae12012-07-31 12:38:26 -07001349 Region::const_iterator it = dirtyRegion.begin();
1350 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001351 while (it != end) {
1352 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001354 { r.left, height - r.top },
1355 { r.left, height - r.bottom },
1356 { r.right, height - r.bottom },
1357 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 };
1359 glVertexPointer(2, GL_FLOAT, 0, vertices);
1360 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1361 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001362
Mathias Agopian42977342012-08-05 00:40:46 -07001363 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364
1365 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001366 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367}
1368
Mathias Agopian87baae12012-07-31 12:38:26 -07001369void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001371 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001372 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001373 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001374 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001375
1376 GLfloat vertices[4][2];
1377 glVertexPointer(2, GL_FLOAT, 0, vertices);
1378 Region::const_iterator it = region.begin();
1379 Region::const_iterator const end = region.end();
1380 while (it != end) {
1381 const Rect& r = *it++;
1382 vertices[0][0] = r.left;
1383 vertices[0][1] = r.top;
1384 vertices[1][0] = r.right;
1385 vertices[1][1] = r.top;
1386 vertices[2][0] = r.right;
1387 vertices[2][1] = r.bottom;
1388 vertices[3][0] = r.left;
1389 vertices[3][1] = r.bottom;
1390 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1391 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392}
1393
Mathias Agopian96f08192010-06-02 23:28:45 -07001394ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1395 const sp<LayerBaseClient>& lbc)
1396{
Mathias Agopian96f08192010-06-02 23:28:45 -07001397 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001398 size_t name = client->attachLayer(lbc);
1399
Mathias Agopian96f08192010-06-02 23:28:45 -07001400 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001401 Mutex::Autolock _l(mStateLock);
1402 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001403
Mathias Agopian4f113742011-05-03 16:21:41 -07001404 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001405}
1406
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001407status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408{
1409 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001410 status_t err = purgatorizeLayer_l(layer);
1411 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001412 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001413 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414}
1415
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001416status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417{
1418 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1419 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001420 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 return NO_ERROR;
1422 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001423 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424}
1425
Mathias Agopian9a112062009-04-17 19:36:26 -07001426status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1427{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001428 // First add the layer to the purgatory list, which makes sure it won't
1429 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001430 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001431 if (err >= 0) {
1432 mLayerPurgatory.add(layerBase);
1433 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001434
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001435 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001436
Mathias Agopian3d579642009-06-04 18:46:21 -07001437 // it's possible that we don't find a layer, because it might
1438 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001439 // from the user because there is a race between Client::destroySurface(),
1440 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001441 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1442}
1443
Mathias Agopiandea20b12011-05-03 17:04:02 -07001444uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1445{
1446 return android_atomic_release_load(&mTransactionFlags);
1447}
1448
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1450{
1451 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1452}
1453
Mathias Agopianbb641242010-05-18 17:06:55 -07001454uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455{
1456 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1457 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001458 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 }
1460 return old;
1461}
1462
Mathias Agopian8b33f032012-07-24 20:43:54 -07001463void SurfaceFlinger::setTransactionState(
1464 const Vector<ComposerState>& state,
1465 const Vector<DisplayState>& displays,
1466 uint32_t flags)
1467{
Mathias Agopian698c0872011-06-28 19:09:31 -07001468 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001469 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001470
1471 size_t count = displays.size();
1472 for (size_t i=0 ; i<count ; i++) {
1473 const DisplayState& s(displays[i]);
1474 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001475 }
1476
Mathias Agopiane57f2922012-08-09 16:29:12 -07001477 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001478 for (size_t i=0 ; i<count ; i++) {
1479 const ComposerState& s(state[i]);
1480 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001481 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001482 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001483
Mathias Agopian386aa982011-11-07 21:58:03 -08001484 if (transactionFlags) {
1485 // this triggers the transaction
1486 setTransactionFlags(transactionFlags);
1487
1488 // if this is a synchronous transaction, wait for it to take effect
1489 // before returning.
1490 if (flags & eSynchronous) {
1491 mTransationPending = true;
1492 }
1493 while (mTransationPending) {
1494 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1495 if (CC_UNLIKELY(err != NO_ERROR)) {
1496 // just in case something goes wrong in SF, return to the
1497 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001498 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001499 mTransationPending = false;
1500 break;
1501 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001502 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 }
1504}
1505
Mathias Agopiane57f2922012-08-09 16:29:12 -07001506uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1507{
1508 uint32_t flags = 0;
1509 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1510 if (disp.id >= 0) {
1511 const uint32_t what = s.what;
1512 if (what & DisplayState::eSurfaceChanged) {
1513 if (disp.surface->asBinder() != s.surface->asBinder()) {
1514 disp.surface = s.surface;
1515 flags |= eDisplayTransactionNeeded;
1516 }
1517 }
1518 if (what & DisplayState::eLayerStackChanged) {
1519 if (disp.layerStack != s.layerStack) {
1520 disp.layerStack = s.layerStack;
1521 flags |= eDisplayTransactionNeeded;
1522 }
1523 }
1524 if (what & DisplayState::eTransformChanged) {
1525 if (disp.orientation != s.orientation) {
1526 disp.orientation = s.orientation;
1527 flags |= eDisplayTransactionNeeded;
1528 }
1529 if (disp.frame != s.frame) {
1530 disp.frame = s.frame;
1531 flags |= eDisplayTransactionNeeded;
1532 }
1533 if (disp.viewport != s.viewport) {
1534 disp.viewport = s.viewport;
1535 flags |= eDisplayTransactionNeeded;
1536 }
1537 }
1538 }
1539 return flags;
1540}
1541
1542uint32_t SurfaceFlinger::setClientStateLocked(
1543 const sp<Client>& client,
1544 const layer_state_t& s)
1545{
1546 uint32_t flags = 0;
1547 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1548 if (layer != 0) {
1549 const uint32_t what = s.what;
1550 if (what & layer_state_t::ePositionChanged) {
1551 if (layer->setPosition(s.x, s.y))
1552 flags |= eTraversalNeeded;
1553 }
1554 if (what & layer_state_t::eLayerChanged) {
1555 // NOTE: index needs to be calculated before we update the state
1556 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1557 if (layer->setLayer(s.z)) {
1558 mCurrentState.layersSortedByZ.removeAt(idx);
1559 mCurrentState.layersSortedByZ.add(layer);
1560 // we need traversal (state changed)
1561 // AND transaction (list changed)
1562 flags |= eTransactionNeeded|eTraversalNeeded;
1563 }
1564 }
1565 if (what & layer_state_t::eSizeChanged) {
1566 if (layer->setSize(s.w, s.h)) {
1567 flags |= eTraversalNeeded;
1568 }
1569 }
1570 if (what & layer_state_t::eAlphaChanged) {
1571 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1572 flags |= eTraversalNeeded;
1573 }
1574 if (what & layer_state_t::eMatrixChanged) {
1575 if (layer->setMatrix(s.matrix))
1576 flags |= eTraversalNeeded;
1577 }
1578 if (what & layer_state_t::eTransparentRegionChanged) {
1579 if (layer->setTransparentRegionHint(s.transparentRegion))
1580 flags |= eTraversalNeeded;
1581 }
1582 if (what & layer_state_t::eVisibilityChanged) {
1583 if (layer->setFlags(s.flags, s.mask))
1584 flags |= eTraversalNeeded;
1585 }
1586 if (what & layer_state_t::eCropChanged) {
1587 if (layer->setCrop(s.crop))
1588 flags |= eTraversalNeeded;
1589 }
1590 if (what & layer_state_t::eLayerStackChanged) {
1591 // NOTE: index needs to be calculated before we update the state
1592 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1593 if (layer->setLayerStack(s.layerStack)) {
1594 mCurrentState.layersSortedByZ.removeAt(idx);
1595 mCurrentState.layersSortedByZ.add(layer);
1596 // we need traversal (state changed)
1597 // AND transaction (list changed)
1598 flags |= eTransactionNeeded|eTraversalNeeded;
1599 }
1600 }
1601 }
1602 return flags;
1603}
1604
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001605sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001606 ISurfaceComposerClient::surface_data_t* params,
1607 const String8& name,
1608 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1610 uint32_t flags)
1611{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001612 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001613 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001614
1615 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001616 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001617 int(w), int(h));
1618 return surfaceHandle;
1619 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001620
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001621 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001622 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1623 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001624 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001626 case ISurfaceComposerClient::eFXSurfaceBlur:
1627 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001628 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001630 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001631 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001632 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 }
1634
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001635 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001636 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001637 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001638 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001640 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001641 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001642 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001643 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001644 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 }
1646
1647 return surfaceHandle;
1648}
1649
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001650sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001651 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001652 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001653 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654{
1655 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001656 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 case PIXEL_FORMAT_TRANSPARENT:
1658 case PIXEL_FORMAT_TRANSLUCENT:
1659 format = PIXEL_FORMAT_RGBA_8888;
1660 break;
1661 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001662#ifdef NO_RGBX_8888
1663 format = PIXEL_FORMAT_RGB_565;
1664#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001665 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001666#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 break;
1668 }
1669
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001670#ifdef NO_RGBX_8888
1671 if (format == PIXEL_FORMAT_RGBX_8888)
1672 format = PIXEL_FORMAT_RGBA_8888;
1673#endif
1674
Mathias Agopian96f08192010-06-02 23:28:45 -07001675 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001676 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001677 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001678 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001679 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 }
1681 return layer;
1682}
1683
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001684sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001685 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001686 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687{
Mathias Agopian96f08192010-06-02 23:28:45 -07001688 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001689 return layer;
1690}
1691
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001692sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001693 const sp<Client>& client, DisplayID display,
1694 uint32_t w, uint32_t h, uint32_t flags)
1695{
1696 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 return layer;
1698}
1699
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001700status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701{
Mathias Agopian9a112062009-04-17 19:36:26 -07001702 /*
1703 * called by the window manager, when a surface should be marked for
1704 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001705 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001706 * The surface is removed from the current and drawing lists, but placed
1707 * in the purgatory queue, so it's not destroyed right-away (we need
1708 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001709 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001710
Mathias Agopian48d819a2009-09-10 19:41:18 -07001711 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001712 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001713 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001714
Mathias Agopian48d819a2009-09-10 19:41:18 -07001715 if (layer != 0) {
1716 err = purgatorizeLayer_l(layer);
1717 if (err == NO_ERROR) {
Jeff Brown01eb9792012-08-15 01:08:54 -07001718 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001719 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001720 }
1721 return err;
1722}
1723
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001724status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001725{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001726 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001727 status_t err = NO_ERROR;
1728 sp<LayerBaseClient> l(layer.promote());
1729 if (l != NULL) {
1730 Mutex::Autolock _l(mStateLock);
1731 err = removeLayer_l(l);
1732 if (err == NAME_NOT_FOUND) {
1733 // The surface wasn't in the current list, which means it was
1734 // removed already, which means it is in the purgatory,
1735 // and need to be removed from there.
1736 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001737 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001738 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001739 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001740 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001741 "error removing layer=%p (%s)", l.get(), strerror(-err));
1742 }
1743 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744}
1745
Mathias Agopianb60314a2012-04-10 22:09:54 -07001746// ---------------------------------------------------------------------------
1747
1748void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001749 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001750 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001751 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001752 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001753 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001754 mVisibleRegionsDirty = true;
1755 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756}
1757
Mathias Agopianb60314a2012-04-10 22:09:54 -07001758void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001759 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001760 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1761 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001762 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001763 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001764 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001765 // from this point on, SF will stop drawing
1766 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767}
1768
Colin Cross8e533062012-06-07 13:17:52 -07001769void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001770 class MessageScreenAcquired : public MessageBase {
1771 SurfaceFlinger* flinger;
1772 public:
1773 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1774 virtual bool handler() {
1775 flinger->onScreenAcquired();
1776 return true;
1777 }
1778 };
1779 sp<MessageBase> msg = new MessageScreenAcquired(this);
1780 postMessageSync(msg);
1781}
1782
Colin Cross8e533062012-06-07 13:17:52 -07001783void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001784 class MessageScreenReleased : public MessageBase {
1785 SurfaceFlinger* flinger;
1786 public:
1787 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1788 virtual bool handler() {
1789 flinger->onScreenReleased();
1790 return true;
1791 }
1792 };
1793 sp<MessageBase> msg = new MessageScreenReleased(this);
1794 postMessageSync(msg);
1795}
1796
1797// ---------------------------------------------------------------------------
1798
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1800{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001801 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802 char buffer[SIZE];
1803 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001804
1805 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806 snprintf(buffer, SIZE, "Permission Denial: "
1807 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1808 IPCThreadState::self()->getCallingPid(),
1809 IPCThreadState::self()->getCallingUid());
1810 result.append(buffer);
1811 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001812 // Try to get the main lock, but don't insist if we can't
1813 // (this would indicate SF is stuck, but we want to be able to
1814 // print something in dumpsys).
1815 int retry = 3;
1816 while (mStateLock.tryLock()<0 && --retry>=0) {
1817 usleep(1000000);
1818 }
1819 const bool locked(retry >= 0);
1820 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001821 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001822 "SurfaceFlinger appears to be unresponsive, "
1823 "dumping anyways (no locks held)\n");
1824 result.append(buffer);
1825 }
1826
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001827 bool dumpAll = true;
1828 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001829 size_t numArgs = args.size();
1830 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001831 if ((index < numArgs) &&
1832 (args[index] == String16("--list"))) {
1833 index++;
1834 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001835 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001836 }
1837
1838 if ((index < numArgs) &&
1839 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001840 index++;
1841 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001842 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001843 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001844
1845 if ((index < numArgs) &&
1846 (args[index] == String16("--latency-clear"))) {
1847 index++;
1848 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001849 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001850 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001852
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001853 if (dumpAll) {
1854 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001855 }
1856
Mathias Agopian9795c422009-08-26 16:36:26 -07001857 if (locked) {
1858 mStateLock.unlock();
1859 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860 }
1861 write(fd, result.string(), result.size());
1862 return NO_ERROR;
1863}
1864
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001865void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1866 String8& result, char* buffer, size_t SIZE) const
1867{
1868 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1869 const size_t count = currentLayers.size();
1870 for (size_t i=0 ; i<count ; i++) {
1871 const sp<LayerBase>& layer(currentLayers[i]);
1872 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1873 result.append(buffer);
1874 }
1875}
1876
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001877void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1878 String8& result, char* buffer, size_t SIZE) const
1879{
1880 String8 name;
1881 if (index < args.size()) {
1882 name = String8(args[index]);
1883 index++;
1884 }
1885
1886 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1887 const size_t count = currentLayers.size();
1888 for (size_t i=0 ; i<count ; i++) {
1889 const sp<LayerBase>& layer(currentLayers[i]);
1890 if (name.isEmpty()) {
1891 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1892 result.append(buffer);
1893 }
1894 if (name.isEmpty() || (name == layer->getName())) {
1895 layer->dumpStats(result, buffer, SIZE);
1896 }
1897 }
1898}
1899
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001900void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1901 String8& result, char* buffer, size_t SIZE) const
1902{
1903 String8 name;
1904 if (index < args.size()) {
1905 name = String8(args[index]);
1906 index++;
1907 }
1908
1909 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1910 const size_t count = currentLayers.size();
1911 for (size_t i=0 ; i<count ; i++) {
1912 const sp<LayerBase>& layer(currentLayers[i]);
1913 if (name.isEmpty() || (name == layer->getName())) {
1914 layer->clearStats();
1915 }
1916 }
1917}
1918
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001919void SurfaceFlinger::dumpAllLocked(
1920 String8& result, char* buffer, size_t SIZE) const
1921{
1922 // figure out if we're stuck somewhere
1923 const nsecs_t now = systemTime();
1924 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1925 const nsecs_t inTransaction(mDebugInTransaction);
1926 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1927 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1928
1929 /*
1930 * Dump the visible layer list
1931 */
1932 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1933 const size_t count = currentLayers.size();
1934 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1935 result.append(buffer);
1936 for (size_t i=0 ; i<count ; i++) {
1937 const sp<LayerBase>& layer(currentLayers[i]);
1938 layer->dump(result, buffer, SIZE);
1939 }
1940
1941 /*
1942 * Dump the layers in the purgatory
1943 */
1944
1945 const size_t purgatorySize = mLayerPurgatory.size();
1946 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1947 result.append(buffer);
1948 for (size_t i=0 ; i<purgatorySize ; i++) {
1949 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1950 layer->shortDump(result, buffer, SIZE);
1951 }
1952
1953 /*
1954 * Dump SurfaceFlinger global state
1955 */
1956
1957 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1958 result.append(buffer);
1959
Mathias Agopian888c8222012-08-04 21:10:38 -07001960 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001961 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001962 const GLExtensions& extensions(GLExtensions::getInstance());
1963 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1964 extensions.getVendor(),
1965 extensions.getRenderer(),
1966 extensions.getVersion());
1967 result.append(buffer);
1968
1969 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001970 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001971 result.append(buffer);
1972
1973 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1974 result.append(buffer);
1975
Mathias Agopian42977342012-08-05 00:40:46 -07001976 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001977 snprintf(buffer, SIZE,
1978 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001979 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001980 result.append(buffer);
1981 snprintf(buffer, SIZE,
1982 " last eglSwapBuffers() time: %f us\n"
1983 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001984 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001985 " refresh-rate : %f fps\n"
1986 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001987 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001988 mLastSwapBufferTime/1000.0,
1989 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001990 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001991 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001992 hwc.getDpiX(),
1993 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001994 result.append(buffer);
1995
1996 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1997 inSwapBuffersDuration/1000.0);
1998 result.append(buffer);
1999
2000 snprintf(buffer, SIZE, " transaction time: %f us\n",
2001 inTransactionDuration/1000.0);
2002 result.append(buffer);
2003
2004 /*
2005 * VSYNC state
2006 */
2007 mEventThread->dump(result, buffer, SIZE);
2008
2009 /*
2010 * Dump HWComposer state
2011 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002012 snprintf(buffer, SIZE, "h/w composer state:\n");
2013 result.append(buffer);
2014 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2015 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2016 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2017 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002018 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002019
2020 /*
2021 * Dump gralloc state
2022 */
2023 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2024 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002025 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002026}
2027
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028status_t SurfaceFlinger::onTransact(
2029 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2030{
2031 switch (code) {
2032 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002033 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002034 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002035 case BLANK:
2036 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002037 {
2038 // codes that require permission check
2039 IPCThreadState* ipc = IPCThreadState::self();
2040 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002041 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002042 if ((uid != AID_GRAPHICS) &&
2043 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002044 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002045 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2046 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002047 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002048 break;
2049 }
2050 case CAPTURE_SCREEN:
2051 {
2052 // codes that require permission check
2053 IPCThreadState* ipc = IPCThreadState::self();
2054 const int pid = ipc->getCallingPid();
2055 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002056 if ((uid != AID_GRAPHICS) &&
2057 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002058 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002059 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2060 return PERMISSION_DENIED;
2061 }
2062 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002063 }
2064 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2067 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002068 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002069 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002070 IPCThreadState* ipc = IPCThreadState::self();
2071 const int pid = ipc->getCallingPid();
2072 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002073 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002074 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002075 return PERMISSION_DENIED;
2076 }
2077 int n;
2078 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002079 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002080 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002081 return NO_ERROR;
2082 case 1002: // SHOW_UPDATES
2083 n = data.readInt32();
2084 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002085 invalidateHwcGeometry();
2086 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002088 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002089 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002090 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002092 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002093 setTransactionFlags(
2094 eTransactionNeeded|
2095 eDisplayTransactionNeeded|
2096 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002097 return NO_ERROR;
2098 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002099 case 1006:{ // send empty update
2100 signalRefresh();
2101 return NO_ERROR;
2102 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002103 case 1008: // toggle use of hw composer
2104 n = data.readInt32();
2105 mDebugDisableHWC = n ? 1 : 0;
2106 invalidateHwcGeometry();
2107 repaintEverything();
2108 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002109 case 1009: // toggle use of transform hint
2110 n = data.readInt32();
2111 mDebugDisableTransformHint = n ? 1 : 0;
2112 invalidateHwcGeometry();
2113 repaintEverything();
2114 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002115 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002116 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002117 reply->writeInt32(0);
2118 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002119 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002120 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121 return NO_ERROR;
2122 case 1013: {
2123 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002124 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2125 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002126 }
2127 return NO_ERROR;
2128 }
2129 }
2130 return err;
2131}
2132
Mathias Agopian53331da2011-08-22 21:44:41 -07002133void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002134 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002135 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002136}
2137
Mathias Agopian59119e62010-10-11 12:37:43 -07002138// ---------------------------------------------------------------------------
2139
Mathias Agopian118d0242011-10-13 16:02:48 -07002140status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2141 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2142{
2143 Mutex::Autolock _l(mStateLock);
2144 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2145}
2146
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002147status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2148 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002149{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002150 ATRACE_CALL();
2151
Mathias Agopian59119e62010-10-11 12:37:43 -07002152 if (!GLExtensions::getInstance().haveFramebufferObject())
2153 return INVALID_OPERATION;
2154
2155 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002156 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2157 const uint32_t hw_w = hw->getWidth();
2158 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002159 GLfloat u = 1;
2160 GLfloat v = 1;
2161
2162 // make sure to clear all GL error flags
2163 while ( glGetError() != GL_NO_ERROR ) ;
2164
2165 // create a FBO
2166 GLuint name, tname;
2167 glGenTextures(1, &tname);
2168 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002169 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2170 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2172 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002173 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002174 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002175 GLint tw = (2 << (31 - clz(hw_w)));
2176 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2178 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002179 u = GLfloat(hw_w) / tw;
2180 v = GLfloat(hw_h) / th;
2181 }
2182 glGenFramebuffersOES(1, &name);
2183 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002184 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2185 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002186
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002187 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002188 glDisable(GL_TEXTURE_EXTERNAL_OES);
2189 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002190 glClearColor(0,0,0,1);
2191 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002192 glMatrixMode(GL_MODELVIEW);
2193 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002194 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002195 const size_t count = layers.size();
2196 for (size_t i=0 ; i<count ; ++i) {
2197 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002198 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002199 }
2200
Mathias Agopian42977342012-08-05 00:40:46 -07002201 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002202
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 // back to main framebuffer
2204 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 glDeleteFramebuffersOES(1, &name);
2206
2207 *textureName = tname;
2208 *uOut = u;
2209 *vOut = v;
2210 return NO_ERROR;
2211}
2212
2213// ---------------------------------------------------------------------------
2214
Mathias Agopian74c40c02010-09-29 13:02:36 -07002215status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2216 sp<IMemoryHeap>* heap,
2217 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002218 uint32_t sw, uint32_t sh,
2219 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002220{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002221 ATRACE_CALL();
2222
Mathias Agopian74c40c02010-09-29 13:02:36 -07002223 status_t result = PERMISSION_DENIED;
2224
2225 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002226 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002227 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002228 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002229 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002230
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002231 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002232 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002233 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234
2235 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002236 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2237 const uint32_t hw_w = hw->getWidth();
2238 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002239
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002240 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002241 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002242 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002243 return PERMISSION_DENIED;
2244 }
2245
2246 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002247 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002248 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002249 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002250
2251 sw = (!sw) ? hw_w : sw;
2252 sh = (!sh) ? hw_h : sh;
2253 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002254 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002255
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002256// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2257// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002258
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259 // make sure to clear all GL error flags
2260 while ( glGetError() != GL_NO_ERROR ) ;
2261
2262 // create a FBO
2263 GLuint name, tname;
2264 glGenRenderbuffersOES(1, &tname);
2265 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2266 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002267
Mathias Agopian74c40c02010-09-29 13:02:36 -07002268 glGenFramebuffersOES(1, &name);
2269 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2270 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2271 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2272
2273 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002274
Mathias Agopian74c40c02010-09-29 13:02:36 -07002275 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2276
2277 // invert everything, b/c glReadPixel() below will invert the FB
2278 glViewport(0, 0, sw, sh);
2279 glMatrixMode(GL_PROJECTION);
2280 glPushMatrix();
2281 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002282 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002283 glMatrixMode(GL_MODELVIEW);
2284
2285 // redraw the screen entirely...
2286 glClearColor(0,0,0,1);
2287 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002288
Jamie Gennis9575f602011-10-07 14:51:16 -07002289 const LayerVector& layers(mDrawingState.layersSortedByZ);
2290 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291 for (size_t i=0 ; i<count ; ++i) {
2292 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002293 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002294 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002295 const uint32_t z = layer->drawingState().z;
2296 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002297 if (filtering) layer->setFiltering(true);
2298 layer->draw(hw);
2299 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002300 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002301 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302 }
2303
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 // check for errors and return screen capture
2305 if (glGetError() != GL_NO_ERROR) {
2306 // error while rendering
2307 result = INVALID_OPERATION;
2308 } else {
2309 // allocate shared memory large enough to hold the
2310 // screen capture
2311 sp<MemoryHeapBase> base(
2312 new MemoryHeapBase(size, 0, "screen-capture") );
2313 void* const ptr = base->getBase();
2314 if (ptr) {
2315 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002316 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002317 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2318 if (glGetError() == GL_NO_ERROR) {
2319 *heap = base;
2320 *w = sw;
2321 *h = sh;
2322 *f = PIXEL_FORMAT_RGBA_8888;
2323 result = NO_ERROR;
2324 }
2325 } else {
2326 result = NO_MEMORY;
2327 }
2328 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 glViewport(0, 0, hw_w, hw_h);
2330 glMatrixMode(GL_PROJECTION);
2331 glPopMatrix();
2332 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002333 } else {
2334 result = BAD_VALUE;
2335 }
2336
2337 // release FBO resources
2338 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2339 glDeleteRenderbuffersOES(1, &tname);
2340 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002341
Mathias Agopian42977342012-08-05 00:40:46 -07002342 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002343
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002344// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002345
Mathias Agopian74c40c02010-09-29 13:02:36 -07002346 return result;
2347}
2348
2349
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002350status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2351 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002353 uint32_t sw, uint32_t sh,
2354 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002355{
2356 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002357 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002358 return BAD_VALUE;
2359
2360 if (!GLExtensions::getInstance().haveFramebufferObject())
2361 return INVALID_OPERATION;
2362
2363 class MessageCaptureScreen : public MessageBase {
2364 SurfaceFlinger* flinger;
2365 DisplayID dpy;
2366 sp<IMemoryHeap>* heap;
2367 uint32_t* w;
2368 uint32_t* h;
2369 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002370 uint32_t sw;
2371 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002372 uint32_t minLayerZ;
2373 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002374 status_t result;
2375 public:
2376 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002377 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002378 uint32_t sw, uint32_t sh,
2379 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002380 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002381 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2382 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2383 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002384 {
2385 }
2386 status_t getResult() const {
2387 return result;
2388 }
2389 virtual bool handler() {
2390 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002391 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002392 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002393 return true;
2394 }
2395 };
2396
2397 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002398 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002399 status_t res = postMessageSync(msg);
2400 if (res == NO_ERROR) {
2401 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2402 }
2403 return res;
2404}
2405
2406// ---------------------------------------------------------------------------
2407
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002408SurfaceFlinger::LayerVector::LayerVector() {
2409}
2410
2411SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2412 : SortedVector<sp<LayerBase> >(rhs) {
2413}
2414
2415int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2416 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002417{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002418 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002419 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2420 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002421
2422 uint32_t ls = l->currentState().layerStack;
2423 uint32_t rs = r->currentState().layerStack;
2424 if (ls != rs)
2425 return ls - rs;
2426
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002427 uint32_t lz = l->currentState().z;
2428 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002429 if (lz != rz)
2430 return lz - rz;
2431
2432 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002433}
2434
2435// ---------------------------------------------------------------------------
2436
Mathias Agopiane57f2922012-08-09 16:29:12 -07002437SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2438}
2439
2440SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2441 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002442}
2443
2444// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002445
Jamie Gennis9a78c902011-01-12 18:30:40 -08002446GraphicBufferAlloc::GraphicBufferAlloc() {}
2447
2448GraphicBufferAlloc::~GraphicBufferAlloc() {}
2449
2450sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002451 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002452 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2453 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002454 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002455 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002456 if (err == NO_MEMORY) {
2457 GraphicBuffer::dumpAllocationsToSystemLog();
2458 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002459 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002460 "failed (%s), handle=%p",
2461 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002462 return 0;
2463 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002464 return graphicBuffer;
2465}
2466
Jamie Gennis9a78c902011-01-12 18:30:40 -08002467// ---------------------------------------------------------------------------
2468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002469}; // namespace android