blob: ed687e3132032fc5887aa0a963949736e481655b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
39#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/SurfaceTextureClient.h>
41
42#include <ui/GraphicBufferAllocator.h>
43#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/String8.h>
46#include <utils/String16.h>
47#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080048#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian921e6ac2012-07-23 23:11:29 -070050#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
52#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080053#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070055#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070057#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070060#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana4912602012-07-12 14:25:33 -070063#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070064#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070086 mRepaintEverything(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -070098 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
113
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 ALOGI_IF(mDebugRegion, "showupdates enabled");
115 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800118void SurfaceFlinger::onFirstRef()
119{
120 mEventQueue.init(this);
121
122 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
123
124 // Wait for the main thread to be done with its initialization
125 mReadyToRunBarrier.wait();
126}
127
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::~SurfaceFlinger()
130{
Mathias Agopiana4912602012-07-12 14:25:33 -0700131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800136void SurfaceFlinger::binderDied(const wp<IBinder>& who)
137{
138 // the window manager died on us. prepare its eulogy.
139
Andy McFadden13a082e2012-08-24 10:16:42 -0700140 // restore initial conditions (default device unblank, etc)
141 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142
143 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700144 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145}
146
Mathias Agopian7e27f052010-05-28 14:22:23 -0700147sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148{
Mathias Agopian96f08192010-06-02 23:28:45 -0700149 sp<ISurfaceComposerClient> bclient;
150 sp<Client> client(new Client(this));
151 status_t err = client->initCheck();
152 if (err == NO_ERROR) {
153 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 return bclient;
156}
157
Jamie Gennis0bceb842012-08-23 20:19:38 -0700158int32_t SurfaceFlinger::chooseNewDisplayIdLocked() const
159{
160 int32_t id = DisplayDevice::DISPLAY_ID_COUNT - 1;
161 bool available;
162 do {
163 id++;
164 available = true;
165 for (size_t i = 0; i < mCurrentState.displays.size(); i++) {
166 const DisplayDeviceState& dds(mCurrentState.displays.valueAt(i));
167 if (dds.id == id) {
168 available = false;
169 break;
170 }
171 }
172 } while (!available);
173 return id;
174}
175
Mathias Agopiane57f2922012-08-09 16:29:12 -0700176sp<IBinder> SurfaceFlinger::createDisplay()
177{
178 class DisplayToken : public BBinder {
179 sp<SurfaceFlinger> flinger;
180 virtual ~DisplayToken() {
181 // no more references, this display must be terminated
182 Mutex::Autolock _l(flinger->mStateLock);
183 flinger->mCurrentState.displays.removeItem(this);
184 flinger->setTransactionFlags(eDisplayTransactionNeeded);
185 }
186 public:
187 DisplayToken(const sp<SurfaceFlinger>& flinger)
188 : flinger(flinger) {
189 }
190 };
191
192 sp<BBinder> token = new DisplayToken(this);
193
194 Mutex::Autolock _l(mStateLock);
Jamie Gennis0bceb842012-08-23 20:19:38 -0700195 int32_t id = chooseNewDisplayIdLocked();
196 DisplayDeviceState info(id);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700197 mCurrentState.displays.add(token, info);
198
199 return token;
200}
201
202sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
203 if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
204 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
205 return NULL;
206 }
207 return mDefaultDisplays[id];
208}
209
Jamie Gennis9a78c902011-01-12 18:30:40 -0800210sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
211{
212 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
213 return gba;
214}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216void SurfaceFlinger::bootFinished()
217{
218 const nsecs_t now = systemTime();
219 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000220 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700221 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700222
223 // wait patiently for the window manager death
224 const String16 name("window");
225 sp<IBinder> window(defaultServiceManager()->getService(name));
226 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700227 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700228 }
229
230 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700231 // formerly we would just kill the process, but we now ask it to exit so it
232 // can choose where to stop the animation.
233 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234}
235
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700236void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
237 class MessageDestroyGLTexture : public MessageBase {
238 GLuint texture;
239 public:
240 MessageDestroyGLTexture(GLuint texture)
241 : texture(texture) {
242 }
243 virtual bool handler() {
244 glDeleteTextures(1, &texture);
245 return true;
246 }
247 };
248 postMessageAsync(new MessageDestroyGLTexture(texture));
249}
250
Mathias Agopiana4912602012-07-12 14:25:33 -0700251status_t SurfaceFlinger::selectConfigForPixelFormat(
252 EGLDisplay dpy,
253 EGLint const* attrs,
254 PixelFormat format,
255 EGLConfig* outConfig)
256{
257 EGLConfig config = NULL;
258 EGLint numConfigs = -1, n=0;
259 eglGetConfigs(dpy, NULL, 0, &numConfigs);
260 EGLConfig* const configs = new EGLConfig[numConfigs];
261 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
262 for (int i=0 ; i<n ; i++) {
263 EGLint nativeVisualId = 0;
264 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
265 if (nativeVisualId>0 && format == nativeVisualId) {
266 *outConfig = configs[i];
267 delete [] configs;
268 return NO_ERROR;
269 }
270 }
271 delete [] configs;
272 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273}
274
Mathias Agopiana4912602012-07-12 14:25:33 -0700275EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
276 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
277 // it is to be used with WIFI displays
278 EGLConfig config;
279 EGLint dummy;
280 status_t err;
281 EGLint attribs[] = {
282 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
283 EGL_RECORDABLE_ANDROID, EGL_TRUE,
284 EGL_NONE
285 };
286 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
287 if (err) {
288 // maybe we failed because of EGL_RECORDABLE_ANDROID
289 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
290 attribs[2] = EGL_NONE;
291 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
292 }
293 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
294 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
295 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
296 }
297 return config;
298}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
301 // Also create our EGLContext
302 EGLint contextAttributes[] = {
303#ifdef EGL_IMG_context_priority
304#ifdef HAS_CONTEXT_PRIORITY
305#warning "using EGL_IMG_context_priority"
306 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
307#endif
308#endif
309 EGL_NONE, EGL_NONE
310 };
311 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
312 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
313 return ctxt;
314}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
Mathias Agopiana4912602012-07-12 14:25:33 -0700316void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
317 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
318 if (!result) {
319 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
320 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 }
322
Mathias Agopiana4912602012-07-12 14:25:33 -0700323 GLExtensions& extensions(GLExtensions::getInstance());
324 extensions.initWithGLStrings(
325 glGetString(GL_VENDOR),
326 glGetString(GL_RENDERER),
327 glGetString(GL_VERSION),
328 glGetString(GL_EXTENSIONS),
329 eglQueryString(display, EGL_VENDOR),
330 eglQueryString(display, EGL_VERSION),
331 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700332
Mathias Agopiana4912602012-07-12 14:25:33 -0700333 EGLint w, h;
334 eglQuerySurface(display, surface, EGL_WIDTH, &w);
335 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700336
Mathias Agopiana4912602012-07-12 14:25:33 -0700337 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
338 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700341 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 glShadeModel(GL_FLAT);
344 glDisable(GL_DITHER);
345 glDisable(GL_CULL_FACE);
346
Mathias Agopiana4912602012-07-12 14:25:33 -0700347 struct pack565 {
348 inline uint16_t operator() (int r, int g, int b) const {
349 return (r<<11)|(g<<5)|b;
350 }
351 } pack565;
352
Jamie Gennis9575f602011-10-07 14:51:16 -0700353 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
354 glGenTextures(1, &mProtectedTexName);
355 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
356 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
357 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
358 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
359 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
360 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
361 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362
363 glViewport(0, 0, w, h);
364 glMatrixMode(GL_PROJECTION);
365 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700366 // put the origin in the left-bottom corner
367 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 -0800368
Mathias Agopiana4912602012-07-12 14:25:33 -0700369 // print some debugging info
370 EGLint r,g,b,a;
371 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
372 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
373 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
374 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
375 ALOGI("EGL informations:");
376 ALOGI("vendor : %s", extensions.getEglVendor());
377 ALOGI("version : %s", extensions.getEglVersion());
378 ALOGI("extensions: %s", extensions.getEglExtension());
379 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
380 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
381 ALOGI("OpenGL ES informations:");
382 ALOGI("vendor : %s", extensions.getVendor());
383 ALOGI("renderer : %s", extensions.getRenderer());
384 ALOGI("version : %s", extensions.getVersion());
385 ALOGI("extensions: %s", extensions.getExtension());
386 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
387 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
388}
389
Mathias Agopiana4912602012-07-12 14:25:33 -0700390status_t SurfaceFlinger::readyToRun()
391{
392 ALOGI( "SurfaceFlinger's main thread ready to run. "
393 "Initializing graphics H/W...");
394
Mathias Agopiana4912602012-07-12 14:25:33 -0700395 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700396 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
397 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700398
399 // Initialize the main display
400 // create native window to main display
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700401 sp<FramebufferSurface> fbs = FramebufferSurface::create();
402 if (fbs == NULL) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700403 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
404 exit(0);
405 }
406
Mathias Agopiane57f2922012-08-09 16:29:12 -0700407 sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
408 static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700409
Mathias Agopiana4912602012-07-12 14:25:33 -0700410 // initialize the config and context
411 int format;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700412 ANativeWindow* const anw = stc.get();
413 anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700414 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
415 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700416
417 // initialize our main display hardware
Mathias Agopiane57f2922012-08-09 16:29:12 -0700418
419 for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
420 mDefaultDisplays[i] = new BBinder();
421 mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
422 }
423 sp<DisplayDevice> hw = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700424 DisplayDevice::DISPLAY_ID_MAIN, HWC_DISPLAY_PRIMARY,
425 anw, fbs, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700426 mDisplays.add(hw->getDisplayId(), hw);
Mathias Agopiana4912602012-07-12 14:25:33 -0700427
428 // initialize OpenGL ES
Mathias Agopian42977342012-08-05 00:40:46 -0700429 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700430 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800431
Mathias Agopian028508c2012-07-25 21:12:12 -0700432 // start the EventThread
433 mEventThread = new EventThread(this);
434 mEventQueue.setEventThread(mEventThread);
435
Mathias Agopian86303202012-07-24 22:46:10 -0700436 // initialize the H/W composer
Mathias Agopian8b736f12012-08-13 17:54:26 -0700437 mHwc = new HWComposer(this,
438 *static_cast<HWComposer::EventHandler *>(this),
439 fbs->getFbHal());
Mathias Agopian92a979a2012-08-02 18:32:23 -0700440
441 // initialize our drawing state
442 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700443
Mathias Agopiana4912602012-07-12 14:25:33 -0700444 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800445 mReadyToRunBarrier.open();
446
Andy McFadden13a082e2012-08-24 10:16:42 -0700447 // set initial conditions (e.g. unblank default device)
448 initializeDisplays();
449
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700450 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700451 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 return NO_ERROR;
454}
455
Mathias Agopiana67e4182012-06-19 17:26:12 -0700456void SurfaceFlinger::startBootAnim() {
457 // start boot animation
458 property_set("service.bootanim.exit", "0");
459 property_set("ctl.start", "bootanim");
460}
461
Mathias Agopiana4912602012-07-12 14:25:33 -0700462uint32_t SurfaceFlinger::getMaxTextureSize() const {
463 return mMaxTextureSize;
464}
465
466uint32_t SurfaceFlinger::getMaxViewportDims() const {
467 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
468 mMaxViewportDims[0] : mMaxViewportDims[1];
469}
470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800472
Jamie Gennis582270d2011-08-17 18:19:00 -0700473bool SurfaceFlinger::authenticateSurfaceTexture(
474 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800475 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700476 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800477
478 // Check the visible layer list for the ISurface
479 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
480 size_t count = currentLayers.size();
481 for (size_t i=0 ; i<count ; i++) {
482 const sp<LayerBase>& layer(currentLayers[i]);
483 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700484 if (lbc != NULL) {
485 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
486 if (lbcBinder == surfaceTextureBinder) {
487 return true;
488 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800489 }
490 }
491
492 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700493 // SurfaceTexture gets destroyed before all the clients are done using it,
494 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800495 // will instead fail later on when the client tries to use the surface,
496 // which should be reported as "surface XYZ returned an -ENODEV". The
497 // purgatorized layers are no less authentic than the visible ones, so this
498 // should not cause any harm.
499 size_t purgatorySize = mLayerPurgatory.size();
500 for (size_t i=0 ; i<purgatorySize ; i++) {
501 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
502 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700503 if (lbc != NULL) {
504 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
505 if (lbcBinder == surfaceTextureBinder) {
506 return true;
507 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800508 }
509 }
510
511 return false;
512}
513
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700514status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
515 // TODO: this is mostly here only for compatibility
516 // the display size is needed but the display metrics should come from elsewhere
517 if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
518 // TODO: additional displays not yet supported
Mathias Agopianc666cae2012-07-25 18:56:13 -0700519 return BAD_INDEX;
520 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700521
522 const HWComposer& hwc(getHwComposer());
523 float xdpi = hwc.getDpiX();
524 float ydpi = hwc.getDpiY();
525
526 // TODO: Not sure if display density should handled by SF any longer
527 class Density {
528 static int getDensityFromProperty(char const* propName) {
529 char property[PROPERTY_VALUE_MAX];
530 int density = 0;
531 if (property_get(propName, property, NULL) > 0) {
532 density = atoi(property);
533 }
534 return density;
535 }
536 public:
537 static int getEmuDensity() {
538 return getDensityFromProperty("qemu.sf.lcd_density"); }
539 static int getBuildDensity() {
540 return getDensityFromProperty("ro.sf.lcd_density"); }
541 };
542 // The density of the device is provided by a build property
543 float density = Density::getBuildDensity() / 160.0f;
544 if (density == 0) {
545 // the build doesn't provide a density -- this is wrong!
546 // use xdpi instead
547 ALOGE("ro.sf.lcd_density must be defined as a build property");
548 density = xdpi / 160.0f;
549 }
550 if (Density::getEmuDensity()) {
551 // if "qemu.sf.lcd_density" is specified, it overrides everything
552 xdpi = ydpi = density = Density::getEmuDensity();
553 density /= 160.0f;
554 }
555
Mathias Agopian42977342012-08-05 00:40:46 -0700556 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
557 info->w = hw->getWidth();
558 info->h = hw->getHeight();
Mathias Agopian8b736f12012-08-13 17:54:26 -0700559 info->xdpi = xdpi;
560 info->ydpi = ydpi;
561 info->fps = float(1e9 / hwc.getRefreshPeriod());
562 info->density = density;
Mathias Agopian42977342012-08-05 00:40:46 -0700563 info->orientation = hw->getOrientation();
Mathias Agopian888c8222012-08-04 21:10:38 -0700564 // TODO: this needs to go away (currently needed only by webkit)
Mathias Agopian42977342012-08-05 00:40:46 -0700565 getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
Mathias Agopian888c8222012-08-04 21:10:38 -0700566 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700567}
568
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800569// ----------------------------------------------------------------------------
570
571sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800572 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700573}
574
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700575void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
Mathias Agopian3094df32012-06-18 18:06:45 -0700576
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700577 sp<IBinder> token;
578 { // scope for the lock
579 Mutex::Autolock _l(mStateLock);
580 token = mExtDisplayToken;
581 }
582
583 if (token == 0) {
584 token = createDisplay();
Mathias Agopian3094df32012-06-18 18:06:45 -0700585 }
586
587 { // scope for the lock
588 Mutex::Autolock _l(mStateLock);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700589 if (surface == 0) {
590 // release our current display. we're guarantee to have
591 // a reference to it (token), while we hold the lock
592 mExtDisplayToken = 0;
593 } else {
594 mExtDisplayToken = token;
595 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700596
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700597 DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
598 info.surface = surface;
599 setTransactionFlags(eDisplayTransactionNeeded);
Mathias Agopian3094df32012-06-18 18:06:45 -0700600 }
601}
602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800604
605void SurfaceFlinger::waitForEvent() {
606 mEventQueue.waitMessage();
607}
608
609void SurfaceFlinger::signalTransaction() {
610 mEventQueue.invalidate();
611}
612
613void SurfaceFlinger::signalLayerUpdate() {
614 mEventQueue.invalidate();
615}
616
617void SurfaceFlinger::signalRefresh() {
618 mEventQueue.refresh();
619}
620
621status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
622 nsecs_t reltime, uint32_t flags) {
623 return mEventQueue.postMessage(msg, reltime);
624}
625
626status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
627 nsecs_t reltime, uint32_t flags) {
628 status_t res = mEventQueue.postMessage(msg, reltime);
629 if (res == NO_ERROR) {
630 msg->wait();
631 }
632 return res;
633}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634
Mathias Agopian4fec8732012-06-29 14:12:52 -0700635bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 return true;
638}
639
Mathias Agopian86303202012-07-24 22:46:10 -0700640void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
Mathias Agopian86303202012-07-24 22:46:10 -0700641 mEventThread->onVSyncReceived(dpy, timestamp);
642}
643
644void SurfaceFlinger::eventControl(int event, int enabled) {
645 getHwComposer().eventControl(event, enabled);
646}
647
Mathias Agopian4fec8732012-06-29 14:12:52 -0700648void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800649 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800650 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651 case MessageQueue::INVALIDATE:
652 handleMessageTransaction();
653 handleMessageInvalidate();
654 signalRefresh();
655 break;
656 case MessageQueue::REFRESH:
657 handleMessageRefresh();
658 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800659 }
660}
661
Mathias Agopian4fec8732012-06-29 14:12:52 -0700662void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700663 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700664 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700665 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700666 }
667}
668
669void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700670 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700671 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700672}
673
674void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700675 ATRACE_CALL();
676 preComposition();
677 rebuildLayerStacks();
678 setUpHWComposer();
679 doDebugFlashRegions();
680 doComposition();
681 postComposition();
682}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700683
Mathias Agopiancd60f992012-08-16 16:28:27 -0700684void SurfaceFlinger::doDebugFlashRegions()
685{
686 // is debugging enabled
687 if (CC_LIKELY(!mDebugRegion))
688 return;
689
690 const bool repaintEverything = mRepaintEverything;
691 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
692 const sp<DisplayDevice>& hw(mDisplays[dpy]);
693 if (hw->canDraw()) {
694 // transform the dirty region into this screen's coordinate space
695 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
696 if (!dirtyRegion.isEmpty()) {
697 // redraw the whole screen
698 doComposeSurfaces(hw, Region(hw->bounds()));
699
700 // and draw the dirty region
701 glDisable(GL_TEXTURE_EXTERNAL_OES);
702 glDisable(GL_TEXTURE_2D);
703 glDisable(GL_BLEND);
704 glColor4f(1, 0, 1, 1);
705 const int32_t height = hw->getHeight();
706 Region::const_iterator it = dirtyRegion.begin();
707 Region::const_iterator const end = dirtyRegion.end();
708 while (it != end) {
709 const Rect& r = *it++;
710 GLfloat vertices[][2] = {
711 { r.left, height - r.top },
712 { r.left, height - r.bottom },
713 { r.right, height - r.bottom },
714 { r.right, height - r.top }
715 };
716 glVertexPointer(2, GL_FLOAT, 0, vertices);
717 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
718 }
719 hw->compositionComplete();
720 // FIXME
721 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
722 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
723 }
724 }
725 }
726 }
727
728 postFramebuffer();
729
730 if (mDebugRegion > 1) {
731 usleep(mDebugRegion * 1000);
732 }
733}
734
735void SurfaceFlinger::preComposition()
736{
737 bool needExtraInvalidate = false;
738 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
739 const size_t count = currentLayers.size();
740 for (size_t i=0 ; i<count ; i++) {
741 if (currentLayers[i]->onPreComposition()) {
742 needExtraInvalidate = true;
743 }
744 }
745 if (needExtraInvalidate) {
746 signalLayerUpdate();
747 }
748}
749
750void SurfaceFlinger::postComposition()
751{
752 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
753 const size_t count = currentLayers.size();
754 for (size_t i=0 ; i<count ; i++) {
755 currentLayers[i]->onPostComposition();
756 }
757}
758
759void SurfaceFlinger::rebuildLayerStacks() {
760 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700761 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700762 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700763 mVisibleRegionsDirty = false;
764 invalidateHwcGeometry();
Mathias Agopian87baae12012-07-31 12:38:26 -0700765 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700766 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700767 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian87baae12012-07-31 12:38:26 -0700768 Region opaqueRegion;
769 Region dirtyRegion;
770 computeVisibleRegions(currentLayers,
Mathias Agopian42977342012-08-05 00:40:46 -0700771 hw->getLayerStack(), dirtyRegion, opaqueRegion);
772 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian87baae12012-07-31 12:38:26 -0700773
774 Vector< sp<LayerBase> > layersSortedByZ;
775 const size_t count = currentLayers.size();
776 for (size_t i=0 ; i<count ; i++) {
777 const Layer::State& s(currentLayers[i]->drawingState());
Mathias Agopian42977342012-08-05 00:40:46 -0700778 if (s.layerStack == hw->getLayerStack()) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700779 if (!currentLayers[i]->visibleRegion.isEmpty()) {
780 layersSortedByZ.add(currentLayers[i]);
781 }
782 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700783 }
Mathias Agopian42977342012-08-05 00:40:46 -0700784 hw->setVisibleLayersSortedByZ(layersSortedByZ);
785 hw->undefinedRegion.set(hw->getBounds());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 hw->undefinedRegion.subtractSelf(
787 hw->getTransform().transform(opaqueRegion));
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700788 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700789 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700791
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700793 HWComposer& hwc(getHwComposer());
794 if (hwc.initCheck() == NO_ERROR) {
795 // build the h/w work list
796 const bool workListsDirty = mHwWorkListDirty;
797 mHwWorkListDirty = false;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700798 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700799 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700800 const int32_t id = hw->getHwcDisplayId();
801 if (id >= 0) {
802 const Vector< sp<LayerBase> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700803 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700804 const size_t count = currentLayers.size();
805 if (hwc.createWorkList(id, count) >= 0) {
806 HWComposer::LayerListIterator cur = hwc.begin(id);
807 const HWComposer::LayerListIterator end = hwc.end(id);
808 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
809 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700810
Mathias Agopiane60b0682012-08-21 23:34:09 -0700811 if (CC_UNLIKELY(workListsDirty)) {
812 layer->setGeometry(hw, *cur);
813 if (mDebugDisableHWC || mDebugRegion) {
814 cur->setSkip(true);
815 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700816 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700817
Mathias Agopiane60b0682012-08-21 23:34:09 -0700818 /*
819 * update the per-frame h/w composer data for each layer
820 * and build the transparent region of the FB
821 */
822 layer->setPerFrameData(hw, *cur);
823 }
Mathias Agopian1e260872012-08-08 18:35:12 -0700824 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700825 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700826 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700827 status_t err = hwc.prepare();
828 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
829 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700830}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700831
Mathias Agopiancd60f992012-08-16 16:28:27 -0700832void SurfaceFlinger::doComposition() {
833 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700834 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700835 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700836 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700837 if (hw->canDraw()) {
838 // transform the dirty region into this screen's coordinate space
839 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
840 if (!dirtyRegion.isEmpty()) {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700841 // repaint the framebuffer (if needed)
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842 doDisplayComposition(hw, dirtyRegion);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700843 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700844 hw->dirtyRegion.clear();
845 hw->flip(hw->swapRegion);
846 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700847 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700848 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700849 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700851 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700852}
853
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854void SurfaceFlinger::postFramebuffer()
855{
Mathias Agopian841cde52012-03-01 15:44:37 -0800856 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800857
Mathias Agopiana44b0412011-10-16 18:46:35 -0700858 const nsecs_t now = systemTime();
859 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700860
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700861 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700862 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700863 // FIXME: EGL spec says:
864 // "surface must be bound to the calling thread's current context,
865 // for the current rendering API."
Mathias Agopiancd60f992012-08-16 16:28:27 -0700866 DisplayDevice::makeCurrent(
867 getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700868 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700869 }
870
Mathias Agopian92a979a2012-08-02 18:32:23 -0700871 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700872 sp<const DisplayDevice> hw(mDisplays[dpy]);
873 const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700874 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700875 int32_t id = hw->getHwcDisplayId();
876 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700877 HWComposer::LayerListIterator cur = hwc.begin(id);
878 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700879 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700880 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700881 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700882 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700883 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700884 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700885 }
Jesse Hallef194142012-06-14 14:45:17 -0700886 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800887 }
888
Mathias Agopiana44b0412011-10-16 18:46:35 -0700889 mLastSwapBufferTime = systemTime() - now;
890 mDebugInSwapBuffers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891}
892
Mathias Agopian87baae12012-07-31 12:38:26 -0700893void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894{
Mathias Agopian841cde52012-03-01 15:44:37 -0800895 ATRACE_CALL();
896
Mathias Agopianca4d3602011-05-19 15:38:14 -0700897 Mutex::Autolock _l(mStateLock);
898 const nsecs_t now = systemTime();
899 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900
Mathias Agopianca4d3602011-05-19 15:38:14 -0700901 // Here we're guaranteed that some transaction flags are set
902 // so we can call handleTransactionLocked() unconditionally.
903 // We call getTransactionFlags(), which will also clear the flags,
904 // with mStateLock held to guarantee that mCurrentState won't change
905 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700906
Mathias Agopiane57f2922012-08-09 16:29:12 -0700907 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700908 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700909
Mathias Agopianca4d3602011-05-19 15:38:14 -0700910 mLastTransactionTime = systemTime() - now;
911 mDebugInTransaction = 0;
912 invalidateHwcGeometry();
913 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700914}
915
Mathias Agopian87baae12012-07-31 12:38:26 -0700916void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700917{
918 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 const size_t count = currentLayers.size();
920
921 /*
922 * Traversal of the children
923 * (perform the transaction for each of them if needed)
924 */
925
Mathias Agopian3559b072012-08-15 13:46:03 -0700926 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700928 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
930 if (!trFlags) continue;
931
932 const uint32_t flags = layer->doTransaction(0);
933 if (flags & Layer::eVisibleRegion)
934 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 }
936 }
937
938 /*
Mathias Agopian3559b072012-08-15 13:46:03 -0700939 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940 */
941
Mathias Agopiane57f2922012-08-09 16:29:12 -0700942 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700943 // here we take advantage of Vector's copy-on-write semantics to
944 // improve performance by skipping the transaction entirely when
945 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -0700946 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
947 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -0700950 const size_t cc = curr.size();
951 const size_t dc = draw.size();
952
953 // find the displays that were removed
954 // (ie: in drawing state but not in current state)
955 // also handle displays that changed
956 // (ie: displays that are in both lists)
957 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700958 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
959 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700960 // in drawing state but not in current state
961 if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
Mathias Agopian92a979a2012-08-02 18:32:23 -0700962 mDisplays.removeItem(draw[i].id);
963 } else {
964 ALOGW("trying to remove the main display");
965 }
966 } else {
967 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -0700968 const DisplayDeviceState& state(curr[j]);
Mathias Agopian111b2d82012-08-16 20:52:17 -0700969 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700970 // changing the surface is like destroying and
971 // recreating the DisplayDevice
Mathias Agopiane60b0682012-08-21 23:34:09 -0700972 const int32_t hwcDisplayId =
973 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
974 state.id : getHwComposer().allocateDisplayId();
Mathias Agopiane57f2922012-08-09 16:29:12 -0700975
976 sp<SurfaceTextureClient> stc(
977 new SurfaceTextureClient(state.surface));
978
979 sp<DisplayDevice> disp = new DisplayDevice(this,
Mathias Agopiane60b0682012-08-21 23:34:09 -0700980 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700981
982 disp->setLayerStack(state.layerStack);
983 disp->setOrientation(state.orientation);
984 // TODO: take viewport and frame into account
985 mDisplays.replaceValueFor(state.id, disp);
986 }
Mathias Agopian92a979a2012-08-02 18:32:23 -0700987 if (state.layerStack != draw[i].layerStack) {
Mathias Agopian42977342012-08-05 00:40:46 -0700988 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
Mathias Agopian28947d72012-08-08 18:51:15 -0700989 disp->setLayerStack(state.layerStack);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700990 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700991 if (state.orientation != draw[i].orientation ||
992 state.viewport != draw[i].viewport ||
993 state.frame != draw[i].frame) {
Mathias Agopian42977342012-08-05 00:40:46 -0700994 const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
995 disp->setOrientation(state.orientation);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700996 // TODO: take viewport and frame into account
Mathias Agopian92a979a2012-08-02 18:32:23 -0700997 }
998 }
999 }
1000
1001 // find displays that were added
1002 // (ie: in current state but not in drawing state)
1003 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001004 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001005 const DisplayDeviceState& state(curr[i]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001006 const int32_t hwcDisplayId =
1007 (uint32_t(state.id) < DisplayDevice::DISPLAY_ID_COUNT) ?
1008 state.id : getHwComposer().allocateDisplayId();
1009
Mathias Agopiane57f2922012-08-09 16:29:12 -07001010 sp<SurfaceTextureClient> stc(
1011 new SurfaceTextureClient(state.surface));
Mathias Agopiane60b0682012-08-21 23:34:09 -07001012 sp<DisplayDevice> disp = new DisplayDevice(this,
1013 state.id, hwcDisplayId, stc, 0, mEGLConfig);
Mathias Agopiane57f2922012-08-09 16:29:12 -07001014 mDisplays.add(state.id, disp);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001015 }
1016 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019
Mathias Agopian3559b072012-08-15 13:46:03 -07001020 /*
1021 * Perform our own transaction if needed
1022 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001023
Mathias Agopiancd60f992012-08-16 16:28:27 -07001024 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
1025 if (currentLayers.size() > previousLayers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001026 // layers have been added
1027 mVisibleRegionsDirty = true;
1028 }
1029
1030 // some layers might have been removed, so
1031 // we need to update the regions they're exposing.
1032 if (mLayersRemoved) {
1033 mLayersRemoved = false;
1034 mVisibleRegionsDirty = true;
Mathias Agopian3559b072012-08-15 13:46:03 -07001035 const size_t count = previousLayers.size();
1036 for (size_t i=0 ; i<count ; i++) {
1037 const sp<LayerBase>& layer(previousLayers[i]);
1038 if (currentLayers.indexOf(layer) < 0) {
1039 // this layer is not visible anymore
1040 // TODO: we could traverse the tree from front to back and
1041 // compute the actual visible region
1042 // TODO: we could cache the transformed region
1043 Layer::State front(layer->drawingState());
1044 Region visibleReg = front.transform.transform(
1045 Region(Rect(front.active.w, front.active.h)));
1046 invalidateLayerStack(front.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001047 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 }
1050
1051 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001052}
1053
1054void SurfaceFlinger::commitTransaction()
1055{
1056 if (!mLayersPendingRemoval.isEmpty()) {
1057 // Notify removed layers now that they can't be drawn from
1058 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1059 mLayersPendingRemoval[i]->onRemoved();
1060 }
1061 mLayersPendingRemoval.clear();
1062 }
1063
1064 mDrawingState = mCurrentState;
1065 mTransationPending = false;
1066 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067}
1068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001070 const LayerVector& currentLayers, uint32_t layerStack,
1071 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
Mathias Agopian841cde52012-03-01 15:44:37 -08001073 ATRACE_CALL();
1074
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 Region aboveOpaqueLayers;
1076 Region aboveCoveredLayers;
1077 Region dirty;
1078
Mathias Agopian87baae12012-07-31 12:38:26 -07001079 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080
1081 size_t i = currentLayers.size();
1082 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001083 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084
1085 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -07001086 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087
Mathias Agopian87baae12012-07-31 12:38:26 -07001088 // only consider the layers on the given later stack
1089 if (s.layerStack != layerStack)
1090 continue;
1091
Mathias Agopianab028732010-03-16 16:41:46 -07001092 /*
1093 * opaqueRegion: area of a surface that is fully opaque.
1094 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001096
1097 /*
1098 * visibleRegion: area of a surface that is visible on screen
1099 * and not fully transparent. This is essentially the layer's
1100 * footprint minus the opaque regions above it.
1101 * Areas covered by a translucent surface are considered visible.
1102 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001104
1105 /*
1106 * coveredRegion: area of a surface that is covered by all
1107 * visible regions above it (which includes the translucent areas).
1108 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001110
1111
1112 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian3165cc22012-08-08 19:42:09 -07001113 if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001114 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001115 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001117 if (!visibleRegion.isEmpty()) {
1118 // Remove the transparent area from the visible region
1119 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001120 Region transparentRegionScreen;
1121 const Transform tr(s.transform);
1122 if (tr.transformed()) {
1123 if (tr.preserveRects()) {
1124 // transform the transparent region
1125 transparentRegionScreen = tr.transform(s.transparentRegion);
1126 } else {
1127 // transformation too complex, can't do the
1128 // transparent region optimization.
1129 transparentRegionScreen.clear();
1130 }
1131 } else {
1132 transparentRegionScreen = s.transparentRegion;
1133 }
1134 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -07001135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136
Mathias Agopianab028732010-03-16 16:41:46 -07001137 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001138 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001139 if (s.alpha==255 && !translucent &&
1140 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1141 // the opaque region is the layer's footprint
1142 opaqueRegion = visibleRegion;
1143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144 }
1145 }
1146
Mathias Agopianab028732010-03-16 16:41:46 -07001147 // Clip the covered region to the visible region
1148 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1149
1150 // Update aboveCoveredLayers for next (lower) layer
1151 aboveCoveredLayers.orSelf(visibleRegion);
1152
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 // subtract the opaque region covered by the layers above us
1154 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155
1156 // compute this layer's dirty region
1157 if (layer->contentDirty) {
1158 // we need to invalidate the whole region
1159 dirty = visibleRegion;
1160 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001161 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 layer->contentDirty = false;
1163 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001164 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001165 * the exposed region consists of two components:
1166 * 1) what's VISIBLE now and was COVERED before
1167 * 2) what's EXPOSED now less what was EXPOSED before
1168 *
1169 * note that (1) is conservative, we start with the whole
1170 * visible region but only keep what used to be covered by
1171 * something -- which mean it may have been exposed.
1172 *
1173 * (2) handles areas that were not covered by anything but got
1174 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001175 */
Mathias Agopianab028732010-03-16 16:41:46 -07001176 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001177 const Region oldVisibleRegion = layer->visibleRegion;
1178 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001179 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1180 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 }
1182 dirty.subtractSelf(aboveOpaqueLayers);
1183
1184 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001185 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186
Mathias Agopianab028732010-03-16 16:41:46 -07001187 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 // Store the visible region is screen space
1191 layer->setVisibleRegion(visibleRegion);
1192 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 }
1194
Mathias Agopian87baae12012-07-31 12:38:26 -07001195 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196}
1197
Mathias Agopian87baae12012-07-31 12:38:26 -07001198void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1199 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001200 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001201 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1202 if (hw->getLayerStack() == layerStack) {
1203 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001204 }
1205 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001206}
1207
1208void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001210 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211
Mathias Agopian4fec8732012-06-29 14:12:52 -07001212 bool visibleRegions = false;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001213 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001214 const size_t count = currentLayers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001215 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001216 const sp<LayerBase>& layer(currentLayers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001217 const Region dirty(layer->latchBuffer(visibleRegions));
1218 Layer::State s(layer->drawingState());
1219 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001220 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001221
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001222 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223}
1224
Mathias Agopianad456f92011-01-13 17:53:01 -08001225void SurfaceFlinger::invalidateHwcGeometry()
1226{
1227 mHwWorkListDirty = true;
1228}
1229
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001230
Mathias Agopiancd60f992012-08-16 16:28:27 -07001231void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001232 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
Mathias Agopian87baae12012-07-31 12:38:26 -07001234 Region dirtyRegion(inDirtyRegion);
1235
Mathias Agopianb8a55602009-06-26 19:06:36 -07001236 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001237 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238
Mathias Agopian42977342012-08-05 00:40:46 -07001239 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001240 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001241 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1242 // takes a rectangle, we must make sure to update that whole
1243 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001244 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001245 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001246 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001247 // We need to redraw the rectangle that will be updated
1248 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001249 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001250 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001251 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001252 } else {
1253 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001254 dirtyRegion.set(hw->bounds());
1255 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 }
1257 }
1258
Mathias Agopiancd60f992012-08-16 16:28:27 -07001259 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260
Mathias Agopiancd60f992012-08-16 16:28:27 -07001261 // FIXME: we need to call eglSwapBuffers() on displays that have
1262 // GL composition and only on those.
1263 // however, currently hwc.commit() already does that for the main
1264 // display and never for the other ones
1265 if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
1266 // FIXME: EGL spec says:
1267 // "surface must be bound to the calling thread's current context,
1268 // for the current rendering API."
1269 eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001270 }
1271
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001272 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001273 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274}
1275
Mathias Agopiancd60f992012-08-16 16:28:27 -07001276void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001277{
Mathias Agopian86303202012-07-24 22:46:10 -07001278 HWComposer& hwc(getHwComposer());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001279 int32_t id = hw->getHwcDisplayId();
Mathias Agopian1e260872012-08-08 18:35:12 -07001280 HWComposer::LayerListIterator cur = hwc.begin(id);
1281 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001282
Mathias Agopiane60b0682012-08-21 23:34:09 -07001283 const bool hasGlesComposition = hwc.hasGlesComposition(id);
1284 const bool hasHwcComposition = hwc.hasHwcComposition(id);
1285 if (cur==end || hasGlesComposition) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001286
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001287 DisplayDevice::makeCurrent(hw, mEGLContext);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001288
1289 // set the frame buffer
1290 glMatrixMode(GL_MODELVIEW);
1291 glLoadIdentity();
1292
1293 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07001294 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001295 // when using overlays, we assume a fully transparent framebuffer
1296 // NOTE: we could reduce how much we need to clear, for instance
1297 // remove where there are opaque FB layers. however, on some
1298 // GPUs doing a "clean slate" glClear might be more efficient.
1299 // We'll revisit later if needed.
1300 glClearColor(0, 0, 0, 0);
1301 glClear(GL_COLOR_BUFFER_BIT);
1302 } else {
Mathias Agopian42977342012-08-05 00:40:46 -07001303 const Region region(hw->undefinedRegion.intersect(dirty));
Mathias Agopianb9494d52012-04-18 02:28:45 -07001304 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001305 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001306 // can happen with SurfaceView
Mathias Agopian87baae12012-07-31 12:38:26 -07001307 drawWormhole(region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001308 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001309 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001310
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001311 /*
1312 * and then, render the layers targeted at the framebuffer
1313 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001314
Mathias Agopian42977342012-08-05 00:40:46 -07001315 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001316 const size_t count = layers.size();
Mathias Agopian42977342012-08-05 00:40:46 -07001317 const Transform& tr = hw->getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001318 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001319 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001320 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Jesse Halla6b32db2012-07-19 16:44:38 -07001321 if (cur != end) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001322 // we're using h/w composer
1323 if (!clip.isEmpty()) {
1324 if (cur->getCompositionType() == HWC_OVERLAY) {
1325 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
1326 && layer->isOpaque()) {
1327 // never clear the very first layer since we're
1328 // guaranteed the FB is already cleared
1329 layer->clearWithOpenGL(hw, clip);
1330 }
1331 } else {
1332 layer->draw(hw, clip);
1333 }
1334 layer->setAcquireFence(hw, *cur);
1335 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001336 ++cur;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001337 } else {
1338 // we're not using h/w composer
1339 if (!clip.isEmpty()) {
1340 layer->draw(hw, clip);
1341 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001342 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001343 }
1344 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345}
1346
Mathias Agopian87baae12012-07-31 12:38:26 -07001347void SurfaceFlinger::drawWormhole(const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001349 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001350 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001351 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001352 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001353
1354 GLfloat vertices[4][2];
1355 glVertexPointer(2, GL_FLOAT, 0, vertices);
1356 Region::const_iterator it = region.begin();
1357 Region::const_iterator const end = region.end();
1358 while (it != end) {
1359 const Rect& r = *it++;
1360 vertices[0][0] = r.left;
1361 vertices[0][1] = r.top;
1362 vertices[1][0] = r.right;
1363 vertices[1][1] = r.top;
1364 vertices[2][0] = r.right;
1365 vertices[2][1] = r.bottom;
1366 vertices[3][0] = r.left;
1367 vertices[3][1] = r.bottom;
1368 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1369 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370}
1371
Mathias Agopian96f08192010-06-02 23:28:45 -07001372ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1373 const sp<LayerBaseClient>& lbc)
1374{
Mathias Agopian96f08192010-06-02 23:28:45 -07001375 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001376 size_t name = client->attachLayer(lbc);
1377
Mathias Agopian96f08192010-06-02 23:28:45 -07001378 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001379 Mutex::Autolock _l(mStateLock);
1380 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001381
Mathias Agopian4f113742011-05-03 16:21:41 -07001382 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001383}
1384
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001385status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386{
1387 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001388 status_t err = purgatorizeLayer_l(layer);
1389 if (err == NO_ERROR)
Mathias Agopian3559b072012-08-15 13:46:03 -07001390 setTransactionFlags(eTransactionNeeded);
Mathias Agopian3d579642009-06-04 18:46:21 -07001391 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392}
1393
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001394status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395{
1396 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1397 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001398 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 return NO_ERROR;
1400 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001401 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402}
1403
Mathias Agopian9a112062009-04-17 19:36:26 -07001404status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1405{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001406 // First add the layer to the purgatory list, which makes sure it won't
1407 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001408 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001409 if (err >= 0) {
1410 mLayerPurgatory.add(layerBase);
1411 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001412
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001413 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001414
Mathias Agopian3d579642009-06-04 18:46:21 -07001415 // it's possible that we don't find a layer, because it might
1416 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001417 // from the user because there is a race between Client::destroySurface(),
1418 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001419 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1420}
1421
Mathias Agopiandea20b12011-05-03 17:04:02 -07001422uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1423{
1424 return android_atomic_release_load(&mTransactionFlags);
1425}
1426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1428{
1429 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1430}
1431
Mathias Agopianbb641242010-05-18 17:06:55 -07001432uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433{
1434 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1435 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001436 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 }
1438 return old;
1439}
1440
Mathias Agopian8b33f032012-07-24 20:43:54 -07001441void SurfaceFlinger::setTransactionState(
1442 const Vector<ComposerState>& state,
1443 const Vector<DisplayState>& displays,
1444 uint32_t flags)
1445{
Mathias Agopian698c0872011-06-28 19:09:31 -07001446 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001447 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001448
1449 size_t count = displays.size();
1450 for (size_t i=0 ; i<count ; i++) {
1451 const DisplayState& s(displays[i]);
1452 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001453 }
1454
Mathias Agopiane57f2922012-08-09 16:29:12 -07001455 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001456 for (size_t i=0 ; i<count ; i++) {
1457 const ComposerState& s(state[i]);
1458 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001459 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001460 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001461
Mathias Agopian386aa982011-11-07 21:58:03 -08001462 if (transactionFlags) {
1463 // this triggers the transaction
1464 setTransactionFlags(transactionFlags);
1465
1466 // if this is a synchronous transaction, wait for it to take effect
1467 // before returning.
1468 if (flags & eSynchronous) {
1469 mTransationPending = true;
1470 }
1471 while (mTransationPending) {
1472 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1473 if (CC_UNLIKELY(err != NO_ERROR)) {
1474 // just in case something goes wrong in SF, return to the
1475 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001476 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001477 mTransationPending = false;
1478 break;
1479 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 }
1482}
1483
Mathias Agopiane57f2922012-08-09 16:29:12 -07001484uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1485{
1486 uint32_t flags = 0;
1487 DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
1488 if (disp.id >= 0) {
1489 const uint32_t what = s.what;
1490 if (what & DisplayState::eSurfaceChanged) {
1491 if (disp.surface->asBinder() != s.surface->asBinder()) {
1492 disp.surface = s.surface;
1493 flags |= eDisplayTransactionNeeded;
1494 }
1495 }
1496 if (what & DisplayState::eLayerStackChanged) {
1497 if (disp.layerStack != s.layerStack) {
1498 disp.layerStack = s.layerStack;
1499 flags |= eDisplayTransactionNeeded;
1500 }
1501 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001502 if (what & DisplayState::eOrientationChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001503 if (disp.orientation != s.orientation) {
1504 disp.orientation = s.orientation;
1505 flags |= eDisplayTransactionNeeded;
1506 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001507 }
1508 if (what & DisplayState::eFrameChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001509 if (disp.frame != s.frame) {
1510 disp.frame = s.frame;
1511 flags |= eDisplayTransactionNeeded;
1512 }
Mathias Agopian818b4602012-08-16 20:57:39 -07001513 }
1514 if (what & DisplayState::eViewportChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001515 if (disp.viewport != s.viewport) {
1516 disp.viewport = s.viewport;
1517 flags |= eDisplayTransactionNeeded;
1518 }
1519 }
1520 }
1521 return flags;
1522}
1523
1524uint32_t SurfaceFlinger::setClientStateLocked(
1525 const sp<Client>& client,
1526 const layer_state_t& s)
1527{
1528 uint32_t flags = 0;
1529 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1530 if (layer != 0) {
1531 const uint32_t what = s.what;
1532 if (what & layer_state_t::ePositionChanged) {
1533 if (layer->setPosition(s.x, s.y))
1534 flags |= eTraversalNeeded;
1535 }
1536 if (what & layer_state_t::eLayerChanged) {
1537 // NOTE: index needs to be calculated before we update the state
1538 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1539 if (layer->setLayer(s.z)) {
1540 mCurrentState.layersSortedByZ.removeAt(idx);
1541 mCurrentState.layersSortedByZ.add(layer);
1542 // we need traversal (state changed)
1543 // AND transaction (list changed)
1544 flags |= eTransactionNeeded|eTraversalNeeded;
1545 }
1546 }
1547 if (what & layer_state_t::eSizeChanged) {
1548 if (layer->setSize(s.w, s.h)) {
1549 flags |= eTraversalNeeded;
1550 }
1551 }
1552 if (what & layer_state_t::eAlphaChanged) {
1553 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1554 flags |= eTraversalNeeded;
1555 }
1556 if (what & layer_state_t::eMatrixChanged) {
1557 if (layer->setMatrix(s.matrix))
1558 flags |= eTraversalNeeded;
1559 }
1560 if (what & layer_state_t::eTransparentRegionChanged) {
1561 if (layer->setTransparentRegionHint(s.transparentRegion))
1562 flags |= eTraversalNeeded;
1563 }
1564 if (what & layer_state_t::eVisibilityChanged) {
1565 if (layer->setFlags(s.flags, s.mask))
1566 flags |= eTraversalNeeded;
1567 }
1568 if (what & layer_state_t::eCropChanged) {
1569 if (layer->setCrop(s.crop))
1570 flags |= eTraversalNeeded;
1571 }
1572 if (what & layer_state_t::eLayerStackChanged) {
1573 // NOTE: index needs to be calculated before we update the state
1574 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1575 if (layer->setLayerStack(s.layerStack)) {
1576 mCurrentState.layersSortedByZ.removeAt(idx);
1577 mCurrentState.layersSortedByZ.add(layer);
1578 // we need traversal (state changed)
1579 // AND transaction (list changed)
1580 flags |= eTransactionNeeded|eTraversalNeeded;
1581 }
1582 }
1583 }
1584 return flags;
1585}
1586
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001587sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001588 ISurfaceComposerClient::surface_data_t* params,
1589 const String8& name,
1590 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1592 uint32_t flags)
1593{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001594 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001595 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001596
1597 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001598 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001599 int(w), int(h));
1600 return surfaceHandle;
1601 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001602
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001603 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian3165cc22012-08-08 19:42:09 -07001604 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1605 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001606 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001608 case ISurfaceComposerClient::eFXSurfaceBlur:
1609 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001610 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001612 case ISurfaceComposerClient::eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001613 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001614 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615 }
1616
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001617 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001618 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001619 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001620 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001622 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001623 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001624 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001625 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627 }
1628
1629 return surfaceHandle;
1630}
1631
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001632sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001633 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001634 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001635 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636{
1637 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001638 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 case PIXEL_FORMAT_TRANSPARENT:
1640 case PIXEL_FORMAT_TRANSLUCENT:
1641 format = PIXEL_FORMAT_RGBA_8888;
1642 break;
1643 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001644#ifdef NO_RGBX_8888
1645 format = PIXEL_FORMAT_RGB_565;
1646#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001647 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001648#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 break;
1650 }
1651
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001652#ifdef NO_RGBX_8888
1653 if (format == PIXEL_FORMAT_RGBX_8888)
1654 format = PIXEL_FORMAT_RGBA_8888;
1655#endif
1656
Mathias Agopian96f08192010-06-02 23:28:45 -07001657 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001658 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001659 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001660 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001661 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 }
1663 return layer;
1664}
1665
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001666sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001667 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001668 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669{
Mathias Agopian96f08192010-06-02 23:28:45 -07001670 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001671 return layer;
1672}
1673
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001674sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001675 const sp<Client>& client, DisplayID display,
1676 uint32_t w, uint32_t h, uint32_t flags)
1677{
1678 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 return layer;
1680}
1681
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001682status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683{
Mathias Agopian9a112062009-04-17 19:36:26 -07001684 /*
1685 * called by the window manager, when a surface should be marked for
1686 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001687 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001688 * The surface is removed from the current and drawing lists, but placed
1689 * in the purgatory queue, so it's not destroyed right-away (we need
1690 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001691 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001692
Mathias Agopian48d819a2009-09-10 19:41:18 -07001693 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001694 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001695 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001696
Mathias Agopian48d819a2009-09-10 19:41:18 -07001697 if (layer != 0) {
1698 err = purgatorizeLayer_l(layer);
1699 if (err == NO_ERROR) {
Mathias Agopian13233e02012-08-15 16:14:33 -07001700 setTransactionFlags(eTransactionNeeded);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001701 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001702 }
1703 return err;
1704}
1705
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001706status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001707{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001708 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001709 status_t err = NO_ERROR;
1710 sp<LayerBaseClient> l(layer.promote());
1711 if (l != NULL) {
1712 Mutex::Autolock _l(mStateLock);
1713 err = removeLayer_l(l);
1714 if (err == NAME_NOT_FOUND) {
1715 // The surface wasn't in the current list, which means it was
1716 // removed already, which means it is in the purgatory,
1717 // and need to be removed from there.
1718 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001719 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001720 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001721 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001722 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001723 "error removing layer=%p (%s)", l.get(), strerror(-err));
1724 }
1725 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726}
1727
Mathias Agopianb60314a2012-04-10 22:09:54 -07001728// ---------------------------------------------------------------------------
1729
Andy McFadden13a082e2012-08-24 10:16:42 -07001730void SurfaceFlinger::onInitializeDisplays() {
1731 // reset screen orientation
1732 Vector<ComposerState> state;
1733 Vector<DisplayState> displays;
1734 DisplayState d;
1735 d.what = DisplayState::eOrientationChanged;
1736 d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
1737 d.orientation = DisplayState::eOrientationDefault;
1738 displays.add(d);
1739 setTransactionState(state, displays, 0);
1740
1741 // XXX: this should init default device to "unblank" and all other devices to "blank"
1742 onScreenAcquired();
1743}
1744
1745void SurfaceFlinger::initializeDisplays() {
1746 class MessageScreenInitialized : public MessageBase {
1747 SurfaceFlinger* flinger;
1748 public:
1749 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1750 virtual bool handler() {
1751 flinger->onInitializeDisplays();
1752 return true;
1753 }
1754 };
1755 sp<MessageBase> msg = new MessageScreenInitialized(this);
1756 postMessageAsync(msg); // we may be called from main thread, use async message
1757}
1758
1759
Mathias Agopianb60314a2012-04-10 22:09:54 -07001760void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001761 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001762 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
Mathias Agopian86303202012-07-24 22:46:10 -07001763 getHwComposer().acquire();
Mathias Agopian42977342012-08-05 00:40:46 -07001764 hw->acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001765 mEventThread->onScreenAcquired();
Mathias Agopian20128302012-08-13 18:32:13 -07001766 mVisibleRegionsDirty = true;
1767 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768}
1769
Mathias Agopianb60314a2012-04-10 22:09:54 -07001770void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001771 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian42977342012-08-05 00:40:46 -07001772 sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
1773 if (hw->isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001774 mEventThread->onScreenReleased();
Mathias Agopian42977342012-08-05 00:40:46 -07001775 hw->releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001776 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001777 // from this point on, SF will stop drawing
1778 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779}
1780
Colin Cross8e533062012-06-07 13:17:52 -07001781void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001782 class MessageScreenAcquired : public MessageBase {
1783 SurfaceFlinger* flinger;
1784 public:
1785 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1786 virtual bool handler() {
1787 flinger->onScreenAcquired();
1788 return true;
1789 }
1790 };
1791 sp<MessageBase> msg = new MessageScreenAcquired(this);
1792 postMessageSync(msg);
1793}
1794
Colin Cross8e533062012-06-07 13:17:52 -07001795void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001796 class MessageScreenReleased : public MessageBase {
1797 SurfaceFlinger* flinger;
1798 public:
1799 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1800 virtual bool handler() {
1801 flinger->onScreenReleased();
1802 return true;
1803 }
1804 };
1805 sp<MessageBase> msg = new MessageScreenReleased(this);
1806 postMessageSync(msg);
1807}
1808
1809// ---------------------------------------------------------------------------
1810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1812{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001813 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814 char buffer[SIZE];
1815 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001816
1817 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 snprintf(buffer, SIZE, "Permission Denial: "
1819 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1820 IPCThreadState::self()->getCallingPid(),
1821 IPCThreadState::self()->getCallingUid());
1822 result.append(buffer);
1823 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001824 // Try to get the main lock, but don't insist if we can't
1825 // (this would indicate SF is stuck, but we want to be able to
1826 // print something in dumpsys).
1827 int retry = 3;
1828 while (mStateLock.tryLock()<0 && --retry>=0) {
1829 usleep(1000000);
1830 }
1831 const bool locked(retry >= 0);
1832 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001833 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001834 "SurfaceFlinger appears to be unresponsive, "
1835 "dumping anyways (no locks held)\n");
1836 result.append(buffer);
1837 }
1838
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001839 bool dumpAll = true;
1840 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001841 size_t numArgs = args.size();
1842 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001843 if ((index < numArgs) &&
1844 (args[index] == String16("--list"))) {
1845 index++;
1846 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001847 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001848 }
1849
1850 if ((index < numArgs) &&
1851 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001852 index++;
1853 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001854 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001855 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001856
1857 if ((index < numArgs) &&
1858 (args[index] == String16("--latency-clear"))) {
1859 index++;
1860 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001861 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001862 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001864
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001865 if (dumpAll) {
1866 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001867 }
1868
Mathias Agopian9795c422009-08-26 16:36:26 -07001869 if (locked) {
1870 mStateLock.unlock();
1871 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872 }
1873 write(fd, result.string(), result.size());
1874 return NO_ERROR;
1875}
1876
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001877void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1878 String8& result, char* buffer, size_t SIZE) const
1879{
1880 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1881 const size_t count = currentLayers.size();
1882 for (size_t i=0 ; i<count ; i++) {
1883 const sp<LayerBase>& layer(currentLayers[i]);
1884 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1885 result.append(buffer);
1886 }
1887}
1888
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001889void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1890 String8& result, char* buffer, size_t SIZE) const
1891{
1892 String8 name;
1893 if (index < args.size()) {
1894 name = String8(args[index]);
1895 index++;
1896 }
1897
1898 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1899 const size_t count = currentLayers.size();
1900 for (size_t i=0 ; i<count ; i++) {
1901 const sp<LayerBase>& layer(currentLayers[i]);
1902 if (name.isEmpty()) {
1903 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1904 result.append(buffer);
1905 }
1906 if (name.isEmpty() || (name == layer->getName())) {
1907 layer->dumpStats(result, buffer, SIZE);
1908 }
1909 }
1910}
1911
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001912void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1913 String8& result, char* buffer, size_t SIZE) const
1914{
1915 String8 name;
1916 if (index < args.size()) {
1917 name = String8(args[index]);
1918 index++;
1919 }
1920
1921 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1922 const size_t count = currentLayers.size();
1923 for (size_t i=0 ; i<count ; i++) {
1924 const sp<LayerBase>& layer(currentLayers[i]);
1925 if (name.isEmpty() || (name == layer->getName())) {
1926 layer->clearStats();
1927 }
1928 }
1929}
1930
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001931void SurfaceFlinger::dumpAllLocked(
1932 String8& result, char* buffer, size_t SIZE) const
1933{
1934 // figure out if we're stuck somewhere
1935 const nsecs_t now = systemTime();
1936 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1937 const nsecs_t inTransaction(mDebugInTransaction);
1938 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1939 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1940
1941 /*
1942 * Dump the visible layer list
1943 */
1944 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1945 const size_t count = currentLayers.size();
1946 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1947 result.append(buffer);
1948 for (size_t i=0 ; i<count ; i++) {
1949 const sp<LayerBase>& layer(currentLayers[i]);
1950 layer->dump(result, buffer, SIZE);
1951 }
1952
1953 /*
1954 * Dump the layers in the purgatory
1955 */
1956
1957 const size_t purgatorySize = mLayerPurgatory.size();
1958 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1959 result.append(buffer);
1960 for (size_t i=0 ; i<purgatorySize ; i++) {
1961 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1962 layer->shortDump(result, buffer, SIZE);
1963 }
1964
1965 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07001966 * Dump Display state
1967 */
1968
1969 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1970 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
1971 snprintf(buffer, SIZE,
1972 "+ DisplayDevice[%u]\n"
1973 " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
1974 "flips=%u, secure=%d, numLayers=%u\n",
1975 dpy,
1976 hw->getDisplayId(), hw->getLayerStack(),
1977 hw->getWidth(), hw->getHeight(),
1978 hw->getOrientation(), hw->getTransform().getType(),
1979 hw->getPageFlipCount(),
1980 hw->getSecureLayerVisible(),
1981 hw->getVisibleLayersSortedByZ().size());
1982 result.append(buffer);
1983 }
1984
1985 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001986 * Dump SurfaceFlinger global state
1987 */
1988
1989 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1990 result.append(buffer);
1991
Mathias Agopian888c8222012-08-04 21:10:38 -07001992 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07001993 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001994 const GLExtensions& extensions(GLExtensions::getInstance());
1995 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1996 extensions.getVendor(),
1997 extensions.getRenderer(),
1998 extensions.getVersion());
1999 result.append(buffer);
2000
2001 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopiand3ee2312012-08-02 14:01:42 -07002002 eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002003 result.append(buffer);
2004
2005 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
2006 result.append(buffer);
2007
Mathias Agopian42977342012-08-05 00:40:46 -07002008 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002009 snprintf(buffer, SIZE,
2010 " orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002011 hw->getOrientation(), hw->canDraw());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002012 result.append(buffer);
2013 snprintf(buffer, SIZE,
2014 " last eglSwapBuffers() time: %f us\n"
2015 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002016 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002017 " refresh-rate : %f fps\n"
2018 " x-dpi : %f\n"
Mathias Agopian8b736f12012-08-13 17:54:26 -07002019 " y-dpi : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002020 mLastSwapBufferTime/1000.0,
2021 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002022 mTransactionFlags,
Mathias Agopian888c8222012-08-04 21:10:38 -07002023 1e9 / hwc.getRefreshPeriod(),
Mathias Agopian8b736f12012-08-13 17:54:26 -07002024 hwc.getDpiX(),
2025 hwc.getDpiY());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002026 result.append(buffer);
2027
2028 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
2029 inSwapBuffersDuration/1000.0);
2030 result.append(buffer);
2031
2032 snprintf(buffer, SIZE, " transaction time: %f us\n",
2033 inTransactionDuration/1000.0);
2034 result.append(buffer);
2035
2036 /*
2037 * VSYNC state
2038 */
2039 mEventThread->dump(result, buffer, SIZE);
2040
2041 /*
2042 * Dump HWComposer state
2043 */
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002044 snprintf(buffer, SIZE, "h/w composer state:\n");
2045 result.append(buffer);
2046 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
2047 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2048 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
2049 result.append(buffer);
Mathias Agopian42977342012-08-05 00:40:46 -07002050 hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002051
2052 /*
2053 * Dump gralloc state
2054 */
2055 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2056 alloc.dump(result);
Mathias Agopian42977342012-08-05 00:40:46 -07002057 hw->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002058}
2059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060status_t SurfaceFlinger::onTransact(
2061 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2062{
2063 switch (code) {
2064 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07002065 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002067 case BLANK:
2068 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002069 {
2070 // codes that require permission check
2071 IPCThreadState* ipc = IPCThreadState::self();
2072 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002073 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002074 if ((uid != AID_GRAPHICS) &&
2075 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002076 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002077 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2078 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002080 break;
2081 }
2082 case CAPTURE_SCREEN:
2083 {
2084 // codes that require permission check
2085 IPCThreadState* ipc = IPCThreadState::self();
2086 const int pid = ipc->getCallingPid();
2087 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002088 if ((uid != AID_GRAPHICS) &&
2089 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002090 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002091 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2092 return PERMISSION_DENIED;
2093 }
2094 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 }
2096 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002098 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2099 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002100 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002101 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002102 IPCThreadState* ipc = IPCThreadState::self();
2103 const int pid = ipc->getCallingPid();
2104 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002105 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002106 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107 return PERMISSION_DENIED;
2108 }
2109 int n;
2110 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002111 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002112 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002113 return NO_ERROR;
2114 case 1002: // SHOW_UPDATES
2115 n = data.readInt32();
2116 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002117 invalidateHwcGeometry();
2118 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002121 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002122 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002124 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002125 setTransactionFlags(
2126 eTransactionNeeded|
2127 eDisplayTransactionNeeded|
2128 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002129 return NO_ERROR;
2130 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002131 case 1006:{ // send empty update
2132 signalRefresh();
2133 return NO_ERROR;
2134 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002135 case 1008: // toggle use of hw composer
2136 n = data.readInt32();
2137 mDebugDisableHWC = n ? 1 : 0;
2138 invalidateHwcGeometry();
2139 repaintEverything();
2140 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002141 case 1009: // toggle use of transform hint
2142 n = data.readInt32();
2143 mDebugDisableTransformHint = n ? 1 : 0;
2144 invalidateHwcGeometry();
2145 repaintEverything();
2146 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002147 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002148 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149 reply->writeInt32(0);
2150 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002151 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002152 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002153 return NO_ERROR;
2154 case 1013: {
2155 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002156 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2157 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002158 }
2159 return NO_ERROR;
2160 }
2161 }
2162 return err;
2163}
2164
Mathias Agopian53331da2011-08-22 21:44:41 -07002165void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002166 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002167 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002168}
2169
Mathias Agopian59119e62010-10-11 12:37:43 -07002170// ---------------------------------------------------------------------------
2171
Mathias Agopian118d0242011-10-13 16:02:48 -07002172status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2173 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2174{
2175 Mutex::Autolock _l(mStateLock);
2176 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2177}
2178
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2180 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002181{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002182 ATRACE_CALL();
2183
Mathias Agopian59119e62010-10-11 12:37:43 -07002184 if (!GLExtensions::getInstance().haveFramebufferObject())
2185 return INVALID_OPERATION;
2186
2187 // get screen geometry
Mathias Agopian42977342012-08-05 00:40:46 -07002188 sp<const DisplayDevice> hw(getDisplayDevice(dpy));
2189 const uint32_t hw_w = hw->getWidth();
2190 const uint32_t hw_h = hw->getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002191 GLfloat u = 1;
2192 GLfloat v = 1;
2193
2194 // make sure to clear all GL error flags
2195 while ( glGetError() != GL_NO_ERROR ) ;
2196
2197 // create a FBO
2198 GLuint name, tname;
2199 glGenTextures(1, &tname);
2200 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002201 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2202 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2204 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002205 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002206 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002207 GLint tw = (2 << (31 - clz(hw_w)));
2208 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002209 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2210 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002211 u = GLfloat(hw_w) / tw;
2212 v = GLfloat(hw_h) / th;
2213 }
2214 glGenFramebuffersOES(1, &name);
2215 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002216 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2217 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002218
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002219 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002220 glDisable(GL_TEXTURE_EXTERNAL_OES);
2221 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002222 glClearColor(0,0,0,1);
2223 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002224 glMatrixMode(GL_MODELVIEW);
2225 glLoadIdentity();
Mathias Agopian42977342012-08-05 00:40:46 -07002226 const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002227 const size_t count = layers.size();
2228 for (size_t i=0 ; i<count ; ++i) {
2229 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002230 layer->draw(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002231 }
2232
Mathias Agopian42977342012-08-05 00:40:46 -07002233 hw->compositionComplete();
Mathias Agopian118d0242011-10-13 16:02:48 -07002234
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002235 // back to main framebuffer
2236 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237 glDeleteFramebuffersOES(1, &name);
2238
2239 *textureName = tname;
2240 *uOut = u;
2241 *vOut = v;
2242 return NO_ERROR;
2243}
2244
2245// ---------------------------------------------------------------------------
2246
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002247status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002248 sp<IMemoryHeap>* heap,
2249 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002250 uint32_t sw, uint32_t sh,
2251 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002252{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002253 ATRACE_CALL();
2254
Mathias Agopian74c40c02010-09-29 13:02:36 -07002255 status_t result = PERMISSION_DENIED;
2256
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002257 const DisplayDeviceState& disp(mDrawingState.displays.valueFor(display));
2258 if (CC_UNLIKELY(disp.id < 0)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002260 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002261
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002262 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002263 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002264 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002265
2266 // get screen geometry
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002267 sp<const DisplayDevice> hw(getDisplayDevice(disp.id));
Mathias Agopian42977342012-08-05 00:40:46 -07002268 const uint32_t hw_w = hw->getWidth();
2269 const uint32_t hw_h = hw->getHeight();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002271 // if we have secure windows on this display, never allow the screen capture
Mathias Agopian42977342012-08-05 00:40:46 -07002272 if (hw->getSecureLayerVisible()) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002273 ALOGW("FB is protected: PERMISSION_DENIED");
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002274 return PERMISSION_DENIED;
2275 }
2276
2277 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002278 ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002279 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002280 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002281
2282 sw = (!sw) ? hw_w : sw;
2283 sh = (!sh) ? hw_h : sh;
2284 const size_t size = sw * sh * 4;
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002285 const bool filtering = sw != hw_w || sh != hw_h;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002286
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002287// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2288// sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002289
Mathias Agopian74c40c02010-09-29 13:02:36 -07002290 // make sure to clear all GL error flags
2291 while ( glGetError() != GL_NO_ERROR ) ;
2292
2293 // create a FBO
2294 GLuint name, tname;
2295 glGenRenderbuffersOES(1, &tname);
2296 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2297 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002298
Mathias Agopian74c40c02010-09-29 13:02:36 -07002299 glGenFramebuffersOES(1, &name);
2300 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2301 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2302 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2303
2304 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002305
Mathias Agopian74c40c02010-09-29 13:02:36 -07002306 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2307
2308 // invert everything, b/c glReadPixel() below will invert the FB
2309 glViewport(0, 0, sw, sh);
2310 glMatrixMode(GL_PROJECTION);
2311 glPushMatrix();
2312 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002313 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314 glMatrixMode(GL_MODELVIEW);
2315
2316 // redraw the screen entirely...
2317 glClearColor(0,0,0,1);
2318 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002319
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002320 // TODO: filter the layers that are drawn based on the layer stack of the display.
Jamie Gennis9575f602011-10-07 14:51:16 -07002321 const LayerVector& layers(mDrawingState.layersSortedByZ);
2322 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002323 for (size_t i=0 ; i<count ; ++i) {
2324 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002325 const uint32_t flags = layer->drawingState().flags;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002326 if (!(flags & layer_state_t::eLayerHidden)) {
Mathias Agopianb0610332011-08-25 14:36:43 -07002327 const uint32_t z = layer->drawingState().z;
2328 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopianfcb239d2012-08-02 16:01:34 -07002329 if (filtering) layer->setFiltering(true);
2330 layer->draw(hw);
2331 if (filtering) layer->setFiltering(false);
Mathias Agopianb0610332011-08-25 14:36:43 -07002332 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002333 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002334 }
2335
Mathias Agopian74c40c02010-09-29 13:02:36 -07002336 // check for errors and return screen capture
2337 if (glGetError() != GL_NO_ERROR) {
2338 // error while rendering
2339 result = INVALID_OPERATION;
2340 } else {
2341 // allocate shared memory large enough to hold the
2342 // screen capture
2343 sp<MemoryHeapBase> base(
2344 new MemoryHeapBase(size, 0, "screen-capture") );
2345 void* const ptr = base->getBase();
2346 if (ptr) {
2347 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002348 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2350 if (glGetError() == GL_NO_ERROR) {
2351 *heap = base;
2352 *w = sw;
2353 *h = sh;
2354 *f = PIXEL_FORMAT_RGBA_8888;
2355 result = NO_ERROR;
2356 }
2357 } else {
2358 result = NO_MEMORY;
2359 }
2360 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361 glViewport(0, 0, hw_w, hw_h);
2362 glMatrixMode(GL_PROJECTION);
2363 glPopMatrix();
2364 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002365 } else {
2366 result = BAD_VALUE;
2367 }
2368
2369 // release FBO resources
2370 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2371 glDeleteRenderbuffersOES(1, &tname);
2372 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002373
Mathias Agopian42977342012-08-05 00:40:46 -07002374 hw->compositionComplete();
Mathias Agopiane6f09842010-12-15 14:41:59 -08002375
Mathias Agopianef7b9c72012-08-10 15:22:19 -07002376// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002377
Mathias Agopian74c40c02010-09-29 13:02:36 -07002378 return result;
2379}
2380
2381
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002382status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002383 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002385 uint32_t sw, uint32_t sh,
2386 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002387{
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002388 if (CC_UNLIKELY(display == 0))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002389 return BAD_VALUE;
2390
2391 if (!GLExtensions::getInstance().haveFramebufferObject())
2392 return INVALID_OPERATION;
2393
2394 class MessageCaptureScreen : public MessageBase {
2395 SurfaceFlinger* flinger;
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002396 sp<IBinder> display;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002397 sp<IMemoryHeap>* heap;
2398 uint32_t* w;
2399 uint32_t* h;
2400 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 uint32_t sw;
2402 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002403 uint32_t minLayerZ;
2404 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002405 status_t result;
2406 public:
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002407 MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002408 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002409 uint32_t sw, uint32_t sh,
2410 uint32_t minLayerZ, uint32_t maxLayerZ)
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002411 : flinger(flinger), display(display),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002412 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2413 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2414 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002415 {
2416 }
2417 status_t getResult() const {
2418 return result;
2419 }
2420 virtual bool handler() {
2421 Mutex::Autolock _l(flinger->mStateLock);
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002422 result = flinger->captureScreenImplLocked(display,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002423 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002424 return true;
2425 }
2426 };
2427
2428 sp<MessageBase> msg = new MessageCaptureScreen(this,
Jeff Brown9d4e3d22012-08-24 20:00:51 -07002429 display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002430 status_t res = postMessageSync(msg);
2431 if (res == NO_ERROR) {
2432 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2433 }
2434 return res;
2435}
2436
2437// ---------------------------------------------------------------------------
2438
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002439SurfaceFlinger::LayerVector::LayerVector() {
2440}
2441
2442SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2443 : SortedVector<sp<LayerBase> >(rhs) {
2444}
2445
2446int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2447 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002448{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002449 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002450 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2451 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002452
2453 uint32_t ls = l->currentState().layerStack;
2454 uint32_t rs = r->currentState().layerStack;
2455 if (ls != rs)
2456 return ls - rs;
2457
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002458 uint32_t lz = l->currentState().z;
2459 uint32_t rz = r->currentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002460 if (lz != rz)
2461 return lz - rz;
2462
2463 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002464}
2465
2466// ---------------------------------------------------------------------------
2467
Mathias Agopiane57f2922012-08-09 16:29:12 -07002468SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
2469}
2470
2471SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
2472 : id(id), layerStack(0), orientation(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002473}
2474
2475// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476
Jamie Gennis9a78c902011-01-12 18:30:40 -08002477GraphicBufferAlloc::GraphicBufferAlloc() {}
2478
2479GraphicBufferAlloc::~GraphicBufferAlloc() {}
2480
2481sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002482 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002483 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2484 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002485 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002486 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002487 if (err == NO_MEMORY) {
2488 GraphicBuffer::dumpAllocationsToSystemLog();
2489 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002490 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002491 "failed (%s), handle=%p",
2492 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002493 return 0;
2494 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002495 return graphicBuffer;
2496}
2497
Jamie Gennis9a78c902011-01-12 18:30:40 -08002498// ---------------------------------------------------------------------------
2499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500}; // namespace android