blob: 7b070bac9914bdbbcb3e4f0c7d47a7753fe87791 [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 Agopian5f20e2d2012-08-10 18:50:38 -070098 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
113
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 ALOGI_IF(mDebugRegion, "showupdates enabled");
115 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800118void SurfaceFlinger::onFirstRef()
119{
120 mEventQueue.init(this);
121
122 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
123
124 // Wait for the main thread to be done with its initialization
125 mReadyToRunBarrier.wait();
126}
127
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::~SurfaceFlinger()
130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void SurfaceFlinger::binderDied(const wp<IBinder>& who)
137{
138 // the window manager died on us. prepare its eulogy.
139
Andy McFadden9b6a3952012-08-24 10:12:22 -0700140 // reset screen orientation
141 Vector<ComposerState> state;
142 Vector<DisplayState> displays;
143 DisplayState d;
144 d.what = DisplayState::eOrientationChanged;
145 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
146 d.orientation = DisplayState::eOrientationDefault;
147 displays.add(d);
148 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800149
150 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700151 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152}
153
Mathias Agopian7e27f052010-05-28 14:22:23 -0700154sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian96f08192010-06-02 23:28:45 -0700156 sp<ISurfaceComposerClient> bclient;
157 sp<Client> client(new Client(this));
158 status_t err = client->initCheck();
159 if (err == NO_ERROR) {
160 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return bclient;
163}
164
Jamie Gennis0bceb842012-08-23 20:19:38 -0700165int32_t SurfaceFlinger::chooseNewDisplayIdLocked() const
166{
167 int32_t id = DisplayDevice::DISPLAY_ID_COUNT - 1;
168 bool available;
169 do {
170 id++;
171 available = true;
172 for (size_t i = 0; i < mCurrentState.displays.size(); i++) {
173 const DisplayDeviceState& dds(mCurrentState.displays.valueAt(i));
174 if (dds.id == id) {
175 available = false;
176 break;
177 }
178 }
179 } while (!available);
180 return id;
181}
182
Mathias Agopiane57f2922012-08-09 16:29:12 -0700183sp<IBinder> SurfaceFlinger::createDisplay()
184{
185 class DisplayToken : public BBinder {
186 sp<SurfaceFlinger> flinger;
187 virtual ~DisplayToken() {
188 // no more references, this display must be terminated
189 Mutex::Autolock _l(flinger->mStateLock);
190 flinger->mCurrentState.displays.removeItem(this);
191 flinger->setTransactionFlags(eDisplayTransactionNeeded);
192 }
193 public:
194 DisplayToken(const sp<SurfaceFlinger>& flinger)
195 : flinger(flinger) {
196 }
197 };
198
199 sp<BBinder> token = new DisplayToken(this);
200
201 Mutex::Autolock _l(mStateLock);
Jamie Gennis0bceb842012-08-23 20:19:38 -0700202 int32_t id = chooseNewDisplayIdLocked();
203 DisplayDeviceState info(id);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700204 mCurrentState.displays.add(token, info);
205
206 return token;
207}
208
209sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
210 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
211 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
212 return NULL;
213 }
214 return mDefaultDisplays[id];
215}
216
Jamie Gennis9a78c902011-01-12 18:30:40 -0800217sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
218{
219 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
220 return gba;
221}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223void SurfaceFlinger::bootFinished()
224{
225 const nsecs_t now = systemTime();
226 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000227 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700228 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700229
230 // wait patiently for the window manager death
231 const String16 name("window");
232 sp<IBinder> window(defaultServiceManager()->getService(name));
233 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700234 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700235 }
236
237 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700238 // formerly we would just kill the process, but we now ask it to exit so it
239 // can choose where to stop the animation.
240 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700243void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
244 class MessageDestroyGLTexture : public MessageBase {
245 GLuint texture;
246 public:
247 MessageDestroyGLTexture(GLuint texture)
248 : texture(texture) {
249 }
250 virtual bool handler() {
251 glDeleteTextures(1, &texture);
252 return true;
253 }
254 };
255 postMessageAsync(new MessageDestroyGLTexture(texture));
256}
257
Mathias Agopiana4912602012-07-12 14:25:33 -0700258status_t SurfaceFlinger::selectConfigForPixelFormat(
259 EGLDisplay dpy,
260 EGLint const* attrs,
261 PixelFormat format,
262 EGLConfig* outConfig)
263{
264 EGLConfig config = NULL;
265 EGLint numConfigs = -1, n=0;
266 eglGetConfigs(dpy, NULL, 0, &numConfigs);
267 EGLConfig* const configs = new EGLConfig[numConfigs];
268 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
269 for (int i=0 ; i<n ; i++) {
270 EGLint nativeVisualId = 0;
271 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
272 if (nativeVisualId>0 && format == nativeVisualId) {
273 *outConfig = configs[i];
274 delete [] configs;
275 return NO_ERROR;
276 }
277 }
278 delete [] configs;
279 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280}
281
Mathias Agopiana4912602012-07-12 14:25:33 -0700282EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
283 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
284 // it is to be used with WIFI displays
285 EGLConfig config;
286 EGLint dummy;
287 status_t err;
288 EGLint attribs[] = {
289 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
290 EGL_RECORDABLE_ANDROID, EGL_TRUE,
291 EGL_NONE
292 };
293 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
294 if (err) {
295 // maybe we failed because of EGL_RECORDABLE_ANDROID
296 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
297 attribs[2] = EGL_NONE;
298 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
299 }
300 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
301 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
302 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
303 }
304 return config;
305}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
Mathias Agopiana4912602012-07-12 14:25:33 -0700307EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
308 // Also create our EGLContext
309 EGLint contextAttributes[] = {
310#ifdef EGL_IMG_context_priority
311#ifdef HAS_CONTEXT_PRIORITY
312#warning "using EGL_IMG_context_priority"
313 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
314#endif
315#endif
316 EGL_NONE, EGL_NONE
317 };
318 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
319 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
320 return ctxt;
321}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
Mathias Agopiana4912602012-07-12 14:25:33 -0700323void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
324 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
325 if (!result) {
326 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
327 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 }
329
Mathias Agopiana4912602012-07-12 14:25:33 -0700330 GLExtensions& extensions(GLExtensions::getInstance());
331 extensions.initWithGLStrings(
332 glGetString(GL_VENDOR),
333 glGetString(GL_RENDERER),
334 glGetString(GL_VERSION),
335 glGetString(GL_EXTENSIONS),
336 eglQueryString(display, EGL_VENDOR),
337 eglQueryString(display, EGL_VERSION),
338 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700339
Mathias Agopiana4912602012-07-12 14:25:33 -0700340 EGLint w, h;
341 eglQuerySurface(display, surface, EGL_WIDTH, &w);
342 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700343
Mathias Agopiana4912602012-07-12 14:25:33 -0700344 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
345 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700346
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700348 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 glShadeModel(GL_FLAT);
351 glDisable(GL_DITHER);
352 glDisable(GL_CULL_FACE);
353
Mathias Agopiana4912602012-07-12 14:25:33 -0700354 struct pack565 {
355 inline uint16_t operator() (int r, int g, int b) const {
356 return (r<<11)|(g<<5)|b;
357 }
358 } pack565;
359
Jamie Gennis9575f602011-10-07 14:51:16 -0700360 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
361 glGenTextures(1, &mProtectedTexName);
362 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
363 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
364 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
365 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
366 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
367 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
368 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369
370 glViewport(0, 0, w, h);
371 glMatrixMode(GL_PROJECTION);
372 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700373 // put the origin in the left-bottom corner
374 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 -0800375
Mathias Agopiana4912602012-07-12 14:25:33 -0700376 // print some debugging info
377 EGLint r,g,b,a;
378 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
379 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
380 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
381 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
382 ALOGI("EGL informations:");
383 ALOGI("vendor : %s", extensions.getEglVendor());
384 ALOGI("version : %s", extensions.getEglVersion());
385 ALOGI("extensions: %s", extensions.getEglExtension());
386 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
387 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
388 ALOGI("OpenGL ES informations:");
389 ALOGI("vendor : %s", extensions.getVendor());
390 ALOGI("renderer : %s", extensions.getRenderer());
391 ALOGI("version : %s", extensions.getVersion());
392 ALOGI("extensions: %s", extensions.getExtension());
393 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
394 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
395}
396
Mathias Agopiana4912602012-07-12 14:25:33 -0700397status_t SurfaceFlinger::readyToRun()
398{
399 ALOGI( "SurfaceFlinger's main thread ready to run. "
400 "Initializing graphics H/W...");
401
Mathias Agopiana4912602012-07-12 14:25:33 -0700402 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700403 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
404 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700405
406 // Initialize the main display
407 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700408 sp<FramebufferSurface> fbs = FramebufferSurface::create();
409 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700410 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
411 exit(0);
412 }
413
Mathias Agopiane57f2922012-08-09 16:29:12 -0700414 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
415 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700416
Mathias Agopiana4912602012-07-12 14:25:33 -0700417 // initialize the config and context
418 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700419 ANativeWindow* const anw = stc.get();
420 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700421 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
422 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700423
424 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700425
426 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
427 mDefaultDisplays[i] = new BBinder();
428 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
429 }
430 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700431 DisplayDevice::DISPLAY_ID_MAIN, HWC_DISPLAY_PRIMARY,
432 anw, fbs, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700433 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700434
435 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700436 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700437 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800438
Mathias Agopian028508c2012-07-25 21:12:12 -0700439 // start the EventThread
440 mEventThread = new EventThread(this);
441 mEventQueue.setEventThread(mEventThread);
442
Mathias Agopian86303202012-07-24 22:46:10 -0700443 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700444 mHwc = new HWComposer(this,
445 *static_cast<HWComposer::EventHandler *>(this),
446 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700447
448 // initialize our drawing state
449 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700450
Mathias Agopiana4912602012-07-12 14:25:33 -0700451 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800452 mReadyToRunBarrier.open();
453
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700454 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700455 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 return NO_ERROR;
458}
459
Mathias Agopiana67e4182012-06-19 17:26:12 -0700460void SurfaceFlinger::startBootAnim() {
461 // start boot animation
462 property_set("service.bootanim.exit", "0");
463 property_set("ctl.start", "bootanim");
464}
465
Mathias Agopiana4912602012-07-12 14:25:33 -0700466uint32_t SurfaceFlinger::getMaxTextureSize() const {
467 return mMaxTextureSize;
468}
469
470uint32_t SurfaceFlinger::getMaxViewportDims() const {
471 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
472 mMaxViewportDims[0] : mMaxViewportDims[1];
473}
474
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800476
Jamie Gennis582270d2011-08-17 18:19:00 -0700477bool SurfaceFlinger::authenticateSurfaceTexture(
478 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800479 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700480 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800481
482 // Check the visible layer list for the ISurface
483 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
484 size_t count = currentLayers.size();
485 for (size_t i=0 ; i<count ; i++) {
486 const sp<LayerBase>& layer(currentLayers[i]);
487 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700488 if (lbc != NULL) {
489 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
490 if (lbcBinder == surfaceTextureBinder) {
491 return true;
492 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800493 }
494 }
495
496 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700497 // SurfaceTexture gets destroyed before all the clients are done using it,
498 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800499 // will instead fail later on when the client tries to use the surface,
500 // which should be reported as "surface XYZ returned an -ENODEV". The
501 // purgatorized layers are no less authentic than the visible ones, so this
502 // should not cause any harm.
503 size_t purgatorySize = mLayerPurgatory.size();
504 for (size_t i=0 ; i<purgatorySize ; i++) {
505 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
506 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700507 if (lbc != NULL) {
508 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
509 if (lbcBinder == surfaceTextureBinder) {
510 return true;
511 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800512 }
513 }
514
515 return false;
516}
517
Mathias Agopianc666cae2012-07-25 18:56:13 -0700518status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700519 // TODO: this is here only for compatibility -- should go away eventually.
520 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700521 return BAD_INDEX;
522 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700523
524 const HWComposer& hwc(getHwComposer());
525 float xdpi = hwc.getDpiX();
526 float ydpi = hwc.getDpiY();
527
528 // TODO: Not sure if display density should handled by SF any longer
529 class Density {
530 static int getDensityFromProperty(char const* propName) {
531 char property[PROPERTY_VALUE_MAX];
532 int density = 0;
533 if (property_get(propName, property, NULL) > 0) {
534 density = atoi(property);
535 }
536 return density;
537 }
538 public:
539 static int getEmuDensity() {
540 return getDensityFromProperty("qemu.sf.lcd_density"); }
541 static int getBuildDensity() {
542 return getDensityFromProperty("ro.sf.lcd_density"); }
543 };
544 // The density of the device is provided by a build property
545 float density = Density::getBuildDensity() / 160.0f;
546 if (density == 0) {
547 // the build doesn't provide a density -- this is wrong!
548 // use xdpi instead
549 ALOGE("ro.sf.lcd_density must be defined as a build property");
550 density = xdpi / 160.0f;
551 }
552 if (Density::getEmuDensity()) {
553 // if "qemu.sf.lcd_density" is specified, it overrides everything
554 xdpi = ydpi = density = Density::getEmuDensity();
555 density /= 160.0f;
556 }
557
Mathias Agopian42977342012-08-05 00:40:46 -0700558 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
559 info->w = hw->getWidth();
560 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700561 info->xdpi = xdpi;
562 info->ydpi = ydpi;
563 info->fps = float(1e9 / hwc.getRefreshPeriod());
564 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700565 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700566 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700567 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700568 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700569}
570
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800571// ----------------------------------------------------------------------------
572
573sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800574 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700575}
576
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700577void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700578
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700579 sp<IBinder> token;
580 { // scope for the lock
581 Mutex::Autolock _l(mStateLock);
582 token = mExtDisplayToken;
583 }
584
585 if (token == 0) {
586 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700587 }
588
589 { // scope for the lock
590 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700591 if (surface == 0) {
592 // release our current display. we're guarantee to have
593 // a reference to it (token), while we hold the lock
594 mExtDisplayToken = 0;
595 } else {
596 mExtDisplayToken = token;
597 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700598
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700599 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
600 info.surface = surface;
601 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700602 }
603}
604
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800606
607void SurfaceFlinger::waitForEvent() {
608 mEventQueue.waitMessage();
609}
610
611void SurfaceFlinger::signalTransaction() {
612 mEventQueue.invalidate();
613}
614
615void SurfaceFlinger::signalLayerUpdate() {
616 mEventQueue.invalidate();
617}
618
619void SurfaceFlinger::signalRefresh() {
620 mEventQueue.refresh();
621}
622
623status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
624 nsecs_t reltime, uint32_t flags) {
625 return mEventQueue.postMessage(msg, reltime);
626}
627
628status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
629 nsecs_t reltime, uint32_t flags) {
630 status_t res = mEventQueue.postMessage(msg, reltime);
631 if (res == NO_ERROR) {
632 msg->wait();
633 }
634 return res;
635}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636
Mathias Agopian4fec8732012-06-29 14:12:52 -0700637bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 return true;
640}
641
Mathias Agopian86303202012-07-24 22:46:10 -0700642void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700643 mEventThread->onVSyncReceived(dpy, timestamp);
644}
645
646void SurfaceFlinger::eventControl(int event, int enabled) {
647 getHwComposer().eventControl(event, enabled);
648}
649
Mathias Agopian4fec8732012-06-29 14:12:52 -0700650void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800651 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800652 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700653 case MessageQueue::INVALIDATE:
654 handleMessageTransaction();
655 handleMessageInvalidate();
656 signalRefresh();
657 break;
658 case MessageQueue::REFRESH:
659 handleMessageRefresh();
660 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800661 }
662}
663
Mathias Agopian4fec8732012-06-29 14:12:52 -0700664void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700665 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700666 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700667 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700668 }
669}
670
671void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700672 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700673 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700674}
675
676void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700677 ATRACE_CALL();
678 preComposition();
679 rebuildLayerStacks();
680 setUpHWComposer();
681 doDebugFlashRegions();
682 doComposition();
683 postComposition();
684}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700685
Mathias Agopiancd60f992012-08-16 16:28:27 -0700686void SurfaceFlinger::doDebugFlashRegions()
687{
688 // is debugging enabled
689 if (CC_LIKELY(!mDebugRegion))
690 return;
691
692 const bool repaintEverything = mRepaintEverything;
693 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
694 const sp<DisplayDevice>& hw(mDisplays[dpy]);
695 if (hw->canDraw()) {
696 // transform the dirty region into this screen's coordinate space
697 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
698 if (!dirtyRegion.isEmpty()) {
699 // redraw the whole screen
700 doComposeSurfaces(hw, Region(hw->bounds()));
701
702 // and draw the dirty region
703 glDisable(GL_TEXTURE_EXTERNAL_OES);
704 glDisable(GL_TEXTURE_2D);
705 glDisable(GL_BLEND);
706 glColor4f(1, 0, 1, 1);
707 const int32_t height = hw->getHeight();
708 Region::const_iterator it = dirtyRegion.begin();
709 Region::const_iterator const end = dirtyRegion.end();
710 while (it != end) {
711 const Rect& r = *it++;
712 GLfloat vertices[][2] = {
713 { r.left, height - r.top },
714 { r.left, height - r.bottom },
715 { r.right, height - r.bottom },
716 { r.right, height - r.top }
717 };
718 glVertexPointer(2, GL_FLOAT, 0, vertices);
719 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
720 }
721 hw->compositionComplete();
722 // FIXME
723 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
724 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
725 }
726 }
727 }
728 }
729
730 postFramebuffer();
731
732 if (mDebugRegion > 1) {
733 usleep(mDebugRegion * 1000);
734 }
735}
736
737void SurfaceFlinger::preComposition()
738{
739 bool needExtraInvalidate = false;
740 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
741 const size_t count = currentLayers.size();
742 for (size_t i=0 ; i<count ; i++) {
743 if (currentLayers[i]->onPreComposition()) {
744 needExtraInvalidate = true;
745 }
746 }
747 if (needExtraInvalidate) {
748 signalLayerUpdate();
749 }
750}
751
752void SurfaceFlinger::postComposition()
753{
754 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
755 const size_t count = currentLayers.size();
756 for (size_t i=0 ; i<count ; i++) {
757 currentLayers[i]->onPostComposition();
758 }
759}
760
761void SurfaceFlinger::rebuildLayerStacks() {
762 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700763 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700765 mVisibleRegionsDirty = false;
766 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700767 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700768 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700769 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700770 Region opaqueRegion;
771 Region dirtyRegion;
772 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700773 hw->getLayerStack(), dirtyRegion, opaqueRegion);
774 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700775
776 Vector< sp<LayerBase> > layersSortedByZ;
777 const size_t count = currentLayers.size();
778 for (size_t i=0 ; i<count ; i++) {
779 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700780 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700781 if (!currentLayers[i]->visibleRegion.isEmpty()) {
782 layersSortedByZ.add(currentLayers[i]);
783 }
784 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700785 }
Mathias Agopian42977342012-08-05 00:40:46 -0700786 hw->setVisibleLayersSortedByZ(layersSortedByZ);
787 hw->undefinedRegion.set(hw->getBounds());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700788 hw->undefinedRegion.subtractSelf(
789 hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700790 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700791 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700793
Mathias Agopiancd60f992012-08-16 16:28:27 -0700794void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700795 HWComposer& hwc(getHwComposer());
796 if (hwc.initCheck() == NO_ERROR) {
797 // build the h/w work list
798 const bool workListsDirty = mHwWorkListDirty;
799 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700800 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700801 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700802 const int32_t id = hw->getHwcDisplayId();
803 if (id >= 0) {
804 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700806 const size_t count = currentLayers.size();
807 if (hwc.createWorkList(id, count) >= 0) {
808 HWComposer::LayerListIterator cur = hwc.begin(id);
809 const HWComposer::LayerListIterator end = hwc.end(id);
810 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
811 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700812
Mathias Agopiane60b0682012-08-21 23:34:09 -0700813 if (CC_UNLIKELY(workListsDirty)) {
814 layer->setGeometry(hw, *cur);
815 if (mDebugDisableHWC || mDebugRegion) {
816 cur->setSkip(true);
817 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700818 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700819
Mathias Agopiane60b0682012-08-21 23:34:09 -0700820 /*
821 * update the per-frame h/w composer data for each layer
822 * and build the transparent region of the FB
823 */
824 layer->setPerFrameData(hw, *cur);
825 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700826 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700827 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700828 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700829 status_t err = hwc.prepare();
830 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
831 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700832}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700833
Mathias Agopiancd60f992012-08-16 16:28:27 -0700834void SurfaceFlinger::doComposition() {
835 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700836 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700837 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700838 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700839 if (hw->canDraw()) {
840 // transform the dirty region into this screen's coordinate space
841 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
842 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700843 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700845 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700846 hw->dirtyRegion.clear();
847 hw->flip(hw->swapRegion);
848 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700849 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700850 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700851 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700852 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700853 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700854}
855
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856void SurfaceFlinger::postFramebuffer()
857{
Mathias Agopian841cde52012-03-01 15:44:37 -0800858 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800859
Mathias Agopiana44b0412011-10-16 18:46:35 -0700860 const nsecs_t now = systemTime();
861 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700862
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700863 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700864 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700865 // FIXME: EGL spec says:
866 // "surface must be bound to the calling thread's current context,
867 // for the current rendering API."
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868 DisplayDevice::makeCurrent(
869 getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700870 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700871 }
872
Mathias Agopian92a979a2012-08-02 18:32:23 -0700873 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700874 sp<const DisplayDevice> hw(mDisplays[dpy]);
875 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700876 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700877 int32_t id = hw->getHwcDisplayId();
878 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700879 HWComposer::LayerListIterator cur = hwc.begin(id);
880 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700881 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700882 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700883 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700884 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700885 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700886 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700887 }
Jesse Hallef194142012-06-14 14:45:17 -0700888 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800889 }
890
Mathias Agopiana44b0412011-10-16 18:46:35 -0700891 mLastSwapBufferTime = systemTime() - now;
892 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893}
894
Mathias Agopian87baae12012-07-31 12:38:26 -0700895void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896{
Mathias Agopian841cde52012-03-01 15:44:37 -0800897 ATRACE_CALL();
898
Mathias Agopianca4d3602011-05-19 15:38:14 -0700899 Mutex::Autolock _l(mStateLock);
900 const nsecs_t now = systemTime();
901 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902
Mathias Agopianca4d3602011-05-19 15:38:14 -0700903 // Here we're guaranteed that some transaction flags are set
904 // so we can call handleTransactionLocked() unconditionally.
905 // We call getTransactionFlags(), which will also clear the flags,
906 // with mStateLock held to guarantee that mCurrentState won't change
907 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700908
Mathias Agopiane57f2922012-08-09 16:29:12 -0700909 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700910 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700911
Mathias Agopianca4d3602011-05-19 15:38:14 -0700912 mLastTransactionTime = systemTime() - now;
913 mDebugInTransaction = 0;
914 invalidateHwcGeometry();
915 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700916}
917
Mathias Agopian87baae12012-07-31 12:38:26 -0700918void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700919{
920 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 const size_t count = currentLayers.size();
922
923 /*
924 * Traversal of the children
925 * (perform the transaction for each of them if needed)
926 */
927
Mathias Agopian3559b072012-08-15 13:46:03 -0700928 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700930 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
932 if (!trFlags) continue;
933
934 const uint32_t flags = layer->doTransaction(0);
935 if (flags & Layer::eVisibleRegion)
936 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 }
938 }
939
940 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700941 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 */
943
Mathias Agopiane57f2922012-08-09 16:29:12 -0700944 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700945 // here we take advantage of Vector's copy-on-write semantics to
946 // improve performance by skipping the transaction entirely when
947 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700948 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
949 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700950 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700952 const size_t cc = curr.size();
953 const size_t dc = draw.size();
954
955 // find the displays that were removed
956 // (ie: in drawing state but not in current state)
957 // also handle displays that changed
958 // (ie: displays that are in both lists)
959 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700960 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
961 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700962 // in drawing state but not in current state
963 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700964 mDisplays.removeItem(draw[i].id);
965 } else {
966 ALOGW("trying to remove the main display");
967 }
968 } else {
969 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700971 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700972 // changing the surface is like destroying and
973 // recreating the DisplayDevice
Mathias Agopiane60b0682012-08-21 23:34:09 -0700974 const int32_t hwcDisplayId =
975 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
976 state.id : getHwComposer().allocateDisplayId();
Mathias Agopiane57f2922012-08-09 16:29:12 -0700977
978 sp<SurfaceTextureClient> stc(
979 new SurfaceTextureClient(state.surface));
980
981 sp<DisplayDevice> disp = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700982 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700983
984 disp->setLayerStack(state.layerStack);
985 disp->setOrientation(state.orientation);
986 // TODO: take viewport and frame into account
987 mDisplays.replaceValueFor(state.id, disp);
988 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700989 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700990 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700991 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700992 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700993 if (state.orientation != draw[i].orientation ||
994 state.viewport != draw[i].viewport ||
995 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700996 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
997 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700998 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700999 }
1000 }
1001 }
1002
1003 // find displays that were added
1004 // (ie: in current state but not in drawing state)
1005 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001006 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001007 const DisplayDeviceState& state(curr[i]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001008 const int32_t hwcDisplayId =
1009 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
1010 state.id : getHwComposer().allocateDisplayId();
1011
Mathias Agopiane57f2922012-08-09 16:29:12 -07001012 sp<SurfaceTextureClient> stc(
1013 new SurfaceTextureClient(state.surface));
Mathias Agopiane60b0682012-08-21 23:34:09 -07001014 sp<DisplayDevice> disp = new DisplayDevice(this,
1015 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -07001016 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001017 }
1018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001020 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021
Mathias Agopian3559b072012-08-15 13:46:03 -07001022 /*
1023 * Perform our own transaction if needed
1024 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001025
Mathias Agopiancd60f992012-08-16 16:28:27 -07001026 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1027 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001028 // layers have been added
1029 mVisibleRegionsDirty = true;
1030 }
1031
1032 // some layers might have been removed, so
1033 // we need to update the regions they're exposing.
1034 if (mLayersRemoved) {
1035 mLayersRemoved = false;
1036 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001037 const size_t count = previousLayers.size();
1038 for (size_t i=0 ; i<count ; i++) {
1039 const sp<LayerBase>& layer(previousLayers[i]);
1040 if (currentLayers.indexOf(layer) < 0) {
1041 // this layer is not visible anymore
1042 // TODO: we could traverse the tree from front to back and
1043 // compute the actual visible region
1044 // TODO: we could cache the transformed region
1045 Layer::State front(layer->drawingState());
1046 Region visibleReg = front.transform.transform(
1047 Region(Rect(front.active.w, front.active.h)));
1048 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001049 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 }
1052
1053 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001054}
1055
1056void SurfaceFlinger::commitTransaction()
1057{
1058 if (!mLayersPendingRemoval.isEmpty()) {
1059 // Notify removed layers now that they can't be drawn from
1060 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1061 mLayersPendingRemoval[i]->onRemoved();
1062 }
1063 mLayersPendingRemoval.clear();
1064 }
1065
1066 mDrawingState = mCurrentState;
1067 mTransationPending = false;
1068 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001072 const LayerVector& currentLayers, uint32_t layerStack,
1073 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074{
Mathias Agopian841cde52012-03-01 15:44:37 -08001075 ATRACE_CALL();
1076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 Region aboveOpaqueLayers;
1078 Region aboveCoveredLayers;
1079 Region dirty;
1080
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082
1083 size_t i = currentLayers.size();
1084 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001085 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086
1087 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001088 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089
Mathias Agopian87baae12012-07-31 12:38:26 -07001090 // only consider the layers on the given later stack
1091 if (s.layerStack != layerStack)
1092 continue;
1093
Mathias Agopianab028732010-03-16 16:41:46 -07001094 /*
1095 * opaqueRegion: area of a surface that is fully opaque.
1096 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001098
1099 /*
1100 * visibleRegion: area of a surface that is visible on screen
1101 * and not fully transparent. This is essentially the layer's
1102 * footprint minus the opaque regions above it.
1103 * Areas covered by a translucent surface are considered visible.
1104 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001106
1107 /*
1108 * coveredRegion: area of a surface that is covered by all
1109 * visible regions above it (which includes the translucent areas).
1110 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001112
1113
1114 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001115 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001116 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001117 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001119 if (!visibleRegion.isEmpty()) {
1120 // Remove the transparent area from the visible region
1121 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001122 Region transparentRegionScreen;
1123 const Transform tr(s.transform);
1124 if (tr.transformed()) {
1125 if (tr.preserveRects()) {
1126 // transform the transparent region
1127 transparentRegionScreen = tr.transform(s.transparentRegion);
1128 } else {
1129 // transformation too complex, can't do the
1130 // transparent region optimization.
1131 transparentRegionScreen.clear();
1132 }
1133 } else {
1134 transparentRegionScreen = s.transparentRegion;
1135 }
1136 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
Mathias Agopianab028732010-03-16 16:41:46 -07001139 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001140 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001141 if (s.alpha==255 && !translucent &&
1142 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1143 // the opaque region is the layer's footprint
1144 opaqueRegion = visibleRegion;
1145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 }
1147 }
1148
Mathias Agopianab028732010-03-16 16:41:46 -07001149 // Clip the covered region to the visible region
1150 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1151
1152 // Update aboveCoveredLayers for next (lower) layer
1153 aboveCoveredLayers.orSelf(visibleRegion);
1154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155 // subtract the opaque region covered by the layers above us
1156 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157
1158 // compute this layer's dirty region
1159 if (layer->contentDirty) {
1160 // we need to invalidate the whole region
1161 dirty = visibleRegion;
1162 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001163 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 layer->contentDirty = false;
1165 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001166 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001167 * the exposed region consists of two components:
1168 * 1) what's VISIBLE now and was COVERED before
1169 * 2) what's EXPOSED now less what was EXPOSED before
1170 *
1171 * note that (1) is conservative, we start with the whole
1172 * visible region but only keep what used to be covered by
1173 * something -- which mean it may have been exposed.
1174 *
1175 * (2) handles areas that were not covered by anything but got
1176 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001177 */
Mathias Agopianab028732010-03-16 16:41:46 -07001178 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001179 const Region oldVisibleRegion = layer->visibleRegion;
1180 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001181 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1182 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 }
1184 dirty.subtractSelf(aboveOpaqueLayers);
1185
1186 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001187 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188
Mathias Agopianab028732010-03-16 16:41:46 -07001189 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 // Store the visible region is screen space
1193 layer->setVisibleRegion(visibleRegion);
1194 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 }
1196
Mathias Agopian87baae12012-07-31 12:38:26 -07001197 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198}
1199
Mathias Agopian87baae12012-07-31 12:38:26 -07001200void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1201 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001202 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001203 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1204 if (hw->getLayerStack() == layerStack) {
1205 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001206 }
1207 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001208}
1209
1210void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001212 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213
Mathias Agopian4fec8732012-06-29 14:12:52 -07001214 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001215 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001216 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001217 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001218 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001219 const Region dirty(layer->latchBuffer(visibleRegions));
1220 Layer::State s(layer->drawingState());
1221 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001222 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001223
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001224 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225}
1226
Mathias Agopianad456f92011-01-13 17:53:01 -08001227void SurfaceFlinger::invalidateHwcGeometry()
1228{
1229 mHwWorkListDirty = true;
1230}
1231
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001232
Mathias Agopiancd60f992012-08-16 16:28:27 -07001233void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001234 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235{
Mathias Agopian87baae12012-07-31 12:38:26 -07001236 Region dirtyRegion(inDirtyRegion);
1237
Mathias Agopianb8a55602009-06-26 19:06:36 -07001238 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001239 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240
Mathias Agopian42977342012-08-05 00:40:46 -07001241 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001242 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001243 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1244 // takes a rectangle, we must make sure to update that whole
1245 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001246 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001247 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001248 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001249 // We need to redraw the rectangle that will be updated
1250 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001251 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001252 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001253 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001254 } else {
1255 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001256 dirtyRegion.set(hw->bounds());
1257 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 }
1259 }
1260
Mathias Agopiancd60f992012-08-16 16:28:27 -07001261 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262
Mathias Agopiancd60f992012-08-16 16:28:27 -07001263 // FIXME: we need to call eglSwapBuffers() on displays that have
1264 // GL composition and only on those.
1265 // however, currently hwc.commit() already does that for the main
1266 // display and never for the other ones
1267 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
1268 // FIXME: EGL spec says:
1269 // "surface must be bound to the calling thread's current context,
1270 // for the current rendering API."
1271 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001272 }
1273
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001274 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001275 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276}
1277
Mathias Agopiancd60f992012-08-16 16:28:27 -07001278void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001279{
Mathias Agopian86303202012-07-24 22:46:10 -07001280 HWComposer& hwc(getHwComposer());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001281 int32_t id = hw->getHwcDisplayId();
Mathias Agopian1e260872012-08-08 18:35:12 -07001282 HWComposer::LayerListIterator cur = hwc.begin(id);
1283 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001284
Mathias Agopiane60b0682012-08-21 23:34:09 -07001285 const bool hasGlesComposition = hwc.hasGlesComposition(id);
1286 const bool hasHwcComposition = hwc.hasHwcComposition(id);
1287 if (cur==end || hasGlesComposition) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001288
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001289 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001290
1291 // set the frame buffer
1292 glMatrixMode(GL_MODELVIEW);
1293 glLoadIdentity();
1294
1295 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07001296 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001297 // when using overlays, we assume a fully transparent framebuffer
1298 // NOTE: we could reduce how much we need to clear, for instance
1299 // remove where there are opaque FB layers. however, on some
1300 // GPUs doing a "clean slate" glClear might be more efficient.
1301 // We'll revisit later if needed.
1302 glClearColor(0, 0, 0, 0);
1303 glClear(GL_COLOR_BUFFER_BIT);
1304 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001305 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001306 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001307 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001308 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001309 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001310 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001311 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001312
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001313 /*
1314 * and then, render the layers targeted at the framebuffer
1315 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001316
Mathias Agopian42977342012-08-05 00:40:46 -07001317 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001318 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001319 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001320 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001321 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Jesse Halla6b32db2012-07-19 16:44:38 -07001323 if (cur != end) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001324 // we're using h/w composer
1325 if (!clip.isEmpty()) {
1326 if (cur->getCompositionType() == HWC_OVERLAY) {
1327 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
1328 && layer->isOpaque()) {
1329 // never clear the very first layer since we're
1330 // guaranteed the FB is already cleared
1331 layer->clearWithOpenGL(hw, clip);
1332 }
1333 } else {
1334 layer->draw(hw, clip);
1335 }
1336 layer->setAcquireFence(hw, *cur);
1337 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001338 ++cur;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001339 } else {
1340 // we're not using h/w composer
1341 if (!clip.isEmpty()) {
1342 layer->draw(hw, clip);
1343 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001344 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001345 }
1346 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347}
1348
Mathias Agopian87baae12012-07-31 12:38:26 -07001349void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001351 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001352 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001353 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001354 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001355
1356 GLfloat vertices[4][2];
1357 glVertexPointer(2, GL_FLOAT, 0, vertices);
1358 Region::const_iterator it = region.begin();
1359 Region::const_iterator const end = region.end();
1360 while (it != end) {
1361 const Rect& r = *it++;
1362 vertices[0][0] = r.left;
1363 vertices[0][1] = r.top;
1364 vertices[1][0] = r.right;
1365 vertices[1][1] = r.top;
1366 vertices[2][0] = r.right;
1367 vertices[2][1] = r.bottom;
1368 vertices[3][0] = r.left;
1369 vertices[3][1] = r.bottom;
1370 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1371 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372}
1373
Mathias Agopian96f08192010-06-02 23:28:45 -07001374ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1375 const sp<LayerBaseClient>& lbc)
1376{
Mathias Agopian96f08192010-06-02 23:28:45 -07001377 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001378 size_t name = client->attachLayer(lbc);
1379
Mathias Agopian96f08192010-06-02 23:28:45 -07001380 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001381 Mutex::Autolock _l(mStateLock);
1382 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001383
Mathias Agopian4f113742011-05-03 16:21:41 -07001384 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001385}
1386
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001387status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388{
1389 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001390 status_t err = purgatorizeLayer_l(layer);
1391 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001392 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001393 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394}
1395
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001396status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397{
1398 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1399 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001400 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 return NO_ERROR;
1402 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001403 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404}
1405
Mathias Agopian9a112062009-04-17 19:36:26 -07001406status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1407{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001408 // First add the layer to the purgatory list, which makes sure it won't
1409 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001410 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001411 if (err >= 0) {
1412 mLayerPurgatory.add(layerBase);
1413 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001414
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001415 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001416
Mathias Agopian3d579642009-06-04 18:46:21 -07001417 // it's possible that we don't find a layer, because it might
1418 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001419 // from the user because there is a race between Client::destroySurface(),
1420 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001421 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1422}
1423
Mathias Agopiandea20b12011-05-03 17:04:02 -07001424uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1425{
1426 return android_atomic_release_load(&mTransactionFlags);
1427}
1428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1430{
1431 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1432}
1433
Mathias Agopianbb641242010-05-18 17:06:55 -07001434uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435{
1436 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1437 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001438 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439 }
1440 return old;
1441}
1442
Mathias Agopian8b33f032012-07-24 20:43:54 -07001443void SurfaceFlinger::setTransactionState(
1444 const Vector<ComposerState>& state,
1445 const Vector<DisplayState>& displays,
1446 uint32_t flags)
1447{
Mathias Agopian698c0872011-06-28 19:09:31 -07001448 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001449 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001450
1451 size_t count = displays.size();
1452 for (size_t i=0 ; i<count ; i++) {
1453 const DisplayState& s(displays[i]);
1454 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001455 }
1456
Mathias Agopiane57f2922012-08-09 16:29:12 -07001457 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001458 for (size_t i=0 ; i<count ; i++) {
1459 const ComposerState& s(state[i]);
1460 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001461 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001462 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001463
Mathias Agopian386aa982011-11-07 21:58:03 -08001464 if (transactionFlags) {
1465 // this triggers the transaction
1466 setTransactionFlags(transactionFlags);
1467
1468 // if this is a synchronous transaction, wait for it to take effect
1469 // before returning.
1470 if (flags & eSynchronous) {
1471 mTransationPending = true;
1472 }
1473 while (mTransationPending) {
1474 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1475 if (CC_UNLIKELY(err != NO_ERROR)) {
1476 // just in case something goes wrong in SF, return to the
1477 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001478 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001479 mTransationPending = false;
1480 break;
1481 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 }
1484}
1485
Mathias Agopiane57f2922012-08-09 16:29:12 -07001486uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1487{
1488 uint32_t flags = 0;
1489 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1490 if (disp.id >= 0) {
1491 const uint32_t what = s.what;
1492 if (what & DisplayState::eSurfaceChanged) {
1493 if (disp.surface->asBinder() != s.surface->asBinder()) {
1494 disp.surface = s.surface;
1495 flags |= eDisplayTransactionNeeded;
1496 }
1497 }
1498 if (what & DisplayState::eLayerStackChanged) {
1499 if (disp.layerStack != s.layerStack) {
1500 disp.layerStack = s.layerStack;
1501 flags |= eDisplayTransactionNeeded;
1502 }
1503 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001504 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001505 if (disp.orientation != s.orientation) {
1506 disp.orientation = s.orientation;
1507 flags |= eDisplayTransactionNeeded;
1508 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001509 }
1510 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001511 if (disp.frame != s.frame) {
1512 disp.frame = s.frame;
1513 flags |= eDisplayTransactionNeeded;
1514 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001515 }
1516 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001517 if (disp.viewport != s.viewport) {
1518 disp.viewport = s.viewport;
1519 flags |= eDisplayTransactionNeeded;
1520 }
1521 }
1522 }
1523 return flags;
1524}
1525
1526uint32_t SurfaceFlinger::setClientStateLocked(
1527 const sp<Client>& client,
1528 const layer_state_t& s)
1529{
1530 uint32_t flags = 0;
1531 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1532 if (layer != 0) {
1533 const uint32_t what = s.what;
1534 if (what & layer_state_t::ePositionChanged) {
1535 if (layer->setPosition(s.x, s.y))
1536 flags |= eTraversalNeeded;
1537 }
1538 if (what & layer_state_t::eLayerChanged) {
1539 // NOTE: index needs to be calculated before we update the state
1540 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1541 if (layer->setLayer(s.z)) {
1542 mCurrentState.layersSortedByZ.removeAt(idx);
1543 mCurrentState.layersSortedByZ.add(layer);
1544 // we need traversal (state changed)
1545 // AND transaction (list changed)
1546 flags |= eTransactionNeeded|eTraversalNeeded;
1547 }
1548 }
1549 if (what & layer_state_t::eSizeChanged) {
1550 if (layer->setSize(s.w, s.h)) {
1551 flags |= eTraversalNeeded;
1552 }
1553 }
1554 if (what & layer_state_t::eAlphaChanged) {
1555 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1556 flags |= eTraversalNeeded;
1557 }
1558 if (what & layer_state_t::eMatrixChanged) {
1559 if (layer->setMatrix(s.matrix))
1560 flags |= eTraversalNeeded;
1561 }
1562 if (what & layer_state_t::eTransparentRegionChanged) {
1563 if (layer->setTransparentRegionHint(s.transparentRegion))
1564 flags |= eTraversalNeeded;
1565 }
1566 if (what & layer_state_t::eVisibilityChanged) {
1567 if (layer->setFlags(s.flags, s.mask))
1568 flags |= eTraversalNeeded;
1569 }
1570 if (what & layer_state_t::eCropChanged) {
1571 if (layer->setCrop(s.crop))
1572 flags |= eTraversalNeeded;
1573 }
1574 if (what & layer_state_t::eLayerStackChanged) {
1575 // NOTE: index needs to be calculated before we update the state
1576 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1577 if (layer->setLayerStack(s.layerStack)) {
1578 mCurrentState.layersSortedByZ.removeAt(idx);
1579 mCurrentState.layersSortedByZ.add(layer);
1580 // we need traversal (state changed)
1581 // AND transaction (list changed)
1582 flags |= eTransactionNeeded|eTraversalNeeded;
1583 }
1584 }
1585 }
1586 return flags;
1587}
1588
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001589sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001590 ISurfaceComposerClient::surface_data_t* params,
1591 const String8& name,
1592 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1594 uint32_t flags)
1595{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001596 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001597 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001598
1599 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001600 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001601 int(w), int(h));
1602 return surfaceHandle;
1603 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001604
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001605 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001606 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1607 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001608 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001610 case ISurfaceComposerClient::eFXSurfaceBlur:
1611 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001612 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001614 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001615 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001616 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 }
1618
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001619 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001620 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001621 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001622 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001624 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001625 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001626 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001627 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001628 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629 }
1630
1631 return surfaceHandle;
1632}
1633
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001634sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001635 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001636 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001637 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638{
1639 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001640 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 case PIXEL_FORMAT_TRANSPARENT:
1642 case PIXEL_FORMAT_TRANSLUCENT:
1643 format = PIXEL_FORMAT_RGBA_8888;
1644 break;
1645 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001646#ifdef NO_RGBX_8888
1647 format = PIXEL_FORMAT_RGB_565;
1648#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001649 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001650#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 break;
1652 }
1653
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001654#ifdef NO_RGBX_8888
1655 if (format == PIXEL_FORMAT_RGBX_8888)
1656 format = PIXEL_FORMAT_RGBA_8888;
1657#endif
1658
Mathias Agopian96f08192010-06-02 23:28:45 -07001659 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001660 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001661 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001662 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001663 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 }
1665 return layer;
1666}
1667
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001668sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001669 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001670 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671{
Mathias Agopian96f08192010-06-02 23:28:45 -07001672 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001673 return layer;
1674}
1675
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001676sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001677 const sp<Client>& client, DisplayID display,
1678 uint32_t w, uint32_t h, uint32_t flags)
1679{
1680 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 return layer;
1682}
1683
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001684status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685{
Mathias Agopian9a112062009-04-17 19:36:26 -07001686 /*
1687 * called by the window manager, when a surface should be marked for
1688 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001689 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001690 * The surface is removed from the current and drawing lists, but placed
1691 * in the purgatory queue, so it's not destroyed right-away (we need
1692 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001693 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001694
Mathias Agopian48d819a2009-09-10 19:41:18 -07001695 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001696 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001697 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001698
Mathias Agopian48d819a2009-09-10 19:41:18 -07001699 if (layer != 0) {
1700 err = purgatorizeLayer_l(layer);
1701 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001702 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001703 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001704 }
1705 return err;
1706}
1707
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001708status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001709{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001710 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001711 status_t err = NO_ERROR;
1712 sp<LayerBaseClient> l(layer.promote());
1713 if (l != NULL) {
1714 Mutex::Autolock _l(mStateLock);
1715 err = removeLayer_l(l);
1716 if (err == NAME_NOT_FOUND) {
1717 // The surface wasn't in the current list, which means it was
1718 // removed already, which means it is in the purgatory,
1719 // and need to be removed from there.
1720 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001721 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001722 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001723 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001724 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001725 "error removing layer=%p (%s)", l.get(), strerror(-err));
1726 }
1727 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728}
1729
Mathias Agopianb60314a2012-04-10 22:09:54 -07001730// ---------------------------------------------------------------------------
1731
1732void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001733 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001734 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001735 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001736 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001737 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001738 mVisibleRegionsDirty = true;
1739 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740}
1741
Mathias Agopianb60314a2012-04-10 22:09:54 -07001742void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001743 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001744 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1745 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001746 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001747 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001748 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001749 // from this point on, SF will stop drawing
1750 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751}
1752
Colin Cross8e533062012-06-07 13:17:52 -07001753void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001754 class MessageScreenAcquired : public MessageBase {
1755 SurfaceFlinger* flinger;
1756 public:
1757 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1758 virtual bool handler() {
1759 flinger->onScreenAcquired();
1760 return true;
1761 }
1762 };
1763 sp<MessageBase> msg = new MessageScreenAcquired(this);
1764 postMessageSync(msg);
1765}
1766
Colin Cross8e533062012-06-07 13:17:52 -07001767void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001768 class MessageScreenReleased : public MessageBase {
1769 SurfaceFlinger* flinger;
1770 public:
1771 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1772 virtual bool handler() {
1773 flinger->onScreenReleased();
1774 return true;
1775 }
1776 };
1777 sp<MessageBase> msg = new MessageScreenReleased(this);
1778 postMessageSync(msg);
1779}
1780
1781// ---------------------------------------------------------------------------
1782
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1784{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001785 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 char buffer[SIZE];
1787 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001788
1789 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790 snprintf(buffer, SIZE, "Permission Denial: "
1791 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1792 IPCThreadState::self()->getCallingPid(),
1793 IPCThreadState::self()->getCallingUid());
1794 result.append(buffer);
1795 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001796 // Try to get the main lock, but don't insist if we can't
1797 // (this would indicate SF is stuck, but we want to be able to
1798 // print something in dumpsys).
1799 int retry = 3;
1800 while (mStateLock.tryLock()<0 && --retry>=0) {
1801 usleep(1000000);
1802 }
1803 const bool locked(retry >= 0);
1804 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001805 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001806 "SurfaceFlinger appears to be unresponsive, "
1807 "dumping anyways (no locks held)\n");
1808 result.append(buffer);
1809 }
1810
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001811 bool dumpAll = true;
1812 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001813 size_t numArgs = args.size();
1814 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001815 if ((index < numArgs) &&
1816 (args[index] == String16("--list"))) {
1817 index++;
1818 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001819 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001820 }
1821
1822 if ((index < numArgs) &&
1823 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001824 index++;
1825 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001826 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001827 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001828
1829 if ((index < numArgs) &&
1830 (args[index] == String16("--latency-clear"))) {
1831 index++;
1832 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001833 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001834 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001836
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001837 if (dumpAll) {
1838 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001839 }
1840
Mathias Agopian9795c422009-08-26 16:36:26 -07001841 if (locked) {
1842 mStateLock.unlock();
1843 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844 }
1845 write(fd, result.string(), result.size());
1846 return NO_ERROR;
1847}
1848
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001849void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1850 String8& result, char* buffer, size_t SIZE) const
1851{
1852 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1853 const size_t count = currentLayers.size();
1854 for (size_t i=0 ; i<count ; i++) {
1855 const sp<LayerBase>& layer(currentLayers[i]);
1856 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1857 result.append(buffer);
1858 }
1859}
1860
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001861void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1862 String8& result, char* buffer, size_t SIZE) const
1863{
1864 String8 name;
1865 if (index < args.size()) {
1866 name = String8(args[index]);
1867 index++;
1868 }
1869
1870 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1871 const size_t count = currentLayers.size();
1872 for (size_t i=0 ; i<count ; i++) {
1873 const sp<LayerBase>& layer(currentLayers[i]);
1874 if (name.isEmpty()) {
1875 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1876 result.append(buffer);
1877 }
1878 if (name.isEmpty() || (name == layer->getName())) {
1879 layer->dumpStats(result, buffer, SIZE);
1880 }
1881 }
1882}
1883
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001884void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1885 String8& result, char* buffer, size_t SIZE) const
1886{
1887 String8 name;
1888 if (index < args.size()) {
1889 name = String8(args[index]);
1890 index++;
1891 }
1892
1893 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1894 const size_t count = currentLayers.size();
1895 for (size_t i=0 ; i<count ; i++) {
1896 const sp<LayerBase>& layer(currentLayers[i]);
1897 if (name.isEmpty() || (name == layer->getName())) {
1898 layer->clearStats();
1899 }
1900 }
1901}
1902
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001903void SurfaceFlinger::dumpAllLocked(
1904 String8& result, char* buffer, size_t SIZE) const
1905{
1906 // figure out if we're stuck somewhere
1907 const nsecs_t now = systemTime();
1908 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1909 const nsecs_t inTransaction(mDebugInTransaction);
1910 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1911 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1912
1913 /*
1914 * Dump the visible layer list
1915 */
1916 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1917 const size_t count = currentLayers.size();
1918 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1919 result.append(buffer);
1920 for (size_t i=0 ; i<count ; i++) {
1921 const sp<LayerBase>& layer(currentLayers[i]);
1922 layer->dump(result, buffer, SIZE);
1923 }
1924
1925 /*
1926 * Dump the layers in the purgatory
1927 */
1928
1929 const size_t purgatorySize = mLayerPurgatory.size();
1930 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1931 result.append(buffer);
1932 for (size_t i=0 ; i<purgatorySize ; i++) {
1933 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1934 layer->shortDump(result, buffer, SIZE);
1935 }
1936
1937 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001938 * Dump Display state
1939 */
1940
1941 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1942 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1943 snprintf(buffer, SIZE,
1944 "+ DisplayDevice[%u]\n"
1945 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1946 "flips=%u, secure=%d, numLayers=%u\n",
1947 dpy,
1948 hw->getDisplayId(), hw->getLayerStack(),
1949 hw->getWidth(), hw->getHeight(),
1950 hw->getOrientation(), hw->getTransform().getType(),
1951 hw->getPageFlipCount(),
1952 hw->getSecureLayerVisible(),
1953 hw->getVisibleLayersSortedByZ().size());
1954 result.append(buffer);
1955 }
1956
1957 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001958 * Dump SurfaceFlinger global state
1959 */
1960
1961 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1962 result.append(buffer);
1963
Mathias Agopian888c8222012-08-04 21:10:38 -07001964 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001965 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001966 const GLExtensions& extensions(GLExtensions::getInstance());
1967 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1968 extensions.getVendor(),
1969 extensions.getRenderer(),
1970 extensions.getVersion());
1971 result.append(buffer);
1972
1973 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001974 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001975 result.append(buffer);
1976
1977 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1978 result.append(buffer);
1979
Mathias Agopian42977342012-08-05 00:40:46 -07001980 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001981 snprintf(buffer, SIZE,
1982 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001983 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001984 result.append(buffer);
1985 snprintf(buffer, SIZE,
1986 " last eglSwapBuffers() time: %f us\n"
1987 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001988 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001989 " refresh-rate : %f fps\n"
1990 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07001991 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001992 mLastSwapBufferTime/1000.0,
1993 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001994 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001995 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07001996 hwc.getDpiX(),
1997 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001998 result.append(buffer);
1999
2000 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2001 inSwapBuffersDuration/1000.0);
2002 result.append(buffer);
2003
2004 snprintf(buffer, SIZE, " transaction time: %f us\n",
2005 inTransactionDuration/1000.0);
2006 result.append(buffer);
2007
2008 /*
2009 * VSYNC state
2010 */
2011 mEventThread->dump(result, buffer, SIZE);
2012
2013 /*
2014 * Dump HWComposer state
2015 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002016 snprintf(buffer, SIZE, "h/w composer state:\n");
2017 result.append(buffer);
2018 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2019 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2020 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2021 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002022 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002023
2024 /*
2025 * Dump gralloc state
2026 */
2027 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2028 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002029 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002030}
2031
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002032status_t SurfaceFlinger::onTransact(
2033 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2034{
2035 switch (code) {
2036 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002037 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002038 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002039 case BLANK:
2040 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002041 {
2042 // codes that require permission check
2043 IPCThreadState* ipc = IPCThreadState::self();
2044 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002045 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002046 if ((uid != AID_GRAPHICS) &&
2047 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002048 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002049 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2050 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002052 break;
2053 }
2054 case CAPTURE_SCREEN:
2055 {
2056 // codes that require permission check
2057 IPCThreadState* ipc = IPCThreadState::self();
2058 const int pid = ipc->getCallingPid();
2059 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002060 if ((uid != AID_GRAPHICS) &&
2061 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002062 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002063 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2064 return PERMISSION_DENIED;
2065 }
2066 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067 }
2068 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002069
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002070 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2071 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002072 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002073 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002074 IPCThreadState* ipc = IPCThreadState::self();
2075 const int pid = ipc->getCallingPid();
2076 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002077 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002078 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079 return PERMISSION_DENIED;
2080 }
2081 int n;
2082 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002083 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002084 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085 return NO_ERROR;
2086 case 1002: // SHOW_UPDATES
2087 n = data.readInt32();
2088 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002089 invalidateHwcGeometry();
2090 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002093 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002094 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002096 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002097 setTransactionFlags(
2098 eTransactionNeeded|
2099 eDisplayTransactionNeeded|
2100 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002101 return NO_ERROR;
2102 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002103 case 1006:{ // send empty update
2104 signalRefresh();
2105 return NO_ERROR;
2106 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002107 case 1008: // toggle use of hw composer
2108 n = data.readInt32();
2109 mDebugDisableHWC = n ? 1 : 0;
2110 invalidateHwcGeometry();
2111 repaintEverything();
2112 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002113 case 1009: // toggle use of transform hint
2114 n = data.readInt32();
2115 mDebugDisableTransformHint = n ? 1 : 0;
2116 invalidateHwcGeometry();
2117 repaintEverything();
2118 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002120 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121 reply->writeInt32(0);
2122 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002123 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002124 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002125 return NO_ERROR;
2126 case 1013: {
2127 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002128 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2129 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002130 }
2131 return NO_ERROR;
2132 }
2133 }
2134 return err;
2135}
2136
Mathias Agopian53331da2011-08-22 21:44:41 -07002137void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002138 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002139 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002140}
2141
Mathias Agopian59119e62010-10-11 12:37:43 -07002142// ---------------------------------------------------------------------------
2143
Mathias Agopian118d0242011-10-13 16:02:48 -07002144status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2145 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2146{
2147 Mutex::Autolock _l(mStateLock);
2148 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2149}
2150
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002151status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2152 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002153{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002154 ATRACE_CALL();
2155
Mathias Agopian59119e62010-10-11 12:37:43 -07002156 if (!GLExtensions::getInstance().haveFramebufferObject())
2157 return INVALID_OPERATION;
2158
2159 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002160 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2161 const uint32_t hw_w = hw->getWidth();
2162 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002163 GLfloat u = 1;
2164 GLfloat v = 1;
2165
2166 // make sure to clear all GL error flags
2167 while ( glGetError() != GL_NO_ERROR ) ;
2168
2169 // create a FBO
2170 GLuint name, tname;
2171 glGenTextures(1, &tname);
2172 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002173 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2174 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002175 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2176 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002177 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002178 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002179 GLint tw = (2 << (31 - clz(hw_w)));
2180 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2182 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002183 u = GLfloat(hw_w) / tw;
2184 v = GLfloat(hw_h) / th;
2185 }
2186 glGenFramebuffersOES(1, &name);
2187 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2189 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002190
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002191 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002192 glDisable(GL_TEXTURE_EXTERNAL_OES);
2193 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002194 glClearColor(0,0,0,1);
2195 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002196 glMatrixMode(GL_MODELVIEW);
2197 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002198 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002199 const size_t count = layers.size();
2200 for (size_t i=0 ; i<count ; ++i) {
2201 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002202 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 }
2204
Mathias Agopian42977342012-08-05 00:40:46 -07002205 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002206
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 // back to main framebuffer
2208 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002209 glDeleteFramebuffersOES(1, &name);
2210
2211 *textureName = tname;
2212 *uOut = u;
2213 *vOut = v;
2214 return NO_ERROR;
2215}
2216
2217// ---------------------------------------------------------------------------
2218
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2220 sp<IMemoryHeap>* heap,
2221 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002222 uint32_t sw, uint32_t sh,
2223 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002224{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002225 ATRACE_CALL();
2226
Mathias Agopian74c40c02010-09-29 13:02:36 -07002227 status_t result = PERMISSION_DENIED;
2228
2229 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002230 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002231 ALOGE("invalid display %d", dpy);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002232 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002233 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002234
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002235 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002237 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002238
2239 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002240 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2241 const uint32_t hw_w = hw->getWidth();
2242 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002243
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002244 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002245 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002246 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002247 return PERMISSION_DENIED;
2248 }
2249
2250 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002251 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002253 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002254
2255 sw = (!sw) ? hw_w : sw;
2256 sh = (!sh) ? hw_h : sh;
2257 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002258 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002260// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2261// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002262
Mathias Agopian74c40c02010-09-29 13:02:36 -07002263 // make sure to clear all GL error flags
2264 while ( glGetError() != GL_NO_ERROR ) ;
2265
2266 // create a FBO
2267 GLuint name, tname;
2268 glGenRenderbuffersOES(1, &tname);
2269 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2270 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002271
Mathias Agopian74c40c02010-09-29 13:02:36 -07002272 glGenFramebuffersOES(1, &name);
2273 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2274 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2275 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2276
2277 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002278
Mathias Agopian74c40c02010-09-29 13:02:36 -07002279 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2280
2281 // invert everything, b/c glReadPixel() below will invert the FB
2282 glViewport(0, 0, sw, sh);
2283 glMatrixMode(GL_PROJECTION);
2284 glPushMatrix();
2285 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002286 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287 glMatrixMode(GL_MODELVIEW);
2288
2289 // redraw the screen entirely...
2290 glClearColor(0,0,0,1);
2291 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002292
Jamie Gennis9575f602011-10-07 14:51:16 -07002293 const LayerVector& layers(mDrawingState.layersSortedByZ);
2294 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002295 for (size_t i=0 ; i<count ; ++i) {
2296 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002297 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002298 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002299 const uint32_t z = layer->drawingState().z;
2300 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002301 if (filtering) layer->setFiltering(true);
2302 layer->draw(hw);
2303 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002304 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002305 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002306 }
2307
Mathias Agopian74c40c02010-09-29 13:02:36 -07002308 // check for errors and return screen capture
2309 if (glGetError() != GL_NO_ERROR) {
2310 // error while rendering
2311 result = INVALID_OPERATION;
2312 } else {
2313 // allocate shared memory large enough to hold the
2314 // screen capture
2315 sp<MemoryHeapBase> base(
2316 new MemoryHeapBase(size, 0, "screen-capture") );
2317 void* const ptr = base->getBase();
2318 if (ptr) {
2319 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002320 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002321 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2322 if (glGetError() == GL_NO_ERROR) {
2323 *heap = base;
2324 *w = sw;
2325 *h = sh;
2326 *f = PIXEL_FORMAT_RGBA_8888;
2327 result = NO_ERROR;
2328 }
2329 } else {
2330 result = NO_MEMORY;
2331 }
2332 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002333 glViewport(0, 0, hw_w, hw_h);
2334 glMatrixMode(GL_PROJECTION);
2335 glPopMatrix();
2336 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 } else {
2338 result = BAD_VALUE;
2339 }
2340
2341 // release FBO resources
2342 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2343 glDeleteRenderbuffersOES(1, &tname);
2344 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002345
Mathias Agopian42977342012-08-05 00:40:46 -07002346 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002347
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002348// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002349
Mathias Agopian74c40c02010-09-29 13:02:36 -07002350 return result;
2351}
2352
2353
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002354status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2355 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002357 uint32_t sw, uint32_t sh,
2358 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002359{
2360 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002361 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002362 return BAD_VALUE;
2363
2364 if (!GLExtensions::getInstance().haveFramebufferObject())
2365 return INVALID_OPERATION;
2366
2367 class MessageCaptureScreen : public MessageBase {
2368 SurfaceFlinger* flinger;
2369 DisplayID dpy;
2370 sp<IMemoryHeap>* heap;
2371 uint32_t* w;
2372 uint32_t* h;
2373 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 uint32_t sw;
2375 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002376 uint32_t minLayerZ;
2377 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002378 status_t result;
2379 public:
2380 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002382 uint32_t sw, uint32_t sh,
2383 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002384 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002385 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2386 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2387 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002388 {
2389 }
2390 status_t getResult() const {
2391 return result;
2392 }
2393 virtual bool handler() {
2394 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002395 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002396 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002397 return true;
2398 }
2399 };
2400
2401 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002402 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002403 status_t res = postMessageSync(msg);
2404 if (res == NO_ERROR) {
2405 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2406 }
2407 return res;
2408}
2409
2410// ---------------------------------------------------------------------------
2411
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002412SurfaceFlinger::LayerVector::LayerVector() {
2413}
2414
2415SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2416 : SortedVector<sp<LayerBase> >(rhs) {
2417}
2418
2419int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2420 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002421{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002422 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002423 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2424 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002425
2426 uint32_t ls = l->currentState().layerStack;
2427 uint32_t rs = r->currentState().layerStack;
2428 if (ls != rs)
2429 return ls - rs;
2430
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002431 uint32_t lz = l->currentState().z;
2432 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002433 if (lz != rz)
2434 return lz - rz;
2435
2436 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002437}
2438
2439// ---------------------------------------------------------------------------
2440
Mathias Agopiane57f2922012-08-09 16:29:12 -07002441SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2442}
2443
2444SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2445 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002446}
2447
2448// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002449
Jamie Gennis9a78c902011-01-12 18:30:40 -08002450GraphicBufferAlloc::GraphicBufferAlloc() {}
2451
2452GraphicBufferAlloc::~GraphicBufferAlloc() {}
2453
2454sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002455 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002456 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2457 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002458 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002459 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002460 if (err == NO_MEMORY) {
2461 GraphicBuffer::dumpAllocationsToSystemLog();
2462 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002463 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002464 "failed (%s), handle=%p",
2465 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002466 return 0;
2467 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002468 return graphicBuffer;
2469}
2470
Jamie Gennis9a78c902011-01-12 18:30:40 -08002471// ---------------------------------------------------------------------------
2472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473}; // namespace android