blob: 6542acd296586f97a0720246088aa1f908bac909 [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>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
26#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
40#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/SurfaceTextureClient.h>
42
43#include <ui/GraphicBufferAllocator.h>
44#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/String8.h>
47#include <utils/String16.h>
48#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian921e6ac2012-07-23 23:11:29 -070051#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
53#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080054#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070056#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080057#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070058#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070061#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Mathias Agopiana4912602012-07-12 14:25:33 -070064#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070065#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana67932f2011-04-20 14:20:59 -070067
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070087 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070090 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -070099 mBootFinished(false)
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) {
Keun young Park63f165f2012-08-31 10:53:36 -0700112 if (!startDdmConnection()) {
113 // start failed, and DDMS debugging not enabled
114 mDebugDDMS = 0;
115 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700117 ALOGI_IF(mDebugRegion, "showupdates enabled");
118 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121void SurfaceFlinger::onFirstRef()
122{
123 mEventQueue.init(this);
124
125 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
126
127 // Wait for the main thread to be done with its initialization
128 mReadyToRunBarrier.wait();
129}
130
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::~SurfaceFlinger()
133{
Mathias Agopiana4912602012-07-12 14:25:33 -0700134 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
135 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
136 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800139void SurfaceFlinger::binderDied(const wp<IBinder>& who)
140{
141 // the window manager died on us. prepare its eulogy.
142
Andy McFadden13a082e2012-08-24 10:16:42 -0700143 // restore initial conditions (default device unblank, etc)
144 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145
146 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700147 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148}
149
Mathias Agopian7e27f052010-05-28 14:22:23 -0700150sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Mathias Agopian96f08192010-06-02 23:28:45 -0700152 sp<ISurfaceComposerClient> bclient;
153 sp<Client> client(new Client(this));
154 status_t err = client->initCheck();
155 if (err == NO_ERROR) {
156 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 return bclient;
159}
160
Mathias Agopiane57f2922012-08-09 16:29:12 -0700161sp<IBinder> SurfaceFlinger::createDisplay()
162{
163 class DisplayToken : public BBinder {
164 sp<SurfaceFlinger> flinger;
165 virtual ~DisplayToken() {
166 // no more references, this display must be terminated
167 Mutex::Autolock _l(flinger->mStateLock);
168 flinger->mCurrentState.displays.removeItem(this);
169 flinger->setTransactionFlags(eDisplayTransactionNeeded);
170 }
171 public:
172 DisplayToken(const sp<SurfaceFlinger>& flinger)
173 : flinger(flinger) {
174 }
175 };
176
177 sp<BBinder> token = new DisplayToken(this);
178
179 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700180 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700181 mCurrentState.displays.add(token, info);
182
183 return token;
184}
185
186sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700187 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
189 return NULL;
190 }
191 return mDefaultDisplays[id];
192}
193
Jamie Gennis9a78c902011-01-12 18:30:40 -0800194sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
195{
196 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
197 return gba;
198}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200void SurfaceFlinger::bootFinished()
201{
202 const nsecs_t now = systemTime();
203 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000204 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700205 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700206
207 // wait patiently for the window manager death
208 const String16 name("window");
209 sp<IBinder> window(defaultServiceManager()->getService(name));
210 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700211 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700212 }
213
214 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700215 // formerly we would just kill the process, but we now ask it to exit so it
216 // can choose where to stop the animation.
217 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
219
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700220void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
221 class MessageDestroyGLTexture : public MessageBase {
222 GLuint texture;
223 public:
224 MessageDestroyGLTexture(GLuint texture)
225 : texture(texture) {
226 }
227 virtual bool handler() {
228 glDeleteTextures(1, &texture);
229 return true;
230 }
231 };
232 postMessageAsync(new MessageDestroyGLTexture(texture));
233}
234
Mathias Agopiana4912602012-07-12 14:25:33 -0700235status_t SurfaceFlinger::selectConfigForPixelFormat(
236 EGLDisplay dpy,
237 EGLint const* attrs,
238 PixelFormat format,
239 EGLConfig* outConfig)
240{
241 EGLConfig config = NULL;
242 EGLint numConfigs = -1, n=0;
243 eglGetConfigs(dpy, NULL, 0, &numConfigs);
244 EGLConfig* const configs = new EGLConfig[numConfigs];
245 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
246 for (int i=0 ; i<n ; i++) {
247 EGLint nativeVisualId = 0;
248 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
249 if (nativeVisualId>0 && format == nativeVisualId) {
250 *outConfig = configs[i];
251 delete [] configs;
252 return NO_ERROR;
253 }
254 }
255 delete [] configs;
256 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
Mathias Agopiana4912602012-07-12 14:25:33 -0700259EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
260 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
261 // it is to be used with WIFI displays
262 EGLConfig config;
263 EGLint dummy;
264 status_t err;
265 EGLint attribs[] = {
266 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
267 EGL_RECORDABLE_ANDROID, EGL_TRUE,
268 EGL_NONE
269 };
270 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
271 if (err) {
272 // maybe we failed because of EGL_RECORDABLE_ANDROID
273 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
274 attribs[2] = EGL_NONE;
275 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
276 }
277 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
278 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
279 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
280 }
281 return config;
282}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283
Mathias Agopiana4912602012-07-12 14:25:33 -0700284EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
285 // Also create our EGLContext
286 EGLint contextAttributes[] = {
287#ifdef EGL_IMG_context_priority
288#ifdef HAS_CONTEXT_PRIORITY
289#warning "using EGL_IMG_context_priority"
290 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
291#endif
292#endif
293 EGL_NONE, EGL_NONE
294 };
295 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
296 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
297 return ctxt;
298}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700300void SurfaceFlinger::initializeGL(EGLDisplay display, const sp<DisplayDevice>& hw) {
301 EGLBoolean result = DisplayDevice::makeCurrent(display, hw, mEGLContext);
Mathias Agopiana4912602012-07-12 14:25:33 -0700302 if (!result) {
303 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
304 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
306
Mathias Agopiana4912602012-07-12 14:25:33 -0700307 GLExtensions& extensions(GLExtensions::getInstance());
308 extensions.initWithGLStrings(
309 glGetString(GL_VENDOR),
310 glGetString(GL_RENDERER),
311 glGetString(GL_VERSION),
312 glGetString(GL_EXTENSIONS),
313 eglQueryString(display, EGL_VENDOR),
314 eglQueryString(display, EGL_VERSION),
315 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700316
Mathias Agopiana4912602012-07-12 14:25:33 -0700317 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
318 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700321 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 glShadeModel(GL_FLAT);
324 glDisable(GL_DITHER);
325 glDisable(GL_CULL_FACE);
326
Mathias Agopiana4912602012-07-12 14:25:33 -0700327 struct pack565 {
328 inline uint16_t operator() (int r, int g, int b) const {
329 return (r<<11)|(g<<5)|b;
330 }
331 } pack565;
332
Jamie Gennis9575f602011-10-07 14:51:16 -0700333 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
334 glGenTextures(1, &mProtectedTexName);
335 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
336 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
337 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
338 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
340 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
341 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343 // print some debugging info
344 EGLint r,g,b,a;
345 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
346 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
347 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
348 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
349 ALOGI("EGL informations:");
350 ALOGI("vendor : %s", extensions.getEglVendor());
351 ALOGI("version : %s", extensions.getEglVersion());
352 ALOGI("extensions: %s", extensions.getEglExtension());
353 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
354 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
355 ALOGI("OpenGL ES informations:");
356 ALOGI("vendor : %s", extensions.getVendor());
357 ALOGI("renderer : %s", extensions.getRenderer());
358 ALOGI("version : %s", extensions.getVersion());
359 ALOGI("extensions: %s", extensions.getExtension());
360 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
361 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
362}
363
Mathias Agopiana4912602012-07-12 14:25:33 -0700364status_t SurfaceFlinger::readyToRun()
365{
366 ALOGI( "SurfaceFlinger's main thread ready to run. "
367 "Initializing graphics H/W...");
368
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700369 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700370 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
371 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700372
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700373 // Initialize the H/W composer object. There may or may not be an
374 // actual hardware composer underneath.
375 mHwc = new HWComposer(this,
376 *static_cast<HWComposer::EventHandler *>(this));
377
Mathias Agopiana4912602012-07-12 14:25:33 -0700378 // Initialize the main display
379 // create native window to main display
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700380 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700381 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700382 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
383 exit(0);
384 }
385
Mathias Agopiane57f2922012-08-09 16:29:12 -0700386 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
387 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700388
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 // initialize the config and context
390 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700391 ANativeWindow* const anw = stc.get();
392 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700393 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
394 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700395
396 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700397
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700398 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700399 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700400 mCurrentState.displays.add(mDefaultDisplays[i],
401 DisplayDeviceState((DisplayDevice::DisplayType)i));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700402 }
403 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700404 DisplayDevice::DISPLAY_PRIMARY,
405 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiane60b0682012-08-21 23:34:09 -0700406 anw, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700407 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700408
409 // initialize OpenGL ES
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700410 initializeGL(mEGLDisplay, hw);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800411
Mathias Agopian028508c2012-07-25 21:12:12 -0700412 // start the EventThread
413 mEventThread = new EventThread(this);
414 mEventQueue.setEventThread(mEventThread);
415
Mathias Agopian92a979a2012-08-02 18:32:23 -0700416 // initialize our drawing state
417 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700418
Mathias Agopiana4912602012-07-12 14:25:33 -0700419 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800420 mReadyToRunBarrier.open();
421
Andy McFadden13a082e2012-08-24 10:16:42 -0700422 // set initial conditions (e.g. unblank default device)
423 initializeDisplays();
424
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700425 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700426 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 return NO_ERROR;
429}
430
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700431int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
432 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
433 type : mHwc->allocateDisplayId();
434}
435
Mathias Agopiana67e4182012-06-19 17:26:12 -0700436void SurfaceFlinger::startBootAnim() {
437 // start boot animation
438 property_set("service.bootanim.exit", "0");
439 property_set("ctl.start", "bootanim");
440}
441
Mathias Agopiana4912602012-07-12 14:25:33 -0700442uint32_t SurfaceFlinger::getMaxTextureSize() const {
443 return mMaxTextureSize;
444}
445
446uint32_t SurfaceFlinger::getMaxViewportDims() const {
447 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
448 mMaxViewportDims[0] : mMaxViewportDims[1];
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800452
Jamie Gennis582270d2011-08-17 18:19:00 -0700453bool SurfaceFlinger::authenticateSurfaceTexture(
454 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800455 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700456 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800457
458 // Check the visible layer list for the ISurface
459 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
460 size_t count = currentLayers.size();
461 for (size_t i=0 ; i<count ; i++) {
462 const sp<LayerBase>& layer(currentLayers[i]);
463 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700464 if (lbc != NULL) {
465 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
466 if (lbcBinder == surfaceTextureBinder) {
467 return true;
468 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800469 }
470 }
471
472 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700473 // SurfaceTexture gets destroyed before all the clients are done using it,
474 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800475 // will instead fail later on when the client tries to use the surface,
476 // which should be reported as "surface XYZ returned an -ENODEV". The
477 // purgatorized layers are no less authentic than the visible ones, so this
478 // should not cause any harm.
479 size_t purgatorySize = mLayerPurgatory.size();
480 for (size_t i=0 ; i<purgatorySize ; i++) {
481 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
482 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700483 if (lbc != NULL) {
484 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
485 if (lbcBinder == surfaceTextureBinder) {
486 return true;
487 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800488 }
489 }
490
491 return false;
492}
493
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700494status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
495 // TODO: this is mostly here only for compatibility
496 // the display size is needed but the display metrics should come from elsewhere
497 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
498 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700499 return BAD_INDEX;
500 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700501
502 const HWComposer& hwc(getHwComposer());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700503 float xdpi = hwc.getDpiX(HWC_DISPLAY_PRIMARY);
504 float ydpi = hwc.getDpiY(HWC_DISPLAY_PRIMARY);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700505
506 // TODO: Not sure if display density should handled by SF any longer
507 class Density {
508 static int getDensityFromProperty(char const* propName) {
509 char property[PROPERTY_VALUE_MAX];
510 int density = 0;
511 if (property_get(propName, property, NULL) > 0) {
512 density = atoi(property);
513 }
514 return density;
515 }
516 public:
517 static int getEmuDensity() {
518 return getDensityFromProperty("qemu.sf.lcd_density"); }
519 static int getBuildDensity() {
520 return getDensityFromProperty("ro.sf.lcd_density"); }
521 };
522 // The density of the device is provided by a build property
523 float density = Density::getBuildDensity() / 160.0f;
524 if (density == 0) {
525 // the build doesn't provide a density -- this is wrong!
526 // use xdpi instead
527 ALOGE("ro.sf.lcd_density must be defined as a build property");
528 density = xdpi / 160.0f;
529 }
530 if (Density::getEmuDensity()) {
531 // if "qemu.sf.lcd_density" is specified, it overrides everything
532 xdpi = ydpi = density = Density::getEmuDensity();
533 density /= 160.0f;
534 }
535
Mathias Agopian42977342012-08-05 00:40:46 -0700536 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
537 info->w = hw->getWidth();
538 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700539 info->xdpi = xdpi;
540 info->ydpi = ydpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700541 info->fps = float(1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY));
Mathias Agopian8b736f12012-08-13 17:54:26 -0700542 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700543 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700544 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700545 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700546 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700547}
548
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800549// ----------------------------------------------------------------------------
550
551sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800552 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700553}
554
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700555void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700556
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700557 sp<IBinder> token;
558 { // scope for the lock
559 Mutex::Autolock _l(mStateLock);
560 token = mExtDisplayToken;
561 }
562
563 if (token == 0) {
564 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700565 }
566
567 { // scope for the lock
568 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700569 if (surface == 0) {
570 // release our current display. we're guarantee to have
571 // a reference to it (token), while we hold the lock
572 mExtDisplayToken = 0;
573 } else {
574 mExtDisplayToken = token;
575 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700576
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700577 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
578 info.surface = surface;
579 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700580 }
581}
582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800584
585void SurfaceFlinger::waitForEvent() {
586 mEventQueue.waitMessage();
587}
588
589void SurfaceFlinger::signalTransaction() {
590 mEventQueue.invalidate();
591}
592
593void SurfaceFlinger::signalLayerUpdate() {
594 mEventQueue.invalidate();
595}
596
597void SurfaceFlinger::signalRefresh() {
598 mEventQueue.refresh();
599}
600
601status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
602 nsecs_t reltime, uint32_t flags) {
603 return mEventQueue.postMessage(msg, reltime);
604}
605
606status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
607 nsecs_t reltime, uint32_t flags) {
608 status_t res = mEventQueue.postMessage(msg, reltime);
609 if (res == NO_ERROR) {
610 msg->wait();
611 }
612 return res;
613}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614
Mathias Agopian4fec8732012-06-29 14:12:52 -0700615bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 return true;
618}
619
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700620void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700621 if (mEventThread == NULL) {
622 // This is a temporary workaround for b/7145521. A non-null pointer
623 // does not mean EventThread has finished initializing, so this
624 // is not a correct fix.
625 ALOGW("WARNING: EventThread not started, ignoring vsync");
626 return;
627 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700628 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
629 // we should only receive DisplayDevice::DisplayType from the vsync callback
630 const wp<IBinder>& token(mDefaultDisplays[type]);
631 mEventThread->onVSyncReceived(token, timestamp);
632 }
Mathias Agopian86303202012-07-24 22:46:10 -0700633}
634
635void SurfaceFlinger::eventControl(int event, int enabled) {
636 getHwComposer().eventControl(event, enabled);
637}
638
Mathias Agopian4fec8732012-06-29 14:12:52 -0700639void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800640 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800641 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700642 case MessageQueue::INVALIDATE:
643 handleMessageTransaction();
644 handleMessageInvalidate();
645 signalRefresh();
646 break;
647 case MessageQueue::REFRESH:
648 handleMessageRefresh();
649 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800650 }
651}
652
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700654 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700655 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700656 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700657 }
658}
659
660void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700661 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700662 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700663}
664
665void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700666 ATRACE_CALL();
667 preComposition();
668 rebuildLayerStacks();
669 setUpHWComposer();
670 doDebugFlashRegions();
671 doComposition();
672 postComposition();
673}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700674
Mathias Agopiancd60f992012-08-16 16:28:27 -0700675void SurfaceFlinger::doDebugFlashRegions()
676{
677 // is debugging enabled
678 if (CC_LIKELY(!mDebugRegion))
679 return;
680
681 const bool repaintEverything = mRepaintEverything;
682 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
683 const sp<DisplayDevice>& hw(mDisplays[dpy]);
684 if (hw->canDraw()) {
685 // transform the dirty region into this screen's coordinate space
686 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
687 if (!dirtyRegion.isEmpty()) {
688 // redraw the whole screen
689 doComposeSurfaces(hw, Region(hw->bounds()));
690
691 // and draw the dirty region
692 glDisable(GL_TEXTURE_EXTERNAL_OES);
693 glDisable(GL_TEXTURE_2D);
694 glDisable(GL_BLEND);
695 glColor4f(1, 0, 1, 1);
696 const int32_t height = hw->getHeight();
697 Region::const_iterator it = dirtyRegion.begin();
698 Region::const_iterator const end = dirtyRegion.end();
699 while (it != end) {
700 const Rect& r = *it++;
701 GLfloat vertices[][2] = {
702 { r.left, height - r.top },
703 { r.left, height - r.bottom },
704 { r.right, height - r.bottom },
705 { r.right, height - r.top }
706 };
707 glVertexPointer(2, GL_FLOAT, 0, vertices);
708 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
709 }
710 hw->compositionComplete();
711 // FIXME
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700712 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700713 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
714 }
715 }
716 }
717 }
718
719 postFramebuffer();
720
721 if (mDebugRegion > 1) {
722 usleep(mDebugRegion * 1000);
723 }
724}
725
726void SurfaceFlinger::preComposition()
727{
728 bool needExtraInvalidate = false;
729 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
730 const size_t count = currentLayers.size();
731 for (size_t i=0 ; i<count ; i++) {
732 if (currentLayers[i]->onPreComposition()) {
733 needExtraInvalidate = true;
734 }
735 }
736 if (needExtraInvalidate) {
737 signalLayerUpdate();
738 }
739}
740
741void SurfaceFlinger::postComposition()
742{
743 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
744 const size_t count = currentLayers.size();
745 for (size_t i=0 ; i<count ; i++) {
746 currentLayers[i]->onPostComposition();
747 }
748}
749
750void SurfaceFlinger::rebuildLayerStacks() {
751 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700752 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700753 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700754 mVisibleRegionsDirty = false;
755 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700756
Mathias Agopian87baae12012-07-31 12:38:26 -0700757 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700758 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700759 Region opaqueRegion;
760 Region dirtyRegion;
761 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700762 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700763 const Transform& tr(hw->getTransform());
764 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700765 if (hw->canDraw()) {
766 SurfaceFlinger::computeVisibleRegions(currentLayers,
767 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700768
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700769 const size_t count = currentLayers.size();
770 for (size_t i=0 ; i<count ; i++) {
771 const sp<LayerBase>& layer(currentLayers[i]);
772 const Layer::State& s(layer->drawingState());
773 if (s.layerStack == hw->getLayerStack()) {
774 Region visibleRegion(tr.transform(layer->visibleRegion));
775 visibleRegion.andSelf(bounds);
776 if (!visibleRegion.isEmpty()) {
777 layersSortedByZ.add(layer);
778 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700779 }
780 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700781 }
Mathias Agopian42977342012-08-05 00:40:46 -0700782 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700783 hw->undefinedRegion.set(bounds);
784 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
785 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700786 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700787 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700788}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700789
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700791 HWComposer& hwc(getHwComposer());
792 if (hwc.initCheck() == NO_ERROR) {
793 // build the h/w work list
794 const bool workListsDirty = mHwWorkListDirty;
795 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700796 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700797 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700798 const int32_t id = hw->getHwcDisplayId();
799 if (id >= 0) {
800 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700802 const size_t count = currentLayers.size();
803 if (hwc.createWorkList(id, count) >= 0) {
804 HWComposer::LayerListIterator cur = hwc.begin(id);
805 const HWComposer::LayerListIterator end = hwc.end(id);
806 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
807 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700808
Mathias Agopiane60b0682012-08-21 23:34:09 -0700809 if (CC_UNLIKELY(workListsDirty)) {
810 layer->setGeometry(hw, *cur);
811 if (mDebugDisableHWC || mDebugRegion) {
812 cur->setSkip(true);
813 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700814 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700815
Mathias Agopiane60b0682012-08-21 23:34:09 -0700816 /*
817 * update the per-frame h/w composer data for each layer
818 * and build the transparent region of the FB
819 */
820 layer->setPerFrameData(hw, *cur);
821 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700822 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700823 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700824 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700825 status_t err = hwc.prepare();
826 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
827 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700828}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700829
Mathias Agopiancd60f992012-08-16 16:28:27 -0700830void SurfaceFlinger::doComposition() {
831 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700833 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700834 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700835 if (hw->canDraw()) {
836 // transform the dirty region into this screen's coordinate space
837 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
838 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700839 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700840 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700841 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842 hw->dirtyRegion.clear();
843 hw->flip(hw->swapRegion);
844 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700845 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700846 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700847 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850}
851
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852void SurfaceFlinger::postFramebuffer()
853{
Mathias Agopian841cde52012-03-01 15:44:37 -0800854 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800855
Mathias Agopiana44b0412011-10-16 18:46:35 -0700856 const nsecs_t now = systemTime();
857 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700858
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700859 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700860 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700861 // FIXME: EGL spec says:
862 // "surface must be bound to the calling thread's current context,
863 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700864 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700865 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700866 }
867
Mathias Agopian92a979a2012-08-02 18:32:23 -0700868 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700869 sp<const DisplayDevice> hw(mDisplays[dpy]);
870 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700871 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700872 int32_t id = hw->getHwcDisplayId();
873 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700874 HWComposer::LayerListIterator cur = hwc.begin(id);
875 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700876 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700877 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700878 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700879 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700880 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700881 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700882 }
Jesse Hallef194142012-06-14 14:45:17 -0700883 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800884 }
885
Mathias Agopiana44b0412011-10-16 18:46:35 -0700886 mLastSwapBufferTime = systemTime() - now;
887 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888}
889
Mathias Agopian87baae12012-07-31 12:38:26 -0700890void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891{
Mathias Agopian841cde52012-03-01 15:44:37 -0800892 ATRACE_CALL();
893
Mathias Agopianca4d3602011-05-19 15:38:14 -0700894 Mutex::Autolock _l(mStateLock);
895 const nsecs_t now = systemTime();
896 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897
Mathias Agopianca4d3602011-05-19 15:38:14 -0700898 // Here we're guaranteed that some transaction flags are set
899 // so we can call handleTransactionLocked() unconditionally.
900 // We call getTransactionFlags(), which will also clear the flags,
901 // with mStateLock held to guarantee that mCurrentState won't change
902 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700903
Mathias Agopiane57f2922012-08-09 16:29:12 -0700904 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700905 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700906
Mathias Agopianca4d3602011-05-19 15:38:14 -0700907 mLastTransactionTime = systemTime() - now;
908 mDebugInTransaction = 0;
909 invalidateHwcGeometry();
910 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700911}
912
Mathias Agopian87baae12012-07-31 12:38:26 -0700913void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700914{
915 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 const size_t count = currentLayers.size();
917
918 /*
919 * Traversal of the children
920 * (perform the transaction for each of them if needed)
921 */
922
Mathias Agopian3559b072012-08-15 13:46:03 -0700923 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700925 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
927 if (!trFlags) continue;
928
929 const uint32_t flags = layer->doTransaction(0);
930 if (flags & Layer::eVisibleRegion)
931 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 }
933 }
934
935 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700936 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 */
938
Mathias Agopiane57f2922012-08-09 16:29:12 -0700939 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700940 // here we take advantage of Vector's copy-on-write semantics to
941 // improve performance by skipping the transaction entirely when
942 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700943 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
944 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700945 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700947 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700948 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700949
950 // find the displays that were removed
951 // (ie: in drawing state but not in current state)
952 // also handle displays that changed
953 // (ie: displays that are in both lists)
954 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700955 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
956 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700957 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700958 if (!draw[i].isMainDisplay()) {
959 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700960 } else {
961 ALOGW("trying to remove the main display");
962 }
963 } else {
964 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700965 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700966 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700967 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700968 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700969 // recreating the DisplayDevice, so we just remove it
970 // from the drawing state, so that it get re-added
971 // below.
972 mDisplays.removeItem(display);
973 mDrawingState.displays.removeItemsAt(i);
974 dc--; i--;
975 // at this point we must loop to the next item
976 continue;
977 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700978
Mathias Agopian93997a82012-08-29 17:30:36 -0700979 const sp<DisplayDevice>& disp(getDisplayDevice(display));
980 if (disp != NULL) {
981 if (state.layerStack != draw[i].layerStack) {
982 disp->setLayerStack(state.layerStack);
983 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700984 if ((state.orientation != draw[i].orientation)
985 || (state.viewport != draw[i].viewport)
986 || (state.frame != draw[i].frame))
987 {
988 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -0700989 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -0700990 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700991 }
992 }
993 }
994
995 // find displays that were added
996 // (ie: in current state but not in drawing state)
997 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700998 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700999 const DisplayDeviceState& state(curr[i]);
Mathias Agopian93997a82012-08-29 17:30:36 -07001000 if (state.surface != NULL) {
1001 sp<SurfaceTextureClient> stc(
1002 new SurfaceTextureClient(state.surface));
1003 const wp<IBinder>& display(curr.keyAt(i));
1004 sp<DisplayDevice> disp = new DisplayDevice(this,
1005 state.type, display, stc, 0, mEGLConfig);
1006 disp->setLayerStack(state.layerStack);
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001007 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001008 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001009 mDisplays.add(display, disp);
1010 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001011 }
1012 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001014 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015
Mathias Agopian3559b072012-08-15 13:46:03 -07001016 /*
1017 * Perform our own transaction if needed
1018 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001019
Mathias Agopiancd60f992012-08-16 16:28:27 -07001020 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1021 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001022 // layers have been added
1023 mVisibleRegionsDirty = true;
1024 }
1025
1026 // some layers might have been removed, so
1027 // we need to update the regions they're exposing.
1028 if (mLayersRemoved) {
1029 mLayersRemoved = false;
1030 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001031 const size_t count = previousLayers.size();
1032 for (size_t i=0 ; i<count ; i++) {
1033 const sp<LayerBase>& layer(previousLayers[i]);
1034 if (currentLayers.indexOf(layer) < 0) {
1035 // this layer is not visible anymore
1036 // TODO: we could traverse the tree from front to back and
1037 // compute the actual visible region
1038 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001039 const Layer::State& s(layer->drawingState());
1040 Region visibleReg = s.transform.transform(
1041 Region(Rect(s.active.w, s.active.h)));
1042 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001043 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 }
1046
1047 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001048}
1049
1050void SurfaceFlinger::commitTransaction()
1051{
1052 if (!mLayersPendingRemoval.isEmpty()) {
1053 // Notify removed layers now that they can't be drawn from
1054 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1055 mLayersPendingRemoval[i]->onRemoved();
1056 }
1057 mLayersPendingRemoval.clear();
1058 }
1059
1060 mDrawingState = mCurrentState;
1061 mTransationPending = false;
1062 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063}
1064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001066 const LayerVector& currentLayers, uint32_t layerStack,
1067 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068{
Mathias Agopian841cde52012-03-01 15:44:37 -08001069 ATRACE_CALL();
1070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 Region aboveOpaqueLayers;
1072 Region aboveCoveredLayers;
1073 Region dirty;
1074
Mathias Agopian87baae12012-07-31 12:38:26 -07001075 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076
1077 size_t i = currentLayers.size();
1078 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080
1081 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001082 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083
Mathias Agopian87baae12012-07-31 12:38:26 -07001084 // only consider the layers on the given later stack
1085 if (s.layerStack != layerStack)
1086 continue;
1087
Mathias Agopianab028732010-03-16 16:41:46 -07001088 /*
1089 * opaqueRegion: area of a surface that is fully opaque.
1090 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001092
1093 /*
1094 * visibleRegion: area of a surface that is visible on screen
1095 * and not fully transparent. This is essentially the layer's
1096 * footprint minus the opaque regions above it.
1097 * Areas covered by a translucent surface are considered visible.
1098 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001100
1101 /*
1102 * coveredRegion: area of a surface that is covered by all
1103 * visible regions above it (which includes the translucent areas).
1104 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001106
1107
1108 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001109 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001110 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001111 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001113 if (!visibleRegion.isEmpty()) {
1114 // Remove the transparent area from the visible region
1115 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001116 Region transparentRegionScreen;
1117 const Transform tr(s.transform);
1118 if (tr.transformed()) {
1119 if (tr.preserveRects()) {
1120 // transform the transparent region
1121 transparentRegionScreen = tr.transform(s.transparentRegion);
1122 } else {
1123 // transformation too complex, can't do the
1124 // transparent region optimization.
1125 transparentRegionScreen.clear();
1126 }
1127 } else {
1128 transparentRegionScreen = s.transparentRegion;
1129 }
1130 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001131 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132
Mathias Agopianab028732010-03-16 16:41:46 -07001133 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001134 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001135 if (s.alpha==255 && !translucent &&
1136 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1137 // the opaque region is the layer's footprint
1138 opaqueRegion = visibleRegion;
1139 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 }
1141 }
1142
Mathias Agopianab028732010-03-16 16:41:46 -07001143 // Clip the covered region to the visible region
1144 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1145
1146 // Update aboveCoveredLayers for next (lower) layer
1147 aboveCoveredLayers.orSelf(visibleRegion);
1148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 // subtract the opaque region covered by the layers above us
1150 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151
1152 // compute this layer's dirty region
1153 if (layer->contentDirty) {
1154 // we need to invalidate the whole region
1155 dirty = visibleRegion;
1156 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001157 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 layer->contentDirty = false;
1159 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001160 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001161 * the exposed region consists of two components:
1162 * 1) what's VISIBLE now and was COVERED before
1163 * 2) what's EXPOSED now less what was EXPOSED before
1164 *
1165 * note that (1) is conservative, we start with the whole
1166 * visible region but only keep what used to be covered by
1167 * something -- which mean it may have been exposed.
1168 *
1169 * (2) handles areas that were not covered by anything but got
1170 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001171 */
Mathias Agopianab028732010-03-16 16:41:46 -07001172 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001173 const Region oldVisibleRegion = layer->visibleRegion;
1174 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001175 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1176 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 }
1178 dirty.subtractSelf(aboveOpaqueLayers);
1179
1180 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001181 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182
Mathias Agopianab028732010-03-16 16:41:46 -07001183 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 // Store the visible region is screen space
1187 layer->setVisibleRegion(visibleRegion);
1188 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
1190
Mathias Agopian87baae12012-07-31 12:38:26 -07001191 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192}
1193
Mathias Agopian87baae12012-07-31 12:38:26 -07001194void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1195 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001196 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001197 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1198 if (hw->getLayerStack() == layerStack) {
1199 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001200 }
1201 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001202}
1203
1204void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001206 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207
Mathias Agopian4fec8732012-06-29 14:12:52 -07001208 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001209 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001210 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001211 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001212 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001213 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001214 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001215 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001216 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001217
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001218 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219}
1220
Mathias Agopianad456f92011-01-13 17:53:01 -08001221void SurfaceFlinger::invalidateHwcGeometry()
1222{
1223 mHwWorkListDirty = true;
1224}
1225
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001226
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001228 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229{
Mathias Agopian87baae12012-07-31 12:38:26 -07001230 Region dirtyRegion(inDirtyRegion);
1231
Mathias Agopianb8a55602009-06-26 19:06:36 -07001232 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001233 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234
Mathias Agopian42977342012-08-05 00:40:46 -07001235 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001236 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001237 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1238 // takes a rectangle, we must make sure to update that whole
1239 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001240 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001241 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001242 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001243 // We need to redraw the rectangle that will be updated
1244 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001245 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001246 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001247 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001248 } else {
1249 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001250 dirtyRegion.set(hw->bounds());
1251 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 }
1253 }
1254
Mathias Agopiancd60f992012-08-16 16:28:27 -07001255 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256
Mathias Agopiancd60f992012-08-16 16:28:27 -07001257 // FIXME: we need to call eglSwapBuffers() on displays that have
1258 // GL composition and only on those.
1259 // however, currently hwc.commit() already does that for the main
Jesse Hall9ca48912012-08-30 13:42:23 -07001260 // display (if there is a hwc) and never for the other ones
1261 if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL ||
1262 getHwComposer().initCheck() != NO_ERROR) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001263 // FIXME: EGL spec says:
1264 // "surface must be bound to the calling thread's current context,
1265 // for the current rendering API."
1266 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001267 }
1268
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001269 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001270 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271}
1272
Mathias Agopiancd60f992012-08-16 16:28:27 -07001273void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001274{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001275 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001276 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001277 HWComposer::LayerListIterator cur = hwc.begin(id);
1278 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001279
Mathias Agopian85d751c2012-08-29 16:59:24 -07001280 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1281 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001282 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001283
1284 // set the frame buffer
1285 glMatrixMode(GL_MODELVIEW);
1286 glLoadIdentity();
1287
1288 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001289 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001290 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001291 // when using overlays, we assume a fully transparent framebuffer
1292 // NOTE: we could reduce how much we need to clear, for instance
1293 // remove where there are opaque FB layers. however, on some
1294 // GPUs doing a "clean slate" glClear might be more efficient.
1295 // We'll revisit later if needed.
1296 glClearColor(0, 0, 0, 0);
1297 glClear(GL_COLOR_BUFFER_BIT);
1298 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001299 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001300 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001301 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001302 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001303 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001304 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001305 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001306 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001307
Mathias Agopian85d751c2012-08-29 16:59:24 -07001308 /*
1309 * and then, render the layers targeted at the framebuffer
1310 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001311
Mathias Agopian85d751c2012-08-29 16:59:24 -07001312 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1313 const size_t count = layers.size();
1314 const Transform& tr = hw->getTransform();
1315 if (cur != end) {
1316 // we're using h/w composer
1317 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001318 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001319 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001320 if (!clip.isEmpty()) {
1321 switch (cur->getCompositionType()) {
1322 case HWC_OVERLAY: {
1323 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1324 && i
1325 && layer->isOpaque()
1326 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001327 // never clear the very first layer since we're
1328 // guaranteed the FB is already cleared
1329 layer->clearWithOpenGL(hw, clip);
1330 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001331 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001332 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001333 case HWC_FRAMEBUFFER: {
1334 layer->draw(hw, clip);
1335 break;
1336 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001337 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001338 }
1339 layer->setAcquireFence(hw, *cur);
1340 }
1341 } else {
1342 // we're not using h/w composer
1343 for (size_t i=0 ; i<count ; ++i) {
1344 const sp<LayerBase>& layer(layers[i]);
1345 const Region clip(dirty.intersect(
1346 tr.transform(layer->visibleRegion)));
1347 if (!clip.isEmpty()) {
1348 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001349 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001350 }
1351 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352}
1353
Mathias Agopian55801e42012-08-27 18:54:24 -07001354void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1355 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001357 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001358 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001359 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001360 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001361
Mathias Agopian55801e42012-08-27 18:54:24 -07001362 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001363 Region::const_iterator it = region.begin();
1364 Region::const_iterator const end = region.end();
1365 while (it != end) {
1366 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001367 GLfloat vertices[][2] = {
1368 { r.left, height - r.top },
1369 { r.left, height - r.bottom },
1370 { r.right, height - r.bottom },
1371 { r.right, height - r.top }
1372 };
1373 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001374 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1375 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376}
1377
Mathias Agopian96f08192010-06-02 23:28:45 -07001378ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1379 const sp<LayerBaseClient>& lbc)
1380{
Mathias Agopian96f08192010-06-02 23:28:45 -07001381 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001382 size_t name = client->attachLayer(lbc);
1383
Mathias Agopian96f08192010-06-02 23:28:45 -07001384 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001385 Mutex::Autolock _l(mStateLock);
1386 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001387
Mathias Agopian4f113742011-05-03 16:21:41 -07001388 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001389}
1390
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001391status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392{
1393 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001394 status_t err = purgatorizeLayer_l(layer);
1395 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001396 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001397 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398}
1399
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001400status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401{
1402 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1403 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001404 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 return NO_ERROR;
1406 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001407 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408}
1409
Mathias Agopian9a112062009-04-17 19:36:26 -07001410status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1411{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001412 // First add the layer to the purgatory list, which makes sure it won't
1413 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001414 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001415 if (err >= 0) {
1416 mLayerPurgatory.add(layerBase);
1417 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001418
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001419 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001420
Mathias Agopian3d579642009-06-04 18:46:21 -07001421 // it's possible that we don't find a layer, because it might
1422 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001423 // from the user because there is a race between Client::destroySurface(),
1424 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001425 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1426}
1427
Mathias Agopiandea20b12011-05-03 17:04:02 -07001428uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1429{
1430 return android_atomic_release_load(&mTransactionFlags);
1431}
1432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1434{
1435 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1436}
1437
Mathias Agopianbb641242010-05-18 17:06:55 -07001438uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
1440 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1441 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001442 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 return old;
1445}
1446
Mathias Agopian8b33f032012-07-24 20:43:54 -07001447void SurfaceFlinger::setTransactionState(
1448 const Vector<ComposerState>& state,
1449 const Vector<DisplayState>& displays,
1450 uint32_t flags)
1451{
Mathias Agopian698c0872011-06-28 19:09:31 -07001452 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001453 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001454
1455 size_t count = displays.size();
1456 for (size_t i=0 ; i<count ; i++) {
1457 const DisplayState& s(displays[i]);
1458 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001459 }
1460
Mathias Agopiane57f2922012-08-09 16:29:12 -07001461 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001462 for (size_t i=0 ; i<count ; i++) {
1463 const ComposerState& s(state[i]);
1464 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001465 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001466 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001467
Mathias Agopian386aa982011-11-07 21:58:03 -08001468 if (transactionFlags) {
1469 // this triggers the transaction
1470 setTransactionFlags(transactionFlags);
1471
1472 // if this is a synchronous transaction, wait for it to take effect
1473 // before returning.
1474 if (flags & eSynchronous) {
1475 mTransationPending = true;
1476 }
1477 while (mTransationPending) {
1478 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1479 if (CC_UNLIKELY(err != NO_ERROR)) {
1480 // just in case something goes wrong in SF, return to the
1481 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001482 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001483 mTransationPending = false;
1484 break;
1485 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
1488}
1489
Mathias Agopiane57f2922012-08-09 16:29:12 -07001490uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1491{
1492 uint32_t flags = 0;
1493 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001494 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001495 const uint32_t what = s.what;
1496 if (what & DisplayState::eSurfaceChanged) {
1497 if (disp.surface->asBinder() != s.surface->asBinder()) {
1498 disp.surface = s.surface;
1499 flags |= eDisplayTransactionNeeded;
1500 }
1501 }
1502 if (what & DisplayState::eLayerStackChanged) {
1503 if (disp.layerStack != s.layerStack) {
1504 disp.layerStack = s.layerStack;
1505 flags |= eDisplayTransactionNeeded;
1506 }
1507 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001508 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001509 if (disp.orientation != s.orientation) {
1510 disp.orientation = s.orientation;
1511 flags |= eDisplayTransactionNeeded;
1512 }
1513 if (disp.frame != s.frame) {
1514 disp.frame = s.frame;
1515 flags |= eDisplayTransactionNeeded;
1516 }
1517 if (disp.viewport != s.viewport) {
1518 disp.viewport = s.viewport;
1519 flags |= eDisplayTransactionNeeded;
1520 }
1521 }
1522 }
1523 return flags;
1524}
1525
1526uint32_t SurfaceFlinger::setClientStateLocked(
1527 const sp<Client>& client,
1528 const layer_state_t& s)
1529{
1530 uint32_t flags = 0;
1531 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1532 if (layer != 0) {
1533 const uint32_t what = s.what;
1534 if (what & layer_state_t::ePositionChanged) {
1535 if (layer->setPosition(s.x, s.y))
1536 flags |= eTraversalNeeded;
1537 }
1538 if (what & layer_state_t::eLayerChanged) {
1539 // NOTE: index needs to be calculated before we update the state
1540 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1541 if (layer->setLayer(s.z)) {
1542 mCurrentState.layersSortedByZ.removeAt(idx);
1543 mCurrentState.layersSortedByZ.add(layer);
1544 // we need traversal (state changed)
1545 // AND transaction (list changed)
1546 flags |= eTransactionNeeded|eTraversalNeeded;
1547 }
1548 }
1549 if (what & layer_state_t::eSizeChanged) {
1550 if (layer->setSize(s.w, s.h)) {
1551 flags |= eTraversalNeeded;
1552 }
1553 }
1554 if (what & layer_state_t::eAlphaChanged) {
1555 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1556 flags |= eTraversalNeeded;
1557 }
1558 if (what & layer_state_t::eMatrixChanged) {
1559 if (layer->setMatrix(s.matrix))
1560 flags |= eTraversalNeeded;
1561 }
1562 if (what & layer_state_t::eTransparentRegionChanged) {
1563 if (layer->setTransparentRegionHint(s.transparentRegion))
1564 flags |= eTraversalNeeded;
1565 }
1566 if (what & layer_state_t::eVisibilityChanged) {
1567 if (layer->setFlags(s.flags, s.mask))
1568 flags |= eTraversalNeeded;
1569 }
1570 if (what & layer_state_t::eCropChanged) {
1571 if (layer->setCrop(s.crop))
1572 flags |= eTraversalNeeded;
1573 }
1574 if (what & layer_state_t::eLayerStackChanged) {
1575 // NOTE: index needs to be calculated before we update the state
1576 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1577 if (layer->setLayerStack(s.layerStack)) {
1578 mCurrentState.layersSortedByZ.removeAt(idx);
1579 mCurrentState.layersSortedByZ.add(layer);
1580 // we need traversal (state changed)
1581 // AND transaction (list changed)
1582 flags |= eTransactionNeeded|eTraversalNeeded;
1583 }
1584 }
1585 }
1586 return flags;
1587}
1588
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001589sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001590 ISurfaceComposerClient::surface_data_t* params,
1591 const String8& name,
1592 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001593 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 uint32_t flags)
1595{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001596 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001597 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001598
1599 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001600 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001601 int(w), int(h));
1602 return surfaceHandle;
1603 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001604
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001605 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001606 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1607 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001608 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001610 case ISurfaceComposerClient::eFXSurfaceBlur:
1611 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001612 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001614 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001615 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001616 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 }
1618
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001619 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001620 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001621 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001622 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001624 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001625 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001626 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001627 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001628 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629 }
1630
1631 return surfaceHandle;
1632}
1633
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001634sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001635 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001636 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001637 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638{
1639 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001640 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 case PIXEL_FORMAT_TRANSPARENT:
1642 case PIXEL_FORMAT_TRANSLUCENT:
1643 format = PIXEL_FORMAT_RGBA_8888;
1644 break;
1645 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001646#ifdef NO_RGBX_8888
1647 format = PIXEL_FORMAT_RGB_565;
1648#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001649 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001650#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 break;
1652 }
1653
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001654#ifdef NO_RGBX_8888
1655 if (format == PIXEL_FORMAT_RGBX_8888)
1656 format = PIXEL_FORMAT_RGBA_8888;
1657#endif
1658
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001659 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001660 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001661 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001662 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 }
1665 return layer;
1666}
1667
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001668sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001669 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001670 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001672 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001673 return layer;
1674}
1675
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001676sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001677 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001678 uint32_t w, uint32_t h, uint32_t flags)
1679{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001680 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 return layer;
1682}
1683
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001684status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685{
Mathias Agopian9a112062009-04-17 19:36:26 -07001686 /*
1687 * called by the window manager, when a surface should be marked for
1688 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001689 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001690 * The surface is removed from the current and drawing lists, but placed
1691 * in the purgatory queue, so it's not destroyed right-away (we need
1692 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001693 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001694
Mathias Agopian48d819a2009-09-10 19:41:18 -07001695 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001696 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001697 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001698
Mathias Agopian48d819a2009-09-10 19:41:18 -07001699 if (layer != 0) {
1700 err = purgatorizeLayer_l(layer);
1701 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001702 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001703 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001704 }
1705 return err;
1706}
1707
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001708status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001709{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001710 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001711 status_t err = NO_ERROR;
1712 sp<LayerBaseClient> l(layer.promote());
1713 if (l != NULL) {
1714 Mutex::Autolock _l(mStateLock);
1715 err = removeLayer_l(l);
1716 if (err == NAME_NOT_FOUND) {
1717 // The surface wasn't in the current list, which means it was
1718 // removed already, which means it is in the purgatory,
1719 // and need to be removed from there.
1720 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001721 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001722 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001723 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001724 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001725 "error removing layer=%p (%s)", l.get(), strerror(-err));
1726 }
1727 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728}
1729
Mathias Agopianb60314a2012-04-10 22:09:54 -07001730// ---------------------------------------------------------------------------
1731
Andy McFadden13a082e2012-08-24 10:16:42 -07001732void SurfaceFlinger::onInitializeDisplays() {
1733 // reset screen orientation
1734 Vector<ComposerState> state;
1735 Vector<DisplayState> displays;
1736 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001737 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001738 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001739 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001740 d.frame.makeInvalid();
1741 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001742 displays.add(d);
1743 setTransactionState(state, displays, 0);
1744
1745 // XXX: this should init default device to "unblank" and all other devices to "blank"
1746 onScreenAcquired();
1747}
1748
1749void SurfaceFlinger::initializeDisplays() {
1750 class MessageScreenInitialized : public MessageBase {
1751 SurfaceFlinger* flinger;
1752 public:
1753 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1754 virtual bool handler() {
1755 flinger->onInitializeDisplays();
1756 return true;
1757 }
1758 };
1759 sp<MessageBase> msg = new MessageScreenInitialized(this);
1760 postMessageAsync(msg); // we may be called from main thread, use async message
1761}
1762
1763
Mathias Agopianb60314a2012-04-10 22:09:54 -07001764void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001765 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001766 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001767 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001768 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001769 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001770 mVisibleRegionsDirty = true;
1771 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772}
1773
Mathias Agopianb60314a2012-04-10 22:09:54 -07001774void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001775 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001776 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1777 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001778 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001779 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001780 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001781 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001782 // from this point on, SF will stop drawing
1783 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784}
1785
Colin Cross8e533062012-06-07 13:17:52 -07001786void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001787 class MessageScreenAcquired : public MessageBase {
1788 SurfaceFlinger* flinger;
1789 public:
1790 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1791 virtual bool handler() {
1792 flinger->onScreenAcquired();
1793 return true;
1794 }
1795 };
1796 sp<MessageBase> msg = new MessageScreenAcquired(this);
1797 postMessageSync(msg);
1798}
1799
Colin Cross8e533062012-06-07 13:17:52 -07001800void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001801 class MessageScreenReleased : public MessageBase {
1802 SurfaceFlinger* flinger;
1803 public:
1804 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1805 virtual bool handler() {
1806 flinger->onScreenReleased();
1807 return true;
1808 }
1809 };
1810 sp<MessageBase> msg = new MessageScreenReleased(this);
1811 postMessageSync(msg);
1812}
1813
1814// ---------------------------------------------------------------------------
1815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1817{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001818 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819 char buffer[SIZE];
1820 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001821
1822 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001823 snprintf(buffer, SIZE, "Permission Denial: "
1824 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1825 IPCThreadState::self()->getCallingPid(),
1826 IPCThreadState::self()->getCallingUid());
1827 result.append(buffer);
1828 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001829 // Try to get the main lock, but don't insist if we can't
1830 // (this would indicate SF is stuck, but we want to be able to
1831 // print something in dumpsys).
1832 int retry = 3;
1833 while (mStateLock.tryLock()<0 && --retry>=0) {
1834 usleep(1000000);
1835 }
1836 const bool locked(retry >= 0);
1837 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001838 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001839 "SurfaceFlinger appears to be unresponsive, "
1840 "dumping anyways (no locks held)\n");
1841 result.append(buffer);
1842 }
1843
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001844 bool dumpAll = true;
1845 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001846 size_t numArgs = args.size();
1847 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001848 if ((index < numArgs) &&
1849 (args[index] == String16("--list"))) {
1850 index++;
1851 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001852 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001853 }
1854
1855 if ((index < numArgs) &&
1856 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001857 index++;
1858 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001859 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001860 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001861
1862 if ((index < numArgs) &&
1863 (args[index] == String16("--latency-clear"))) {
1864 index++;
1865 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001866 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001867 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001868 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001869
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001870 if (dumpAll) {
1871 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001872 }
1873
Mathias Agopian9795c422009-08-26 16:36:26 -07001874 if (locked) {
1875 mStateLock.unlock();
1876 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877 }
1878 write(fd, result.string(), result.size());
1879 return NO_ERROR;
1880}
1881
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001882void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1883 String8& result, char* buffer, size_t SIZE) const
1884{
1885 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1886 const size_t count = currentLayers.size();
1887 for (size_t i=0 ; i<count ; i++) {
1888 const sp<LayerBase>& layer(currentLayers[i]);
1889 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1890 result.append(buffer);
1891 }
1892}
1893
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001894void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1895 String8& result, char* buffer, size_t SIZE) const
1896{
1897 String8 name;
1898 if (index < args.size()) {
1899 name = String8(args[index]);
1900 index++;
1901 }
1902
1903 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1904 const size_t count = currentLayers.size();
1905 for (size_t i=0 ; i<count ; i++) {
1906 const sp<LayerBase>& layer(currentLayers[i]);
1907 if (name.isEmpty()) {
1908 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1909 result.append(buffer);
1910 }
1911 if (name.isEmpty() || (name == layer->getName())) {
1912 layer->dumpStats(result, buffer, SIZE);
1913 }
1914 }
1915}
1916
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001917void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1918 String8& result, char* buffer, size_t SIZE) const
1919{
1920 String8 name;
1921 if (index < args.size()) {
1922 name = String8(args[index]);
1923 index++;
1924 }
1925
1926 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1927 const size_t count = currentLayers.size();
1928 for (size_t i=0 ; i<count ; i++) {
1929 const sp<LayerBase>& layer(currentLayers[i]);
1930 if (name.isEmpty() || (name == layer->getName())) {
1931 layer->clearStats();
1932 }
1933 }
1934}
1935
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001936void SurfaceFlinger::dumpAllLocked(
1937 String8& result, char* buffer, size_t SIZE) const
1938{
1939 // figure out if we're stuck somewhere
1940 const nsecs_t now = systemTime();
1941 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1942 const nsecs_t inTransaction(mDebugInTransaction);
1943 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1944 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1945
1946 /*
1947 * Dump the visible layer list
1948 */
1949 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1950 const size_t count = currentLayers.size();
1951 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1952 result.append(buffer);
1953 for (size_t i=0 ; i<count ; i++) {
1954 const sp<LayerBase>& layer(currentLayers[i]);
1955 layer->dump(result, buffer, SIZE);
1956 }
1957
1958 /*
1959 * Dump the layers in the purgatory
1960 */
1961
1962 const size_t purgatorySize = mLayerPurgatory.size();
1963 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1964 result.append(buffer);
1965 for (size_t i=0 ; i<purgatorySize ; i++) {
1966 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1967 layer->shortDump(result, buffer, SIZE);
1968 }
1969
1970 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001971 * Dump Display state
1972 */
1973
1974 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1975 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1976 snprintf(buffer, SIZE,
1977 "+ DisplayDevice[%u]\n"
Jeff Brown4fb39992012-09-07 12:55:10 -07001978 " type=%x, layerStack=%u, (%4dx%4d), orient=%2d (type=%08x), "
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001979 "flips=%u, secure=%d, numLayers=%u, v:[%d,%d,%d,%d], f:[%d,%d,%d,%d]\n",
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001980 dpy,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001981 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001982 hw->getWidth(), hw->getHeight(),
1983 hw->getOrientation(), hw->getTransform().getType(),
1984 hw->getPageFlipCount(),
1985 hw->getSecureLayerVisible(),
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001986 hw->getVisibleLayersSortedByZ().size(),
1987 hw->getViewport().left, hw->getViewport().top, hw->getViewport().right, hw->getViewport().bottom,
1988 hw->getFrame().left, hw->getFrame().top, hw->getFrame().right, hw->getFrame().bottom);
1989
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001990 result.append(buffer);
1991 }
1992
1993 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001994 * Dump SurfaceFlinger global state
1995 */
1996
1997 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1998 result.append(buffer);
1999
Mathias Agopian888c8222012-08-04 21:10:38 -07002000 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002001 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002002 const GLExtensions& extensions(GLExtensions::getInstance());
2003 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2004 extensions.getVendor(),
2005 extensions.getRenderer(),
2006 extensions.getVersion());
2007 result.append(buffer);
2008
2009 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002010 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002011 result.append(buffer);
2012
2013 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2014 result.append(buffer);
2015
Mathias Agopian42977342012-08-05 00:40:46 -07002016 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002017 snprintf(buffer, SIZE,
2018 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002019 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002020 result.append(buffer);
2021 snprintf(buffer, SIZE,
2022 " last eglSwapBuffers() time: %f us\n"
2023 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002024 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002025 " refresh-rate : %f fps\n"
2026 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002027 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002028 mLastSwapBufferTime/1000.0,
2029 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002030 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002031 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2032 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2033 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002034 result.append(buffer);
2035
2036 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2037 inSwapBuffersDuration/1000.0);
2038 result.append(buffer);
2039
2040 snprintf(buffer, SIZE, " transaction time: %f us\n",
2041 inTransactionDuration/1000.0);
2042 result.append(buffer);
2043
2044 /*
2045 * VSYNC state
2046 */
2047 mEventThread->dump(result, buffer, SIZE);
2048
2049 /*
2050 * Dump HWComposer state
2051 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002052 snprintf(buffer, SIZE, "h/w composer state:\n");
2053 result.append(buffer);
2054 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2055 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2056 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2057 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002058 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002059
2060 /*
2061 * Dump gralloc state
2062 */
2063 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2064 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002065 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002066}
2067
Keun young Park63f165f2012-08-31 10:53:36 -07002068bool SurfaceFlinger::startDdmConnection()
2069{
2070 void* libddmconnection_dso =
2071 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2072 if (!libddmconnection_dso) {
2073 return false;
2074 }
2075 void (*DdmConnection_start)(const char* name);
2076 DdmConnection_start =
2077 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2078 if (!DdmConnection_start) {
2079 dlclose(libddmconnection_dso);
2080 return false;
2081 }
2082 (*DdmConnection_start)(getServiceName());
2083 return true;
2084}
2085
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086status_t SurfaceFlinger::onTransact(
2087 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2088{
2089 switch (code) {
2090 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002091 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002093 case BLANK:
2094 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 {
2096 // codes that require permission check
2097 IPCThreadState* ipc = IPCThreadState::self();
2098 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002099 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002100 if ((uid != AID_GRAPHICS) &&
2101 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002102 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002103 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2104 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002105 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002106 break;
2107 }
2108 case CAPTURE_SCREEN:
2109 {
2110 // codes that require permission check
2111 IPCThreadState* ipc = IPCThreadState::self();
2112 const int pid = ipc->getCallingPid();
2113 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002114 if ((uid != AID_GRAPHICS) &&
2115 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002116 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002117 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2118 return PERMISSION_DENIED;
2119 }
2120 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121 }
2122 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002124 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2125 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002126 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002127 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002128 IPCThreadState* ipc = IPCThreadState::self();
2129 const int pid = ipc->getCallingPid();
2130 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002131 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002132 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002133 return PERMISSION_DENIED;
2134 }
2135 int n;
2136 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002137 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002138 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002139 return NO_ERROR;
2140 case 1002: // SHOW_UPDATES
2141 n = data.readInt32();
2142 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002143 invalidateHwcGeometry();
2144 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002145 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002146 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002147 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002148 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002150 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002151 setTransactionFlags(
2152 eTransactionNeeded|
2153 eDisplayTransactionNeeded|
2154 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002155 return NO_ERROR;
2156 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002157 case 1006:{ // send empty update
2158 signalRefresh();
2159 return NO_ERROR;
2160 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002161 case 1008: // toggle use of hw composer
2162 n = data.readInt32();
2163 mDebugDisableHWC = n ? 1 : 0;
2164 invalidateHwcGeometry();
2165 repaintEverything();
2166 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002167 case 1009: // toggle use of transform hint
2168 n = data.readInt32();
2169 mDebugDisableTransformHint = n ? 1 : 0;
2170 invalidateHwcGeometry();
2171 repaintEverything();
2172 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002173 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002174 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002175 reply->writeInt32(0);
2176 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002177 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002178 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179 return NO_ERROR;
2180 case 1013: {
2181 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002182 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2183 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002184 }
2185 return NO_ERROR;
2186 }
2187 }
2188 return err;
2189}
2190
Mathias Agopian53331da2011-08-22 21:44:41 -07002191void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002192 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002193 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002194}
2195
Mathias Agopian59119e62010-10-11 12:37:43 -07002196// ---------------------------------------------------------------------------
2197
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002198status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002199 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2200{
2201 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002202 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002203}
2204
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002205status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002207{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002208 ATRACE_CALL();
2209
Mathias Agopian59119e62010-10-11 12:37:43 -07002210 if (!GLExtensions::getInstance().haveFramebufferObject())
2211 return INVALID_OPERATION;
2212
2213 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002214 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2215 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002216 const uint32_t hw_w = hw->getWidth();
2217 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002218 GLfloat u = 1;
2219 GLfloat v = 1;
2220
2221 // make sure to clear all GL error flags
2222 while ( glGetError() != GL_NO_ERROR ) ;
2223
2224 // create a FBO
2225 GLuint name, tname;
2226 glGenTextures(1, &tname);
2227 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002228 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2229 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002230 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2231 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002232 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002233 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002234 GLint tw = (2 << (31 - clz(hw_w)));
2235 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2237 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002238 u = GLfloat(hw_w) / tw;
2239 v = GLfloat(hw_h) / th;
2240 }
2241 glGenFramebuffersOES(1, &name);
2242 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002243 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2244 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002245
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002246 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002247 glDisable(GL_TEXTURE_EXTERNAL_OES);
2248 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002249 glClearColor(0,0,0,1);
2250 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002251 glMatrixMode(GL_MODELVIEW);
2252 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002253 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002254 const size_t count = layers.size();
2255 for (size_t i=0 ; i<count ; ++i) {
2256 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002257 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002258 }
2259
Mathias Agopian42977342012-08-05 00:40:46 -07002260 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002261
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002262 // back to main framebuffer
2263 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264 glDeleteFramebuffersOES(1, &name);
2265
2266 *textureName = tname;
2267 *uOut = u;
2268 *vOut = v;
2269 return NO_ERROR;
2270}
2271
2272// ---------------------------------------------------------------------------
2273
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002274status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002275 sp<IMemoryHeap>* heap,
2276 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002277 uint32_t sw, uint32_t sh,
2278 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002279{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002280 ATRACE_CALL();
2281
Mathias Agopian74c40c02010-09-29 13:02:36 -07002282 status_t result = PERMISSION_DENIED;
2283
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002284 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002285 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002286 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287
2288 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002289 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002290 const uint32_t hw_w = hw->getWidth();
2291 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002292
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002293 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002294 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002295 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002296 return PERMISSION_DENIED;
2297 }
2298
2299 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002300 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002301 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002302 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002303
2304 sw = (!sw) ? hw_w : sw;
2305 sh = (!sh) ? hw_h : sh;
2306 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002307 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002308
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002309// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2310// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002311
Mathias Agopian74c40c02010-09-29 13:02:36 -07002312 // make sure to clear all GL error flags
2313 while ( glGetError() != GL_NO_ERROR ) ;
2314
2315 // create a FBO
2316 GLuint name, tname;
2317 glGenRenderbuffersOES(1, &tname);
2318 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2319 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002320
Mathias Agopian74c40c02010-09-29 13:02:36 -07002321 glGenFramebuffersOES(1, &name);
2322 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2323 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2324 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2325
2326 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002327
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2329
2330 // invert everything, b/c glReadPixel() below will invert the FB
2331 glViewport(0, 0, sw, sh);
2332 glMatrixMode(GL_PROJECTION);
2333 glPushMatrix();
2334 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002335 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002336 glMatrixMode(GL_MODELVIEW);
2337
2338 // redraw the screen entirely...
2339 glClearColor(0,0,0,1);
2340 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002341
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002342 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002343 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344 for (size_t i=0 ; i<count ; ++i) {
2345 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002346 const uint32_t z = layer->drawingState().z;
2347 if (z >= minLayerZ && z <= maxLayerZ) {
2348 if (filtering) layer->setFiltering(true);
2349 layer->draw(hw);
2350 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002351 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 }
2353
Mathias Agopian74c40c02010-09-29 13:02:36 -07002354 // check for errors and return screen capture
2355 if (glGetError() != GL_NO_ERROR) {
2356 // error while rendering
2357 result = INVALID_OPERATION;
2358 } else {
2359 // allocate shared memory large enough to hold the
2360 // screen capture
2361 sp<MemoryHeapBase> base(
2362 new MemoryHeapBase(size, 0, "screen-capture") );
2363 void* const ptr = base->getBase();
2364 if (ptr) {
2365 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002366 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002367 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2368 if (glGetError() == GL_NO_ERROR) {
2369 *heap = base;
2370 *w = sw;
2371 *h = sh;
2372 *f = PIXEL_FORMAT_RGBA_8888;
2373 result = NO_ERROR;
2374 }
2375 } else {
2376 result = NO_MEMORY;
2377 }
2378 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 glViewport(0, 0, hw_w, hw_h);
2380 glMatrixMode(GL_PROJECTION);
2381 glPopMatrix();
2382 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 } else {
2384 result = BAD_VALUE;
2385 }
2386
2387 // release FBO resources
2388 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2389 glDeleteRenderbuffersOES(1, &tname);
2390 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002391
Mathias Agopian42977342012-08-05 00:40:46 -07002392 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002393
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002394// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002395
Mathias Agopian74c40c02010-09-29 13:02:36 -07002396 return result;
2397}
2398
2399
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002400status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002401 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002402 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002403 uint32_t sw, uint32_t sh,
2404 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002405{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002406 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002407 return BAD_VALUE;
2408
2409 if (!GLExtensions::getInstance().haveFramebufferObject())
2410 return INVALID_OPERATION;
2411
2412 class MessageCaptureScreen : public MessageBase {
2413 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002414 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002415 sp<IMemoryHeap>* heap;
2416 uint32_t* w;
2417 uint32_t* h;
2418 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 uint32_t sw;
2420 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002421 uint32_t minLayerZ;
2422 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002423 status_t result;
2424 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002425 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002426 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002427 uint32_t sw, uint32_t sh,
2428 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002429 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002430 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2431 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2432 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002433 {
2434 }
2435 status_t getResult() const {
2436 return result;
2437 }
2438 virtual bool handler() {
2439 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002440 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002441 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002442 return true;
2443 }
2444 };
2445
2446 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002447 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002448 status_t res = postMessageSync(msg);
2449 if (res == NO_ERROR) {
2450 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2451 }
2452 return res;
2453}
2454
2455// ---------------------------------------------------------------------------
2456
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002457SurfaceFlinger::LayerVector::LayerVector() {
2458}
2459
2460SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2461 : SortedVector<sp<LayerBase> >(rhs) {
2462}
2463
2464int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2465 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002466{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002467 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002468 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2469 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002470
2471 uint32_t ls = l->currentState().layerStack;
2472 uint32_t rs = r->currentState().layerStack;
2473 if (ls != rs)
2474 return ls - rs;
2475
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002476 uint32_t lz = l->currentState().z;
2477 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002478 if (lz != rz)
2479 return lz - rz;
2480
2481 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002482}
2483
2484// ---------------------------------------------------------------------------
2485
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002486SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2487 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002488}
2489
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002490SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2491 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002492 viewport.makeInvalid();
2493 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002494}
2495
2496// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497
Jamie Gennis9a78c902011-01-12 18:30:40 -08002498GraphicBufferAlloc::GraphicBufferAlloc() {}
2499
2500GraphicBufferAlloc::~GraphicBufferAlloc() {}
2501
2502sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002503 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002504 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2505 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002506 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002507 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002508 if (err == NO_MEMORY) {
2509 GraphicBuffer::dumpAllocationsToSystemLog();
2510 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002511 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002512 "failed (%s), handle=%p",
2513 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002514 return 0;
2515 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002516 return graphicBuffer;
2517}
2518
Jamie Gennis9a78c902011-01-12 18:30:40 -08002519// ---------------------------------------------------------------------------
2520
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521}; // namespace android