blob: 27677d6bf74b9faa813e6b2c37cbccb5b23c7930 [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
Mathias Agopiancde87a32012-09-13 14:09:01 -070046#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <utils/String8.h>
48#include <utils/String16.h>
49#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Mathias Agopian921e6ac2012-07-23 23:11:29 -070052#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
54#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080055#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070056#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070057#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080058#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070059#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070062#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Mathias Agopiana4912602012-07-12 14:25:33 -070065#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070066#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopiana67932f2011-04-20 14:20:59 -070068
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080069#define EGL_VERSION_HW_ANDROID 0x3143
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#define DISPLAY_COUNT 1
72
73namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian99b49842011-06-27 16:05:52 -070076const String16 sHardwareTest("android.permission.HARDWARE_TEST");
77const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
78const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
79const String16 sDump("android.permission.DUMP");
80
81// ---------------------------------------------------------------------------
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083SurfaceFlinger::SurfaceFlinger()
84 : BnSurfaceComposer(), Thread(false),
85 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070086 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070088 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070091 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700100 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101{
Steve Blocka19954a2012-01-04 20:05:49 +0000102 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103
104 // debugging stuff...
105 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 property_get("debug.sf.showupdates", value, "0");
108 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110 property_get("debug.sf.ddms", value, "0");
111 mDebugDDMS = atoi(value);
112 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700113 if (!startDdmConnection()) {
114 // start failed, and DDMS debugging not enabled
115 mDebugDDMS = 0;
116 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700118 ALOGI_IF(mDebugRegion, "showupdates enabled");
119 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800122void SurfaceFlinger::onFirstRef()
123{
124 mEventQueue.init(this);
125
126 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
127
128 // Wait for the main thread to be done with its initialization
129 mReadyToRunBarrier.wait();
130}
131
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133SurfaceFlinger::~SurfaceFlinger()
134{
Mathias Agopiana4912602012-07-12 14:25:33 -0700135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
136 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
137 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::binderDied(const wp<IBinder>& who)
141{
142 // the window manager died on us. prepare its eulogy.
143
Andy McFadden13a082e2012-08-24 10:16:42 -0700144 // restore initial conditions (default device unblank, etc)
145 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146
147 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700148 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149}
150
Mathias Agopian7e27f052010-05-28 14:22:23 -0700151sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Mathias Agopian96f08192010-06-02 23:28:45 -0700153 sp<ISurfaceComposerClient> bclient;
154 sp<Client> client(new Client(this));
155 status_t err = client->initCheck();
156 if (err == NO_ERROR) {
157 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 return bclient;
160}
161
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700162sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700163{
164 class DisplayToken : public BBinder {
165 sp<SurfaceFlinger> flinger;
166 virtual ~DisplayToken() {
167 // no more references, this display must be terminated
168 Mutex::Autolock _l(flinger->mStateLock);
169 flinger->mCurrentState.displays.removeItem(this);
170 flinger->setTransactionFlags(eDisplayTransactionNeeded);
171 }
172 public:
173 DisplayToken(const sp<SurfaceFlinger>& flinger)
174 : flinger(flinger) {
175 }
176 };
177
178 sp<BBinder> token = new DisplayToken(this);
179
180 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700181 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700182 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700183 mCurrentState.displays.add(token, info);
184
185 return token;
186}
187
188sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700189 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700190 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
191 return NULL;
192 }
193 return mDefaultDisplays[id];
194}
195
Jamie Gennis9a78c902011-01-12 18:30:40 -0800196sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
197{
198 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
199 return gba;
200}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202void SurfaceFlinger::bootFinished()
203{
204 const nsecs_t now = systemTime();
205 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000206 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700207 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700208
209 // wait patiently for the window manager death
210 const String16 name("window");
211 sp<IBinder> window(defaultServiceManager()->getService(name));
212 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700213 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700214 }
215
216 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700217 // formerly we would just kill the process, but we now ask it to exit so it
218 // can choose where to stop the animation.
219 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220}
221
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700222void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
223 class MessageDestroyGLTexture : public MessageBase {
224 GLuint texture;
225 public:
226 MessageDestroyGLTexture(GLuint texture)
227 : texture(texture) {
228 }
229 virtual bool handler() {
230 glDeleteTextures(1, &texture);
231 return true;
232 }
233 };
234 postMessageAsync(new MessageDestroyGLTexture(texture));
235}
236
Mathias Agopiana4912602012-07-12 14:25:33 -0700237status_t SurfaceFlinger::selectConfigForPixelFormat(
238 EGLDisplay dpy,
239 EGLint const* attrs,
240 PixelFormat format,
241 EGLConfig* outConfig)
242{
243 EGLConfig config = NULL;
244 EGLint numConfigs = -1, n=0;
245 eglGetConfigs(dpy, NULL, 0, &numConfigs);
246 EGLConfig* const configs = new EGLConfig[numConfigs];
247 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700248
Mathias Agopiana4912602012-07-12 14:25:33 -0700249 for (int i=0 ; i<n ; i++) {
250 EGLint nativeVisualId = 0;
251 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
252 if (nativeVisualId>0 && format == nativeVisualId) {
253 *outConfig = configs[i];
254 delete [] configs;
255 return NO_ERROR;
256 }
257 }
258 delete [] configs;
259 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260}
261
Mathias Agopiana4912602012-07-12 14:25:33 -0700262EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
263 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
264 // it is to be used with WIFI displays
265 EGLConfig config;
266 EGLint dummy;
267 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700268
Mathias Agopiana4912602012-07-12 14:25:33 -0700269 EGLint attribs[] = {
270 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiancde87a32012-09-13 14:09:01 -0700271 EGL_RED_SIZE, 8,
272 EGL_GREEN_SIZE, 8,
273 EGL_BLUE_SIZE, 8,
274 // EGL_RECORDABLE_ANDROID must be last so that we can retry
275 // without it easily (see below)
Mathias Agopiana4912602012-07-12 14:25:33 -0700276 EGL_RECORDABLE_ANDROID, EGL_TRUE,
277 EGL_NONE
278 };
279 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
280 if (err) {
281 // maybe we failed because of EGL_RECORDABLE_ANDROID
282 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
Mathias Agopiancde87a32012-09-13 14:09:01 -0700283 attribs[NELEM(attribs) - 3] = EGL_NONE;
Mathias Agopiana4912602012-07-12 14:25:33 -0700284 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
285 }
286 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
287 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
288 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
289 }
290 return config;
291}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
Mathias Agopiana4912602012-07-12 14:25:33 -0700293EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
294 // Also create our EGLContext
295 EGLint contextAttributes[] = {
296#ifdef EGL_IMG_context_priority
297#ifdef HAS_CONTEXT_PRIORITY
298#warning "using EGL_IMG_context_priority"
299 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
300#endif
301#endif
302 EGL_NONE, EGL_NONE
303 };
304 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
305 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
306 return ctxt;
307}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
Mathias Agopiancde87a32012-09-13 14:09:01 -0700309void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700310 GLExtensions& extensions(GLExtensions::getInstance());
311 extensions.initWithGLStrings(
312 glGetString(GL_VENDOR),
313 glGetString(GL_RENDERER),
314 glGetString(GL_VERSION),
315 glGetString(GL_EXTENSIONS),
316 eglQueryString(display, EGL_VENDOR),
317 eglQueryString(display, EGL_VERSION),
318 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700319
Mathias Agopiana4912602012-07-12 14:25:33 -0700320 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
321 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700324 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 glShadeModel(GL_FLAT);
327 glDisable(GL_DITHER);
328 glDisable(GL_CULL_FACE);
329
Mathias Agopiana4912602012-07-12 14:25:33 -0700330 struct pack565 {
331 inline uint16_t operator() (int r, int g, int b) const {
332 return (r<<11)|(g<<5)|b;
333 }
334 } pack565;
335
Jamie Gennis9575f602011-10-07 14:51:16 -0700336 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
337 glGenTextures(1, &mProtectedTexName);
338 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
339 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
340 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
342 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
343 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
344 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345
Mathias Agopiana4912602012-07-12 14:25:33 -0700346 // print some debugging info
347 EGLint r,g,b,a;
348 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
349 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
350 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
351 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
352 ALOGI("EGL informations:");
353 ALOGI("vendor : %s", extensions.getEglVendor());
354 ALOGI("version : %s", extensions.getEglVersion());
355 ALOGI("extensions: %s", extensions.getEglExtension());
356 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
357 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
358 ALOGI("OpenGL ES informations:");
359 ALOGI("vendor : %s", extensions.getVendor());
360 ALOGI("renderer : %s", extensions.getRenderer());
361 ALOGI("version : %s", extensions.getVersion());
362 ALOGI("extensions: %s", extensions.getExtension());
363 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
364 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
365}
366
Mathias Agopiana4912602012-07-12 14:25:33 -0700367status_t SurfaceFlinger::readyToRun()
368{
369 ALOGI( "SurfaceFlinger's main thread ready to run. "
370 "Initializing graphics H/W...");
371
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700372 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700373 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
374 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700375
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700376 // Initialize the H/W composer object. There may or may not be an
377 // actual hardware composer underneath.
378 mHwc = new HWComposer(this,
379 *static_cast<HWComposer::EventHandler *>(this));
380
Mathias Agopiana4912602012-07-12 14:25:33 -0700381 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700382 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700383 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
384 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700385
Mathias Agopianda27af92012-09-13 18:17:13 -0700386 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
387 "couldn't create EGLContext");
388
Mathias Agopiancde87a32012-09-13 14:09:01 -0700389 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700390 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700391 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700392 mCurrentState.displays.add(mDefaultDisplays[i],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700393 DisplayDeviceState(DisplayDevice::DisplayType(i)));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700394 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700395
396 // The main display is a bit special and always exists
397 //
398 // if we didn't add it here, it would be added automatically during the
399 // first transaction, however this would also create some difficulties:
400 //
401 // - there would be a race where a client could call getDisplayInfo(),
402 // for instance before the DisplayDevice is created.
403 //
404 // - we need a GL context current in a few places, when initializing
405 // OpenGL ES (see below), or creating a layer,
406 // or when a texture is (asynchronously) destroyed, and for that
407 // we need a valid surface, so it's conveniant to use the main display
408 // for that.
409
410 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc);
411 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
412 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700413 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700414 DisplayDevice::DISPLAY_PRIMARY,
415 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700416 stc, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700417 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700418
Mathias Agopiancde87a32012-09-13 14:09:01 -0700419
Mathias Agopiana4912602012-07-12 14:25:33 -0700420 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700421 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
422 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800423
Mathias Agopian028508c2012-07-25 21:12:12 -0700424 // start the EventThread
425 mEventThread = new EventThread(this);
426 mEventQueue.setEventThread(mEventThread);
427
Mathias Agopian92a979a2012-08-02 18:32:23 -0700428 // initialize our drawing state
429 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700430
Mathias Agopiancde87a32012-09-13 14:09:01 -0700431
Mathias Agopiana4912602012-07-12 14:25:33 -0700432 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800433 mReadyToRunBarrier.open();
434
Andy McFadden13a082e2012-08-24 10:16:42 -0700435 // set initial conditions (e.g. unblank default device)
436 initializeDisplays();
437
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700438 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700439 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700440
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 return NO_ERROR;
442}
443
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700444int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
445 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
446 type : mHwc->allocateDisplayId();
447}
448
Mathias Agopiana67e4182012-06-19 17:26:12 -0700449void SurfaceFlinger::startBootAnim() {
450 // start boot animation
451 property_set("service.bootanim.exit", "0");
452 property_set("ctl.start", "bootanim");
453}
454
Mathias Agopiana4912602012-07-12 14:25:33 -0700455uint32_t SurfaceFlinger::getMaxTextureSize() const {
456 return mMaxTextureSize;
457}
458
459uint32_t SurfaceFlinger::getMaxViewportDims() const {
460 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
461 mMaxViewportDims[0] : mMaxViewportDims[1];
462}
463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800465
Jamie Gennis582270d2011-08-17 18:19:00 -0700466bool SurfaceFlinger::authenticateSurfaceTexture(
467 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800468 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700469 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800470
471 // Check the visible layer list for the ISurface
472 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
473 size_t count = currentLayers.size();
474 for (size_t i=0 ; i<count ; i++) {
475 const sp<LayerBase>& layer(currentLayers[i]);
476 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700477 if (lbc != NULL) {
478 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
479 if (lbcBinder == surfaceTextureBinder) {
480 return true;
481 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800482 }
483 }
484
485 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700486 // SurfaceTexture gets destroyed before all the clients are done using it,
487 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800488 // will instead fail later on when the client tries to use the surface,
489 // which should be reported as "surface XYZ returned an -ENODEV". The
490 // purgatorized layers are no less authentic than the visible ones, so this
491 // should not cause any harm.
492 size_t purgatorySize = mLayerPurgatory.size();
493 for (size_t i=0 ; i<purgatorySize ; i++) {
494 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
495 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700496 if (lbc != NULL) {
497 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
498 if (lbcBinder == surfaceTextureBinder) {
499 return true;
500 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800501 }
502 }
503
504 return false;
505}
506
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700507status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
508 // TODO: this is mostly here only for compatibility
509 // the display size is needed but the display metrics should come from elsewhere
510 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
511 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700512 return BAD_INDEX;
513 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700514
515 const HWComposer& hwc(getHwComposer());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700516 float xdpi = hwc.getDpiX(HWC_DISPLAY_PRIMARY);
517 float ydpi = hwc.getDpiY(HWC_DISPLAY_PRIMARY);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700518
519 // TODO: Not sure if display density should handled by SF any longer
520 class Density {
521 static int getDensityFromProperty(char const* propName) {
522 char property[PROPERTY_VALUE_MAX];
523 int density = 0;
524 if (property_get(propName, property, NULL) > 0) {
525 density = atoi(property);
526 }
527 return density;
528 }
529 public:
530 static int getEmuDensity() {
531 return getDensityFromProperty("qemu.sf.lcd_density"); }
532 static int getBuildDensity() {
533 return getDensityFromProperty("ro.sf.lcd_density"); }
534 };
535 // The density of the device is provided by a build property
536 float density = Density::getBuildDensity() / 160.0f;
537 if (density == 0) {
538 // the build doesn't provide a density -- this is wrong!
539 // use xdpi instead
540 ALOGE("ro.sf.lcd_density must be defined as a build property");
541 density = xdpi / 160.0f;
542 }
543 if (Density::getEmuDensity()) {
544 // if "qemu.sf.lcd_density" is specified, it overrides everything
545 xdpi = ydpi = density = Density::getEmuDensity();
546 density /= 160.0f;
547 }
548
Mathias Agopian42977342012-08-05 00:40:46 -0700549 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
550 info->w = hw->getWidth();
551 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700552 info->xdpi = xdpi;
553 info->ydpi = ydpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700554 info->fps = float(1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY));
Mathias Agopian8b736f12012-08-13 17:54:26 -0700555 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700556 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700557 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700558 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700559 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700560}
561
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800562// ----------------------------------------------------------------------------
563
564sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800565 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700566}
567
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700568void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700569
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700570 sp<IBinder> token;
571 { // scope for the lock
572 Mutex::Autolock _l(mStateLock);
573 token = mExtDisplayToken;
574 }
575
576 if (token == 0) {
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700577 token = createDisplay(String8("Display from connectDisplay"));
Mathias Agopian3094df32012-06-18 18:06:45 -0700578 }
579
580 { // scope for the lock
581 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700582 if (surface == 0) {
583 // release our current display. we're guarantee to have
584 // a reference to it (token), while we hold the lock
585 mExtDisplayToken = 0;
586 } else {
587 mExtDisplayToken = token;
588 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700589
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700590 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
591 info.surface = surface;
592 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700593 }
594}
595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800597
598void SurfaceFlinger::waitForEvent() {
599 mEventQueue.waitMessage();
600}
601
602void SurfaceFlinger::signalTransaction() {
603 mEventQueue.invalidate();
604}
605
606void SurfaceFlinger::signalLayerUpdate() {
607 mEventQueue.invalidate();
608}
609
610void SurfaceFlinger::signalRefresh() {
611 mEventQueue.refresh();
612}
613
614status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
615 nsecs_t reltime, uint32_t flags) {
616 return mEventQueue.postMessage(msg, reltime);
617}
618
619status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
620 nsecs_t reltime, uint32_t flags) {
621 status_t res = mEventQueue.postMessage(msg, reltime);
622 if (res == NO_ERROR) {
623 msg->wait();
624 }
625 return res;
626}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopian4fec8732012-06-29 14:12:52 -0700628bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 return true;
631}
632
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700633void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700634 if (mEventThread == NULL) {
635 // This is a temporary workaround for b/7145521. A non-null pointer
636 // does not mean EventThread has finished initializing, so this
637 // is not a correct fix.
638 ALOGW("WARNING: EventThread not started, ignoring vsync");
639 return;
640 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700641 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
642 // we should only receive DisplayDevice::DisplayType from the vsync callback
643 const wp<IBinder>& token(mDefaultDisplays[type]);
644 mEventThread->onVSyncReceived(token, timestamp);
645 }
Mathias Agopian86303202012-07-24 22:46:10 -0700646}
647
648void SurfaceFlinger::eventControl(int event, int enabled) {
649 getHwComposer().eventControl(event, enabled);
650}
651
Mathias Agopian4fec8732012-06-29 14:12:52 -0700652void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800653 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800654 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700655 case MessageQueue::INVALIDATE:
656 handleMessageTransaction();
657 handleMessageInvalidate();
658 signalRefresh();
659 break;
660 case MessageQueue::REFRESH:
661 handleMessageRefresh();
662 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800663 }
664}
665
Mathias Agopian4fec8732012-06-29 14:12:52 -0700666void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700667 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700668 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700669 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700670 }
671}
672
673void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700674 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700675 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700676}
677
678void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700679 ATRACE_CALL();
680 preComposition();
681 rebuildLayerStacks();
682 setUpHWComposer();
683 doDebugFlashRegions();
684 doComposition();
685 postComposition();
686}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700687
Mathias Agopiancd60f992012-08-16 16:28:27 -0700688void SurfaceFlinger::doDebugFlashRegions()
689{
690 // is debugging enabled
691 if (CC_LIKELY(!mDebugRegion))
692 return;
693
694 const bool repaintEverything = mRepaintEverything;
695 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
696 const sp<DisplayDevice>& hw(mDisplays[dpy]);
697 if (hw->canDraw()) {
698 // transform the dirty region into this screen's coordinate space
699 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
700 if (!dirtyRegion.isEmpty()) {
701 // redraw the whole screen
702 doComposeSurfaces(hw, Region(hw->bounds()));
703
704 // and draw the dirty region
705 glDisable(GL_TEXTURE_EXTERNAL_OES);
706 glDisable(GL_TEXTURE_2D);
707 glDisable(GL_BLEND);
708 glColor4f(1, 0, 1, 1);
709 const int32_t height = hw->getHeight();
710 Region::const_iterator it = dirtyRegion.begin();
711 Region::const_iterator const end = dirtyRegion.end();
712 while (it != end) {
713 const Rect& r = *it++;
714 GLfloat vertices[][2] = {
715 { r.left, height - r.top },
716 { r.left, height - r.bottom },
717 { r.right, height - r.bottom },
718 { r.right, height - r.top }
719 };
720 glVertexPointer(2, GL_FLOAT, 0, vertices);
721 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
722 }
723 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700724 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700725 }
726 }
727 }
728
729 postFramebuffer();
730
731 if (mDebugRegion > 1) {
732 usleep(mDebugRegion * 1000);
733 }
734}
735
736void SurfaceFlinger::preComposition()
737{
738 bool needExtraInvalidate = false;
739 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
740 const size_t count = currentLayers.size();
741 for (size_t i=0 ; i<count ; i++) {
742 if (currentLayers[i]->onPreComposition()) {
743 needExtraInvalidate = true;
744 }
745 }
746 if (needExtraInvalidate) {
747 signalLayerUpdate();
748 }
749}
750
751void SurfaceFlinger::postComposition()
752{
753 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
754 const size_t count = currentLayers.size();
755 for (size_t i=0 ; i<count ; i++) {
756 currentLayers[i]->onPostComposition();
757 }
758}
759
760void SurfaceFlinger::rebuildLayerStacks() {
761 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700762 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700763 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700764 mVisibleRegionsDirty = false;
765 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700766
Mathias Agopian87baae12012-07-31 12:38:26 -0700767 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700768 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700769 Region opaqueRegion;
770 Region dirtyRegion;
771 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700772 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700773 const Transform& tr(hw->getTransform());
774 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700775 if (hw->canDraw()) {
776 SurfaceFlinger::computeVisibleRegions(currentLayers,
777 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700778
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700779 const size_t count = currentLayers.size();
780 for (size_t i=0 ; i<count ; i++) {
781 const sp<LayerBase>& layer(currentLayers[i]);
782 const Layer::State& s(layer->drawingState());
783 if (s.layerStack == hw->getLayerStack()) {
784 Region visibleRegion(tr.transform(layer->visibleRegion));
785 visibleRegion.andSelf(bounds);
786 if (!visibleRegion.isEmpty()) {
787 layersSortedByZ.add(layer);
788 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700789 }
790 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700791 }
Mathias Agopian42977342012-08-05 00:40:46 -0700792 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700793 hw->undefinedRegion.set(bounds);
794 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
795 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700796 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700797 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700798}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700799
Mathias Agopiancd60f992012-08-16 16:28:27 -0700800void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700801 HWComposer& hwc(getHwComposer());
802 if (hwc.initCheck() == NO_ERROR) {
803 // build the h/w work list
804 const bool workListsDirty = mHwWorkListDirty;
805 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700806 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700807 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700808 const int32_t id = hw->getHwcDisplayId();
809 if (id >= 0) {
810 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700812 const size_t count = currentLayers.size();
813 if (hwc.createWorkList(id, count) >= 0) {
814 HWComposer::LayerListIterator cur = hwc.begin(id);
815 const HWComposer::LayerListIterator end = hwc.end(id);
816 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
817 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700818
Mathias Agopiane60b0682012-08-21 23:34:09 -0700819 if (CC_UNLIKELY(workListsDirty)) {
820 layer->setGeometry(hw, *cur);
821 if (mDebugDisableHWC || mDebugRegion) {
822 cur->setSkip(true);
823 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700824 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700825
Mathias Agopiane60b0682012-08-21 23:34:09 -0700826 /*
827 * update the per-frame h/w composer data for each layer
828 * and build the transparent region of the FB
829 */
830 layer->setPerFrameData(hw, *cur);
831 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700832 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700833 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700834 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700835 status_t err = hwc.prepare();
836 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
837 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700838}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700839
Mathias Agopiancd60f992012-08-16 16:28:27 -0700840void SurfaceFlinger::doComposition() {
841 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700842 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700843 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700844 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 if (hw->canDraw()) {
846 // transform the dirty region into this screen's coordinate space
847 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
848 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700850 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852 hw->dirtyRegion.clear();
853 hw->flip(hw->swapRegion);
854 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700855 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700856 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700857 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700858 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700859 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700860}
861
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862void SurfaceFlinger::postFramebuffer()
863{
Mathias Agopian841cde52012-03-01 15:44:37 -0800864 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800865
Mathias Agopiana44b0412011-10-16 18:46:35 -0700866 const nsecs_t now = systemTime();
867 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700868
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700869 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700870 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700871 // FIXME: EGL spec says:
872 // "surface must be bound to the calling thread's current context,
873 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700874 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700875 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700876 }
877
Mathias Agopian92a979a2012-08-02 18:32:23 -0700878 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700879 sp<const DisplayDevice> hw(mDisplays[dpy]);
880 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700881 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700882 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700883 int32_t id = hw->getHwcDisplayId();
884 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700885 HWComposer::LayerListIterator cur = hwc.begin(id);
886 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700887 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700888 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700889 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700890 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700891 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700892 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700893 }
Jesse Hallef194142012-06-14 14:45:17 -0700894 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800895 }
896
Mathias Agopiana44b0412011-10-16 18:46:35 -0700897 mLastSwapBufferTime = systemTime() - now;
898 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899}
900
Mathias Agopian87baae12012-07-31 12:38:26 -0700901void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902{
Mathias Agopian841cde52012-03-01 15:44:37 -0800903 ATRACE_CALL();
904
Mathias Agopianca4d3602011-05-19 15:38:14 -0700905 Mutex::Autolock _l(mStateLock);
906 const nsecs_t now = systemTime();
907 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908
Mathias Agopianca4d3602011-05-19 15:38:14 -0700909 // Here we're guaranteed that some transaction flags are set
910 // so we can call handleTransactionLocked() unconditionally.
911 // We call getTransactionFlags(), which will also clear the flags,
912 // with mStateLock held to guarantee that mCurrentState won't change
913 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700914
Mathias Agopiane57f2922012-08-09 16:29:12 -0700915 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700916 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700917
Mathias Agopianca4d3602011-05-19 15:38:14 -0700918 mLastTransactionTime = systemTime() - now;
919 mDebugInTransaction = 0;
920 invalidateHwcGeometry();
921 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700922}
923
Mathias Agopian87baae12012-07-31 12:38:26 -0700924void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700925{
926 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 const size_t count = currentLayers.size();
928
929 /*
930 * Traversal of the children
931 * (perform the transaction for each of them if needed)
932 */
933
Mathias Agopian3559b072012-08-15 13:46:03 -0700934 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700936 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
938 if (!trFlags) continue;
939
940 const uint32_t flags = layer->doTransaction(0);
941 if (flags & Layer::eVisibleRegion)
942 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 }
944 }
945
946 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700947 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 */
949
Mathias Agopiane57f2922012-08-09 16:29:12 -0700950 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700951 // here we take advantage of Vector's copy-on-write semantics to
952 // improve performance by skipping the transaction entirely when
953 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700954 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
955 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700956 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700958 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700959 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700960
961 // find the displays that were removed
962 // (ie: in drawing state but not in current state)
963 // also handle displays that changed
964 // (ie: displays that are in both lists)
965 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700966 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
967 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700968 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700969 if (!draw[i].isMainDisplay()) {
970 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700971 } else {
972 ALOGW("trying to remove the main display");
973 }
974 } else {
975 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700976 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700977 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700978 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700979 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700980 // recreating the DisplayDevice, so we just remove it
981 // from the drawing state, so that it get re-added
982 // below.
983 mDisplays.removeItem(display);
984 mDrawingState.displays.removeItemsAt(i);
985 dc--; i--;
986 // at this point we must loop to the next item
987 continue;
988 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700989
Mathias Agopian93997a82012-08-29 17:30:36 -0700990 const sp<DisplayDevice>& disp(getDisplayDevice(display));
991 if (disp != NULL) {
992 if (state.layerStack != draw[i].layerStack) {
993 disp->setLayerStack(state.layerStack);
994 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -0700995 if ((state.orientation != draw[i].orientation)
996 || (state.viewport != draw[i].viewport)
997 || (state.frame != draw[i].frame))
998 {
999 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001000 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001001 }
Andy McFadden69052052012-09-14 16:10:11 -07001002
1003 // Walk through all the layers in currentLayers,
1004 // and update their transform hint.
1005 //
1006 // TODO: we could be much more clever about which
1007 // layers we touch and how often we do these updates
1008 // (e.g. only touch the layers associated with this
1009 // display, and only on a rotation).
1010 for (size_t i = 0; i < count; i++) {
1011 const sp<LayerBase>& layerBase = currentLayers[i];
1012 layerBase->updateTransformHint();
1013 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001014 }
1015 }
1016 }
1017
1018 // find displays that were added
1019 // (ie: in current state but not in drawing state)
1020 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001021 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001022 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001023
1024 sp<FramebufferSurface> fbs;
1025 sp<SurfaceTextureClient> stc;
1026 if (!state.isVirtualDisplay()) {
1027
1028 ALOGE_IF(state.surface!=NULL,
1029 "adding a supported display, but rendering "
1030 "surface is provided (%p), ignoring it",
1031 state.surface.get());
1032
1033 // for supported (by hwc) displays we provide our
1034 // own rendering surface
1035 fbs = new FramebufferSurface(*mHwc);
1036 stc = new SurfaceTextureClient(
1037 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1038 } else {
1039 if (state.surface != NULL) {
1040 stc = new SurfaceTextureClient(state.surface);
1041 }
1042 }
1043
1044 const wp<IBinder>& display(curr.keyAt(i));
1045 if (stc != NULL) {
1046 sp<DisplayDevice> hw = new DisplayDevice(this,
1047 state.type, display, stc, fbs, mEGLConfig);
1048 hw->setLayerStack(state.layerStack);
1049 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001050 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001051 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001052 mDisplays.add(display, hw);
1053 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1054 // notify the system that this display is now up
1055 // (note onScreenAcquired() is safe to call from
1056 // here because we're in the main thread)
1057 onScreenAcquired(hw);
1058 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001059 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001060 }
1061 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001063 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
Mathias Agopian3559b072012-08-15 13:46:03 -07001065 /*
1066 * Perform our own transaction if needed
1067 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001068
Mathias Agopiancd60f992012-08-16 16:28:27 -07001069 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1070 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001071 // layers have been added
1072 mVisibleRegionsDirty = true;
1073 }
1074
1075 // some layers might have been removed, so
1076 // we need to update the regions they're exposing.
1077 if (mLayersRemoved) {
1078 mLayersRemoved = false;
1079 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001080 const size_t count = previousLayers.size();
1081 for (size_t i=0 ; i<count ; i++) {
1082 const sp<LayerBase>& layer(previousLayers[i]);
1083 if (currentLayers.indexOf(layer) < 0) {
1084 // this layer is not visible anymore
1085 // TODO: we could traverse the tree from front to back and
1086 // compute the actual visible region
1087 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001088 const Layer::State& s(layer->drawingState());
1089 Region visibleReg = s.transform.transform(
1090 Region(Rect(s.active.w, s.active.h)));
1091 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001092 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 }
1095
1096 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001097}
1098
1099void SurfaceFlinger::commitTransaction()
1100{
1101 if (!mLayersPendingRemoval.isEmpty()) {
1102 // Notify removed layers now that they can't be drawn from
1103 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1104 mLayersPendingRemoval[i]->onRemoved();
1105 }
1106 mLayersPendingRemoval.clear();
1107 }
1108
1109 mDrawingState = mCurrentState;
1110 mTransationPending = false;
1111 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112}
1113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001115 const LayerVector& currentLayers, uint32_t layerStack,
1116 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117{
Mathias Agopian841cde52012-03-01 15:44:37 -08001118 ATRACE_CALL();
1119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 Region aboveOpaqueLayers;
1121 Region aboveCoveredLayers;
1122 Region dirty;
1123
Mathias Agopian87baae12012-07-31 12:38:26 -07001124 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125
1126 size_t i = currentLayers.size();
1127 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001128 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129
1130 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001131 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132
Mathias Agopian87baae12012-07-31 12:38:26 -07001133 // only consider the layers on the given later stack
1134 if (s.layerStack != layerStack)
1135 continue;
1136
Mathias Agopianab028732010-03-16 16:41:46 -07001137 /*
1138 * opaqueRegion: area of a surface that is fully opaque.
1139 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001141
1142 /*
1143 * visibleRegion: area of a surface that is visible on screen
1144 * and not fully transparent. This is essentially the layer's
1145 * footprint minus the opaque regions above it.
1146 * Areas covered by a translucent surface are considered visible.
1147 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001149
1150 /*
1151 * coveredRegion: area of a surface that is covered by all
1152 * visible regions above it (which includes the translucent areas).
1153 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001155
1156
1157 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001158 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001159 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001160 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001162 if (!visibleRegion.isEmpty()) {
1163 // Remove the transparent area from the visible region
1164 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001165 Region transparentRegionScreen;
1166 const Transform tr(s.transform);
1167 if (tr.transformed()) {
1168 if (tr.preserveRects()) {
1169 // transform the transparent region
1170 transparentRegionScreen = tr.transform(s.transparentRegion);
1171 } else {
1172 // transformation too complex, can't do the
1173 // transparent region optimization.
1174 transparentRegionScreen.clear();
1175 }
1176 } else {
1177 transparentRegionScreen = s.transparentRegion;
1178 }
1179 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001180 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181
Mathias Agopianab028732010-03-16 16:41:46 -07001182 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001183 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001184 if (s.alpha==255 && !translucent &&
1185 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1186 // the opaque region is the layer's footprint
1187 opaqueRegion = visibleRegion;
1188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
1190 }
1191
Mathias Agopianab028732010-03-16 16:41:46 -07001192 // Clip the covered region to the visible region
1193 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1194
1195 // Update aboveCoveredLayers for next (lower) layer
1196 aboveCoveredLayers.orSelf(visibleRegion);
1197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 // subtract the opaque region covered by the layers above us
1199 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200
1201 // compute this layer's dirty region
1202 if (layer->contentDirty) {
1203 // we need to invalidate the whole region
1204 dirty = visibleRegion;
1205 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001206 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 layer->contentDirty = false;
1208 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001209 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001210 * the exposed region consists of two components:
1211 * 1) what's VISIBLE now and was COVERED before
1212 * 2) what's EXPOSED now less what was EXPOSED before
1213 *
1214 * note that (1) is conservative, we start with the whole
1215 * visible region but only keep what used to be covered by
1216 * something -- which mean it may have been exposed.
1217 *
1218 * (2) handles areas that were not covered by anything but got
1219 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001220 */
Mathias Agopianab028732010-03-16 16:41:46 -07001221 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001222 const Region oldVisibleRegion = layer->visibleRegion;
1223 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001224 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1225 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 }
1227 dirty.subtractSelf(aboveOpaqueLayers);
1228
1229 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001230 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231
Mathias Agopianab028732010-03-16 16:41:46 -07001232 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 // Store the visible region is screen space
1236 layer->setVisibleRegion(visibleRegion);
1237 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 }
1239
Mathias Agopian87baae12012-07-31 12:38:26 -07001240 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241}
1242
Mathias Agopian87baae12012-07-31 12:38:26 -07001243void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1244 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001245 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001246 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1247 if (hw->getLayerStack() == layerStack) {
1248 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001249 }
1250 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001251}
1252
1253void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001255 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256
Mathias Agopian4fec8732012-06-29 14:12:52 -07001257 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001258 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001259 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001260 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001261 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001262 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001263 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001264 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001265 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001266
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001267 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268}
1269
Mathias Agopianad456f92011-01-13 17:53:01 -08001270void SurfaceFlinger::invalidateHwcGeometry()
1271{
1272 mHwWorkListDirty = true;
1273}
1274
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001275
Mathias Agopiancd60f992012-08-16 16:28:27 -07001276void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001277 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278{
Mathias Agopian87baae12012-07-31 12:38:26 -07001279 Region dirtyRegion(inDirtyRegion);
1280
Mathias Agopianb8a55602009-06-26 19:06:36 -07001281 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001282 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283
Mathias Agopian42977342012-08-05 00:40:46 -07001284 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001285 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001286 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1287 // takes a rectangle, we must make sure to update that whole
1288 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001289 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001290 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001291 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001292 // We need to redraw the rectangle that will be updated
1293 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001294 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001295 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001296 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001297 } else {
1298 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001299 dirtyRegion.set(hw->bounds());
1300 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 }
1302 }
1303
Mathias Agopiancd60f992012-08-16 16:28:27 -07001304 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001306 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001307 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001308
1309 // swap buffers (presentation)
1310 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311}
1312
Mathias Agopiancd60f992012-08-16 16:28:27 -07001313void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001314{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001315 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001316 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001317 HWComposer::LayerListIterator cur = hwc.begin(id);
1318 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001319
Mathias Agopian85d751c2012-08-29 16:59:24 -07001320 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1321 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001322 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001323
1324 // set the frame buffer
1325 glMatrixMode(GL_MODELVIEW);
1326 glLoadIdentity();
1327
1328 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001329 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001330 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001331 // when using overlays, we assume a fully transparent framebuffer
1332 // NOTE: we could reduce how much we need to clear, for instance
1333 // remove where there are opaque FB layers. however, on some
1334 // GPUs doing a "clean slate" glClear might be more efficient.
1335 // We'll revisit later if needed.
1336 glClearColor(0, 0, 0, 0);
1337 glClear(GL_COLOR_BUFFER_BIT);
1338 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001339 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001340 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001341 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001342 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001343 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001344 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001345 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001346 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001347
Mathias Agopian85d751c2012-08-29 16:59:24 -07001348 /*
1349 * and then, render the layers targeted at the framebuffer
1350 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001351
Mathias Agopian85d751c2012-08-29 16:59:24 -07001352 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1353 const size_t count = layers.size();
1354 const Transform& tr = hw->getTransform();
1355 if (cur != end) {
1356 // we're using h/w composer
1357 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001358 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001359 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001360 if (!clip.isEmpty()) {
1361 switch (cur->getCompositionType()) {
1362 case HWC_OVERLAY: {
1363 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1364 && i
1365 && layer->isOpaque()
1366 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001367 // never clear the very first layer since we're
1368 // guaranteed the FB is already cleared
1369 layer->clearWithOpenGL(hw, clip);
1370 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001371 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001372 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001373 case HWC_FRAMEBUFFER: {
1374 layer->draw(hw, clip);
1375 break;
1376 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001377 case HWC_FRAMEBUFFER_TARGET: {
1378 // this should not happen as the iterator shouldn't
1379 // let us get there.
1380 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1381 break;
1382 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001383 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001384 }
1385 layer->setAcquireFence(hw, *cur);
1386 }
1387 } else {
1388 // we're not using h/w composer
1389 for (size_t i=0 ; i<count ; ++i) {
1390 const sp<LayerBase>& layer(layers[i]);
1391 const Region clip(dirty.intersect(
1392 tr.transform(layer->visibleRegion)));
1393 if (!clip.isEmpty()) {
1394 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001395 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001396 }
1397 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398}
1399
Mathias Agopian55801e42012-08-27 18:54:24 -07001400void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1401 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001403 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001404 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001405 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001406 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001407
Mathias Agopian55801e42012-08-27 18:54:24 -07001408 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001409 Region::const_iterator it = region.begin();
1410 Region::const_iterator const end = region.end();
1411 while (it != end) {
1412 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001413 GLfloat vertices[][2] = {
1414 { r.left, height - r.top },
1415 { r.left, height - r.bottom },
1416 { r.right, height - r.bottom },
1417 { r.right, height - r.top }
1418 };
1419 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001420 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422}
1423
Mathias Agopian96f08192010-06-02 23:28:45 -07001424ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1425 const sp<LayerBaseClient>& lbc)
1426{
Mathias Agopian96f08192010-06-02 23:28:45 -07001427 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001428 size_t name = client->attachLayer(lbc);
1429
Mathias Agopian96f08192010-06-02 23:28:45 -07001430 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001431 Mutex::Autolock _l(mStateLock);
1432 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001433
Mathias Agopian4f113742011-05-03 16:21:41 -07001434 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001435}
1436
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001437status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438{
1439 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001440 status_t err = purgatorizeLayer_l(layer);
1441 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001442 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001443 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444}
1445
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001446status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447{
1448 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1449 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001450 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 return NO_ERROR;
1452 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001453 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454}
1455
Mathias Agopian9a112062009-04-17 19:36:26 -07001456status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1457{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001458 // First add the layer to the purgatory list, which makes sure it won't
1459 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001460 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001461 if (err >= 0) {
1462 mLayerPurgatory.add(layerBase);
1463 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001464
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001465 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001466
Mathias Agopian3d579642009-06-04 18:46:21 -07001467 // it's possible that we don't find a layer, because it might
1468 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001469 // from the user because there is a race between Client::destroySurface(),
1470 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001471 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1472}
1473
Mathias Agopiandea20b12011-05-03 17:04:02 -07001474uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1475{
1476 return android_atomic_release_load(&mTransactionFlags);
1477}
1478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1480{
1481 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1482}
1483
Mathias Agopianbb641242010-05-18 17:06:55 -07001484uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485{
1486 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1487 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001488 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
1490 return old;
1491}
1492
Mathias Agopian8b33f032012-07-24 20:43:54 -07001493void SurfaceFlinger::setTransactionState(
1494 const Vector<ComposerState>& state,
1495 const Vector<DisplayState>& displays,
1496 uint32_t flags)
1497{
Mathias Agopian698c0872011-06-28 19:09:31 -07001498 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001499 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001500
1501 size_t count = displays.size();
1502 for (size_t i=0 ; i<count ; i++) {
1503 const DisplayState& s(displays[i]);
1504 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001505 }
1506
Mathias Agopiane57f2922012-08-09 16:29:12 -07001507 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001508 for (size_t i=0 ; i<count ; i++) {
1509 const ComposerState& s(state[i]);
1510 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001511 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001512 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001513
Mathias Agopian386aa982011-11-07 21:58:03 -08001514 if (transactionFlags) {
1515 // this triggers the transaction
1516 setTransactionFlags(transactionFlags);
1517
1518 // if this is a synchronous transaction, wait for it to take effect
1519 // before returning.
1520 if (flags & eSynchronous) {
1521 mTransationPending = true;
1522 }
1523 while (mTransationPending) {
1524 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1525 if (CC_UNLIKELY(err != NO_ERROR)) {
1526 // just in case something goes wrong in SF, return to the
1527 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001528 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001529 mTransationPending = false;
1530 break;
1531 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001532 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 }
1534}
1535
Mathias Agopiane57f2922012-08-09 16:29:12 -07001536uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1537{
1538 uint32_t flags = 0;
1539 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001540 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001541 const uint32_t what = s.what;
1542 if (what & DisplayState::eSurfaceChanged) {
1543 if (disp.surface->asBinder() != s.surface->asBinder()) {
1544 disp.surface = s.surface;
1545 flags |= eDisplayTransactionNeeded;
1546 }
1547 }
1548 if (what & DisplayState::eLayerStackChanged) {
1549 if (disp.layerStack != s.layerStack) {
1550 disp.layerStack = s.layerStack;
1551 flags |= eDisplayTransactionNeeded;
1552 }
1553 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001554 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001555 if (disp.orientation != s.orientation) {
1556 disp.orientation = s.orientation;
1557 flags |= eDisplayTransactionNeeded;
1558 }
1559 if (disp.frame != s.frame) {
1560 disp.frame = s.frame;
1561 flags |= eDisplayTransactionNeeded;
1562 }
1563 if (disp.viewport != s.viewport) {
1564 disp.viewport = s.viewport;
1565 flags |= eDisplayTransactionNeeded;
1566 }
1567 }
1568 }
1569 return flags;
1570}
1571
1572uint32_t SurfaceFlinger::setClientStateLocked(
1573 const sp<Client>& client,
1574 const layer_state_t& s)
1575{
1576 uint32_t flags = 0;
1577 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1578 if (layer != 0) {
1579 const uint32_t what = s.what;
1580 if (what & layer_state_t::ePositionChanged) {
1581 if (layer->setPosition(s.x, s.y))
1582 flags |= eTraversalNeeded;
1583 }
1584 if (what & layer_state_t::eLayerChanged) {
1585 // NOTE: index needs to be calculated before we update the state
1586 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1587 if (layer->setLayer(s.z)) {
1588 mCurrentState.layersSortedByZ.removeAt(idx);
1589 mCurrentState.layersSortedByZ.add(layer);
1590 // we need traversal (state changed)
1591 // AND transaction (list changed)
1592 flags |= eTransactionNeeded|eTraversalNeeded;
1593 }
1594 }
1595 if (what & layer_state_t::eSizeChanged) {
1596 if (layer->setSize(s.w, s.h)) {
1597 flags |= eTraversalNeeded;
1598 }
1599 }
1600 if (what & layer_state_t::eAlphaChanged) {
1601 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1602 flags |= eTraversalNeeded;
1603 }
1604 if (what & layer_state_t::eMatrixChanged) {
1605 if (layer->setMatrix(s.matrix))
1606 flags |= eTraversalNeeded;
1607 }
1608 if (what & layer_state_t::eTransparentRegionChanged) {
1609 if (layer->setTransparentRegionHint(s.transparentRegion))
1610 flags |= eTraversalNeeded;
1611 }
1612 if (what & layer_state_t::eVisibilityChanged) {
1613 if (layer->setFlags(s.flags, s.mask))
1614 flags |= eTraversalNeeded;
1615 }
1616 if (what & layer_state_t::eCropChanged) {
1617 if (layer->setCrop(s.crop))
1618 flags |= eTraversalNeeded;
1619 }
1620 if (what & layer_state_t::eLayerStackChanged) {
1621 // NOTE: index needs to be calculated before we update the state
1622 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1623 if (layer->setLayerStack(s.layerStack)) {
1624 mCurrentState.layersSortedByZ.removeAt(idx);
1625 mCurrentState.layersSortedByZ.add(layer);
1626 // we need traversal (state changed)
1627 // AND transaction (list changed)
1628 flags |= eTransactionNeeded|eTraversalNeeded;
1629 }
1630 }
1631 }
1632 return flags;
1633}
1634
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001635sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001636 ISurfaceComposerClient::surface_data_t* params,
1637 const String8& name,
1638 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001639 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640 uint32_t flags)
1641{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001642 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001643 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001644
1645 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001646 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001647 int(w), int(h));
1648 return surfaceHandle;
1649 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001650
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001651 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001652 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1653 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001654 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001656 case ISurfaceComposerClient::eFXSurfaceBlur:
1657 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001658 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001660 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001661 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001662 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 }
1664
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001665 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001666 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001667 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001668 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001670 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001671 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001672 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001673 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001674 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 }
1676
1677 return surfaceHandle;
1678}
1679
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001680sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001681 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001682 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001683 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684{
1685 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001686 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 case PIXEL_FORMAT_TRANSPARENT:
1688 case PIXEL_FORMAT_TRANSLUCENT:
1689 format = PIXEL_FORMAT_RGBA_8888;
1690 break;
1691 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001692#ifdef NO_RGBX_8888
1693 format = PIXEL_FORMAT_RGB_565;
1694#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001695 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001696#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 break;
1698 }
1699
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001700#ifdef NO_RGBX_8888
1701 if (format == PIXEL_FORMAT_RGBX_8888)
1702 format = PIXEL_FORMAT_RGBA_8888;
1703#endif
1704
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001705 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001706 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001707 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001708 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001709 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 }
1711 return layer;
1712}
1713
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001714sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001715 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001716 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001718 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001719 return layer;
1720}
1721
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001722sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001723 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001724 uint32_t w, uint32_t h, uint32_t flags)
1725{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001726 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 return layer;
1728}
1729
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001730status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731{
Mathias Agopian9a112062009-04-17 19:36:26 -07001732 /*
1733 * called by the window manager, when a surface should be marked for
1734 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001735 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001736 * The surface is removed from the current and drawing lists, but placed
1737 * in the purgatory queue, so it's not destroyed right-away (we need
1738 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001739 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001740
Mathias Agopian48d819a2009-09-10 19:41:18 -07001741 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001742 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001743 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001744
Mathias Agopian48d819a2009-09-10 19:41:18 -07001745 if (layer != 0) {
1746 err = purgatorizeLayer_l(layer);
1747 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001748 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001749 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001750 }
1751 return err;
1752}
1753
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001754status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001755{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001756 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001757 status_t err = NO_ERROR;
1758 sp<LayerBaseClient> l(layer.promote());
1759 if (l != NULL) {
1760 Mutex::Autolock _l(mStateLock);
1761 err = removeLayer_l(l);
1762 if (err == NAME_NOT_FOUND) {
1763 // The surface wasn't in the current list, which means it was
1764 // removed already, which means it is in the purgatory,
1765 // and need to be removed from there.
1766 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001767 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001768 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001769 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001770 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001771 "error removing layer=%p (%s)", l.get(), strerror(-err));
1772 }
1773 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774}
1775
Mathias Agopianb60314a2012-04-10 22:09:54 -07001776// ---------------------------------------------------------------------------
1777
Andy McFadden13a082e2012-08-24 10:16:42 -07001778void SurfaceFlinger::onInitializeDisplays() {
1779 // reset screen orientation
1780 Vector<ComposerState> state;
1781 Vector<DisplayState> displays;
1782 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001783 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001784 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001785 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001786 d.frame.makeInvalid();
1787 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001788 displays.add(d);
1789 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001790 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001791}
1792
1793void SurfaceFlinger::initializeDisplays() {
1794 class MessageScreenInitialized : public MessageBase {
1795 SurfaceFlinger* flinger;
1796 public:
1797 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1798 virtual bool handler() {
1799 flinger->onInitializeDisplays();
1800 return true;
1801 }
1802 };
1803 sp<MessageBase> msg = new MessageScreenInitialized(this);
1804 postMessageAsync(msg); // we may be called from main thread, use async message
1805}
1806
1807
Mathias Agopiancde87a32012-09-13 14:09:01 -07001808void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001809 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001810 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001811 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001812 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1813 // FIXME: eventthread only knows about the main display right now
1814 mEventThread->onScreenAcquired();
1815 }
Mathias Agopian20128302012-08-13 18:32:13 -07001816 mVisibleRegionsDirty = true;
1817 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818}
1819
Mathias Agopiancde87a32012-09-13 14:09:01 -07001820void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001821 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001822 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001823 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1824 // FIXME: eventthread only knows about the main display right now
1825 mEventThread->onScreenReleased();
1826 }
Mathias Agopian42977342012-08-05 00:40:46 -07001827 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001828 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001829 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001830 // from this point on, SF will stop drawing
1831 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832}
1833
Colin Cross8e533062012-06-07 13:17:52 -07001834void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001835 class MessageScreenAcquired : public MessageBase {
1836 SurfaceFlinger* flinger;
1837 public:
1838 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1839 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001840 // FIXME: should this be per-display?
1841 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001842 return true;
1843 }
1844 };
1845 sp<MessageBase> msg = new MessageScreenAcquired(this);
1846 postMessageSync(msg);
1847}
1848
Colin Cross8e533062012-06-07 13:17:52 -07001849void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001850 class MessageScreenReleased : public MessageBase {
1851 SurfaceFlinger* flinger;
1852 public:
1853 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1854 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001855 // FIXME: should this be per-display?
1856 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001857 return true;
1858 }
1859 };
1860 sp<MessageBase> msg = new MessageScreenReleased(this);
1861 postMessageSync(msg);
1862}
1863
1864// ---------------------------------------------------------------------------
1865
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1867{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001868 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001869 char buffer[SIZE];
1870 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001871
1872 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873 snprintf(buffer, SIZE, "Permission Denial: "
1874 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1875 IPCThreadState::self()->getCallingPid(),
1876 IPCThreadState::self()->getCallingUid());
1877 result.append(buffer);
1878 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001879 // Try to get the main lock, but don't insist if we can't
1880 // (this would indicate SF is stuck, but we want to be able to
1881 // print something in dumpsys).
1882 int retry = 3;
1883 while (mStateLock.tryLock()<0 && --retry>=0) {
1884 usleep(1000000);
1885 }
1886 const bool locked(retry >= 0);
1887 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001888 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001889 "SurfaceFlinger appears to be unresponsive, "
1890 "dumping anyways (no locks held)\n");
1891 result.append(buffer);
1892 }
1893
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001894 bool dumpAll = true;
1895 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001896 size_t numArgs = args.size();
1897 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001898 if ((index < numArgs) &&
1899 (args[index] == String16("--list"))) {
1900 index++;
1901 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001902 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001903 }
1904
1905 if ((index < numArgs) &&
1906 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001907 index++;
1908 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001909 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001910 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001911
1912 if ((index < numArgs) &&
1913 (args[index] == String16("--latency-clear"))) {
1914 index++;
1915 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001916 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001917 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001919
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001920 if (dumpAll) {
1921 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001922 }
1923
Mathias Agopian9795c422009-08-26 16:36:26 -07001924 if (locked) {
1925 mStateLock.unlock();
1926 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927 }
1928 write(fd, result.string(), result.size());
1929 return NO_ERROR;
1930}
1931
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001932void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1933 String8& result, char* buffer, size_t SIZE) const
1934{
1935 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1936 const size_t count = currentLayers.size();
1937 for (size_t i=0 ; i<count ; i++) {
1938 const sp<LayerBase>& layer(currentLayers[i]);
1939 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1940 result.append(buffer);
1941 }
1942}
1943
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001944void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1945 String8& result, char* buffer, size_t SIZE) const
1946{
1947 String8 name;
1948 if (index < args.size()) {
1949 name = String8(args[index]);
1950 index++;
1951 }
1952
1953 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1954 const size_t count = currentLayers.size();
1955 for (size_t i=0 ; i<count ; i++) {
1956 const sp<LayerBase>& layer(currentLayers[i]);
1957 if (name.isEmpty()) {
1958 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1959 result.append(buffer);
1960 }
1961 if (name.isEmpty() || (name == layer->getName())) {
1962 layer->dumpStats(result, buffer, SIZE);
1963 }
1964 }
1965}
1966
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001967void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1968 String8& result, char* buffer, size_t SIZE) const
1969{
1970 String8 name;
1971 if (index < args.size()) {
1972 name = String8(args[index]);
1973 index++;
1974 }
1975
1976 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1977 const size_t count = currentLayers.size();
1978 for (size_t i=0 ; i<count ; i++) {
1979 const sp<LayerBase>& layer(currentLayers[i]);
1980 if (name.isEmpty() || (name == layer->getName())) {
1981 layer->clearStats();
1982 }
1983 }
1984}
1985
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001986void SurfaceFlinger::dumpAllLocked(
1987 String8& result, char* buffer, size_t SIZE) const
1988{
1989 // figure out if we're stuck somewhere
1990 const nsecs_t now = systemTime();
1991 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1992 const nsecs_t inTransaction(mDebugInTransaction);
1993 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1994 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1995
1996 /*
1997 * Dump the visible layer list
1998 */
1999 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2000 const size_t count = currentLayers.size();
2001 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2002 result.append(buffer);
2003 for (size_t i=0 ; i<count ; i++) {
2004 const sp<LayerBase>& layer(currentLayers[i]);
2005 layer->dump(result, buffer, SIZE);
2006 }
2007
2008 /*
2009 * Dump the layers in the purgatory
2010 */
2011
2012 const size_t purgatorySize = mLayerPurgatory.size();
2013 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2014 result.append(buffer);
2015 for (size_t i=0 ; i<purgatorySize ; i++) {
2016 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2017 layer->shortDump(result, buffer, SIZE);
2018 }
2019
2020 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002021 * Dump Display state
2022 */
2023
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002024 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2025 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002026 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2027 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
2028 snprintf(buffer, SIZE,
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002029 "+ DisplayDevice[%u] '%s'\n"
Jeff Brown4fb39992012-09-07 12:55:10 -07002030 " type=%x, layerStack=%u, (%4dx%4d), orient=%2d (type=%08x), "
Jeff Brown6e220a62012-09-13 19:22:41 -07002031 "flips=%u, secure=%d, numLayers=%u, v:[%d,%d,%d,%d], f:[%d,%d,%d,%d], "
2032 "transform:[[%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f][%0.3f,%0.3f,%0.3f]]\n",
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002033 dpy, (const char*) hw->getDisplayName(),
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002034 hw->getDisplayType(), hw->getLayerStack(),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002035 hw->getWidth(), hw->getHeight(),
2036 hw->getOrientation(), hw->getTransform().getType(),
2037 hw->getPageFlipCount(),
2038 hw->getSecureLayerVisible(),
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002039 hw->getVisibleLayersSortedByZ().size(),
2040 hw->getViewport().left, hw->getViewport().top, hw->getViewport().right, hw->getViewport().bottom,
Jeff Brown6e220a62012-09-13 19:22:41 -07002041 hw->getFrame().left, hw->getFrame().top, hw->getFrame().right, hw->getFrame().bottom,
2042 hw->getTransform()[0][0], hw->getTransform()[1][0], hw->getTransform()[2][0],
2043 hw->getTransform()[0][1], hw->getTransform()[1][1], hw->getTransform()[2][1],
2044 hw->getTransform()[0][2], hw->getTransform()[1][2], hw->getTransform()[2][2]);
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002045
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002046 result.append(buffer);
2047 }
2048
2049 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002050 * Dump SurfaceFlinger global state
2051 */
2052
2053 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2054 result.append(buffer);
2055
Mathias Agopian888c8222012-08-04 21:10:38 -07002056 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002057 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002058 const GLExtensions& extensions(GLExtensions::getInstance());
2059 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2060 extensions.getVendor(),
2061 extensions.getRenderer(),
2062 extensions.getVersion());
2063 result.append(buffer);
2064
2065 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002066 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002067 result.append(buffer);
2068
2069 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2070 result.append(buffer);
2071
Mathias Agopian42977342012-08-05 00:40:46 -07002072 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002073 snprintf(buffer, SIZE,
2074 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002075 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002076 result.append(buffer);
2077 snprintf(buffer, SIZE,
2078 " last eglSwapBuffers() time: %f us\n"
2079 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002080 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002081 " refresh-rate : %f fps\n"
2082 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002083 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002084 mLastSwapBufferTime/1000.0,
2085 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002086 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002087 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2088 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2089 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002090 result.append(buffer);
2091
2092 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2093 inSwapBuffersDuration/1000.0);
2094 result.append(buffer);
2095
2096 snprintf(buffer, SIZE, " transaction time: %f us\n",
2097 inTransactionDuration/1000.0);
2098 result.append(buffer);
2099
2100 /*
2101 * VSYNC state
2102 */
2103 mEventThread->dump(result, buffer, SIZE);
2104
2105 /*
2106 * Dump HWComposer state
2107 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002108 snprintf(buffer, SIZE, "h/w composer state:\n");
2109 result.append(buffer);
2110 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2111 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2112 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2113 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002114 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002115
2116 /*
2117 * Dump gralloc state
2118 */
2119 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2120 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002121 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002122}
2123
Keun young Park63f165f2012-08-31 10:53:36 -07002124bool SurfaceFlinger::startDdmConnection()
2125{
2126 void* libddmconnection_dso =
2127 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2128 if (!libddmconnection_dso) {
2129 return false;
2130 }
2131 void (*DdmConnection_start)(const char* name);
2132 DdmConnection_start =
2133 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2134 if (!DdmConnection_start) {
2135 dlclose(libddmconnection_dso);
2136 return false;
2137 }
2138 (*DdmConnection_start)(getServiceName());
2139 return true;
2140}
2141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002142status_t SurfaceFlinger::onTransact(
2143 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2144{
2145 switch (code) {
2146 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002147 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002149 case BLANK:
2150 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002151 {
2152 // codes that require permission check
2153 IPCThreadState* ipc = IPCThreadState::self();
2154 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002155 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002156 if ((uid != AID_GRAPHICS) &&
2157 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002158 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002159 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2160 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002161 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002162 break;
2163 }
2164 case CAPTURE_SCREEN:
2165 {
2166 // codes that require permission check
2167 IPCThreadState* ipc = IPCThreadState::self();
2168 const int pid = ipc->getCallingPid();
2169 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002170 if ((uid != AID_GRAPHICS) &&
2171 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002172 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002173 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2174 return PERMISSION_DENIED;
2175 }
2176 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002177 }
2178 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002180 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2181 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002182 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002183 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002184 IPCThreadState* ipc = IPCThreadState::self();
2185 const int pid = ipc->getCallingPid();
2186 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002187 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002188 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 return PERMISSION_DENIED;
2190 }
2191 int n;
2192 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002193 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002194 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195 return NO_ERROR;
2196 case 1002: // SHOW_UPDATES
2197 n = data.readInt32();
2198 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002199 invalidateHwcGeometry();
2200 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002202 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002203 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002204 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002205 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002206 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002207 setTransactionFlags(
2208 eTransactionNeeded|
2209 eDisplayTransactionNeeded|
2210 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002211 return NO_ERROR;
2212 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002213 case 1006:{ // send empty update
2214 signalRefresh();
2215 return NO_ERROR;
2216 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002217 case 1008: // toggle use of hw composer
2218 n = data.readInt32();
2219 mDebugDisableHWC = n ? 1 : 0;
2220 invalidateHwcGeometry();
2221 repaintEverything();
2222 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002223 case 1009: // toggle use of transform hint
2224 n = data.readInt32();
2225 mDebugDisableTransformHint = n ? 1 : 0;
2226 invalidateHwcGeometry();
2227 repaintEverything();
2228 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002229 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002230 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231 reply->writeInt32(0);
2232 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002233 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002234 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235 return NO_ERROR;
2236 case 1013: {
2237 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002238 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2239 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002240 }
2241 return NO_ERROR;
2242 }
2243 }
2244 return err;
2245}
2246
Mathias Agopian53331da2011-08-22 21:44:41 -07002247void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002248 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002249 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002250}
2251
Mathias Agopian59119e62010-10-11 12:37:43 -07002252// ---------------------------------------------------------------------------
2253
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002254status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002255 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2256{
2257 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002258 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002259}
2260
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002261status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002262 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002263{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002264 ATRACE_CALL();
2265
Mathias Agopian59119e62010-10-11 12:37:43 -07002266 if (!GLExtensions::getInstance().haveFramebufferObject())
2267 return INVALID_OPERATION;
2268
2269 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002270 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2271 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002272 const uint32_t hw_w = hw->getWidth();
2273 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002274 GLfloat u = 1;
2275 GLfloat v = 1;
2276
2277 // make sure to clear all GL error flags
2278 while ( glGetError() != GL_NO_ERROR ) ;
2279
2280 // create a FBO
2281 GLuint name, tname;
2282 glGenTextures(1, &tname);
2283 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2287 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002288 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002289 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002290 GLint tw = (2 << (31 - clz(hw_w)));
2291 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2293 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002294 u = GLfloat(hw_w) / tw;
2295 v = GLfloat(hw_h) / th;
2296 }
2297 glGenFramebuffersOES(1, &name);
2298 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002299 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2300 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002301
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002302 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002303 glDisable(GL_TEXTURE_EXTERNAL_OES);
2304 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002305 glClearColor(0,0,0,1);
2306 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002307 glMatrixMode(GL_MODELVIEW);
2308 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002309 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002310 const size_t count = layers.size();
2311 for (size_t i=0 ; i<count ; ++i) {
2312 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002313 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002314 }
2315
Mathias Agopian42977342012-08-05 00:40:46 -07002316 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002317
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002318 // back to main framebuffer
2319 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002320 glDeleteFramebuffersOES(1, &name);
2321
2322 *textureName = tname;
2323 *uOut = u;
2324 *vOut = v;
2325 return NO_ERROR;
2326}
2327
2328// ---------------------------------------------------------------------------
2329
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002330status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 sp<IMemoryHeap>* heap,
2332 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002333 uint32_t sw, uint32_t sh,
2334 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002336 ATRACE_CALL();
2337
Mathias Agopian74c40c02010-09-29 13:02:36 -07002338 status_t result = PERMISSION_DENIED;
2339
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002340 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002341 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002342 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343
2344 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002345 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002346 const uint32_t hw_w = hw->getWidth();
2347 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002348
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002349 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002350 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002351 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002352 return PERMISSION_DENIED;
2353 }
2354
2355 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002356 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002357 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002358 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002359
2360 sw = (!sw) ? hw_w : sw;
2361 sh = (!sh) ? hw_h : sh;
2362 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002363 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002364
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002365// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2366// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002367
Mathias Agopian74c40c02010-09-29 13:02:36 -07002368 // make sure to clear all GL error flags
2369 while ( glGetError() != GL_NO_ERROR ) ;
2370
2371 // create a FBO
2372 GLuint name, tname;
2373 glGenRenderbuffersOES(1, &tname);
2374 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2375 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002376
Mathias Agopian74c40c02010-09-29 13:02:36 -07002377 glGenFramebuffersOES(1, &name);
2378 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2379 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2380 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2381
2382 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002383
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2385
2386 // invert everything, b/c glReadPixel() below will invert the FB
2387 glViewport(0, 0, sw, sh);
2388 glMatrixMode(GL_PROJECTION);
2389 glPushMatrix();
2390 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002391 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 glMatrixMode(GL_MODELVIEW);
2393
2394 // redraw the screen entirely...
2395 glClearColor(0,0,0,1);
2396 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002397
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002398 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002399 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002400 for (size_t i=0 ; i<count ; ++i) {
2401 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002402 const uint32_t z = layer->drawingState().z;
2403 if (z >= minLayerZ && z <= maxLayerZ) {
2404 if (filtering) layer->setFiltering(true);
2405 layer->draw(hw);
2406 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002408 }
2409
Mathias Agopian74c40c02010-09-29 13:02:36 -07002410 // check for errors and return screen capture
2411 if (glGetError() != GL_NO_ERROR) {
2412 // error while rendering
2413 result = INVALID_OPERATION;
2414 } else {
2415 // allocate shared memory large enough to hold the
2416 // screen capture
2417 sp<MemoryHeapBase> base(
2418 new MemoryHeapBase(size, 0, "screen-capture") );
2419 void* const ptr = base->getBase();
2420 if (ptr) {
2421 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002422 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2424 if (glGetError() == GL_NO_ERROR) {
2425 *heap = base;
2426 *w = sw;
2427 *h = sh;
2428 *f = PIXEL_FORMAT_RGBA_8888;
2429 result = NO_ERROR;
2430 }
2431 } else {
2432 result = NO_MEMORY;
2433 }
2434 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002435 glViewport(0, 0, hw_w, hw_h);
2436 glMatrixMode(GL_PROJECTION);
2437 glPopMatrix();
2438 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002439 } else {
2440 result = BAD_VALUE;
2441 }
2442
2443 // release FBO resources
2444 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2445 glDeleteRenderbuffersOES(1, &tname);
2446 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002447
Mathias Agopian42977342012-08-05 00:40:46 -07002448 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002449
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002450// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002451
Mathias Agopian74c40c02010-09-29 13:02:36 -07002452 return result;
2453}
2454
2455
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002456status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002457 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002458 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002459 uint32_t sw, uint32_t sh,
2460 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002461{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002462 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002463 return BAD_VALUE;
2464
2465 if (!GLExtensions::getInstance().haveFramebufferObject())
2466 return INVALID_OPERATION;
2467
2468 class MessageCaptureScreen : public MessageBase {
2469 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002470 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002471 sp<IMemoryHeap>* heap;
2472 uint32_t* w;
2473 uint32_t* h;
2474 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002475 uint32_t sw;
2476 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002477 uint32_t minLayerZ;
2478 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002479 status_t result;
2480 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002481 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002482 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002483 uint32_t sw, uint32_t sh,
2484 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002485 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002486 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2487 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2488 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489 {
2490 }
2491 status_t getResult() const {
2492 return result;
2493 }
2494 virtual bool handler() {
2495 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002496 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002497 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002498 return true;
2499 }
2500 };
2501
2502 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002503 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504 status_t res = postMessageSync(msg);
2505 if (res == NO_ERROR) {
2506 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2507 }
2508 return res;
2509}
2510
2511// ---------------------------------------------------------------------------
2512
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002513SurfaceFlinger::LayerVector::LayerVector() {
2514}
2515
2516SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2517 : SortedVector<sp<LayerBase> >(rhs) {
2518}
2519
2520int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2521 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002522{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002523 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002524 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2525 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002526
2527 uint32_t ls = l->currentState().layerStack;
2528 uint32_t rs = r->currentState().layerStack;
2529 if (ls != rs)
2530 return ls - rs;
2531
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002532 uint32_t lz = l->currentState().z;
2533 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002534 if (lz != rz)
2535 return lz - rz;
2536
2537 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002538}
2539
2540// ---------------------------------------------------------------------------
2541
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002542SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2543 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002544}
2545
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002546SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2547 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002548 viewport.makeInvalid();
2549 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002550}
2551
2552// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002553
Jamie Gennis9a78c902011-01-12 18:30:40 -08002554GraphicBufferAlloc::GraphicBufferAlloc() {}
2555
2556GraphicBufferAlloc::~GraphicBufferAlloc() {}
2557
2558sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002559 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002560 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2561 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002562 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002563 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002564 if (err == NO_MEMORY) {
2565 GraphicBuffer::dumpAllocationsToSystemLog();
2566 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002567 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002568 "failed (%s), handle=%p",
2569 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002570 return 0;
2571 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002572 return graphicBuffer;
2573}
2574
Jamie Gennis9a78c902011-01-12 18:30:40 -08002575// ---------------------------------------------------------------------------
2576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577}; // namespace android