blob: c99f25003cc6fac6004e628e62186891f3487fb9 [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,
Jesse Hallf21cffa2012-09-19 21:00:49 -0700271 // The rest of the attributes must be in this order and at the end
272 // of the list; we rely on that for fallback searches below.
Mathias Agopiancde87a32012-09-13 14:09:01 -0700273 EGL_RED_SIZE, 8,
274 EGL_GREEN_SIZE, 8,
275 EGL_BLUE_SIZE, 8,
Mathias Agopiana4912602012-07-12 14:25:33 -0700276 EGL_RECORDABLE_ANDROID, EGL_TRUE,
277 EGL_NONE
278 };
279 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700280 if (!err)
281 goto success;
282
283 // maybe we failed because of EGL_RECORDABLE_ANDROID
284 ALOGW("no suitable EGLConfig found, trying without EGL_RECORDABLE_ANDROID");
285 attribs[NELEM(attribs) - 3] = EGL_NONE;
286 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
287 if (!err)
288 goto success;
289
290 // allow less than 24-bit color; the non-gpu-accelerated emulator only
291 // supports 16-bit color
292 ALOGW("no suitable EGLConfig found, trying with 16-bit color allowed");
293 attribs[NELEM(attribs) - 9] = EGL_NONE;
294 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
295 if (!err)
296 goto success;
297
298 // this EGL is too lame for Android
299 ALOGE("no suitable EGLConfig found, giving up");
300
301 return 0;
302
303success:
304 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700305 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700306 return config;
307}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
Mathias Agopiana4912602012-07-12 14:25:33 -0700309EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
310 // Also create our EGLContext
311 EGLint contextAttributes[] = {
312#ifdef EGL_IMG_context_priority
313#ifdef HAS_CONTEXT_PRIORITY
314#warning "using EGL_IMG_context_priority"
315 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
316#endif
317#endif
318 EGL_NONE, EGL_NONE
319 };
320 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
321 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
322 return ctxt;
323}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324
Mathias Agopiancde87a32012-09-13 14:09:01 -0700325void SurfaceFlinger::initializeGL(EGLDisplay display) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700326 GLExtensions& extensions(GLExtensions::getInstance());
327 extensions.initWithGLStrings(
328 glGetString(GL_VENDOR),
329 glGetString(GL_RENDERER),
330 glGetString(GL_VERSION),
331 glGetString(GL_EXTENSIONS),
332 eglQueryString(display, EGL_VENDOR),
333 eglQueryString(display, EGL_VERSION),
334 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700335
Mathias Agopiana4912602012-07-12 14:25:33 -0700336 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
337 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700340 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 glShadeModel(GL_FLAT);
343 glDisable(GL_DITHER);
344 glDisable(GL_CULL_FACE);
345
Mathias Agopiana4912602012-07-12 14:25:33 -0700346 struct pack565 {
347 inline uint16_t operator() (int r, int g, int b) const {
348 return (r<<11)|(g<<5)|b;
349 }
350 } pack565;
351
Jamie Gennis9575f602011-10-07 14:51:16 -0700352 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
353 glGenTextures(1, &mProtectedTexName);
354 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
355 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
356 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
357 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
359 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
360 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361
Mathias Agopiana4912602012-07-12 14:25:33 -0700362 // print some debugging info
363 EGLint r,g,b,a;
364 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
365 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
366 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
367 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
368 ALOGI("EGL informations:");
369 ALOGI("vendor : %s", extensions.getEglVendor());
370 ALOGI("version : %s", extensions.getEglVersion());
371 ALOGI("extensions: %s", extensions.getEglExtension());
372 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
373 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
374 ALOGI("OpenGL ES informations:");
375 ALOGI("vendor : %s", extensions.getVendor());
376 ALOGI("renderer : %s", extensions.getRenderer());
377 ALOGI("version : %s", extensions.getVersion());
378 ALOGI("extensions: %s", extensions.getExtension());
379 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
380 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
381}
382
Mathias Agopiana4912602012-07-12 14:25:33 -0700383status_t SurfaceFlinger::readyToRun()
384{
385 ALOGI( "SurfaceFlinger's main thread ready to run. "
386 "Initializing graphics H/W...");
387
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700388 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700389 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
390 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700391
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700392 // Initialize the H/W composer object. There may or may not be an
393 // actual hardware composer underneath.
394 mHwc = new HWComposer(this,
395 *static_cast<HWComposer::EventHandler *>(this));
396
Mathias Agopiana4912602012-07-12 14:25:33 -0700397 // initialize the config and context
Mathias Agopiancde87a32012-09-13 14:09:01 -0700398 EGLint format = mHwc->getVisualID();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700399 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
400 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700401
Mathias Agopianda27af92012-09-13 18:17:13 -0700402 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
403 "couldn't create EGLContext");
404
Mathias Agopiancde87a32012-09-13 14:09:01 -0700405 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700406 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700407 mDefaultDisplays[i] = new BBinder();
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700408 mCurrentState.displays.add(mDefaultDisplays[i],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700409 DisplayDeviceState(DisplayDevice::DisplayType(i)));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700410 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700411
412 // The main display is a bit special and always exists
413 //
414 // if we didn't add it here, it would be added automatically during the
415 // first transaction, however this would also create some difficulties:
416 //
417 // - there would be a race where a client could call getDisplayInfo(),
418 // for instance before the DisplayDevice is created.
419 //
420 // - we need a GL context current in a few places, when initializing
421 // OpenGL ES (see below), or creating a layer,
422 // or when a texture is (asynchronously) destroyed, and for that
423 // we need a valid surface, so it's conveniant to use the main display
424 // for that.
425
426 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc);
427 sp<SurfaceTextureClient> stc = new SurfaceTextureClient(
428 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700429 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700430 DisplayDevice::DISPLAY_PRIMARY,
431 mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY],
Mathias Agopiancde87a32012-09-13 14:09:01 -0700432 stc, fbs, mEGLConfig);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700433 mDisplays.add(mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY], hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700434
Mathias Agopiancde87a32012-09-13 14:09:01 -0700435
Mathias Agopiana4912602012-07-12 14:25:33 -0700436 // initialize OpenGL ES
Mathias Agopiancde87a32012-09-13 14:09:01 -0700437 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
438 initializeGL(mEGLDisplay);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800439
Mathias Agopian028508c2012-07-25 21:12:12 -0700440 // start the EventThread
441 mEventThread = new EventThread(this);
442 mEventQueue.setEventThread(mEventThread);
443
Mathias Agopian92a979a2012-08-02 18:32:23 -0700444 // initialize our drawing state
445 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700446
Mathias Agopiancde87a32012-09-13 14:09:01 -0700447
Mathias Agopiana4912602012-07-12 14:25:33 -0700448 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800449 mReadyToRunBarrier.open();
450
Andy McFadden13a082e2012-08-24 10:16:42 -0700451 // set initial conditions (e.g. unblank default device)
452 initializeDisplays();
453
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700454 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700455 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 return NO_ERROR;
458}
459
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700460int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
461 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
462 type : mHwc->allocateDisplayId();
463}
464
Mathias Agopiana67e4182012-06-19 17:26:12 -0700465void SurfaceFlinger::startBootAnim() {
466 // start boot animation
467 property_set("service.bootanim.exit", "0");
468 property_set("ctl.start", "bootanim");
469}
470
Mathias Agopiana4912602012-07-12 14:25:33 -0700471uint32_t SurfaceFlinger::getMaxTextureSize() const {
472 return mMaxTextureSize;
473}
474
475uint32_t SurfaceFlinger::getMaxViewportDims() const {
476 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
477 mMaxViewportDims[0] : mMaxViewportDims[1];
478}
479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800481
Jamie Gennis582270d2011-08-17 18:19:00 -0700482bool SurfaceFlinger::authenticateSurfaceTexture(
483 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800484 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700485 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800486
487 // Check the visible layer list for the ISurface
488 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
489 size_t count = currentLayers.size();
490 for (size_t i=0 ; i<count ; i++) {
491 const sp<LayerBase>& layer(currentLayers[i]);
492 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700493 if (lbc != NULL) {
494 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
495 if (lbcBinder == surfaceTextureBinder) {
496 return true;
497 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800498 }
499 }
500
501 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700502 // SurfaceTexture gets destroyed before all the clients are done using it,
503 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800504 // will instead fail later on when the client tries to use the surface,
505 // which should be reported as "surface XYZ returned an -ENODEV". The
506 // purgatorized layers are no less authentic than the visible ones, so this
507 // should not cause any harm.
508 size_t purgatorySize = mLayerPurgatory.size();
509 for (size_t i=0 ; i<purgatorySize ; i++) {
510 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
511 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700512 if (lbc != NULL) {
513 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
514 if (lbcBinder == surfaceTextureBinder) {
515 return true;
516 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800517 }
518 }
519
520 return false;
521}
522
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700523status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
524 // TODO: this is mostly here only for compatibility
525 // the display size is needed but the display metrics should come from elsewhere
526 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
527 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700528 return BAD_INDEX;
529 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700530
531 const HWComposer& hwc(getHwComposer());
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700532 float xdpi = hwc.getDpiX(HWC_DISPLAY_PRIMARY);
533 float ydpi = hwc.getDpiY(HWC_DISPLAY_PRIMARY);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700534
535 // TODO: Not sure if display density should handled by SF any longer
536 class Density {
537 static int getDensityFromProperty(char const* propName) {
538 char property[PROPERTY_VALUE_MAX];
539 int density = 0;
540 if (property_get(propName, property, NULL) > 0) {
541 density = atoi(property);
542 }
543 return density;
544 }
545 public:
546 static int getEmuDensity() {
547 return getDensityFromProperty("qemu.sf.lcd_density"); }
548 static int getBuildDensity() {
549 return getDensityFromProperty("ro.sf.lcd_density"); }
550 };
551 // The density of the device is provided by a build property
552 float density = Density::getBuildDensity() / 160.0f;
553 if (density == 0) {
554 // the build doesn't provide a density -- this is wrong!
555 // use xdpi instead
556 ALOGE("ro.sf.lcd_density must be defined as a build property");
557 density = xdpi / 160.0f;
558 }
559 if (Density::getEmuDensity()) {
560 // if "qemu.sf.lcd_density" is specified, it overrides everything
561 xdpi = ydpi = density = Density::getEmuDensity();
562 density /= 160.0f;
563 }
564
Mathias Agopian42977342012-08-05 00:40:46 -0700565 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
566 info->w = hw->getWidth();
567 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700568 info->xdpi = xdpi;
569 info->ydpi = ydpi;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700570 info->fps = float(1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY));
Mathias Agopian8b736f12012-08-13 17:54:26 -0700571 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700572 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700573 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700574 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700575 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700576}
577
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800578// ----------------------------------------------------------------------------
579
580sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800581 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700582}
583
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700584void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700585
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700586 sp<IBinder> token;
587 { // scope for the lock
588 Mutex::Autolock _l(mStateLock);
589 token = mExtDisplayToken;
590 }
591
592 if (token == 0) {
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700593 token = createDisplay(String8("Display from connectDisplay"));
Mathias Agopian3094df32012-06-18 18:06:45 -0700594 }
595
596 { // scope for the lock
597 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700598 if (surface == 0) {
599 // release our current display. we're guarantee to have
600 // a reference to it (token), while we hold the lock
601 mExtDisplayToken = 0;
602 } else {
603 mExtDisplayToken = token;
604 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700605
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700606 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
607 info.surface = surface;
608 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700609 }
610}
611
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800613
614void SurfaceFlinger::waitForEvent() {
615 mEventQueue.waitMessage();
616}
617
618void SurfaceFlinger::signalTransaction() {
619 mEventQueue.invalidate();
620}
621
622void SurfaceFlinger::signalLayerUpdate() {
623 mEventQueue.invalidate();
624}
625
626void SurfaceFlinger::signalRefresh() {
627 mEventQueue.refresh();
628}
629
630status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
631 nsecs_t reltime, uint32_t flags) {
632 return mEventQueue.postMessage(msg, reltime);
633}
634
635status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
636 nsecs_t reltime, uint32_t flags) {
637 status_t res = mEventQueue.postMessage(msg, reltime);
638 if (res == NO_ERROR) {
639 msg->wait();
640 }
641 return res;
642}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
Mathias Agopian4fec8732012-06-29 14:12:52 -0700644bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 return true;
647}
648
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700649void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700650 if (mEventThread == NULL) {
651 // This is a temporary workaround for b/7145521. A non-null pointer
652 // does not mean EventThread has finished initializing, so this
653 // is not a correct fix.
654 ALOGW("WARNING: EventThread not started, ignoring vsync");
655 return;
656 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700657 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
658 // we should only receive DisplayDevice::DisplayType from the vsync callback
659 const wp<IBinder>& token(mDefaultDisplays[type]);
660 mEventThread->onVSyncReceived(token, timestamp);
661 }
Mathias Agopian86303202012-07-24 22:46:10 -0700662}
663
664void SurfaceFlinger::eventControl(int event, int enabled) {
665 getHwComposer().eventControl(event, enabled);
666}
667
Mathias Agopian4fec8732012-06-29 14:12:52 -0700668void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800669 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800670 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700671 case MessageQueue::INVALIDATE:
672 handleMessageTransaction();
673 handleMessageInvalidate();
674 signalRefresh();
675 break;
676 case MessageQueue::REFRESH:
677 handleMessageRefresh();
678 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800679 }
680}
681
Mathias Agopian4fec8732012-06-29 14:12:52 -0700682void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700683 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700684 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700685 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700686 }
687}
688
689void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700690 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700691 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700692}
693
694void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700695 ATRACE_CALL();
696 preComposition();
697 rebuildLayerStacks();
698 setUpHWComposer();
699 doDebugFlashRegions();
700 doComposition();
701 postComposition();
702}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700703
Mathias Agopiancd60f992012-08-16 16:28:27 -0700704void SurfaceFlinger::doDebugFlashRegions()
705{
706 // is debugging enabled
707 if (CC_LIKELY(!mDebugRegion))
708 return;
709
710 const bool repaintEverything = mRepaintEverything;
711 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
712 const sp<DisplayDevice>& hw(mDisplays[dpy]);
713 if (hw->canDraw()) {
714 // transform the dirty region into this screen's coordinate space
715 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
716 if (!dirtyRegion.isEmpty()) {
717 // redraw the whole screen
718 doComposeSurfaces(hw, Region(hw->bounds()));
719
720 // and draw the dirty region
721 glDisable(GL_TEXTURE_EXTERNAL_OES);
722 glDisable(GL_TEXTURE_2D);
723 glDisable(GL_BLEND);
724 glColor4f(1, 0, 1, 1);
725 const int32_t height = hw->getHeight();
726 Region::const_iterator it = dirtyRegion.begin();
727 Region::const_iterator const end = dirtyRegion.end();
728 while (it != end) {
729 const Rect& r = *it++;
730 GLfloat vertices[][2] = {
731 { r.left, height - r.top },
732 { r.left, height - r.bottom },
733 { r.right, height - r.bottom },
734 { r.right, height - r.top }
735 };
736 glVertexPointer(2, GL_FLOAT, 0, vertices);
737 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
738 }
739 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700740 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700741 }
742 }
743 }
744
745 postFramebuffer();
746
747 if (mDebugRegion > 1) {
748 usleep(mDebugRegion * 1000);
749 }
750}
751
752void SurfaceFlinger::preComposition()
753{
754 bool needExtraInvalidate = false;
755 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
756 const size_t count = currentLayers.size();
757 for (size_t i=0 ; i<count ; i++) {
758 if (currentLayers[i]->onPreComposition()) {
759 needExtraInvalidate = true;
760 }
761 }
762 if (needExtraInvalidate) {
763 signalLayerUpdate();
764 }
765}
766
767void SurfaceFlinger::postComposition()
768{
769 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
770 const size_t count = currentLayers.size();
771 for (size_t i=0 ; i<count ; i++) {
772 currentLayers[i]->onPostComposition();
773 }
774}
775
776void SurfaceFlinger::rebuildLayerStacks() {
777 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700778 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700779 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700780 mVisibleRegionsDirty = false;
781 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700782
Mathias Agopian87baae12012-07-31 12:38:26 -0700783 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700784 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700785 Region opaqueRegion;
786 Region dirtyRegion;
787 Vector< sp<LayerBase> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700788 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700789 const Transform& tr(hw->getTransform());
790 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700791 if (hw->canDraw()) {
792 SurfaceFlinger::computeVisibleRegions(currentLayers,
793 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700794
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700795 const size_t count = currentLayers.size();
796 for (size_t i=0 ; i<count ; i++) {
797 const sp<LayerBase>& layer(currentLayers[i]);
798 const Layer::State& s(layer->drawingState());
799 if (s.layerStack == hw->getLayerStack()) {
800 Region visibleRegion(tr.transform(layer->visibleRegion));
801 visibleRegion.andSelf(bounds);
802 if (!visibleRegion.isEmpty()) {
803 layersSortedByZ.add(layer);
804 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700805 }
806 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700807 }
Mathias Agopian42977342012-08-05 00:40:46 -0700808 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700809 hw->undefinedRegion.set(bounds);
810 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
811 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700812 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700813 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700814}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700815
Mathias Agopiancd60f992012-08-16 16:28:27 -0700816void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700817 HWComposer& hwc(getHwComposer());
818 if (hwc.initCheck() == NO_ERROR) {
819 // build the h/w work list
820 const bool workListsDirty = mHwWorkListDirty;
821 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700822 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700823 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700824 const int32_t id = hw->getHwcDisplayId();
825 if (id >= 0) {
826 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700827 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700828 const size_t count = currentLayers.size();
829 if (hwc.createWorkList(id, count) >= 0) {
830 HWComposer::LayerListIterator cur = hwc.begin(id);
831 const HWComposer::LayerListIterator end = hwc.end(id);
832 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
833 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700834
Mathias Agopiane60b0682012-08-21 23:34:09 -0700835 if (CC_UNLIKELY(workListsDirty)) {
836 layer->setGeometry(hw, *cur);
837 if (mDebugDisableHWC || mDebugRegion) {
838 cur->setSkip(true);
839 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700840 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700841
Mathias Agopiane60b0682012-08-21 23:34:09 -0700842 /*
843 * update the per-frame h/w composer data for each layer
844 * and build the transparent region of the FB
845 */
846 layer->setPerFrameData(hw, *cur);
847 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700848 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700849 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700850 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851 status_t err = hwc.prepare();
852 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
853 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700854}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700855
Mathias Agopiancd60f992012-08-16 16:28:27 -0700856void SurfaceFlinger::doComposition() {
857 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700858 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700859 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700860 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861 if (hw->canDraw()) {
862 // transform the dirty region into this screen's coordinate space
863 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
864 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700865 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700866 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700867 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868 hw->dirtyRegion.clear();
869 hw->flip(hw->swapRegion);
870 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700871 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700872 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700873 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700874 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700875 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700876}
877
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878void SurfaceFlinger::postFramebuffer()
879{
Mathias Agopian841cde52012-03-01 15:44:37 -0800880 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800881
Mathias Agopiana44b0412011-10-16 18:46:35 -0700882 const nsecs_t now = systemTime();
883 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700884
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700885 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700886 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700887 // FIXME: EGL spec says:
888 // "surface must be bound to the calling thread's current context,
889 // for the current rendering API."
Mathias Agopianda8d0a52012-09-04 15:05:38 -0700890 DisplayDevice::makeCurrent(mEGLDisplay, getDefaultDisplayDevice(), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700891 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700892 }
893
Mathias Agopian92a979a2012-08-02 18:32:23 -0700894 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700895 sp<const DisplayDevice> hw(mDisplays[dpy]);
896 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700897 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700898 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700899 int32_t id = hw->getHwcDisplayId();
900 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700901 HWComposer::LayerListIterator cur = hwc.begin(id);
902 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700903 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700904 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700905 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700907 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700908 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700909 }
Jesse Hallef194142012-06-14 14:45:17 -0700910 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800911 }
912
Mathias Agopiana44b0412011-10-16 18:46:35 -0700913 mLastSwapBufferTime = systemTime() - now;
914 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915}
916
Mathias Agopian87baae12012-07-31 12:38:26 -0700917void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918{
Mathias Agopian841cde52012-03-01 15:44:37 -0800919 ATRACE_CALL();
920
Mathias Agopianca4d3602011-05-19 15:38:14 -0700921 Mutex::Autolock _l(mStateLock);
922 const nsecs_t now = systemTime();
923 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924
Mathias Agopianca4d3602011-05-19 15:38:14 -0700925 // Here we're guaranteed that some transaction flags are set
926 // so we can call handleTransactionLocked() unconditionally.
927 // We call getTransactionFlags(), which will also clear the flags,
928 // with mStateLock held to guarantee that mCurrentState won't change
929 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700930
Mathias Agopiane57f2922012-08-09 16:29:12 -0700931 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700932 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700933
Mathias Agopianca4d3602011-05-19 15:38:14 -0700934 mLastTransactionTime = systemTime() - now;
935 mDebugInTransaction = 0;
936 invalidateHwcGeometry();
937 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700938}
939
Mathias Agopian87baae12012-07-31 12:38:26 -0700940void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700941{
942 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 const size_t count = currentLayers.size();
944
945 /*
946 * Traversal of the children
947 * (perform the transaction for each of them if needed)
948 */
949
Mathias Agopian3559b072012-08-15 13:46:03 -0700950 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700952 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
954 if (!trFlags) continue;
955
956 const uint32_t flags = layer->doTransaction(0);
957 if (flags & Layer::eVisibleRegion)
958 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 }
960 }
961
962 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700963 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964 */
965
Mathias Agopiane57f2922012-08-09 16:29:12 -0700966 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700967 // here we take advantage of Vector's copy-on-write semantics to
968 // improve performance by skipping the transaction entirely when
969 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
971 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700974 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -0700975 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -0700976
977 // find the displays that were removed
978 // (ie: in drawing state but not in current state)
979 // also handle displays that changed
980 // (ie: displays that are in both lists)
981 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700982 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
983 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700984 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700985 if (!draw[i].isMainDisplay()) {
986 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700987 } else {
988 ALOGW("trying to remove the main display");
989 }
990 } else {
991 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700992 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700993 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -0700994 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700995 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -0700996 // recreating the DisplayDevice, so we just remove it
997 // from the drawing state, so that it get re-added
998 // below.
999 mDisplays.removeItem(display);
1000 mDrawingState.displays.removeItemsAt(i);
1001 dc--; i--;
1002 // at this point we must loop to the next item
1003 continue;
1004 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001005
Mathias Agopian93997a82012-08-29 17:30:36 -07001006 const sp<DisplayDevice>& disp(getDisplayDevice(display));
1007 if (disp != NULL) {
1008 if (state.layerStack != draw[i].layerStack) {
1009 disp->setLayerStack(state.layerStack);
1010 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001011 if ((state.orientation != draw[i].orientation)
1012 || (state.viewport != draw[i].viewport)
1013 || (state.frame != draw[i].frame))
1014 {
1015 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001016 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001017 }
Andy McFadden69052052012-09-14 16:10:11 -07001018
1019 // Walk through all the layers in currentLayers,
1020 // and update their transform hint.
1021 //
1022 // TODO: we could be much more clever about which
1023 // layers we touch and how often we do these updates
1024 // (e.g. only touch the layers associated with this
1025 // display, and only on a rotation).
1026 for (size_t i = 0; i < count; i++) {
1027 const sp<LayerBase>& layerBase = currentLayers[i];
1028 layerBase->updateTransformHint();
1029 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001030 }
1031 }
1032 }
1033
1034 // find displays that were added
1035 // (ie: in current state but not in drawing state)
1036 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001037 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001038 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001039
1040 sp<FramebufferSurface> fbs;
1041 sp<SurfaceTextureClient> stc;
1042 if (!state.isVirtualDisplay()) {
1043
1044 ALOGE_IF(state.surface!=NULL,
1045 "adding a supported display, but rendering "
1046 "surface is provided (%p), ignoring it",
1047 state.surface.get());
1048
1049 // for supported (by hwc) displays we provide our
1050 // own rendering surface
1051 fbs = new FramebufferSurface(*mHwc);
1052 stc = new SurfaceTextureClient(
1053 static_cast< sp<ISurfaceTexture> >(fbs->getBufferQueue()));
1054 } else {
1055 if (state.surface != NULL) {
1056 stc = new SurfaceTextureClient(state.surface);
1057 }
1058 }
1059
1060 const wp<IBinder>& display(curr.keyAt(i));
1061 if (stc != NULL) {
1062 sp<DisplayDevice> hw = new DisplayDevice(this,
1063 state.type, display, stc, fbs, mEGLConfig);
1064 hw->setLayerStack(state.layerStack);
1065 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001066 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001067 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001068 mDisplays.add(display, hw);
1069 if (hw->getDisplayType() < DisplayDevice::NUM_DISPLAY_TYPES) {
1070 // notify the system that this display is now up
1071 // (note onScreenAcquired() is safe to call from
1072 // here because we're in the main thread)
1073 onScreenAcquired(hw);
1074 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001075 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001076 }
1077 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001079 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080
Mathias Agopian3559b072012-08-15 13:46:03 -07001081 /*
1082 * Perform our own transaction if needed
1083 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001084
Mathias Agopiancd60f992012-08-16 16:28:27 -07001085 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1086 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001087 // layers have been added
1088 mVisibleRegionsDirty = true;
1089 }
1090
1091 // some layers might have been removed, so
1092 // we need to update the regions they're exposing.
1093 if (mLayersRemoved) {
1094 mLayersRemoved = false;
1095 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001096 const size_t count = previousLayers.size();
1097 for (size_t i=0 ; i<count ; i++) {
1098 const sp<LayerBase>& layer(previousLayers[i]);
1099 if (currentLayers.indexOf(layer) < 0) {
1100 // this layer is not visible anymore
1101 // TODO: we could traverse the tree from front to back and
1102 // compute the actual visible region
1103 // TODO: we could cache the transformed region
Mathias Agopian1501d542012-09-04 21:04:09 -07001104 const Layer::State& s(layer->drawingState());
1105 Region visibleReg = s.transform.transform(
1106 Region(Rect(s.active.w, s.active.h)));
1107 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001108 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 }
1111
1112 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001113}
1114
1115void SurfaceFlinger::commitTransaction()
1116{
1117 if (!mLayersPendingRemoval.isEmpty()) {
1118 // Notify removed layers now that they can't be drawn from
1119 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1120 mLayersPendingRemoval[i]->onRemoved();
1121 }
1122 mLayersPendingRemoval.clear();
1123 }
1124
1125 mDrawingState = mCurrentState;
1126 mTransationPending = false;
1127 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128}
1129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001131 const LayerVector& currentLayers, uint32_t layerStack,
1132 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133{
Mathias Agopian841cde52012-03-01 15:44:37 -08001134 ATRACE_CALL();
1135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 Region aboveOpaqueLayers;
1137 Region aboveCoveredLayers;
1138 Region dirty;
1139
Mathias Agopian87baae12012-07-31 12:38:26 -07001140 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
1142 size_t i = currentLayers.size();
1143 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001144 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145
1146 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001147 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148
Mathias Agopian87baae12012-07-31 12:38:26 -07001149 // only consider the layers on the given later stack
1150 if (s.layerStack != layerStack)
1151 continue;
1152
Mathias Agopianab028732010-03-16 16:41:46 -07001153 /*
1154 * opaqueRegion: area of a surface that is fully opaque.
1155 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001157
1158 /*
1159 * visibleRegion: area of a surface that is visible on screen
1160 * and not fully transparent. This is essentially the layer's
1161 * footprint minus the opaque regions above it.
1162 * Areas covered by a translucent surface are considered visible.
1163 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001165
1166 /*
1167 * coveredRegion: area of a surface that is covered by all
1168 * visible regions above it (which includes the translucent areas).
1169 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001171
1172
1173 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001174 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001175 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001176 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001178 if (!visibleRegion.isEmpty()) {
1179 // Remove the transparent area from the visible region
1180 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001181 Region transparentRegionScreen;
1182 const Transform tr(s.transform);
1183 if (tr.transformed()) {
1184 if (tr.preserveRects()) {
1185 // transform the transparent region
1186 transparentRegionScreen = tr.transform(s.transparentRegion);
1187 } else {
1188 // transformation too complex, can't do the
1189 // transparent region optimization.
1190 transparentRegionScreen.clear();
1191 }
1192 } else {
1193 transparentRegionScreen = s.transparentRegion;
1194 }
1195 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001196 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197
Mathias Agopianab028732010-03-16 16:41:46 -07001198 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001199 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001200 if (s.alpha==255 && !translucent &&
1201 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1202 // the opaque region is the layer's footprint
1203 opaqueRegion = visibleRegion;
1204 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 }
1206 }
1207
Mathias Agopianab028732010-03-16 16:41:46 -07001208 // Clip the covered region to the visible region
1209 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1210
1211 // Update aboveCoveredLayers for next (lower) layer
1212 aboveCoveredLayers.orSelf(visibleRegion);
1213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 // subtract the opaque region covered by the layers above us
1215 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216
1217 // compute this layer's dirty region
1218 if (layer->contentDirty) {
1219 // we need to invalidate the whole region
1220 dirty = visibleRegion;
1221 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001222 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 layer->contentDirty = false;
1224 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001225 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001226 * the exposed region consists of two components:
1227 * 1) what's VISIBLE now and was COVERED before
1228 * 2) what's EXPOSED now less what was EXPOSED before
1229 *
1230 * note that (1) is conservative, we start with the whole
1231 * visible region but only keep what used to be covered by
1232 * something -- which mean it may have been exposed.
1233 *
1234 * (2) handles areas that were not covered by anything but got
1235 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001236 */
Mathias Agopianab028732010-03-16 16:41:46 -07001237 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001238 const Region oldVisibleRegion = layer->visibleRegion;
1239 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001240 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1241 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 }
1243 dirty.subtractSelf(aboveOpaqueLayers);
1244
1245 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001246 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247
Mathias Agopianab028732010-03-16 16:41:46 -07001248 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 // Store the visible region is screen space
1252 layer->setVisibleRegion(visibleRegion);
1253 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 }
1255
Mathias Agopian87baae12012-07-31 12:38:26 -07001256 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257}
1258
Mathias Agopian87baae12012-07-31 12:38:26 -07001259void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1260 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001261 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001262 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1263 if (hw->getLayerStack() == layerStack) {
1264 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001265 }
1266 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001267}
1268
1269void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001271 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272
Mathias Agopian4fec8732012-06-29 14:12:52 -07001273 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001274 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001275 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001276 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001277 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001278 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1501d542012-09-04 21:04:09 -07001279 const Layer::State& s(layer->drawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001280 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001281 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001282
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001283 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284}
1285
Mathias Agopianad456f92011-01-13 17:53:01 -08001286void SurfaceFlinger::invalidateHwcGeometry()
1287{
1288 mHwWorkListDirty = true;
1289}
1290
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001291
Mathias Agopiancd60f992012-08-16 16:28:27 -07001292void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001293 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294{
Mathias Agopian87baae12012-07-31 12:38:26 -07001295 Region dirtyRegion(inDirtyRegion);
1296
Mathias Agopianb8a55602009-06-26 19:06:36 -07001297 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001298 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299
Mathias Agopian42977342012-08-05 00:40:46 -07001300 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001301 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001302 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1303 // takes a rectangle, we must make sure to update that whole
1304 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001305 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001306 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001307 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001308 // We need to redraw the rectangle that will be updated
1309 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001310 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001311 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001312 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001313 } else {
1314 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001315 dirtyRegion.set(hw->bounds());
1316 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 }
1318 }
1319
Mathias Agopiancd60f992012-08-16 16:28:27 -07001320 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001322 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001323 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001324
1325 // swap buffers (presentation)
1326 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327}
1328
Mathias Agopiancd60f992012-08-16 16:28:27 -07001329void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001330{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001331 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001332 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001333 HWComposer::LayerListIterator cur = hwc.begin(id);
1334 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001335
Mathias Agopian85d751c2012-08-29 16:59:24 -07001336 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1337 if (hasGlesComposition) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07001338 DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001339
1340 // set the frame buffer
1341 glMatrixMode(GL_MODELVIEW);
1342 glLoadIdentity();
1343
1344 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001345 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001346 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001347 // when using overlays, we assume a fully transparent framebuffer
1348 // NOTE: we could reduce how much we need to clear, for instance
1349 // remove where there are opaque FB layers. however, on some
1350 // GPUs doing a "clean slate" glClear might be more efficient.
1351 // We'll revisit later if needed.
1352 glClearColor(0, 0, 0, 0);
1353 glClear(GL_COLOR_BUFFER_BIT);
1354 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001355 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001356 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001357 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001358 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001359 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001360 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001361 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001362 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001363
Mathias Agopian85d751c2012-08-29 16:59:24 -07001364 /*
1365 * and then, render the layers targeted at the framebuffer
1366 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001367
Mathias Agopian85d751c2012-08-29 16:59:24 -07001368 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
1369 const size_t count = layers.size();
1370 const Transform& tr = hw->getTransform();
1371 if (cur != end) {
1372 // we're using h/w composer
1373 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001374 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001375 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001376 if (!clip.isEmpty()) {
1377 switch (cur->getCompositionType()) {
1378 case HWC_OVERLAY: {
1379 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1380 && i
1381 && layer->isOpaque()
1382 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001383 // never clear the very first layer since we're
1384 // guaranteed the FB is already cleared
1385 layer->clearWithOpenGL(hw, clip);
1386 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001387 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001388 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001389 case HWC_FRAMEBUFFER: {
1390 layer->draw(hw, clip);
1391 break;
1392 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001393 case HWC_FRAMEBUFFER_TARGET: {
1394 // this should not happen as the iterator shouldn't
1395 // let us get there.
1396 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1397 break;
1398 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001399 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001400 }
1401 layer->setAcquireFence(hw, *cur);
1402 }
1403 } else {
1404 // we're not using h/w composer
1405 for (size_t i=0 ; i<count ; ++i) {
1406 const sp<LayerBase>& layer(layers[i]);
1407 const Region clip(dirty.intersect(
1408 tr.transform(layer->visibleRegion)));
1409 if (!clip.isEmpty()) {
1410 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001411 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001412 }
1413 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414}
1415
Mathias Agopian55801e42012-08-27 18:54:24 -07001416void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1417 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001419 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001420 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001421 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001422 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001423
Mathias Agopian55801e42012-08-27 18:54:24 -07001424 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001425 Region::const_iterator it = region.begin();
1426 Region::const_iterator const end = region.end();
1427 while (it != end) {
1428 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001429 GLfloat vertices[][2] = {
1430 { r.left, height - r.top },
1431 { r.left, height - r.bottom },
1432 { r.right, height - r.bottom },
1433 { r.right, height - r.top }
1434 };
1435 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001436 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1437 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438}
1439
Mathias Agopian96f08192010-06-02 23:28:45 -07001440ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1441 const sp<LayerBaseClient>& lbc)
1442{
Mathias Agopian96f08192010-06-02 23:28:45 -07001443 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001444 size_t name = client->attachLayer(lbc);
1445
Mathias Agopian96f08192010-06-02 23:28:45 -07001446 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001447 Mutex::Autolock _l(mStateLock);
1448 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001449
Mathias Agopian4f113742011-05-03 16:21:41 -07001450 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001451}
1452
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001453status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454{
1455 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001456 status_t err = purgatorizeLayer_l(layer);
1457 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001458 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001459 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460}
1461
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001462status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463{
1464 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1465 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001466 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 return NO_ERROR;
1468 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001469 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470}
1471
Mathias Agopian9a112062009-04-17 19:36:26 -07001472status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1473{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001474 // First add the layer to the purgatory list, which makes sure it won't
1475 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001476 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001477 if (err >= 0) {
1478 mLayerPurgatory.add(layerBase);
1479 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001480
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001481 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001482
Mathias Agopian3d579642009-06-04 18:46:21 -07001483 // it's possible that we don't find a layer, because it might
1484 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001485 // from the user because there is a race between Client::destroySurface(),
1486 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001487 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1488}
1489
Mathias Agopiandea20b12011-05-03 17:04:02 -07001490uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1491{
1492 return android_atomic_release_load(&mTransactionFlags);
1493}
1494
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1496{
1497 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1498}
1499
Mathias Agopianbb641242010-05-18 17:06:55 -07001500uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501{
1502 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1503 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001504 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 }
1506 return old;
1507}
1508
Mathias Agopian8b33f032012-07-24 20:43:54 -07001509void SurfaceFlinger::setTransactionState(
1510 const Vector<ComposerState>& state,
1511 const Vector<DisplayState>& displays,
1512 uint32_t flags)
1513{
Mathias Agopian698c0872011-06-28 19:09:31 -07001514 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001515 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001516
1517 size_t count = displays.size();
1518 for (size_t i=0 ; i<count ; i++) {
1519 const DisplayState& s(displays[i]);
1520 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001521 }
1522
Mathias Agopiane57f2922012-08-09 16:29:12 -07001523 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001524 for (size_t i=0 ; i<count ; i++) {
1525 const ComposerState& s(state[i]);
1526 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001527 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001528 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001529
Mathias Agopian386aa982011-11-07 21:58:03 -08001530 if (transactionFlags) {
1531 // this triggers the transaction
1532 setTransactionFlags(transactionFlags);
1533
1534 // if this is a synchronous transaction, wait for it to take effect
1535 // before returning.
1536 if (flags & eSynchronous) {
1537 mTransationPending = true;
1538 }
1539 while (mTransationPending) {
1540 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1541 if (CC_UNLIKELY(err != NO_ERROR)) {
1542 // just in case something goes wrong in SF, return to the
1543 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001544 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001545 mTransationPending = false;
1546 break;
1547 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001548 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549 }
1550}
1551
Mathias Agopiane57f2922012-08-09 16:29:12 -07001552uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1553{
1554 uint32_t flags = 0;
1555 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001556 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001557 const uint32_t what = s.what;
1558 if (what & DisplayState::eSurfaceChanged) {
1559 if (disp.surface->asBinder() != s.surface->asBinder()) {
1560 disp.surface = s.surface;
1561 flags |= eDisplayTransactionNeeded;
1562 }
1563 }
1564 if (what & DisplayState::eLayerStackChanged) {
1565 if (disp.layerStack != s.layerStack) {
1566 disp.layerStack = s.layerStack;
1567 flags |= eDisplayTransactionNeeded;
1568 }
1569 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001570 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001571 if (disp.orientation != s.orientation) {
1572 disp.orientation = s.orientation;
1573 flags |= eDisplayTransactionNeeded;
1574 }
1575 if (disp.frame != s.frame) {
1576 disp.frame = s.frame;
1577 flags |= eDisplayTransactionNeeded;
1578 }
1579 if (disp.viewport != s.viewport) {
1580 disp.viewport = s.viewport;
1581 flags |= eDisplayTransactionNeeded;
1582 }
1583 }
1584 }
1585 return flags;
1586}
1587
1588uint32_t SurfaceFlinger::setClientStateLocked(
1589 const sp<Client>& client,
1590 const layer_state_t& s)
1591{
1592 uint32_t flags = 0;
1593 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1594 if (layer != 0) {
1595 const uint32_t what = s.what;
1596 if (what & layer_state_t::ePositionChanged) {
1597 if (layer->setPosition(s.x, s.y))
1598 flags |= eTraversalNeeded;
1599 }
1600 if (what & layer_state_t::eLayerChanged) {
1601 // NOTE: index needs to be calculated before we update the state
1602 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1603 if (layer->setLayer(s.z)) {
1604 mCurrentState.layersSortedByZ.removeAt(idx);
1605 mCurrentState.layersSortedByZ.add(layer);
1606 // we need traversal (state changed)
1607 // AND transaction (list changed)
1608 flags |= eTransactionNeeded|eTraversalNeeded;
1609 }
1610 }
1611 if (what & layer_state_t::eSizeChanged) {
1612 if (layer->setSize(s.w, s.h)) {
1613 flags |= eTraversalNeeded;
1614 }
1615 }
1616 if (what & layer_state_t::eAlphaChanged) {
1617 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1618 flags |= eTraversalNeeded;
1619 }
1620 if (what & layer_state_t::eMatrixChanged) {
1621 if (layer->setMatrix(s.matrix))
1622 flags |= eTraversalNeeded;
1623 }
1624 if (what & layer_state_t::eTransparentRegionChanged) {
1625 if (layer->setTransparentRegionHint(s.transparentRegion))
1626 flags |= eTraversalNeeded;
1627 }
1628 if (what & layer_state_t::eVisibilityChanged) {
1629 if (layer->setFlags(s.flags, s.mask))
1630 flags |= eTraversalNeeded;
1631 }
1632 if (what & layer_state_t::eCropChanged) {
1633 if (layer->setCrop(s.crop))
1634 flags |= eTraversalNeeded;
1635 }
1636 if (what & layer_state_t::eLayerStackChanged) {
1637 // NOTE: index needs to be calculated before we update the state
1638 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1639 if (layer->setLayerStack(s.layerStack)) {
1640 mCurrentState.layersSortedByZ.removeAt(idx);
1641 mCurrentState.layersSortedByZ.add(layer);
1642 // we need traversal (state changed)
1643 // AND transaction (list changed)
1644 flags |= eTransactionNeeded|eTraversalNeeded;
1645 }
1646 }
1647 }
1648 return flags;
1649}
1650
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001651sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001652 ISurfaceComposerClient::surface_data_t* params,
1653 const String8& name,
1654 const sp<Client>& client,
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001655 uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 uint32_t flags)
1657{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001658 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001659 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001660
1661 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001662 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001663 int(w), int(h));
1664 return surfaceHandle;
1665 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001666
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001667 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001668 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1669 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001670 layer = createNormalLayer(client, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001672 case ISurfaceComposerClient::eFXSurfaceBlur:
1673 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001674 layer = createDimLayer(client, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001676 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001677 layer = createScreenshotLayer(client, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001678 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 }
1680
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001681 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001682 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001683 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001684 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001686 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001687 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001688 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001689 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001690 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 }
1692
1693 return surfaceHandle;
1694}
1695
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001696sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001697 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001698 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001699 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700{
1701 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001702 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 case PIXEL_FORMAT_TRANSPARENT:
1704 case PIXEL_FORMAT_TRANSLUCENT:
1705 format = PIXEL_FORMAT_RGBA_8888;
1706 break;
1707 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001708#ifdef NO_RGBX_8888
1709 format = PIXEL_FORMAT_RGB_565;
1710#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001711 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001712#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713 break;
1714 }
1715
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001716#ifdef NO_RGBX_8888
1717 if (format == PIXEL_FORMAT_RGBX_8888)
1718 format = PIXEL_FORMAT_RGBA_8888;
1719#endif
1720
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001721 sp<Layer> layer = new Layer(this, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001722 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001723 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001724 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001725 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 }
1727 return layer;
1728}
1729
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001730sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001731 const sp<Client>& client,
Mathias Agopian96f08192010-06-02 23:28:45 -07001732 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001734 sp<LayerDim> layer = new LayerDim(this, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001735 return layer;
1736}
1737
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001738sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001739 const sp<Client>& client,
Mathias Agopian118d0242011-10-13 16:02:48 -07001740 uint32_t w, uint32_t h, uint32_t flags)
1741{
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001742 sp<LayerScreenshot> layer = new LayerScreenshot(this, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 return layer;
1744}
1745
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001746status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747{
Mathias Agopian9a112062009-04-17 19:36:26 -07001748 /*
1749 * called by the window manager, when a surface should be marked for
1750 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001751 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001752 * The surface is removed from the current and drawing lists, but placed
1753 * in the purgatory queue, so it's not destroyed right-away (we need
1754 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001755 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001756
Mathias Agopian48d819a2009-09-10 19:41:18 -07001757 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001758 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001759 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001760
Mathias Agopian48d819a2009-09-10 19:41:18 -07001761 if (layer != 0) {
1762 err = purgatorizeLayer_l(layer);
1763 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001764 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001765 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001766 }
1767 return err;
1768}
1769
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001770status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001771{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001772 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001773 status_t err = NO_ERROR;
1774 sp<LayerBaseClient> l(layer.promote());
1775 if (l != NULL) {
1776 Mutex::Autolock _l(mStateLock);
1777 err = removeLayer_l(l);
1778 if (err == NAME_NOT_FOUND) {
1779 // The surface wasn't in the current list, which means it was
1780 // removed already, which means it is in the purgatory,
1781 // and need to be removed from there.
1782 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001783 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001784 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001785 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001786 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001787 "error removing layer=%p (%s)", l.get(), strerror(-err));
1788 }
1789 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790}
1791
Mathias Agopianb60314a2012-04-10 22:09:54 -07001792// ---------------------------------------------------------------------------
1793
Andy McFadden13a082e2012-08-24 10:16:42 -07001794void SurfaceFlinger::onInitializeDisplays() {
1795 // reset screen orientation
1796 Vector<ComposerState> state;
1797 Vector<DisplayState> displays;
1798 DisplayState d;
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001799 d.what = DisplayState::eDisplayProjectionChanged;
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001800 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_PRIMARY];
Andy McFadden13a082e2012-08-24 10:16:42 -07001801 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001802 d.frame.makeInvalid();
1803 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001804 displays.add(d);
1805 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001806 onScreenAcquired(getDefaultDisplayDevice());
Andy McFadden13a082e2012-08-24 10:16:42 -07001807}
1808
1809void SurfaceFlinger::initializeDisplays() {
1810 class MessageScreenInitialized : public MessageBase {
1811 SurfaceFlinger* flinger;
1812 public:
1813 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1814 virtual bool handler() {
1815 flinger->onInitializeDisplays();
1816 return true;
1817 }
1818 };
1819 sp<MessageBase> msg = new MessageScreenInitialized(this);
1820 postMessageAsync(msg); // we may be called from main thread, use async message
1821}
1822
1823
Mathias Agopiancde87a32012-09-13 14:09:01 -07001824void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001825 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian86303202012-07-24 22:46:10 -07001826 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001827 hw->acquireScreen();
Mathias Agopiancde87a32012-09-13 14:09:01 -07001828 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1829 // FIXME: eventthread only knows about the main display right now
1830 mEventThread->onScreenAcquired();
1831 }
Mathias Agopian20128302012-08-13 18:32:13 -07001832 mVisibleRegionsDirty = true;
1833 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834}
1835
Mathias Agopiancde87a32012-09-13 14:09:01 -07001836void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Colin Cross8e533062012-06-07 13:17:52 -07001837 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001838 if (hw->isScreenAcquired()) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001839 if (hw->getDisplayType() == DisplayDevice::DISPLAY_PRIMARY) {
1840 // FIXME: eventthread only knows about the main display right now
1841 mEventThread->onScreenReleased();
1842 }
Mathias Agopian42977342012-08-05 00:40:46 -07001843 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001844 getHwComposer().release();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001845 mVisibleRegionsDirty = true;
Mathias Agopianb60314a2012-04-10 22:09:54 -07001846 // from this point on, SF will stop drawing
1847 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848}
1849
Colin Cross8e533062012-06-07 13:17:52 -07001850void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001851 class MessageScreenAcquired : public MessageBase {
1852 SurfaceFlinger* flinger;
1853 public:
1854 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1855 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001856 // FIXME: should this be per-display?
1857 flinger->onScreenAcquired(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001858 return true;
1859 }
1860 };
1861 sp<MessageBase> msg = new MessageScreenAcquired(this);
1862 postMessageSync(msg);
1863}
1864
Colin Cross8e533062012-06-07 13:17:52 -07001865void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001866 class MessageScreenReleased : public MessageBase {
1867 SurfaceFlinger* flinger;
1868 public:
1869 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1870 virtual bool handler() {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001871 // FIXME: should this be per-display?
1872 flinger->onScreenReleased(flinger->getDefaultDisplayDevice());
Mathias Agopianb60314a2012-04-10 22:09:54 -07001873 return true;
1874 }
1875 };
1876 sp<MessageBase> msg = new MessageScreenReleased(this);
1877 postMessageSync(msg);
1878}
1879
1880// ---------------------------------------------------------------------------
1881
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1883{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001884 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001885 char buffer[SIZE];
1886 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001887
1888 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889 snprintf(buffer, SIZE, "Permission Denial: "
1890 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1891 IPCThreadState::self()->getCallingPid(),
1892 IPCThreadState::self()->getCallingUid());
1893 result.append(buffer);
1894 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001895 // Try to get the main lock, but don't insist if we can't
1896 // (this would indicate SF is stuck, but we want to be able to
1897 // print something in dumpsys).
1898 int retry = 3;
1899 while (mStateLock.tryLock()<0 && --retry>=0) {
1900 usleep(1000000);
1901 }
1902 const bool locked(retry >= 0);
1903 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001904 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001905 "SurfaceFlinger appears to be unresponsive, "
1906 "dumping anyways (no locks held)\n");
1907 result.append(buffer);
1908 }
1909
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001910 bool dumpAll = true;
1911 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001912 size_t numArgs = args.size();
1913 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001914 if ((index < numArgs) &&
1915 (args[index] == String16("--list"))) {
1916 index++;
1917 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001918 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001919 }
1920
1921 if ((index < numArgs) &&
1922 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001923 index++;
1924 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001925 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001926 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001927
1928 if ((index < numArgs) &&
1929 (args[index] == String16("--latency-clear"))) {
1930 index++;
1931 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001932 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001933 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001935
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001936 if (dumpAll) {
1937 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001938 }
1939
Mathias Agopian9795c422009-08-26 16:36:26 -07001940 if (locked) {
1941 mStateLock.unlock();
1942 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 }
1944 write(fd, result.string(), result.size());
1945 return NO_ERROR;
1946}
1947
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001948void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1949 String8& result, char* buffer, size_t SIZE) const
1950{
1951 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1952 const size_t count = currentLayers.size();
1953 for (size_t i=0 ; i<count ; i++) {
1954 const sp<LayerBase>& layer(currentLayers[i]);
1955 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1956 result.append(buffer);
1957 }
1958}
1959
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001960void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1961 String8& result, char* buffer, size_t SIZE) const
1962{
1963 String8 name;
1964 if (index < args.size()) {
1965 name = String8(args[index]);
1966 index++;
1967 }
1968
1969 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1970 const size_t count = currentLayers.size();
1971 for (size_t i=0 ; i<count ; i++) {
1972 const sp<LayerBase>& layer(currentLayers[i]);
1973 if (name.isEmpty()) {
1974 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1975 result.append(buffer);
1976 }
1977 if (name.isEmpty() || (name == layer->getName())) {
1978 layer->dumpStats(result, buffer, SIZE);
1979 }
1980 }
1981}
1982
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001983void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1984 String8& result, char* buffer, size_t SIZE) const
1985{
1986 String8 name;
1987 if (index < args.size()) {
1988 name = String8(args[index]);
1989 index++;
1990 }
1991
1992 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1993 const size_t count = currentLayers.size();
1994 for (size_t i=0 ; i<count ; i++) {
1995 const sp<LayerBase>& layer(currentLayers[i]);
1996 if (name.isEmpty() || (name == layer->getName())) {
1997 layer->clearStats();
1998 }
1999 }
2000}
2001
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002002void SurfaceFlinger::dumpAllLocked(
2003 String8& result, char* buffer, size_t SIZE) const
2004{
2005 // figure out if we're stuck somewhere
2006 const nsecs_t now = systemTime();
2007 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2008 const nsecs_t inTransaction(mDebugInTransaction);
2009 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2010 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2011
2012 /*
2013 * Dump the visible layer list
2014 */
2015 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2016 const size_t count = currentLayers.size();
2017 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
2018 result.append(buffer);
2019 for (size_t i=0 ; i<count ; i++) {
2020 const sp<LayerBase>& layer(currentLayers[i]);
2021 layer->dump(result, buffer, SIZE);
2022 }
2023
2024 /*
2025 * Dump the layers in the purgatory
2026 */
2027
2028 const size_t purgatorySize = mLayerPurgatory.size();
2029 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
2030 result.append(buffer);
2031 for (size_t i=0 ; i<purgatorySize ; i++) {
2032 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
2033 layer->shortDump(result, buffer, SIZE);
2034 }
2035
2036 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002037 * Dump Display state
2038 */
2039
Andy McFadden8dfa92f2012-09-17 18:27:17 -07002040 snprintf(buffer, SIZE, "Displays (%d entries)\n", mDisplays.size());
2041 result.append(buffer);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002042 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2043 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian1d12d8a2012-09-18 01:38:00 -07002044 hw->dump(result, buffer, SIZE);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002045 }
2046
2047 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002048 * Dump SurfaceFlinger global state
2049 */
2050
2051 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
2052 result.append(buffer);
2053
Mathias Agopian888c8222012-08-04 21:10:38 -07002054 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002055 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002056 const GLExtensions& extensions(GLExtensions::getInstance());
2057 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
2058 extensions.getVendor(),
2059 extensions.getRenderer(),
2060 extensions.getVersion());
2061 result.append(buffer);
2062
2063 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002064 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002065 result.append(buffer);
2066
2067 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2068 result.append(buffer);
2069
Mathias Agopian42977342012-08-05 00:40:46 -07002070 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002071 snprintf(buffer, SIZE,
2072 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002073 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002074 result.append(buffer);
2075 snprintf(buffer, SIZE,
2076 " last eglSwapBuffers() time: %f us\n"
2077 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002078 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002079 " refresh-rate : %f fps\n"
2080 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002081 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002082 mLastSwapBufferTime/1000.0,
2083 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002084 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002085 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2086 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
2087 hwc.getDpiY(HWC_DISPLAY_PRIMARY));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002088 result.append(buffer);
2089
2090 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2091 inSwapBuffersDuration/1000.0);
2092 result.append(buffer);
2093
2094 snprintf(buffer, SIZE, " transaction time: %f us\n",
2095 inTransactionDuration/1000.0);
2096 result.append(buffer);
2097
2098 /*
2099 * VSYNC state
2100 */
2101 mEventThread->dump(result, buffer, SIZE);
2102
2103 /*
2104 * Dump HWComposer state
2105 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002106 snprintf(buffer, SIZE, "h/w composer state:\n");
2107 result.append(buffer);
2108 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2109 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2110 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2111 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002112 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002113
2114 /*
2115 * Dump gralloc state
2116 */
2117 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2118 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002119}
2120
Keun young Park63f165f2012-08-31 10:53:36 -07002121bool SurfaceFlinger::startDdmConnection()
2122{
2123 void* libddmconnection_dso =
2124 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2125 if (!libddmconnection_dso) {
2126 return false;
2127 }
2128 void (*DdmConnection_start)(const char* name);
2129 DdmConnection_start =
2130 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2131 if (!DdmConnection_start) {
2132 dlclose(libddmconnection_dso);
2133 return false;
2134 }
2135 (*DdmConnection_start)(getServiceName());
2136 return true;
2137}
2138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002139status_t SurfaceFlinger::onTransact(
2140 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2141{
2142 switch (code) {
2143 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002144 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002145 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002146 case BLANK:
2147 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148 {
2149 // codes that require permission check
2150 IPCThreadState* ipc = IPCThreadState::self();
2151 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002152 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002153 if ((uid != AID_GRAPHICS) &&
2154 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002155 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002156 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2157 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002158 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002159 break;
2160 }
2161 case CAPTURE_SCREEN:
2162 {
2163 // codes that require permission check
2164 IPCThreadState* ipc = IPCThreadState::self();
2165 const int pid = ipc->getCallingPid();
2166 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002167 if ((uid != AID_GRAPHICS) &&
2168 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002169 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002170 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2171 return PERMISSION_DENIED;
2172 }
2173 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174 }
2175 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002177 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2178 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002179 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002180 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002181 IPCThreadState* ipc = IPCThreadState::self();
2182 const int pid = ipc->getCallingPid();
2183 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002184 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002185 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002186 return PERMISSION_DENIED;
2187 }
2188 int n;
2189 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002190 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002191 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002192 return NO_ERROR;
2193 case 1002: // SHOW_UPDATES
2194 n = data.readInt32();
2195 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002196 invalidateHwcGeometry();
2197 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002198 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002199 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002200 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002201 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002202 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002203 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002204 setTransactionFlags(
2205 eTransactionNeeded|
2206 eDisplayTransactionNeeded|
2207 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002208 return NO_ERROR;
2209 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002210 case 1006:{ // send empty update
2211 signalRefresh();
2212 return NO_ERROR;
2213 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002214 case 1008: // toggle use of hw composer
2215 n = data.readInt32();
2216 mDebugDisableHWC = n ? 1 : 0;
2217 invalidateHwcGeometry();
2218 repaintEverything();
2219 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002220 case 1009: // toggle use of transform hint
2221 n = data.readInt32();
2222 mDebugDisableTransformHint = n ? 1 : 0;
2223 invalidateHwcGeometry();
2224 repaintEverything();
2225 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002226 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002227 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002228 reply->writeInt32(0);
2229 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002230 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002231 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002232 return NO_ERROR;
2233 case 1013: {
2234 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002235 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2236 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002237 }
2238 return NO_ERROR;
2239 }
2240 }
2241 return err;
2242}
2243
Mathias Agopian53331da2011-08-22 21:44:41 -07002244void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002245 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002246 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002247}
2248
Mathias Agopian59119e62010-10-11 12:37:43 -07002249// ---------------------------------------------------------------------------
2250
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002251status_t SurfaceFlinger::renderScreenToTexture(uint32_t layerStack,
Mathias Agopian118d0242011-10-13 16:02:48 -07002252 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2253{
2254 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002255 return renderScreenToTextureLocked(layerStack, textureName, uOut, vOut);
Mathias Agopian118d0242011-10-13 16:02:48 -07002256}
2257
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002258status_t SurfaceFlinger::renderScreenToTextureLocked(uint32_t layerStack,
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002260{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002261 ATRACE_CALL();
2262
Mathias Agopian59119e62010-10-11 12:37:43 -07002263 if (!GLExtensions::getInstance().haveFramebufferObject())
2264 return INVALID_OPERATION;
2265
2266 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002267 // FIXME: figure out what it means to have a screenshot texture w/ multi-display
2268 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian42977342012-08-05 00:40:46 -07002269 const uint32_t hw_w = hw->getWidth();
2270 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002271 GLfloat u = 1;
2272 GLfloat v = 1;
2273
2274 // make sure to clear all GL error flags
2275 while ( glGetError() != GL_NO_ERROR ) ;
2276
2277 // create a FBO
2278 GLuint name, tname;
2279 glGenTextures(1, &tname);
2280 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002283 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2284 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002285 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002286 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002287 GLint tw = (2 << (31 - clz(hw_w)));
2288 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2290 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002291 u = GLfloat(hw_w) / tw;
2292 v = GLfloat(hw_h) / th;
2293 }
2294 glGenFramebuffersOES(1, &name);
2295 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002296 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2297 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002298
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002299 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002300 glDisable(GL_TEXTURE_EXTERNAL_OES);
2301 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002302 glClearColor(0,0,0,1);
2303 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002304 glMatrixMode(GL_MODELVIEW);
2305 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002306 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002307 const size_t count = layers.size();
2308 for (size_t i=0 ; i<count ; ++i) {
2309 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002310 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002311 }
2312
Mathias Agopian42977342012-08-05 00:40:46 -07002313 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002314
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002315 // back to main framebuffer
2316 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002317 glDeleteFramebuffersOES(1, &name);
2318
2319 *textureName = tname;
2320 *uOut = u;
2321 *vOut = v;
2322 return NO_ERROR;
2323}
2324
2325// ---------------------------------------------------------------------------
2326
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002327status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 sp<IMemoryHeap>* heap,
2329 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002330 uint32_t sw, uint32_t sh,
2331 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002332{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002333 ATRACE_CALL();
2334
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 status_t result = PERMISSION_DENIED;
2336
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002337 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002338 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002339 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002340
2341 // get screen geometry
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002342 sp<const DisplayDevice> hw(getDisplayDevice(display));
Mathias Agopian42977342012-08-05 00:40:46 -07002343 const uint32_t hw_w = hw->getWidth();
2344 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002345
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002346 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002347 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002348 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002349 return PERMISSION_DENIED;
2350 }
2351
2352 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002353 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002354 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002355 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356
2357 sw = (!sw) ? hw_w : sw;
2358 sh = (!sh) ? hw_h : sh;
2359 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002360 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002362// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2363// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002364
Mathias Agopian74c40c02010-09-29 13:02:36 -07002365 // make sure to clear all GL error flags
2366 while ( glGetError() != GL_NO_ERROR ) ;
2367
2368 // create a FBO
2369 GLuint name, tname;
2370 glGenRenderbuffersOES(1, &tname);
2371 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2372 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002373
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 glGenFramebuffersOES(1, &name);
2375 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2376 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2377 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2378
2379 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002380
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2382
2383 // invert everything, b/c glReadPixel() below will invert the FB
2384 glViewport(0, 0, sw, sh);
2385 glMatrixMode(GL_PROJECTION);
2386 glPushMatrix();
2387 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002388 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002389 glMatrixMode(GL_MODELVIEW);
2390
2391 // redraw the screen entirely...
2392 glClearColor(0,0,0,1);
2393 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002394
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002395 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Jamie Gennis9575f602011-10-07 14:51:16 -07002396 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002397 for (size_t i=0 ; i<count ; ++i) {
2398 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002399 const uint32_t z = layer->drawingState().z;
2400 if (z >= minLayerZ && z <= maxLayerZ) {
2401 if (filtering) layer->setFiltering(true);
2402 layer->draw(hw);
2403 if (filtering) layer->setFiltering(false);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002404 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 }
2406
Mathias Agopian74c40c02010-09-29 13:02:36 -07002407 // check for errors and return screen capture
2408 if (glGetError() != GL_NO_ERROR) {
2409 // error while rendering
2410 result = INVALID_OPERATION;
2411 } else {
2412 // allocate shared memory large enough to hold the
2413 // screen capture
2414 sp<MemoryHeapBase> base(
2415 new MemoryHeapBase(size, 0, "screen-capture") );
2416 void* const ptr = base->getBase();
2417 if (ptr) {
2418 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002419 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002420 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2421 if (glGetError() == GL_NO_ERROR) {
2422 *heap = base;
2423 *w = sw;
2424 *h = sh;
2425 *f = PIXEL_FORMAT_RGBA_8888;
2426 result = NO_ERROR;
2427 }
2428 } else {
2429 result = NO_MEMORY;
2430 }
2431 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002432 glViewport(0, 0, hw_w, hw_h);
2433 glMatrixMode(GL_PROJECTION);
2434 glPopMatrix();
2435 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 } else {
2437 result = BAD_VALUE;
2438 }
2439
2440 // release FBO resources
2441 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2442 glDeleteRenderbuffersOES(1, &tname);
2443 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002444
Mathias Agopian42977342012-08-05 00:40:46 -07002445 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002446
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002447// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002448
Mathias Agopian74c40c02010-09-29 13:02:36 -07002449 return result;
2450}
2451
2452
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002453status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002454 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002455 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002456 uint32_t sw, uint32_t sh,
2457 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002459 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002460 return BAD_VALUE;
2461
2462 if (!GLExtensions::getInstance().haveFramebufferObject())
2463 return INVALID_OPERATION;
2464
2465 class MessageCaptureScreen : public MessageBase {
2466 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002467 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002468 sp<IMemoryHeap>* heap;
2469 uint32_t* w;
2470 uint32_t* h;
2471 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002472 uint32_t sw;
2473 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002474 uint32_t minLayerZ;
2475 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002476 status_t result;
2477 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002478 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002479 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002480 uint32_t sw, uint32_t sh,
2481 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002482 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002483 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2484 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2485 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002486 {
2487 }
2488 status_t getResult() const {
2489 return result;
2490 }
2491 virtual bool handler() {
2492 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002493 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002494 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002495 return true;
2496 }
2497 };
2498
2499 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002500 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002501 status_t res = postMessageSync(msg);
2502 if (res == NO_ERROR) {
2503 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2504 }
2505 return res;
2506}
2507
2508// ---------------------------------------------------------------------------
2509
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002510SurfaceFlinger::LayerVector::LayerVector() {
2511}
2512
2513SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2514 : SortedVector<sp<LayerBase> >(rhs) {
2515}
2516
2517int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2518 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002519{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002520 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002521 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2522 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002523
2524 uint32_t ls = l->currentState().layerStack;
2525 uint32_t rs = r->currentState().layerStack;
2526 if (ls != rs)
2527 return ls - rs;
2528
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002529 uint32_t lz = l->currentState().z;
2530 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002531 if (lz != rz)
2532 return lz - rz;
2533
2534 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002535}
2536
2537// ---------------------------------------------------------------------------
2538
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002539SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2540 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002541}
2542
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002543SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
2544 : type(type), layerStack(0), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002545 viewport.makeInvalid();
2546 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002547}
2548
2549// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550
Jamie Gennis9a78c902011-01-12 18:30:40 -08002551GraphicBufferAlloc::GraphicBufferAlloc() {}
2552
2553GraphicBufferAlloc::~GraphicBufferAlloc() {}
2554
2555sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002556 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002557 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2558 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002559 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002560 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002561 if (err == NO_MEMORY) {
2562 GraphicBuffer::dumpAllocationsToSystemLog();
2563 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002564 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002565 "failed (%s), handle=%p",
2566 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002567 return 0;
2568 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002569 return graphicBuffer;
2570}
2571
Jamie Gennis9a78c902011-01-12 18:30:40 -08002572// ---------------------------------------------------------------------------
2573
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002574}; // namespace android