blob: f2b49e8b65c0fdf595ad601aef10102ece40d697 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
39#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/SurfaceTextureClient.h>
41
42#include <ui/GraphicBufferAllocator.h>
43#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/String8.h>
46#include <utils/String16.h>
47#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080048#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian921e6ac2012-07-23 23:11:29 -070050#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080053#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070055#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070060#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana4912602012-07-12 14:25:33 -070063#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070064#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070086 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070099 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Steve Blocka19954a2012-01-04 20:05:49 +0000101 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
112 DdmConnection::start(getServiceName());
113 }
114
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700115 ALOGI_IF(mDebugRegion, "showupdates enabled");
116 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800119void SurfaceFlinger::onFirstRef()
120{
121 mEventQueue.init(this);
122
123 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
124
125 // Wait for the main thread to be done with its initialization
126 mReadyToRunBarrier.wait();
127}
128
129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130SurfaceFlinger::~SurfaceFlinger()
131{
Mathias Agopiana4912602012-07-12 14:25:33 -0700132 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
133 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
134 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800137void SurfaceFlinger::binderDied(const wp<IBinder>& who)
138{
139 // the window manager died on us. prepare its eulogy.
140
141 // reset screen orientation
142 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700143 Vector<DisplayState> displays;
144 DisplayState d;
Mathias Agopian3165cc22012-08-08 19:42:09 -0700145 d.orientation = DisplayState::eOrientationDefault;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700146 displays.add(d);
147 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148
149 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700150 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800151}
152
Mathias Agopian7e27f052010-05-28 14:22:23 -0700153sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154{
Mathias Agopian96f08192010-06-02 23:28:45 -0700155 sp<ISurfaceComposerClient> bclient;
156 sp<Client> client(new Client(this));
157 status_t err = client->initCheck();
158 if (err == NO_ERROR) {
159 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 return bclient;
162}
163
Mathias Agopiane57f2922012-08-09 16:29:12 -0700164sp<IBinder> SurfaceFlinger::createDisplay()
165{
166 class DisplayToken : public BBinder {
167 sp<SurfaceFlinger> flinger;
168 virtual ~DisplayToken() {
169 // no more references, this display must be terminated
170 Mutex::Autolock _l(flinger->mStateLock);
171 flinger->mCurrentState.displays.removeItem(this);
172 flinger->setTransactionFlags(eDisplayTransactionNeeded);
173 }
174 public:
175 DisplayToken(const sp<SurfaceFlinger>& flinger)
176 : flinger(flinger) {
177 }
178 };
179
180 sp<BBinder> token = new DisplayToken(this);
181
182 Mutex::Autolock _l(mStateLock);
183 DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
184 mCurrentState.displays.add(token, info);
185
186 return token;
187}
188
189sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
190 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
191 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
192 return NULL;
193 }
194 return mDefaultDisplays[id];
195}
196
Jamie Gennis9a78c902011-01-12 18:30:40 -0800197sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
198{
199 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
200 return gba;
201}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203void SurfaceFlinger::bootFinished()
204{
205 const nsecs_t now = systemTime();
206 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000207 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700208 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700209
210 // wait patiently for the window manager death
211 const String16 name("window");
212 sp<IBinder> window(defaultServiceManager()->getService(name));
213 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700214 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700215 }
216
217 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700218 // formerly we would just kill the process, but we now ask it to exit so it
219 // can choose where to stop the animation.
220 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700223void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
224 class MessageDestroyGLTexture : public MessageBase {
225 GLuint texture;
226 public:
227 MessageDestroyGLTexture(GLuint texture)
228 : texture(texture) {
229 }
230 virtual bool handler() {
231 glDeleteTextures(1, &texture);
232 return true;
233 }
234 };
235 postMessageAsync(new MessageDestroyGLTexture(texture));
236}
237
Mathias Agopiana4912602012-07-12 14:25:33 -0700238status_t SurfaceFlinger::selectConfigForPixelFormat(
239 EGLDisplay dpy,
240 EGLint const* attrs,
241 PixelFormat format,
242 EGLConfig* outConfig)
243{
244 EGLConfig config = NULL;
245 EGLint numConfigs = -1, n=0;
246 eglGetConfigs(dpy, NULL, 0, &numConfigs);
247 EGLConfig* const configs = new EGLConfig[numConfigs];
248 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
249 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;
268 EGLint attribs[] = {
269 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
270 EGL_RECORDABLE_ANDROID, EGL_TRUE,
271 EGL_NONE
272 };
273 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
274 if (err) {
275 // maybe we failed because of EGL_RECORDABLE_ANDROID
276 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
277 attribs[2] = EGL_NONE;
278 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
279 }
280 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
281 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
282 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
283 }
284 return config;
285}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
Mathias Agopiana4912602012-07-12 14:25:33 -0700287EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
288 // Also create our EGLContext
289 EGLint contextAttributes[] = {
290#ifdef EGL_IMG_context_priority
291#ifdef HAS_CONTEXT_PRIORITY
292#warning "using EGL_IMG_context_priority"
293 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
294#endif
295#endif
296 EGL_NONE, EGL_NONE
297 };
298 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
299 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
300 return ctxt;
301}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopiana4912602012-07-12 14:25:33 -0700303void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
304 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
305 if (!result) {
306 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
307 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 }
309
Mathias Agopiana4912602012-07-12 14:25:33 -0700310 GLExtensions& extensions(GLExtensions::getInstance());
311 extensions.initWithGLStrings(
312 glGetString(GL_VENDOR),
313 glGetString(GL_RENDERER),
314 glGetString(GL_VERSION),
315 glGetString(GL_EXTENSIONS),
316 eglQueryString(display, EGL_VENDOR),
317 eglQueryString(display, EGL_VERSION),
318 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700319
Mathias Agopiana4912602012-07-12 14:25:33 -0700320 EGLint w, h;
321 eglQuerySurface(display, surface, EGL_WIDTH, &w);
322 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700323
Mathias Agopiana4912602012-07-12 14:25:33 -0700324 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
325 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700328 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 glShadeModel(GL_FLAT);
331 glDisable(GL_DITHER);
332 glDisable(GL_CULL_FACE);
333
Mathias Agopiana4912602012-07-12 14:25:33 -0700334 struct pack565 {
335 inline uint16_t operator() (int r, int g, int b) const {
336 return (r<<11)|(g<<5)|b;
337 }
338 } pack565;
339
Jamie Gennis9575f602011-10-07 14:51:16 -0700340 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
341 glGenTextures(1, &mProtectedTexName);
342 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
343 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
344 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
345 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
346 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
347 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
348 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349
350 glViewport(0, 0, w, h);
351 glMatrixMode(GL_PROJECTION);
352 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700353 // put the origin in the left-bottom corner
354 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
Mathias Agopiana4912602012-07-12 14:25:33 -0700356 // print some debugging info
357 EGLint r,g,b,a;
358 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
359 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
360 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
361 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
362 ALOGI("EGL informations:");
363 ALOGI("vendor : %s", extensions.getEglVendor());
364 ALOGI("version : %s", extensions.getEglVersion());
365 ALOGI("extensions: %s", extensions.getEglExtension());
366 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
367 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
368 ALOGI("OpenGL ES informations:");
369 ALOGI("vendor : %s", extensions.getVendor());
370 ALOGI("renderer : %s", extensions.getRenderer());
371 ALOGI("version : %s", extensions.getVersion());
372 ALOGI("extensions: %s", extensions.getExtension());
373 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
374 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
375}
376
Mathias Agopiana4912602012-07-12 14:25:33 -0700377status_t SurfaceFlinger::readyToRun()
378{
379 ALOGI( "SurfaceFlinger's main thread ready to run. "
380 "Initializing graphics H/W...");
381
Mathias Agopiana4912602012-07-12 14:25:33 -0700382 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700383 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
384 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700385
386 // Initialize the main display
387 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700388 sp<FramebufferSurface> fbs = FramebufferSurface::create();
389 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700390 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
391 exit(0);
392 }
393
Mathias Agopiane57f2922012-08-09 16:29:12 -0700394 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
395 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700396
Mathias Agopiana4912602012-07-12 14:25:33 -0700397 // initialize the config and context
398 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700399 ANativeWindow* const anw = stc.get();
400 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700401 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
402 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700403
404 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700405
406 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
407 mDefaultDisplays[i] = new BBinder();
408 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
409 }
410 sp<DisplayDevice> hw = new DisplayDevice(this,
411 DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
412 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700413
414 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700415 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700416 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800417
Mathias Agopian028508c2012-07-25 21:12:12 -0700418 // start the EventThread
419 mEventThread = new EventThread(this);
420 mEventQueue.setEventThread(mEventThread);
421
Mathias Agopian86303202012-07-24 22:46:10 -0700422 // initialize the H/W composer
Mathias Agopian888c8222012-08-04 21:10:38 -0700423 mHwc = new HWComposer(this, *static_cast<HWComposer::EventHandler *>(this));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700424
425 // initialize our drawing state
426 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700427
Mathias Agopiana4912602012-07-12 14:25:33 -0700428 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800429 mReadyToRunBarrier.open();
430
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700431 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700432 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 return NO_ERROR;
435}
436
Mathias Agopiana67e4182012-06-19 17:26:12 -0700437void SurfaceFlinger::startBootAnim() {
438 // start boot animation
439 property_set("service.bootanim.exit", "0");
440 property_set("ctl.start", "bootanim");
441}
442
Mathias Agopiana4912602012-07-12 14:25:33 -0700443uint32_t SurfaceFlinger::getMaxTextureSize() const {
444 return mMaxTextureSize;
445}
446
447uint32_t SurfaceFlinger::getMaxViewportDims() const {
448 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
449 mMaxViewportDims[0] : mMaxViewportDims[1];
450}
451
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800453
Jamie Gennis582270d2011-08-17 18:19:00 -0700454bool SurfaceFlinger::authenticateSurfaceTexture(
455 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800456 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700457 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800458
459 // Check the visible layer list for the ISurface
460 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
461 size_t count = currentLayers.size();
462 for (size_t i=0 ; i<count ; i++) {
463 const sp<LayerBase>& layer(currentLayers[i]);
464 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700465 if (lbc != NULL) {
466 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
467 if (lbcBinder == surfaceTextureBinder) {
468 return true;
469 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800470 }
471 }
472
473 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700474 // SurfaceTexture gets destroyed before all the clients are done using it,
475 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800476 // will instead fail later on when the client tries to use the surface,
477 // which should be reported as "surface XYZ returned an -ENODEV". The
478 // purgatorized layers are no less authentic than the visible ones, so this
479 // should not cause any harm.
480 size_t purgatorySize = mLayerPurgatory.size();
481 for (size_t i=0 ; i<purgatorySize ; i++) {
482 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
483 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700484 if (lbc != NULL) {
485 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
486 if (lbcBinder == surfaceTextureBinder) {
487 return true;
488 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800489 }
490 }
491
492 return false;
493}
494
Mathias Agopianc666cae2012-07-25 18:56:13 -0700495status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700496 // TODO: this is here only for compatibility -- should go away eventually.
497 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700498 return BAD_INDEX;
499 }
Mathias Agopian42977342012-08-05 00:40:46 -0700500 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
501 info->w = hw->getWidth();
502 info->h = hw->getHeight();
503 info->xdpi = hw->getDpiX();
504 info->ydpi = hw->getDpiY();
Mathias Agopian888c8222012-08-04 21:10:38 -0700505 info->fps = float(1e9 / getHwComposer().getRefreshPeriod());
Mathias Agopian42977342012-08-05 00:40:46 -0700506 info->density = hw->getDensity();
507 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700508 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700509 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700510 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700511}
512
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800513// ----------------------------------------------------------------------------
514
515sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800516 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700517}
518
Mathias Agopian3094df32012-06-18 18:06:45 -0700519void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700520 EGLSurface result = EGL_NO_SURFACE;
521 EGLSurface old_surface = EGL_NO_SURFACE;
522 sp<SurfaceTextureClient> stc;
523
524 if (display != NULL) {
525 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700526 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700527 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700528 ALOGE_IF(result == EGL_NO_SURFACE,
529 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
530 display.get());
531 }
532
533 { // scope for the lock
534 Mutex::Autolock _l(mStateLock);
535 old_surface = mExternalDisplaySurface;
536 mExternalDisplayNativeWindow = stc;
537 mExternalDisplaySurface = result;
538 ALOGD("mExternalDisplaySurface = %p", result);
539 }
540
541 if (old_surface != EGL_NO_SURFACE) {
542 // Note: EGL allows to destroy an object while its current
543 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700544 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700545 }
546}
547
548EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
549 Mutex::Autolock _l(mStateLock);
550 return mExternalDisplaySurface;
551}
552
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800554
555void SurfaceFlinger::waitForEvent() {
556 mEventQueue.waitMessage();
557}
558
559void SurfaceFlinger::signalTransaction() {
560 mEventQueue.invalidate();
561}
562
563void SurfaceFlinger::signalLayerUpdate() {
564 mEventQueue.invalidate();
565}
566
567void SurfaceFlinger::signalRefresh() {
568 mEventQueue.refresh();
569}
570
571status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
572 nsecs_t reltime, uint32_t flags) {
573 return mEventQueue.postMessage(msg, reltime);
574}
575
576status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
577 nsecs_t reltime, uint32_t flags) {
578 status_t res = mEventQueue.postMessage(msg, reltime);
579 if (res == NO_ERROR) {
580 msg->wait();
581 }
582 return res;
583}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584
Mathias Agopian4fec8732012-06-29 14:12:52 -0700585bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 return true;
588}
589
Mathias Agopian86303202012-07-24 22:46:10 -0700590void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700591 mEventThread->onVSyncReceived(dpy, timestamp);
592}
593
594void SurfaceFlinger::eventControl(int event, int enabled) {
595 getHwComposer().eventControl(event, enabled);
596}
597
Mathias Agopian4fec8732012-06-29 14:12:52 -0700598void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800599 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800600 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700601 case MessageQueue::INVALIDATE:
602 handleMessageTransaction();
603 handleMessageInvalidate();
604 signalRefresh();
605 break;
606 case MessageQueue::REFRESH:
607 handleMessageRefresh();
608 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800609 }
610}
611
Mathias Agopian4fec8732012-06-29 14:12:52 -0700612void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700613 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700614 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700615 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700616 }
617}
618
619void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700620 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700621}
622
623void SurfaceFlinger::handleMessageRefresh() {
624 handleRefresh();
625
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700626 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700627 mVisibleRegionsDirty = false;
628 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700629
630 /*
631 * rebuild the visible layer list per screen
632 */
633
Mathias Agopian87baae12012-07-31 12:38:26 -0700634 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700635 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700636 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700637 Region opaqueRegion;
638 Region dirtyRegion;
639 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700640 hw->getLayerStack(), dirtyRegion, opaqueRegion);
641 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700642
643 Vector< sp<LayerBase> > layersSortedByZ;
644 const size_t count = currentLayers.size();
645 for (size_t i=0 ; i<count ; i++) {
646 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700647 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700648 if (!currentLayers[i]->visibleRegion.isEmpty()) {
649 layersSortedByZ.add(currentLayers[i]);
650 }
651 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700652 }
Mathias Agopian42977342012-08-05 00:40:46 -0700653 hw->setVisibleLayersSortedByZ(layersSortedByZ);
654 hw->undefinedRegion.set(hw->getBounds());
655 hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700656 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700657 }
658
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700659 HWComposer& hwc(getHwComposer());
660 if (hwc.initCheck() == NO_ERROR) {
661 // build the h/w work list
662 const bool workListsDirty = mHwWorkListDirty;
663 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700664 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700665 sp<const DisplayDevice> hw(mDisplays[dpy]);
666 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700667 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700668
Mathias Agopian1e260872012-08-08 18:35:12 -0700669 const int32_t id = hw->getDisplayId();
670 if (hwc.createWorkList(id, count) >= 0) {
671 HWComposer::LayerListIterator cur = hwc.begin(id);
672 const HWComposer::LayerListIterator end = hwc.end(id);
673 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
674 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700675
Mathias Agopian1e260872012-08-08 18:35:12 -0700676 if (CC_UNLIKELY(workListsDirty)) {
677 layer->setGeometry(hw, *cur);
678 if (mDebugDisableHWC || mDebugRegion) {
679 cur->setSkip(true);
680 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700681 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700682
Mathias Agopian1e260872012-08-08 18:35:12 -0700683 /*
684 * update the per-frame h/w composer data for each layer
685 * and build the transparent region of the FB
686 */
687 layer->setPerFrameData(hw, *cur);
688 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700689 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700690 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700691 status_t err = hwc.prepare();
692 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
693 }
694
695 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700696 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700697 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700698
Mathias Agopian87baae12012-07-31 12:38:26 -0700699 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700700 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700701 Region dirtyRegion;
702 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700703 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700704 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700705 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
706 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700707 }
Mathias Agopian42977342012-08-05 00:40:46 -0700708 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700709
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700710 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700711 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700712 // repaint the framebuffer (if needed)
713 handleRepaint(hw, dirtyRegion);
714 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700715 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700716 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700717 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700718 }
719
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700720 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700721
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700722
723#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724 // render to the external display if we have one
725 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
726 if (externalDisplaySurface != EGL_NO_SURFACE) {
727 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
728 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
729 externalDisplaySurface, externalDisplaySurface,
730 eglGetCurrentContext());
731
732 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
733
734 if (success) {
735 // redraw the screen entirely...
736 glDisable(GL_TEXTURE_EXTERNAL_OES);
737 glDisable(GL_TEXTURE_2D);
738 glClearColor(0,0,0,1);
739 glClear(GL_COLOR_BUFFER_BIT);
740 glMatrixMode(GL_MODELVIEW);
741 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700742
Mathias Agopian42977342012-08-05 00:40:46 -0700743 const sp<DisplayDevice>& hw(getDisplayDevice(0));
744 const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745 const size_t count = layers.size();
746 for (size_t i=0 ; i<count ; ++i) {
747 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700748 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700749 }
750
751 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
752 ALOGE_IF(!success, "external display eglSwapBuffers failed");
753
Mathias Agopian42977342012-08-05 00:40:46 -0700754 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700755 }
756
757 success = eglMakeCurrent(eglGetCurrentDisplay(),
758 cur, cur, eglGetCurrentContext());
759
760 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
761 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700762#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700763
764}
765
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766void SurfaceFlinger::postFramebuffer()
767{
Mathias Agopian841cde52012-03-01 15:44:37 -0800768 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800769
Mathias Agopiana44b0412011-10-16 18:46:35 -0700770 const nsecs_t now = systemTime();
771 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700772
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700773 HWComposer& hwc(getHwComposer());
774
Mathias Agopian92a979a2012-08-02 18:32:23 -0700775 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700776 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700777 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700778 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700779 const size_t count = currentLayers.size();
Mathias Agopian1e260872012-08-08 18:35:12 -0700780 const int32_t id = hw->getDisplayId();
781 HWComposer::LayerListIterator cur = hwc.begin(id);
782 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700783 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
784 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700785 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700786 }
787 }
Mathias Agopian42977342012-08-05 00:40:46 -0700788 hw->flip(hw->swapRegion);
789 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700790 }
791
Jesse Hallef194142012-06-14 14:45:17 -0700792 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700793 // FIXME: eventually commit() won't take arguments
Mathias Agopian42977342012-08-05 00:40:46 -0700794 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700795 }
796
Mathias Agopian92a979a2012-08-02 18:32:23 -0700797 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700798 sp<const DisplayDevice> hw(mDisplays[dpy]);
799 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700800 const size_t count = currentLayers.size();
801 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700802 int32_t id = hw->getDisplayId();
803 HWComposer::LayerListIterator cur = hwc.begin(id);
804 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700805 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700806 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700807 }
808 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700809 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700810 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700811 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700812 }
Jesse Hallef194142012-06-14 14:45:17 -0700813 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700814
815 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800816 }
817
Mathias Agopiana44b0412011-10-16 18:46:35 -0700818 mLastSwapBufferTime = systemTime() - now;
819 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820}
821
Mathias Agopian87baae12012-07-31 12:38:26 -0700822void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823{
Mathias Agopian841cde52012-03-01 15:44:37 -0800824 ATRACE_CALL();
825
Mathias Agopianca4d3602011-05-19 15:38:14 -0700826 Mutex::Autolock _l(mStateLock);
827 const nsecs_t now = systemTime();
828 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829
Mathias Agopianca4d3602011-05-19 15:38:14 -0700830 // Here we're guaranteed that some transaction flags are set
831 // so we can call handleTransactionLocked() unconditionally.
832 // We call getTransactionFlags(), which will also clear the flags,
833 // with mStateLock held to guarantee that mCurrentState won't change
834 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700835
Mathias Agopiane57f2922012-08-09 16:29:12 -0700836 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700837 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700838
Mathias Agopianca4d3602011-05-19 15:38:14 -0700839 mLastTransactionTime = systemTime() - now;
840 mDebugInTransaction = 0;
841 invalidateHwcGeometry();
842 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700843}
844
Mathias Agopian87baae12012-07-31 12:38:26 -0700845void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700846{
847 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 const size_t count = currentLayers.size();
849
850 /*
851 * Traversal of the children
852 * (perform the transaction for each of them if needed)
853 */
854
855 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
856 if (layersNeedTransaction) {
857 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700858 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
860 if (!trFlags) continue;
861
862 const uint32_t flags = layer->doTransaction(0);
863 if (flags & Layer::eVisibleRegion)
864 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865 }
866 }
867
868 /*
869 * Perform our own transaction if needed
870 */
871
Mathias Agopiane57f2922012-08-09 16:29:12 -0700872 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700873 // here we take advantage of Vector's copy-on-write semantics to
874 // improve performance by skipping the transaction entirely when
875 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700876 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
877 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700878 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700880 const size_t cc = curr.size();
881 const size_t dc = draw.size();
882
883 // find the displays that were removed
884 // (ie: in drawing state but not in current state)
885 // also handle displays that changed
886 // (ie: displays that are in both lists)
887 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700888 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
889 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700890 // in drawing state but not in current state
891 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700892 mDisplays.removeItem(draw[i].id);
893 } else {
894 ALOGW("trying to remove the main display");
895 }
896 } else {
897 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700898 const DisplayDeviceState& state(curr[j]);
899 if (state.surface != draw[i].surface) {
900 // changing the surface is like destroying and
901 // recreating the DisplayDevice
902
903 sp<SurfaceTextureClient> stc(
904 new SurfaceTextureClient(state.surface));
905
906 sp<DisplayDevice> disp = new DisplayDevice(this,
907 state.id, stc, 0, mEGLConfig);
908
909 disp->setLayerStack(state.layerStack);
910 disp->setOrientation(state.orientation);
911 // TODO: take viewport and frame into account
912 mDisplays.replaceValueFor(state.id, disp);
913 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700914 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700915 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700916 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700917 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700918 if (state.orientation != draw[i].orientation ||
919 state.viewport != draw[i].viewport ||
920 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700921 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
922 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700923 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700924 }
925 }
926 }
927
928 // find displays that were added
929 // (ie: in current state but not in drawing state)
930 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700931 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700932 // FIXME: we need to pass the surface here
Mathias Agopiane57f2922012-08-09 16:29:12 -0700933 const DisplayDeviceState& state(curr[i]);
934 sp<SurfaceTextureClient> stc(
935 new SurfaceTextureClient(state.surface));
936 sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
937 stc, 0, mEGLConfig);
938 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700939 }
940 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 }
942
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700943 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
944 // layers have been added
945 mVisibleRegionsDirty = true;
946 }
947
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 // some layers might have been removed, so
949 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700950 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700951 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700953 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700954 const size_t count = previousLayers.size();
955 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700956 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700957 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700958 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700959 // TODO: we could traverse the tree from front to back and
960 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700961 // TODO: we could cache the transformed region
962 Layer::State front(layer->drawingState());
963 Region visibleReg = front.transform.transform(
964 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700965 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700966 }
967 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 }
970
971 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700972}
973
974void SurfaceFlinger::commitTransaction()
975{
976 if (!mLayersPendingRemoval.isEmpty()) {
977 // Notify removed layers now that they can't be drawn from
978 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
979 mLayersPendingRemoval[i]->onRemoved();
980 }
981 mLayersPendingRemoval.clear();
982 }
983
984 mDrawingState = mCurrentState;
985 mTransationPending = false;
986 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987}
988
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700990 const LayerVector& currentLayers, uint32_t layerStack,
991 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992{
Mathias Agopian841cde52012-03-01 15:44:37 -0800993 ATRACE_CALL();
994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 Region aboveOpaqueLayers;
996 Region aboveCoveredLayers;
997 Region dirty;
998
Mathias Agopian87baae12012-07-31 12:38:26 -0700999 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000
1001 size_t i = currentLayers.size();
1002 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001003 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004
1005 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001006 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007
Mathias Agopian87baae12012-07-31 12:38:26 -07001008 // only consider the layers on the given later stack
1009 if (s.layerStack != layerStack)
1010 continue;
1011
Mathias Agopianab028732010-03-16 16:41:46 -07001012 /*
1013 * opaqueRegion: area of a surface that is fully opaque.
1014 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001016
1017 /*
1018 * visibleRegion: area of a surface that is visible on screen
1019 * and not fully transparent. This is essentially the layer's
1020 * footprint minus the opaque regions above it.
1021 * Areas covered by a translucent surface are considered visible.
1022 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001024
1025 /*
1026 * coveredRegion: area of a surface that is covered by all
1027 * visible regions above it (which includes the translucent areas).
1028 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001030
1031
1032 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001033 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001034 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001035 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001037 if (!visibleRegion.isEmpty()) {
1038 // Remove the transparent area from the visible region
1039 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001040 Region transparentRegionScreen;
1041 const Transform tr(s.transform);
1042 if (tr.transformed()) {
1043 if (tr.preserveRects()) {
1044 // transform the transparent region
1045 transparentRegionScreen = tr.transform(s.transparentRegion);
1046 } else {
1047 // transformation too complex, can't do the
1048 // transparent region optimization.
1049 transparentRegionScreen.clear();
1050 }
1051 } else {
1052 transparentRegionScreen = s.transparentRegion;
1053 }
1054 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001055 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056
Mathias Agopianab028732010-03-16 16:41:46 -07001057 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001058 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001059 if (s.alpha==255 && !translucent &&
1060 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1061 // the opaque region is the layer's footprint
1062 opaqueRegion = visibleRegion;
1063 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 }
1065 }
1066
Mathias Agopianab028732010-03-16 16:41:46 -07001067 // Clip the covered region to the visible region
1068 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1069
1070 // Update aboveCoveredLayers for next (lower) layer
1071 aboveCoveredLayers.orSelf(visibleRegion);
1072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 // subtract the opaque region covered by the layers above us
1074 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075
1076 // compute this layer's dirty region
1077 if (layer->contentDirty) {
1078 // we need to invalidate the whole region
1079 dirty = visibleRegion;
1080 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001081 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 layer->contentDirty = false;
1083 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001084 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001085 * the exposed region consists of two components:
1086 * 1) what's VISIBLE now and was COVERED before
1087 * 2) what's EXPOSED now less what was EXPOSED before
1088 *
1089 * note that (1) is conservative, we start with the whole
1090 * visible region but only keep what used to be covered by
1091 * something -- which mean it may have been exposed.
1092 *
1093 * (2) handles areas that were not covered by anything but got
1094 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001095 */
Mathias Agopianab028732010-03-16 16:41:46 -07001096 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001097 const Region oldVisibleRegion = layer->visibleRegion;
1098 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001099 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1100 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 }
1102 dirty.subtractSelf(aboveOpaqueLayers);
1103
1104 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001105 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106
Mathias Agopianab028732010-03-16 16:41:46 -07001107 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 // Store the visible region is screen space
1111 layer->setVisibleRegion(visibleRegion);
1112 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 }
1114
Mathias Agopian87baae12012-07-31 12:38:26 -07001115 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116}
1117
Mathias Agopian87baae12012-07-31 12:38:26 -07001118void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1119 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001120 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001121 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1122 if (hw->getLayerStack() == layerStack) {
1123 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001124 }
1125 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001126}
1127
1128void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001130 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001131 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001133 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001134
Mathias Agopian4fec8732012-06-29 14:12:52 -07001135 bool visibleRegions = false;
1136 const size_t count = currentLayers.size();
1137 sp<LayerBase> const* layers = currentLayers.array();
1138 for (size_t i=0 ; i<count ; i++) {
1139 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001140 const Region dirty(layer->latchBuffer(visibleRegions));
1141 Layer::State s(layer->drawingState());
1142 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001143 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001144
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001145 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146}
1147
Mathias Agopianad456f92011-01-13 17:53:01 -08001148void SurfaceFlinger::invalidateHwcGeometry()
1149{
1150 mHwWorkListDirty = true;
1151}
1152
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001153void SurfaceFlinger::handleRefresh()
1154{
1155 bool needInvalidate = false;
1156 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1157 const size_t count = currentLayers.size();
1158 for (size_t i=0 ; i<count ; i++) {
1159 const sp<LayerBase>& layer(currentLayers[i]);
1160 if (layer->onPreComposition()) {
1161 needInvalidate = true;
1162 }
1163 }
1164 if (needInvalidate) {
1165 signalLayerUpdate();
1166 }
1167}
1168
Mathias Agopian42977342012-08-05 00:40:46 -07001169void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001170 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171{
Mathias Agopian841cde52012-03-01 15:44:37 -08001172 ATRACE_CALL();
1173
Mathias Agopian87baae12012-07-31 12:38:26 -07001174 Region dirtyRegion(inDirtyRegion);
1175
Mathias Agopianb8a55602009-06-26 19:06:36 -07001176 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001177 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178
Glenn Kasten99ed2242011-12-15 09:51:17 -08001179 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001180 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 }
1182
Mathias Agopian42977342012-08-05 00:40:46 -07001183 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001184 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001185 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1186 // takes a rectangle, we must make sure to update that whole
1187 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001188 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001189 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001190 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001191 // We need to redraw the rectangle that will be updated
1192 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001193 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001194 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001195 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001196 } else {
1197 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001198 dirtyRegion.set(hw->bounds());
1199 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 }
1201 }
1202
Mathias Agopian87baae12012-07-31 12:38:26 -07001203 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001205 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1206 const size_t count = currentLayers.size();
1207 for (size_t i=0 ; i<count ; i++) {
1208 currentLayers[i]->onPostComposition();
1209 }
1210
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001211 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001212 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213}
1214
Mathias Agopian42977342012-08-05 00:40:46 -07001215void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001216{
Mathias Agopian86303202012-07-24 22:46:10 -07001217 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001218 int32_t id = hw->getDisplayId();
1219 HWComposer::LayerListIterator cur = hwc.begin(id);
1220 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001221
Mathias Agopian1e260872012-08-08 18:35:12 -07001222 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001223 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001224
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001225 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001226
1227 // set the frame buffer
1228 glMatrixMode(GL_MODELVIEW);
1229 glLoadIdentity();
1230
1231 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001232 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001233 // when using overlays, we assume a fully transparent framebuffer
1234 // NOTE: we could reduce how much we need to clear, for instance
1235 // remove where there are opaque FB layers. however, on some
1236 // GPUs doing a "clean slate" glClear might be more efficient.
1237 // We'll revisit later if needed.
1238 glClearColor(0, 0, 0, 0);
1239 glClear(GL_COLOR_BUFFER_BIT);
1240 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001241 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001242 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001243 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001244 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001245 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001246 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001247 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001248
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001249 /*
1250 * and then, render the layers targeted at the framebuffer
1251 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001252
Mathias Agopian42977342012-08-05 00:40:46 -07001253 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001254 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001255 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001256 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001257 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001258 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001259 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001260 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001261 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001262 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001263 // never clear the very first layer since we're
1264 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001265 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001266 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001267 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001268 continue;
1269 }
1270 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001271 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001272 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001273 if (cur != end) {
1274 ++cur;
1275 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001276 }
1277 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278}
1279
Mathias Agopian42977342012-08-05 00:40:46 -07001280void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001281 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282{
Mathias Agopian42977342012-08-05 00:40:46 -07001283 const uint32_t flags = hw->getFlags();
1284 const int32_t height = hw->getHeight();
1285 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001286 return;
1287 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001288
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001289 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1290 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001291 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001292 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001293 }
1294
Mathias Agopianc492e672011-10-18 14:49:27 -07001295 glDisable(GL_TEXTURE_EXTERNAL_OES);
1296 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298
Mathias Agopian0926f502009-05-04 14:17:04 -07001299 static int toggle = 0;
1300 toggle = 1 - toggle;
1301 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001302 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001303 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001304 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001305 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306
Mathias Agopian87baae12012-07-31 12:38:26 -07001307 Region::const_iterator it = dirtyRegion.begin();
1308 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001309 while (it != end) {
1310 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001312 { r.left, height - r.top },
1313 { r.left, height - r.bottom },
1314 { r.right, height - r.bottom },
1315 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 };
1317 glVertexPointer(2, GL_FLOAT, 0, vertices);
1318 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1319 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001320
Mathias Agopian42977342012-08-05 00:40:46 -07001321 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322
1323 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001324 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325}
1326
Mathias Agopian87baae12012-07-31 12:38:26 -07001327void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001329 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001330 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001331 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001332 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001333
1334 GLfloat vertices[4][2];
1335 glVertexPointer(2, GL_FLOAT, 0, vertices);
1336 Region::const_iterator it = region.begin();
1337 Region::const_iterator const end = region.end();
1338 while (it != end) {
1339 const Rect& r = *it++;
1340 vertices[0][0] = r.left;
1341 vertices[0][1] = r.top;
1342 vertices[1][0] = r.right;
1343 vertices[1][1] = r.top;
1344 vertices[2][0] = r.right;
1345 vertices[2][1] = r.bottom;
1346 vertices[3][0] = r.left;
1347 vertices[3][1] = r.bottom;
1348 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1349 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350}
1351
Mathias Agopian96f08192010-06-02 23:28:45 -07001352ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1353 const sp<LayerBaseClient>& lbc)
1354{
Mathias Agopian96f08192010-06-02 23:28:45 -07001355 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001356 size_t name = client->attachLayer(lbc);
1357
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001359 Mutex::Autolock _l(mStateLock);
1360 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001361
Mathias Agopian4f113742011-05-03 16:21:41 -07001362 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001363}
1364
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001365status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366{
1367 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001368 status_t err = purgatorizeLayer_l(layer);
1369 if (err == NO_ERROR)
1370 setTransactionFlags(eTransactionNeeded);
1371 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372}
1373
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001374status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375{
1376 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1377 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001378 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 return NO_ERROR;
1380 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001381 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382}
1383
Mathias Agopian9a112062009-04-17 19:36:26 -07001384status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1385{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001386 // First add the layer to the purgatory list, which makes sure it won't
1387 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001388 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001389 if (err >= 0) {
1390 mLayerPurgatory.add(layerBase);
1391 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001392
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001393 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001394
Mathias Agopian3d579642009-06-04 18:46:21 -07001395 // it's possible that we don't find a layer, because it might
1396 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001397 // from the user because there is a race between Client::destroySurface(),
1398 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001399 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1400}
1401
Mathias Agopiandea20b12011-05-03 17:04:02 -07001402uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1403{
1404 return android_atomic_release_load(&mTransactionFlags);
1405}
1406
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1408{
1409 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1410}
1411
Mathias Agopianbb641242010-05-18 17:06:55 -07001412uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413{
1414 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1415 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001416 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 }
1418 return old;
1419}
1420
Mathias Agopian8b33f032012-07-24 20:43:54 -07001421void SurfaceFlinger::setTransactionState(
1422 const Vector<ComposerState>& state,
1423 const Vector<DisplayState>& displays,
1424 uint32_t flags)
1425{
Mathias Agopian698c0872011-06-28 19:09:31 -07001426 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001427 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001428
1429 size_t count = displays.size();
1430 for (size_t i=0 ; i<count ; i++) {
1431 const DisplayState& s(displays[i]);
1432 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001433 }
1434
Mathias Agopiane57f2922012-08-09 16:29:12 -07001435 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001436 for (size_t i=0 ; i<count ; i++) {
1437 const ComposerState& s(state[i]);
1438 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001439 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001440 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001441
Mathias Agopian386aa982011-11-07 21:58:03 -08001442 if (transactionFlags) {
1443 // this triggers the transaction
1444 setTransactionFlags(transactionFlags);
1445
1446 // if this is a synchronous transaction, wait for it to take effect
1447 // before returning.
1448 if (flags & eSynchronous) {
1449 mTransationPending = true;
1450 }
1451 while (mTransationPending) {
1452 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1453 if (CC_UNLIKELY(err != NO_ERROR)) {
1454 // just in case something goes wrong in SF, return to the
1455 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001456 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001457 mTransationPending = false;
1458 break;
1459 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001460 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 }
1462}
1463
Mathias Agopiane57f2922012-08-09 16:29:12 -07001464uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1465{
1466 uint32_t flags = 0;
1467 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1468 if (disp.id >= 0) {
1469 const uint32_t what = s.what;
1470 if (what & DisplayState::eSurfaceChanged) {
1471 if (disp.surface->asBinder() != s.surface->asBinder()) {
1472 disp.surface = s.surface;
1473 flags |= eDisplayTransactionNeeded;
1474 }
1475 }
1476 if (what & DisplayState::eLayerStackChanged) {
1477 if (disp.layerStack != s.layerStack) {
1478 disp.layerStack = s.layerStack;
1479 flags |= eDisplayTransactionNeeded;
1480 }
1481 }
1482 if (what & DisplayState::eTransformChanged) {
1483 if (disp.orientation != s.orientation) {
1484 disp.orientation = s.orientation;
1485 flags |= eDisplayTransactionNeeded;
1486 }
1487 if (disp.frame != s.frame) {
1488 disp.frame = s.frame;
1489 flags |= eDisplayTransactionNeeded;
1490 }
1491 if (disp.viewport != s.viewport) {
1492 disp.viewport = s.viewport;
1493 flags |= eDisplayTransactionNeeded;
1494 }
1495 }
1496 }
1497 return flags;
1498}
1499
1500uint32_t SurfaceFlinger::setClientStateLocked(
1501 const sp<Client>& client,
1502 const layer_state_t& s)
1503{
1504 uint32_t flags = 0;
1505 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1506 if (layer != 0) {
1507 const uint32_t what = s.what;
1508 if (what & layer_state_t::ePositionChanged) {
1509 if (layer->setPosition(s.x, s.y))
1510 flags |= eTraversalNeeded;
1511 }
1512 if (what & layer_state_t::eLayerChanged) {
1513 // NOTE: index needs to be calculated before we update the state
1514 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1515 if (layer->setLayer(s.z)) {
1516 mCurrentState.layersSortedByZ.removeAt(idx);
1517 mCurrentState.layersSortedByZ.add(layer);
1518 // we need traversal (state changed)
1519 // AND transaction (list changed)
1520 flags |= eTransactionNeeded|eTraversalNeeded;
1521 }
1522 }
1523 if (what & layer_state_t::eSizeChanged) {
1524 if (layer->setSize(s.w, s.h)) {
1525 flags |= eTraversalNeeded;
1526 }
1527 }
1528 if (what & layer_state_t::eAlphaChanged) {
1529 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1530 flags |= eTraversalNeeded;
1531 }
1532 if (what & layer_state_t::eMatrixChanged) {
1533 if (layer->setMatrix(s.matrix))
1534 flags |= eTraversalNeeded;
1535 }
1536 if (what & layer_state_t::eTransparentRegionChanged) {
1537 if (layer->setTransparentRegionHint(s.transparentRegion))
1538 flags |= eTraversalNeeded;
1539 }
1540 if (what & layer_state_t::eVisibilityChanged) {
1541 if (layer->setFlags(s.flags, s.mask))
1542 flags |= eTraversalNeeded;
1543 }
1544 if (what & layer_state_t::eCropChanged) {
1545 if (layer->setCrop(s.crop))
1546 flags |= eTraversalNeeded;
1547 }
1548 if (what & layer_state_t::eLayerStackChanged) {
1549 // NOTE: index needs to be calculated before we update the state
1550 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1551 if (layer->setLayerStack(s.layerStack)) {
1552 mCurrentState.layersSortedByZ.removeAt(idx);
1553 mCurrentState.layersSortedByZ.add(layer);
1554 // we need traversal (state changed)
1555 // AND transaction (list changed)
1556 flags |= eTransactionNeeded|eTraversalNeeded;
1557 }
1558 }
1559 }
1560 return flags;
1561}
1562
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001563sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001564 ISurfaceComposerClient::surface_data_t* params,
1565 const String8& name,
1566 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1568 uint32_t flags)
1569{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001570 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001571 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001572
1573 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001574 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001575 int(w), int(h));
1576 return surfaceHandle;
1577 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001578
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001579 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001580 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1581 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001582 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001584 case ISurfaceComposerClient::eFXSurfaceBlur:
1585 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001586 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001588 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001589 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001590 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 }
1592
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001593 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001594 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001595 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001596 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001598 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001599 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001600 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001601 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001602 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 }
1604
1605 return surfaceHandle;
1606}
1607
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001608sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001609 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001610 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001611 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612{
1613 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001614 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615 case PIXEL_FORMAT_TRANSPARENT:
1616 case PIXEL_FORMAT_TRANSLUCENT:
1617 format = PIXEL_FORMAT_RGBA_8888;
1618 break;
1619 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001620#ifdef NO_RGBX_8888
1621 format = PIXEL_FORMAT_RGB_565;
1622#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001623 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001624#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 break;
1626 }
1627
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001628#ifdef NO_RGBX_8888
1629 if (format == PIXEL_FORMAT_RGBX_8888)
1630 format = PIXEL_FORMAT_RGBA_8888;
1631#endif
1632
Mathias Agopian96f08192010-06-02 23:28:45 -07001633 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001634 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001635 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001636 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001637 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
1639 return layer;
1640}
1641
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001642sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001643 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001644 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645{
Mathias Agopian96f08192010-06-02 23:28:45 -07001646 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001647 return layer;
1648}
1649
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001650sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001651 const sp<Client>& client, DisplayID display,
1652 uint32_t w, uint32_t h, uint32_t flags)
1653{
1654 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 return layer;
1656}
1657
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001658status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659{
Mathias Agopian9a112062009-04-17 19:36:26 -07001660 /*
1661 * called by the window manager, when a surface should be marked for
1662 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001663 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001664 * The surface is removed from the current and drawing lists, but placed
1665 * in the purgatory queue, so it's not destroyed right-away (we need
1666 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001667 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001668
Mathias Agopian48d819a2009-09-10 19:41:18 -07001669 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001670 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001671 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001672
Mathias Agopian48d819a2009-09-10 19:41:18 -07001673 if (layer != 0) {
1674 err = purgatorizeLayer_l(layer);
1675 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001676 setTransactionFlags(eTransactionNeeded);
1677 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001678 }
1679 return err;
1680}
1681
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001682status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001683{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001684 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001685 status_t err = NO_ERROR;
1686 sp<LayerBaseClient> l(layer.promote());
1687 if (l != NULL) {
1688 Mutex::Autolock _l(mStateLock);
1689 err = removeLayer_l(l);
1690 if (err == NAME_NOT_FOUND) {
1691 // The surface wasn't in the current list, which means it was
1692 // removed already, which means it is in the purgatory,
1693 // and need to be removed from there.
1694 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001695 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001696 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001697 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001698 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001699 "error removing layer=%p (%s)", l.get(), strerror(-err));
1700 }
1701 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702}
1703
Mathias Agopianb60314a2012-04-10 22:09:54 -07001704// ---------------------------------------------------------------------------
1705
1706void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001707 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001708 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001709 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001710 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001711 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712}
1713
Mathias Agopianb60314a2012-04-10 22:09:54 -07001714void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001715 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001716 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1717 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001718 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001719 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001720 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001721 // from this point on, SF will stop drawing
1722 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723}
1724
Colin Cross8e533062012-06-07 13:17:52 -07001725void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001726 class MessageScreenAcquired : public MessageBase {
1727 SurfaceFlinger* flinger;
1728 public:
1729 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1730 virtual bool handler() {
1731 flinger->onScreenAcquired();
1732 return true;
1733 }
1734 };
1735 sp<MessageBase> msg = new MessageScreenAcquired(this);
1736 postMessageSync(msg);
1737}
1738
Colin Cross8e533062012-06-07 13:17:52 -07001739void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001740 class MessageScreenReleased : public MessageBase {
1741 SurfaceFlinger* flinger;
1742 public:
1743 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1744 virtual bool handler() {
1745 flinger->onScreenReleased();
1746 return true;
1747 }
1748 };
1749 sp<MessageBase> msg = new MessageScreenReleased(this);
1750 postMessageSync(msg);
1751}
1752
1753// ---------------------------------------------------------------------------
1754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1756{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001757 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 char buffer[SIZE];
1759 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001760
1761 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762 snprintf(buffer, SIZE, "Permission Denial: "
1763 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1764 IPCThreadState::self()->getCallingPid(),
1765 IPCThreadState::self()->getCallingUid());
1766 result.append(buffer);
1767 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001768 // Try to get the main lock, but don't insist if we can't
1769 // (this would indicate SF is stuck, but we want to be able to
1770 // print something in dumpsys).
1771 int retry = 3;
1772 while (mStateLock.tryLock()<0 && --retry>=0) {
1773 usleep(1000000);
1774 }
1775 const bool locked(retry >= 0);
1776 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001777 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001778 "SurfaceFlinger appears to be unresponsive, "
1779 "dumping anyways (no locks held)\n");
1780 result.append(buffer);
1781 }
1782
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001783 bool dumpAll = true;
1784 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001785 size_t numArgs = args.size();
1786 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001787 if ((index < numArgs) &&
1788 (args[index] == String16("--list"))) {
1789 index++;
1790 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001791 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001792 }
1793
1794 if ((index < numArgs) &&
1795 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001796 index++;
1797 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001798 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001799 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001800
1801 if ((index < numArgs) &&
1802 (args[index] == String16("--latency-clear"))) {
1803 index++;
1804 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001805 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001806 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001808
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001809 if (dumpAll) {
1810 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001811 }
1812
Mathias Agopian9795c422009-08-26 16:36:26 -07001813 if (locked) {
1814 mStateLock.unlock();
1815 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 }
1817 write(fd, result.string(), result.size());
1818 return NO_ERROR;
1819}
1820
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001821void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1822 String8& result, char* buffer, size_t SIZE) const
1823{
1824 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1825 const size_t count = currentLayers.size();
1826 for (size_t i=0 ; i<count ; i++) {
1827 const sp<LayerBase>& layer(currentLayers[i]);
1828 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1829 result.append(buffer);
1830 }
1831}
1832
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001833void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1834 String8& result, char* buffer, size_t SIZE) const
1835{
1836 String8 name;
1837 if (index < args.size()) {
1838 name = String8(args[index]);
1839 index++;
1840 }
1841
1842 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1843 const size_t count = currentLayers.size();
1844 for (size_t i=0 ; i<count ; i++) {
1845 const sp<LayerBase>& layer(currentLayers[i]);
1846 if (name.isEmpty()) {
1847 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1848 result.append(buffer);
1849 }
1850 if (name.isEmpty() || (name == layer->getName())) {
1851 layer->dumpStats(result, buffer, SIZE);
1852 }
1853 }
1854}
1855
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001856void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1857 String8& result, char* buffer, size_t SIZE) const
1858{
1859 String8 name;
1860 if (index < args.size()) {
1861 name = String8(args[index]);
1862 index++;
1863 }
1864
1865 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1866 const size_t count = currentLayers.size();
1867 for (size_t i=0 ; i<count ; i++) {
1868 const sp<LayerBase>& layer(currentLayers[i]);
1869 if (name.isEmpty() || (name == layer->getName())) {
1870 layer->clearStats();
1871 }
1872 }
1873}
1874
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001875void SurfaceFlinger::dumpAllLocked(
1876 String8& result, char* buffer, size_t SIZE) const
1877{
1878 // figure out if we're stuck somewhere
1879 const nsecs_t now = systemTime();
1880 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1881 const nsecs_t inTransaction(mDebugInTransaction);
1882 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1883 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1884
1885 /*
1886 * Dump the visible layer list
1887 */
1888 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1889 const size_t count = currentLayers.size();
1890 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1891 result.append(buffer);
1892 for (size_t i=0 ; i<count ; i++) {
1893 const sp<LayerBase>& layer(currentLayers[i]);
1894 layer->dump(result, buffer, SIZE);
1895 }
1896
1897 /*
1898 * Dump the layers in the purgatory
1899 */
1900
1901 const size_t purgatorySize = mLayerPurgatory.size();
1902 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1903 result.append(buffer);
1904 for (size_t i=0 ; i<purgatorySize ; i++) {
1905 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1906 layer->shortDump(result, buffer, SIZE);
1907 }
1908
1909 /*
1910 * Dump SurfaceFlinger global state
1911 */
1912
1913 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1914 result.append(buffer);
1915
Mathias Agopian888c8222012-08-04 21:10:38 -07001916 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001917 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001918 const GLExtensions& extensions(GLExtensions::getInstance());
1919 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1920 extensions.getVendor(),
1921 extensions.getRenderer(),
1922 extensions.getVersion());
1923 result.append(buffer);
1924
1925 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001926 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001927 result.append(buffer);
1928
1929 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1930 result.append(buffer);
1931
Mathias Agopian42977342012-08-05 00:40:46 -07001932 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001933 snprintf(buffer, SIZE,
1934 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001935 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001936 result.append(buffer);
1937 snprintf(buffer, SIZE,
1938 " last eglSwapBuffers() time: %f us\n"
1939 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001940 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001941 " refresh-rate : %f fps\n"
1942 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001943 " y-dpi : %f\n"
1944 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001945 mLastSwapBufferTime/1000.0,
1946 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001947 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001948 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian42977342012-08-05 00:40:46 -07001949 hw->getDpiX(),
1950 hw->getDpiY(),
1951 hw->getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001952 result.append(buffer);
1953
1954 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1955 inSwapBuffersDuration/1000.0);
1956 result.append(buffer);
1957
1958 snprintf(buffer, SIZE, " transaction time: %f us\n",
1959 inTransactionDuration/1000.0);
1960 result.append(buffer);
1961
1962 /*
1963 * VSYNC state
1964 */
1965 mEventThread->dump(result, buffer, SIZE);
1966
1967 /*
1968 * Dump HWComposer state
1969 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001970 snprintf(buffer, SIZE, "h/w composer state:\n");
1971 result.append(buffer);
1972 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1973 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1974 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1975 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07001976 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001977
1978 /*
1979 * Dump gralloc state
1980 */
1981 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1982 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07001983 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001984}
1985
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986status_t SurfaceFlinger::onTransact(
1987 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1988{
1989 switch (code) {
1990 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001991 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001993 case BLANK:
1994 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995 {
1996 // codes that require permission check
1997 IPCThreadState* ipc = IPCThreadState::self();
1998 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001999 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002000 if ((uid != AID_GRAPHICS) &&
2001 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002002 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002003 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2004 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002006 break;
2007 }
2008 case CAPTURE_SCREEN:
2009 {
2010 // codes that require permission check
2011 IPCThreadState* ipc = IPCThreadState::self();
2012 const int pid = ipc->getCallingPid();
2013 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002014 if ((uid != AID_GRAPHICS) &&
2015 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002016 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002017 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2018 return PERMISSION_DENIED;
2019 }
2020 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002021 }
2022 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002024 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2025 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002026 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002027 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002028 IPCThreadState* ipc = IPCThreadState::self();
2029 const int pid = ipc->getCallingPid();
2030 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002031 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002032 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002033 return PERMISSION_DENIED;
2034 }
2035 int n;
2036 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002037 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002038 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039 return NO_ERROR;
2040 case 1002: // SHOW_UPDATES
2041 n = data.readInt32();
2042 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002043 invalidateHwcGeometry();
2044 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002045 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002047 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002048 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002049 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002050 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002051 setTransactionFlags(
2052 eTransactionNeeded|
2053 eDisplayTransactionNeeded|
2054 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002055 return NO_ERROR;
2056 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002057 case 1006:{ // send empty update
2058 signalRefresh();
2059 return NO_ERROR;
2060 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002061 case 1008: // toggle use of hw composer
2062 n = data.readInt32();
2063 mDebugDisableHWC = n ? 1 : 0;
2064 invalidateHwcGeometry();
2065 repaintEverything();
2066 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002067 case 1009: // toggle use of transform hint
2068 n = data.readInt32();
2069 mDebugDisableTransformHint = n ? 1 : 0;
2070 invalidateHwcGeometry();
2071 repaintEverything();
2072 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002073 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002074 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002075 reply->writeInt32(0);
2076 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002077 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002078 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079 return NO_ERROR;
2080 case 1013: {
2081 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002082 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2083 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084 }
2085 return NO_ERROR;
2086 }
2087 }
2088 return err;
2089}
2090
Mathias Agopian53331da2011-08-22 21:44:41 -07002091void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002092 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002093 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002094}
2095
Mathias Agopian59119e62010-10-11 12:37:43 -07002096// ---------------------------------------------------------------------------
2097
Mathias Agopian118d0242011-10-13 16:02:48 -07002098status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2099 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2100{
2101 Mutex::Autolock _l(mStateLock);
2102 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2103}
2104
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002105status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2106 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002107{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002108 ATRACE_CALL();
2109
Mathias Agopian59119e62010-10-11 12:37:43 -07002110 if (!GLExtensions::getInstance().haveFramebufferObject())
2111 return INVALID_OPERATION;
2112
2113 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002114 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2115 const uint32_t hw_w = hw->getWidth();
2116 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002117 GLfloat u = 1;
2118 GLfloat v = 1;
2119
2120 // make sure to clear all GL error flags
2121 while ( glGetError() != GL_NO_ERROR ) ;
2122
2123 // create a FBO
2124 GLuint name, tname;
2125 glGenTextures(1, &tname);
2126 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002127 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2128 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002129 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2130 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002131 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002132 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002133 GLint tw = (2 << (31 - clz(hw_w)));
2134 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002135 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2136 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002137 u = GLfloat(hw_w) / tw;
2138 v = GLfloat(hw_h) / th;
2139 }
2140 glGenFramebuffersOES(1, &name);
2141 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002142 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2143 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002144
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002145 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002146 glDisable(GL_TEXTURE_EXTERNAL_OES);
2147 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002148 glClearColor(0,0,0,1);
2149 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002150 glMatrixMode(GL_MODELVIEW);
2151 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002152 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002153 const size_t count = layers.size();
2154 for (size_t i=0 ; i<count ; ++i) {
2155 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002156 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002157 }
2158
Mathias Agopian42977342012-08-05 00:40:46 -07002159 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002160
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002161 // back to main framebuffer
2162 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002163 glDeleteFramebuffersOES(1, &name);
2164
2165 *textureName = tname;
2166 *uOut = u;
2167 *vOut = v;
2168 return NO_ERROR;
2169}
2170
2171// ---------------------------------------------------------------------------
2172
Mathias Agopian74c40c02010-09-29 13:02:36 -07002173status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2174 sp<IMemoryHeap>* heap,
2175 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002176 uint32_t sw, uint32_t sh,
2177 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002178{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002179 ATRACE_CALL();
2180
Mathias Agopian74c40c02010-09-29 13:02:36 -07002181 status_t result = PERMISSION_DENIED;
2182
2183 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002184 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002185 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002186 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002187 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002188
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002189 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002190 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002191 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002192
2193 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002194 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2195 const uint32_t hw_w = hw->getWidth();
2196 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002197
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002198 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002199 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002200 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002201 return PERMISSION_DENIED;
2202 }
2203
2204 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002205 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002206 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002207 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002208
2209 sw = (!sw) ? hw_w : sw;
2210 sh = (!sh) ? hw_h : sh;
2211 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002212 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002213
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002214// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2215// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002216
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217 // make sure to clear all GL error flags
2218 while ( glGetError() != GL_NO_ERROR ) ;
2219
2220 // create a FBO
2221 GLuint name, tname;
2222 glGenRenderbuffersOES(1, &tname);
2223 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2224 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002225
Mathias Agopian74c40c02010-09-29 13:02:36 -07002226 glGenFramebuffersOES(1, &name);
2227 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2228 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2229 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2230
2231 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002232
Mathias Agopian74c40c02010-09-29 13:02:36 -07002233 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2234
2235 // invert everything, b/c glReadPixel() below will invert the FB
2236 glViewport(0, 0, sw, sh);
2237 glMatrixMode(GL_PROJECTION);
2238 glPushMatrix();
2239 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002240 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241 glMatrixMode(GL_MODELVIEW);
2242
2243 // redraw the screen entirely...
2244 glClearColor(0,0,0,1);
2245 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002246
Jamie Gennis9575f602011-10-07 14:51:16 -07002247 const LayerVector& layers(mDrawingState.layersSortedByZ);
2248 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002249 for (size_t i=0 ; i<count ; ++i) {
2250 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002251 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002252 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002253 const uint32_t z = layer->drawingState().z;
2254 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002255 if (filtering) layer->setFiltering(true);
2256 layer->draw(hw);
2257 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002258 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002259 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002260 }
2261
Mathias Agopian74c40c02010-09-29 13:02:36 -07002262 // check for errors and return screen capture
2263 if (glGetError() != GL_NO_ERROR) {
2264 // error while rendering
2265 result = INVALID_OPERATION;
2266 } else {
2267 // allocate shared memory large enough to hold the
2268 // screen capture
2269 sp<MemoryHeapBase> base(
2270 new MemoryHeapBase(size, 0, "screen-capture") );
2271 void* const ptr = base->getBase();
2272 if (ptr) {
2273 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002274 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002275 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2276 if (glGetError() == GL_NO_ERROR) {
2277 *heap = base;
2278 *w = sw;
2279 *h = sh;
2280 *f = PIXEL_FORMAT_RGBA_8888;
2281 result = NO_ERROR;
2282 }
2283 } else {
2284 result = NO_MEMORY;
2285 }
2286 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287 glViewport(0, 0, hw_w, hw_h);
2288 glMatrixMode(GL_PROJECTION);
2289 glPopMatrix();
2290 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291 } else {
2292 result = BAD_VALUE;
2293 }
2294
2295 // release FBO resources
2296 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2297 glDeleteRenderbuffersOES(1, &tname);
2298 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002299
Mathias Agopian42977342012-08-05 00:40:46 -07002300 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002301
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002302// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002303
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 return result;
2305}
2306
2307
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002308status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2309 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002310 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002311 uint32_t sw, uint32_t sh,
2312 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002313{
2314 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002315 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002316 return BAD_VALUE;
2317
2318 if (!GLExtensions::getInstance().haveFramebufferObject())
2319 return INVALID_OPERATION;
2320
2321 class MessageCaptureScreen : public MessageBase {
2322 SurfaceFlinger* flinger;
2323 DisplayID dpy;
2324 sp<IMemoryHeap>* heap;
2325 uint32_t* w;
2326 uint32_t* h;
2327 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 uint32_t sw;
2329 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002330 uint32_t minLayerZ;
2331 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002332 status_t result;
2333 public:
2334 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002336 uint32_t sw, uint32_t sh,
2337 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002338 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002339 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2340 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2341 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002342 {
2343 }
2344 status_t getResult() const {
2345 return result;
2346 }
2347 virtual bool handler() {
2348 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002350 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002351 return true;
2352 }
2353 };
2354
2355 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002356 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002357 status_t res = postMessageSync(msg);
2358 if (res == NO_ERROR) {
2359 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2360 }
2361 return res;
2362}
2363
2364// ---------------------------------------------------------------------------
2365
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002366SurfaceFlinger::LayerVector::LayerVector() {
2367}
2368
2369SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2370 : SortedVector<sp<LayerBase> >(rhs) {
2371}
2372
2373int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2374 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002375{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002376 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002377 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2378 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002379
2380 uint32_t ls = l->currentState().layerStack;
2381 uint32_t rs = r->currentState().layerStack;
2382 if (ls != rs)
2383 return ls - rs;
2384
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002385 uint32_t lz = l->currentState().z;
2386 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002387 if (lz != rz)
2388 return lz - rz;
2389
2390 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002391}
2392
2393// ---------------------------------------------------------------------------
2394
Mathias Agopiane57f2922012-08-09 16:29:12 -07002395SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2396}
2397
2398SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2399 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002400}
2401
2402// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002403
Jamie Gennis9a78c902011-01-12 18:30:40 -08002404GraphicBufferAlloc::GraphicBufferAlloc() {}
2405
2406GraphicBufferAlloc::~GraphicBufferAlloc() {}
2407
2408sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002409 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002410 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2411 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002412 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002413 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002414 if (err == NO_MEMORY) {
2415 GraphicBuffer::dumpAllocationsToSystemLog();
2416 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002417 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002418 "failed (%s), handle=%p",
2419 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002420 return 0;
2421 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002422 return graphicBuffer;
2423}
2424
Jamie Gennis9a78c902011-01-12 18:30:40 -08002425// ---------------------------------------------------------------------------
2426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427}; // namespace android