blob: 6cf11283419a39491ce59ced6431e32bd10302c3 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
39#include <gui/SurfaceTextureClient.h>
40
41#include <ui/GraphicBufferAllocator.h>
42#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <utils/String8.h>
45#include <utils/String16.h>
46#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Mathias Agopian921e6ac2012-07-23 23:11:29 -070049#include <private/android_filesystem_config.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
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
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070098 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099{
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // debugging stuff...
103 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 property_get("debug.sf.showupdates", value, "0");
106 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107
Colin Cross3854ed52012-03-23 14:17:18 -0700108#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 property_get("debug.sf.ddms", value, "0");
110 mDebugDDMS = atoi(value);
111 if (mDebugDDMS) {
112 DdmConnection::start(getServiceName());
113 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700114#else
115#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700116#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117
Steve Blocka19954a2012-01-04 20:05:49 +0000118 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000119 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800122void SurfaceFlinger::onFirstRef()
123{
124 mEventQueue.init(this);
125
126 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
127
128 // Wait for the main thread to be done with its initialization
129 mReadyToRunBarrier.wait();
130}
131
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133SurfaceFlinger::~SurfaceFlinger()
134{
135 glDeleteTextures(1, &mWormholeTexName);
Mathias Agopiana4912602012-07-12 14:25:33 -0700136 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
137 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141void SurfaceFlinger::binderDied(const wp<IBinder>& who)
142{
143 // the window manager died on us. prepare its eulogy.
144
145 // reset screen orientation
146 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700147 Vector<DisplayState> displays;
148 DisplayState d;
149 d.orientation = eOrientationDefault;
150 displays.add(d);
151 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152
153 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700154 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800155}
156
Mathias Agopian7e27f052010-05-28 14:22:23 -0700157sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Mathias Agopian96f08192010-06-02 23:28:45 -0700159 sp<ISurfaceComposerClient> bclient;
160 sp<Client> client(new Client(this));
161 status_t err = client->initCheck();
162 if (err == NO_ERROR) {
163 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 return bclient;
166}
167
Jamie Gennis9a78c902011-01-12 18:30:40 -0800168sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
169{
170 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
171 return gba;
172}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700173
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174void SurfaceFlinger::bootFinished()
175{
176 const nsecs_t now = systemTime();
177 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000178 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700180
181 // wait patiently for the window manager death
182 const String16 name("window");
183 sp<IBinder> window(defaultServiceManager()->getService(name));
184 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700185 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700186 }
187
188 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700189 // formerly we would just kill the process, but we now ask it to exit so it
190 // can choose where to stop the animation.
191 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700194void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
195 class MessageDestroyGLTexture : public MessageBase {
196 GLuint texture;
197 public:
198 MessageDestroyGLTexture(GLuint texture)
199 : texture(texture) {
200 }
201 virtual bool handler() {
202 glDeleteTextures(1, &texture);
203 return true;
204 }
205 };
206 postMessageAsync(new MessageDestroyGLTexture(texture));
207}
208
Mathias Agopiana4912602012-07-12 14:25:33 -0700209status_t SurfaceFlinger::selectConfigForPixelFormat(
210 EGLDisplay dpy,
211 EGLint const* attrs,
212 PixelFormat format,
213 EGLConfig* outConfig)
214{
215 EGLConfig config = NULL;
216 EGLint numConfigs = -1, n=0;
217 eglGetConfigs(dpy, NULL, 0, &numConfigs);
218 EGLConfig* const configs = new EGLConfig[numConfigs];
219 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
220 for (int i=0 ; i<n ; i++) {
221 EGLint nativeVisualId = 0;
222 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
223 if (nativeVisualId>0 && format == nativeVisualId) {
224 *outConfig = configs[i];
225 delete [] configs;
226 return NO_ERROR;
227 }
228 }
229 delete [] configs;
230 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
Mathias Agopiana4912602012-07-12 14:25:33 -0700233EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
234 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
235 // it is to be used with WIFI displays
236 EGLConfig config;
237 EGLint dummy;
238 status_t err;
239 EGLint attribs[] = {
240 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
241 EGL_RECORDABLE_ANDROID, EGL_TRUE,
242 EGL_NONE
243 };
244 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
245 if (err) {
246 // maybe we failed because of EGL_RECORDABLE_ANDROID
247 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
248 attribs[2] = EGL_NONE;
249 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
250 }
251 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
252 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
253 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
254 }
255 return config;
256}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
Mathias Agopiana4912602012-07-12 14:25:33 -0700258EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
259 // Also create our EGLContext
260 EGLint contextAttributes[] = {
261#ifdef EGL_IMG_context_priority
262#ifdef HAS_CONTEXT_PRIORITY
263#warning "using EGL_IMG_context_priority"
264 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
265#endif
266#endif
267 EGL_NONE, EGL_NONE
268 };
269 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
270 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
271 return ctxt;
272}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
Mathias Agopiana4912602012-07-12 14:25:33 -0700274void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
275 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
276 if (!result) {
277 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
278 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 }
280
Mathias Agopiana4912602012-07-12 14:25:33 -0700281 GLExtensions& extensions(GLExtensions::getInstance());
282 extensions.initWithGLStrings(
283 glGetString(GL_VENDOR),
284 glGetString(GL_RENDERER),
285 glGetString(GL_VERSION),
286 glGetString(GL_EXTENSIONS),
287 eglQueryString(display, EGL_VENDOR),
288 eglQueryString(display, EGL_VERSION),
289 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700290
Mathias Agopiana4912602012-07-12 14:25:33 -0700291 EGLint w, h;
292 eglQuerySurface(display, surface, EGL_WIDTH, &w);
293 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294
Mathias Agopiana4912602012-07-12 14:25:33 -0700295 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
296 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700299 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 glShadeModel(GL_FLAT);
302 glDisable(GL_DITHER);
303 glDisable(GL_CULL_FACE);
304
Mathias Agopiana4912602012-07-12 14:25:33 -0700305 struct pack565 {
306 inline uint16_t operator() (int r, int g, int b) const {
307 return (r<<11)|(g<<5)|b;
308 }
309 } pack565;
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
312 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700313 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 glGenTextures(1, &mWormholeTexName);
315 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
316 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
317 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
318 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
319 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
320 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700321 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
322
323 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
324 glGenTextures(1, &mProtectedTexName);
325 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
326 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
327 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
328 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
329 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
331 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332
333 glViewport(0, 0, w, h);
334 glMatrixMode(GL_PROJECTION);
335 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700336 // put the origin in the left-bottom corner
337 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338
Mathias Agopiana4912602012-07-12 14:25:33 -0700339 // print some debugging info
340 EGLint r,g,b,a;
341 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
342 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
343 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
344 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
345 ALOGI("EGL informations:");
346 ALOGI("vendor : %s", extensions.getEglVendor());
347 ALOGI("version : %s", extensions.getEglVersion());
348 ALOGI("extensions: %s", extensions.getEglExtension());
349 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
350 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
351 ALOGI("OpenGL ES informations:");
352 ALOGI("vendor : %s", extensions.getVendor());
353 ALOGI("renderer : %s", extensions.getRenderer());
354 ALOGI("version : %s", extensions.getVersion());
355 ALOGI("extensions: %s", extensions.getExtension());
356 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
357 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
358}
359
Mathias Agopiana4912602012-07-12 14:25:33 -0700360status_t SurfaceFlinger::readyToRun()
361{
362 ALOGI( "SurfaceFlinger's main thread ready to run. "
363 "Initializing graphics H/W...");
364
Mathias Agopiana4912602012-07-12 14:25:33 -0700365 // initialize EGL
Jesse Hall34a09ba2012-07-29 22:35:34 -0700366 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
367 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700368
369 // Initialize the main display
370 // create native window to main display
371 sp<FramebufferSurface> anw = FramebufferSurface::create();
372 ANativeWindow* const window = anw.get();
373 if (!window) {
374 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
375 exit(0);
376 }
377
378 // initialize the config and context
379 int format;
380 window->query(window, NATIVE_WINDOW_FORMAT, &format);
Jesse Hall34a09ba2012-07-29 22:35:34 -0700381 mEGLConfig = selectEGLConfig(mEGLDisplay, format);
382 mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
Mathias Agopiana4912602012-07-12 14:25:33 -0700383
384 // initialize our main display hardware
385 DisplayHardware* const hw = new DisplayHardware(this, 0, anw, mEGLConfig);
386 mDisplayHardwares[0] = hw;
387
388 // initialize OpenGL ES
389 EGLSurface surface = hw->getEGLSurface();
Jesse Hall34a09ba2012-07-29 22:35:34 -0700390 initializeGL(mEGLDisplay, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800391
Mathias Agopian028508c2012-07-25 21:12:12 -0700392 // start the EventThread
393 mEventThread = new EventThread(this);
394 mEventQueue.setEventThread(mEventThread);
395
Mathias Agopian86303202012-07-24 22:46:10 -0700396 // initialize the H/W composer
397 mHwc = new HWComposer(this,
398 *static_cast<HWComposer::EventHandler *>(this),
399 hw->getRefreshPeriod());
Mathias Agopian86303202012-07-24 22:46:10 -0700400
Mathias Agopiana4912602012-07-12 14:25:33 -0700401 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800402 mReadyToRunBarrier.open();
403
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700404 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700405 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700406
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407 return NO_ERROR;
408}
409
Mathias Agopiana67e4182012-06-19 17:26:12 -0700410void SurfaceFlinger::startBootAnim() {
411 // start boot animation
412 property_set("service.bootanim.exit", "0");
413 property_set("ctl.start", "bootanim");
414}
415
Mathias Agopiana4912602012-07-12 14:25:33 -0700416uint32_t SurfaceFlinger::getMaxTextureSize() const {
417 return mMaxTextureSize;
418}
419
420uint32_t SurfaceFlinger::getMaxViewportDims() const {
421 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
422 mMaxViewportDims[0] : mMaxViewportDims[1];
423}
424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800426
Jamie Gennis582270d2011-08-17 18:19:00 -0700427bool SurfaceFlinger::authenticateSurfaceTexture(
428 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800429 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700430 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800431
432 // Check the visible layer list for the ISurface
433 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
434 size_t count = currentLayers.size();
435 for (size_t i=0 ; i<count ; i++) {
436 const sp<LayerBase>& layer(currentLayers[i]);
437 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700438 if (lbc != NULL) {
439 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
440 if (lbcBinder == surfaceTextureBinder) {
441 return true;
442 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800443 }
444 }
445
446 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700447 // SurfaceTexture gets destroyed before all the clients are done using it,
448 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800449 // will instead fail later on when the client tries to use the surface,
450 // which should be reported as "surface XYZ returned an -ENODEV". The
451 // purgatorized layers are no less authentic than the visible ones, so this
452 // should not cause any harm.
453 size_t purgatorySize = mLayerPurgatory.size();
454 for (size_t i=0 ; i<purgatorySize ; i++) {
455 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
456 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700457 if (lbc != NULL) {
458 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
459 if (lbcBinder == surfaceTextureBinder) {
460 return true;
461 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800462 }
463 }
464
465 return false;
466}
467
Mathias Agopianc666cae2012-07-25 18:56:13 -0700468status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
469 if (uint32_t(dpy) >= 2) {
470 return BAD_INDEX;
471 }
472 const DisplayHardware& hw(getDefaultDisplayHardware());
473 return hw.getInfo(info);
474}
475
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800476// ----------------------------------------------------------------------------
477
478sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800479 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700480}
481
Mathias Agopian3094df32012-06-18 18:06:45 -0700482void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700483 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700484 EGLSurface result = EGL_NO_SURFACE;
485 EGLSurface old_surface = EGL_NO_SURFACE;
486 sp<SurfaceTextureClient> stc;
487
488 if (display != NULL) {
489 stc = new SurfaceTextureClient(display);
490 result = eglCreateWindowSurface(hw.getEGLDisplay(),
Mathias Agopiana4912602012-07-12 14:25:33 -0700491 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700492 ALOGE_IF(result == EGL_NO_SURFACE,
493 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
494 display.get());
495 }
496
497 { // scope for the lock
498 Mutex::Autolock _l(mStateLock);
499 old_surface = mExternalDisplaySurface;
500 mExternalDisplayNativeWindow = stc;
501 mExternalDisplaySurface = result;
502 ALOGD("mExternalDisplaySurface = %p", result);
503 }
504
505 if (old_surface != EGL_NO_SURFACE) {
506 // Note: EGL allows to destroy an object while its current
507 // it will fail to become current next time though.
508 eglDestroySurface(hw.getEGLDisplay(), old_surface);
509 }
510}
511
512EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
513 Mutex::Autolock _l(mStateLock);
514 return mExternalDisplaySurface;
515}
516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800518
519void SurfaceFlinger::waitForEvent() {
520 mEventQueue.waitMessage();
521}
522
523void SurfaceFlinger::signalTransaction() {
524 mEventQueue.invalidate();
525}
526
527void SurfaceFlinger::signalLayerUpdate() {
528 mEventQueue.invalidate();
529}
530
531void SurfaceFlinger::signalRefresh() {
532 mEventQueue.refresh();
533}
534
535status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
536 nsecs_t reltime, uint32_t flags) {
537 return mEventQueue.postMessage(msg, reltime);
538}
539
540status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
541 nsecs_t reltime, uint32_t flags) {
542 status_t res = mEventQueue.postMessage(msg, reltime);
543 if (res == NO_ERROR) {
544 msg->wait();
545 }
546 return res;
547}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548
Mathias Agopian4fec8732012-06-29 14:12:52 -0700549bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 return true;
552}
553
Mathias Agopian86303202012-07-24 22:46:10 -0700554void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
555 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
556 hw.onVSyncReceived(timestamp);
557 mEventThread->onVSyncReceived(dpy, timestamp);
558}
559
560void SurfaceFlinger::eventControl(int event, int enabled) {
561 getHwComposer().eventControl(event, enabled);
562}
563
Mathias Agopian4fec8732012-06-29 14:12:52 -0700564void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800565 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800566 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700567 case MessageQueue::INVALIDATE:
568 handleMessageTransaction();
569 handleMessageInvalidate();
570 signalRefresh();
571 break;
572 case MessageQueue::REFRESH:
573 handleMessageRefresh();
574 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800575 }
576}
577
Mathias Agopian4fec8732012-06-29 14:12:52 -0700578void SurfaceFlinger::handleMessageTransaction() {
579 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
580 uint32_t transactionFlags = peekTransactionFlags(mask);
581 if (transactionFlags) {
582 Region dirtyRegion;
583 dirtyRegion = handleTransaction(transactionFlags);
584 // XXX: dirtyRegion should be per screen
585 mDirtyRegion |= dirtyRegion;
586 }
587}
588
589void SurfaceFlinger::handleMessageInvalidate() {
590 Region dirtyRegion;
591 dirtyRegion = handlePageFlip();
592 // XXX: dirtyRegion should be per screen
593 mDirtyRegion |= dirtyRegion;
594}
595
596void SurfaceFlinger::handleMessageRefresh() {
597 handleRefresh();
598
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700599 if (mVisibleRegionsDirty) {
600 Region opaqueRegion;
601 Region dirtyRegion;
602 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
603 computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
604 mDirtyRegion.orSelf(dirtyRegion);
605
606 /*
607 * rebuild the visible layer list per screen
608 */
609
610 // TODO: iterate through all displays
611 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
612
613 Vector< sp<LayerBase> > layersSortedByZ;
614 const size_t count = currentLayers.size();
615 for (size_t i=0 ; i<count ; i++) {
616 if (!currentLayers[i]->visibleRegion.isEmpty()) {
617 // TODO: also check that this layer is associated to this display
618 layersSortedByZ.add(currentLayers[i]);
619 }
620 }
621 hw.setVisibleLayersSortedByZ(layersSortedByZ);
622
623
624 // FIXME: mWormholeRegion needs to be calculated per screen
625 //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
626 mWormholeRegion = Region(hw.getBounds()).subtract(
627 hw.getTransform().transform(opaqueRegion) );
628 mVisibleRegionsDirty = false;
629 invalidateHwcGeometry();
630 }
631
632
Mathias Agopian4fec8732012-06-29 14:12:52 -0700633 // XXX: dirtyRegion should be per screen, we should check all of them
634 if (mDirtyRegion.isEmpty()) {
635 return;
636 }
637
638 // TODO: iterate through all displays
639 const DisplayHardware& hw(getDisplayHardware(0));
640
641 // XXX: dirtyRegion should be per screen
642 // transform the dirty region into this screen's coordinate space
643 const Transform& planeTransform(hw.getTransform());
644 mDirtyRegion = planeTransform.transform(mDirtyRegion);
645 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
646 mDirtyRegion.andSelf(hw.bounds());
647
648
649 if (CC_UNLIKELY(mHwWorkListDirty)) {
650 // build the h/w work list
651 handleWorkList(hw);
652 }
653
654 if (CC_LIKELY(hw.canDraw())) {
655 // repaint the framebuffer (if needed)
656 handleRepaint(hw);
657 // inform the h/w that we're done compositing
658 hw.compositionComplete();
659 postFramebuffer();
660 } else {
661 // pretend we did the post
662 hw.compositionComplete();
663 }
664
665 // render to the external display if we have one
666 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
667 if (externalDisplaySurface != EGL_NO_SURFACE) {
668 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
669 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
670 externalDisplaySurface, externalDisplaySurface,
671 eglGetCurrentContext());
672
673 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
674
675 if (success) {
676 // redraw the screen entirely...
677 glDisable(GL_TEXTURE_EXTERNAL_OES);
678 glDisable(GL_TEXTURE_2D);
679 glClearColor(0,0,0,1);
680 glClear(GL_COLOR_BUFFER_BIT);
681 glMatrixMode(GL_MODELVIEW);
682 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700683
684 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700685 const size_t count = layers.size();
686 for (size_t i=0 ; i<count ; ++i) {
687 const sp<LayerBase>& layer(layers[i]);
688 layer->drawForSreenShot(hw);
689 }
690
691 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
692 ALOGE_IF(!success, "external display eglSwapBuffers failed");
693
694 hw.compositionComplete();
695 }
696
697 success = eglMakeCurrent(eglGetCurrentDisplay(),
698 cur, cur, eglGetCurrentContext());
699
700 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
701 }
702
703}
704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705void SurfaceFlinger::postFramebuffer()
706{
Mathias Agopian841cde52012-03-01 15:44:37 -0800707 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800708 // mSwapRegion can be empty here is some cases, for instance if a hidden
709 // or fully transparent window is updating.
710 // in that case, we need to flip anyways to not risk a deadlock with
711 // h/w composer.
712
Mathias Agopian1b031492012-06-20 17:51:20 -0700713 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian86303202012-07-24 22:46:10 -0700714 HWComposer& hwc(getHwComposer());
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700715 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
716 size_t numLayers = layers.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700717 const nsecs_t now = systemTime();
718 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700719
720 if (hwc.initCheck() == NO_ERROR) {
721 HWComposer::LayerListIterator cur = hwc.begin();
722 const HWComposer::LayerListIterator end = hwc.end();
723 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
724 if (cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700725 layers[i]->setAcquireFence(*cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700726 } else {
727 cur->setAcquireFenceFd(-1);
728 }
729 }
730 }
731
Mathias Agopiana44b0412011-10-16 18:46:35 -0700732 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800733
Jesse Hallef194142012-06-14 14:45:17 -0700734 if (hwc.initCheck() == NO_ERROR) {
Jesse Hall34a09ba2012-07-29 22:35:34 -0700735 hwc.commit(mEGLDisplay, hw.getEGLSurface());
Jesse Hallef194142012-06-14 14:45:17 -0700736 HWComposer::LayerListIterator cur = hwc.begin();
737 const HWComposer::LayerListIterator end = hwc.end();
738 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700739 layers[i]->onLayerDisplayed(&*cur);
Jesse Hallef194142012-06-14 14:45:17 -0700740 }
741 } else {
Jesse Hall34a09ba2012-07-29 22:35:34 -0700742 eglSwapBuffers(mEGLDisplay, hw.getEGLSurface());
Jesse Hallef194142012-06-14 14:45:17 -0700743 for (size_t i = 0; i < numLayers; i++) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700744 layers[i]->onLayerDisplayed(NULL);
Jesse Hallef194142012-06-14 14:45:17 -0700745 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800746 }
747
Mathias Agopiana44b0412011-10-16 18:46:35 -0700748 mLastSwapBufferTime = systemTime() - now;
749 mDebugInSwapBuffers = 0;
750 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751}
752
Mathias Agopian4fec8732012-06-29 14:12:52 -0700753Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754{
Mathias Agopian841cde52012-03-01 15:44:37 -0800755 ATRACE_CALL();
756
Mathias Agopian4fec8732012-06-29 14:12:52 -0700757 Region dirtyRegion;
758
Mathias Agopianca4d3602011-05-19 15:38:14 -0700759 Mutex::Autolock _l(mStateLock);
760 const nsecs_t now = systemTime();
761 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762
Mathias Agopianca4d3602011-05-19 15:38:14 -0700763 // Here we're guaranteed that some transaction flags are set
764 // so we can call handleTransactionLocked() unconditionally.
765 // We call getTransactionFlags(), which will also clear the flags,
766 // with mStateLock held to guarantee that mCurrentState won't change
767 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700768
Mathias Agopianca4d3602011-05-19 15:38:14 -0700769 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
770 transactionFlags = getTransactionFlags(mask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700771 dirtyRegion = handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700772
Mathias Agopianca4d3602011-05-19 15:38:14 -0700773 mLastTransactionTime = systemTime() - now;
774 mDebugInTransaction = 0;
775 invalidateHwcGeometry();
776 // here the transaction has been committed
Mathias Agopian4fec8732012-06-29 14:12:52 -0700777
778 return dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700779}
780
Mathias Agopian4fec8732012-06-29 14:12:52 -0700781Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700782{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783 Region dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700784 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 const size_t count = currentLayers.size();
786
787 /*
788 * Traversal of the children
789 * (perform the transaction for each of them if needed)
790 */
791
792 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
793 if (layersNeedTransaction) {
794 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700795 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
797 if (!trFlags) continue;
798
799 const uint32_t flags = layer->doTransaction(0);
800 if (flags & Layer::eVisibleRegion)
801 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 }
803 }
804
805 /*
806 * Perform our own transaction if needed
807 */
808
809 if (transactionFlags & eTransactionNeeded) {
810 if (mCurrentState.orientation != mDrawingState.orientation) {
811 // the orientation has changed, recompute all visible regions
812 // and invalidate everything.
813
Mathias Agopian1b031492012-06-20 17:51:20 -0700814 const int dpy = 0; // TODO: should be a parameter
815 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
Mathias Agopian98a121a2012-07-24 21:08:59 -0700816 hw.setOrientation(mCurrentState.orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817
Mathias Agopian1b031492012-06-20 17:51:20 -0700818 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 mVisibleRegionsDirty = true;
820 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 }
822
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700823 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
824 // layers have been added
825 mVisibleRegionsDirty = true;
826 }
827
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 // some layers might have been removed, so
829 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700830 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700831 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700833 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700834 const size_t count = previousLayers.size();
835 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700836 const sp<LayerBase>& layer(previousLayers[i]);
837 if (currentLayers.indexOf( layer ) < 0) {
838 // this layer is not visible anymore
Mathias Agopian4fec8732012-06-29 14:12:52 -0700839 // TODO: we could traverse the tree from front to back and compute the actual visible region
840 // TODO: we could cache the transformed region
841 Layer::State front(layer->drawingState());
842 Region visibleReg = front.transform.transform(
843 Region(Rect(front.active.w, front.active.h)));
844 dirtyRegion.orSelf(visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700845 }
846 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 }
849
850 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700851 return dirtyRegion;
852}
853
854void SurfaceFlinger::commitTransaction()
855{
856 if (!mLayersPendingRemoval.isEmpty()) {
857 // Notify removed layers now that they can't be drawn from
858 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
859 mLayersPendingRemoval[i]->onRemoved();
860 }
861 mLayersPendingRemoval.clear();
862 }
863
864 mDrawingState = mCurrentState;
865 mTransationPending = false;
866 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867}
868
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800870 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871{
Mathias Agopian841cde52012-03-01 15:44:37 -0800872 ATRACE_CALL();
873
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 Region aboveOpaqueLayers;
875 Region aboveCoveredLayers;
876 Region dirty;
877
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700878 dirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879
880 size_t i = currentLayers.size();
881 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700882 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883
884 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700885 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886
Mathias Agopianab028732010-03-16 16:41:46 -0700887 /*
888 * opaqueRegion: area of a surface that is fully opaque.
889 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700891
892 /*
893 * visibleRegion: area of a surface that is visible on screen
894 * and not fully transparent. This is essentially the layer's
895 * footprint minus the opaque regions above it.
896 * Areas covered by a translucent surface are considered visible.
897 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700899
900 /*
901 * coveredRegion: area of a surface that is covered by all
902 * visible regions above it (which includes the translucent areas).
903 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700905
906
907 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800908 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700909 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700910 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700912 if (!visibleRegion.isEmpty()) {
913 // Remove the transparent area from the visible region
914 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700915 Region transparentRegionScreen;
916 const Transform tr(s.transform);
917 if (tr.transformed()) {
918 if (tr.preserveRects()) {
919 // transform the transparent region
920 transparentRegionScreen = tr.transform(s.transparentRegion);
921 } else {
922 // transformation too complex, can't do the
923 // transparent region optimization.
924 transparentRegionScreen.clear();
925 }
926 } else {
927 transparentRegionScreen = s.transparentRegion;
928 }
929 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700930 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931
Mathias Agopianab028732010-03-16 16:41:46 -0700932 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700933 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700934 if (s.alpha==255 && !translucent &&
935 ((layerOrientation & Transform::ROT_INVALID) == false)) {
936 // the opaque region is the layer's footprint
937 opaqueRegion = visibleRegion;
938 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 }
940 }
941
Mathias Agopianab028732010-03-16 16:41:46 -0700942 // Clip the covered region to the visible region
943 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
944
945 // Update aboveCoveredLayers for next (lower) layer
946 aboveCoveredLayers.orSelf(visibleRegion);
947
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 // subtract the opaque region covered by the layers above us
949 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950
951 // compute this layer's dirty region
952 if (layer->contentDirty) {
953 // we need to invalidate the whole region
954 dirty = visibleRegion;
955 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700956 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 layer->contentDirty = false;
958 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700959 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700960 * the exposed region consists of two components:
961 * 1) what's VISIBLE now and was COVERED before
962 * 2) what's EXPOSED now less what was EXPOSED before
963 *
964 * note that (1) is conservative, we start with the whole
965 * visible region but only keep what used to be covered by
966 * something -- which mean it may have been exposed.
967 *
968 * (2) handles areas that were not covered by anything but got
969 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700970 */
Mathias Agopianab028732010-03-16 16:41:46 -0700971 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700972 const Region oldVisibleRegion = layer->visibleRegion;
973 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700974 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
975 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 }
977 dirty.subtractSelf(aboveOpaqueLayers);
978
979 // accumulate to the screen dirty region
980 dirtyRegion.orSelf(dirty);
981
Mathias Agopianab028732010-03-16 16:41:46 -0700982 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700984
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 // Store the visible region is screen space
986 layer->setVisibleRegion(visibleRegion);
987 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 }
989
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 opaqueRegion = aboveOpaqueLayers;
991}
992
Mathias Agopian4fec8732012-06-29 14:12:52 -0700993Region SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800995 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700996 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800998 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800999
Mathias Agopian4fec8732012-06-29 14:12:52 -07001000 bool visibleRegions = false;
1001 const size_t count = currentLayers.size();
1002 sp<LayerBase> const* layers = currentLayers.array();
1003 for (size_t i=0 ; i<count ; i++) {
1004 const sp<LayerBase>& layer(layers[i]);
1005 dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
1006 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001007
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001008 mVisibleRegionsDirty |= visibleRegions;
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001009
Mathias Agopian4fec8732012-06-29 14:12:52 -07001010 return dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011}
1012
Mathias Agopianad456f92011-01-13 17:53:01 -08001013void SurfaceFlinger::invalidateHwcGeometry()
1014{
1015 mHwWorkListDirty = true;
1016}
1017
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001018void SurfaceFlinger::handleRefresh()
1019{
1020 bool needInvalidate = false;
1021 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1022 const size_t count = currentLayers.size();
1023 for (size_t i=0 ; i<count ; i++) {
1024 const sp<LayerBase>& layer(currentLayers[i]);
1025 if (layer->onPreComposition()) {
1026 needInvalidate = true;
1027 }
1028 }
1029 if (needInvalidate) {
1030 signalLayerUpdate();
1031 }
1032}
1033
1034
Mathias Agopian1b031492012-06-20 17:51:20 -07001035void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -07001036{
1037 mHwWorkListDirty = false;
Mathias Agopian86303202012-07-24 22:46:10 -07001038 HWComposer& hwc(getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001039 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001040 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001041 const size_t count = currentLayers.size();
1042 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001043
1044 HWComposer::LayerListIterator cur = hwc.begin();
1045 const HWComposer::LayerListIterator end = hwc.end();
1046 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001047 currentLayers[i]->setGeometry(hw, *cur);
Mathias Agopian53331da2011-08-22 21:44:41 -07001048 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001049 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001050 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001051 }
1052 }
1053}
Mathias Agopianb8a55602009-06-26 19:06:36 -07001054
Mathias Agopian1b031492012-06-20 17:51:20 -07001055void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056{
Mathias Agopian841cde52012-03-01 15:44:37 -08001057 ATRACE_CALL();
1058
Mathias Agopianb8a55602009-06-26 19:06:36 -07001059 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -07001060 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061
Glenn Kasten99ed2242011-12-15 09:51:17 -08001062 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -07001063 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 }
1065
Mathias Agopianb8a55602009-06-26 19:06:36 -07001066 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -07001067 glMatrixMode(GL_MODELVIEW);
1068 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069
1070 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001071 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001072 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1073 // takes a rectangle, we must make sure to update that whole
1074 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001075 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001076 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -07001077 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001078 // We need to redraw the rectangle that will be updated
1079 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001080 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001081 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -07001082 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001083 } else {
1084 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -07001086 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 }
1088 }
1089
Mathias Agopian1b031492012-06-20 17:51:20 -07001090 setupHardwareComposer(hw);
1091 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001093 // update the swap region and clear the dirty region
1094 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 mDirtyRegion.clear();
1096}
1097
Mathias Agopian1b031492012-06-20 17:51:20 -07001098void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099{
Mathias Agopian86303202012-07-24 22:46:10 -07001100 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001101 HWComposer::LayerListIterator cur = hwc.begin();
1102 const HWComposer::LayerListIterator end = hwc.end();
1103 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001104 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -07001105 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001106
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001107 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001108 size_t count = layers.size();
1109
Steve Blocke6f43dd2012-01-06 19:20:56 +00001110 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -07001111 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
1112 hwc.getNumLayers(), count);
1113
1114 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -07001115 if (hwc.initCheck() == NO_ERROR) {
1116 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
1117 }
Mathias Agopian45721772010-08-12 15:03:26 -07001118
1119 /*
1120 * update the per-frame h/w composer data for each layer
1121 * and build the transparent region of the FB
1122 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001123 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001124 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001125 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -07001126 }
1127 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001128 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -07001129}
Mathias Agopian45721772010-08-12 15:03:26 -07001130
Mathias Agopian1b031492012-06-20 17:51:20 -07001131void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001132{
Mathias Agopian86303202012-07-24 22:46:10 -07001133 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001134 HWComposer::LayerListIterator cur = hwc.begin();
1135 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001136
1137 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001138 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001139 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -07001140
Mathias Agopianb9494d52012-04-18 02:28:45 -07001141 if (hwc.getLayerCount(HWC_OVERLAY)) {
1142 // when using overlays, we assume a fully transparent framebuffer
1143 // NOTE: we could reduce how much we need to clear, for instance
1144 // remove where there are opaque FB layers. however, on some
1145 // GPUs doing a "clean slate" glClear might be more efficient.
1146 // We'll revisit later if needed.
1147 glClearColor(0, 0, 0, 0);
1148 glClear(GL_COLOR_BUFFER_BIT);
1149 } else {
1150 // screen is already cleared here
1151 if (!mWormholeRegion.isEmpty()) {
1152 // can happen with SurfaceView
1153 drawWormhole();
1154 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001155 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001156
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001157 /*
1158 * and then, render the layers targeted at the framebuffer
1159 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001160
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001161 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001162 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001163 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001164 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001165 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001166 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001167 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001168 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001169 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001170 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001171 // never clear the very first layer since we're
1172 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001173 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001174 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001175 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001176 continue;
1177 }
1178 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001179 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001180 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001181 if (cur != end) {
1182 ++cur;
1183 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001184 }
1185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186}
1187
Mathias Agopian1b031492012-06-20 17:51:20 -07001188void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189{
Mathias Agopian0a917752010-06-14 21:20:00 -07001190 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001191 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001192 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001193 return;
1194 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001195
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001196 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001197 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1198 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001199 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001200 }
1201
Mathias Agopianc492e672011-10-18 14:49:27 -07001202 glDisable(GL_TEXTURE_EXTERNAL_OES);
1203 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205
Mathias Agopian0926f502009-05-04 14:17:04 -07001206 static int toggle = 0;
1207 toggle = 1 - toggle;
1208 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001209 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001210 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001211 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213
Mathias Agopian20f68782009-05-11 00:03:41 -07001214 Region::const_iterator it = mDirtyRegion.begin();
1215 Region::const_iterator const end = mDirtyRegion.end();
1216 while (it != end) {
1217 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001219 { r.left, height - r.top },
1220 { r.left, height - r.bottom },
1221 { r.right, height - r.bottom },
1222 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 };
1224 glVertexPointer(2, GL_FLOAT, 0, vertices);
1225 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1226 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001227
Mathias Agopian0656a682011-09-20 17:22:44 -07001228 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229
1230 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001231 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232}
1233
1234void SurfaceFlinger::drawWormhole() const
1235{
1236 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1237 if (region.isEmpty())
1238 return;
1239
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001240 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001241 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001242 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001243 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001244
1245 GLfloat vertices[4][2];
1246 glVertexPointer(2, GL_FLOAT, 0, vertices);
1247 Region::const_iterator it = region.begin();
1248 Region::const_iterator const end = region.end();
1249 while (it != end) {
1250 const Rect& r = *it++;
1251 vertices[0][0] = r.left;
1252 vertices[0][1] = r.top;
1253 vertices[1][0] = r.right;
1254 vertices[1][1] = r.top;
1255 vertices[2][0] = r.right;
1256 vertices[2][1] = r.bottom;
1257 vertices[3][0] = r.left;
1258 vertices[3][1] = r.bottom;
1259 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1260 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261}
1262
Mathias Agopian96f08192010-06-02 23:28:45 -07001263ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1264 const sp<LayerBaseClient>& lbc)
1265{
Mathias Agopian96f08192010-06-02 23:28:45 -07001266 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001267 size_t name = client->attachLayer(lbc);
1268
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001270 Mutex::Autolock _l(mStateLock);
1271 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001272
Mathias Agopian4f113742011-05-03 16:21:41 -07001273 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001274}
1275
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001276status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277{
1278 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001279 status_t err = purgatorizeLayer_l(layer);
1280 if (err == NO_ERROR)
1281 setTransactionFlags(eTransactionNeeded);
1282 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283}
1284
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001285status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286{
1287 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1288 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001289 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 return NO_ERROR;
1291 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001292 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293}
1294
Mathias Agopian9a112062009-04-17 19:36:26 -07001295status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1296{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001297 // First add the layer to the purgatory list, which makes sure it won't
1298 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001299 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001300 if (err >= 0) {
1301 mLayerPurgatory.add(layerBase);
1302 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001303
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001304 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001305
Mathias Agopian3d579642009-06-04 18:46:21 -07001306 // it's possible that we don't find a layer, because it might
1307 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001308 // from the user because there is a race between Client::destroySurface(),
1309 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001310 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1311}
1312
Mathias Agopiandea20b12011-05-03 17:04:02 -07001313uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1314{
1315 return android_atomic_release_load(&mTransactionFlags);
1316}
1317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1319{
1320 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1321}
1322
Mathias Agopianbb641242010-05-18 17:06:55 -07001323uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324{
1325 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1326 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001327 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 }
1329 return old;
1330}
1331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332
Mathias Agopian8b33f032012-07-24 20:43:54 -07001333void SurfaceFlinger::setTransactionState(
1334 const Vector<ComposerState>& state,
1335 const Vector<DisplayState>& displays,
1336 uint32_t flags)
1337{
Mathias Agopian698c0872011-06-28 19:09:31 -07001338 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001339
Mathias Agopian8b33f032012-07-24 20:43:54 -07001340 int orientation = eOrientationUnchanged;
1341 if (displays.size()) {
1342 // TODO: handle all displays
1343 orientation = displays[0].orientation;
1344 }
1345
Jamie Gennis28378392011-10-12 17:39:00 -07001346 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001347 if (mCurrentState.orientation != orientation) {
1348 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1349 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001350 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001351 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001352 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001353 orientation);
1354 }
1355 }
1356
Mathias Agopian698c0872011-06-28 19:09:31 -07001357 const size_t count = state.size();
1358 for (size_t i=0 ; i<count ; i++) {
1359 const ComposerState& s(state[i]);
1360 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001361 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001362 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001363
Mathias Agopian386aa982011-11-07 21:58:03 -08001364 if (transactionFlags) {
1365 // this triggers the transaction
1366 setTransactionFlags(transactionFlags);
1367
1368 // if this is a synchronous transaction, wait for it to take effect
1369 // before returning.
1370 if (flags & eSynchronous) {
1371 mTransationPending = true;
1372 }
1373 while (mTransationPending) {
1374 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1375 if (CC_UNLIKELY(err != NO_ERROR)) {
1376 // just in case something goes wrong in SF, return to the
1377 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001378 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001379 mTransationPending = false;
1380 break;
1381 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001382 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 }
1384}
1385
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001386sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001387 ISurfaceComposerClient::surface_data_t* params,
1388 const String8& name,
1389 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1391 uint32_t flags)
1392{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001393 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001394 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001395
1396 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001397 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001398 int(w), int(h));
1399 return surfaceHandle;
1400 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001401
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001402 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403 switch (flags & eFXSurfaceMask) {
1404 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001405 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 break;
1407 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001408 // for now we treat Blur as Dim, until we can implement it
1409 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001411 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001413 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001414 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001415 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 }
1417
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001418 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001419 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001420 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001421 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001423 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001424 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001425 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001426 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001427 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 }
1429
1430 return surfaceHandle;
1431}
1432
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001433sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001434 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001435 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001436 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437{
1438 // initialize the surfaces
1439 switch (format) { // TODO: take h/w into account
1440 case PIXEL_FORMAT_TRANSPARENT:
1441 case PIXEL_FORMAT_TRANSLUCENT:
1442 format = PIXEL_FORMAT_RGBA_8888;
1443 break;
1444 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001445#ifdef NO_RGBX_8888
1446 format = PIXEL_FORMAT_RGB_565;
1447#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001448 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001449#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450 break;
1451 }
1452
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001453#ifdef NO_RGBX_8888
1454 if (format == PIXEL_FORMAT_RGBX_8888)
1455 format = PIXEL_FORMAT_RGBA_8888;
1456#endif
1457
Mathias Agopian96f08192010-06-02 23:28:45 -07001458 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001459 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001460 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001461 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001462 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 }
1464 return layer;
1465}
1466
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001467sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001468 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001469 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470{
Mathias Agopian96f08192010-06-02 23:28:45 -07001471 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001472 return layer;
1473}
1474
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001475sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001476 const sp<Client>& client, DisplayID display,
1477 uint32_t w, uint32_t h, uint32_t flags)
1478{
1479 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 return layer;
1481}
1482
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001483status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484{
Mathias Agopian9a112062009-04-17 19:36:26 -07001485 /*
1486 * called by the window manager, when a surface should be marked for
1487 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001488 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001489 * The surface is removed from the current and drawing lists, but placed
1490 * in the purgatory queue, so it's not destroyed right-away (we need
1491 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001492 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001493
Mathias Agopian48d819a2009-09-10 19:41:18 -07001494 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001495 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001496 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001497
Mathias Agopian48d819a2009-09-10 19:41:18 -07001498 if (layer != 0) {
1499 err = purgatorizeLayer_l(layer);
1500 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001501 setTransactionFlags(eTransactionNeeded);
1502 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001503 }
1504 return err;
1505}
1506
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001507status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001508{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001509 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001510 status_t err = NO_ERROR;
1511 sp<LayerBaseClient> l(layer.promote());
1512 if (l != NULL) {
1513 Mutex::Autolock _l(mStateLock);
1514 err = removeLayer_l(l);
1515 if (err == NAME_NOT_FOUND) {
1516 // The surface wasn't in the current list, which means it was
1517 // removed already, which means it is in the purgatory,
1518 // and need to be removed from there.
1519 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001520 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001521 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001522 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001523 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001524 "error removing layer=%p (%s)", l.get(), strerror(-err));
1525 }
1526 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527}
1528
Mathias Agopian698c0872011-06-28 19:09:31 -07001529uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001530 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001531 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001534 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1535 if (layer != 0) {
1536 const uint32_t what = s.what;
1537 if (what & ePositionChanged) {
1538 if (layer->setPosition(s.x, s.y))
1539 flags |= eTraversalNeeded;
1540 }
1541 if (what & eLayerChanged) {
1542 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1543 if (layer->setLayer(s.z)) {
1544 mCurrentState.layersSortedByZ.removeAt(idx);
1545 mCurrentState.layersSortedByZ.add(layer);
1546 // we need traversal (state changed)
1547 // AND transaction (list changed)
1548 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549 }
1550 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001551 if (what & eSizeChanged) {
1552 if (layer->setSize(s.w, s.h)) {
1553 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001554 }
1555 }
1556 if (what & eAlphaChanged) {
1557 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1558 flags |= eTraversalNeeded;
1559 }
1560 if (what & eMatrixChanged) {
1561 if (layer->setMatrix(s.matrix))
1562 flags |= eTraversalNeeded;
1563 }
1564 if (what & eTransparentRegionChanged) {
1565 if (layer->setTransparentRegionHint(s.transparentRegion))
1566 flags |= eTraversalNeeded;
1567 }
1568 if (what & eVisibilityChanged) {
1569 if (layer->setFlags(s.flags, s.mask))
1570 flags |= eTraversalNeeded;
1571 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001572 if (what & eCropChanged) {
1573 if (layer->setCrop(s.crop))
1574 flags |= eTraversalNeeded;
1575 }
Mathias Agopian87855782012-07-24 21:41:09 -07001576 if (what & eLayerStackChanged) {
1577 if (layer->setLayerStack(s.layerStack))
1578 flags |= eTraversalNeeded;
1579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001581 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582}
1583
Mathias Agopianb60314a2012-04-10 22:09:54 -07001584// ---------------------------------------------------------------------------
1585
1586void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001587 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001588 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopian86303202012-07-24 22:46:10 -07001589 getHwComposer().acquire();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001590 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001591 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001592 // this is a temporary work-around, eventually this should be called
1593 // by the power-manager
1594 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001595 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001596 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597}
1598
Mathias Agopianb60314a2012-04-10 22:09:54 -07001599void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001600 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001601 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001602 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001603 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001604 hw.releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001605 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001606 // from this point on, SF will stop drawing
1607 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608}
1609
Colin Cross8e533062012-06-07 13:17:52 -07001610void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001611 class MessageScreenAcquired : public MessageBase {
1612 SurfaceFlinger* flinger;
1613 public:
1614 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1615 virtual bool handler() {
1616 flinger->onScreenAcquired();
1617 return true;
1618 }
1619 };
1620 sp<MessageBase> msg = new MessageScreenAcquired(this);
1621 postMessageSync(msg);
1622}
1623
Colin Cross8e533062012-06-07 13:17:52 -07001624void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001625 class MessageScreenReleased : public MessageBase {
1626 SurfaceFlinger* flinger;
1627 public:
1628 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1629 virtual bool handler() {
1630 flinger->onScreenReleased();
1631 return true;
1632 }
1633 };
1634 sp<MessageBase> msg = new MessageScreenReleased(this);
1635 postMessageSync(msg);
1636}
1637
1638// ---------------------------------------------------------------------------
1639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1641{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001642 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 char buffer[SIZE];
1644 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001645
1646 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 snprintf(buffer, SIZE, "Permission Denial: "
1648 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1649 IPCThreadState::self()->getCallingPid(),
1650 IPCThreadState::self()->getCallingUid());
1651 result.append(buffer);
1652 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001653 // Try to get the main lock, but don't insist if we can't
1654 // (this would indicate SF is stuck, but we want to be able to
1655 // print something in dumpsys).
1656 int retry = 3;
1657 while (mStateLock.tryLock()<0 && --retry>=0) {
1658 usleep(1000000);
1659 }
1660 const bool locked(retry >= 0);
1661 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001662 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001663 "SurfaceFlinger appears to be unresponsive, "
1664 "dumping anyways (no locks held)\n");
1665 result.append(buffer);
1666 }
1667
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001668 bool dumpAll = true;
1669 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001670 size_t numArgs = args.size();
1671 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001672 if ((index < numArgs) &&
1673 (args[index] == String16("--list"))) {
1674 index++;
1675 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001676 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001677 }
1678
1679 if ((index < numArgs) &&
1680 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001681 index++;
1682 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001683 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001684 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001685
1686 if ((index < numArgs) &&
1687 (args[index] == String16("--latency-clear"))) {
1688 index++;
1689 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001690 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001691 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001693
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001694 if (dumpAll) {
1695 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001696 }
1697
Mathias Agopian9795c422009-08-26 16:36:26 -07001698 if (locked) {
1699 mStateLock.unlock();
1700 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 }
1702 write(fd, result.string(), result.size());
1703 return NO_ERROR;
1704}
1705
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001706void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1707 String8& result, char* buffer, size_t SIZE) const
1708{
1709 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1710 const size_t count = currentLayers.size();
1711 for (size_t i=0 ; i<count ; i++) {
1712 const sp<LayerBase>& layer(currentLayers[i]);
1713 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1714 result.append(buffer);
1715 }
1716}
1717
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001718void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1719 String8& result, char* buffer, size_t SIZE) const
1720{
1721 String8 name;
1722 if (index < args.size()) {
1723 name = String8(args[index]);
1724 index++;
1725 }
1726
1727 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1728 const size_t count = currentLayers.size();
1729 for (size_t i=0 ; i<count ; i++) {
1730 const sp<LayerBase>& layer(currentLayers[i]);
1731 if (name.isEmpty()) {
1732 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1733 result.append(buffer);
1734 }
1735 if (name.isEmpty() || (name == layer->getName())) {
1736 layer->dumpStats(result, buffer, SIZE);
1737 }
1738 }
1739}
1740
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001741void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1742 String8& result, char* buffer, size_t SIZE) const
1743{
1744 String8 name;
1745 if (index < args.size()) {
1746 name = String8(args[index]);
1747 index++;
1748 }
1749
1750 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1751 const size_t count = currentLayers.size();
1752 for (size_t i=0 ; i<count ; i++) {
1753 const sp<LayerBase>& layer(currentLayers[i]);
1754 if (name.isEmpty() || (name == layer->getName())) {
1755 layer->clearStats();
1756 }
1757 }
1758}
1759
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001760void SurfaceFlinger::dumpAllLocked(
1761 String8& result, char* buffer, size_t SIZE) const
1762{
1763 // figure out if we're stuck somewhere
1764 const nsecs_t now = systemTime();
1765 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1766 const nsecs_t inTransaction(mDebugInTransaction);
1767 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1768 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1769
1770 /*
1771 * Dump the visible layer list
1772 */
1773 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1774 const size_t count = currentLayers.size();
1775 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1776 result.append(buffer);
1777 for (size_t i=0 ; i<count ; i++) {
1778 const sp<LayerBase>& layer(currentLayers[i]);
1779 layer->dump(result, buffer, SIZE);
1780 }
1781
1782 /*
1783 * Dump the layers in the purgatory
1784 */
1785
1786 const size_t purgatorySize = mLayerPurgatory.size();
1787 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1788 result.append(buffer);
1789 for (size_t i=0 ; i<purgatorySize ; i++) {
1790 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1791 layer->shortDump(result, buffer, SIZE);
1792 }
1793
1794 /*
1795 * Dump SurfaceFlinger global state
1796 */
1797
1798 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1799 result.append(buffer);
1800
Mathias Agopian1b031492012-06-20 17:51:20 -07001801 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001802 const GLExtensions& extensions(GLExtensions::getInstance());
1803 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1804 extensions.getVendor(),
1805 extensions.getRenderer(),
1806 extensions.getVersion());
1807 result.append(buffer);
1808
1809 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001810 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001811 EGL_VERSION_HW_ANDROID));
1812 result.append(buffer);
1813
1814 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1815 result.append(buffer);
1816
1817 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001818 snprintf(buffer, SIZE,
1819 " orientation=%d, canDraw=%d\n",
1820 mCurrentState.orientation, hw.canDraw());
1821 result.append(buffer);
1822 snprintf(buffer, SIZE,
1823 " last eglSwapBuffers() time: %f us\n"
1824 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001825 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001826 " refresh-rate : %f fps\n"
1827 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001828 " y-dpi : %f\n"
1829 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001830 mLastSwapBufferTime/1000.0,
1831 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001832 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001833 hw.getRefreshRate(),
1834 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001835 hw.getDpiY(),
1836 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001837 result.append(buffer);
1838
1839 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1840 inSwapBuffersDuration/1000.0);
1841 result.append(buffer);
1842
1843 snprintf(buffer, SIZE, " transaction time: %f us\n",
1844 inTransactionDuration/1000.0);
1845 result.append(buffer);
1846
1847 /*
1848 * VSYNC state
1849 */
1850 mEventThread->dump(result, buffer, SIZE);
1851
1852 /*
1853 * Dump HWComposer state
1854 */
Mathias Agopian86303202012-07-24 22:46:10 -07001855 HWComposer& hwc(getHwComposer());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001856 snprintf(buffer, SIZE, "h/w composer state:\n");
1857 result.append(buffer);
1858 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1859 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1860 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1861 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001862 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001863
1864 /*
1865 * Dump gralloc state
1866 */
1867 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1868 alloc.dump(result);
1869 hw.dump(result);
1870}
1871
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872status_t SurfaceFlinger::onTransact(
1873 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1874{
1875 switch (code) {
1876 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001877 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001880 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001881 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001882 case BLANK:
1883 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884 {
1885 // codes that require permission check
1886 IPCThreadState* ipc = IPCThreadState::self();
1887 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001888 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001889 if ((uid != AID_GRAPHICS) &&
1890 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001891 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001892 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1893 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001895 break;
1896 }
1897 case CAPTURE_SCREEN:
1898 {
1899 // codes that require permission check
1900 IPCThreadState* ipc = IPCThreadState::self();
1901 const int pid = ipc->getCallingPid();
1902 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001903 if ((uid != AID_GRAPHICS) &&
1904 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001905 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001906 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1907 return PERMISSION_DENIED;
1908 }
1909 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 }
1911 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001912
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1914 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001915 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001916 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001917 IPCThreadState* ipc = IPCThreadState::self();
1918 const int pid = ipc->getCallingPid();
1919 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001920 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001921 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 return PERMISSION_DENIED;
1923 }
1924 int n;
1925 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001926 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001927 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 return NO_ERROR;
1929 case 1002: // SHOW_UPDATES
1930 n = data.readInt32();
1931 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001932 invalidateHwcGeometry();
1933 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001936 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001937 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001939 case 1005:{ // force transaction
1940 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1941 return NO_ERROR;
1942 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001943 case 1006:{ // send empty update
1944 signalRefresh();
1945 return NO_ERROR;
1946 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001947 case 1008: // toggle use of hw composer
1948 n = data.readInt32();
1949 mDebugDisableHWC = n ? 1 : 0;
1950 invalidateHwcGeometry();
1951 repaintEverything();
1952 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001953 case 1009: // toggle use of transform hint
1954 n = data.readInt32();
1955 mDebugDisableTransformHint = n ? 1 : 0;
1956 invalidateHwcGeometry();
1957 repaintEverything();
1958 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001960 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961 reply->writeInt32(0);
1962 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001963 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001964 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965 return NO_ERROR;
1966 case 1013: {
1967 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001968 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001969 reply->writeInt32(hw.getPageFlipCount());
1970 }
1971 return NO_ERROR;
1972 }
1973 }
1974 return err;
1975}
1976
Mathias Agopian53331da2011-08-22 21:44:41 -07001977void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001978 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001979 const Rect bounds(hw.getBounds());
1980 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001981 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001982}
1983
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001984void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1985 Mutex::Autolock _l(mInvalidateLock);
1986 mInvalidateRegion = reg;
1987}
1988
1989Region SurfaceFlinger::getAndClearInvalidateRegion() {
1990 Mutex::Autolock _l(mInvalidateLock);
1991 Region reg(mInvalidateRegion);
1992 mInvalidateRegion.clear();
1993 return reg;
1994}
1995
Mathias Agopian59119e62010-10-11 12:37:43 -07001996// ---------------------------------------------------------------------------
1997
Mathias Agopian118d0242011-10-13 16:02:48 -07001998status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1999 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2000{
2001 Mutex::Autolock _l(mStateLock);
2002 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2003}
2004
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002005status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2006 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002007{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002008 ATRACE_CALL();
2009
Mathias Agopian59119e62010-10-11 12:37:43 -07002010 if (!GLExtensions::getInstance().haveFramebufferObject())
2011 return INVALID_OPERATION;
2012
2013 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002014 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002015 const uint32_t hw_w = hw.getWidth();
2016 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 GLfloat u = 1;
2018 GLfloat v = 1;
2019
2020 // make sure to clear all GL error flags
2021 while ( glGetError() != GL_NO_ERROR ) ;
2022
2023 // create a FBO
2024 GLuint name, tname;
2025 glGenTextures(1, &tname);
2026 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002027 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2028 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002029 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2030 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002031 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002032 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002033 GLint tw = (2 << (31 - clz(hw_w)));
2034 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002035 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2036 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002037 u = GLfloat(hw_w) / tw;
2038 v = GLfloat(hw_h) / th;
2039 }
2040 glGenFramebuffersOES(1, &name);
2041 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002042 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2043 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002044
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002046 glDisable(GL_TEXTURE_EXTERNAL_OES);
2047 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002048 glClearColor(0,0,0,1);
2049 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002050 glMatrixMode(GL_MODELVIEW);
2051 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002052 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053 const size_t count = layers.size();
2054 for (size_t i=0 ; i<count ; ++i) {
2055 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07002056 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 }
2058
Mathias Agopian118d0242011-10-13 16:02:48 -07002059 hw.compositionComplete();
2060
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002061 // back to main framebuffer
2062 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002063 glDeleteFramebuffersOES(1, &name);
2064
2065 *textureName = tname;
2066 *uOut = u;
2067 *vOut = v;
2068 return NO_ERROR;
2069}
2070
2071// ---------------------------------------------------------------------------
2072
Mathias Agopianed9807b2012-05-18 14:18:08 -07002073class VSyncWaiter {
2074 DisplayEventReceiver::Event buffer[4];
2075 sp<Looper> looper;
2076 sp<IDisplayEventConnection> events;
2077 sp<BitTube> eventTube;
2078public:
2079 VSyncWaiter(const sp<EventThread>& eventThread) {
2080 looper = new Looper(true);
2081 events = eventThread->createEventConnection();
2082 eventTube = events->getDataChannel();
2083 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
2084 events->requestNextVsync();
2085 }
2086
2087 void wait() {
2088 ssize_t n;
2089
2090 looper->pollOnce(-1);
2091 // we don't handle any errors here, it doesn't matter
2092 // and we don't want to take the risk to get stuck.
2093
2094 // drain the events...
2095 while ((n = DisplayEventReceiver::getEvents(
2096 eventTube, buffer, 4)) > 0) ;
2097
2098 events->requestNextVsync();
2099 }
2100};
2101
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002102status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
2103{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002104 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002105 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 const uint32_t hw_w = hw.getWidth();
2107 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07002108 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002109
2110 GLfloat u, v;
2111 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002112 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002113 if (result != NO_ERROR) {
2114 return result;
2115 }
2116
2117 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002118 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002119 glBindTexture(GL_TEXTURE_2D, tname);
2120 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2121 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2122 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002123 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2124 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002125 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2126 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2127 glVertexPointer(2, GL_FLOAT, 0, vtx);
2128
Mathias Agopianffcf4652011-07-07 17:30:31 -07002129 /*
2130 * Texture coordinate mapping
2131 *
2132 * u
2133 * 1 +----------+---+
2134 * | | | | image is inverted
2135 * | V | | w.r.t. the texture
2136 * 1-v +----------+ | coordinates
2137 * | |
2138 * | |
2139 * | |
2140 * 0 +--------------+
2141 * 0 1
2142 *
2143 */
2144
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002145 class s_curve_interpolator {
2146 const float nbFrames, s, v;
2147 public:
2148 s_curve_interpolator(int nbFrames, float s)
2149 : nbFrames(1.0f / (nbFrames-1)), s(s),
2150 v(1.0f + expf(-s + 0.5f*s)) {
2151 }
2152 float operator()(int f) {
2153 const float x = f * nbFrames;
2154 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2155 }
2156 };
2157
2158 class v_stretch {
2159 const GLfloat hw_w, hw_h;
2160 public:
2161 v_stretch(uint32_t hw_w, uint32_t hw_h)
2162 : hw_w(hw_w), hw_h(hw_h) {
2163 }
2164 void operator()(GLfloat* vtx, float v) {
2165 const GLfloat w = hw_w + (hw_w * v);
2166 const GLfloat h = hw_h - (hw_h * v);
2167 const GLfloat x = (hw_w - w) * 0.5f;
2168 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002169 vtx[0] = x; vtx[1] = y;
2170 vtx[2] = x; vtx[3] = y + h;
2171 vtx[4] = x + w; vtx[5] = y + h;
2172 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002173 }
2174 };
2175
2176 class h_stretch {
2177 const GLfloat hw_w, hw_h;
2178 public:
2179 h_stretch(uint32_t hw_w, uint32_t hw_h)
2180 : hw_w(hw_w), hw_h(hw_h) {
2181 }
2182 void operator()(GLfloat* vtx, float v) {
2183 const GLfloat w = hw_w - (hw_w * v);
2184 const GLfloat h = 1.0f;
2185 const GLfloat x = (hw_w - w) * 0.5f;
2186 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002187 vtx[0] = x; vtx[1] = y;
2188 vtx[2] = x; vtx[3] = y + h;
2189 vtx[4] = x + w; vtx[5] = y + h;
2190 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002191 }
2192 };
2193
Mathias Agopianed9807b2012-05-18 14:18:08 -07002194 VSyncWaiter vsync(mEventThread);
2195
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002196 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002197 char value[PROPERTY_VALUE_MAX];
2198 property_get("debug.sf.electron_frames", value, "24");
2199 int nbFrames = (atoi(value) + 1) >> 1;
2200 if (nbFrames <= 0) // just in case
2201 nbFrames = 24;
2202
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 s_curve_interpolator itr(nbFrames, 7.5f);
2204 s_curve_interpolator itg(nbFrames, 8.0f);
2205 s_curve_interpolator itb(nbFrames, 8.5f);
2206
2207 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002208
2209 glMatrixMode(GL_TEXTURE);
2210 glLoadIdentity();
2211 glMatrixMode(GL_MODELVIEW);
2212 glLoadIdentity();
2213
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002214 glEnable(GL_BLEND);
2215 glBlendFunc(GL_ONE, GL_ONE);
2216 for (int i=0 ; i<nbFrames ; i++) {
2217 float x, y, w, h;
2218 const float vr = itr(i);
2219 const float vg = itg(i);
2220 const float vb = itb(i);
2221
Mathias Agopianed9807b2012-05-18 14:18:08 -07002222 // wait for vsync
2223 vsync.wait();
2224
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002225 // clear screen
2226 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002227 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002228 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002229
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002230 // draw the red plane
2231 vverts(vtx, vr);
2232 glColorMask(1,0,0,1);
2233 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002234
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002235 // draw the green plane
2236 vverts(vtx, vg);
2237 glColorMask(0,1,0,1);
2238 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002239
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002240 // draw the blue plane
2241 vverts(vtx, vb);
2242 glColorMask(0,0,1,1);
2243 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002244
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002245 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002246 glDisable(GL_TEXTURE_2D);
2247 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002248 glColor4f(vg, vg, vg, 1);
2249 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2250 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002251 }
2252
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002253 h_stretch hverts(hw_w, hw_h);
2254 glDisable(GL_BLEND);
2255 glDisable(GL_TEXTURE_2D);
2256 glColorMask(1,1,1,1);
2257 for (int i=0 ; i<nbFrames ; i++) {
2258 const float v = itg(i);
2259 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002260
2261 // wait for vsync
2262 vsync.wait();
2263
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264 glClear(GL_COLOR_BUFFER_BIT);
2265 glColor4f(1-v, 1-v, 1-v, 1);
2266 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2267 hw.flip(screenBounds);
2268 }
2269
2270 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002271 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2272 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002273 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002274 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002275 return NO_ERROR;
2276}
2277
2278status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2279{
2280 status_t result = PERMISSION_DENIED;
2281
2282 if (!GLExtensions::getInstance().haveFramebufferObject())
2283 return INVALID_OPERATION;
2284
2285
2286 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002287 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002288 const uint32_t hw_w = hw.getWidth();
2289 const uint32_t hw_h = hw.getHeight();
2290 const Region screenBounds(hw.bounds());
2291
2292 GLfloat u, v;
2293 GLuint tname;
2294 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2295 if (result != NO_ERROR) {
2296 return result;
2297 }
2298
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002299 GLfloat vtx[8];
2300 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 glBindTexture(GL_TEXTURE_2D, tname);
2302 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2303 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2304 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002305 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2306 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002307 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2308 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2309 glVertexPointer(2, GL_FLOAT, 0, vtx);
2310
2311 class s_curve_interpolator {
2312 const float nbFrames, s, v;
2313 public:
2314 s_curve_interpolator(int nbFrames, float s)
2315 : nbFrames(1.0f / (nbFrames-1)), s(s),
2316 v(1.0f + expf(-s + 0.5f*s)) {
2317 }
2318 float operator()(int f) {
2319 const float x = f * nbFrames;
2320 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2321 }
2322 };
2323
2324 class v_stretch {
2325 const GLfloat hw_w, hw_h;
2326 public:
2327 v_stretch(uint32_t hw_w, uint32_t hw_h)
2328 : hw_w(hw_w), hw_h(hw_h) {
2329 }
2330 void operator()(GLfloat* vtx, float v) {
2331 const GLfloat w = hw_w + (hw_w * v);
2332 const GLfloat h = hw_h - (hw_h * v);
2333 const GLfloat x = (hw_w - w) * 0.5f;
2334 const GLfloat y = (hw_h - h) * 0.5f;
2335 vtx[0] = x; vtx[1] = y;
2336 vtx[2] = x; vtx[3] = y + h;
2337 vtx[4] = x + w; vtx[5] = y + h;
2338 vtx[6] = x + w; vtx[7] = y;
2339 }
2340 };
2341
2342 class h_stretch {
2343 const GLfloat hw_w, hw_h;
2344 public:
2345 h_stretch(uint32_t hw_w, uint32_t hw_h)
2346 : hw_w(hw_w), hw_h(hw_h) {
2347 }
2348 void operator()(GLfloat* vtx, float v) {
2349 const GLfloat w = hw_w - (hw_w * v);
2350 const GLfloat h = 1.0f;
2351 const GLfloat x = (hw_w - w) * 0.5f;
2352 const GLfloat y = (hw_h - h) * 0.5f;
2353 vtx[0] = x; vtx[1] = y;
2354 vtx[2] = x; vtx[3] = y + h;
2355 vtx[4] = x + w; vtx[5] = y + h;
2356 vtx[6] = x + w; vtx[7] = y;
2357 }
2358 };
2359
Mathias Agopianed9807b2012-05-18 14:18:08 -07002360 VSyncWaiter vsync(mEventThread);
2361
Mathias Agopiana6546e52010-10-14 12:33:07 -07002362 // the full animation is 12 frames
2363 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002364 s_curve_interpolator itr(nbFrames, 7.5f);
2365 s_curve_interpolator itg(nbFrames, 8.0f);
2366 s_curve_interpolator itb(nbFrames, 8.5f);
2367
2368 h_stretch hverts(hw_w, hw_h);
2369 glDisable(GL_BLEND);
2370 glDisable(GL_TEXTURE_2D);
2371 glColorMask(1,1,1,1);
2372 for (int i=nbFrames-1 ; i>=0 ; i--) {
2373 const float v = itg(i);
2374 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002375
2376 // wait for vsync
2377 vsync.wait();
2378
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002379 glClear(GL_COLOR_BUFFER_BIT);
2380 glColor4f(1-v, 1-v, 1-v, 1);
2381 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2382 hw.flip(screenBounds);
2383 }
2384
Mathias Agopiana6546e52010-10-14 12:33:07 -07002385 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002386 v_stretch vverts(hw_w, hw_h);
2387 glEnable(GL_BLEND);
2388 glBlendFunc(GL_ONE, GL_ONE);
2389 for (int i=nbFrames-1 ; i>=0 ; i--) {
2390 float x, y, w, h;
2391 const float vr = itr(i);
2392 const float vg = itg(i);
2393 const float vb = itb(i);
2394
Mathias Agopianed9807b2012-05-18 14:18:08 -07002395 // wait for vsync
2396 vsync.wait();
2397
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002398 // clear screen
2399 glColorMask(1,1,1,1);
2400 glClear(GL_COLOR_BUFFER_BIT);
2401 glEnable(GL_TEXTURE_2D);
2402
2403 // draw the red plane
2404 vverts(vtx, vr);
2405 glColorMask(1,0,0,1);
2406 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2407
2408 // draw the green plane
2409 vverts(vtx, vg);
2410 glColorMask(0,1,0,1);
2411 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2412
2413 // draw the blue plane
2414 vverts(vtx, vb);
2415 glColorMask(0,0,1,1);
2416 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2417
2418 hw.flip(screenBounds);
2419 }
2420
2421 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002422 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002423 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002424 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002425 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002426
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002427 return NO_ERROR;
2428}
2429
2430// ---------------------------------------------------------------------------
2431
Mathias Agopianabd671a2010-10-14 14:54:06 -07002432status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002433{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002434 ATRACE_CALL();
2435
Mathias Agopian1b031492012-06-20 17:51:20 -07002436 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002437 if (!hw.canDraw()) {
2438 // we're already off
2439 return NO_ERROR;
2440 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002441
2442 // turn off hwc while we're doing the animation
Mathias Agopian86303202012-07-24 22:46:10 -07002443 getHwComposer().disable();
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002444 // and make sure to turn it back on (if needed) next time we compose
2445 invalidateHwcGeometry();
2446
Mathias Agopianabd671a2010-10-14 14:54:06 -07002447 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2448 electronBeamOffAnimationImplLocked();
2449 }
2450
2451 // always clear the whole screen at the end of the animation
2452 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002453 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002454 hw.flip( Region(hw.bounds()) );
2455
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002456 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002457}
2458
2459status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2460{
Mathias Agopian59119e62010-10-11 12:37:43 -07002461 class MessageTurnElectronBeamOff : public MessageBase {
2462 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002463 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002464 status_t result;
2465 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002466 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2467 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002468 }
2469 status_t getResult() const {
2470 return result;
2471 }
2472 virtual bool handler() {
2473 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002474 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002475 return true;
2476 }
2477 };
2478
Mathias Agopianabd671a2010-10-14 14:54:06 -07002479 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002480 status_t res = postMessageSync(msg);
2481 if (res == NO_ERROR) {
2482 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002483
2484 // work-around: when the power-manager calls us we activate the
2485 // animation. eventually, the "on" animation will be called
2486 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002487 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002488 }
2489 return res;
2490}
2491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002493
Mathias Agopianabd671a2010-10-14 14:54:06 -07002494status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002495{
Mathias Agopian1b031492012-06-20 17:51:20 -07002496 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002497 if (hw.canDraw()) {
2498 // we're already on
2499 return NO_ERROR;
2500 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002501 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2502 electronBeamOnAnimationImplLocked();
2503 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002504
2505 // make sure to redraw the whole screen when the animation is done
2506 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002507 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002508
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002509 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002510}
2511
2512status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2513{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002514 class MessageTurnElectronBeamOn : public MessageBase {
2515 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002516 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002517 status_t result;
2518 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002519 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2520 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002521 }
2522 status_t getResult() const {
2523 return result;
2524 }
2525 virtual bool handler() {
2526 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002527 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002528 return true;
2529 }
2530 };
2531
Mathias Agopianabd671a2010-10-14 14:54:06 -07002532 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002533 return NO_ERROR;
2534}
2535
2536// ---------------------------------------------------------------------------
2537
Mathias Agopian74c40c02010-09-29 13:02:36 -07002538status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2539 sp<IMemoryHeap>* heap,
2540 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002541 uint32_t sw, uint32_t sh,
2542 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002543{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002544 ATRACE_CALL();
2545
Mathias Agopian74c40c02010-09-29 13:02:36 -07002546 status_t result = PERMISSION_DENIED;
2547
2548 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002549 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002550 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002551 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002552
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002553 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002554 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002555 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002556
2557 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002558 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002559 const uint32_t hw_w = hw.getWidth();
2560 const uint32_t hw_h = hw.getHeight();
2561
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002562 // if we have secure windows on this display, never allow the screen capture
2563 if (hw.getSecureLayerVisible()) {
2564 return PERMISSION_DENIED;
2565 }
2566
2567 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002568 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002569 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002570
2571 sw = (!sw) ? hw_w : sw;
2572 sh = (!sh) ? hw_h : sh;
2573 const size_t size = sw * sh * 4;
2574
Steve Block9d453682011-12-20 16:23:08 +00002575 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002576 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002577
Mathias Agopian74c40c02010-09-29 13:02:36 -07002578 // make sure to clear all GL error flags
2579 while ( glGetError() != GL_NO_ERROR ) ;
2580
2581 // create a FBO
2582 GLuint name, tname;
2583 glGenRenderbuffersOES(1, &tname);
2584 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2585 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002586
Mathias Agopian74c40c02010-09-29 13:02:36 -07002587 glGenFramebuffersOES(1, &name);
2588 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2589 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2590 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2591
2592 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002593
Mathias Agopian74c40c02010-09-29 13:02:36 -07002594 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2595
2596 // invert everything, b/c glReadPixel() below will invert the FB
2597 glViewport(0, 0, sw, sh);
2598 glMatrixMode(GL_PROJECTION);
2599 glPushMatrix();
2600 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002601 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002602 glMatrixMode(GL_MODELVIEW);
2603
2604 // redraw the screen entirely...
2605 glClearColor(0,0,0,1);
2606 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002607
Jamie Gennis9575f602011-10-07 14:51:16 -07002608 const LayerVector& layers(mDrawingState.layersSortedByZ);
2609 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002610 for (size_t i=0 ; i<count ; ++i) {
2611 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002612 const uint32_t flags = layer->drawingState().flags;
2613 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2614 const uint32_t z = layer->drawingState().z;
2615 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002616 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002617 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002618 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002619 }
2620
Mathias Agopian74c40c02010-09-29 13:02:36 -07002621 // check for errors and return screen capture
2622 if (glGetError() != GL_NO_ERROR) {
2623 // error while rendering
2624 result = INVALID_OPERATION;
2625 } else {
2626 // allocate shared memory large enough to hold the
2627 // screen capture
2628 sp<MemoryHeapBase> base(
2629 new MemoryHeapBase(size, 0, "screen-capture") );
2630 void* const ptr = base->getBase();
2631 if (ptr) {
2632 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002633 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002634 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2635 if (glGetError() == GL_NO_ERROR) {
2636 *heap = base;
2637 *w = sw;
2638 *h = sh;
2639 *f = PIXEL_FORMAT_RGBA_8888;
2640 result = NO_ERROR;
2641 }
2642 } else {
2643 result = NO_MEMORY;
2644 }
2645 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002646 glViewport(0, 0, hw_w, hw_h);
2647 glMatrixMode(GL_PROJECTION);
2648 glPopMatrix();
2649 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002650 } else {
2651 result = BAD_VALUE;
2652 }
2653
2654 // release FBO resources
2655 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2656 glDeleteRenderbuffersOES(1, &tname);
2657 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002658
2659 hw.compositionComplete();
2660
Steve Block9d453682011-12-20 16:23:08 +00002661 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002662
Mathias Agopian74c40c02010-09-29 13:02:36 -07002663 return result;
2664}
2665
2666
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002667status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2668 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002669 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002670 uint32_t sw, uint32_t sh,
2671 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002672{
2673 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002674 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002675 return BAD_VALUE;
2676
2677 if (!GLExtensions::getInstance().haveFramebufferObject())
2678 return INVALID_OPERATION;
2679
2680 class MessageCaptureScreen : public MessageBase {
2681 SurfaceFlinger* flinger;
2682 DisplayID dpy;
2683 sp<IMemoryHeap>* heap;
2684 uint32_t* w;
2685 uint32_t* h;
2686 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002687 uint32_t sw;
2688 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002689 uint32_t minLayerZ;
2690 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002691 status_t result;
2692 public:
2693 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002694 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002695 uint32_t sw, uint32_t sh,
2696 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002697 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002698 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2699 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2700 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002701 {
2702 }
2703 status_t getResult() const {
2704 return result;
2705 }
2706 virtual bool handler() {
2707 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002708 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002709 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002710 return true;
2711 }
2712 };
2713
2714 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002715 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002716 status_t res = postMessageSync(msg);
2717 if (res == NO_ERROR) {
2718 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2719 }
2720 return res;
2721}
2722
2723// ---------------------------------------------------------------------------
2724
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002725SurfaceFlinger::LayerVector::LayerVector() {
2726}
2727
2728SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2729 : SortedVector<sp<LayerBase> >(rhs) {
2730}
2731
2732int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2733 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002734{
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002735 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2736 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
2737 // sort layers by Z order
2738 uint32_t lz = l->currentState().z;
2739 uint32_t rz = r->currentState().z;
2740 // then by sequence, so we get a stable ordering
2741 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
2742}
2743
2744// ---------------------------------------------------------------------------
2745
2746SurfaceFlinger::State::State()
2747 : orientation(ISurfaceComposer::eOrientationDefault),
2748 orientationFlags(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002749}
2750
2751// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002752
Jamie Gennis9a78c902011-01-12 18:30:40 -08002753GraphicBufferAlloc::GraphicBufferAlloc() {}
2754
2755GraphicBufferAlloc::~GraphicBufferAlloc() {}
2756
2757sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002758 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002759 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2760 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002761 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002762 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002763 if (err == NO_MEMORY) {
2764 GraphicBuffer::dumpAllocationsToSystemLog();
2765 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002766 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002767 "failed (%s), handle=%p",
2768 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002769 return 0;
2770 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002771 return graphicBuffer;
2772}
2773
Jamie Gennis9a78c902011-01-12 18:30:40 -08002774// ---------------------------------------------------------------------------
2775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002776}; // namespace android