blob: 1f9d694930f7e3c4ece93876794a8614bb65b8c8 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
39#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/SurfaceTextureClient.h>
41
42#include <ui/GraphicBufferAllocator.h>
43#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/String8.h>
46#include <utils/String16.h>
47#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080048#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian921e6ac2012-07-23 23:11:29 -070050#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080053#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070055#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070060#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana4912602012-07-12 14:25:33 -070063#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070064#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070086 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070099 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
Steve Blocka19954a2012-01-04 20:05:49 +0000101 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // debugging stuff...
104 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 property_get("debug.sf.showupdates", value, "0");
107 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
112 DdmConnection::start(getServiceName());
113 }
114
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700115 ALOGI_IF(mDebugRegion, "showupdates enabled");
116 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800119void SurfaceFlinger::onFirstRef()
120{
121 mEventQueue.init(this);
122
123 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
124
125 // Wait for the main thread to be done with its initialization
126 mReadyToRunBarrier.wait();
127}
128
129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130SurfaceFlinger::~SurfaceFlinger()
131{
Mathias Agopiana4912602012-07-12 14:25:33 -0700132 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
133 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
134 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800137void SurfaceFlinger::binderDied(const wp<IBinder>& who)
138{
139 // the window manager died on us. prepare its eulogy.
140
141 // reset screen orientation
142 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700143 Vector<DisplayState> displays;
144 DisplayState d;
145 d.orientation = eOrientationDefault;
146 displays.add(d);
147 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148
149 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700150 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800151}
152
Mathias Agopian7e27f052010-05-28 14:22:23 -0700153sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154{
Mathias Agopian96f08192010-06-02 23:28:45 -0700155 sp<ISurfaceComposerClient> bclient;
156 sp<Client> client(new Client(this));
157 status_t err = client->initCheck();
158 if (err == NO_ERROR) {
159 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 return bclient;
162}
163
Jamie Gennis9a78c902011-01-12 18:30:40 -0800164sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
165{
166 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
167 return gba;
168}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170void SurfaceFlinger::bootFinished()
171{
172 const nsecs_t now = systemTime();
173 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000174 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700175 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700176
177 // wait patiently for the window manager death
178 const String16 name("window");
179 sp<IBinder> window(defaultServiceManager()->getService(name));
180 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700181 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700182 }
183
184 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700185 // formerly we would just kill the process, but we now ask it to exit so it
186 // can choose where to stop the animation.
187 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188}
189
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700190void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
191 class MessageDestroyGLTexture : public MessageBase {
192 GLuint texture;
193 public:
194 MessageDestroyGLTexture(GLuint texture)
195 : texture(texture) {
196 }
197 virtual bool handler() {
198 glDeleteTextures(1, &texture);
199 return true;
200 }
201 };
202 postMessageAsync(new MessageDestroyGLTexture(texture));
203}
204
Mathias Agopiana4912602012-07-12 14:25:33 -0700205status_t SurfaceFlinger::selectConfigForPixelFormat(
206 EGLDisplay dpy,
207 EGLint const* attrs,
208 PixelFormat format,
209 EGLConfig* outConfig)
210{
211 EGLConfig config = NULL;
212 EGLint numConfigs = -1, n=0;
213 eglGetConfigs(dpy, NULL, 0, &numConfigs);
214 EGLConfig* const configs = new EGLConfig[numConfigs];
215 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
216 for (int i=0 ; i<n ; i++) {
217 EGLint nativeVisualId = 0;
218 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
219 if (nativeVisualId>0 && format == nativeVisualId) {
220 *outConfig = configs[i];
221 delete [] configs;
222 return NO_ERROR;
223 }
224 }
225 delete [] configs;
226 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227}
228
Mathias Agopiana4912602012-07-12 14:25:33 -0700229EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
230 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
231 // it is to be used with WIFI displays
232 EGLConfig config;
233 EGLint dummy;
234 status_t err;
235 EGLint attribs[] = {
236 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
237 EGL_RECORDABLE_ANDROID, EGL_TRUE,
238 EGL_NONE
239 };
240 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
241 if (err) {
242 // maybe we failed because of EGL_RECORDABLE_ANDROID
243 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
244 attribs[2] = EGL_NONE;
245 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
246 }
247 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
248 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
249 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
250 }
251 return config;
252}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253
Mathias Agopiana4912602012-07-12 14:25:33 -0700254EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
255 // Also create our EGLContext
256 EGLint contextAttributes[] = {
257#ifdef EGL_IMG_context_priority
258#ifdef HAS_CONTEXT_PRIORITY
259#warning "using EGL_IMG_context_priority"
260 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
261#endif
262#endif
263 EGL_NONE, EGL_NONE
264 };
265 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
266 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
267 return ctxt;
268}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269
Mathias Agopiana4912602012-07-12 14:25:33 -0700270void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
271 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
272 if (!result) {
273 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
274 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 }
276
Mathias Agopiana4912602012-07-12 14:25:33 -0700277 GLExtensions& extensions(GLExtensions::getInstance());
278 extensions.initWithGLStrings(
279 glGetString(GL_VENDOR),
280 glGetString(GL_RENDERER),
281 glGetString(GL_VERSION),
282 glGetString(GL_EXTENSIONS),
283 eglQueryString(display, EGL_VENDOR),
284 eglQueryString(display, EGL_VERSION),
285 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700286
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 EGLint w, h;
288 eglQuerySurface(display, surface, EGL_WIDTH, &w);
289 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700290
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
292 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700293
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700295 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 glShadeModel(GL_FLAT);
298 glDisable(GL_DITHER);
299 glDisable(GL_CULL_FACE);
300
Mathias Agopiana4912602012-07-12 14:25:33 -0700301 struct pack565 {
302 inline uint16_t operator() (int r, int g, int b) const {
303 return (r<<11)|(g<<5)|b;
304 }
305 } pack565;
306
Jamie Gennis9575f602011-10-07 14:51:16 -0700307 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
308 glGenTextures(1, &mProtectedTexName);
309 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
310 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
311 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
312 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
313 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
314 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
315 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316
317 glViewport(0, 0, w, h);
318 glMatrixMode(GL_PROJECTION);
319 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700320 // put the origin in the left-bottom corner
321 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 -0800322
Mathias Agopiana4912602012-07-12 14:25:33 -0700323 // print some debugging info
324 EGLint r,g,b,a;
325 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
326 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
327 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
328 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
329 ALOGI("EGL informations:");
330 ALOGI("vendor : %s", extensions.getEglVendor());
331 ALOGI("version : %s", extensions.getEglVersion());
332 ALOGI("extensions: %s", extensions.getEglExtension());
333 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
334 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
335 ALOGI("OpenGL ES informations:");
336 ALOGI("vendor : %s", extensions.getVendor());
337 ALOGI("renderer : %s", extensions.getRenderer());
338 ALOGI("version : %s", extensions.getVersion());
339 ALOGI("extensions: %s", extensions.getExtension());
340 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
341 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
342}
343
Mathias Agopiana4912602012-07-12 14:25:33 -0700344status_t SurfaceFlinger::readyToRun()
345{
346 ALOGI( "SurfaceFlinger's main thread ready to run. "
347 "Initializing graphics H/W...");
348
Mathias Agopiana4912602012-07-12 14:25:33 -0700349 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700350 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
351 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700352
353 // Initialize the main display
354 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700355 sp<FramebufferSurface> fbs = FramebufferSurface::create();
356 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700357 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
358 exit(0);
359 }
360
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700361 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
362
Mathias Agopiana4912602012-07-12 14:25:33 -0700363 // initialize the config and context
364 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700365 ANativeWindow* const anw = stc.get();
366 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700367 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
368 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700369
370 // initialize our main display hardware
Mathias Agopian92a979a2012-08-02 18:32:23 -0700371 mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState());
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700372 sp<DisplayDevice> hw = new DisplayDevice(this, DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700373 mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700374
375 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700376 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700377 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800378
Mathias Agopian028508c2012-07-25 21:12:12 -0700379 // start the EventThread
380 mEventThread = new EventThread(this);
381 mEventQueue.setEventThread(mEventThread);
382
Mathias Agopian86303202012-07-24 22:46:10 -0700383 // initialize the H/W composer
Mathias Agopian888c8222012-08-04 21:10:38 -0700384 mHwc = new HWComposer(this, *static_cast<HWComposer::EventHandler *>(this));
Mathias Agopian92a979a2012-08-02 18:32:23 -0700385
386 // initialize our drawing state
387 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700388
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800390 mReadyToRunBarrier.open();
391
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700392 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700393 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 return NO_ERROR;
396}
397
Mathias Agopiana67e4182012-06-19 17:26:12 -0700398void SurfaceFlinger::startBootAnim() {
399 // start boot animation
400 property_set("service.bootanim.exit", "0");
401 property_set("ctl.start", "bootanim");
402}
403
Mathias Agopiana4912602012-07-12 14:25:33 -0700404uint32_t SurfaceFlinger::getMaxTextureSize() const {
405 return mMaxTextureSize;
406}
407
408uint32_t SurfaceFlinger::getMaxViewportDims() const {
409 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
410 mMaxViewportDims[0] : mMaxViewportDims[1];
411}
412
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800414
Jamie Gennis582270d2011-08-17 18:19:00 -0700415bool SurfaceFlinger::authenticateSurfaceTexture(
416 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800417 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700418 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800419
420 // Check the visible layer list for the ISurface
421 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
422 size_t count = currentLayers.size();
423 for (size_t i=0 ; i<count ; i++) {
424 const sp<LayerBase>& layer(currentLayers[i]);
425 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700426 if (lbc != NULL) {
427 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
428 if (lbcBinder == surfaceTextureBinder) {
429 return true;
430 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800431 }
432 }
433
434 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700435 // SurfaceTexture gets destroyed before all the clients are done using it,
436 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800437 // will instead fail later on when the client tries to use the surface,
438 // which should be reported as "surface XYZ returned an -ENODEV". The
439 // purgatorized layers are no less authentic than the visible ones, so this
440 // should not cause any harm.
441 size_t purgatorySize = mLayerPurgatory.size();
442 for (size_t i=0 ; i<purgatorySize ; i++) {
443 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
444 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700445 if (lbc != NULL) {
446 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
447 if (lbcBinder == surfaceTextureBinder) {
448 return true;
449 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800450 }
451 }
452
453 return false;
454}
455
Mathias Agopianc666cae2012-07-25 18:56:13 -0700456status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700457 // TODO: this is here only for compatibility -- show go away eventually.
Mathias Agopianc666cae2012-07-25 18:56:13 -0700458 if (uint32_t(dpy) >= 2) {
459 return BAD_INDEX;
460 }
Mathias Agopian42977342012-08-05 00:40:46 -0700461 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
462 info->w = hw->getWidth();
463 info->h = hw->getHeight();
464 info->xdpi = hw->getDpiX();
465 info->ydpi = hw->getDpiY();
Mathias Agopian888c8222012-08-04 21:10:38 -0700466 info->fps = float(1e9 / getHwComposer().getRefreshPeriod());
Mathias Agopian42977342012-08-05 00:40:46 -0700467 info->density = hw->getDensity();
468 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700469 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700470 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700471 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700472}
473
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800474// ----------------------------------------------------------------------------
475
476sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800477 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700478}
479
Mathias Agopian3094df32012-06-18 18:06:45 -0700480void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700481 EGLSurface result = EGL_NO_SURFACE;
482 EGLSurface old_surface = EGL_NO_SURFACE;
483 sp<SurfaceTextureClient> stc;
484
485 if (display != NULL) {
486 stc = new SurfaceTextureClient(display);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700487 result = eglCreateWindowSurface(mEGLDisplay,
Mathias Agopiana4912602012-07-12 14:25:33 -0700488 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700489 ALOGE_IF(result == EGL_NO_SURFACE,
490 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
491 display.get());
492 }
493
494 { // scope for the lock
495 Mutex::Autolock _l(mStateLock);
496 old_surface = mExternalDisplaySurface;
497 mExternalDisplayNativeWindow = stc;
498 mExternalDisplaySurface = result;
499 ALOGD("mExternalDisplaySurface = %p", result);
500 }
501
502 if (old_surface != EGL_NO_SURFACE) {
503 // Note: EGL allows to destroy an object while its current
504 // it will fail to become current next time though.
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700505 eglDestroySurface(mEGLDisplay, old_surface);
Mathias Agopian3094df32012-06-18 18:06:45 -0700506 }
507}
508
509EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
510 Mutex::Autolock _l(mStateLock);
511 return mExternalDisplaySurface;
512}
513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800515
516void SurfaceFlinger::waitForEvent() {
517 mEventQueue.waitMessage();
518}
519
520void SurfaceFlinger::signalTransaction() {
521 mEventQueue.invalidate();
522}
523
524void SurfaceFlinger::signalLayerUpdate() {
525 mEventQueue.invalidate();
526}
527
528void SurfaceFlinger::signalRefresh() {
529 mEventQueue.refresh();
530}
531
532status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
533 nsecs_t reltime, uint32_t flags) {
534 return mEventQueue.postMessage(msg, reltime);
535}
536
537status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
538 nsecs_t reltime, uint32_t flags) {
539 status_t res = mEventQueue.postMessage(msg, reltime);
540 if (res == NO_ERROR) {
541 msg->wait();
542 }
543 return res;
544}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545
Mathias Agopian4fec8732012-06-29 14:12:52 -0700546bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 return true;
549}
550
Mathias Agopian86303202012-07-24 22:46:10 -0700551void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700552 mEventThread->onVSyncReceived(dpy, timestamp);
553}
554
555void SurfaceFlinger::eventControl(int event, int enabled) {
556 getHwComposer().eventControl(event, enabled);
557}
558
Mathias Agopian4fec8732012-06-29 14:12:52 -0700559void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800560 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800561 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700562 case MessageQueue::INVALIDATE:
563 handleMessageTransaction();
564 handleMessageInvalidate();
565 signalRefresh();
566 break;
567 case MessageQueue::REFRESH:
568 handleMessageRefresh();
569 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800570 }
571}
572
Mathias Agopian4fec8732012-06-29 14:12:52 -0700573void SurfaceFlinger::handleMessageTransaction() {
574 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
575 uint32_t transactionFlags = peekTransactionFlags(mask);
576 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700577 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700578 }
579}
580
581void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopian87baae12012-07-31 12:38:26 -0700582 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700583}
584
585void SurfaceFlinger::handleMessageRefresh() {
586 handleRefresh();
587
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700588 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700589 mVisibleRegionsDirty = false;
590 invalidateHwcGeometry();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700591
592 /*
593 * rebuild the visible layer list per screen
594 */
595
Mathias Agopian87baae12012-07-31 12:38:26 -0700596 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700597 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700598 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700599 Region opaqueRegion;
600 Region dirtyRegion;
601 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700602 hw->getLayerStack(), dirtyRegion, opaqueRegion);
603 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700604
605 Vector< sp<LayerBase> > layersSortedByZ;
606 const size_t count = currentLayers.size();
607 for (size_t i=0 ; i<count ; i++) {
608 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700609 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700610 if (!currentLayers[i]->visibleRegion.isEmpty()) {
611 layersSortedByZ.add(currentLayers[i]);
612 }
613 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700614 }
Mathias Agopian42977342012-08-05 00:40:46 -0700615 hw->setVisibleLayersSortedByZ(layersSortedByZ);
616 hw->undefinedRegion.set(hw->getBounds());
617 hw->undefinedRegion.subtractSelf(hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700618 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700619 }
620
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700621 HWComposer& hwc(getHwComposer());
622 if (hwc.initCheck() == NO_ERROR) {
623 // build the h/w work list
624 const bool workListsDirty = mHwWorkListDirty;
625 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700626 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700627 sp<const DisplayDevice> hw(mDisplays[dpy]);
628 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700629 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700630
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700631 hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays
632
633 HWComposer::LayerListIterator cur = hwc.begin();
634 const HWComposer::LayerListIterator end = hwc.end();
635 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
636 const sp<LayerBase>& layer(currentLayers[i]);
637
638 if (CC_UNLIKELY(workListsDirty)) {
639 layer->setGeometry(hw, *cur);
640 if (mDebugDisableHWC || mDebugRegion) {
641 cur->setSkip(true);
642 }
643 }
644
645 /*
646 * update the per-frame h/w composer data for each layer
647 * and build the transparent region of the FB
648 */
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700649 layer->setPerFrameData(hw, *cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700650 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700651 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700652 status_t err = hwc.prepare();
653 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
654 }
655
656 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700657 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700658 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700659
Mathias Agopian87baae12012-07-31 12:38:26 -0700660 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700661 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700662 Region dirtyRegion;
663 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700664 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700665 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700666 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
667 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700668 }
Mathias Agopian42977342012-08-05 00:40:46 -0700669 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700670
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700671 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700672 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700673 // repaint the framebuffer (if needed)
674 handleRepaint(hw, dirtyRegion);
675 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700676 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700677 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700678 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679 }
680
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700681 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700682
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700683
684#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700685 // render to the external display if we have one
686 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
687 if (externalDisplaySurface != EGL_NO_SURFACE) {
688 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
689 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
690 externalDisplaySurface, externalDisplaySurface,
691 eglGetCurrentContext());
692
693 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
694
695 if (success) {
696 // redraw the screen entirely...
697 glDisable(GL_TEXTURE_EXTERNAL_OES);
698 glDisable(GL_TEXTURE_2D);
699 glClearColor(0,0,0,1);
700 glClear(GL_COLOR_BUFFER_BIT);
701 glMatrixMode(GL_MODELVIEW);
702 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700703
Mathias Agopian42977342012-08-05 00:40:46 -0700704 const sp<DisplayDevice>& hw(getDisplayDevice(0));
705 const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700706 const size_t count = layers.size();
707 for (size_t i=0 ; i<count ; ++i) {
708 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700709 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700710 }
711
712 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
713 ALOGE_IF(!success, "external display eglSwapBuffers failed");
714
Mathias Agopian42977342012-08-05 00:40:46 -0700715 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700716 }
717
718 success = eglMakeCurrent(eglGetCurrentDisplay(),
719 cur, cur, eglGetCurrentContext());
720
721 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
722 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700723#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724
725}
726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727void SurfaceFlinger::postFramebuffer()
728{
Mathias Agopian841cde52012-03-01 15:44:37 -0800729 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800730
Mathias Agopiana44b0412011-10-16 18:46:35 -0700731 const nsecs_t now = systemTime();
732 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700733
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700734 HWComposer& hwc(getHwComposer());
735
Mathias Agopian92a979a2012-08-02 18:32:23 -0700736 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700737 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700738 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700739 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700740 const size_t count = currentLayers.size();
741 HWComposer::LayerListIterator cur = hwc.begin();
742 const HWComposer::LayerListIterator end = hwc.end();
743 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
744 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700745 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700746 }
747 }
Mathias Agopian42977342012-08-05 00:40:46 -0700748 hw->flip(hw->swapRegion);
749 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700750 }
751
Jesse Hallef194142012-06-14 14:45:17 -0700752 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700753 // FIXME: eventually commit() won't take arguments
Mathias Agopian42977342012-08-05 00:40:46 -0700754 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700755 }
756
Mathias Agopian92a979a2012-08-02 18:32:23 -0700757 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700758 sp<const DisplayDevice> hw(mDisplays[dpy]);
759 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700760 const size_t count = currentLayers.size();
761 if (hwc.initCheck() == NO_ERROR) {
762 HWComposer::LayerListIterator cur = hwc.begin();
763 const HWComposer::LayerListIterator end = hwc.end();
764 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700765 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700766 }
767 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700768 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700769 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700770 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700771 }
Jesse Hallef194142012-06-14 14:45:17 -0700772 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700773
774 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800775 }
776
Mathias Agopiana44b0412011-10-16 18:46:35 -0700777 mLastSwapBufferTime = systemTime() - now;
778 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779}
780
Mathias Agopian87baae12012-07-31 12:38:26 -0700781void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782{
Mathias Agopian841cde52012-03-01 15:44:37 -0800783 ATRACE_CALL();
784
Mathias Agopianca4d3602011-05-19 15:38:14 -0700785 Mutex::Autolock _l(mStateLock);
786 const nsecs_t now = systemTime();
787 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788
Mathias Agopianca4d3602011-05-19 15:38:14 -0700789 // Here we're guaranteed that some transaction flags are set
790 // so we can call handleTransactionLocked() unconditionally.
791 // We call getTransactionFlags(), which will also clear the flags,
792 // with mStateLock held to guarantee that mCurrentState won't change
793 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700794
Mathias Agopianca4d3602011-05-19 15:38:14 -0700795 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
796 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700797 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700798
Mathias Agopianca4d3602011-05-19 15:38:14 -0700799 mLastTransactionTime = systemTime() - now;
800 mDebugInTransaction = 0;
801 invalidateHwcGeometry();
802 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700803}
804
Mathias Agopian87baae12012-07-31 12:38:26 -0700805void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700806{
807 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 const size_t count = currentLayers.size();
809
810 /*
811 * Traversal of the children
812 * (perform the transaction for each of them if needed)
813 */
814
815 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
816 if (layersNeedTransaction) {
817 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700818 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
820 if (!trFlags) continue;
821
822 const uint32_t flags = layer->doTransaction(0);
823 if (flags & Layer::eVisibleRegion)
824 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 }
826 }
827
828 /*
829 * Perform our own transaction if needed
830 */
831
832 if (transactionFlags & eTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700833 // here we take advantage of Vector's copy-on-write semantics to
834 // improve performance by skipping the transaction entirely when
835 // know that the lists are identical
836 const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
837 const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
838 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700840 const size_t cc = curr.size();
841 const size_t dc = draw.size();
842
843 // find the displays that were removed
844 // (ie: in drawing state but not in current state)
845 // also handle displays that changed
846 // (ie: displays that are in both lists)
847 for (size_t i=0 ; i<dc ; i++) {
848 if (curr.indexOfKey(draw[i].id) < 0) {
849 // in drawing state but not in current state
850 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700851 mDisplays.removeItem(draw[i].id);
852 } else {
853 ALOGW("trying to remove the main display");
854 }
855 } else {
856 // this display is in both lists. see if something changed.
857 const DisplayDeviceState& state(curr[i]);
858 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700859 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
860 //disp->setLayerStack(state.layerStack); // FIXME: set layer stack
Mathias Agopian92a979a2012-08-02 18:32:23 -0700861 }
862 if (curr[i].orientation != draw[i].orientation) {
Mathias Agopian42977342012-08-05 00:40:46 -0700863 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
864 disp->setOrientation(state.orientation);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700865 }
866 }
867 }
868
869 // find displays that were added
870 // (ie: in current state but not in drawing state)
871 for (size_t i=0 ; i<cc ; i++) {
872 if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
873 // FIXME: we need to pass the surface here
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700874 sp<DisplayDevice> disp = new DisplayDevice(this, curr[i].id, 0, 0, mEGLConfig);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700875 mDisplays.add(curr[i].id, disp);
876 }
877 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 }
879
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700880 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
881 // layers have been added
882 mVisibleRegionsDirty = true;
883 }
884
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 // some layers might have been removed, so
886 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700887 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700888 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700890 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700891 const size_t count = previousLayers.size();
892 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700893 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700894 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700895 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700896 // TODO: we could traverse the tree from front to back and
897 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700898 // TODO: we could cache the transformed region
899 Layer::State front(layer->drawingState());
900 Region visibleReg = front.transform.transform(
901 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700902 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700903 }
904 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 }
907
908 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700909}
910
911void SurfaceFlinger::commitTransaction()
912{
913 if (!mLayersPendingRemoval.isEmpty()) {
914 // Notify removed layers now that they can't be drawn from
915 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
916 mLayersPendingRemoval[i]->onRemoved();
917 }
918 mLayersPendingRemoval.clear();
919 }
920
921 mDrawingState = mCurrentState;
922 mTransationPending = false;
923 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924}
925
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700927 const LayerVector& currentLayers, uint32_t layerStack,
928 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929{
Mathias Agopian841cde52012-03-01 15:44:37 -0800930 ATRACE_CALL();
931
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 Region aboveOpaqueLayers;
933 Region aboveCoveredLayers;
934 Region dirty;
935
Mathias Agopian87baae12012-07-31 12:38:26 -0700936 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937
938 size_t i = currentLayers.size();
939 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700940 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941
942 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700943 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944
Mathias Agopian87baae12012-07-31 12:38:26 -0700945 // only consider the layers on the given later stack
946 if (s.layerStack != layerStack)
947 continue;
948
Mathias Agopianab028732010-03-16 16:41:46 -0700949 /*
950 * opaqueRegion: area of a surface that is fully opaque.
951 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700953
954 /*
955 * visibleRegion: area of a surface that is visible on screen
956 * and not fully transparent. This is essentially the layer's
957 * footprint minus the opaque regions above it.
958 * Areas covered by a translucent surface are considered visible.
959 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700961
962 /*
963 * coveredRegion: area of a surface that is covered by all
964 * visible regions above it (which includes the translucent areas).
965 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700967
968
969 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800970 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700971 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700972 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700974 if (!visibleRegion.isEmpty()) {
975 // Remove the transparent area from the visible region
976 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700977 Region transparentRegionScreen;
978 const Transform tr(s.transform);
979 if (tr.transformed()) {
980 if (tr.preserveRects()) {
981 // transform the transparent region
982 transparentRegionScreen = tr.transform(s.transparentRegion);
983 } else {
984 // transformation too complex, can't do the
985 // transparent region optimization.
986 transparentRegionScreen.clear();
987 }
988 } else {
989 transparentRegionScreen = s.transparentRegion;
990 }
991 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700992 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993
Mathias Agopianab028732010-03-16 16:41:46 -0700994 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700995 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700996 if (s.alpha==255 && !translucent &&
997 ((layerOrientation & Transform::ROT_INVALID) == false)) {
998 // the opaque region is the layer's footprint
999 opaqueRegion = visibleRegion;
1000 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 }
1002 }
1003
Mathias Agopianab028732010-03-16 16:41:46 -07001004 // Clip the covered region to the visible region
1005 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1006
1007 // Update aboveCoveredLayers for next (lower) layer
1008 aboveCoveredLayers.orSelf(visibleRegion);
1009
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 // subtract the opaque region covered by the layers above us
1011 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012
1013 // compute this layer's dirty region
1014 if (layer->contentDirty) {
1015 // we need to invalidate the whole region
1016 dirty = visibleRegion;
1017 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001018 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 layer->contentDirty = false;
1020 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001021 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001022 * the exposed region consists of two components:
1023 * 1) what's VISIBLE now and was COVERED before
1024 * 2) what's EXPOSED now less what was EXPOSED before
1025 *
1026 * note that (1) is conservative, we start with the whole
1027 * visible region but only keep what used to be covered by
1028 * something -- which mean it may have been exposed.
1029 *
1030 * (2) handles areas that were not covered by anything but got
1031 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001032 */
Mathias Agopianab028732010-03-16 16:41:46 -07001033 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001034 const Region oldVisibleRegion = layer->visibleRegion;
1035 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001036 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1037 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 }
1039 dirty.subtractSelf(aboveOpaqueLayers);
1040
1041 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001042 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043
Mathias Agopianab028732010-03-16 16:41:46 -07001044 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 // Store the visible region is screen space
1048 layer->setVisibleRegion(visibleRegion);
1049 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 }
1051
Mathias Agopian87baae12012-07-31 12:38:26 -07001052 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053}
1054
Mathias Agopian87baae12012-07-31 12:38:26 -07001055void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1056 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001057 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001058 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1059 if (hw->getLayerStack() == layerStack) {
1060 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 }
1062 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001063}
1064
1065void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001067 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001068 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001070 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001071
Mathias Agopian4fec8732012-06-29 14:12:52 -07001072 bool visibleRegions = false;
1073 const size_t count = currentLayers.size();
1074 sp<LayerBase> const* layers = currentLayers.array();
1075 for (size_t i=0 ; i<count ; i++) {
1076 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001077 const Region dirty(layer->latchBuffer(visibleRegions));
1078 Layer::State s(layer->drawingState());
1079 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001080 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001081
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001082 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083}
1084
Mathias Agopianad456f92011-01-13 17:53:01 -08001085void SurfaceFlinger::invalidateHwcGeometry()
1086{
1087 mHwWorkListDirty = true;
1088}
1089
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001090void SurfaceFlinger::handleRefresh()
1091{
1092 bool needInvalidate = false;
1093 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1094 const size_t count = currentLayers.size();
1095 for (size_t i=0 ; i<count ; i++) {
1096 const sp<LayerBase>& layer(currentLayers[i]);
1097 if (layer->onPreComposition()) {
1098 needInvalidate = true;
1099 }
1100 }
1101 if (needInvalidate) {
1102 signalLayerUpdate();
1103 }
1104}
1105
Mathias Agopian42977342012-08-05 00:40:46 -07001106void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001107 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108{
Mathias Agopian841cde52012-03-01 15:44:37 -08001109 ATRACE_CALL();
1110
Mathias Agopian87baae12012-07-31 12:38:26 -07001111 Region dirtyRegion(inDirtyRegion);
1112
Mathias Agopianb8a55602009-06-26 19:06:36 -07001113 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001114 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115
Glenn Kasten99ed2242011-12-15 09:51:17 -08001116 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001117 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 }
1119
Mathias Agopian42977342012-08-05 00:40:46 -07001120 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001121 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001122 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1123 // takes a rectangle, we must make sure to update that whole
1124 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001125 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001126 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001127 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001128 // We need to redraw the rectangle that will be updated
1129 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001130 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001131 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001132 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001133 } else {
1134 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001135 dirtyRegion.set(hw->bounds());
1136 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 }
1138 }
1139
Mathias Agopian87baae12012-07-31 12:38:26 -07001140 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001142 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1143 const size_t count = currentLayers.size();
1144 for (size_t i=0 ; i<count ; i++) {
1145 currentLayers[i]->onPostComposition();
1146 }
1147
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001148 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001149 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150}
1151
Mathias Agopian42977342012-08-05 00:40:46 -07001152void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001153{
Mathias Agopian86303202012-07-24 22:46:10 -07001154 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001155 HWComposer::LayerListIterator cur = hwc.begin();
1156 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001157
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001158 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001159 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001160
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001161 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001162
1163 // set the frame buffer
1164 glMatrixMode(GL_MODELVIEW);
1165 glLoadIdentity();
1166
1167 // Never touch the framebuffer if we don't have any framebuffer layers
1168 if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display
Mathias Agopianb9494d52012-04-18 02:28:45 -07001169 // when using overlays, we assume a fully transparent framebuffer
1170 // NOTE: we could reduce how much we need to clear, for instance
1171 // remove where there are opaque FB layers. however, on some
1172 // GPUs doing a "clean slate" glClear might be more efficient.
1173 // We'll revisit later if needed.
1174 glClearColor(0, 0, 0, 0);
1175 glClear(GL_COLOR_BUFFER_BIT);
1176 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001177 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001178 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001179 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001180 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001181 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001182 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001183 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001184
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001185 /*
1186 * and then, render the layers targeted at the framebuffer
1187 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001188
Mathias Agopian42977342012-08-05 00:40:46 -07001189 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001190 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001191 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001192 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001193 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001194 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001195 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001196 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001197 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001198 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001199 // never clear the very first layer since we're
1200 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001201 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001202 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001203 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001204 continue;
1205 }
1206 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001207 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001208 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001209 if (cur != end) {
1210 ++cur;
1211 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001212 }
1213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214}
1215
Mathias Agopian42977342012-08-05 00:40:46 -07001216void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001217 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218{
Mathias Agopian42977342012-08-05 00:40:46 -07001219 const uint32_t flags = hw->getFlags();
1220 const int32_t height = hw->getHeight();
1221 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001222 return;
1223 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001224
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001225 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1226 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001227 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001228 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001229 }
1230
Mathias Agopianc492e672011-10-18 14:49:27 -07001231 glDisable(GL_TEXTURE_EXTERNAL_OES);
1232 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234
Mathias Agopian0926f502009-05-04 14:17:04 -07001235 static int toggle = 0;
1236 toggle = 1 - toggle;
1237 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001238 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001239 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001240 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001241 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242
Mathias Agopian87baae12012-07-31 12:38:26 -07001243 Region::const_iterator it = dirtyRegion.begin();
1244 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001245 while (it != end) {
1246 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001248 { r.left, height - r.top },
1249 { r.left, height - r.bottom },
1250 { r.right, height - r.bottom },
1251 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 };
1253 glVertexPointer(2, GL_FLOAT, 0, vertices);
1254 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1255 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001256
Mathias Agopian42977342012-08-05 00:40:46 -07001257 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258
1259 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001260 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261}
1262
Mathias Agopian87baae12012-07-31 12:38:26 -07001263void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001265 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001266 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001267 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001268 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001269
1270 GLfloat vertices[4][2];
1271 glVertexPointer(2, GL_FLOAT, 0, vertices);
1272 Region::const_iterator it = region.begin();
1273 Region::const_iterator const end = region.end();
1274 while (it != end) {
1275 const Rect& r = *it++;
1276 vertices[0][0] = r.left;
1277 vertices[0][1] = r.top;
1278 vertices[1][0] = r.right;
1279 vertices[1][1] = r.top;
1280 vertices[2][0] = r.right;
1281 vertices[2][1] = r.bottom;
1282 vertices[3][0] = r.left;
1283 vertices[3][1] = r.bottom;
1284 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1285 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286}
1287
Mathias Agopian96f08192010-06-02 23:28:45 -07001288ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1289 const sp<LayerBaseClient>& lbc)
1290{
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001292 size_t name = client->attachLayer(lbc);
1293
Mathias Agopian96f08192010-06-02 23:28:45 -07001294 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001295 Mutex::Autolock _l(mStateLock);
1296 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001297
Mathias Agopian4f113742011-05-03 16:21:41 -07001298 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001299}
1300
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001301status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302{
1303 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001304 status_t err = purgatorizeLayer_l(layer);
1305 if (err == NO_ERROR)
1306 setTransactionFlags(eTransactionNeeded);
1307 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308}
1309
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001310status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311{
1312 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1313 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001314 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 return NO_ERROR;
1316 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001317 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318}
1319
Mathias Agopian9a112062009-04-17 19:36:26 -07001320status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1321{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001322 // First add the layer to the purgatory list, which makes sure it won't
1323 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001324 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001325 if (err >= 0) {
1326 mLayerPurgatory.add(layerBase);
1327 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001328
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001329 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001330
Mathias Agopian3d579642009-06-04 18:46:21 -07001331 // it's possible that we don't find a layer, because it might
1332 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001333 // from the user because there is a race between Client::destroySurface(),
1334 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001335 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1336}
1337
Mathias Agopiandea20b12011-05-03 17:04:02 -07001338uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1339{
1340 return android_atomic_release_load(&mTransactionFlags);
1341}
1342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1344{
1345 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1346}
1347
Mathias Agopianbb641242010-05-18 17:06:55 -07001348uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349{
1350 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1351 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001352 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 }
1354 return old;
1355}
1356
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357
Mathias Agopian8b33f032012-07-24 20:43:54 -07001358void SurfaceFlinger::setTransactionState(
1359 const Vector<ComposerState>& state,
1360 const Vector<DisplayState>& displays,
1361 uint32_t flags)
1362{
Mathias Agopian698c0872011-06-28 19:09:31 -07001363 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001364
Mathias Agopian8b33f032012-07-24 20:43:54 -07001365 int orientation = eOrientationUnchanged;
1366 if (displays.size()) {
1367 // TODO: handle all displays
1368 orientation = displays[0].orientation;
1369 }
1370
Jamie Gennis28378392011-10-12 17:39:00 -07001371 uint32_t transactionFlags = 0;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001372 // FIXME: don't hardcode display id here
1373 if (mCurrentState.displays.valueFor(0).orientation != orientation) {
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001374 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001375 mCurrentState.displays.editValueFor(0).orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001376 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001377 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001378 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001379 orientation);
1380 }
1381 }
1382
Mathias Agopian698c0872011-06-28 19:09:31 -07001383 const size_t count = state.size();
1384 for (size_t i=0 ; i<count ; i++) {
1385 const ComposerState& s(state[i]);
1386 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001387 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001388 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001389
Mathias Agopian386aa982011-11-07 21:58:03 -08001390 if (transactionFlags) {
1391 // this triggers the transaction
1392 setTransactionFlags(transactionFlags);
1393
1394 // if this is a synchronous transaction, wait for it to take effect
1395 // before returning.
1396 if (flags & eSynchronous) {
1397 mTransationPending = true;
1398 }
1399 while (mTransationPending) {
1400 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1401 if (CC_UNLIKELY(err != NO_ERROR)) {
1402 // just in case something goes wrong in SF, return to the
1403 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001404 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001405 mTransationPending = false;
1406 break;
1407 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001408 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 }
1410}
1411
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001412sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001413 ISurfaceComposerClient::surface_data_t* params,
1414 const String8& name,
1415 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1417 uint32_t flags)
1418{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001419 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001420 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001421
1422 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001423 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001424 int(w), int(h));
1425 return surfaceHandle;
1426 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001427
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001428 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 switch (flags & eFXSurfaceMask) {
1430 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001431 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 break;
1433 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001434 // for now we treat Blur as Dim, until we can implement it
1435 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001437 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001439 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001440 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001441 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 }
1443
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001444 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001445 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001446 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001447 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001449 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001450 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001451 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001452 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001453 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 }
1455
1456 return surfaceHandle;
1457}
1458
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001459sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001460 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001461 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001462 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463{
1464 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001465 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 case PIXEL_FORMAT_TRANSPARENT:
1467 case PIXEL_FORMAT_TRANSLUCENT:
1468 format = PIXEL_FORMAT_RGBA_8888;
1469 break;
1470 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001471#ifdef NO_RGBX_8888
1472 format = PIXEL_FORMAT_RGB_565;
1473#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001474 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001475#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 break;
1477 }
1478
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001479#ifdef NO_RGBX_8888
1480 if (format == PIXEL_FORMAT_RGBX_8888)
1481 format = PIXEL_FORMAT_RGBA_8888;
1482#endif
1483
Mathias Agopian96f08192010-06-02 23:28:45 -07001484 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001485 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001486 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001487 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001488 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
1490 return layer;
1491}
1492
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001493sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001494 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001495 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496{
Mathias Agopian96f08192010-06-02 23:28:45 -07001497 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001498 return layer;
1499}
1500
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001501sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001502 const sp<Client>& client, DisplayID display,
1503 uint32_t w, uint32_t h, uint32_t flags)
1504{
1505 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 return layer;
1507}
1508
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001509status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510{
Mathias Agopian9a112062009-04-17 19:36:26 -07001511 /*
1512 * called by the window manager, when a surface should be marked for
1513 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001514 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001515 * The surface is removed from the current and drawing lists, but placed
1516 * in the purgatory queue, so it's not destroyed right-away (we need
1517 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001518 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001519
Mathias Agopian48d819a2009-09-10 19:41:18 -07001520 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001521 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001522 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001523
Mathias Agopian48d819a2009-09-10 19:41:18 -07001524 if (layer != 0) {
1525 err = purgatorizeLayer_l(layer);
1526 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001527 setTransactionFlags(eTransactionNeeded);
1528 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001529 }
1530 return err;
1531}
1532
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001533status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001534{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001535 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001536 status_t err = NO_ERROR;
1537 sp<LayerBaseClient> l(layer.promote());
1538 if (l != NULL) {
1539 Mutex::Autolock _l(mStateLock);
1540 err = removeLayer_l(l);
1541 if (err == NAME_NOT_FOUND) {
1542 // The surface wasn't in the current list, which means it was
1543 // removed already, which means it is in the purgatory,
1544 // and need to be removed from there.
1545 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001546 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001547 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001548 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001549 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001550 "error removing layer=%p (%s)", l.get(), strerror(-err));
1551 }
1552 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553}
1554
Mathias Agopian698c0872011-06-28 19:09:31 -07001555uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001556 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001557 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001560 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1561 if (layer != 0) {
1562 const uint32_t what = s.what;
1563 if (what & ePositionChanged) {
1564 if (layer->setPosition(s.x, s.y))
1565 flags |= eTraversalNeeded;
1566 }
1567 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001568 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001569 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1570 if (layer->setLayer(s.z)) {
1571 mCurrentState.layersSortedByZ.removeAt(idx);
1572 mCurrentState.layersSortedByZ.add(layer);
1573 // we need traversal (state changed)
1574 // AND transaction (list changed)
1575 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 }
1577 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001578 if (what & eSizeChanged) {
1579 if (layer->setSize(s.w, s.h)) {
1580 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001581 }
1582 }
1583 if (what & eAlphaChanged) {
1584 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1585 flags |= eTraversalNeeded;
1586 }
1587 if (what & eMatrixChanged) {
1588 if (layer->setMatrix(s.matrix))
1589 flags |= eTraversalNeeded;
1590 }
1591 if (what & eTransparentRegionChanged) {
1592 if (layer->setTransparentRegionHint(s.transparentRegion))
1593 flags |= eTraversalNeeded;
1594 }
1595 if (what & eVisibilityChanged) {
1596 if (layer->setFlags(s.flags, s.mask))
1597 flags |= eTraversalNeeded;
1598 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001599 if (what & eCropChanged) {
1600 if (layer->setCrop(s.crop))
1601 flags |= eTraversalNeeded;
1602 }
Mathias Agopian87855782012-07-24 21:41:09 -07001603 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001604 // NOTE: index needs to be calculated before we update the state
1605 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1606 if (layer->setLayerStack(s.layerStack)) {
1607 mCurrentState.layersSortedByZ.removeAt(idx);
1608 mCurrentState.layersSortedByZ.add(layer);
1609 // we need traversal (state changed)
1610 // AND transaction (list changed)
1611 flags |= eTransactionNeeded|eTraversalNeeded;
1612 }
Mathias Agopian87855782012-07-24 21:41:09 -07001613 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001615 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616}
1617
Mathias Agopianb60314a2012-04-10 22:09:54 -07001618// ---------------------------------------------------------------------------
1619
1620void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001621 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001622 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001623 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001624 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001625 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626}
1627
Mathias Agopianb60314a2012-04-10 22:09:54 -07001628void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001629 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001630 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1631 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001632 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001633 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001634 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001635 // from this point on, SF will stop drawing
1636 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637}
1638
Colin Cross8e533062012-06-07 13:17:52 -07001639void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001640 class MessageScreenAcquired : public MessageBase {
1641 SurfaceFlinger* flinger;
1642 public:
1643 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1644 virtual bool handler() {
1645 flinger->onScreenAcquired();
1646 return true;
1647 }
1648 };
1649 sp<MessageBase> msg = new MessageScreenAcquired(this);
1650 postMessageSync(msg);
1651}
1652
Colin Cross8e533062012-06-07 13:17:52 -07001653void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001654 class MessageScreenReleased : public MessageBase {
1655 SurfaceFlinger* flinger;
1656 public:
1657 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1658 virtual bool handler() {
1659 flinger->onScreenReleased();
1660 return true;
1661 }
1662 };
1663 sp<MessageBase> msg = new MessageScreenReleased(this);
1664 postMessageSync(msg);
1665}
1666
1667// ---------------------------------------------------------------------------
1668
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1670{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001671 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672 char buffer[SIZE];
1673 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001674
1675 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 snprintf(buffer, SIZE, "Permission Denial: "
1677 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1678 IPCThreadState::self()->getCallingPid(),
1679 IPCThreadState::self()->getCallingUid());
1680 result.append(buffer);
1681 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001682 // Try to get the main lock, but don't insist if we can't
1683 // (this would indicate SF is stuck, but we want to be able to
1684 // print something in dumpsys).
1685 int retry = 3;
1686 while (mStateLock.tryLock()<0 && --retry>=0) {
1687 usleep(1000000);
1688 }
1689 const bool locked(retry >= 0);
1690 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001691 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001692 "SurfaceFlinger appears to be unresponsive, "
1693 "dumping anyways (no locks held)\n");
1694 result.append(buffer);
1695 }
1696
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001697 bool dumpAll = true;
1698 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001699 size_t numArgs = args.size();
1700 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001701 if ((index < numArgs) &&
1702 (args[index] == String16("--list"))) {
1703 index++;
1704 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001705 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001706 }
1707
1708 if ((index < numArgs) &&
1709 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001710 index++;
1711 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001712 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001713 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001714
1715 if ((index < numArgs) &&
1716 (args[index] == String16("--latency-clear"))) {
1717 index++;
1718 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001719 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001720 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001722
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001723 if (dumpAll) {
1724 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001725 }
1726
Mathias Agopian9795c422009-08-26 16:36:26 -07001727 if (locked) {
1728 mStateLock.unlock();
1729 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730 }
1731 write(fd, result.string(), result.size());
1732 return NO_ERROR;
1733}
1734
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001735void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1736 String8& result, char* buffer, size_t SIZE) const
1737{
1738 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1739 const size_t count = currentLayers.size();
1740 for (size_t i=0 ; i<count ; i++) {
1741 const sp<LayerBase>& layer(currentLayers[i]);
1742 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1743 result.append(buffer);
1744 }
1745}
1746
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001747void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1748 String8& result, char* buffer, size_t SIZE) const
1749{
1750 String8 name;
1751 if (index < args.size()) {
1752 name = String8(args[index]);
1753 index++;
1754 }
1755
1756 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1757 const size_t count = currentLayers.size();
1758 for (size_t i=0 ; i<count ; i++) {
1759 const sp<LayerBase>& layer(currentLayers[i]);
1760 if (name.isEmpty()) {
1761 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1762 result.append(buffer);
1763 }
1764 if (name.isEmpty() || (name == layer->getName())) {
1765 layer->dumpStats(result, buffer, SIZE);
1766 }
1767 }
1768}
1769
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001770void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1771 String8& result, char* buffer, size_t SIZE) const
1772{
1773 String8 name;
1774 if (index < args.size()) {
1775 name = String8(args[index]);
1776 index++;
1777 }
1778
1779 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1780 const size_t count = currentLayers.size();
1781 for (size_t i=0 ; i<count ; i++) {
1782 const sp<LayerBase>& layer(currentLayers[i]);
1783 if (name.isEmpty() || (name == layer->getName())) {
1784 layer->clearStats();
1785 }
1786 }
1787}
1788
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001789void SurfaceFlinger::dumpAllLocked(
1790 String8& result, char* buffer, size_t SIZE) const
1791{
1792 // figure out if we're stuck somewhere
1793 const nsecs_t now = systemTime();
1794 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1795 const nsecs_t inTransaction(mDebugInTransaction);
1796 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1797 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1798
1799 /*
1800 * Dump the visible layer list
1801 */
1802 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1803 const size_t count = currentLayers.size();
1804 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1805 result.append(buffer);
1806 for (size_t i=0 ; i<count ; i++) {
1807 const sp<LayerBase>& layer(currentLayers[i]);
1808 layer->dump(result, buffer, SIZE);
1809 }
1810
1811 /*
1812 * Dump the layers in the purgatory
1813 */
1814
1815 const size_t purgatorySize = mLayerPurgatory.size();
1816 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1817 result.append(buffer);
1818 for (size_t i=0 ; i<purgatorySize ; i++) {
1819 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1820 layer->shortDump(result, buffer, SIZE);
1821 }
1822
1823 /*
1824 * Dump SurfaceFlinger global state
1825 */
1826
1827 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1828 result.append(buffer);
1829
Mathias Agopian888c8222012-08-04 21:10:38 -07001830 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001831 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001832 const GLExtensions& extensions(GLExtensions::getInstance());
1833 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1834 extensions.getVendor(),
1835 extensions.getRenderer(),
1836 extensions.getVersion());
1837 result.append(buffer);
1838
1839 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001840 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001841 result.append(buffer);
1842
1843 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1844 result.append(buffer);
1845
Mathias Agopian42977342012-08-05 00:40:46 -07001846 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001847 snprintf(buffer, SIZE,
1848 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001849 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001850 result.append(buffer);
1851 snprintf(buffer, SIZE,
1852 " last eglSwapBuffers() time: %f us\n"
1853 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001854 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001855 " refresh-rate : %f fps\n"
1856 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001857 " y-dpi : %f\n"
1858 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001859 mLastSwapBufferTime/1000.0,
1860 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001861 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001862 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian42977342012-08-05 00:40:46 -07001863 hw->getDpiX(),
1864 hw->getDpiY(),
1865 hw->getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001866 result.append(buffer);
1867
1868 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1869 inSwapBuffersDuration/1000.0);
1870 result.append(buffer);
1871
1872 snprintf(buffer, SIZE, " transaction time: %f us\n",
1873 inTransactionDuration/1000.0);
1874 result.append(buffer);
1875
1876 /*
1877 * VSYNC state
1878 */
1879 mEventThread->dump(result, buffer, SIZE);
1880
1881 /*
1882 * Dump HWComposer state
1883 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001884 snprintf(buffer, SIZE, "h/w composer state:\n");
1885 result.append(buffer);
1886 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1887 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1888 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1889 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07001890 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001891
1892 /*
1893 * Dump gralloc state
1894 */
1895 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1896 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07001897 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001898}
1899
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900status_t SurfaceFlinger::onTransact(
1901 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1902{
1903 switch (code) {
1904 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001905 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001906 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001908 case BLANK:
1909 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 {
1911 // codes that require permission check
1912 IPCThreadState* ipc = IPCThreadState::self();
1913 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001914 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001915 if ((uid != AID_GRAPHICS) &&
1916 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001917 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001918 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1919 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001921 break;
1922 }
1923 case CAPTURE_SCREEN:
1924 {
1925 // codes that require permission check
1926 IPCThreadState* ipc = IPCThreadState::self();
1927 const int pid = ipc->getCallingPid();
1928 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001929 if ((uid != AID_GRAPHICS) &&
1930 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001931 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001932 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1933 return PERMISSION_DENIED;
1934 }
1935 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936 }
1937 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001938
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1940 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001941 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001942 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001943 IPCThreadState* ipc = IPCThreadState::self();
1944 const int pid = ipc->getCallingPid();
1945 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001946 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001947 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001948 return PERMISSION_DENIED;
1949 }
1950 int n;
1951 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001952 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001953 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954 return NO_ERROR;
1955 case 1002: // SHOW_UPDATES
1956 n = data.readInt32();
1957 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001958 invalidateHwcGeometry();
1959 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001962 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001963 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001965 case 1005:{ // force transaction
1966 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1967 return NO_ERROR;
1968 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001969 case 1006:{ // send empty update
1970 signalRefresh();
1971 return NO_ERROR;
1972 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001973 case 1008: // toggle use of hw composer
1974 n = data.readInt32();
1975 mDebugDisableHWC = n ? 1 : 0;
1976 invalidateHwcGeometry();
1977 repaintEverything();
1978 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001979 case 1009: // toggle use of transform hint
1980 n = data.readInt32();
1981 mDebugDisableTransformHint = n ? 1 : 0;
1982 invalidateHwcGeometry();
1983 repaintEverything();
1984 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001985 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001986 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987 reply->writeInt32(0);
1988 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001989 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001990 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001991 return NO_ERROR;
1992 case 1013: {
1993 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07001994 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
1995 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001996 }
1997 return NO_ERROR;
1998 }
1999 }
2000 return err;
2001}
2002
Mathias Agopian53331da2011-08-22 21:44:41 -07002003void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002004 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002005 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002006}
2007
Mathias Agopian59119e62010-10-11 12:37:43 -07002008// ---------------------------------------------------------------------------
2009
Mathias Agopian118d0242011-10-13 16:02:48 -07002010status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2011 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2012{
2013 Mutex::Autolock _l(mStateLock);
2014 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2015}
2016
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002017status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2018 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002019{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002020 ATRACE_CALL();
2021
Mathias Agopian59119e62010-10-11 12:37:43 -07002022 if (!GLExtensions::getInstance().haveFramebufferObject())
2023 return INVALID_OPERATION;
2024
2025 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002026 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2027 const uint32_t hw_w = hw->getWidth();
2028 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002029 GLfloat u = 1;
2030 GLfloat v = 1;
2031
2032 // make sure to clear all GL error flags
2033 while ( glGetError() != GL_NO_ERROR ) ;
2034
2035 // create a FBO
2036 GLuint name, tname;
2037 glGenTextures(1, &tname);
2038 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002039 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2040 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002041 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2042 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002043 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002044 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002045 GLint tw = (2 << (31 - clz(hw_w)));
2046 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2048 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002049 u = GLfloat(hw_w) / tw;
2050 v = GLfloat(hw_h) / th;
2051 }
2052 glGenFramebuffersOES(1, &name);
2053 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2055 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002056
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002058 glDisable(GL_TEXTURE_EXTERNAL_OES);
2059 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002060 glClearColor(0,0,0,1);
2061 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002062 glMatrixMode(GL_MODELVIEW);
2063 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002064 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002065 const size_t count = layers.size();
2066 for (size_t i=0 ; i<count ; ++i) {
2067 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002068 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002069 }
2070
Mathias Agopian42977342012-08-05 00:40:46 -07002071 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002072
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002073 // back to main framebuffer
2074 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002075 glDeleteFramebuffersOES(1, &name);
2076
2077 *textureName = tname;
2078 *uOut = u;
2079 *vOut = v;
2080 return NO_ERROR;
2081}
2082
2083// ---------------------------------------------------------------------------
2084
Mathias Agopian74c40c02010-09-29 13:02:36 -07002085status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2086 sp<IMemoryHeap>* heap,
2087 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002088 uint32_t sw, uint32_t sh,
2089 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002090{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002091 ATRACE_CALL();
2092
Mathias Agopian74c40c02010-09-29 13:02:36 -07002093 status_t result = PERMISSION_DENIED;
2094
2095 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002096 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002097 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002098 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002099
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002100 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002101 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002102 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002103
2104 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002105 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2106 const uint32_t hw_w = hw->getWidth();
2107 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002108
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002109 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002110 if (hw->getSecureLayerVisible()) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002111 return PERMISSION_DENIED;
2112 }
2113
2114 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002115 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002116 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002117
2118 sw = (!sw) ? hw_w : sw;
2119 sh = (!sh) ? hw_h : sh;
2120 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002121 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002122
Steve Block9d453682011-12-20 16:23:08 +00002123 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002124 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002125
Mathias Agopian74c40c02010-09-29 13:02:36 -07002126 // make sure to clear all GL error flags
2127 while ( glGetError() != GL_NO_ERROR ) ;
2128
2129 // create a FBO
2130 GLuint name, tname;
2131 glGenRenderbuffersOES(1, &tname);
2132 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2133 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002134
Mathias Agopian74c40c02010-09-29 13:02:36 -07002135 glGenFramebuffersOES(1, &name);
2136 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2137 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2138 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2139
2140 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002141
Mathias Agopian74c40c02010-09-29 13:02:36 -07002142 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2143
2144 // invert everything, b/c glReadPixel() below will invert the FB
2145 glViewport(0, 0, sw, sh);
2146 glMatrixMode(GL_PROJECTION);
2147 glPushMatrix();
2148 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002149 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002150 glMatrixMode(GL_MODELVIEW);
2151
2152 // redraw the screen entirely...
2153 glClearColor(0,0,0,1);
2154 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002155
Jamie Gennis9575f602011-10-07 14:51:16 -07002156 const LayerVector& layers(mDrawingState.layersSortedByZ);
2157 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002158 for (size_t i=0 ; i<count ; ++i) {
2159 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002160 const uint32_t flags = layer->drawingState().flags;
2161 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2162 const uint32_t z = layer->drawingState().z;
2163 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002164 if (filtering) layer->setFiltering(true);
2165 layer->draw(hw);
2166 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002167 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002168 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002169 }
2170
Mathias Agopian74c40c02010-09-29 13:02:36 -07002171 // check for errors and return screen capture
2172 if (glGetError() != GL_NO_ERROR) {
2173 // error while rendering
2174 result = INVALID_OPERATION;
2175 } else {
2176 // allocate shared memory large enough to hold the
2177 // screen capture
2178 sp<MemoryHeapBase> base(
2179 new MemoryHeapBase(size, 0, "screen-capture") );
2180 void* const ptr = base->getBase();
2181 if (ptr) {
2182 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002183 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002184 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2185 if (glGetError() == GL_NO_ERROR) {
2186 *heap = base;
2187 *w = sw;
2188 *h = sh;
2189 *f = PIXEL_FORMAT_RGBA_8888;
2190 result = NO_ERROR;
2191 }
2192 } else {
2193 result = NO_MEMORY;
2194 }
2195 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002196 glViewport(0, 0, hw_w, hw_h);
2197 glMatrixMode(GL_PROJECTION);
2198 glPopMatrix();
2199 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002200 } else {
2201 result = BAD_VALUE;
2202 }
2203
2204 // release FBO resources
2205 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2206 glDeleteRenderbuffersOES(1, &tname);
2207 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002208
Mathias Agopian42977342012-08-05 00:40:46 -07002209 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002210
Steve Block9d453682011-12-20 16:23:08 +00002211 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002212
Mathias Agopian74c40c02010-09-29 13:02:36 -07002213 return result;
2214}
2215
2216
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002217status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2218 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002219 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002220 uint32_t sw, uint32_t sh,
2221 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002222{
2223 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002224 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002225 return BAD_VALUE;
2226
2227 if (!GLExtensions::getInstance().haveFramebufferObject())
2228 return INVALID_OPERATION;
2229
2230 class MessageCaptureScreen : public MessageBase {
2231 SurfaceFlinger* flinger;
2232 DisplayID dpy;
2233 sp<IMemoryHeap>* heap;
2234 uint32_t* w;
2235 uint32_t* h;
2236 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002237 uint32_t sw;
2238 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002239 uint32_t minLayerZ;
2240 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002241 status_t result;
2242 public:
2243 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002244 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002245 uint32_t sw, uint32_t sh,
2246 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002247 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002248 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2249 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2250 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002251 {
2252 }
2253 status_t getResult() const {
2254 return result;
2255 }
2256 virtual bool handler() {
2257 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002258 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002259 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002260 return true;
2261 }
2262 };
2263
2264 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002265 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002266 status_t res = postMessageSync(msg);
2267 if (res == NO_ERROR) {
2268 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2269 }
2270 return res;
2271}
2272
2273// ---------------------------------------------------------------------------
2274
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002275SurfaceFlinger::LayerVector::LayerVector() {
2276}
2277
2278SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2279 : SortedVector<sp<LayerBase> >(rhs) {
2280}
2281
2282int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2283 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002284{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002285 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002286 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2287 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002288
2289 uint32_t ls = l->currentState().layerStack;
2290 uint32_t rs = r->currentState().layerStack;
2291 if (ls != rs)
2292 return ls - rs;
2293
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002294 uint32_t lz = l->currentState().z;
2295 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002296 if (lz != rz)
2297 return lz - rz;
2298
2299 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002300}
2301
2302// ---------------------------------------------------------------------------
2303
Mathias Agopian92a979a2012-08-02 18:32:23 -07002304SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2305 : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002306}
2307
2308// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309
Jamie Gennis9a78c902011-01-12 18:30:40 -08002310GraphicBufferAlloc::GraphicBufferAlloc() {}
2311
2312GraphicBufferAlloc::~GraphicBufferAlloc() {}
2313
2314sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002315 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002316 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2317 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002318 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002319 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002320 if (err == NO_MEMORY) {
2321 GraphicBuffer::dumpAllocationsToSystemLog();
2322 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002323 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002324 "failed (%s), handle=%p",
2325 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002326 return 0;
2327 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002328 return graphicBuffer;
2329}
2330
Jamie Gennis9a78c902011-01-12 18:30:40 -08002331// ---------------------------------------------------------------------------
2332
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002333}; // namespace android