blob: 840222c04ccb26a101685367ceff95126e9d20e6 [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 <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070053#include "DisplayHardware.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070054#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070056#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070059#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopiana4912602012-07-12 14:25:33 -070062#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070063#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Glenn Kasten1db13d72011-12-19 13:55:34 -080065#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080066#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070067#include <gui/BitTube.h>
Mathias Agopian3094df32012-06-18 18:06:45 -070068#include <gui/SurfaceTextureClient.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070069
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080070#define EGL_VERSION_HW_ANDROID 0x3143
71
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072#define DISPLAY_COUNT 1
73
74namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075// ---------------------------------------------------------------------------
76
Mathias Agopian99b49842011-06-27 16:05:52 -070077const String16 sHardwareTest("android.permission.HARDWARE_TEST");
78const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
79const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
80const String16 sDump("android.permission.DUMP");
81
82// ---------------------------------------------------------------------------
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084SurfaceFlinger::SurfaceFlinger()
85 : BnSurfaceComposer(), Thread(false),
86 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070087 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070091 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070092 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070094 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070095 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070096 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070097 mDebugInSwapBuffers(0),
98 mLastSwapBufferTime(0),
99 mDebugInTransaction(0),
100 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700101 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -0700102 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
Steve Blocka19954a2012-01-04 20:05:49 +0000109 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
Colin Cross3854ed52012-03-23 14:17:18 -0700117#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700123#else
124#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700125#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126
Steve Blocka19954a2012-01-04 20:05:49 +0000127 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000128 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800131void SurfaceFlinger::onFirstRef()
132{
133 mEventQueue.init(this);
134
135 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
136
137 // Wait for the main thread to be done with its initialization
138 mReadyToRunBarrier.wait();
139}
140
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142SurfaceFlinger::~SurfaceFlinger()
143{
144 glDeleteTextures(1, &mWormholeTexName);
Mathias Agopiana4912602012-07-12 14:25:33 -0700145 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
146 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
147 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148}
149
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150void SurfaceFlinger::binderDied(const wp<IBinder>& who)
151{
152 // the window manager died on us. prepare its eulogy.
153
154 // reset screen orientation
155 Vector<ComposerState> state;
156 setTransactionState(state, eOrientationDefault, 0);
157
158 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700159 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800160}
161
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700162sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700164 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165}
166
Mathias Agopian7e27f052010-05-28 14:22:23 -0700167sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168{
Mathias Agopian96f08192010-06-02 23:28:45 -0700169 sp<ISurfaceComposerClient> bclient;
170 sp<Client> client(new Client(this));
171 status_t err = client->initCheck();
172 if (err == NO_ERROR) {
173 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 return bclient;
176}
177
Jamie Gennis9a78c902011-01-12 18:30:40 -0800178sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
179{
180 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
181 return gba;
182}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184void SurfaceFlinger::bootFinished()
185{
186 const nsecs_t now = systemTime();
187 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000188 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700189 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700190
191 // wait patiently for the window manager death
192 const String16 name("window");
193 sp<IBinder> window(defaultServiceManager()->getService(name));
194 if (window != 0) {
195 window->linkToDeath(this);
196 }
197
198 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700199 // formerly we would just kill the process, but we now ask it to exit so it
200 // can choose where to stop the animation.
201 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202}
203
Mathias Agopiana4912602012-07-12 14:25:33 -0700204status_t SurfaceFlinger::selectConfigForPixelFormat(
205 EGLDisplay dpy,
206 EGLint const* attrs,
207 PixelFormat format,
208 EGLConfig* outConfig)
209{
210 EGLConfig config = NULL;
211 EGLint numConfigs = -1, n=0;
212 eglGetConfigs(dpy, NULL, 0, &numConfigs);
213 EGLConfig* const configs = new EGLConfig[numConfigs];
214 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
215 for (int i=0 ; i<n ; i++) {
216 EGLint nativeVisualId = 0;
217 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
218 if (nativeVisualId>0 && format == nativeVisualId) {
219 *outConfig = configs[i];
220 delete [] configs;
221 return NO_ERROR;
222 }
223 }
224 delete [] configs;
225 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
Mathias Agopiana4912602012-07-12 14:25:33 -0700228EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
229 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
230 // it is to be used with WIFI displays
231 EGLConfig config;
232 EGLint dummy;
233 status_t err;
234 EGLint attribs[] = {
235 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
236 EGL_RECORDABLE_ANDROID, EGL_TRUE,
237 EGL_NONE
238 };
239 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
240 if (err) {
241 // maybe we failed because of EGL_RECORDABLE_ANDROID
242 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
243 attribs[2] = EGL_NONE;
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 }
246 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
247 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
248 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
249 }
250 return config;
251}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
Mathias Agopiana4912602012-07-12 14:25:33 -0700253EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
254 // Also create our EGLContext
255 EGLint contextAttributes[] = {
256#ifdef EGL_IMG_context_priority
257#ifdef HAS_CONTEXT_PRIORITY
258#warning "using EGL_IMG_context_priority"
259 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
260#endif
261#endif
262 EGL_NONE, EGL_NONE
263 };
264 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
265 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
266 return ctxt;
267}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268
Mathias Agopiana4912602012-07-12 14:25:33 -0700269void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
270 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
271 if (!result) {
272 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
273 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 }
275
Mathias Agopiana4912602012-07-12 14:25:33 -0700276 GLExtensions& extensions(GLExtensions::getInstance());
277 extensions.initWithGLStrings(
278 glGetString(GL_VENDOR),
279 glGetString(GL_RENDERER),
280 glGetString(GL_VERSION),
281 glGetString(GL_EXTENSIONS),
282 eglQueryString(display, EGL_VENDOR),
283 eglQueryString(display, EGL_VERSION),
284 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700285
Mathias Agopiana4912602012-07-12 14:25:33 -0700286 EGLint w, h;
287 eglQuerySurface(display, surface, EGL_WIDTH, &w);
288 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700289
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
291 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700292
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 glShadeModel(GL_FLAT);
297 glDisable(GL_DITHER);
298 glDisable(GL_CULL_FACE);
299
Mathias Agopiana4912602012-07-12 14:25:33 -0700300 struct pack565 {
301 inline uint16_t operator() (int r, int g, int b) const {
302 return (r<<11)|(g<<5)|b;
303 }
304 } pack565;
305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
307 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700308 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 glGenTextures(1, &mWormholeTexName);
310 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
311 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
312 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
313 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
314 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
315 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700316 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
317
318 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
319 glGenTextures(1, &mProtectedTexName);
320 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
321 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
322 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
323 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
324 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
325 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
326 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327
328 glViewport(0, 0, w, h);
329 glMatrixMode(GL_PROJECTION);
330 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700331 // put the origin in the left-bottom corner
332 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 -0800333
Mathias Agopiana4912602012-07-12 14:25:33 -0700334 // print some debugging info
335 EGLint r,g,b,a;
336 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
337 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
338 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
339 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
340 ALOGI("EGL informations:");
341 ALOGI("vendor : %s", extensions.getEglVendor());
342 ALOGI("version : %s", extensions.getEglVersion());
343 ALOGI("extensions: %s", extensions.getEglExtension());
344 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
345 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
346 ALOGI("OpenGL ES informations:");
347 ALOGI("vendor : %s", extensions.getVendor());
348 ALOGI("renderer : %s", extensions.getRenderer());
349 ALOGI("version : %s", extensions.getVersion());
350 ALOGI("extensions: %s", extensions.getExtension());
351 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
352 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
353}
354
355surface_flinger_cblk_t* SurfaceFlinger::getControlBlock() const {
356 return mServerCblk;
357}
358
359status_t SurfaceFlinger::readyToRun()
360{
361 ALOGI( "SurfaceFlinger's main thread ready to run. "
362 "Initializing graphics H/W...");
363
364 // create the shared control-block
365 mServerHeap = new MemoryHeapBase(4096,
366 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
367 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
368 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
369 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
370 new(mServerCblk) surface_flinger_cblk_t;
371
372
373 // initialize EGL
374 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
375 eglInitialize(display, NULL, NULL);
376
377 // Initialize the main display
378 // create native window to main display
379 sp<FramebufferSurface> anw = FramebufferSurface::create();
380 ANativeWindow* const window = anw.get();
381 if (!window) {
382 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
383 exit(0);
384 }
385
386 // initialize the config and context
387 int format;
388 window->query(window, NATIVE_WINDOW_FORMAT, &format);
389 mEGLConfig = selectEGLConfig(display, format);
390 mEGLContext = createGLContext(display, mEGLConfig);
391
392 // initialize our main display hardware
393 DisplayHardware* const hw = new DisplayHardware(this, 0, anw, mEGLConfig);
394 mDisplayHardwares[0] = hw;
395
396 // initialize OpenGL ES
397 EGLSurface surface = hw->getEGLSurface();
398 initializeGL(display, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800399
400 // start the EventThread
401 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800402 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403
Mathias Agopiana4912602012-07-12 14:25:33 -0700404 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800405 mReadyToRunBarrier.open();
406
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700407 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700408 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 return NO_ERROR;
411}
412
Mathias Agopiana67e4182012-06-19 17:26:12 -0700413void SurfaceFlinger::startBootAnim() {
414 // start boot animation
415 property_set("service.bootanim.exit", "0");
416 property_set("ctl.start", "bootanim");
417}
418
Mathias Agopiana4912602012-07-12 14:25:33 -0700419uint32_t SurfaceFlinger::getMaxTextureSize() const {
420 return mMaxTextureSize;
421}
422
423uint32_t SurfaceFlinger::getMaxViewportDims() const {
424 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
425 mMaxViewportDims[0] : mMaxViewportDims[1];
426}
427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800429
Jamie Gennis582270d2011-08-17 18:19:00 -0700430bool SurfaceFlinger::authenticateSurfaceTexture(
431 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800432 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700433 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800434
435 // Check the visible layer list for the ISurface
436 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
437 size_t count = currentLayers.size();
438 for (size_t i=0 ; i<count ; i++) {
439 const sp<LayerBase>& layer(currentLayers[i]);
440 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700441 if (lbc != NULL) {
442 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
443 if (lbcBinder == surfaceTextureBinder) {
444 return true;
445 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800446 }
447 }
448
449 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700450 // SurfaceTexture gets destroyed before all the clients are done using it,
451 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800452 // will instead fail later on when the client tries to use the surface,
453 // which should be reported as "surface XYZ returned an -ENODEV". The
454 // purgatorized layers are no less authentic than the visible ones, so this
455 // should not cause any harm.
456 size_t purgatorySize = mLayerPurgatory.size();
457 for (size_t i=0 ; i<purgatorySize ; i++) {
458 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
459 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700460 if (lbc != NULL) {
461 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
462 if (lbcBinder == surfaceTextureBinder) {
463 return true;
464 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800465 }
466 }
467
468 return false;
469}
470
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800471// ----------------------------------------------------------------------------
472
473sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800474 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700475}
476
Mathias Agopian3094df32012-06-18 18:06:45 -0700477void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700478 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700479 EGLSurface result = EGL_NO_SURFACE;
480 EGLSurface old_surface = EGL_NO_SURFACE;
481 sp<SurfaceTextureClient> stc;
482
483 if (display != NULL) {
484 stc = new SurfaceTextureClient(display);
485 result = eglCreateWindowSurface(hw.getEGLDisplay(),
Mathias Agopiana4912602012-07-12 14:25:33 -0700486 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700487 ALOGE_IF(result == EGL_NO_SURFACE,
488 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
489 display.get());
490 }
491
492 { // scope for the lock
493 Mutex::Autolock _l(mStateLock);
494 old_surface = mExternalDisplaySurface;
495 mExternalDisplayNativeWindow = stc;
496 mExternalDisplaySurface = result;
497 ALOGD("mExternalDisplaySurface = %p", result);
498 }
499
500 if (old_surface != EGL_NO_SURFACE) {
501 // Note: EGL allows to destroy an object while its current
502 // it will fail to become current next time though.
503 eglDestroySurface(hw.getEGLDisplay(), old_surface);
504 }
505}
506
507EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
508 Mutex::Autolock _l(mStateLock);
509 return mExternalDisplaySurface;
510}
511
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800513
514void SurfaceFlinger::waitForEvent() {
515 mEventQueue.waitMessage();
516}
517
518void SurfaceFlinger::signalTransaction() {
519 mEventQueue.invalidate();
520}
521
522void SurfaceFlinger::signalLayerUpdate() {
523 mEventQueue.invalidate();
524}
525
526void SurfaceFlinger::signalRefresh() {
527 mEventQueue.refresh();
528}
529
530status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
531 nsecs_t reltime, uint32_t flags) {
532 return mEventQueue.postMessage(msg, reltime);
533}
534
535status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
536 nsecs_t reltime, uint32_t flags) {
537 status_t res = mEventQueue.postMessage(msg, reltime);
538 if (res == NO_ERROR) {
539 msg->wait();
540 }
541 return res;
542}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543
Mathias Agopian4fec8732012-06-29 14:12:52 -0700544bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 return true;
547}
548
Mathias Agopian4fec8732012-06-29 14:12:52 -0700549void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800550 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800551 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700552 case MessageQueue::INVALIDATE:
553 handleMessageTransaction();
554 handleMessageInvalidate();
555 signalRefresh();
556 break;
557 case MessageQueue::REFRESH:
558 handleMessageRefresh();
559 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800560 }
561}
562
Mathias Agopian4fec8732012-06-29 14:12:52 -0700563void SurfaceFlinger::handleMessageTransaction() {
564 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
565 uint32_t transactionFlags = peekTransactionFlags(mask);
566 if (transactionFlags) {
567 Region dirtyRegion;
568 dirtyRegion = handleTransaction(transactionFlags);
569 // XXX: dirtyRegion should be per screen
570 mDirtyRegion |= dirtyRegion;
571 }
572}
573
574void SurfaceFlinger::handleMessageInvalidate() {
575 Region dirtyRegion;
576 dirtyRegion = handlePageFlip();
577 // XXX: dirtyRegion should be per screen
578 mDirtyRegion |= dirtyRegion;
579}
580
581void SurfaceFlinger::handleMessageRefresh() {
582 handleRefresh();
583
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700584 if (mVisibleRegionsDirty) {
585 Region opaqueRegion;
586 Region dirtyRegion;
587 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
588 computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
589 mDirtyRegion.orSelf(dirtyRegion);
590
591 /*
592 * rebuild the visible layer list per screen
593 */
594
595 // TODO: iterate through all displays
596 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
597
598 Vector< sp<LayerBase> > layersSortedByZ;
599 const size_t count = currentLayers.size();
600 for (size_t i=0 ; i<count ; i++) {
601 if (!currentLayers[i]->visibleRegion.isEmpty()) {
602 // TODO: also check that this layer is associated to this display
603 layersSortedByZ.add(currentLayers[i]);
604 }
605 }
606 hw.setVisibleLayersSortedByZ(layersSortedByZ);
607
608
609 // FIXME: mWormholeRegion needs to be calculated per screen
610 //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
611 mWormholeRegion = Region(hw.getBounds()).subtract(
612 hw.getTransform().transform(opaqueRegion) );
613 mVisibleRegionsDirty = false;
614 invalidateHwcGeometry();
615 }
616
617
Mathias Agopian4fec8732012-06-29 14:12:52 -0700618 // XXX: dirtyRegion should be per screen, we should check all of them
619 if (mDirtyRegion.isEmpty()) {
620 return;
621 }
622
623 // TODO: iterate through all displays
624 const DisplayHardware& hw(getDisplayHardware(0));
625
626 // XXX: dirtyRegion should be per screen
627 // transform the dirty region into this screen's coordinate space
628 const Transform& planeTransform(hw.getTransform());
629 mDirtyRegion = planeTransform.transform(mDirtyRegion);
630 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
631 mDirtyRegion.andSelf(hw.bounds());
632
633
634 if (CC_UNLIKELY(mHwWorkListDirty)) {
635 // build the h/w work list
636 handleWorkList(hw);
637 }
638
639 if (CC_LIKELY(hw.canDraw())) {
640 // repaint the framebuffer (if needed)
641 handleRepaint(hw);
642 // inform the h/w that we're done compositing
643 hw.compositionComplete();
644 postFramebuffer();
645 } else {
646 // pretend we did the post
647 hw.compositionComplete();
648 }
649
650 // render to the external display if we have one
651 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
652 if (externalDisplaySurface != EGL_NO_SURFACE) {
653 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
654 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
655 externalDisplaySurface, externalDisplaySurface,
656 eglGetCurrentContext());
657
658 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
659
660 if (success) {
661 // redraw the screen entirely...
662 glDisable(GL_TEXTURE_EXTERNAL_OES);
663 glDisable(GL_TEXTURE_2D);
664 glClearColor(0,0,0,1);
665 glClear(GL_COLOR_BUFFER_BIT);
666 glMatrixMode(GL_MODELVIEW);
667 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700668
669 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700670 const size_t count = layers.size();
671 for (size_t i=0 ; i<count ; ++i) {
672 const sp<LayerBase>& layer(layers[i]);
673 layer->drawForSreenShot(hw);
674 }
675
676 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
677 ALOGE_IF(!success, "external display eglSwapBuffers failed");
678
679 hw.compositionComplete();
680 }
681
682 success = eglMakeCurrent(eglGetCurrentDisplay(),
683 cur, cur, eglGetCurrentContext());
684
685 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
686 }
687
688}
689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690void SurfaceFlinger::postFramebuffer()
691{
Mathias Agopian841cde52012-03-01 15:44:37 -0800692 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800693 // mSwapRegion can be empty here is some cases, for instance if a hidden
694 // or fully transparent window is updating.
695 // in that case, we need to flip anyways to not risk a deadlock with
696 // h/w composer.
697
Mathias Agopian1b031492012-06-20 17:51:20 -0700698 const DisplayHardware& hw(getDefaultDisplayHardware());
Jesse Hallc5c5a142012-07-02 16:49:28 -0700699 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700700 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
701 size_t numLayers = layers.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700702 const nsecs_t now = systemTime();
703 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700704
705 if (hwc.initCheck() == NO_ERROR) {
706 HWComposer::LayerListIterator cur = hwc.begin();
707 const HWComposer::LayerListIterator end = hwc.end();
708 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
709 if (cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700710 layers[i]->setAcquireFence(*cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700711 } else {
712 cur->setAcquireFenceFd(-1);
713 }
714 }
715 }
716
Mathias Agopiana44b0412011-10-16 18:46:35 -0700717 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800718
Jesse Hallef194142012-06-14 14:45:17 -0700719 if (hwc.initCheck() == NO_ERROR) {
720 HWComposer::LayerListIterator cur = hwc.begin();
721 const HWComposer::LayerListIterator end = hwc.end();
722 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700723 layers[i]->onLayerDisplayed(&*cur);
Jesse Hallef194142012-06-14 14:45:17 -0700724 }
725 } else {
726 for (size_t i = 0; i < numLayers; i++) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700727 layers[i]->onLayerDisplayed(NULL);
Jesse Hallef194142012-06-14 14:45:17 -0700728 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800729 }
730
Mathias Agopiana44b0412011-10-16 18:46:35 -0700731 mLastSwapBufferTime = systemTime() - now;
732 mDebugInSwapBuffers = 0;
733 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734}
735
Mathias Agopian4fec8732012-06-29 14:12:52 -0700736Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737{
Mathias Agopian841cde52012-03-01 15:44:37 -0800738 ATRACE_CALL();
739
Mathias Agopian4fec8732012-06-29 14:12:52 -0700740 Region dirtyRegion;
741
Mathias Agopianca4d3602011-05-19 15:38:14 -0700742 Mutex::Autolock _l(mStateLock);
743 const nsecs_t now = systemTime();
744 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745
Mathias Agopianca4d3602011-05-19 15:38:14 -0700746 // Here we're guaranteed that some transaction flags are set
747 // so we can call handleTransactionLocked() unconditionally.
748 // We call getTransactionFlags(), which will also clear the flags,
749 // with mStateLock held to guarantee that mCurrentState won't change
750 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700751
Mathias Agopianca4d3602011-05-19 15:38:14 -0700752 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
753 transactionFlags = getTransactionFlags(mask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700754 dirtyRegion = handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700755
Mathias Agopianca4d3602011-05-19 15:38:14 -0700756 mLastTransactionTime = systemTime() - now;
757 mDebugInTransaction = 0;
758 invalidateHwcGeometry();
759 // here the transaction has been committed
Mathias Agopian4fec8732012-06-29 14:12:52 -0700760
761 return dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700762}
763
Mathias Agopian4fec8732012-06-29 14:12:52 -0700764Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700765{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700766 Region dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700767 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768 const size_t count = currentLayers.size();
769
770 /*
771 * Traversal of the children
772 * (perform the transaction for each of them if needed)
773 */
774
775 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
776 if (layersNeedTransaction) {
777 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700778 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
780 if (!trFlags) continue;
781
782 const uint32_t flags = layer->doTransaction(0);
783 if (flags & Layer::eVisibleRegion)
784 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 }
786 }
787
788 /*
789 * Perform our own transaction if needed
790 */
791
792 if (transactionFlags & eTransactionNeeded) {
793 if (mCurrentState.orientation != mDrawingState.orientation) {
794 // the orientation has changed, recompute all visible regions
795 // and invalidate everything.
796
Mathias Agopian1b031492012-06-20 17:51:20 -0700797 const int dpy = 0; // TODO: should be a parameter
798 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 const int orientation = mCurrentState.orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700800 hw.setOrientation(orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801
802 // update the shared control block
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
804 dcblk->orientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700805 dcblk->w = hw.getUserWidth();
806 dcblk->h = hw.getUserHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807
Mathias Agopian1b031492012-06-20 17:51:20 -0700808 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 mVisibleRegionsDirty = true;
810 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 }
812
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700813 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
814 // layers have been added
815 mVisibleRegionsDirty = true;
816 }
817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 // some layers might have been removed, so
819 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700820 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700821 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700823 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700824 const size_t count = previousLayers.size();
825 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700826 const sp<LayerBase>& layer(previousLayers[i]);
827 if (currentLayers.indexOf( layer ) < 0) {
828 // this layer is not visible anymore
Mathias Agopian4fec8732012-06-29 14:12:52 -0700829 // TODO: we could traverse the tree from front to back and compute the actual visible region
830 // TODO: we could cache the transformed region
831 Layer::State front(layer->drawingState());
832 Region visibleReg = front.transform.transform(
833 Region(Rect(front.active.w, front.active.h)));
834 dirtyRegion.orSelf(visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700835 }
836 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 }
839
840 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700841 return dirtyRegion;
842}
843
844void SurfaceFlinger::commitTransaction()
845{
846 if (!mLayersPendingRemoval.isEmpty()) {
847 // Notify removed layers now that they can't be drawn from
848 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
849 mLayersPendingRemoval[i]->onRemoved();
850 }
851 mLayersPendingRemoval.clear();
852 }
853
854 mDrawingState = mCurrentState;
855 mTransationPending = false;
856 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857}
858
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800860 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861{
Mathias Agopian841cde52012-03-01 15:44:37 -0800862 ATRACE_CALL();
863
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 Region aboveOpaqueLayers;
865 Region aboveCoveredLayers;
866 Region dirty;
867
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700868 dirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869
870 size_t i = currentLayers.size();
871 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700872 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873
874 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700875 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876
Mathias Agopianab028732010-03-16 16:41:46 -0700877 /*
878 * opaqueRegion: area of a surface that is fully opaque.
879 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700881
882 /*
883 * visibleRegion: area of a surface that is visible on screen
884 * and not fully transparent. This is essentially the layer's
885 * footprint minus the opaque regions above it.
886 * Areas covered by a translucent surface are considered visible.
887 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700889
890 /*
891 * coveredRegion: area of a surface that is covered by all
892 * visible regions above it (which includes the translucent areas).
893 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700895
896
897 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800898 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700899 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700900 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700902 if (!visibleRegion.isEmpty()) {
903 // Remove the transparent area from the visible region
904 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700905 Region transparentRegionScreen;
906 const Transform tr(s.transform);
907 if (tr.transformed()) {
908 if (tr.preserveRects()) {
909 // transform the transparent region
910 transparentRegionScreen = tr.transform(s.transparentRegion);
911 } else {
912 // transformation too complex, can't do the
913 // transparent region optimization.
914 transparentRegionScreen.clear();
915 }
916 } else {
917 transparentRegionScreen = s.transparentRegion;
918 }
919 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700920 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921
Mathias Agopianab028732010-03-16 16:41:46 -0700922 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700923 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700924 if (s.alpha==255 && !translucent &&
925 ((layerOrientation & Transform::ROT_INVALID) == false)) {
926 // the opaque region is the layer's footprint
927 opaqueRegion = visibleRegion;
928 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 }
930 }
931
Mathias Agopianab028732010-03-16 16:41:46 -0700932 // Clip the covered region to the visible region
933 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
934
935 // Update aboveCoveredLayers for next (lower) layer
936 aboveCoveredLayers.orSelf(visibleRegion);
937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 // subtract the opaque region covered by the layers above us
939 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940
941 // compute this layer's dirty region
942 if (layer->contentDirty) {
943 // we need to invalidate the whole region
944 dirty = visibleRegion;
945 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700946 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 layer->contentDirty = false;
948 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700949 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700950 * the exposed region consists of two components:
951 * 1) what's VISIBLE now and was COVERED before
952 * 2) what's EXPOSED now less what was EXPOSED before
953 *
954 * note that (1) is conservative, we start with the whole
955 * visible region but only keep what used to be covered by
956 * something -- which mean it may have been exposed.
957 *
958 * (2) handles areas that were not covered by anything but got
959 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700960 */
Mathias Agopianab028732010-03-16 16:41:46 -0700961 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700962 const Region oldVisibleRegion = layer->visibleRegion;
963 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700964 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
965 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 }
967 dirty.subtractSelf(aboveOpaqueLayers);
968
969 // accumulate to the screen dirty region
970 dirtyRegion.orSelf(dirty);
971
Mathias Agopianab028732010-03-16 16:41:46 -0700972 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700974
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 // Store the visible region is screen space
976 layer->setVisibleRegion(visibleRegion);
977 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 }
979
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 opaqueRegion = aboveOpaqueLayers;
981}
982
Mathias Agopian4fec8732012-06-29 14:12:52 -0700983Region SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800985 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700986 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800988 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800989
Mathias Agopian4fec8732012-06-29 14:12:52 -0700990 bool visibleRegions = false;
991 const size_t count = currentLayers.size();
992 sp<LayerBase> const* layers = currentLayers.array();
993 for (size_t i=0 ; i<count ; i++) {
994 const sp<LayerBase>& layer(layers[i]);
995 dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
996 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700997
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700998 mVisibleRegionsDirty |= visibleRegions;
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700999
Mathias Agopian4fec8732012-06-29 14:12:52 -07001000 return dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001}
1002
Mathias Agopianad456f92011-01-13 17:53:01 -08001003void SurfaceFlinger::invalidateHwcGeometry()
1004{
1005 mHwWorkListDirty = true;
1006}
1007
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001008void SurfaceFlinger::handleRefresh()
1009{
1010 bool needInvalidate = false;
1011 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1012 const size_t count = currentLayers.size();
1013 for (size_t i=0 ; i<count ; i++) {
1014 const sp<LayerBase>& layer(currentLayers[i]);
1015 if (layer->onPreComposition()) {
1016 needInvalidate = true;
1017 }
1018 }
1019 if (needInvalidate) {
1020 signalLayerUpdate();
1021 }
1022}
1023
1024
Mathias Agopian1b031492012-06-20 17:51:20 -07001025void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -07001026{
1027 mHwWorkListDirty = false;
Mathias Agopian1b031492012-06-20 17:51:20 -07001028 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001029 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001030 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001031 const size_t count = currentLayers.size();
1032 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001033
1034 HWComposer::LayerListIterator cur = hwc.begin();
1035 const HWComposer::LayerListIterator end = hwc.end();
1036 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001037 currentLayers[i]->setGeometry(hw, *cur);
Mathias Agopian53331da2011-08-22 21:44:41 -07001038 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001039 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001040 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001041 }
1042 }
1043}
Mathias Agopianb8a55602009-06-26 19:06:36 -07001044
Mathias Agopian1b031492012-06-20 17:51:20 -07001045void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046{
Mathias Agopian841cde52012-03-01 15:44:37 -08001047 ATRACE_CALL();
1048
Mathias Agopianb8a55602009-06-26 19:06:36 -07001049 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -07001050 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051
Glenn Kasten99ed2242011-12-15 09:51:17 -08001052 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -07001053 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 }
1055
Mathias Agopianb8a55602009-06-26 19:06:36 -07001056 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -07001057 glMatrixMode(GL_MODELVIEW);
1058 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059
1060 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001061 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001062 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1063 // takes a rectangle, we must make sure to update that whole
1064 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001065 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001066 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -07001067 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001068 // We need to redraw the rectangle that will be updated
1069 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001070 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001071 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -07001072 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001073 } else {
1074 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -07001076 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 }
1078 }
1079
Mathias Agopian1b031492012-06-20 17:51:20 -07001080 setupHardwareComposer(hw);
1081 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001083 // update the swap region and clear the dirty region
1084 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 mDirtyRegion.clear();
1086}
1087
Mathias Agopian1b031492012-06-20 17:51:20 -07001088void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089{
Mathias Agopiana350ff92010-08-10 17:14:02 -07001090 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001091 HWComposer::LayerListIterator cur = hwc.begin();
1092 const HWComposer::LayerListIterator end = hwc.end();
1093 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001094 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -07001095 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001096
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001097 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001098 size_t count = layers.size();
1099
Steve Blocke6f43dd2012-01-06 19:20:56 +00001100 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -07001101 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
1102 hwc.getNumLayers(), count);
1103
1104 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -07001105 if (hwc.initCheck() == NO_ERROR) {
1106 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
1107 }
Mathias Agopian45721772010-08-12 15:03:26 -07001108
1109 /*
1110 * update the per-frame h/w composer data for each layer
1111 * and build the transparent region of the FB
1112 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001113 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001114 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001115 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -07001116 }
1117 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001118 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -07001119}
Mathias Agopian45721772010-08-12 15:03:26 -07001120
Mathias Agopian1b031492012-06-20 17:51:20 -07001121void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001122{
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001123 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001124 HWComposer::LayerListIterator cur = hwc.begin();
1125 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001126
1127 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001128 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001129 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -07001130
Mathias Agopianb9494d52012-04-18 02:28:45 -07001131 if (hwc.getLayerCount(HWC_OVERLAY)) {
1132 // when using overlays, we assume a fully transparent framebuffer
1133 // NOTE: we could reduce how much we need to clear, for instance
1134 // remove where there are opaque FB layers. however, on some
1135 // GPUs doing a "clean slate" glClear might be more efficient.
1136 // We'll revisit later if needed.
1137 glClearColor(0, 0, 0, 0);
1138 glClear(GL_COLOR_BUFFER_BIT);
1139 } else {
1140 // screen is already cleared here
1141 if (!mWormholeRegion.isEmpty()) {
1142 // can happen with SurfaceView
1143 drawWormhole();
1144 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001145 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001146
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001147 /*
1148 * and then, render the layers targeted at the framebuffer
1149 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001150
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001151 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001152 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001153 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001154 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001155 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001156 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001157 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001158 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001159 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001160 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001161 // never clear the very first layer since we're
1162 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001163 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001164 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001165 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001166 continue;
1167 }
1168 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001169 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001170 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001171 if (cur != end) {
1172 ++cur;
1173 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001174 }
1175 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176}
1177
Mathias Agopian1b031492012-06-20 17:51:20 -07001178void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179{
Mathias Agopian0a917752010-06-14 21:20:00 -07001180 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001181 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001182 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001183 return;
1184 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001185
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001186 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001187 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1188 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001189 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001190 }
1191
Mathias Agopianc492e672011-10-18 14:49:27 -07001192 glDisable(GL_TEXTURE_EXTERNAL_OES);
1193 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195
Mathias Agopian0926f502009-05-04 14:17:04 -07001196 static int toggle = 0;
1197 toggle = 1 - toggle;
1198 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001199 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001200 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001201 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001202 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203
Mathias Agopian20f68782009-05-11 00:03:41 -07001204 Region::const_iterator it = mDirtyRegion.begin();
1205 Region::const_iterator const end = mDirtyRegion.end();
1206 while (it != end) {
1207 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001209 { r.left, height - r.top },
1210 { r.left, height - r.bottom },
1211 { r.right, height - r.bottom },
1212 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 };
1214 glVertexPointer(2, GL_FLOAT, 0, vertices);
1215 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1216 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001217
Mathias Agopian0656a682011-09-20 17:22:44 -07001218 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219
1220 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001221 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222}
1223
1224void SurfaceFlinger::drawWormhole() const
1225{
1226 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1227 if (region.isEmpty())
1228 return;
1229
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001230 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001231 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001232 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001233 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001234
1235 GLfloat vertices[4][2];
1236 glVertexPointer(2, GL_FLOAT, 0, vertices);
1237 Region::const_iterator it = region.begin();
1238 Region::const_iterator const end = region.end();
1239 while (it != end) {
1240 const Rect& r = *it++;
1241 vertices[0][0] = r.left;
1242 vertices[0][1] = r.top;
1243 vertices[1][0] = r.right;
1244 vertices[1][1] = r.top;
1245 vertices[2][0] = r.right;
1246 vertices[2][1] = r.bottom;
1247 vertices[3][0] = r.left;
1248 vertices[3][1] = r.bottom;
1249 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1250 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251}
1252
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001253status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254{
1255 Mutex::Autolock _l(mStateLock);
1256 addLayer_l(layer);
1257 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1258 return NO_ERROR;
1259}
1260
Mathias Agopian96f08192010-06-02 23:28:45 -07001261status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1262{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001263 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001264 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1265}
1266
1267ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1268 const sp<LayerBaseClient>& lbc)
1269{
Mathias Agopian96f08192010-06-02 23:28:45 -07001270 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001271 size_t name = client->attachLayer(lbc);
1272
1273 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001274
1275 // add this layer to the current state list
1276 addLayer_l(lbc);
1277
Mathias Agopian4f113742011-05-03 16:21:41 -07001278 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001279}
1280
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001281status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282{
1283 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001284 status_t err = purgatorizeLayer_l(layer);
1285 if (err == NO_ERROR)
1286 setTransactionFlags(eTransactionNeeded);
1287 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288}
1289
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001290status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001292 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1293 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001294 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1297 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001298 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 return NO_ERROR;
1300 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001301 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302}
1303
Mathias Agopian9a112062009-04-17 19:36:26 -07001304status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1305{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001306 // First add the layer to the purgatory list, which makes sure it won't
1307 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001308 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001309 if (err >= 0) {
1310 mLayerPurgatory.add(layerBase);
1311 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001312
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001313 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001314
Mathias Agopian3d579642009-06-04 18:46:21 -07001315 // it's possible that we don't find a layer, because it might
1316 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001317 // from the user because there is a race between Client::destroySurface(),
1318 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001319 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1320}
1321
Mathias Agopian96f08192010-06-02 23:28:45 -07001322status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323{
Mathias Agopian96f08192010-06-02 23:28:45 -07001324 layer->forceVisibilityTransaction();
1325 setTransactionFlags(eTraversalNeeded);
1326 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327}
1328
Mathias Agopiandea20b12011-05-03 17:04:02 -07001329uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1330{
1331 return android_atomic_release_load(&mTransactionFlags);
1332}
1333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1335{
1336 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1337}
1338
Mathias Agopianbb641242010-05-18 17:06:55 -07001339uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340{
1341 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1342 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001343 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 }
1345 return old;
1346}
1347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001349void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001350 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001351 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001352
Jamie Gennis28378392011-10-12 17:39:00 -07001353 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001354 if (mCurrentState.orientation != orientation) {
1355 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1356 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001357 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001358 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001359 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001360 orientation);
1361 }
1362 }
1363
Mathias Agopian698c0872011-06-28 19:09:31 -07001364 const size_t count = state.size();
1365 for (size_t i=0 ; i<count ; i++) {
1366 const ComposerState& s(state[i]);
1367 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001368 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001369 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001370
Mathias Agopian386aa982011-11-07 21:58:03 -08001371 if (transactionFlags) {
1372 // this triggers the transaction
1373 setTransactionFlags(transactionFlags);
1374
1375 // if this is a synchronous transaction, wait for it to take effect
1376 // before returning.
1377 if (flags & eSynchronous) {
1378 mTransationPending = true;
1379 }
1380 while (mTransationPending) {
1381 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1382 if (CC_UNLIKELY(err != NO_ERROR)) {
1383 // just in case something goes wrong in SF, return to the
1384 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001385 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001386 mTransationPending = false;
1387 break;
1388 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001389 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 }
1391}
1392
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001393sp<ISurface> SurfaceFlinger::createSurface(
1394 ISurfaceComposerClient::surface_data_t* params,
1395 const String8& name,
1396 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1398 uint32_t flags)
1399{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001400 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001401 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001402
1403 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001404 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001405 int(w), int(h));
1406 return surfaceHandle;
1407 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001408
Mathias Agopianff615cc2012-02-24 14:58:36 -08001409 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001410 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 switch (flags & eFXSurfaceMask) {
1412 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001413 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1414 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 break;
1416 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001417 // for now we treat Blur as Dim, until we can implement it
1418 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001420 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001422 case eFXSurfaceScreenshot:
1423 layer = createScreenshotSurface(client, d, w, h, flags);
1424 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 }
1426
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001427 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001428 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001429 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001430 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001431
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001433 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001434 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001435 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001436 if (normalLayer != 0) {
1437 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001438 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001439 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001440 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001441
Mathias Agopian96f08192010-06-02 23:28:45 -07001442 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444
1445 return surfaceHandle;
1446}
1447
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001448sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001449 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001450 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001451 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452{
1453 // initialize the surfaces
1454 switch (format) { // TODO: take h/w into account
1455 case PIXEL_FORMAT_TRANSPARENT:
1456 case PIXEL_FORMAT_TRANSLUCENT:
1457 format = PIXEL_FORMAT_RGBA_8888;
1458 break;
1459 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001460#ifdef NO_RGBX_8888
1461 format = PIXEL_FORMAT_RGB_565;
1462#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001463 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001464#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 break;
1466 }
1467
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001468#ifdef NO_RGBX_8888
1469 if (format == PIXEL_FORMAT_RGBX_8888)
1470 format = PIXEL_FORMAT_RGBA_8888;
1471#endif
1472
Mathias Agopian96f08192010-06-02 23:28:45 -07001473 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001474 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001475 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001476 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001477 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 }
1479 return layer;
1480}
1481
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001482sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001483 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001484 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485{
Mathias Agopian96f08192010-06-02 23:28:45 -07001486 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001487 return layer;
1488}
1489
1490sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1491 const sp<Client>& client, DisplayID display,
1492 uint32_t w, uint32_t h, uint32_t flags)
1493{
1494 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 return layer;
1496}
1497
Mathias Agopian96f08192010-06-02 23:28:45 -07001498status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499{
Mathias Agopian9a112062009-04-17 19:36:26 -07001500 /*
1501 * called by the window manager, when a surface should be marked for
1502 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001503 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001504 * The surface is removed from the current and drawing lists, but placed
1505 * in the purgatory queue, so it's not destroyed right-away (we need
1506 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001507 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001508
Mathias Agopian48d819a2009-09-10 19:41:18 -07001509 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001510 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001511 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001512
Mathias Agopian48d819a2009-09-10 19:41:18 -07001513 if (layer != 0) {
1514 err = purgatorizeLayer_l(layer);
1515 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001516 setTransactionFlags(eTransactionNeeded);
1517 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001518 }
1519 return err;
1520}
1521
Mathias Agopianca4d3602011-05-19 15:38:14 -07001522status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001523{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001524 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001525 status_t err = NO_ERROR;
1526 sp<LayerBaseClient> l(layer.promote());
1527 if (l != NULL) {
1528 Mutex::Autolock _l(mStateLock);
1529 err = removeLayer_l(l);
1530 if (err == NAME_NOT_FOUND) {
1531 // The surface wasn't in the current list, which means it was
1532 // removed already, which means it is in the purgatory,
1533 // and need to be removed from there.
1534 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001535 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001536 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001537 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001538 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001539 "error removing layer=%p (%s)", l.get(), strerror(-err));
1540 }
1541 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542}
1543
Mathias Agopian698c0872011-06-28 19:09:31 -07001544uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001545 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001546 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001549 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1550 if (layer != 0) {
1551 const uint32_t what = s.what;
1552 if (what & ePositionChanged) {
1553 if (layer->setPosition(s.x, s.y))
1554 flags |= eTraversalNeeded;
1555 }
1556 if (what & eLayerChanged) {
1557 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1558 if (layer->setLayer(s.z)) {
1559 mCurrentState.layersSortedByZ.removeAt(idx);
1560 mCurrentState.layersSortedByZ.add(layer);
1561 // we need traversal (state changed)
1562 // AND transaction (list changed)
1563 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 }
1565 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001566 if (what & eSizeChanged) {
1567 if (layer->setSize(s.w, s.h)) {
1568 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001569 }
1570 }
1571 if (what & eAlphaChanged) {
1572 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1573 flags |= eTraversalNeeded;
1574 }
1575 if (what & eMatrixChanged) {
1576 if (layer->setMatrix(s.matrix))
1577 flags |= eTraversalNeeded;
1578 }
1579 if (what & eTransparentRegionChanged) {
1580 if (layer->setTransparentRegionHint(s.transparentRegion))
1581 flags |= eTraversalNeeded;
1582 }
1583 if (what & eVisibilityChanged) {
1584 if (layer->setFlags(s.flags, s.mask))
1585 flags |= eTraversalNeeded;
1586 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001587 if (what & eCropChanged) {
1588 if (layer->setCrop(s.crop))
1589 flags |= eTraversalNeeded;
1590 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001592 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593}
1594
Mathias Agopianb60314a2012-04-10 22:09:54 -07001595// ---------------------------------------------------------------------------
1596
1597void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001598 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001599 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001600 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001601 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001602 // this is a temporary work-around, eventually this should be called
1603 // by the power-manager
1604 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001605 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001606 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607}
1608
Mathias Agopianb60314a2012-04-10 22:09:54 -07001609void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001610 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001611 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001612 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001613 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001614 hw.releaseScreen();
1615 // from this point on, SF will stop drawing
1616 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}
1618
Colin Cross8e533062012-06-07 13:17:52 -07001619void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001620 class MessageScreenAcquired : public MessageBase {
1621 SurfaceFlinger* flinger;
1622 public:
1623 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1624 virtual bool handler() {
1625 flinger->onScreenAcquired();
1626 return true;
1627 }
1628 };
1629 sp<MessageBase> msg = new MessageScreenAcquired(this);
1630 postMessageSync(msg);
1631}
1632
Colin Cross8e533062012-06-07 13:17:52 -07001633void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001634 class MessageScreenReleased : public MessageBase {
1635 SurfaceFlinger* flinger;
1636 public:
1637 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1638 virtual bool handler() {
1639 flinger->onScreenReleased();
1640 return true;
1641 }
1642 };
1643 sp<MessageBase> msg = new MessageScreenReleased(this);
1644 postMessageSync(msg);
1645}
1646
1647// ---------------------------------------------------------------------------
1648
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1650{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001651 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 char buffer[SIZE];
1653 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001654
1655 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 snprintf(buffer, SIZE, "Permission Denial: "
1657 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1658 IPCThreadState::self()->getCallingPid(),
1659 IPCThreadState::self()->getCallingUid());
1660 result.append(buffer);
1661 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001662 // Try to get the main lock, but don't insist if we can't
1663 // (this would indicate SF is stuck, but we want to be able to
1664 // print something in dumpsys).
1665 int retry = 3;
1666 while (mStateLock.tryLock()<0 && --retry>=0) {
1667 usleep(1000000);
1668 }
1669 const bool locked(retry >= 0);
1670 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001671 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001672 "SurfaceFlinger appears to be unresponsive, "
1673 "dumping anyways (no locks held)\n");
1674 result.append(buffer);
1675 }
1676
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001677 bool dumpAll = true;
1678 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001679 size_t numArgs = args.size();
1680 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001681 if ((index < numArgs) &&
1682 (args[index] == String16("--list"))) {
1683 index++;
1684 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001685 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001686 }
1687
1688 if ((index < numArgs) &&
1689 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001690 index++;
1691 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001692 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001693 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001694
1695 if ((index < numArgs) &&
1696 (args[index] == String16("--latency-clear"))) {
1697 index++;
1698 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001699 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001700 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001702
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001703 if (dumpAll) {
1704 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001705 }
1706
Mathias Agopian9795c422009-08-26 16:36:26 -07001707 if (locked) {
1708 mStateLock.unlock();
1709 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 }
1711 write(fd, result.string(), result.size());
1712 return NO_ERROR;
1713}
1714
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001715void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1716 String8& result, char* buffer, size_t SIZE) const
1717{
1718 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1719 const size_t count = currentLayers.size();
1720 for (size_t i=0 ; i<count ; i++) {
1721 const sp<LayerBase>& layer(currentLayers[i]);
1722 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1723 result.append(buffer);
1724 }
1725}
1726
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001727void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1728 String8& result, char* buffer, size_t SIZE) const
1729{
1730 String8 name;
1731 if (index < args.size()) {
1732 name = String8(args[index]);
1733 index++;
1734 }
1735
1736 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1737 const size_t count = currentLayers.size();
1738 for (size_t i=0 ; i<count ; i++) {
1739 const sp<LayerBase>& layer(currentLayers[i]);
1740 if (name.isEmpty()) {
1741 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1742 result.append(buffer);
1743 }
1744 if (name.isEmpty() || (name == layer->getName())) {
1745 layer->dumpStats(result, buffer, SIZE);
1746 }
1747 }
1748}
1749
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001750void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1751 String8& result, char* buffer, size_t SIZE) const
1752{
1753 String8 name;
1754 if (index < args.size()) {
1755 name = String8(args[index]);
1756 index++;
1757 }
1758
1759 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1760 const size_t count = currentLayers.size();
1761 for (size_t i=0 ; i<count ; i++) {
1762 const sp<LayerBase>& layer(currentLayers[i]);
1763 if (name.isEmpty() || (name == layer->getName())) {
1764 layer->clearStats();
1765 }
1766 }
1767}
1768
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001769void SurfaceFlinger::dumpAllLocked(
1770 String8& result, char* buffer, size_t SIZE) const
1771{
1772 // figure out if we're stuck somewhere
1773 const nsecs_t now = systemTime();
1774 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1775 const nsecs_t inTransaction(mDebugInTransaction);
1776 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1777 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1778
1779 /*
1780 * Dump the visible layer list
1781 */
1782 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1783 const size_t count = currentLayers.size();
1784 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1785 result.append(buffer);
1786 for (size_t i=0 ; i<count ; i++) {
1787 const sp<LayerBase>& layer(currentLayers[i]);
1788 layer->dump(result, buffer, SIZE);
1789 }
1790
1791 /*
1792 * Dump the layers in the purgatory
1793 */
1794
1795 const size_t purgatorySize = mLayerPurgatory.size();
1796 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1797 result.append(buffer);
1798 for (size_t i=0 ; i<purgatorySize ; i++) {
1799 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1800 layer->shortDump(result, buffer, SIZE);
1801 }
1802
1803 /*
1804 * Dump SurfaceFlinger global state
1805 */
1806
1807 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1808 result.append(buffer);
1809
Mathias Agopian1b031492012-06-20 17:51:20 -07001810 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001811 const GLExtensions& extensions(GLExtensions::getInstance());
1812 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1813 extensions.getVendor(),
1814 extensions.getRenderer(),
1815 extensions.getVersion());
1816 result.append(buffer);
1817
1818 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001819 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001820 EGL_VERSION_HW_ANDROID));
1821 result.append(buffer);
1822
1823 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1824 result.append(buffer);
1825
1826 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001827 snprintf(buffer, SIZE,
1828 " orientation=%d, canDraw=%d\n",
1829 mCurrentState.orientation, hw.canDraw());
1830 result.append(buffer);
1831 snprintf(buffer, SIZE,
1832 " last eglSwapBuffers() time: %f us\n"
1833 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001834 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001835 " refresh-rate : %f fps\n"
1836 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001837 " y-dpi : %f\n"
1838 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001839 mLastSwapBufferTime/1000.0,
1840 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001841 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001842 hw.getRefreshRate(),
1843 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001844 hw.getDpiY(),
1845 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001846 result.append(buffer);
1847
1848 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1849 inSwapBuffersDuration/1000.0);
1850 result.append(buffer);
1851
1852 snprintf(buffer, SIZE, " transaction time: %f us\n",
1853 inTransactionDuration/1000.0);
1854 result.append(buffer);
1855
1856 /*
1857 * VSYNC state
1858 */
1859 mEventThread->dump(result, buffer, SIZE);
1860
1861 /*
1862 * Dump HWComposer state
1863 */
1864 HWComposer& hwc(hw.getHwComposer());
1865 snprintf(buffer, SIZE, "h/w composer state:\n");
1866 result.append(buffer);
1867 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1868 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1869 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1870 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001871 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001872
1873 /*
1874 * Dump gralloc state
1875 */
1876 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1877 alloc.dump(result);
1878 hw.dump(result);
1879}
1880
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881status_t SurfaceFlinger::onTransact(
1882 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1883{
1884 switch (code) {
1885 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001886 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001889 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001890 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001891 case BLANK:
1892 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 {
1894 // codes that require permission check
1895 IPCThreadState* ipc = IPCThreadState::self();
1896 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001897 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001898 if ((uid != AID_GRAPHICS) &&
1899 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
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);
1902 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001903 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001904 break;
1905 }
1906 case CAPTURE_SCREEN:
1907 {
1908 // codes that require permission check
1909 IPCThreadState* ipc = IPCThreadState::self();
1910 const int pid = ipc->getCallingPid();
1911 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001912 if ((uid != AID_GRAPHICS) &&
1913 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001914 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001915 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1916 return PERMISSION_DENIED;
1917 }
1918 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 }
1920 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1923 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001924 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001925 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001926 IPCThreadState* ipc = IPCThreadState::self();
1927 const int pid = ipc->getCallingPid();
1928 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001929 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001930 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931 return PERMISSION_DENIED;
1932 }
1933 int n;
1934 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001935 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001936 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 return NO_ERROR;
1938 case 1002: // SHOW_UPDATES
1939 n = data.readInt32();
1940 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001941 invalidateHwcGeometry();
1942 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001945 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001946 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001947 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001948 case 1005:{ // force transaction
1949 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1950 return NO_ERROR;
1951 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001952 case 1006:{ // send empty update
1953 signalRefresh();
1954 return NO_ERROR;
1955 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001956 case 1008: // toggle use of hw composer
1957 n = data.readInt32();
1958 mDebugDisableHWC = n ? 1 : 0;
1959 invalidateHwcGeometry();
1960 repaintEverything();
1961 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001962 case 1009: // toggle use of transform hint
1963 n = data.readInt32();
1964 mDebugDisableTransformHint = n ? 1 : 0;
1965 invalidateHwcGeometry();
1966 repaintEverything();
1967 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001969 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970 reply->writeInt32(0);
1971 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001972 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001973 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974 return NO_ERROR;
1975 case 1013: {
1976 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001977 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001978 reply->writeInt32(hw.getPageFlipCount());
1979 }
1980 return NO_ERROR;
1981 }
1982 }
1983 return err;
1984}
1985
Mathias Agopian53331da2011-08-22 21:44:41 -07001986void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001987 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001988 const Rect bounds(hw.getBounds());
1989 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001990 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001991}
1992
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001993void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1994 Mutex::Autolock _l(mInvalidateLock);
1995 mInvalidateRegion = reg;
1996}
1997
1998Region SurfaceFlinger::getAndClearInvalidateRegion() {
1999 Mutex::Autolock _l(mInvalidateLock);
2000 Region reg(mInvalidateRegion);
2001 mInvalidateRegion.clear();
2002 return reg;
2003}
2004
Mathias Agopian59119e62010-10-11 12:37:43 -07002005// ---------------------------------------------------------------------------
2006
Mathias Agopian118d0242011-10-13 16:02:48 -07002007status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2008 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2009{
2010 Mutex::Autolock _l(mStateLock);
2011 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2012}
2013
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002014status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2015 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002016{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002017 ATRACE_CALL();
2018
Mathias Agopian59119e62010-10-11 12:37:43 -07002019 if (!GLExtensions::getInstance().haveFramebufferObject())
2020 return INVALID_OPERATION;
2021
2022 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002023 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002024 const uint32_t hw_w = hw.getWidth();
2025 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002026 GLfloat u = 1;
2027 GLfloat v = 1;
2028
2029 // make sure to clear all GL error flags
2030 while ( glGetError() != GL_NO_ERROR ) ;
2031
2032 // create a FBO
2033 GLuint name, tname;
2034 glGenTextures(1, &tname);
2035 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002036 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2037 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2039 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002040 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002041 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002042 GLint tw = (2 << (31 - clz(hw_w)));
2043 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2045 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002046 u = GLfloat(hw_w) / tw;
2047 v = GLfloat(hw_h) / th;
2048 }
2049 glGenFramebuffersOES(1, &name);
2050 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2052 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002053
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002055 glDisable(GL_TEXTURE_EXTERNAL_OES);
2056 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 glClearColor(0,0,0,1);
2058 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002059 glMatrixMode(GL_MODELVIEW);
2060 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002061 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002062 const size_t count = layers.size();
2063 for (size_t i=0 ; i<count ; ++i) {
2064 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07002065 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002066 }
2067
Mathias Agopian118d0242011-10-13 16:02:48 -07002068 hw.compositionComplete();
2069
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070 // back to main framebuffer
2071 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 glDeleteFramebuffersOES(1, &name);
2073
2074 *textureName = tname;
2075 *uOut = u;
2076 *vOut = v;
2077 return NO_ERROR;
2078}
2079
2080// ---------------------------------------------------------------------------
2081
Mathias Agopianed9807b2012-05-18 14:18:08 -07002082class VSyncWaiter {
2083 DisplayEventReceiver::Event buffer[4];
2084 sp<Looper> looper;
2085 sp<IDisplayEventConnection> events;
2086 sp<BitTube> eventTube;
2087public:
2088 VSyncWaiter(const sp<EventThread>& eventThread) {
2089 looper = new Looper(true);
2090 events = eventThread->createEventConnection();
2091 eventTube = events->getDataChannel();
2092 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
2093 events->requestNextVsync();
2094 }
2095
2096 void wait() {
2097 ssize_t n;
2098
2099 looper->pollOnce(-1);
2100 // we don't handle any errors here, it doesn't matter
2101 // and we don't want to take the risk to get stuck.
2102
2103 // drain the events...
2104 while ((n = DisplayEventReceiver::getEvents(
2105 eventTube, buffer, 4)) > 0) ;
2106
2107 events->requestNextVsync();
2108 }
2109};
2110
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002111status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
2112{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002113 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002114 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002115 const uint32_t hw_w = hw.getWidth();
2116 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07002117 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118
2119 GLfloat u, v;
2120 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002121 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002122 if (result != NO_ERROR) {
2123 return result;
2124 }
2125
2126 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002127 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002128 glBindTexture(GL_TEXTURE_2D, tname);
2129 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2130 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2131 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002132 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2133 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002134 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2135 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2136 glVertexPointer(2, GL_FLOAT, 0, vtx);
2137
Mathias Agopianffcf4652011-07-07 17:30:31 -07002138 /*
2139 * Texture coordinate mapping
2140 *
2141 * u
2142 * 1 +----------+---+
2143 * | | | | image is inverted
2144 * | V | | w.r.t. the texture
2145 * 1-v +----------+ | coordinates
2146 * | |
2147 * | |
2148 * | |
2149 * 0 +--------------+
2150 * 0 1
2151 *
2152 */
2153
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002154 class s_curve_interpolator {
2155 const float nbFrames, s, v;
2156 public:
2157 s_curve_interpolator(int nbFrames, float s)
2158 : nbFrames(1.0f / (nbFrames-1)), s(s),
2159 v(1.0f + expf(-s + 0.5f*s)) {
2160 }
2161 float operator()(int f) {
2162 const float x = f * nbFrames;
2163 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2164 }
2165 };
2166
2167 class v_stretch {
2168 const GLfloat hw_w, hw_h;
2169 public:
2170 v_stretch(uint32_t hw_w, uint32_t hw_h)
2171 : hw_w(hw_w), hw_h(hw_h) {
2172 }
2173 void operator()(GLfloat* vtx, float v) {
2174 const GLfloat w = hw_w + (hw_w * v);
2175 const GLfloat h = hw_h - (hw_h * v);
2176 const GLfloat x = (hw_w - w) * 0.5f;
2177 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002178 vtx[0] = x; vtx[1] = y;
2179 vtx[2] = x; vtx[3] = y + h;
2180 vtx[4] = x + w; vtx[5] = y + h;
2181 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002182 }
2183 };
2184
2185 class h_stretch {
2186 const GLfloat hw_w, hw_h;
2187 public:
2188 h_stretch(uint32_t hw_w, uint32_t hw_h)
2189 : hw_w(hw_w), hw_h(hw_h) {
2190 }
2191 void operator()(GLfloat* vtx, float v) {
2192 const GLfloat w = hw_w - (hw_w * v);
2193 const GLfloat h = 1.0f;
2194 const GLfloat x = (hw_w - w) * 0.5f;
2195 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002196 vtx[0] = x; vtx[1] = y;
2197 vtx[2] = x; vtx[3] = y + h;
2198 vtx[4] = x + w; vtx[5] = y + h;
2199 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 }
2201 };
2202
Mathias Agopianed9807b2012-05-18 14:18:08 -07002203 VSyncWaiter vsync(mEventThread);
2204
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002206 char value[PROPERTY_VALUE_MAX];
2207 property_get("debug.sf.electron_frames", value, "24");
2208 int nbFrames = (atoi(value) + 1) >> 1;
2209 if (nbFrames <= 0) // just in case
2210 nbFrames = 24;
2211
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002212 s_curve_interpolator itr(nbFrames, 7.5f);
2213 s_curve_interpolator itg(nbFrames, 8.0f);
2214 s_curve_interpolator itb(nbFrames, 8.5f);
2215
2216 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002217
2218 glMatrixMode(GL_TEXTURE);
2219 glLoadIdentity();
2220 glMatrixMode(GL_MODELVIEW);
2221 glLoadIdentity();
2222
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002223 glEnable(GL_BLEND);
2224 glBlendFunc(GL_ONE, GL_ONE);
2225 for (int i=0 ; i<nbFrames ; i++) {
2226 float x, y, w, h;
2227 const float vr = itr(i);
2228 const float vg = itg(i);
2229 const float vb = itb(i);
2230
Mathias Agopianed9807b2012-05-18 14:18:08 -07002231 // wait for vsync
2232 vsync.wait();
2233
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002234 // clear screen
2235 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002236 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002237 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002238
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002239 // draw the red plane
2240 vverts(vtx, vr);
2241 glColorMask(1,0,0,1);
2242 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002243
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244 // draw the green plane
2245 vverts(vtx, vg);
2246 glColorMask(0,1,0,1);
2247 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002248
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002249 // draw the blue plane
2250 vverts(vtx, vb);
2251 glColorMask(0,0,1,1);
2252 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002253
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002254 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002255 glDisable(GL_TEXTURE_2D);
2256 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002257 glColor4f(vg, vg, vg, 1);
2258 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2259 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002260 }
2261
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002262 h_stretch hverts(hw_w, hw_h);
2263 glDisable(GL_BLEND);
2264 glDisable(GL_TEXTURE_2D);
2265 glColorMask(1,1,1,1);
2266 for (int i=0 ; i<nbFrames ; i++) {
2267 const float v = itg(i);
2268 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002269
2270 // wait for vsync
2271 vsync.wait();
2272
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002273 glClear(GL_COLOR_BUFFER_BIT);
2274 glColor4f(1-v, 1-v, 1-v, 1);
2275 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2276 hw.flip(screenBounds);
2277 }
2278
2279 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002280 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2281 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002282 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002283 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002284 return NO_ERROR;
2285}
2286
2287status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2288{
2289 status_t result = PERMISSION_DENIED;
2290
2291 if (!GLExtensions::getInstance().haveFramebufferObject())
2292 return INVALID_OPERATION;
2293
2294
2295 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002296 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002297 const uint32_t hw_w = hw.getWidth();
2298 const uint32_t hw_h = hw.getHeight();
2299 const Region screenBounds(hw.bounds());
2300
2301 GLfloat u, v;
2302 GLuint tname;
2303 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2304 if (result != NO_ERROR) {
2305 return result;
2306 }
2307
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002308 GLfloat vtx[8];
2309 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002310 glBindTexture(GL_TEXTURE_2D, tname);
2311 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2312 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2313 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002314 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2315 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002316 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2317 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2318 glVertexPointer(2, GL_FLOAT, 0, vtx);
2319
2320 class s_curve_interpolator {
2321 const float nbFrames, s, v;
2322 public:
2323 s_curve_interpolator(int nbFrames, float s)
2324 : nbFrames(1.0f / (nbFrames-1)), s(s),
2325 v(1.0f + expf(-s + 0.5f*s)) {
2326 }
2327 float operator()(int f) {
2328 const float x = f * nbFrames;
2329 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2330 }
2331 };
2332
2333 class v_stretch {
2334 const GLfloat hw_w, hw_h;
2335 public:
2336 v_stretch(uint32_t hw_w, uint32_t hw_h)
2337 : hw_w(hw_w), hw_h(hw_h) {
2338 }
2339 void operator()(GLfloat* vtx, float v) {
2340 const GLfloat w = hw_w + (hw_w * v);
2341 const GLfloat h = hw_h - (hw_h * v);
2342 const GLfloat x = (hw_w - w) * 0.5f;
2343 const GLfloat y = (hw_h - h) * 0.5f;
2344 vtx[0] = x; vtx[1] = y;
2345 vtx[2] = x; vtx[3] = y + h;
2346 vtx[4] = x + w; vtx[5] = y + h;
2347 vtx[6] = x + w; vtx[7] = y;
2348 }
2349 };
2350
2351 class h_stretch {
2352 const GLfloat hw_w, hw_h;
2353 public:
2354 h_stretch(uint32_t hw_w, uint32_t hw_h)
2355 : hw_w(hw_w), hw_h(hw_h) {
2356 }
2357 void operator()(GLfloat* vtx, float v) {
2358 const GLfloat w = hw_w - (hw_w * v);
2359 const GLfloat h = 1.0f;
2360 const GLfloat x = (hw_w - w) * 0.5f;
2361 const GLfloat y = (hw_h - h) * 0.5f;
2362 vtx[0] = x; vtx[1] = y;
2363 vtx[2] = x; vtx[3] = y + h;
2364 vtx[4] = x + w; vtx[5] = y + h;
2365 vtx[6] = x + w; vtx[7] = y;
2366 }
2367 };
2368
Mathias Agopianed9807b2012-05-18 14:18:08 -07002369 VSyncWaiter vsync(mEventThread);
2370
Mathias Agopiana6546e52010-10-14 12:33:07 -07002371 // the full animation is 12 frames
2372 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002373 s_curve_interpolator itr(nbFrames, 7.5f);
2374 s_curve_interpolator itg(nbFrames, 8.0f);
2375 s_curve_interpolator itb(nbFrames, 8.5f);
2376
2377 h_stretch hverts(hw_w, hw_h);
2378 glDisable(GL_BLEND);
2379 glDisable(GL_TEXTURE_2D);
2380 glColorMask(1,1,1,1);
2381 for (int i=nbFrames-1 ; i>=0 ; i--) {
2382 const float v = itg(i);
2383 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002384
2385 // wait for vsync
2386 vsync.wait();
2387
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002388 glClear(GL_COLOR_BUFFER_BIT);
2389 glColor4f(1-v, 1-v, 1-v, 1);
2390 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2391 hw.flip(screenBounds);
2392 }
2393
Mathias Agopiana6546e52010-10-14 12:33:07 -07002394 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002395 v_stretch vverts(hw_w, hw_h);
2396 glEnable(GL_BLEND);
2397 glBlendFunc(GL_ONE, GL_ONE);
2398 for (int i=nbFrames-1 ; i>=0 ; i--) {
2399 float x, y, w, h;
2400 const float vr = itr(i);
2401 const float vg = itg(i);
2402 const float vb = itb(i);
2403
Mathias Agopianed9807b2012-05-18 14:18:08 -07002404 // wait for vsync
2405 vsync.wait();
2406
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002407 // clear screen
2408 glColorMask(1,1,1,1);
2409 glClear(GL_COLOR_BUFFER_BIT);
2410 glEnable(GL_TEXTURE_2D);
2411
2412 // draw the red plane
2413 vverts(vtx, vr);
2414 glColorMask(1,0,0,1);
2415 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2416
2417 // draw the green plane
2418 vverts(vtx, vg);
2419 glColorMask(0,1,0,1);
2420 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2421
2422 // draw the blue plane
2423 vverts(vtx, vb);
2424 glColorMask(0,0,1,1);
2425 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2426
2427 hw.flip(screenBounds);
2428 }
2429
2430 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002431 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002432 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002433 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002434 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002435
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002436 return NO_ERROR;
2437}
2438
2439// ---------------------------------------------------------------------------
2440
Mathias Agopianabd671a2010-10-14 14:54:06 -07002441status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002442{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002443 ATRACE_CALL();
2444
Mathias Agopian1b031492012-06-20 17:51:20 -07002445 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002446 if (!hw.canDraw()) {
2447 // we're already off
2448 return NO_ERROR;
2449 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002450
2451 // turn off hwc while we're doing the animation
2452 hw.getHwComposer().disable();
2453 // and make sure to turn it back on (if needed) next time we compose
2454 invalidateHwcGeometry();
2455
Mathias Agopianabd671a2010-10-14 14:54:06 -07002456 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2457 electronBeamOffAnimationImplLocked();
2458 }
2459
2460 // always clear the whole screen at the end of the animation
2461 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002462 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002463 hw.flip( Region(hw.bounds()) );
2464
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002465 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002466}
2467
2468status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2469{
Mathias Agopian59119e62010-10-11 12:37:43 -07002470 class MessageTurnElectronBeamOff : public MessageBase {
2471 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002472 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002473 status_t result;
2474 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002475 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2476 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002477 }
2478 status_t getResult() const {
2479 return result;
2480 }
2481 virtual bool handler() {
2482 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002483 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002484 return true;
2485 }
2486 };
2487
Mathias Agopianabd671a2010-10-14 14:54:06 -07002488 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002489 status_t res = postMessageSync(msg);
2490 if (res == NO_ERROR) {
2491 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002492
2493 // work-around: when the power-manager calls us we activate the
2494 // animation. eventually, the "on" animation will be called
2495 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002496 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002497 }
2498 return res;
2499}
2500
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002501// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002502
Mathias Agopianabd671a2010-10-14 14:54:06 -07002503status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002504{
Mathias Agopian1b031492012-06-20 17:51:20 -07002505 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002506 if (hw.canDraw()) {
2507 // we're already on
2508 return NO_ERROR;
2509 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002510 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2511 electronBeamOnAnimationImplLocked();
2512 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002513
2514 // make sure to redraw the whole screen when the animation is done
2515 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002516 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002517
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002518 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002519}
2520
2521status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2522{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002523 class MessageTurnElectronBeamOn : public MessageBase {
2524 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002525 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002526 status_t result;
2527 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002528 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2529 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002530 }
2531 status_t getResult() const {
2532 return result;
2533 }
2534 virtual bool handler() {
2535 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002536 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002537 return true;
2538 }
2539 };
2540
Mathias Agopianabd671a2010-10-14 14:54:06 -07002541 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002542 return NO_ERROR;
2543}
2544
2545// ---------------------------------------------------------------------------
2546
Mathias Agopian74c40c02010-09-29 13:02:36 -07002547status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2548 sp<IMemoryHeap>* heap,
2549 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002550 uint32_t sw, uint32_t sh,
2551 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002552{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002553 ATRACE_CALL();
2554
Mathias Agopian74c40c02010-09-29 13:02:36 -07002555 status_t result = PERMISSION_DENIED;
2556
2557 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002558 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002559 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002560 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002561
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002562 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002563 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002564 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002565
2566 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002567 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002568 const uint32_t hw_w = hw.getWidth();
2569 const uint32_t hw_h = hw.getHeight();
2570
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002571 // if we have secure windows on this display, never allow the screen capture
2572 if (hw.getSecureLayerVisible()) {
2573 return PERMISSION_DENIED;
2574 }
2575
2576 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002577 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002578 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002579
2580 sw = (!sw) ? hw_w : sw;
2581 sh = (!sh) ? hw_h : sh;
2582 const size_t size = sw * sh * 4;
2583
Steve Block9d453682011-12-20 16:23:08 +00002584 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002585 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002586
Mathias Agopian74c40c02010-09-29 13:02:36 -07002587 // make sure to clear all GL error flags
2588 while ( glGetError() != GL_NO_ERROR ) ;
2589
2590 // create a FBO
2591 GLuint name, tname;
2592 glGenRenderbuffersOES(1, &tname);
2593 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2594 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002595
Mathias Agopian74c40c02010-09-29 13:02:36 -07002596 glGenFramebuffersOES(1, &name);
2597 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2598 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2599 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2600
2601 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002602
Mathias Agopian74c40c02010-09-29 13:02:36 -07002603 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2604
2605 // invert everything, b/c glReadPixel() below will invert the FB
2606 glViewport(0, 0, sw, sh);
2607 glMatrixMode(GL_PROJECTION);
2608 glPushMatrix();
2609 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002610 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002611 glMatrixMode(GL_MODELVIEW);
2612
2613 // redraw the screen entirely...
2614 glClearColor(0,0,0,1);
2615 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002616
Jamie Gennis9575f602011-10-07 14:51:16 -07002617 const LayerVector& layers(mDrawingState.layersSortedByZ);
2618 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002619 for (size_t i=0 ; i<count ; ++i) {
2620 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002621 const uint32_t flags = layer->drawingState().flags;
2622 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2623 const uint32_t z = layer->drawingState().z;
2624 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002625 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002626 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002627 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002628 }
2629
Mathias Agopian74c40c02010-09-29 13:02:36 -07002630 // check for errors and return screen capture
2631 if (glGetError() != GL_NO_ERROR) {
2632 // error while rendering
2633 result = INVALID_OPERATION;
2634 } else {
2635 // allocate shared memory large enough to hold the
2636 // screen capture
2637 sp<MemoryHeapBase> base(
2638 new MemoryHeapBase(size, 0, "screen-capture") );
2639 void* const ptr = base->getBase();
2640 if (ptr) {
2641 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002642 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002643 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2644 if (glGetError() == GL_NO_ERROR) {
2645 *heap = base;
2646 *w = sw;
2647 *h = sh;
2648 *f = PIXEL_FORMAT_RGBA_8888;
2649 result = NO_ERROR;
2650 }
2651 } else {
2652 result = NO_MEMORY;
2653 }
2654 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002655 glViewport(0, 0, hw_w, hw_h);
2656 glMatrixMode(GL_PROJECTION);
2657 glPopMatrix();
2658 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002659 } else {
2660 result = BAD_VALUE;
2661 }
2662
2663 // release FBO resources
2664 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2665 glDeleteRenderbuffersOES(1, &tname);
2666 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002667
2668 hw.compositionComplete();
2669
Steve Block9d453682011-12-20 16:23:08 +00002670 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002671
Mathias Agopian74c40c02010-09-29 13:02:36 -07002672 return result;
2673}
2674
2675
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002676status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2677 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002678 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002679 uint32_t sw, uint32_t sh,
2680 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002681{
2682 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002683 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002684 return BAD_VALUE;
2685
2686 if (!GLExtensions::getInstance().haveFramebufferObject())
2687 return INVALID_OPERATION;
2688
2689 class MessageCaptureScreen : public MessageBase {
2690 SurfaceFlinger* flinger;
2691 DisplayID dpy;
2692 sp<IMemoryHeap>* heap;
2693 uint32_t* w;
2694 uint32_t* h;
2695 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002696 uint32_t sw;
2697 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002698 uint32_t minLayerZ;
2699 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002700 status_t result;
2701 public:
2702 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002703 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002704 uint32_t sw, uint32_t sh,
2705 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002706 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002707 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2708 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2709 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002710 {
2711 }
2712 status_t getResult() const {
2713 return result;
2714 }
2715 virtual bool handler() {
2716 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002717 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002718 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002719 return true;
2720 }
2721 };
2722
2723 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002724 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002725 status_t res = postMessageSync(msg);
2726 if (res == NO_ERROR) {
2727 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2728 }
2729 return res;
2730}
2731
2732// ---------------------------------------------------------------------------
2733
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002734sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2735{
2736 sp<Layer> result;
2737 Mutex::Autolock _l(mStateLock);
2738 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2739 return result;
2740}
2741
2742// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002743
Jamie Gennis9a78c902011-01-12 18:30:40 -08002744GraphicBufferAlloc::GraphicBufferAlloc() {}
2745
2746GraphicBufferAlloc::~GraphicBufferAlloc() {}
2747
2748sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002749 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002750 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2751 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002752 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002753 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002754 if (err == NO_MEMORY) {
2755 GraphicBuffer::dumpAllocationsToSystemLog();
2756 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002757 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002758 "failed (%s), handle=%p",
2759 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002760 return 0;
2761 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002762 return graphicBuffer;
2763}
2764
Jamie Gennis9a78c902011-01-12 18:30:40 -08002765// ---------------------------------------------------------------------------
2766
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002767}; // namespace android