blob: ad3865ed0eb8725de7d06939be4541b958fdbde8 [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 Agopiand0566bc2011-11-17 17:49:17 -080035#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070036#include <gui/BitTube.h>
37#include <gui/SurfaceTextureClient.h>
38
39#include <ui/GraphicBufferAllocator.h>
40#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <utils/String8.h>
43#include <utils/String16.h>
44#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080045#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047#include <private/android_filesystem_config.h>
48#include <private/gui/SharedBufferStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080051#include "DdmConnection.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070052#include "DisplayHardware.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070053#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopiana4912602012-07-12 14:25:33 -070061#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Mathias Agopiana67932f2011-04-20 14:20:59 -070064
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080065#define EGL_VERSION_HW_ANDROID 0x3143
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian99b49842011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070082 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070087 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070089 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070090 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070091 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070092 mDebugInSwapBuffers(0),
93 mLastSwapBufferTime(0),
94 mDebugInTransaction(0),
95 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070096 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070097 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098{
Steve Blocka19954a2012-01-04 20:05:49 +000099 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
101 // debugging stuff...
102 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 property_get("debug.sf.showupdates", value, "0");
105 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700106
Colin Cross3854ed52012-03-23 14:17:18 -0700107#ifdef DDMS_DEBUGGING
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 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700113#else
114#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700115#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000118 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121void SurfaceFlinger::onFirstRef()
122{
123 mEventQueue.init(this);
124
125 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
126
127 // Wait for the main thread to be done with its initialization
128 mReadyToRunBarrier.wait();
129}
130
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::~SurfaceFlinger()
133{
134 glDeleteTextures(1, &mWormholeTexName);
Mathias Agopiana4912602012-07-12 14:25:33 -0700135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
136 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
137 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::binderDied(const wp<IBinder>& who)
141{
142 // the window manager died on us. prepare its eulogy.
143
144 // reset screen orientation
145 Vector<ComposerState> state;
146 setTransactionState(state, eOrientationDefault, 0);
147
148 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700149 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150}
151
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700152sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700154 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155}
156
Mathias Agopian7e27f052010-05-28 14:22:23 -0700157sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Mathias Agopian96f08192010-06-02 23:28:45 -0700159 sp<ISurfaceComposerClient> bclient;
160 sp<Client> client(new Client(this));
161 status_t err = client->initCheck();
162 if (err == NO_ERROR) {
163 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 return bclient;
166}
167
Jamie Gennis9a78c902011-01-12 18:30:40 -0800168sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
169{
170 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
171 return gba;
172}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174void SurfaceFlinger::bootFinished()
175{
176 const nsecs_t now = systemTime();
177 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000178 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700180
181 // wait patiently for the window manager death
182 const String16 name("window");
183 sp<IBinder> window(defaultServiceManager()->getService(name));
184 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700185 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700186 }
187
188 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700189 // formerly we would just kill the process, but we now ask it to exit so it
190 // can choose where to stop the animation.
191 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700194void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
195 class MessageDestroyGLTexture : public MessageBase {
196 GLuint texture;
197 public:
198 MessageDestroyGLTexture(GLuint texture)
199 : texture(texture) {
200 }
201 virtual bool handler() {
202 glDeleteTextures(1, &texture);
203 return true;
204 }
205 };
206 postMessageAsync(new MessageDestroyGLTexture(texture));
207}
208
Mathias Agopiana4912602012-07-12 14:25:33 -0700209status_t SurfaceFlinger::selectConfigForPixelFormat(
210 EGLDisplay dpy,
211 EGLint const* attrs,
212 PixelFormat format,
213 EGLConfig* outConfig)
214{
215 EGLConfig config = NULL;
216 EGLint numConfigs = -1, n=0;
217 eglGetConfigs(dpy, NULL, 0, &numConfigs);
218 EGLConfig* const configs = new EGLConfig[numConfigs];
219 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
220 for (int i=0 ; i<n ; i++) {
221 EGLint nativeVisualId = 0;
222 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
223 if (nativeVisualId>0 && format == nativeVisualId) {
224 *outConfig = configs[i];
225 delete [] configs;
226 return NO_ERROR;
227 }
228 }
229 delete [] configs;
230 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
Mathias Agopiana4912602012-07-12 14:25:33 -0700233EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
234 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
235 // it is to be used with WIFI displays
236 EGLConfig config;
237 EGLint dummy;
238 status_t err;
239 EGLint attribs[] = {
240 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
241 EGL_RECORDABLE_ANDROID, EGL_TRUE,
242 EGL_NONE
243 };
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 if (err) {
246 // maybe we failed because of EGL_RECORDABLE_ANDROID
247 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
248 attribs[2] = EGL_NONE;
249 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
250 }
251 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
252 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
253 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
254 }
255 return config;
256}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
Mathias Agopiana4912602012-07-12 14:25:33 -0700258EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
259 // Also create our EGLContext
260 EGLint contextAttributes[] = {
261#ifdef EGL_IMG_context_priority
262#ifdef HAS_CONTEXT_PRIORITY
263#warning "using EGL_IMG_context_priority"
264 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
265#endif
266#endif
267 EGL_NONE, EGL_NONE
268 };
269 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
270 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
271 return ctxt;
272}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
Mathias Agopiana4912602012-07-12 14:25:33 -0700274void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
275 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
276 if (!result) {
277 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
278 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 }
280
Mathias Agopiana4912602012-07-12 14:25:33 -0700281 GLExtensions& extensions(GLExtensions::getInstance());
282 extensions.initWithGLStrings(
283 glGetString(GL_VENDOR),
284 glGetString(GL_RENDERER),
285 glGetString(GL_VERSION),
286 glGetString(GL_EXTENSIONS),
287 eglQueryString(display, EGL_VENDOR),
288 eglQueryString(display, EGL_VERSION),
289 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700290
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 EGLint w, h;
292 eglQuerySurface(display, surface, EGL_WIDTH, &w);
293 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294
Mathias Agopiana4912602012-07-12 14:25:33 -0700295 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
296 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700299 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 glShadeModel(GL_FLAT);
302 glDisable(GL_DITHER);
303 glDisable(GL_CULL_FACE);
304
Mathias Agopiana4912602012-07-12 14:25:33 -0700305 struct pack565 {
306 inline uint16_t operator() (int r, int g, int b) const {
307 return (r<<11)|(g<<5)|b;
308 }
309 } pack565;
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
312 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700313 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 glGenTextures(1, &mWormholeTexName);
315 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
316 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
317 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
318 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
319 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
320 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700321 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
322
323 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
324 glGenTextures(1, &mProtectedTexName);
325 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
326 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
327 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
328 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
329 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
331 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332
333 glViewport(0, 0, w, h);
334 glMatrixMode(GL_PROJECTION);
335 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700336 // put the origin in the left-bottom corner
337 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 -0800338
Mathias Agopiana4912602012-07-12 14:25:33 -0700339 // print some debugging info
340 EGLint r,g,b,a;
341 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
342 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
343 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
344 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
345 ALOGI("EGL informations:");
346 ALOGI("vendor : %s", extensions.getEglVendor());
347 ALOGI("version : %s", extensions.getEglVersion());
348 ALOGI("extensions: %s", extensions.getEglExtension());
349 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
350 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
351 ALOGI("OpenGL ES informations:");
352 ALOGI("vendor : %s", extensions.getVendor());
353 ALOGI("renderer : %s", extensions.getRenderer());
354 ALOGI("version : %s", extensions.getVersion());
355 ALOGI("extensions: %s", extensions.getExtension());
356 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
357 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
358}
359
360surface_flinger_cblk_t* SurfaceFlinger::getControlBlock() const {
361 return mServerCblk;
362}
363
364status_t SurfaceFlinger::readyToRun()
365{
366 ALOGI( "SurfaceFlinger's main thread ready to run. "
367 "Initializing graphics H/W...");
368
369 // create the shared control-block
370 mServerHeap = new MemoryHeapBase(4096,
371 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
372 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
373 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
374 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
375 new(mServerCblk) surface_flinger_cblk_t;
376
377
378 // initialize EGL
379 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
380 eglInitialize(display, NULL, NULL);
381
382 // Initialize the main display
383 // create native window to main display
384 sp<FramebufferSurface> anw = FramebufferSurface::create();
385 ANativeWindow* const window = anw.get();
386 if (!window) {
387 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
388 exit(0);
389 }
390
391 // initialize the config and context
392 int format;
393 window->query(window, NATIVE_WINDOW_FORMAT, &format);
394 mEGLConfig = selectEGLConfig(display, format);
395 mEGLContext = createGLContext(display, mEGLConfig);
396
397 // initialize our main display hardware
398 DisplayHardware* const hw = new DisplayHardware(this, 0, anw, mEGLConfig);
399 mDisplayHardwares[0] = hw;
400
401 // initialize OpenGL ES
402 EGLSurface surface = hw->getEGLSurface();
403 initializeGL(display, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800404
405 // start the EventThread
406 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800407 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408
Mathias Agopiana4912602012-07-12 14:25:33 -0700409 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800410 mReadyToRunBarrier.open();
411
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700412 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700413 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 return NO_ERROR;
416}
417
Mathias Agopiana67e4182012-06-19 17:26:12 -0700418void SurfaceFlinger::startBootAnim() {
419 // start boot animation
420 property_set("service.bootanim.exit", "0");
421 property_set("ctl.start", "bootanim");
422}
423
Mathias Agopiana4912602012-07-12 14:25:33 -0700424uint32_t SurfaceFlinger::getMaxTextureSize() const {
425 return mMaxTextureSize;
426}
427
428uint32_t SurfaceFlinger::getMaxViewportDims() const {
429 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
430 mMaxViewportDims[0] : mMaxViewportDims[1];
431}
432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800434
Jamie Gennis582270d2011-08-17 18:19:00 -0700435bool SurfaceFlinger::authenticateSurfaceTexture(
436 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800437 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700438 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800439
440 // Check the visible layer list for the ISurface
441 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
442 size_t count = currentLayers.size();
443 for (size_t i=0 ; i<count ; i++) {
444 const sp<LayerBase>& layer(currentLayers[i]);
445 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700446 if (lbc != NULL) {
447 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
448 if (lbcBinder == surfaceTextureBinder) {
449 return true;
450 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800451 }
452 }
453
454 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700455 // SurfaceTexture gets destroyed before all the clients are done using it,
456 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800457 // will instead fail later on when the client tries to use the surface,
458 // which should be reported as "surface XYZ returned an -ENODEV". The
459 // purgatorized layers are no less authentic than the visible ones, so this
460 // should not cause any harm.
461 size_t purgatorySize = mLayerPurgatory.size();
462 for (size_t i=0 ; i<purgatorySize ; i++) {
463 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
464 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700465 if (lbc != NULL) {
466 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
467 if (lbcBinder == surfaceTextureBinder) {
468 return true;
469 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800470 }
471 }
472
473 return false;
474}
475
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800476// ----------------------------------------------------------------------------
477
478sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800479 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700480}
481
Mathias Agopian3094df32012-06-18 18:06:45 -0700482void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700483 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700484 EGLSurface result = EGL_NO_SURFACE;
485 EGLSurface old_surface = EGL_NO_SURFACE;
486 sp<SurfaceTextureClient> stc;
487
488 if (display != NULL) {
489 stc = new SurfaceTextureClient(display);
490 result = eglCreateWindowSurface(hw.getEGLDisplay(),
Mathias Agopiana4912602012-07-12 14:25:33 -0700491 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700492 ALOGE_IF(result == EGL_NO_SURFACE,
493 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
494 display.get());
495 }
496
497 { // scope for the lock
498 Mutex::Autolock _l(mStateLock);
499 old_surface = mExternalDisplaySurface;
500 mExternalDisplayNativeWindow = stc;
501 mExternalDisplaySurface = result;
502 ALOGD("mExternalDisplaySurface = %p", result);
503 }
504
505 if (old_surface != EGL_NO_SURFACE) {
506 // Note: EGL allows to destroy an object while its current
507 // it will fail to become current next time though.
508 eglDestroySurface(hw.getEGLDisplay(), old_surface);
509 }
510}
511
512EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
513 Mutex::Autolock _l(mStateLock);
514 return mExternalDisplaySurface;
515}
516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800518
519void SurfaceFlinger::waitForEvent() {
520 mEventQueue.waitMessage();
521}
522
523void SurfaceFlinger::signalTransaction() {
524 mEventQueue.invalidate();
525}
526
527void SurfaceFlinger::signalLayerUpdate() {
528 mEventQueue.invalidate();
529}
530
531void SurfaceFlinger::signalRefresh() {
532 mEventQueue.refresh();
533}
534
535status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
536 nsecs_t reltime, uint32_t flags) {
537 return mEventQueue.postMessage(msg, reltime);
538}
539
540status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
541 nsecs_t reltime, uint32_t flags) {
542 status_t res = mEventQueue.postMessage(msg, reltime);
543 if (res == NO_ERROR) {
544 msg->wait();
545 }
546 return res;
547}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548
Mathias Agopian4fec8732012-06-29 14:12:52 -0700549bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 return true;
552}
553
Mathias Agopian4fec8732012-06-29 14:12:52 -0700554void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800555 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800556 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700557 case MessageQueue::INVALIDATE:
558 handleMessageTransaction();
559 handleMessageInvalidate();
560 signalRefresh();
561 break;
562 case MessageQueue::REFRESH:
563 handleMessageRefresh();
564 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800565 }
566}
567
Mathias Agopian4fec8732012-06-29 14:12:52 -0700568void SurfaceFlinger::handleMessageTransaction() {
569 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
570 uint32_t transactionFlags = peekTransactionFlags(mask);
571 if (transactionFlags) {
572 Region dirtyRegion;
573 dirtyRegion = handleTransaction(transactionFlags);
574 // XXX: dirtyRegion should be per screen
575 mDirtyRegion |= dirtyRegion;
576 }
577}
578
579void SurfaceFlinger::handleMessageInvalidate() {
580 Region dirtyRegion;
581 dirtyRegion = handlePageFlip();
582 // XXX: dirtyRegion should be per screen
583 mDirtyRegion |= dirtyRegion;
584}
585
586void SurfaceFlinger::handleMessageRefresh() {
587 handleRefresh();
588
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700589 if (mVisibleRegionsDirty) {
590 Region opaqueRegion;
591 Region dirtyRegion;
592 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
593 computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
594 mDirtyRegion.orSelf(dirtyRegion);
595
596 /*
597 * rebuild the visible layer list per screen
598 */
599
600 // TODO: iterate through all displays
601 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
602
603 Vector< sp<LayerBase> > layersSortedByZ;
604 const size_t count = currentLayers.size();
605 for (size_t i=0 ; i<count ; i++) {
606 if (!currentLayers[i]->visibleRegion.isEmpty()) {
607 // TODO: also check that this layer is associated to this display
608 layersSortedByZ.add(currentLayers[i]);
609 }
610 }
611 hw.setVisibleLayersSortedByZ(layersSortedByZ);
612
613
614 // FIXME: mWormholeRegion needs to be calculated per screen
615 //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
616 mWormholeRegion = Region(hw.getBounds()).subtract(
617 hw.getTransform().transform(opaqueRegion) );
618 mVisibleRegionsDirty = false;
619 invalidateHwcGeometry();
620 }
621
622
Mathias Agopian4fec8732012-06-29 14:12:52 -0700623 // XXX: dirtyRegion should be per screen, we should check all of them
624 if (mDirtyRegion.isEmpty()) {
625 return;
626 }
627
628 // TODO: iterate through all displays
629 const DisplayHardware& hw(getDisplayHardware(0));
630
631 // XXX: dirtyRegion should be per screen
632 // transform the dirty region into this screen's coordinate space
633 const Transform& planeTransform(hw.getTransform());
634 mDirtyRegion = planeTransform.transform(mDirtyRegion);
635 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
636 mDirtyRegion.andSelf(hw.bounds());
637
638
639 if (CC_UNLIKELY(mHwWorkListDirty)) {
640 // build the h/w work list
641 handleWorkList(hw);
642 }
643
644 if (CC_LIKELY(hw.canDraw())) {
645 // repaint the framebuffer (if needed)
646 handleRepaint(hw);
647 // inform the h/w that we're done compositing
648 hw.compositionComplete();
649 postFramebuffer();
650 } else {
651 // pretend we did the post
652 hw.compositionComplete();
653 }
654
655 // render to the external display if we have one
656 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
657 if (externalDisplaySurface != EGL_NO_SURFACE) {
658 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
659 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
660 externalDisplaySurface, externalDisplaySurface,
661 eglGetCurrentContext());
662
663 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
664
665 if (success) {
666 // redraw the screen entirely...
667 glDisable(GL_TEXTURE_EXTERNAL_OES);
668 glDisable(GL_TEXTURE_2D);
669 glClearColor(0,0,0,1);
670 glClear(GL_COLOR_BUFFER_BIT);
671 glMatrixMode(GL_MODELVIEW);
672 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700673
674 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700675 const size_t count = layers.size();
676 for (size_t i=0 ; i<count ; ++i) {
677 const sp<LayerBase>& layer(layers[i]);
678 layer->drawForSreenShot(hw);
679 }
680
681 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
682 ALOGE_IF(!success, "external display eglSwapBuffers failed");
683
684 hw.compositionComplete();
685 }
686
687 success = eglMakeCurrent(eglGetCurrentDisplay(),
688 cur, cur, eglGetCurrentContext());
689
690 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
691 }
692
693}
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695void SurfaceFlinger::postFramebuffer()
696{
Mathias Agopian841cde52012-03-01 15:44:37 -0800697 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800698 // mSwapRegion can be empty here is some cases, for instance if a hidden
699 // or fully transparent window is updating.
700 // in that case, we need to flip anyways to not risk a deadlock with
701 // h/w composer.
702
Mathias Agopian1b031492012-06-20 17:51:20 -0700703 const DisplayHardware& hw(getDefaultDisplayHardware());
Jesse Hallc5c5a142012-07-02 16:49:28 -0700704 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700705 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
706 size_t numLayers = layers.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700707 const nsecs_t now = systemTime();
708 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700709
710 if (hwc.initCheck() == NO_ERROR) {
711 HWComposer::LayerListIterator cur = hwc.begin();
712 const HWComposer::LayerListIterator end = hwc.end();
713 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
714 if (cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700715 layers[i]->setAcquireFence(*cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700716 } else {
717 cur->setAcquireFenceFd(-1);
718 }
719 }
720 }
721
Mathias Agopiana44b0412011-10-16 18:46:35 -0700722 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800723
Jesse Hallef194142012-06-14 14:45:17 -0700724 if (hwc.initCheck() == NO_ERROR) {
725 HWComposer::LayerListIterator cur = hwc.begin();
726 const HWComposer::LayerListIterator end = hwc.end();
727 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700728 layers[i]->onLayerDisplayed(&*cur);
Jesse Hallef194142012-06-14 14:45:17 -0700729 }
730 } else {
731 for (size_t i = 0; i < numLayers; i++) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700732 layers[i]->onLayerDisplayed(NULL);
Jesse Hallef194142012-06-14 14:45:17 -0700733 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800734 }
735
Mathias Agopiana44b0412011-10-16 18:46:35 -0700736 mLastSwapBufferTime = systemTime() - now;
737 mDebugInSwapBuffers = 0;
738 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739}
740
Mathias Agopian4fec8732012-06-29 14:12:52 -0700741Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742{
Mathias Agopian841cde52012-03-01 15:44:37 -0800743 ATRACE_CALL();
744
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745 Region dirtyRegion;
746
Mathias Agopianca4d3602011-05-19 15:38:14 -0700747 Mutex::Autolock _l(mStateLock);
748 const nsecs_t now = systemTime();
749 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750
Mathias Agopianca4d3602011-05-19 15:38:14 -0700751 // Here we're guaranteed that some transaction flags are set
752 // so we can call handleTransactionLocked() unconditionally.
753 // We call getTransactionFlags(), which will also clear the flags,
754 // with mStateLock held to guarantee that mCurrentState won't change
755 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700756
Mathias Agopianca4d3602011-05-19 15:38:14 -0700757 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
758 transactionFlags = getTransactionFlags(mask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700759 dirtyRegion = handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700760
Mathias Agopianca4d3602011-05-19 15:38:14 -0700761 mLastTransactionTime = systemTime() - now;
762 mDebugInTransaction = 0;
763 invalidateHwcGeometry();
764 // here the transaction has been committed
Mathias Agopian4fec8732012-06-29 14:12:52 -0700765
766 return dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700767}
768
Mathias Agopian4fec8732012-06-29 14:12:52 -0700769Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700770{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700771 Region dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700772 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 const size_t count = currentLayers.size();
774
775 /*
776 * Traversal of the children
777 * (perform the transaction for each of them if needed)
778 */
779
780 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
781 if (layersNeedTransaction) {
782 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700783 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
785 if (!trFlags) continue;
786
787 const uint32_t flags = layer->doTransaction(0);
788 if (flags & Layer::eVisibleRegion)
789 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 }
791 }
792
793 /*
794 * Perform our own transaction if needed
795 */
796
797 if (transactionFlags & eTransactionNeeded) {
798 if (mCurrentState.orientation != mDrawingState.orientation) {
799 // the orientation has changed, recompute all visible regions
800 // and invalidate everything.
801
Mathias Agopian1b031492012-06-20 17:51:20 -0700802 const int dpy = 0; // TODO: should be a parameter
803 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 const int orientation = mCurrentState.orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700805 hw.setOrientation(orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806
807 // update the shared control block
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
809 dcblk->orientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700810 dcblk->w = hw.getUserWidth();
811 dcblk->h = hw.getUserHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812
Mathias Agopian1b031492012-06-20 17:51:20 -0700813 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 mVisibleRegionsDirty = true;
815 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 }
817
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700818 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
819 // layers have been added
820 mVisibleRegionsDirty = true;
821 }
822
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 // some layers might have been removed, so
824 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700825 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700826 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700828 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700829 const size_t count = previousLayers.size();
830 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700831 const sp<LayerBase>& layer(previousLayers[i]);
832 if (currentLayers.indexOf( layer ) < 0) {
833 // this layer is not visible anymore
Mathias Agopian4fec8732012-06-29 14:12:52 -0700834 // TODO: we could traverse the tree from front to back and compute the actual visible region
835 // TODO: we could cache the transformed region
836 Layer::State front(layer->drawingState());
837 Region visibleReg = front.transform.transform(
838 Region(Rect(front.active.w, front.active.h)));
839 dirtyRegion.orSelf(visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700840 }
841 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 }
844
845 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700846 return dirtyRegion;
847}
848
849void SurfaceFlinger::commitTransaction()
850{
851 if (!mLayersPendingRemoval.isEmpty()) {
852 // Notify removed layers now that they can't be drawn from
853 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
854 mLayersPendingRemoval[i]->onRemoved();
855 }
856 mLayersPendingRemoval.clear();
857 }
858
859 mDrawingState = mCurrentState;
860 mTransationPending = false;
861 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862}
863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800865 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866{
Mathias Agopian841cde52012-03-01 15:44:37 -0800867 ATRACE_CALL();
868
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869 Region aboveOpaqueLayers;
870 Region aboveCoveredLayers;
871 Region dirty;
872
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700873 dirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874
875 size_t i = currentLayers.size();
876 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700877 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878
879 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700880 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881
Mathias Agopianab028732010-03-16 16:41:46 -0700882 /*
883 * opaqueRegion: area of a surface that is fully opaque.
884 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700886
887 /*
888 * visibleRegion: area of a surface that is visible on screen
889 * and not fully transparent. This is essentially the layer's
890 * footprint minus the opaque regions above it.
891 * Areas covered by a translucent surface are considered visible.
892 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700894
895 /*
896 * coveredRegion: area of a surface that is covered by all
897 * visible regions above it (which includes the translucent areas).
898 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700900
901
902 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800903 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700904 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700905 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700907 if (!visibleRegion.isEmpty()) {
908 // Remove the transparent area from the visible region
909 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700910 Region transparentRegionScreen;
911 const Transform tr(s.transform);
912 if (tr.transformed()) {
913 if (tr.preserveRects()) {
914 // transform the transparent region
915 transparentRegionScreen = tr.transform(s.transparentRegion);
916 } else {
917 // transformation too complex, can't do the
918 // transparent region optimization.
919 transparentRegionScreen.clear();
920 }
921 } else {
922 transparentRegionScreen = s.transparentRegion;
923 }
924 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700925 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926
Mathias Agopianab028732010-03-16 16:41:46 -0700927 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700928 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700929 if (s.alpha==255 && !translucent &&
930 ((layerOrientation & Transform::ROT_INVALID) == false)) {
931 // the opaque region is the layer's footprint
932 opaqueRegion = visibleRegion;
933 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 }
935 }
936
Mathias Agopianab028732010-03-16 16:41:46 -0700937 // Clip the covered region to the visible region
938 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
939
940 // Update aboveCoveredLayers for next (lower) layer
941 aboveCoveredLayers.orSelf(visibleRegion);
942
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 // subtract the opaque region covered by the layers above us
944 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945
946 // compute this layer's dirty region
947 if (layer->contentDirty) {
948 // we need to invalidate the whole region
949 dirty = visibleRegion;
950 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700951 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 layer->contentDirty = false;
953 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700954 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700955 * the exposed region consists of two components:
956 * 1) what's VISIBLE now and was COVERED before
957 * 2) what's EXPOSED now less what was EXPOSED before
958 *
959 * note that (1) is conservative, we start with the whole
960 * visible region but only keep what used to be covered by
961 * something -- which mean it may have been exposed.
962 *
963 * (2) handles areas that were not covered by anything but got
964 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700965 */
Mathias Agopianab028732010-03-16 16:41:46 -0700966 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700967 const Region oldVisibleRegion = layer->visibleRegion;
968 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700969 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
970 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 }
972 dirty.subtractSelf(aboveOpaqueLayers);
973
974 // accumulate to the screen dirty region
975 dirtyRegion.orSelf(dirty);
976
Mathias Agopianab028732010-03-16 16:41:46 -0700977 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700979
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 // Store the visible region is screen space
981 layer->setVisibleRegion(visibleRegion);
982 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 }
984
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 opaqueRegion = aboveOpaqueLayers;
986}
987
Mathias Agopian4fec8732012-06-29 14:12:52 -0700988Region SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800990 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700991 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800993 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800994
Mathias Agopian4fec8732012-06-29 14:12:52 -0700995 bool visibleRegions = false;
996 const size_t count = currentLayers.size();
997 sp<LayerBase> const* layers = currentLayers.array();
998 for (size_t i=0 ; i<count ; i++) {
999 const sp<LayerBase>& layer(layers[i]);
1000 dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
1001 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001002
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001003 mVisibleRegionsDirty |= visibleRegions;
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001004
Mathias Agopian4fec8732012-06-29 14:12:52 -07001005 return dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006}
1007
Mathias Agopianad456f92011-01-13 17:53:01 -08001008void SurfaceFlinger::invalidateHwcGeometry()
1009{
1010 mHwWorkListDirty = true;
1011}
1012
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001013void SurfaceFlinger::handleRefresh()
1014{
1015 bool needInvalidate = false;
1016 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1017 const size_t count = currentLayers.size();
1018 for (size_t i=0 ; i<count ; i++) {
1019 const sp<LayerBase>& layer(currentLayers[i]);
1020 if (layer->onPreComposition()) {
1021 needInvalidate = true;
1022 }
1023 }
1024 if (needInvalidate) {
1025 signalLayerUpdate();
1026 }
1027}
1028
1029
Mathias Agopian1b031492012-06-20 17:51:20 -07001030void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -07001031{
1032 mHwWorkListDirty = false;
Mathias Agopian1b031492012-06-20 17:51:20 -07001033 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001034 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001035 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001036 const size_t count = currentLayers.size();
1037 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001038
1039 HWComposer::LayerListIterator cur = hwc.begin();
1040 const HWComposer::LayerListIterator end = hwc.end();
1041 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001042 currentLayers[i]->setGeometry(hw, *cur);
Mathias Agopian53331da2011-08-22 21:44:41 -07001043 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001044 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001045 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001046 }
1047 }
1048}
Mathias Agopianb8a55602009-06-26 19:06:36 -07001049
Mathias Agopian1b031492012-06-20 17:51:20 -07001050void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051{
Mathias Agopian841cde52012-03-01 15:44:37 -08001052 ATRACE_CALL();
1053
Mathias Agopianb8a55602009-06-26 19:06:36 -07001054 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -07001055 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056
Glenn Kasten99ed2242011-12-15 09:51:17 -08001057 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -07001058 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 }
1060
Mathias Agopianb8a55602009-06-26 19:06:36 -07001061 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -07001062 glMatrixMode(GL_MODELVIEW);
1063 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
1065 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001066 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001067 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1068 // takes a rectangle, we must make sure to update that whole
1069 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001070 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001071 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -07001072 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001073 // We need to redraw the rectangle that will be updated
1074 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001075 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001076 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -07001077 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001078 } else {
1079 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -07001081 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 }
1083 }
1084
Mathias Agopian1b031492012-06-20 17:51:20 -07001085 setupHardwareComposer(hw);
1086 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001088 // update the swap region and clear the dirty region
1089 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 mDirtyRegion.clear();
1091}
1092
Mathias Agopian1b031492012-06-20 17:51:20 -07001093void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094{
Mathias Agopiana350ff92010-08-10 17:14:02 -07001095 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001096 HWComposer::LayerListIterator cur = hwc.begin();
1097 const HWComposer::LayerListIterator end = hwc.end();
1098 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001099 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -07001100 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001101
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001102 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001103 size_t count = layers.size();
1104
Steve Blocke6f43dd2012-01-06 19:20:56 +00001105 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -07001106 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
1107 hwc.getNumLayers(), count);
1108
1109 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -07001110 if (hwc.initCheck() == NO_ERROR) {
1111 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
1112 }
Mathias Agopian45721772010-08-12 15:03:26 -07001113
1114 /*
1115 * update the per-frame h/w composer data for each layer
1116 * and build the transparent region of the FB
1117 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001118 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001119 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001120 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -07001121 }
1122 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001123 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -07001124}
Mathias Agopian45721772010-08-12 15:03:26 -07001125
Mathias Agopian1b031492012-06-20 17:51:20 -07001126void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001127{
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001128 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001129 HWComposer::LayerListIterator cur = hwc.begin();
1130 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001131
1132 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001133 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001134 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -07001135
Mathias Agopianb9494d52012-04-18 02:28:45 -07001136 if (hwc.getLayerCount(HWC_OVERLAY)) {
1137 // when using overlays, we assume a fully transparent framebuffer
1138 // NOTE: we could reduce how much we need to clear, for instance
1139 // remove where there are opaque FB layers. however, on some
1140 // GPUs doing a "clean slate" glClear might be more efficient.
1141 // We'll revisit later if needed.
1142 glClearColor(0, 0, 0, 0);
1143 glClear(GL_COLOR_BUFFER_BIT);
1144 } else {
1145 // screen is already cleared here
1146 if (!mWormholeRegion.isEmpty()) {
1147 // can happen with SurfaceView
1148 drawWormhole();
1149 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001150 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001151
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001152 /*
1153 * and then, render the layers targeted at the framebuffer
1154 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001155
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001156 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001157 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001158 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001159 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001160 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001161 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001162 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001163 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001164 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001165 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001166 // never clear the very first layer since we're
1167 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001168 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001169 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001170 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001171 continue;
1172 }
1173 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001174 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001175 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001176 if (cur != end) {
1177 ++cur;
1178 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001179 }
1180 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181}
1182
Mathias Agopian1b031492012-06-20 17:51:20 -07001183void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184{
Mathias Agopian0a917752010-06-14 21:20:00 -07001185 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001186 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001187 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001188 return;
1189 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001190
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001191 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001192 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1193 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001194 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001195 }
1196
Mathias Agopianc492e672011-10-18 14:49:27 -07001197 glDisable(GL_TEXTURE_EXTERNAL_OES);
1198 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200
Mathias Agopian0926f502009-05-04 14:17:04 -07001201 static int toggle = 0;
1202 toggle = 1 - toggle;
1203 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001204 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001205 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001206 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208
Mathias Agopian20f68782009-05-11 00:03:41 -07001209 Region::const_iterator it = mDirtyRegion.begin();
1210 Region::const_iterator const end = mDirtyRegion.end();
1211 while (it != end) {
1212 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001214 { r.left, height - r.top },
1215 { r.left, height - r.bottom },
1216 { r.right, height - r.bottom },
1217 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 };
1219 glVertexPointer(2, GL_FLOAT, 0, vertices);
1220 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1221 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001222
Mathias Agopian0656a682011-09-20 17:22:44 -07001223 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224
1225 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001226 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227}
1228
1229void SurfaceFlinger::drawWormhole() const
1230{
1231 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1232 if (region.isEmpty())
1233 return;
1234
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001235 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001236 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001237 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001238 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001239
1240 GLfloat vertices[4][2];
1241 glVertexPointer(2, GL_FLOAT, 0, vertices);
1242 Region::const_iterator it = region.begin();
1243 Region::const_iterator const end = region.end();
1244 while (it != end) {
1245 const Rect& r = *it++;
1246 vertices[0][0] = r.left;
1247 vertices[0][1] = r.top;
1248 vertices[1][0] = r.right;
1249 vertices[1][1] = r.top;
1250 vertices[2][0] = r.right;
1251 vertices[2][1] = r.bottom;
1252 vertices[3][0] = r.left;
1253 vertices[3][1] = r.bottom;
1254 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1255 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256}
1257
Mathias Agopian96f08192010-06-02 23:28:45 -07001258ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1259 const sp<LayerBaseClient>& lbc)
1260{
Mathias Agopian96f08192010-06-02 23:28:45 -07001261 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001262 size_t name = client->attachLayer(lbc);
1263
Mathias Agopian96f08192010-06-02 23:28:45 -07001264 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001265 Mutex::Autolock _l(mStateLock);
1266 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001267
Mathias Agopian4f113742011-05-03 16:21:41 -07001268 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001269}
1270
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001271status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272{
1273 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001274 status_t err = purgatorizeLayer_l(layer);
1275 if (err == NO_ERROR)
1276 setTransactionFlags(eTransactionNeeded);
1277 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278}
1279
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001280status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281{
1282 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1283 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001284 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 return NO_ERROR;
1286 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001287 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288}
1289
Mathias Agopian9a112062009-04-17 19:36:26 -07001290status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1291{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001292 // First add the layer to the purgatory list, which makes sure it won't
1293 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001294 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001295 if (err >= 0) {
1296 mLayerPurgatory.add(layerBase);
1297 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001298
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001299 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001300
Mathias Agopian3d579642009-06-04 18:46:21 -07001301 // it's possible that we don't find a layer, because it might
1302 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001303 // from the user because there is a race between Client::destroySurface(),
1304 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001305 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1306}
1307
Mathias Agopiandea20b12011-05-03 17:04:02 -07001308uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1309{
1310 return android_atomic_release_load(&mTransactionFlags);
1311}
1312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1314{
1315 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1316}
1317
Mathias Agopianbb641242010-05-18 17:06:55 -07001318uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319{
1320 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1321 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001322 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 }
1324 return old;
1325}
1326
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001328void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001329 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001330 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001331
Jamie Gennis28378392011-10-12 17:39:00 -07001332 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001333 if (mCurrentState.orientation != orientation) {
1334 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1335 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001336 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001337 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001338 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001339 orientation);
1340 }
1341 }
1342
Mathias Agopian698c0872011-06-28 19:09:31 -07001343 const size_t count = state.size();
1344 for (size_t i=0 ; i<count ; i++) {
1345 const ComposerState& s(state[i]);
1346 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001347 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001348 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001349
Mathias Agopian386aa982011-11-07 21:58:03 -08001350 if (transactionFlags) {
1351 // this triggers the transaction
1352 setTransactionFlags(transactionFlags);
1353
1354 // if this is a synchronous transaction, wait for it to take effect
1355 // before returning.
1356 if (flags & eSynchronous) {
1357 mTransationPending = true;
1358 }
1359 while (mTransationPending) {
1360 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1361 if (CC_UNLIKELY(err != NO_ERROR)) {
1362 // just in case something goes wrong in SF, return to the
1363 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001364 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001365 mTransationPending = false;
1366 break;
1367 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001368 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 }
1370}
1371
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001372sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001373 ISurfaceComposerClient::surface_data_t* params,
1374 const String8& name,
1375 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1377 uint32_t flags)
1378{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001379 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001380 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001381
1382 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001383 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001384 int(w), int(h));
1385 return surfaceHandle;
1386 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001387
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001388 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 switch (flags & eFXSurfaceMask) {
1390 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001391 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 break;
1393 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001394 // for now we treat Blur as Dim, until we can implement it
1395 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001397 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001399 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001400 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001401 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 }
1403
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001404 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001405 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001406 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001407 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001409 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001410 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001411 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001412 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001413 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 }
1415
1416 return surfaceHandle;
1417}
1418
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001419sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001420 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001421 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001422 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423{
1424 // initialize the surfaces
1425 switch (format) { // TODO: take h/w into account
1426 case PIXEL_FORMAT_TRANSPARENT:
1427 case PIXEL_FORMAT_TRANSLUCENT:
1428 format = PIXEL_FORMAT_RGBA_8888;
1429 break;
1430 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001431#ifdef NO_RGBX_8888
1432 format = PIXEL_FORMAT_RGB_565;
1433#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001434 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001435#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 break;
1437 }
1438
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001439#ifdef NO_RGBX_8888
1440 if (format == PIXEL_FORMAT_RGBX_8888)
1441 format = PIXEL_FORMAT_RGBA_8888;
1442#endif
1443
Mathias Agopian96f08192010-06-02 23:28:45 -07001444 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001445 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001446 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001447 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001448 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 }
1450 return layer;
1451}
1452
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001453sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001454 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001455 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456{
Mathias Agopian96f08192010-06-02 23:28:45 -07001457 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001458 return layer;
1459}
1460
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001461sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001462 const sp<Client>& client, DisplayID display,
1463 uint32_t w, uint32_t h, uint32_t flags)
1464{
1465 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 return layer;
1467}
1468
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001469status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470{
Mathias Agopian9a112062009-04-17 19:36:26 -07001471 /*
1472 * called by the window manager, when a surface should be marked for
1473 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001474 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001475 * The surface is removed from the current and drawing lists, but placed
1476 * in the purgatory queue, so it's not destroyed right-away (we need
1477 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001478 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001479
Mathias Agopian48d819a2009-09-10 19:41:18 -07001480 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001481 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001482 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001483
Mathias Agopian48d819a2009-09-10 19:41:18 -07001484 if (layer != 0) {
1485 err = purgatorizeLayer_l(layer);
1486 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001487 setTransactionFlags(eTransactionNeeded);
1488 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001489 }
1490 return err;
1491}
1492
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001493status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001494{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001495 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001496 status_t err = NO_ERROR;
1497 sp<LayerBaseClient> l(layer.promote());
1498 if (l != NULL) {
1499 Mutex::Autolock _l(mStateLock);
1500 err = removeLayer_l(l);
1501 if (err == NAME_NOT_FOUND) {
1502 // The surface wasn't in the current list, which means it was
1503 // removed already, which means it is in the purgatory,
1504 // and need to be removed from there.
1505 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001506 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001507 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001508 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001509 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001510 "error removing layer=%p (%s)", l.get(), strerror(-err));
1511 }
1512 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513}
1514
Mathias Agopian698c0872011-06-28 19:09:31 -07001515uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001516 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001517 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001520 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1521 if (layer != 0) {
1522 const uint32_t what = s.what;
1523 if (what & ePositionChanged) {
1524 if (layer->setPosition(s.x, s.y))
1525 flags |= eTraversalNeeded;
1526 }
1527 if (what & eLayerChanged) {
1528 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1529 if (layer->setLayer(s.z)) {
1530 mCurrentState.layersSortedByZ.removeAt(idx);
1531 mCurrentState.layersSortedByZ.add(layer);
1532 // we need traversal (state changed)
1533 // AND transaction (list changed)
1534 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 }
1536 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001537 if (what & eSizeChanged) {
1538 if (layer->setSize(s.w, s.h)) {
1539 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001540 }
1541 }
1542 if (what & eAlphaChanged) {
1543 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1544 flags |= eTraversalNeeded;
1545 }
1546 if (what & eMatrixChanged) {
1547 if (layer->setMatrix(s.matrix))
1548 flags |= eTraversalNeeded;
1549 }
1550 if (what & eTransparentRegionChanged) {
1551 if (layer->setTransparentRegionHint(s.transparentRegion))
1552 flags |= eTraversalNeeded;
1553 }
1554 if (what & eVisibilityChanged) {
1555 if (layer->setFlags(s.flags, s.mask))
1556 flags |= eTraversalNeeded;
1557 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001558 if (what & eCropChanged) {
1559 if (layer->setCrop(s.crop))
1560 flags |= eTraversalNeeded;
1561 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001563 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564}
1565
Mathias Agopianb60314a2012-04-10 22:09:54 -07001566// ---------------------------------------------------------------------------
1567
1568void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001569 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001570 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001571 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001572 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001573 // this is a temporary work-around, eventually this should be called
1574 // by the power-manager
1575 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001576 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001577 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578}
1579
Mathias Agopianb60314a2012-04-10 22:09:54 -07001580void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001581 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001582 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001583 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001584 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001585 hw.releaseScreen();
1586 // from this point on, SF will stop drawing
1587 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588}
1589
Colin Cross8e533062012-06-07 13:17:52 -07001590void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001591 class MessageScreenAcquired : public MessageBase {
1592 SurfaceFlinger* flinger;
1593 public:
1594 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1595 virtual bool handler() {
1596 flinger->onScreenAcquired();
1597 return true;
1598 }
1599 };
1600 sp<MessageBase> msg = new MessageScreenAcquired(this);
1601 postMessageSync(msg);
1602}
1603
Colin Cross8e533062012-06-07 13:17:52 -07001604void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001605 class MessageScreenReleased : public MessageBase {
1606 SurfaceFlinger* flinger;
1607 public:
1608 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1609 virtual bool handler() {
1610 flinger->onScreenReleased();
1611 return true;
1612 }
1613 };
1614 sp<MessageBase> msg = new MessageScreenReleased(this);
1615 postMessageSync(msg);
1616}
1617
1618// ---------------------------------------------------------------------------
1619
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1621{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001622 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 char buffer[SIZE];
1624 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001625
1626 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627 snprintf(buffer, SIZE, "Permission Denial: "
1628 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1629 IPCThreadState::self()->getCallingPid(),
1630 IPCThreadState::self()->getCallingUid());
1631 result.append(buffer);
1632 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001633 // Try to get the main lock, but don't insist if we can't
1634 // (this would indicate SF is stuck, but we want to be able to
1635 // print something in dumpsys).
1636 int retry = 3;
1637 while (mStateLock.tryLock()<0 && --retry>=0) {
1638 usleep(1000000);
1639 }
1640 const bool locked(retry >= 0);
1641 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001642 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001643 "SurfaceFlinger appears to be unresponsive, "
1644 "dumping anyways (no locks held)\n");
1645 result.append(buffer);
1646 }
1647
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001648 bool dumpAll = true;
1649 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001650 size_t numArgs = args.size();
1651 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001652 if ((index < numArgs) &&
1653 (args[index] == String16("--list"))) {
1654 index++;
1655 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001656 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001657 }
1658
1659 if ((index < numArgs) &&
1660 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001661 index++;
1662 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001663 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001664 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001665
1666 if ((index < numArgs) &&
1667 (args[index] == String16("--latency-clear"))) {
1668 index++;
1669 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001670 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001671 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001673
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001674 if (dumpAll) {
1675 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001676 }
1677
Mathias Agopian9795c422009-08-26 16:36:26 -07001678 if (locked) {
1679 mStateLock.unlock();
1680 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 }
1682 write(fd, result.string(), result.size());
1683 return NO_ERROR;
1684}
1685
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001686void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1687 String8& result, char* buffer, size_t SIZE) const
1688{
1689 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1690 const size_t count = currentLayers.size();
1691 for (size_t i=0 ; i<count ; i++) {
1692 const sp<LayerBase>& layer(currentLayers[i]);
1693 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1694 result.append(buffer);
1695 }
1696}
1697
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001698void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1699 String8& result, char* buffer, size_t SIZE) const
1700{
1701 String8 name;
1702 if (index < args.size()) {
1703 name = String8(args[index]);
1704 index++;
1705 }
1706
1707 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1708 const size_t count = currentLayers.size();
1709 for (size_t i=0 ; i<count ; i++) {
1710 const sp<LayerBase>& layer(currentLayers[i]);
1711 if (name.isEmpty()) {
1712 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1713 result.append(buffer);
1714 }
1715 if (name.isEmpty() || (name == layer->getName())) {
1716 layer->dumpStats(result, buffer, SIZE);
1717 }
1718 }
1719}
1720
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001721void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1722 String8& result, char* buffer, size_t SIZE) const
1723{
1724 String8 name;
1725 if (index < args.size()) {
1726 name = String8(args[index]);
1727 index++;
1728 }
1729
1730 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1731 const size_t count = currentLayers.size();
1732 for (size_t i=0 ; i<count ; i++) {
1733 const sp<LayerBase>& layer(currentLayers[i]);
1734 if (name.isEmpty() || (name == layer->getName())) {
1735 layer->clearStats();
1736 }
1737 }
1738}
1739
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001740void SurfaceFlinger::dumpAllLocked(
1741 String8& result, char* buffer, size_t SIZE) const
1742{
1743 // figure out if we're stuck somewhere
1744 const nsecs_t now = systemTime();
1745 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1746 const nsecs_t inTransaction(mDebugInTransaction);
1747 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1748 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1749
1750 /*
1751 * Dump the visible layer list
1752 */
1753 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1754 const size_t count = currentLayers.size();
1755 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1756 result.append(buffer);
1757 for (size_t i=0 ; i<count ; i++) {
1758 const sp<LayerBase>& layer(currentLayers[i]);
1759 layer->dump(result, buffer, SIZE);
1760 }
1761
1762 /*
1763 * Dump the layers in the purgatory
1764 */
1765
1766 const size_t purgatorySize = mLayerPurgatory.size();
1767 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1768 result.append(buffer);
1769 for (size_t i=0 ; i<purgatorySize ; i++) {
1770 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1771 layer->shortDump(result, buffer, SIZE);
1772 }
1773
1774 /*
1775 * Dump SurfaceFlinger global state
1776 */
1777
1778 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1779 result.append(buffer);
1780
Mathias Agopian1b031492012-06-20 17:51:20 -07001781 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001782 const GLExtensions& extensions(GLExtensions::getInstance());
1783 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1784 extensions.getVendor(),
1785 extensions.getRenderer(),
1786 extensions.getVersion());
1787 result.append(buffer);
1788
1789 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001790 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001791 EGL_VERSION_HW_ANDROID));
1792 result.append(buffer);
1793
1794 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1795 result.append(buffer);
1796
1797 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001798 snprintf(buffer, SIZE,
1799 " orientation=%d, canDraw=%d\n",
1800 mCurrentState.orientation, hw.canDraw());
1801 result.append(buffer);
1802 snprintf(buffer, SIZE,
1803 " last eglSwapBuffers() time: %f us\n"
1804 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001805 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001806 " refresh-rate : %f fps\n"
1807 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001808 " y-dpi : %f\n"
1809 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001810 mLastSwapBufferTime/1000.0,
1811 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001812 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001813 hw.getRefreshRate(),
1814 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001815 hw.getDpiY(),
1816 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001817 result.append(buffer);
1818
1819 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1820 inSwapBuffersDuration/1000.0);
1821 result.append(buffer);
1822
1823 snprintf(buffer, SIZE, " transaction time: %f us\n",
1824 inTransactionDuration/1000.0);
1825 result.append(buffer);
1826
1827 /*
1828 * VSYNC state
1829 */
1830 mEventThread->dump(result, buffer, SIZE);
1831
1832 /*
1833 * Dump HWComposer state
1834 */
1835 HWComposer& hwc(hw.getHwComposer());
1836 snprintf(buffer, SIZE, "h/w composer state:\n");
1837 result.append(buffer);
1838 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1839 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1840 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1841 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001842 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001843
1844 /*
1845 * Dump gralloc state
1846 */
1847 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1848 alloc.dump(result);
1849 hw.dump(result);
1850}
1851
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001852status_t SurfaceFlinger::onTransact(
1853 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1854{
1855 switch (code) {
1856 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001857 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001859 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001860 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001862 case BLANK:
1863 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001864 {
1865 // codes that require permission check
1866 IPCThreadState* ipc = IPCThreadState::self();
1867 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001868 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001869 if ((uid != AID_GRAPHICS) &&
1870 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001871 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001872 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1873 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001875 break;
1876 }
1877 case CAPTURE_SCREEN:
1878 {
1879 // codes that require permission check
1880 IPCThreadState* ipc = IPCThreadState::self();
1881 const int pid = ipc->getCallingPid();
1882 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001883 if ((uid != AID_GRAPHICS) &&
1884 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001885 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001886 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1887 return PERMISSION_DENIED;
1888 }
1889 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 }
1891 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001892
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1894 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001895 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001896 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001897 IPCThreadState* ipc = IPCThreadState::self();
1898 const int pid = ipc->getCallingPid();
1899 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001900 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001901 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 return PERMISSION_DENIED;
1903 }
1904 int n;
1905 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001906 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001907 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 return NO_ERROR;
1909 case 1002: // SHOW_UPDATES
1910 n = data.readInt32();
1911 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001912 invalidateHwcGeometry();
1913 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001916 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001917 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001919 case 1005:{ // force transaction
1920 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1921 return NO_ERROR;
1922 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001923 case 1006:{ // send empty update
1924 signalRefresh();
1925 return NO_ERROR;
1926 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001927 case 1008: // toggle use of hw composer
1928 n = data.readInt32();
1929 mDebugDisableHWC = n ? 1 : 0;
1930 invalidateHwcGeometry();
1931 repaintEverything();
1932 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001933 case 1009: // toggle use of transform hint
1934 n = data.readInt32();
1935 mDebugDisableTransformHint = n ? 1 : 0;
1936 invalidateHwcGeometry();
1937 repaintEverything();
1938 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001940 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941 reply->writeInt32(0);
1942 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001943 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001944 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 return NO_ERROR;
1946 case 1013: {
1947 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001948 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949 reply->writeInt32(hw.getPageFlipCount());
1950 }
1951 return NO_ERROR;
1952 }
1953 }
1954 return err;
1955}
1956
Mathias Agopian53331da2011-08-22 21:44:41 -07001957void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001958 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001959 const Rect bounds(hw.getBounds());
1960 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001961 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001962}
1963
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001964void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1965 Mutex::Autolock _l(mInvalidateLock);
1966 mInvalidateRegion = reg;
1967}
1968
1969Region SurfaceFlinger::getAndClearInvalidateRegion() {
1970 Mutex::Autolock _l(mInvalidateLock);
1971 Region reg(mInvalidateRegion);
1972 mInvalidateRegion.clear();
1973 return reg;
1974}
1975
Mathias Agopian59119e62010-10-11 12:37:43 -07001976// ---------------------------------------------------------------------------
1977
Mathias Agopian118d0242011-10-13 16:02:48 -07001978status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1979 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1980{
1981 Mutex::Autolock _l(mStateLock);
1982 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1983}
1984
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1986 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001987{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001988 ATRACE_CALL();
1989
Mathias Agopian59119e62010-10-11 12:37:43 -07001990 if (!GLExtensions::getInstance().haveFramebufferObject())
1991 return INVALID_OPERATION;
1992
1993 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001994 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07001995 const uint32_t hw_w = hw.getWidth();
1996 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001997 GLfloat u = 1;
1998 GLfloat v = 1;
1999
2000 // make sure to clear all GL error flags
2001 while ( glGetError() != GL_NO_ERROR ) ;
2002
2003 // create a FBO
2004 GLuint name, tname;
2005 glGenTextures(1, &tname);
2006 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002007 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2008 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2010 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002011 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002012 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 GLint tw = (2 << (31 - clz(hw_w)));
2014 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002015 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2016 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 u = GLfloat(hw_w) / tw;
2018 v = GLfloat(hw_h) / th;
2019 }
2020 glGenFramebuffersOES(1, &name);
2021 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002022 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2023 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002024
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002026 glDisable(GL_TEXTURE_EXTERNAL_OES);
2027 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002028 glClearColor(0,0,0,1);
2029 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002030 glMatrixMode(GL_MODELVIEW);
2031 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002032 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 const size_t count = layers.size();
2034 for (size_t i=0 ; i<count ; ++i) {
2035 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07002036 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002037 }
2038
Mathias Agopian118d0242011-10-13 16:02:48 -07002039 hw.compositionComplete();
2040
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002041 // back to main framebuffer
2042 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002043 glDeleteFramebuffersOES(1, &name);
2044
2045 *textureName = tname;
2046 *uOut = u;
2047 *vOut = v;
2048 return NO_ERROR;
2049}
2050
2051// ---------------------------------------------------------------------------
2052
Mathias Agopianed9807b2012-05-18 14:18:08 -07002053class VSyncWaiter {
2054 DisplayEventReceiver::Event buffer[4];
2055 sp<Looper> looper;
2056 sp<IDisplayEventConnection> events;
2057 sp<BitTube> eventTube;
2058public:
2059 VSyncWaiter(const sp<EventThread>& eventThread) {
2060 looper = new Looper(true);
2061 events = eventThread->createEventConnection();
2062 eventTube = events->getDataChannel();
2063 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
2064 events->requestNextVsync();
2065 }
2066
2067 void wait() {
2068 ssize_t n;
2069
2070 looper->pollOnce(-1);
2071 // we don't handle any errors here, it doesn't matter
2072 // and we don't want to take the risk to get stuck.
2073
2074 // drain the events...
2075 while ((n = DisplayEventReceiver::getEvents(
2076 eventTube, buffer, 4)) > 0) ;
2077
2078 events->requestNextVsync();
2079 }
2080};
2081
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
2083{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002084 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002085 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002086 const uint32_t hw_w = hw.getWidth();
2087 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07002088 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002089
2090 GLfloat u, v;
2091 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002092 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002093 if (result != NO_ERROR) {
2094 return result;
2095 }
2096
2097 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002098 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002099 glBindTexture(GL_TEXTURE_2D, tname);
2100 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2101 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2102 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002103 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2104 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002105 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2106 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2107 glVertexPointer(2, GL_FLOAT, 0, vtx);
2108
Mathias Agopianffcf4652011-07-07 17:30:31 -07002109 /*
2110 * Texture coordinate mapping
2111 *
2112 * u
2113 * 1 +----------+---+
2114 * | | | | image is inverted
2115 * | V | | w.r.t. the texture
2116 * 1-v +----------+ | coordinates
2117 * | |
2118 * | |
2119 * | |
2120 * 0 +--------------+
2121 * 0 1
2122 *
2123 */
2124
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002125 class s_curve_interpolator {
2126 const float nbFrames, s, v;
2127 public:
2128 s_curve_interpolator(int nbFrames, float s)
2129 : nbFrames(1.0f / (nbFrames-1)), s(s),
2130 v(1.0f + expf(-s + 0.5f*s)) {
2131 }
2132 float operator()(int f) {
2133 const float x = f * nbFrames;
2134 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2135 }
2136 };
2137
2138 class v_stretch {
2139 const GLfloat hw_w, hw_h;
2140 public:
2141 v_stretch(uint32_t hw_w, uint32_t hw_h)
2142 : hw_w(hw_w), hw_h(hw_h) {
2143 }
2144 void operator()(GLfloat* vtx, float v) {
2145 const GLfloat w = hw_w + (hw_w * v);
2146 const GLfloat h = hw_h - (hw_h * v);
2147 const GLfloat x = (hw_w - w) * 0.5f;
2148 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002149 vtx[0] = x; vtx[1] = y;
2150 vtx[2] = x; vtx[3] = y + h;
2151 vtx[4] = x + w; vtx[5] = y + h;
2152 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002153 }
2154 };
2155
2156 class h_stretch {
2157 const GLfloat hw_w, hw_h;
2158 public:
2159 h_stretch(uint32_t hw_w, uint32_t hw_h)
2160 : hw_w(hw_w), hw_h(hw_h) {
2161 }
2162 void operator()(GLfloat* vtx, float v) {
2163 const GLfloat w = hw_w - (hw_w * v);
2164 const GLfloat h = 1.0f;
2165 const GLfloat x = (hw_w - w) * 0.5f;
2166 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002167 vtx[0] = x; vtx[1] = y;
2168 vtx[2] = x; vtx[3] = y + h;
2169 vtx[4] = x + w; vtx[5] = y + h;
2170 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002171 }
2172 };
2173
Mathias Agopianed9807b2012-05-18 14:18:08 -07002174 VSyncWaiter vsync(mEventThread);
2175
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002176 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002177 char value[PROPERTY_VALUE_MAX];
2178 property_get("debug.sf.electron_frames", value, "24");
2179 int nbFrames = (atoi(value) + 1) >> 1;
2180 if (nbFrames <= 0) // just in case
2181 nbFrames = 24;
2182
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002183 s_curve_interpolator itr(nbFrames, 7.5f);
2184 s_curve_interpolator itg(nbFrames, 8.0f);
2185 s_curve_interpolator itb(nbFrames, 8.5f);
2186
2187 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002188
2189 glMatrixMode(GL_TEXTURE);
2190 glLoadIdentity();
2191 glMatrixMode(GL_MODELVIEW);
2192 glLoadIdentity();
2193
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002194 glEnable(GL_BLEND);
2195 glBlendFunc(GL_ONE, GL_ONE);
2196 for (int i=0 ; i<nbFrames ; i++) {
2197 float x, y, w, h;
2198 const float vr = itr(i);
2199 const float vg = itg(i);
2200 const float vb = itb(i);
2201
Mathias Agopianed9807b2012-05-18 14:18:08 -07002202 // wait for vsync
2203 vsync.wait();
2204
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 // clear screen
2206 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002207 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002208 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002209
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002210 // draw the red plane
2211 vverts(vtx, vr);
2212 glColorMask(1,0,0,1);
2213 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002214
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002215 // draw the green plane
2216 vverts(vtx, vg);
2217 glColorMask(0,1,0,1);
2218 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002219
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002220 // draw the blue plane
2221 vverts(vtx, vb);
2222 glColorMask(0,0,1,1);
2223 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002224
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002225 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002226 glDisable(GL_TEXTURE_2D);
2227 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002228 glColor4f(vg, vg, vg, 1);
2229 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2230 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002231 }
2232
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002233 h_stretch hverts(hw_w, hw_h);
2234 glDisable(GL_BLEND);
2235 glDisable(GL_TEXTURE_2D);
2236 glColorMask(1,1,1,1);
2237 for (int i=0 ; i<nbFrames ; i++) {
2238 const float v = itg(i);
2239 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002240
2241 // wait for vsync
2242 vsync.wait();
2243
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244 glClear(GL_COLOR_BUFFER_BIT);
2245 glColor4f(1-v, 1-v, 1-v, 1);
2246 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2247 hw.flip(screenBounds);
2248 }
2249
2250 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002251 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2252 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002253 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002254 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002255 return NO_ERROR;
2256}
2257
2258status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2259{
2260 status_t result = PERMISSION_DENIED;
2261
2262 if (!GLExtensions::getInstance().haveFramebufferObject())
2263 return INVALID_OPERATION;
2264
2265
2266 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002267 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002268 const uint32_t hw_w = hw.getWidth();
2269 const uint32_t hw_h = hw.getHeight();
2270 const Region screenBounds(hw.bounds());
2271
2272 GLfloat u, v;
2273 GLuint tname;
2274 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2275 if (result != NO_ERROR) {
2276 return result;
2277 }
2278
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002279 GLfloat vtx[8];
2280 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002281 glBindTexture(GL_TEXTURE_2D, tname);
2282 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002287 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2288 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2289 glVertexPointer(2, GL_FLOAT, 0, vtx);
2290
2291 class s_curve_interpolator {
2292 const float nbFrames, s, v;
2293 public:
2294 s_curve_interpolator(int nbFrames, float s)
2295 : nbFrames(1.0f / (nbFrames-1)), s(s),
2296 v(1.0f + expf(-s + 0.5f*s)) {
2297 }
2298 float operator()(int f) {
2299 const float x = f * nbFrames;
2300 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2301 }
2302 };
2303
2304 class v_stretch {
2305 const GLfloat hw_w, hw_h;
2306 public:
2307 v_stretch(uint32_t hw_w, uint32_t hw_h)
2308 : hw_w(hw_w), hw_h(hw_h) {
2309 }
2310 void operator()(GLfloat* vtx, float v) {
2311 const GLfloat w = hw_w + (hw_w * v);
2312 const GLfloat h = hw_h - (hw_h * v);
2313 const GLfloat x = (hw_w - w) * 0.5f;
2314 const GLfloat y = (hw_h - h) * 0.5f;
2315 vtx[0] = x; vtx[1] = y;
2316 vtx[2] = x; vtx[3] = y + h;
2317 vtx[4] = x + w; vtx[5] = y + h;
2318 vtx[6] = x + w; vtx[7] = y;
2319 }
2320 };
2321
2322 class h_stretch {
2323 const GLfloat hw_w, hw_h;
2324 public:
2325 h_stretch(uint32_t hw_w, uint32_t hw_h)
2326 : hw_w(hw_w), hw_h(hw_h) {
2327 }
2328 void operator()(GLfloat* vtx, float v) {
2329 const GLfloat w = hw_w - (hw_w * v);
2330 const GLfloat h = 1.0f;
2331 const GLfloat x = (hw_w - w) * 0.5f;
2332 const GLfloat y = (hw_h - h) * 0.5f;
2333 vtx[0] = x; vtx[1] = y;
2334 vtx[2] = x; vtx[3] = y + h;
2335 vtx[4] = x + w; vtx[5] = y + h;
2336 vtx[6] = x + w; vtx[7] = y;
2337 }
2338 };
2339
Mathias Agopianed9807b2012-05-18 14:18:08 -07002340 VSyncWaiter vsync(mEventThread);
2341
Mathias Agopiana6546e52010-10-14 12:33:07 -07002342 // the full animation is 12 frames
2343 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002344 s_curve_interpolator itr(nbFrames, 7.5f);
2345 s_curve_interpolator itg(nbFrames, 8.0f);
2346 s_curve_interpolator itb(nbFrames, 8.5f);
2347
2348 h_stretch hverts(hw_w, hw_h);
2349 glDisable(GL_BLEND);
2350 glDisable(GL_TEXTURE_2D);
2351 glColorMask(1,1,1,1);
2352 for (int i=nbFrames-1 ; i>=0 ; i--) {
2353 const float v = itg(i);
2354 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002355
2356 // wait for vsync
2357 vsync.wait();
2358
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002359 glClear(GL_COLOR_BUFFER_BIT);
2360 glColor4f(1-v, 1-v, 1-v, 1);
2361 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2362 hw.flip(screenBounds);
2363 }
2364
Mathias Agopiana6546e52010-10-14 12:33:07 -07002365 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002366 v_stretch vverts(hw_w, hw_h);
2367 glEnable(GL_BLEND);
2368 glBlendFunc(GL_ONE, GL_ONE);
2369 for (int i=nbFrames-1 ; i>=0 ; i--) {
2370 float x, y, w, h;
2371 const float vr = itr(i);
2372 const float vg = itg(i);
2373 const float vb = itb(i);
2374
Mathias Agopianed9807b2012-05-18 14:18:08 -07002375 // wait for vsync
2376 vsync.wait();
2377
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002378 // clear screen
2379 glColorMask(1,1,1,1);
2380 glClear(GL_COLOR_BUFFER_BIT);
2381 glEnable(GL_TEXTURE_2D);
2382
2383 // draw the red plane
2384 vverts(vtx, vr);
2385 glColorMask(1,0,0,1);
2386 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2387
2388 // draw the green plane
2389 vverts(vtx, vg);
2390 glColorMask(0,1,0,1);
2391 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2392
2393 // draw the blue plane
2394 vverts(vtx, vb);
2395 glColorMask(0,0,1,1);
2396 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2397
2398 hw.flip(screenBounds);
2399 }
2400
2401 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002402 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002403 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002404 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002405 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002406
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002407 return NO_ERROR;
2408}
2409
2410// ---------------------------------------------------------------------------
2411
Mathias Agopianabd671a2010-10-14 14:54:06 -07002412status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002413{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002414 ATRACE_CALL();
2415
Mathias Agopian1b031492012-06-20 17:51:20 -07002416 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002417 if (!hw.canDraw()) {
2418 // we're already off
2419 return NO_ERROR;
2420 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002421
2422 // turn off hwc while we're doing the animation
2423 hw.getHwComposer().disable();
2424 // and make sure to turn it back on (if needed) next time we compose
2425 invalidateHwcGeometry();
2426
Mathias Agopianabd671a2010-10-14 14:54:06 -07002427 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2428 electronBeamOffAnimationImplLocked();
2429 }
2430
2431 // always clear the whole screen at the end of the animation
2432 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002433 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002434 hw.flip( Region(hw.bounds()) );
2435
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002436 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002437}
2438
2439status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2440{
Mathias Agopian59119e62010-10-11 12:37:43 -07002441 class MessageTurnElectronBeamOff : public MessageBase {
2442 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002443 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002444 status_t result;
2445 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002446 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2447 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002448 }
2449 status_t getResult() const {
2450 return result;
2451 }
2452 virtual bool handler() {
2453 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002454 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002455 return true;
2456 }
2457 };
2458
Mathias Agopianabd671a2010-10-14 14:54:06 -07002459 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002460 status_t res = postMessageSync(msg);
2461 if (res == NO_ERROR) {
2462 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002463
2464 // work-around: when the power-manager calls us we activate the
2465 // animation. eventually, the "on" animation will be called
2466 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002467 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002468 }
2469 return res;
2470}
2471
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002473
Mathias Agopianabd671a2010-10-14 14:54:06 -07002474status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002475{
Mathias Agopian1b031492012-06-20 17:51:20 -07002476 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002477 if (hw.canDraw()) {
2478 // we're already on
2479 return NO_ERROR;
2480 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002481 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2482 electronBeamOnAnimationImplLocked();
2483 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002484
2485 // make sure to redraw the whole screen when the animation is done
2486 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002487 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002488
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002489 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002490}
2491
2492status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2493{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002494 class MessageTurnElectronBeamOn : public MessageBase {
2495 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002496 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002497 status_t result;
2498 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002499 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2500 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002501 }
2502 status_t getResult() const {
2503 return result;
2504 }
2505 virtual bool handler() {
2506 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002507 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002508 return true;
2509 }
2510 };
2511
Mathias Agopianabd671a2010-10-14 14:54:06 -07002512 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002513 return NO_ERROR;
2514}
2515
2516// ---------------------------------------------------------------------------
2517
Mathias Agopian74c40c02010-09-29 13:02:36 -07002518status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2519 sp<IMemoryHeap>* heap,
2520 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002521 uint32_t sw, uint32_t sh,
2522 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002523{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002524 ATRACE_CALL();
2525
Mathias Agopian74c40c02010-09-29 13:02:36 -07002526 status_t result = PERMISSION_DENIED;
2527
2528 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002529 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002530 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002531 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002532
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002533 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002534 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002535 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002536
2537 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002538 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002539 const uint32_t hw_w = hw.getWidth();
2540 const uint32_t hw_h = hw.getHeight();
2541
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002542 // if we have secure windows on this display, never allow the screen capture
2543 if (hw.getSecureLayerVisible()) {
2544 return PERMISSION_DENIED;
2545 }
2546
2547 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002548 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002549 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002550
2551 sw = (!sw) ? hw_w : sw;
2552 sh = (!sh) ? hw_h : sh;
2553 const size_t size = sw * sh * 4;
2554
Steve Block9d453682011-12-20 16:23:08 +00002555 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002556 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002557
Mathias Agopian74c40c02010-09-29 13:02:36 -07002558 // make sure to clear all GL error flags
2559 while ( glGetError() != GL_NO_ERROR ) ;
2560
2561 // create a FBO
2562 GLuint name, tname;
2563 glGenRenderbuffersOES(1, &tname);
2564 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2565 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002566
Mathias Agopian74c40c02010-09-29 13:02:36 -07002567 glGenFramebuffersOES(1, &name);
2568 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2569 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2570 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2571
2572 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002573
Mathias Agopian74c40c02010-09-29 13:02:36 -07002574 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2575
2576 // invert everything, b/c glReadPixel() below will invert the FB
2577 glViewport(0, 0, sw, sh);
2578 glMatrixMode(GL_PROJECTION);
2579 glPushMatrix();
2580 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002581 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002582 glMatrixMode(GL_MODELVIEW);
2583
2584 // redraw the screen entirely...
2585 glClearColor(0,0,0,1);
2586 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002587
Jamie Gennis9575f602011-10-07 14:51:16 -07002588 const LayerVector& layers(mDrawingState.layersSortedByZ);
2589 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002590 for (size_t i=0 ; i<count ; ++i) {
2591 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002592 const uint32_t flags = layer->drawingState().flags;
2593 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2594 const uint32_t z = layer->drawingState().z;
2595 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002596 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002597 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002598 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002599 }
2600
Mathias Agopian74c40c02010-09-29 13:02:36 -07002601 // check for errors and return screen capture
2602 if (glGetError() != GL_NO_ERROR) {
2603 // error while rendering
2604 result = INVALID_OPERATION;
2605 } else {
2606 // allocate shared memory large enough to hold the
2607 // screen capture
2608 sp<MemoryHeapBase> base(
2609 new MemoryHeapBase(size, 0, "screen-capture") );
2610 void* const ptr = base->getBase();
2611 if (ptr) {
2612 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002613 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002614 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2615 if (glGetError() == GL_NO_ERROR) {
2616 *heap = base;
2617 *w = sw;
2618 *h = sh;
2619 *f = PIXEL_FORMAT_RGBA_8888;
2620 result = NO_ERROR;
2621 }
2622 } else {
2623 result = NO_MEMORY;
2624 }
2625 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002626 glViewport(0, 0, hw_w, hw_h);
2627 glMatrixMode(GL_PROJECTION);
2628 glPopMatrix();
2629 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002630 } else {
2631 result = BAD_VALUE;
2632 }
2633
2634 // release FBO resources
2635 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2636 glDeleteRenderbuffersOES(1, &tname);
2637 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002638
2639 hw.compositionComplete();
2640
Steve Block9d453682011-12-20 16:23:08 +00002641 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002642
Mathias Agopian74c40c02010-09-29 13:02:36 -07002643 return result;
2644}
2645
2646
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002647status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2648 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002649 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002650 uint32_t sw, uint32_t sh,
2651 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002652{
2653 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002654 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002655 return BAD_VALUE;
2656
2657 if (!GLExtensions::getInstance().haveFramebufferObject())
2658 return INVALID_OPERATION;
2659
2660 class MessageCaptureScreen : public MessageBase {
2661 SurfaceFlinger* flinger;
2662 DisplayID dpy;
2663 sp<IMemoryHeap>* heap;
2664 uint32_t* w;
2665 uint32_t* h;
2666 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002667 uint32_t sw;
2668 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002669 uint32_t minLayerZ;
2670 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002671 status_t result;
2672 public:
2673 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002674 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002675 uint32_t sw, uint32_t sh,
2676 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002677 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002678 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2679 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2680 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002681 {
2682 }
2683 status_t getResult() const {
2684 return result;
2685 }
2686 virtual bool handler() {
2687 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002688 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002689 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002690 return true;
2691 }
2692 };
2693
2694 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002695 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002696 status_t res = postMessageSync(msg);
2697 if (res == NO_ERROR) {
2698 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2699 }
2700 return res;
2701}
2702
2703// ---------------------------------------------------------------------------
2704
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002705SurfaceFlinger::LayerVector::LayerVector() {
2706}
2707
2708SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2709 : SortedVector<sp<LayerBase> >(rhs) {
2710}
2711
2712int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2713 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002714{
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002715 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2716 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
2717 // sort layers by Z order
2718 uint32_t lz = l->currentState().z;
2719 uint32_t rz = r->currentState().z;
2720 // then by sequence, so we get a stable ordering
2721 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
2722}
2723
2724// ---------------------------------------------------------------------------
2725
2726SurfaceFlinger::State::State()
2727 : orientation(ISurfaceComposer::eOrientationDefault),
2728 orientationFlags(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002729}
2730
2731// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002732
Jamie Gennis9a78c902011-01-12 18:30:40 -08002733GraphicBufferAlloc::GraphicBufferAlloc() {}
2734
2735GraphicBufferAlloc::~GraphicBufferAlloc() {}
2736
2737sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002738 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002739 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2740 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002741 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002742 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002743 if (err == NO_MEMORY) {
2744 GraphicBuffer::dumpAllocationsToSystemLog();
2745 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002746 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002747 "failed (%s), handle=%p",
2748 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002749 return 0;
2750 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002751 return graphicBuffer;
2752}
2753
Jamie Gennis9a78c902011-01-12 18:30:40 -08002754// ---------------------------------------------------------------------------
2755
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002756}; // namespace android