blob: f17a7d289b315e97abcb9efa0b326bbb2f5a4c23 [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 Agopian1e260872012-08-08 18:35:12 -0700457 // TODO: this is here only for compatibility -- should go away eventually.
458 if (uint32_t(dpy) >= 1) {
Mathias Agopianc666cae2012-07-25 18:56:13 -0700459 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 Agopian1e260872012-08-08 18:35:12 -0700631 const int32_t id = hw->getDisplayId();
632 if (hwc.createWorkList(id, count) >= 0) {
633 HWComposer::LayerListIterator cur = hwc.begin(id);
634 const HWComposer::LayerListIterator end = hwc.end(id);
635 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
636 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700637
Mathias Agopian1e260872012-08-08 18:35:12 -0700638 if (CC_UNLIKELY(workListsDirty)) {
639 layer->setGeometry(hw, *cur);
640 if (mDebugDisableHWC || mDebugRegion) {
641 cur->setSkip(true);
642 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700643 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700644
Mathias Agopian1e260872012-08-08 18:35:12 -0700645 /*
646 * update the per-frame h/w composer data for each layer
647 * and build the transparent region of the FB
648 */
649 layer->setPerFrameData(hw, *cur);
650 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700651 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700652 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700653 status_t err = hwc.prepare();
654 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
655 }
656
657 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700658 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700659 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700660
Mathias Agopian87baae12012-07-31 12:38:26 -0700661 // transform the dirty region into this screen's coordinate space
Mathias Agopian42977342012-08-05 00:40:46 -0700662 const Transform& planeTransform(hw->getTransform());
Mathias Agopian87baae12012-07-31 12:38:26 -0700663 Region dirtyRegion;
664 if (repaintEverything) {
Mathias Agopian42977342012-08-05 00:40:46 -0700665 dirtyRegion.set(hw->bounds());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700666 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700667 dirtyRegion = planeTransform.transform(hw->dirtyRegion);
668 dirtyRegion.andSelf(hw->bounds());
Mathias Agopian87baae12012-07-31 12:38:26 -0700669 }
Mathias Agopian42977342012-08-05 00:40:46 -0700670 hw->dirtyRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700671
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700672 if (!dirtyRegion.isEmpty()) {
Mathias Agopian42977342012-08-05 00:40:46 -0700673 if (hw->canDraw()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700674 // repaint the framebuffer (if needed)
675 handleRepaint(hw, dirtyRegion);
676 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700677 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700678 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700679 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700680 }
681
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700682 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700683
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700684
685#if 1
Mathias Agopian4fec8732012-06-29 14:12:52 -0700686 // render to the external display if we have one
687 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
688 if (externalDisplaySurface != EGL_NO_SURFACE) {
689 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
690 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
691 externalDisplaySurface, externalDisplaySurface,
692 eglGetCurrentContext());
693
694 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
695
696 if (success) {
697 // redraw the screen entirely...
698 glDisable(GL_TEXTURE_EXTERNAL_OES);
699 glDisable(GL_TEXTURE_2D);
700 glClearColor(0,0,0,1);
701 glClear(GL_COLOR_BUFFER_BIT);
702 glMatrixMode(GL_MODELVIEW);
703 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700704
Mathias Agopian42977342012-08-05 00:40:46 -0700705 const sp<DisplayDevice>& hw(getDisplayDevice(0));
706 const Vector< sp<LayerBase> >& layers( hw->getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700707 const size_t count = layers.size();
708 for (size_t i=0 ; i<count ; ++i) {
709 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -0700710 layer->draw(hw);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711 }
712
713 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
714 ALOGE_IF(!success, "external display eglSwapBuffers failed");
715
Mathias Agopian42977342012-08-05 00:40:46 -0700716 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700717 }
718
719 success = eglMakeCurrent(eglGetCurrentDisplay(),
720 cur, cur, eglGetCurrentContext());
721
722 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
723 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700724#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700725
726}
727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728void SurfaceFlinger::postFramebuffer()
729{
Mathias Agopian841cde52012-03-01 15:44:37 -0800730 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800731
Mathias Agopiana44b0412011-10-16 18:46:35 -0700732 const nsecs_t now = systemTime();
733 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700734
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700735 HWComposer& hwc(getHwComposer());
736
Mathias Agopian92a979a2012-08-02 18:32:23 -0700737 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700738 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700739 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian42977342012-08-05 00:40:46 -0700740 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700741 const size_t count = currentLayers.size();
Mathias Agopian1e260872012-08-08 18:35:12 -0700742 const int32_t id = hw->getDisplayId();
743 HWComposer::LayerListIterator cur = hwc.begin(id);
744 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700745 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
746 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700747 layer->setAcquireFence(hw, *cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700748 }
749 }
Mathias Agopian42977342012-08-05 00:40:46 -0700750 hw->flip(hw->swapRegion);
751 hw->swapRegion.clear();
Jesse Hallc5c5a142012-07-02 16:49:28 -0700752 }
753
Jesse Hallef194142012-06-14 14:45:17 -0700754 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700755 // FIXME: eventually commit() won't take arguments
Mathias Agopian42977342012-08-05 00:40:46 -0700756 hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700757 }
758
Mathias Agopian92a979a2012-08-02 18:32:23 -0700759 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700760 sp<const DisplayDevice> hw(mDisplays[dpy]);
761 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700762 const size_t count = currentLayers.size();
763 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700764 int32_t id = hw->getDisplayId();
765 HWComposer::LayerListIterator cur = hwc.begin(id);
766 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700767 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700768 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700769 }
770 } else {
Mathias Agopian42977342012-08-05 00:40:46 -0700771 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700772 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700773 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700774 }
Jesse Hallef194142012-06-14 14:45:17 -0700775 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700776
777 // FIXME: we need to call eglSwapBuffers() on displays that have GL composition
Jamie Gennise8696a42012-01-15 18:54:57 -0800778 }
779
Mathias Agopiana44b0412011-10-16 18:46:35 -0700780 mLastSwapBufferTime = systemTime() - now;
781 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782}
783
Mathias Agopian87baae12012-07-31 12:38:26 -0700784void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785{
Mathias Agopian841cde52012-03-01 15:44:37 -0800786 ATRACE_CALL();
787
Mathias Agopianca4d3602011-05-19 15:38:14 -0700788 Mutex::Autolock _l(mStateLock);
789 const nsecs_t now = systemTime();
790 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791
Mathias Agopianca4d3602011-05-19 15:38:14 -0700792 // Here we're guaranteed that some transaction flags are set
793 // so we can call handleTransactionLocked() unconditionally.
794 // We call getTransactionFlags(), which will also clear the flags,
795 // with mStateLock held to guarantee that mCurrentState won't change
796 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700797
Mathias Agopianca4d3602011-05-19 15:38:14 -0700798 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
799 transactionFlags = getTransactionFlags(mask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700800 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700801
Mathias Agopianca4d3602011-05-19 15:38:14 -0700802 mLastTransactionTime = systemTime() - now;
803 mDebugInTransaction = 0;
804 invalidateHwcGeometry();
805 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700806}
807
Mathias Agopian87baae12012-07-31 12:38:26 -0700808void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700809{
810 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 const size_t count = currentLayers.size();
812
813 /*
814 * Traversal of the children
815 * (perform the transaction for each of them if needed)
816 */
817
818 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
819 if (layersNeedTransaction) {
820 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700821 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
823 if (!trFlags) continue;
824
825 const uint32_t flags = layer->doTransaction(0);
826 if (flags & Layer::eVisibleRegion)
827 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 }
829 }
830
831 /*
832 * Perform our own transaction if needed
833 */
834
835 if (transactionFlags & eTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700836 // here we take advantage of Vector's copy-on-write semantics to
837 // improve performance by skipping the transaction entirely when
838 // know that the lists are identical
839 const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays);
840 const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays);
841 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700843 const size_t cc = curr.size();
844 const size_t dc = draw.size();
845
846 // find the displays that were removed
847 // (ie: in drawing state but not in current state)
848 // also handle displays that changed
849 // (ie: displays that are in both lists)
850 for (size_t i=0 ; i<dc ; i++) {
851 if (curr.indexOfKey(draw[i].id) < 0) {
852 // in drawing state but not in current state
853 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700854 mDisplays.removeItem(draw[i].id);
855 } else {
856 ALOGW("trying to remove the main display");
857 }
858 } else {
859 // this display is in both lists. see if something changed.
860 const DisplayDeviceState& state(curr[i]);
861 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700862 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700863 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700864 }
865 if (curr[i].orientation != draw[i].orientation) {
Mathias Agopian42977342012-08-05 00:40:46 -0700866 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
867 disp->setOrientation(state.orientation);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700868 }
869 }
870 }
871
872 // find displays that were added
873 // (ie: in current state but not in drawing state)
874 for (size_t i=0 ; i<cc ; i++) {
875 if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) {
876 // FIXME: we need to pass the surface here
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700877 sp<DisplayDevice> disp = new DisplayDevice(this, curr[i].id, 0, 0, mEGLConfig);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700878 mDisplays.add(curr[i].id, disp);
879 }
880 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 }
882
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700883 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
884 // layers have been added
885 mVisibleRegionsDirty = true;
886 }
887
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 // some layers might have been removed, so
889 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700890 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700891 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700893 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700894 const size_t count = previousLayers.size();
895 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700896 const sp<LayerBase>& layer(previousLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700897 if (currentLayers.indexOf(layer) < 0) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700898 // this layer is not visible anymore
Mathias Agopian87baae12012-07-31 12:38:26 -0700899 // TODO: we could traverse the tree from front to back and
900 // compute the actual visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700901 // TODO: we could cache the transformed region
902 Layer::State front(layer->drawingState());
903 Region visibleReg = front.transform.transform(
904 Region(Rect(front.active.w, front.active.h)));
Mathias Agopian87baae12012-07-31 12:38:26 -0700905 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700906 }
907 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 }
910
911 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700912}
913
914void SurfaceFlinger::commitTransaction()
915{
916 if (!mLayersPendingRemoval.isEmpty()) {
917 // Notify removed layers now that they can't be drawn from
918 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
919 mLayersPendingRemoval[i]->onRemoved();
920 }
921 mLayersPendingRemoval.clear();
922 }
923
924 mDrawingState = mCurrentState;
925 mTransationPending = false;
926 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927}
928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -0700930 const LayerVector& currentLayers, uint32_t layerStack,
931 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932{
Mathias Agopian841cde52012-03-01 15:44:37 -0800933 ATRACE_CALL();
934
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 Region aboveOpaqueLayers;
936 Region aboveCoveredLayers;
937 Region dirty;
938
Mathias Agopian87baae12012-07-31 12:38:26 -0700939 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940
941 size_t i = currentLayers.size();
942 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700943 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944
945 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700946 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947
Mathias Agopian87baae12012-07-31 12:38:26 -0700948 // only consider the layers on the given later stack
949 if (s.layerStack != layerStack)
950 continue;
951
Mathias Agopianab028732010-03-16 16:41:46 -0700952 /*
953 * opaqueRegion: area of a surface that is fully opaque.
954 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700956
957 /*
958 * visibleRegion: area of a surface that is visible on screen
959 * and not fully transparent. This is essentially the layer's
960 * footprint minus the opaque regions above it.
961 * Areas covered by a translucent surface are considered visible.
962 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700964
965 /*
966 * coveredRegion: area of a surface that is covered by all
967 * visible regions above it (which includes the translucent areas).
968 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700970
971
972 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800973 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700974 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700975 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700977 if (!visibleRegion.isEmpty()) {
978 // Remove the transparent area from the visible region
979 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700980 Region transparentRegionScreen;
981 const Transform tr(s.transform);
982 if (tr.transformed()) {
983 if (tr.preserveRects()) {
984 // transform the transparent region
985 transparentRegionScreen = tr.transform(s.transparentRegion);
986 } else {
987 // transformation too complex, can't do the
988 // transparent region optimization.
989 transparentRegionScreen.clear();
990 }
991 } else {
992 transparentRegionScreen = s.transparentRegion;
993 }
994 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700995 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996
Mathias Agopianab028732010-03-16 16:41:46 -0700997 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700998 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700999 if (s.alpha==255 && !translucent &&
1000 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1001 // the opaque region is the layer's footprint
1002 opaqueRegion = visibleRegion;
1003 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 }
1005 }
1006
Mathias Agopianab028732010-03-16 16:41:46 -07001007 // Clip the covered region to the visible region
1008 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1009
1010 // Update aboveCoveredLayers for next (lower) layer
1011 aboveCoveredLayers.orSelf(visibleRegion);
1012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 // subtract the opaque region covered by the layers above us
1014 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015
1016 // compute this layer's dirty region
1017 if (layer->contentDirty) {
1018 // we need to invalidate the whole region
1019 dirty = visibleRegion;
1020 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001021 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 layer->contentDirty = false;
1023 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001024 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001025 * the exposed region consists of two components:
1026 * 1) what's VISIBLE now and was COVERED before
1027 * 2) what's EXPOSED now less what was EXPOSED before
1028 *
1029 * note that (1) is conservative, we start with the whole
1030 * visible region but only keep what used to be covered by
1031 * something -- which mean it may have been exposed.
1032 *
1033 * (2) handles areas that were not covered by anything but got
1034 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001035 */
Mathias Agopianab028732010-03-16 16:41:46 -07001036 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001037 const Region oldVisibleRegion = layer->visibleRegion;
1038 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001039 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1040 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 }
1042 dirty.subtractSelf(aboveOpaqueLayers);
1043
1044 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001045 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046
Mathias Agopianab028732010-03-16 16:41:46 -07001047 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 // Store the visible region is screen space
1051 layer->setVisibleRegion(visibleRegion);
1052 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 }
1054
Mathias Agopian87baae12012-07-31 12:38:26 -07001055 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
Mathias Agopian87baae12012-07-31 12:38:26 -07001058void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1059 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001060 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001061 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1062 if (hw->getLayerStack() == layerStack) {
1063 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001064 }
1065 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001066}
1067
1068void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001070 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001071 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001073 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001074
Mathias Agopian4fec8732012-06-29 14:12:52 -07001075 bool visibleRegions = false;
1076 const size_t count = currentLayers.size();
1077 sp<LayerBase> const* layers = currentLayers.array();
1078 for (size_t i=0 ; i<count ; i++) {
1079 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001080 const Region dirty(layer->latchBuffer(visibleRegions));
1081 Layer::State s(layer->drawingState());
1082 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001083 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001084
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001085 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086}
1087
Mathias Agopianad456f92011-01-13 17:53:01 -08001088void SurfaceFlinger::invalidateHwcGeometry()
1089{
1090 mHwWorkListDirty = true;
1091}
1092
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001093void SurfaceFlinger::handleRefresh()
1094{
1095 bool needInvalidate = false;
1096 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1097 const size_t count = currentLayers.size();
1098 for (size_t i=0 ; i<count ; i++) {
1099 const sp<LayerBase>& layer(currentLayers[i]);
1100 if (layer->onPreComposition()) {
1101 needInvalidate = true;
1102 }
1103 }
1104 if (needInvalidate) {
1105 signalLayerUpdate();
1106 }
1107}
1108
Mathias Agopian42977342012-08-05 00:40:46 -07001109void SurfaceFlinger::handleRepaint(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001110 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111{
Mathias Agopian841cde52012-03-01 15:44:37 -08001112 ATRACE_CALL();
1113
Mathias Agopian87baae12012-07-31 12:38:26 -07001114 Region dirtyRegion(inDirtyRegion);
1115
Mathias Agopianb8a55602009-06-26 19:06:36 -07001116 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001117 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118
Glenn Kasten99ed2242011-12-15 09:51:17 -08001119 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001120 debugFlashRegions(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 }
1122
Mathias Agopian42977342012-08-05 00:40:46 -07001123 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001124 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001125 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1126 // takes a rectangle, we must make sure to update that whole
1127 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001128 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001129 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001130 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001131 // We need to redraw the rectangle that will be updated
1132 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001133 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001134 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001135 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001136 } else {
1137 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001138 dirtyRegion.set(hw->bounds());
1139 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 }
1141 }
1142
Mathias Agopian87baae12012-07-31 12:38:26 -07001143 composeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001145 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1146 const size_t count = currentLayers.size();
1147 for (size_t i=0 ; i<count ; i++) {
1148 currentLayers[i]->onPostComposition();
1149 }
1150
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001151 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001152 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153}
1154
Mathias Agopian42977342012-08-05 00:40:46 -07001155void SurfaceFlinger::composeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001156{
Mathias Agopian86303202012-07-24 22:46:10 -07001157 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001158 int32_t id = hw->getDisplayId();
1159 HWComposer::LayerListIterator cur = hwc.begin(id);
1160 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001161
Mathias Agopian1e260872012-08-08 18:35:12 -07001162 const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001163 if (cur==end || fbLayerCount) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001164
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001165 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001166
1167 // set the frame buffer
1168 glMatrixMode(GL_MODELVIEW);
1169 glLoadIdentity();
1170
1171 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian1e260872012-08-08 18:35:12 -07001172 if (hwc.getLayerCount(id, HWC_OVERLAY)) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001173 // when using overlays, we assume a fully transparent framebuffer
1174 // NOTE: we could reduce how much we need to clear, for instance
1175 // remove where there are opaque FB layers. however, on some
1176 // GPUs doing a "clean slate" glClear might be more efficient.
1177 // We'll revisit later if needed.
1178 glClearColor(0, 0, 0, 0);
1179 glClear(GL_COLOR_BUFFER_BIT);
1180 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001181 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001182 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001183 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001184 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001185 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001186 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001187 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001188
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001189 /*
1190 * and then, render the layers targeted at the framebuffer
1191 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001192
Mathias Agopian42977342012-08-05 00:40:46 -07001193 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001194 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001195 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001196 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001197 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001198 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001199 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001200 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001201 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001202 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001203 // never clear the very first layer since we're
1204 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001205 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001206 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001207 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001208 continue;
1209 }
1210 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001211 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001212 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001213 if (cur != end) {
1214 ++cur;
1215 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001216 }
1217 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218}
1219
Mathias Agopian42977342012-08-05 00:40:46 -07001220void SurfaceFlinger::debugFlashRegions(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001221 const Region& dirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222{
Mathias Agopian42977342012-08-05 00:40:46 -07001223 const uint32_t flags = hw->getFlags();
1224 const int32_t height = hw->getHeight();
1225 if (hw->swapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001226 return;
1227 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001228
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001229 if (!(flags & DisplayDevice::SWAP_RECTANGLE)) {
1230 const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ?
Mathias Agopian42977342012-08-05 00:40:46 -07001231 dirtyRegion.bounds() : hw->bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001232 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001233 }
1234
Mathias Agopianc492e672011-10-18 14:49:27 -07001235 glDisable(GL_TEXTURE_EXTERNAL_OES);
1236 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238
Mathias Agopian0926f502009-05-04 14:17:04 -07001239 static int toggle = 0;
1240 toggle = 1 - toggle;
1241 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001242 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001243 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001244 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001245 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246
Mathias Agopian87baae12012-07-31 12:38:26 -07001247 Region::const_iterator it = dirtyRegion.begin();
1248 Region::const_iterator const end = dirtyRegion.end();
Mathias Agopian20f68782009-05-11 00:03:41 -07001249 while (it != end) {
1250 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001252 { r.left, height - r.top },
1253 { r.left, height - r.bottom },
1254 { r.right, height - r.bottom },
1255 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 };
1257 glVertexPointer(2, GL_FLOAT, 0, vertices);
1258 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1259 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001260
Mathias Agopian42977342012-08-05 00:40:46 -07001261 hw->flip(hw->swapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262
1263 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001264 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265}
1266
Mathias Agopian87baae12012-07-31 12:38:26 -07001267void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001269 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001270 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001271 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001272 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001273
1274 GLfloat vertices[4][2];
1275 glVertexPointer(2, GL_FLOAT, 0, vertices);
1276 Region::const_iterator it = region.begin();
1277 Region::const_iterator const end = region.end();
1278 while (it != end) {
1279 const Rect& r = *it++;
1280 vertices[0][0] = r.left;
1281 vertices[0][1] = r.top;
1282 vertices[1][0] = r.right;
1283 vertices[1][1] = r.top;
1284 vertices[2][0] = r.right;
1285 vertices[2][1] = r.bottom;
1286 vertices[3][0] = r.left;
1287 vertices[3][1] = r.bottom;
1288 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1289 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290}
1291
Mathias Agopian96f08192010-06-02 23:28:45 -07001292ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1293 const sp<LayerBaseClient>& lbc)
1294{
Mathias Agopian96f08192010-06-02 23:28:45 -07001295 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001296 size_t name = client->attachLayer(lbc);
1297
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001299 Mutex::Autolock _l(mStateLock);
1300 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001301
Mathias Agopian4f113742011-05-03 16:21:41 -07001302 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001303}
1304
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001305status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
1307 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001308 status_t err = purgatorizeLayer_l(layer);
1309 if (err == NO_ERROR)
1310 setTransactionFlags(eTransactionNeeded);
1311 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312}
1313
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001314status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315{
1316 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1317 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001318 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 return NO_ERROR;
1320 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001321 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322}
1323
Mathias Agopian9a112062009-04-17 19:36:26 -07001324status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1325{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001326 // First add the layer to the purgatory list, which makes sure it won't
1327 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001328 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001329 if (err >= 0) {
1330 mLayerPurgatory.add(layerBase);
1331 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001332
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001333 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001334
Mathias Agopian3d579642009-06-04 18:46:21 -07001335 // it's possible that we don't find a layer, because it might
1336 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001337 // from the user because there is a race between Client::destroySurface(),
1338 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001339 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1340}
1341
Mathias Agopiandea20b12011-05-03 17:04:02 -07001342uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1343{
1344 return android_atomic_release_load(&mTransactionFlags);
1345}
1346
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1348{
1349 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1350}
1351
Mathias Agopianbb641242010-05-18 17:06:55 -07001352uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353{
1354 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1355 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001356 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 }
1358 return old;
1359}
1360
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361
Mathias Agopian8b33f032012-07-24 20:43:54 -07001362void SurfaceFlinger::setTransactionState(
1363 const Vector<ComposerState>& state,
1364 const Vector<DisplayState>& displays,
1365 uint32_t flags)
1366{
Mathias Agopian698c0872011-06-28 19:09:31 -07001367 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001368
Mathias Agopian8b33f032012-07-24 20:43:54 -07001369 int orientation = eOrientationUnchanged;
1370 if (displays.size()) {
1371 // TODO: handle all displays
1372 orientation = displays[0].orientation;
1373 }
1374
Jamie Gennis28378392011-10-12 17:39:00 -07001375 uint32_t transactionFlags = 0;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001376 // FIXME: don't hardcode display id here
1377 if (mCurrentState.displays.valueFor(0).orientation != orientation) {
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001378 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001379 mCurrentState.displays.editValueFor(0).orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001380 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001381 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001382 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001383 orientation);
1384 }
1385 }
1386
Mathias Agopian698c0872011-06-28 19:09:31 -07001387 const size_t count = state.size();
1388 for (size_t i=0 ; i<count ; i++) {
1389 const ComposerState& s(state[i]);
1390 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001391 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001392 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001393
Mathias Agopian386aa982011-11-07 21:58:03 -08001394 if (transactionFlags) {
1395 // this triggers the transaction
1396 setTransactionFlags(transactionFlags);
1397
1398 // if this is a synchronous transaction, wait for it to take effect
1399 // before returning.
1400 if (flags & eSynchronous) {
1401 mTransationPending = true;
1402 }
1403 while (mTransationPending) {
1404 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1405 if (CC_UNLIKELY(err != NO_ERROR)) {
1406 // just in case something goes wrong in SF, return to the
1407 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001408 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001409 mTransationPending = false;
1410 break;
1411 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001412 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 }
1414}
1415
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001416sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001417 ISurfaceComposerClient::surface_data_t* params,
1418 const String8& name,
1419 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1421 uint32_t flags)
1422{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001423 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001424 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001425
1426 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001427 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001428 int(w), int(h));
1429 return surfaceHandle;
1430 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001431
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001432 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 switch (flags & eFXSurfaceMask) {
1434 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001435 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 break;
1437 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001438 // for now we treat Blur as Dim, until we can implement it
1439 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001441 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001443 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001444 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001445 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446 }
1447
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001448 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001449 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001450 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001451 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001453 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001454 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001455 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001456 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001457 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
1459
1460 return surfaceHandle;
1461}
1462
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001463sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001464 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001465 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001466 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467{
1468 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001469 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 case PIXEL_FORMAT_TRANSPARENT:
1471 case PIXEL_FORMAT_TRANSLUCENT:
1472 format = PIXEL_FORMAT_RGBA_8888;
1473 break;
1474 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001475#ifdef NO_RGBX_8888
1476 format = PIXEL_FORMAT_RGB_565;
1477#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001478 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001479#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 break;
1481 }
1482
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001483#ifdef NO_RGBX_8888
1484 if (format == PIXEL_FORMAT_RGBX_8888)
1485 format = PIXEL_FORMAT_RGBA_8888;
1486#endif
1487
Mathias Agopian96f08192010-06-02 23:28:45 -07001488 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001489 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001490 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001491 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001492 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493 }
1494 return layer;
1495}
1496
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001497sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001498 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001499 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500{
Mathias Agopian96f08192010-06-02 23:28:45 -07001501 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001502 return layer;
1503}
1504
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001505sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001506 const sp<Client>& client, DisplayID display,
1507 uint32_t w, uint32_t h, uint32_t flags)
1508{
1509 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 return layer;
1511}
1512
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001513status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514{
Mathias Agopian9a112062009-04-17 19:36:26 -07001515 /*
1516 * called by the window manager, when a surface should be marked for
1517 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001518 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001519 * The surface is removed from the current and drawing lists, but placed
1520 * in the purgatory queue, so it's not destroyed right-away (we need
1521 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001522 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001523
Mathias Agopian48d819a2009-09-10 19:41:18 -07001524 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001525 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001526 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001527
Mathias Agopian48d819a2009-09-10 19:41:18 -07001528 if (layer != 0) {
1529 err = purgatorizeLayer_l(layer);
1530 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001531 setTransactionFlags(eTransactionNeeded);
1532 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001533 }
1534 return err;
1535}
1536
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001537status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001538{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001539 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001540 status_t err = NO_ERROR;
1541 sp<LayerBaseClient> l(layer.promote());
1542 if (l != NULL) {
1543 Mutex::Autolock _l(mStateLock);
1544 err = removeLayer_l(l);
1545 if (err == NAME_NOT_FOUND) {
1546 // The surface wasn't in the current list, which means it was
1547 // removed already, which means it is in the purgatory,
1548 // and need to be removed from there.
1549 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001550 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001551 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001552 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001553 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001554 "error removing layer=%p (%s)", l.get(), strerror(-err));
1555 }
1556 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557}
1558
Mathias Agopian698c0872011-06-28 19:09:31 -07001559uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001560 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001561 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001564 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1565 if (layer != 0) {
1566 const uint32_t what = s.what;
1567 if (what & ePositionChanged) {
1568 if (layer->setPosition(s.x, s.y))
1569 flags |= eTraversalNeeded;
1570 }
1571 if (what & eLayerChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001572 // NOTE: index needs to be calculated before we update the state
Mathias Agopian698c0872011-06-28 19:09:31 -07001573 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1574 if (layer->setLayer(s.z)) {
1575 mCurrentState.layersSortedByZ.removeAt(idx);
1576 mCurrentState.layersSortedByZ.add(layer);
1577 // we need traversal (state changed)
1578 // AND transaction (list changed)
1579 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 }
1581 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001582 if (what & eSizeChanged) {
1583 if (layer->setSize(s.w, s.h)) {
1584 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001585 }
1586 }
1587 if (what & eAlphaChanged) {
1588 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1589 flags |= eTraversalNeeded;
1590 }
1591 if (what & eMatrixChanged) {
1592 if (layer->setMatrix(s.matrix))
1593 flags |= eTraversalNeeded;
1594 }
1595 if (what & eTransparentRegionChanged) {
1596 if (layer->setTransparentRegionHint(s.transparentRegion))
1597 flags |= eTraversalNeeded;
1598 }
1599 if (what & eVisibilityChanged) {
1600 if (layer->setFlags(s.flags, s.mask))
1601 flags |= eTraversalNeeded;
1602 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001603 if (what & eCropChanged) {
1604 if (layer->setCrop(s.crop))
1605 flags |= eTraversalNeeded;
1606 }
Mathias Agopian87855782012-07-24 21:41:09 -07001607 if (what & eLayerStackChanged) {
Mathias Agopianbe246f82012-07-31 22:59:38 -07001608 // NOTE: index needs to be calculated before we update the state
1609 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1610 if (layer->setLayerStack(s.layerStack)) {
1611 mCurrentState.layersSortedByZ.removeAt(idx);
1612 mCurrentState.layersSortedByZ.add(layer);
1613 // we need traversal (state changed)
1614 // AND transaction (list changed)
1615 flags |= eTransactionNeeded|eTraversalNeeded;
1616 }
Mathias Agopian87855782012-07-24 21:41:09 -07001617 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001619 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620}
1621
Mathias Agopianb60314a2012-04-10 22:09:54 -07001622// ---------------------------------------------------------------------------
1623
1624void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001625 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001626 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001627 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001628 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001629 mEventThread->onScreenAcquired();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630}
1631
Mathias Agopianb60314a2012-04-10 22:09:54 -07001632void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001633 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001634 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1635 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001636 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001637 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001638 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001639 // from this point on, SF will stop drawing
1640 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641}
1642
Colin Cross8e533062012-06-07 13:17:52 -07001643void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001644 class MessageScreenAcquired : public MessageBase {
1645 SurfaceFlinger* flinger;
1646 public:
1647 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1648 virtual bool handler() {
1649 flinger->onScreenAcquired();
1650 return true;
1651 }
1652 };
1653 sp<MessageBase> msg = new MessageScreenAcquired(this);
1654 postMessageSync(msg);
1655}
1656
Colin Cross8e533062012-06-07 13:17:52 -07001657void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001658 class MessageScreenReleased : public MessageBase {
1659 SurfaceFlinger* flinger;
1660 public:
1661 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1662 virtual bool handler() {
1663 flinger->onScreenReleased();
1664 return true;
1665 }
1666 };
1667 sp<MessageBase> msg = new MessageScreenReleased(this);
1668 postMessageSync(msg);
1669}
1670
1671// ---------------------------------------------------------------------------
1672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1674{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001675 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 char buffer[SIZE];
1677 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001678
1679 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 snprintf(buffer, SIZE, "Permission Denial: "
1681 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1682 IPCThreadState::self()->getCallingPid(),
1683 IPCThreadState::self()->getCallingUid());
1684 result.append(buffer);
1685 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001686 // Try to get the main lock, but don't insist if we can't
1687 // (this would indicate SF is stuck, but we want to be able to
1688 // print something in dumpsys).
1689 int retry = 3;
1690 while (mStateLock.tryLock()<0 && --retry>=0) {
1691 usleep(1000000);
1692 }
1693 const bool locked(retry >= 0);
1694 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001695 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001696 "SurfaceFlinger appears to be unresponsive, "
1697 "dumping anyways (no locks held)\n");
1698 result.append(buffer);
1699 }
1700
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001701 bool dumpAll = true;
1702 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001703 size_t numArgs = args.size();
1704 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001705 if ((index < numArgs) &&
1706 (args[index] == String16("--list"))) {
1707 index++;
1708 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001709 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001710 }
1711
1712 if ((index < numArgs) &&
1713 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001714 index++;
1715 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001716 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001717 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001718
1719 if ((index < numArgs) &&
1720 (args[index] == String16("--latency-clear"))) {
1721 index++;
1722 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001723 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001724 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001726
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001727 if (dumpAll) {
1728 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001729 }
1730
Mathias Agopian9795c422009-08-26 16:36:26 -07001731 if (locked) {
1732 mStateLock.unlock();
1733 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 }
1735 write(fd, result.string(), result.size());
1736 return NO_ERROR;
1737}
1738
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001739void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1740 String8& result, char* buffer, size_t SIZE) const
1741{
1742 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1743 const size_t count = currentLayers.size();
1744 for (size_t i=0 ; i<count ; i++) {
1745 const sp<LayerBase>& layer(currentLayers[i]);
1746 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1747 result.append(buffer);
1748 }
1749}
1750
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001751void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1752 String8& result, char* buffer, size_t SIZE) const
1753{
1754 String8 name;
1755 if (index < args.size()) {
1756 name = String8(args[index]);
1757 index++;
1758 }
1759
1760 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1761 const size_t count = currentLayers.size();
1762 for (size_t i=0 ; i<count ; i++) {
1763 const sp<LayerBase>& layer(currentLayers[i]);
1764 if (name.isEmpty()) {
1765 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1766 result.append(buffer);
1767 }
1768 if (name.isEmpty() || (name == layer->getName())) {
1769 layer->dumpStats(result, buffer, SIZE);
1770 }
1771 }
1772}
1773
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001774void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1775 String8& result, char* buffer, size_t SIZE) const
1776{
1777 String8 name;
1778 if (index < args.size()) {
1779 name = String8(args[index]);
1780 index++;
1781 }
1782
1783 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1784 const size_t count = currentLayers.size();
1785 for (size_t i=0 ; i<count ; i++) {
1786 const sp<LayerBase>& layer(currentLayers[i]);
1787 if (name.isEmpty() || (name == layer->getName())) {
1788 layer->clearStats();
1789 }
1790 }
1791}
1792
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001793void SurfaceFlinger::dumpAllLocked(
1794 String8& result, char* buffer, size_t SIZE) const
1795{
1796 // figure out if we're stuck somewhere
1797 const nsecs_t now = systemTime();
1798 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1799 const nsecs_t inTransaction(mDebugInTransaction);
1800 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1801 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1802
1803 /*
1804 * Dump the visible layer list
1805 */
1806 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1807 const size_t count = currentLayers.size();
1808 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1809 result.append(buffer);
1810 for (size_t i=0 ; i<count ; i++) {
1811 const sp<LayerBase>& layer(currentLayers[i]);
1812 layer->dump(result, buffer, SIZE);
1813 }
1814
1815 /*
1816 * Dump the layers in the purgatory
1817 */
1818
1819 const size_t purgatorySize = mLayerPurgatory.size();
1820 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1821 result.append(buffer);
1822 for (size_t i=0 ; i<purgatorySize ; i++) {
1823 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1824 layer->shortDump(result, buffer, SIZE);
1825 }
1826
1827 /*
1828 * Dump SurfaceFlinger global state
1829 */
1830
1831 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1832 result.append(buffer);
1833
Mathias Agopian888c8222012-08-04 21:10:38 -07001834 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001835 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001836 const GLExtensions& extensions(GLExtensions::getInstance());
1837 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1838 extensions.getVendor(),
1839 extensions.getRenderer(),
1840 extensions.getVersion());
1841 result.append(buffer);
1842
1843 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001844 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001845 result.append(buffer);
1846
1847 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1848 result.append(buffer);
1849
Mathias Agopian42977342012-08-05 00:40:46 -07001850 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001851 snprintf(buffer, SIZE,
1852 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07001853 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001854 result.append(buffer);
1855 snprintf(buffer, SIZE,
1856 " last eglSwapBuffers() time: %f us\n"
1857 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001858 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001859 " refresh-rate : %f fps\n"
1860 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001861 " y-dpi : %f\n"
1862 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001863 mLastSwapBufferTime/1000.0,
1864 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001865 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07001866 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian42977342012-08-05 00:40:46 -07001867 hw->getDpiX(),
1868 hw->getDpiY(),
1869 hw->getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001870 result.append(buffer);
1871
1872 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1873 inSwapBuffersDuration/1000.0);
1874 result.append(buffer);
1875
1876 snprintf(buffer, SIZE, " transaction time: %f us\n",
1877 inTransactionDuration/1000.0);
1878 result.append(buffer);
1879
1880 /*
1881 * VSYNC state
1882 */
1883 mEventThread->dump(result, buffer, SIZE);
1884
1885 /*
1886 * Dump HWComposer state
1887 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001888 snprintf(buffer, SIZE, "h/w composer state:\n");
1889 result.append(buffer);
1890 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1891 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1892 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1893 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07001894 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001895
1896 /*
1897 * Dump gralloc state
1898 */
1899 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1900 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07001901 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001902}
1903
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904status_t SurfaceFlinger::onTransact(
1905 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1906{
1907 switch (code) {
1908 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001909 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07001912 case BLANK:
1913 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 {
1915 // codes that require permission check
1916 IPCThreadState* ipc = IPCThreadState::self();
1917 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001918 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001919 if ((uid != AID_GRAPHICS) &&
1920 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001921 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001922 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1923 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001925 break;
1926 }
1927 case CAPTURE_SCREEN:
1928 {
1929 // codes that require permission check
1930 IPCThreadState* ipc = IPCThreadState::self();
1931 const int pid = ipc->getCallingPid();
1932 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001933 if ((uid != AID_GRAPHICS) &&
1934 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001935 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001936 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1937 return PERMISSION_DENIED;
1938 }
1939 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 }
1941 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001942
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1944 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001945 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001946 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001947 IPCThreadState* ipc = IPCThreadState::self();
1948 const int pid = ipc->getCallingPid();
1949 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001950 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001951 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 return PERMISSION_DENIED;
1953 }
1954 int n;
1955 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001956 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001957 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958 return NO_ERROR;
1959 case 1002: // SHOW_UPDATES
1960 n = data.readInt32();
1961 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001962 invalidateHwcGeometry();
1963 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001966 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001967 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001969 case 1005:{ // force transaction
1970 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1971 return NO_ERROR;
1972 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001973 case 1006:{ // send empty update
1974 signalRefresh();
1975 return NO_ERROR;
1976 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001977 case 1008: // toggle use of hw composer
1978 n = data.readInt32();
1979 mDebugDisableHWC = n ? 1 : 0;
1980 invalidateHwcGeometry();
1981 repaintEverything();
1982 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001983 case 1009: // toggle use of transform hint
1984 n = data.readInt32();
1985 mDebugDisableTransformHint = n ? 1 : 0;
1986 invalidateHwcGeometry();
1987 repaintEverything();
1988 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001989 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001990 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001991 reply->writeInt32(0);
1992 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001993 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001994 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995 return NO_ERROR;
1996 case 1013: {
1997 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07001998 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
1999 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002000 }
2001 return NO_ERROR;
2002 }
2003 }
2004 return err;
2005}
2006
Mathias Agopian53331da2011-08-22 21:44:41 -07002007void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002008 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002009 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002010}
2011
Mathias Agopian59119e62010-10-11 12:37:43 -07002012// ---------------------------------------------------------------------------
2013
Mathias Agopian118d0242011-10-13 16:02:48 -07002014status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2015 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2016{
2017 Mutex::Autolock _l(mStateLock);
2018 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2019}
2020
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2022 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002023{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002024 ATRACE_CALL();
2025
Mathias Agopian59119e62010-10-11 12:37:43 -07002026 if (!GLExtensions::getInstance().haveFramebufferObject())
2027 return INVALID_OPERATION;
2028
2029 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002030 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2031 const uint32_t hw_w = hw->getWidth();
2032 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002033 GLfloat u = 1;
2034 GLfloat v = 1;
2035
2036 // make sure to clear all GL error flags
2037 while ( glGetError() != GL_NO_ERROR ) ;
2038
2039 // create a FBO
2040 GLuint name, tname;
2041 glGenTextures(1, &tname);
2042 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002043 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2044 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2046 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002047 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002048 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002049 GLint tw = (2 << (31 - clz(hw_w)));
2050 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2052 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002053 u = GLfloat(hw_w) / tw;
2054 v = GLfloat(hw_h) / th;
2055 }
2056 glGenFramebuffersOES(1, &name);
2057 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002058 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2059 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002060
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002061 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002062 glDisable(GL_TEXTURE_EXTERNAL_OES);
2063 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 glClearColor(0,0,0,1);
2065 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002066 glMatrixMode(GL_MODELVIEW);
2067 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002068 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002069 const size_t count = layers.size();
2070 for (size_t i=0 ; i<count ; ++i) {
2071 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002072 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002073 }
2074
Mathias Agopian42977342012-08-05 00:40:46 -07002075 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002076
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002077 // back to main framebuffer
2078 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002079 glDeleteFramebuffersOES(1, &name);
2080
2081 *textureName = tname;
2082 *uOut = u;
2083 *vOut = v;
2084 return NO_ERROR;
2085}
2086
2087// ---------------------------------------------------------------------------
2088
Mathias Agopian74c40c02010-09-29 13:02:36 -07002089status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2090 sp<IMemoryHeap>* heap,
2091 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002092 uint32_t sw, uint32_t sh,
2093 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002094{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002095 ATRACE_CALL();
2096
Mathias Agopian74c40c02010-09-29 13:02:36 -07002097 status_t result = PERMISSION_DENIED;
2098
2099 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002100 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002101 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002102 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002103
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002104 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002105 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002106 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002107
2108 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002109 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2110 const uint32_t hw_w = hw->getWidth();
2111 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002112
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002113 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002114 if (hw->getSecureLayerVisible()) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002115 return PERMISSION_DENIED;
2116 }
2117
2118 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002119 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002120 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002121
2122 sw = (!sw) ? hw_w : sw;
2123 sh = (!sh) ? hw_h : sh;
2124 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002125 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002126
Steve Block9d453682011-12-20 16:23:08 +00002127 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002128 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002129
Mathias Agopian74c40c02010-09-29 13:02:36 -07002130 // make sure to clear all GL error flags
2131 while ( glGetError() != GL_NO_ERROR ) ;
2132
2133 // create a FBO
2134 GLuint name, tname;
2135 glGenRenderbuffersOES(1, &tname);
2136 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2137 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002138
Mathias Agopian74c40c02010-09-29 13:02:36 -07002139 glGenFramebuffersOES(1, &name);
2140 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2141 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2142 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2143
2144 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002145
Mathias Agopian74c40c02010-09-29 13:02:36 -07002146 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2147
2148 // invert everything, b/c glReadPixel() below will invert the FB
2149 glViewport(0, 0, sw, sh);
2150 glMatrixMode(GL_PROJECTION);
2151 glPushMatrix();
2152 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002153 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002154 glMatrixMode(GL_MODELVIEW);
2155
2156 // redraw the screen entirely...
2157 glClearColor(0,0,0,1);
2158 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002159
Jamie Gennis9575f602011-10-07 14:51:16 -07002160 const LayerVector& layers(mDrawingState.layersSortedByZ);
2161 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002162 for (size_t i=0 ; i<count ; ++i) {
2163 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002164 const uint32_t flags = layer->drawingState().flags;
2165 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2166 const uint32_t z = layer->drawingState().z;
2167 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002168 if (filtering) layer->setFiltering(true);
2169 layer->draw(hw);
2170 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002171 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002172 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002173 }
2174
Mathias Agopian74c40c02010-09-29 13:02:36 -07002175 // check for errors and return screen capture
2176 if (glGetError() != GL_NO_ERROR) {
2177 // error while rendering
2178 result = INVALID_OPERATION;
2179 } else {
2180 // allocate shared memory large enough to hold the
2181 // screen capture
2182 sp<MemoryHeapBase> base(
2183 new MemoryHeapBase(size, 0, "screen-capture") );
2184 void* const ptr = base->getBase();
2185 if (ptr) {
2186 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002187 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002188 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2189 if (glGetError() == GL_NO_ERROR) {
2190 *heap = base;
2191 *w = sw;
2192 *h = sh;
2193 *f = PIXEL_FORMAT_RGBA_8888;
2194 result = NO_ERROR;
2195 }
2196 } else {
2197 result = NO_MEMORY;
2198 }
2199 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002200 glViewport(0, 0, hw_w, hw_h);
2201 glMatrixMode(GL_PROJECTION);
2202 glPopMatrix();
2203 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002204 } else {
2205 result = BAD_VALUE;
2206 }
2207
2208 // release FBO resources
2209 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2210 glDeleteRenderbuffersOES(1, &tname);
2211 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002212
Mathias Agopian42977342012-08-05 00:40:46 -07002213 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002214
Steve Block9d453682011-12-20 16:23:08 +00002215 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002216
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217 return result;
2218}
2219
2220
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002221status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2222 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002223 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002224 uint32_t sw, uint32_t sh,
2225 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002226{
2227 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002228 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002229 return BAD_VALUE;
2230
2231 if (!GLExtensions::getInstance().haveFramebufferObject())
2232 return INVALID_OPERATION;
2233
2234 class MessageCaptureScreen : public MessageBase {
2235 SurfaceFlinger* flinger;
2236 DisplayID dpy;
2237 sp<IMemoryHeap>* heap;
2238 uint32_t* w;
2239 uint32_t* h;
2240 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241 uint32_t sw;
2242 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002243 uint32_t minLayerZ;
2244 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002245 status_t result;
2246 public:
2247 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002248 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002249 uint32_t sw, uint32_t sh,
2250 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002251 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002252 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2253 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2254 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002255 {
2256 }
2257 status_t getResult() const {
2258 return result;
2259 }
2260 virtual bool handler() {
2261 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002262 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002263 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002264 return true;
2265 }
2266 };
2267
2268 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002269 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002270 status_t res = postMessageSync(msg);
2271 if (res == NO_ERROR) {
2272 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2273 }
2274 return res;
2275}
2276
2277// ---------------------------------------------------------------------------
2278
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002279SurfaceFlinger::LayerVector::LayerVector() {
2280}
2281
2282SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2283 : SortedVector<sp<LayerBase> >(rhs) {
2284}
2285
2286int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2287 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002288{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002289 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002290 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2291 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002292
2293 uint32_t ls = l->currentState().layerStack;
2294 uint32_t rs = r->currentState().layerStack;
2295 if (ls != rs)
2296 return ls - rs;
2297
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002298 uint32_t lz = l->currentState().z;
2299 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002300 if (lz != rz)
2301 return lz - rz;
2302
2303 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002304}
2305
2306// ---------------------------------------------------------------------------
2307
Mathias Agopian92a979a2012-08-02 18:32:23 -07002308SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2309 : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002310}
2311
2312// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002313
Jamie Gennis9a78c902011-01-12 18:30:40 -08002314GraphicBufferAlloc::GraphicBufferAlloc() {}
2315
2316GraphicBufferAlloc::~GraphicBufferAlloc() {}
2317
2318sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002319 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002320 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2321 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002322 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002323 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002324 if (err == NO_MEMORY) {
2325 GraphicBuffer::dumpAllocationsToSystemLog();
2326 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002327 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002328 "failed (%s), handle=%p",
2329 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002330 return 0;
2331 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002332 return graphicBuffer;
2333}
2334
Jamie Gennis9a78c902011-01-12 18:30:40 -08002335// ---------------------------------------------------------------------------
2336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002337}; // namespace android