blob: 192378f3e4c3cc35eb00734abcb15854eae9846d [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
366 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
367 eglInitialize(display, NULL, NULL);
368
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);
381 mEGLConfig = selectEGLConfig(display, format);
382 mEGLContext = createGLContext(display, mEGLConfig);
383
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();
390 initializeGL(display, 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());
400 if (mHwc->initCheck() == NO_ERROR) {
401 mHwc->setFrameBuffer(display, surface);
402 }
403
Mathias Agopiana4912602012-07-12 14:25:33 -0700404 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800405 mReadyToRunBarrier.open();
406
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700407 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700408 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 return NO_ERROR;
411}
412
Mathias Agopiana67e4182012-06-19 17:26:12 -0700413void SurfaceFlinger::startBootAnim() {
414 // start boot animation
415 property_set("service.bootanim.exit", "0");
416 property_set("ctl.start", "bootanim");
417}
418
Mathias Agopiana4912602012-07-12 14:25:33 -0700419uint32_t SurfaceFlinger::getMaxTextureSize() const {
420 return mMaxTextureSize;
421}
422
423uint32_t SurfaceFlinger::getMaxViewportDims() const {
424 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
425 mMaxViewportDims[0] : mMaxViewportDims[1];
426}
427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800429
Jamie Gennis582270d2011-08-17 18:19:00 -0700430bool SurfaceFlinger::authenticateSurfaceTexture(
431 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800432 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700433 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800434
435 // Check the visible layer list for the ISurface
436 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
437 size_t count = currentLayers.size();
438 for (size_t i=0 ; i<count ; i++) {
439 const sp<LayerBase>& layer(currentLayers[i]);
440 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700441 if (lbc != NULL) {
442 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
443 if (lbcBinder == surfaceTextureBinder) {
444 return true;
445 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800446 }
447 }
448
449 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700450 // SurfaceTexture gets destroyed before all the clients are done using it,
451 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800452 // will instead fail later on when the client tries to use the surface,
453 // which should be reported as "surface XYZ returned an -ENODEV". The
454 // purgatorized layers are no less authentic than the visible ones, so this
455 // should not cause any harm.
456 size_t purgatorySize = mLayerPurgatory.size();
457 for (size_t i=0 ; i<purgatorySize ; i++) {
458 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
459 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700460 if (lbc != NULL) {
461 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
462 if (lbcBinder == surfaceTextureBinder) {
463 return true;
464 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800465 }
466 }
467
468 return false;
469}
470
Mathias Agopianc666cae2012-07-25 18:56:13 -0700471status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
472 if (uint32_t(dpy) >= 2) {
473 return BAD_INDEX;
474 }
475 const DisplayHardware& hw(getDefaultDisplayHardware());
476 return hw.getInfo(info);
477}
478
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800479// ----------------------------------------------------------------------------
480
481sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800482 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700483}
484
Mathias Agopian3094df32012-06-18 18:06:45 -0700485void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700486 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700487 EGLSurface result = EGL_NO_SURFACE;
488 EGLSurface old_surface = EGL_NO_SURFACE;
489 sp<SurfaceTextureClient> stc;
490
491 if (display != NULL) {
492 stc = new SurfaceTextureClient(display);
493 result = eglCreateWindowSurface(hw.getEGLDisplay(),
Mathias Agopiana4912602012-07-12 14:25:33 -0700494 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700495 ALOGE_IF(result == EGL_NO_SURFACE,
496 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
497 display.get());
498 }
499
500 { // scope for the lock
501 Mutex::Autolock _l(mStateLock);
502 old_surface = mExternalDisplaySurface;
503 mExternalDisplayNativeWindow = stc;
504 mExternalDisplaySurface = result;
505 ALOGD("mExternalDisplaySurface = %p", result);
506 }
507
508 if (old_surface != EGL_NO_SURFACE) {
509 // Note: EGL allows to destroy an object while its current
510 // it will fail to become current next time though.
511 eglDestroySurface(hw.getEGLDisplay(), old_surface);
512 }
513}
514
515EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
516 Mutex::Autolock _l(mStateLock);
517 return mExternalDisplaySurface;
518}
519
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800521
522void SurfaceFlinger::waitForEvent() {
523 mEventQueue.waitMessage();
524}
525
526void SurfaceFlinger::signalTransaction() {
527 mEventQueue.invalidate();
528}
529
530void SurfaceFlinger::signalLayerUpdate() {
531 mEventQueue.invalidate();
532}
533
534void SurfaceFlinger::signalRefresh() {
535 mEventQueue.refresh();
536}
537
538status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
539 nsecs_t reltime, uint32_t flags) {
540 return mEventQueue.postMessage(msg, reltime);
541}
542
543status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
544 nsecs_t reltime, uint32_t flags) {
545 status_t res = mEventQueue.postMessage(msg, reltime);
546 if (res == NO_ERROR) {
547 msg->wait();
548 }
549 return res;
550}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551
Mathias Agopian4fec8732012-06-29 14:12:52 -0700552bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 return true;
555}
556
Mathias Agopian86303202012-07-24 22:46:10 -0700557void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
558 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
559 hw.onVSyncReceived(timestamp);
560 mEventThread->onVSyncReceived(dpy, timestamp);
561}
562
563void SurfaceFlinger::eventControl(int event, int enabled) {
564 getHwComposer().eventControl(event, enabled);
565}
566
Mathias Agopian4fec8732012-06-29 14:12:52 -0700567void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800568 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800569 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700570 case MessageQueue::INVALIDATE:
571 handleMessageTransaction();
572 handleMessageInvalidate();
573 signalRefresh();
574 break;
575 case MessageQueue::REFRESH:
576 handleMessageRefresh();
577 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800578 }
579}
580
Mathias Agopian4fec8732012-06-29 14:12:52 -0700581void SurfaceFlinger::handleMessageTransaction() {
582 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
583 uint32_t transactionFlags = peekTransactionFlags(mask);
584 if (transactionFlags) {
585 Region dirtyRegion;
586 dirtyRegion = handleTransaction(transactionFlags);
587 // XXX: dirtyRegion should be per screen
588 mDirtyRegion |= dirtyRegion;
589 }
590}
591
592void SurfaceFlinger::handleMessageInvalidate() {
593 Region dirtyRegion;
594 dirtyRegion = handlePageFlip();
595 // XXX: dirtyRegion should be per screen
596 mDirtyRegion |= dirtyRegion;
597}
598
599void SurfaceFlinger::handleMessageRefresh() {
600 handleRefresh();
601
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700602 if (mVisibleRegionsDirty) {
603 Region opaqueRegion;
604 Region dirtyRegion;
605 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
606 computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
607 mDirtyRegion.orSelf(dirtyRegion);
608
609 /*
610 * rebuild the visible layer list per screen
611 */
612
613 // TODO: iterate through all displays
614 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
615
616 Vector< sp<LayerBase> > layersSortedByZ;
617 const size_t count = currentLayers.size();
618 for (size_t i=0 ; i<count ; i++) {
619 if (!currentLayers[i]->visibleRegion.isEmpty()) {
620 // TODO: also check that this layer is associated to this display
621 layersSortedByZ.add(currentLayers[i]);
622 }
623 }
624 hw.setVisibleLayersSortedByZ(layersSortedByZ);
625
626
627 // FIXME: mWormholeRegion needs to be calculated per screen
628 //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
629 mWormholeRegion = Region(hw.getBounds()).subtract(
630 hw.getTransform().transform(opaqueRegion) );
631 mVisibleRegionsDirty = false;
632 invalidateHwcGeometry();
633 }
634
635
Mathias Agopian4fec8732012-06-29 14:12:52 -0700636 // XXX: dirtyRegion should be per screen, we should check all of them
637 if (mDirtyRegion.isEmpty()) {
638 return;
639 }
640
641 // TODO: iterate through all displays
642 const DisplayHardware& hw(getDisplayHardware(0));
643
644 // XXX: dirtyRegion should be per screen
645 // transform the dirty region into this screen's coordinate space
646 const Transform& planeTransform(hw.getTransform());
647 mDirtyRegion = planeTransform.transform(mDirtyRegion);
648 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
649 mDirtyRegion.andSelf(hw.bounds());
650
651
652 if (CC_UNLIKELY(mHwWorkListDirty)) {
653 // build the h/w work list
654 handleWorkList(hw);
655 }
656
657 if (CC_LIKELY(hw.canDraw())) {
658 // repaint the framebuffer (if needed)
659 handleRepaint(hw);
660 // inform the h/w that we're done compositing
661 hw.compositionComplete();
662 postFramebuffer();
663 } else {
664 // pretend we did the post
665 hw.compositionComplete();
666 }
667
668 // render to the external display if we have one
669 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
670 if (externalDisplaySurface != EGL_NO_SURFACE) {
671 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
672 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
673 externalDisplaySurface, externalDisplaySurface,
674 eglGetCurrentContext());
675
676 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
677
678 if (success) {
679 // redraw the screen entirely...
680 glDisable(GL_TEXTURE_EXTERNAL_OES);
681 glDisable(GL_TEXTURE_2D);
682 glClearColor(0,0,0,1);
683 glClear(GL_COLOR_BUFFER_BIT);
684 glMatrixMode(GL_MODELVIEW);
685 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700686
687 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700688 const size_t count = layers.size();
689 for (size_t i=0 ; i<count ; ++i) {
690 const sp<LayerBase>& layer(layers[i]);
691 layer->drawForSreenShot(hw);
692 }
693
694 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
695 ALOGE_IF(!success, "external display eglSwapBuffers failed");
696
697 hw.compositionComplete();
698 }
699
700 success = eglMakeCurrent(eglGetCurrentDisplay(),
701 cur, cur, eglGetCurrentContext());
702
703 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
704 }
705
706}
707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708void SurfaceFlinger::postFramebuffer()
709{
Mathias Agopian841cde52012-03-01 15:44:37 -0800710 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800711 // mSwapRegion can be empty here is some cases, for instance if a hidden
712 // or fully transparent window is updating.
713 // in that case, we need to flip anyways to not risk a deadlock with
714 // h/w composer.
715
Mathias Agopian1b031492012-06-20 17:51:20 -0700716 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian86303202012-07-24 22:46:10 -0700717 HWComposer& hwc(getHwComposer());
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700718 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
719 size_t numLayers = layers.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700720 const nsecs_t now = systemTime();
721 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700722
723 if (hwc.initCheck() == NO_ERROR) {
724 HWComposer::LayerListIterator cur = hwc.begin();
725 const HWComposer::LayerListIterator end = hwc.end();
726 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
727 if (cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700728 layers[i]->setAcquireFence(*cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700729 } else {
730 cur->setAcquireFenceFd(-1);
731 }
732 }
733 }
734
Mathias Agopiana44b0412011-10-16 18:46:35 -0700735 hw.flip(mSwapRegion);
Mathias Agopian86303202012-07-24 22:46:10 -0700736 hwc.commit();
Jamie Gennise8696a42012-01-15 18:54:57 -0800737
Jesse Hallef194142012-06-14 14:45:17 -0700738 if (hwc.initCheck() == NO_ERROR) {
739 HWComposer::LayerListIterator cur = hwc.begin();
740 const HWComposer::LayerListIterator end = hwc.end();
741 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700742 layers[i]->onLayerDisplayed(&*cur);
Jesse Hallef194142012-06-14 14:45:17 -0700743 }
744 } else {
745 for (size_t i = 0; i < numLayers; i++) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700746 layers[i]->onLayerDisplayed(NULL);
Jesse Hallef194142012-06-14 14:45:17 -0700747 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800748 }
749
Mathias Agopiana44b0412011-10-16 18:46:35 -0700750 mLastSwapBufferTime = systemTime() - now;
751 mDebugInSwapBuffers = 0;
752 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753}
754
Mathias Agopian4fec8732012-06-29 14:12:52 -0700755Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756{
Mathias Agopian841cde52012-03-01 15:44:37 -0800757 ATRACE_CALL();
758
Mathias Agopian4fec8732012-06-29 14:12:52 -0700759 Region dirtyRegion;
760
Mathias Agopianca4d3602011-05-19 15:38:14 -0700761 Mutex::Autolock _l(mStateLock);
762 const nsecs_t now = systemTime();
763 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764
Mathias Agopianca4d3602011-05-19 15:38:14 -0700765 // Here we're guaranteed that some transaction flags are set
766 // so we can call handleTransactionLocked() unconditionally.
767 // We call getTransactionFlags(), which will also clear the flags,
768 // with mStateLock held to guarantee that mCurrentState won't change
769 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700770
Mathias Agopianca4d3602011-05-19 15:38:14 -0700771 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
772 transactionFlags = getTransactionFlags(mask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700773 dirtyRegion = handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700774
Mathias Agopianca4d3602011-05-19 15:38:14 -0700775 mLastTransactionTime = systemTime() - now;
776 mDebugInTransaction = 0;
777 invalidateHwcGeometry();
778 // here the transaction has been committed
Mathias Agopian4fec8732012-06-29 14:12:52 -0700779
780 return dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700781}
782
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700784{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700785 Region dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700786 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 const size_t count = currentLayers.size();
788
789 /*
790 * Traversal of the children
791 * (perform the transaction for each of them if needed)
792 */
793
794 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
795 if (layersNeedTransaction) {
796 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700797 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
799 if (!trFlags) continue;
800
801 const uint32_t flags = layer->doTransaction(0);
802 if (flags & Layer::eVisibleRegion)
803 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 }
805 }
806
807 /*
808 * Perform our own transaction if needed
809 */
810
811 if (transactionFlags & eTransactionNeeded) {
812 if (mCurrentState.orientation != mDrawingState.orientation) {
813 // the orientation has changed, recompute all visible regions
814 // and invalidate everything.
815
Mathias Agopian1b031492012-06-20 17:51:20 -0700816 const int dpy = 0; // TODO: should be a parameter
817 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
Mathias Agopian98a121a2012-07-24 21:08:59 -0700818 hw.setOrientation(mCurrentState.orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819
Mathias Agopian1b031492012-06-20 17:51:20 -0700820 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 mVisibleRegionsDirty = true;
822 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 }
824
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700825 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
826 // layers have been added
827 mVisibleRegionsDirty = true;
828 }
829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 // some layers might have been removed, so
831 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700832 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700833 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700835 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700836 const size_t count = previousLayers.size();
837 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700838 const sp<LayerBase>& layer(previousLayers[i]);
839 if (currentLayers.indexOf( layer ) < 0) {
840 // this layer is not visible anymore
Mathias Agopian4fec8732012-06-29 14:12:52 -0700841 // TODO: we could traverse the tree from front to back and compute the actual visible region
842 // TODO: we could cache the transformed region
843 Layer::State front(layer->drawingState());
844 Region visibleReg = front.transform.transform(
845 Region(Rect(front.active.w, front.active.h)));
846 dirtyRegion.orSelf(visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700847 }
848 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 }
851
852 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700853 return dirtyRegion;
854}
855
856void SurfaceFlinger::commitTransaction()
857{
858 if (!mLayersPendingRemoval.isEmpty()) {
859 // Notify removed layers now that they can't be drawn from
860 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
861 mLayersPendingRemoval[i]->onRemoved();
862 }
863 mLayersPendingRemoval.clear();
864 }
865
866 mDrawingState = mCurrentState;
867 mTransationPending = false;
868 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869}
870
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800872 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873{
Mathias Agopian841cde52012-03-01 15:44:37 -0800874 ATRACE_CALL();
875
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 Region aboveOpaqueLayers;
877 Region aboveCoveredLayers;
878 Region dirty;
879
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700880 dirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881
882 size_t i = currentLayers.size();
883 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700884 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885
886 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700887 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888
Mathias Agopianab028732010-03-16 16:41:46 -0700889 /*
890 * opaqueRegion: area of a surface that is fully opaque.
891 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700893
894 /*
895 * visibleRegion: area of a surface that is visible on screen
896 * and not fully transparent. This is essentially the layer's
897 * footprint minus the opaque regions above it.
898 * Areas covered by a translucent surface are considered visible.
899 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700901
902 /*
903 * coveredRegion: area of a surface that is covered by all
904 * visible regions above it (which includes the translucent areas).
905 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700907
908
909 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800910 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700911 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700912 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700914 if (!visibleRegion.isEmpty()) {
915 // Remove the transparent area from the visible region
916 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700917 Region transparentRegionScreen;
918 const Transform tr(s.transform);
919 if (tr.transformed()) {
920 if (tr.preserveRects()) {
921 // transform the transparent region
922 transparentRegionScreen = tr.transform(s.transparentRegion);
923 } else {
924 // transformation too complex, can't do the
925 // transparent region optimization.
926 transparentRegionScreen.clear();
927 }
928 } else {
929 transparentRegionScreen = s.transparentRegion;
930 }
931 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700932 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933
Mathias Agopianab028732010-03-16 16:41:46 -0700934 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700935 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700936 if (s.alpha==255 && !translucent &&
937 ((layerOrientation & Transform::ROT_INVALID) == false)) {
938 // the opaque region is the layer's footprint
939 opaqueRegion = visibleRegion;
940 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 }
942 }
943
Mathias Agopianab028732010-03-16 16:41:46 -0700944 // Clip the covered region to the visible region
945 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
946
947 // Update aboveCoveredLayers for next (lower) layer
948 aboveCoveredLayers.orSelf(visibleRegion);
949
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 // subtract the opaque region covered by the layers above us
951 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952
953 // compute this layer's dirty region
954 if (layer->contentDirty) {
955 // we need to invalidate the whole region
956 dirty = visibleRegion;
957 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700958 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 layer->contentDirty = false;
960 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700961 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700962 * the exposed region consists of two components:
963 * 1) what's VISIBLE now and was COVERED before
964 * 2) what's EXPOSED now less what was EXPOSED before
965 *
966 * note that (1) is conservative, we start with the whole
967 * visible region but only keep what used to be covered by
968 * something -- which mean it may have been exposed.
969 *
970 * (2) handles areas that were not covered by anything but got
971 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700972 */
Mathias Agopianab028732010-03-16 16:41:46 -0700973 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700974 const Region oldVisibleRegion = layer->visibleRegion;
975 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700976 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
977 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 }
979 dirty.subtractSelf(aboveOpaqueLayers);
980
981 // accumulate to the screen dirty region
982 dirtyRegion.orSelf(dirty);
983
Mathias Agopianab028732010-03-16 16:41:46 -0700984 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700986
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 // Store the visible region is screen space
988 layer->setVisibleRegion(visibleRegion);
989 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 }
991
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 opaqueRegion = aboveOpaqueLayers;
993}
994
Mathias Agopian4fec8732012-06-29 14:12:52 -0700995Region SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800997 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700998 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001000 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001001
Mathias Agopian4fec8732012-06-29 14:12:52 -07001002 bool visibleRegions = false;
1003 const size_t count = currentLayers.size();
1004 sp<LayerBase> const* layers = currentLayers.array();
1005 for (size_t i=0 ; i<count ; i++) {
1006 const sp<LayerBase>& layer(layers[i]);
1007 dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
1008 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001009
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001010 mVisibleRegionsDirty |= visibleRegions;
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001011
Mathias Agopian4fec8732012-06-29 14:12:52 -07001012 return dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013}
1014
Mathias Agopianad456f92011-01-13 17:53:01 -08001015void SurfaceFlinger::invalidateHwcGeometry()
1016{
1017 mHwWorkListDirty = true;
1018}
1019
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001020void SurfaceFlinger::handleRefresh()
1021{
1022 bool needInvalidate = false;
1023 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1024 const size_t count = currentLayers.size();
1025 for (size_t i=0 ; i<count ; i++) {
1026 const sp<LayerBase>& layer(currentLayers[i]);
1027 if (layer->onPreComposition()) {
1028 needInvalidate = true;
1029 }
1030 }
1031 if (needInvalidate) {
1032 signalLayerUpdate();
1033 }
1034}
1035
1036
Mathias Agopian1b031492012-06-20 17:51:20 -07001037void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -07001038{
1039 mHwWorkListDirty = false;
Mathias Agopian86303202012-07-24 22:46:10 -07001040 HWComposer& hwc(getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001041 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001042 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001043 const size_t count = currentLayers.size();
1044 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001045
1046 HWComposer::LayerListIterator cur = hwc.begin();
1047 const HWComposer::LayerListIterator end = hwc.end();
1048 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001049 currentLayers[i]->setGeometry(hw, *cur);
Mathias Agopian53331da2011-08-22 21:44:41 -07001050 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001051 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001052 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001053 }
1054 }
1055}
Mathias Agopianb8a55602009-06-26 19:06:36 -07001056
Mathias Agopian1b031492012-06-20 17:51:20 -07001057void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
Mathias Agopian841cde52012-03-01 15:44:37 -08001059 ATRACE_CALL();
1060
Mathias Agopianb8a55602009-06-26 19:06:36 -07001061 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -07001062 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063
Glenn Kasten99ed2242011-12-15 09:51:17 -08001064 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -07001065 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 }
1067
Mathias Agopianb8a55602009-06-26 19:06:36 -07001068 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -07001069 glMatrixMode(GL_MODELVIEW);
1070 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071
1072 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001073 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001074 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1075 // takes a rectangle, we must make sure to update that whole
1076 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001077 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001078 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -07001079 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001080 // We need to redraw the rectangle that will be updated
1081 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001082 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001083 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -07001084 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001085 } else {
1086 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -07001088 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 }
1090 }
1091
Mathias Agopian1b031492012-06-20 17:51:20 -07001092 setupHardwareComposer(hw);
1093 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001095 // update the swap region and clear the dirty region
1096 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 mDirtyRegion.clear();
1098}
1099
Mathias Agopian1b031492012-06-20 17:51:20 -07001100void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101{
Mathias Agopian86303202012-07-24 22:46:10 -07001102 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001103 HWComposer::LayerListIterator cur = hwc.begin();
1104 const HWComposer::LayerListIterator end = hwc.end();
1105 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001106 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -07001107 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001108
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001109 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001110 size_t count = layers.size();
1111
Steve Blocke6f43dd2012-01-06 19:20:56 +00001112 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -07001113 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
1114 hwc.getNumLayers(), count);
1115
1116 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -07001117 if (hwc.initCheck() == NO_ERROR) {
1118 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
1119 }
Mathias Agopian45721772010-08-12 15:03:26 -07001120
1121 /*
1122 * update the per-frame h/w composer data for each layer
1123 * and build the transparent region of the FB
1124 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001125 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001126 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001127 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -07001128 }
1129 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001130 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -07001131}
Mathias Agopian45721772010-08-12 15:03:26 -07001132
Mathias Agopian1b031492012-06-20 17:51:20 -07001133void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001134{
Mathias Agopian86303202012-07-24 22:46:10 -07001135 HWComposer& hwc(getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001136 HWComposer::LayerListIterator cur = hwc.begin();
1137 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001138
1139 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001140 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001141 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -07001142
Mathias Agopianb9494d52012-04-18 02:28:45 -07001143 if (hwc.getLayerCount(HWC_OVERLAY)) {
1144 // when using overlays, we assume a fully transparent framebuffer
1145 // NOTE: we could reduce how much we need to clear, for instance
1146 // remove where there are opaque FB layers. however, on some
1147 // GPUs doing a "clean slate" glClear might be more efficient.
1148 // We'll revisit later if needed.
1149 glClearColor(0, 0, 0, 0);
1150 glClear(GL_COLOR_BUFFER_BIT);
1151 } else {
1152 // screen is already cleared here
1153 if (!mWormholeRegion.isEmpty()) {
1154 // can happen with SurfaceView
1155 drawWormhole();
1156 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001157 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001158
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001159 /*
1160 * and then, render the layers targeted at the framebuffer
1161 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001162
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001163 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001164 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001165 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001166 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001167 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001168 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001169 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001170 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001171 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001172 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001173 // never clear the very first layer since we're
1174 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001175 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001176 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001177 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001178 continue;
1179 }
1180 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001181 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001182 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001183 if (cur != end) {
1184 ++cur;
1185 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001186 }
1187 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188}
1189
Mathias Agopian1b031492012-06-20 17:51:20 -07001190void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191{
Mathias Agopian0a917752010-06-14 21:20:00 -07001192 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001193 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001194 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001195 return;
1196 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001197
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001198 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001199 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1200 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001201 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001202 }
1203
Mathias Agopianc492e672011-10-18 14:49:27 -07001204 glDisable(GL_TEXTURE_EXTERNAL_OES);
1205 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207
Mathias Agopian0926f502009-05-04 14:17:04 -07001208 static int toggle = 0;
1209 toggle = 1 - toggle;
1210 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001211 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001212 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001213 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215
Mathias Agopian20f68782009-05-11 00:03:41 -07001216 Region::const_iterator it = mDirtyRegion.begin();
1217 Region::const_iterator const end = mDirtyRegion.end();
1218 while (it != end) {
1219 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001221 { r.left, height - r.top },
1222 { r.left, height - r.bottom },
1223 { r.right, height - r.bottom },
1224 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 };
1226 glVertexPointer(2, GL_FLOAT, 0, vertices);
1227 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1228 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001229
Mathias Agopian0656a682011-09-20 17:22:44 -07001230 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231
1232 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001233 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234}
1235
1236void SurfaceFlinger::drawWormhole() const
1237{
1238 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1239 if (region.isEmpty())
1240 return;
1241
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001242 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001243 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001244 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001245 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001246
1247 GLfloat vertices[4][2];
1248 glVertexPointer(2, GL_FLOAT, 0, vertices);
1249 Region::const_iterator it = region.begin();
1250 Region::const_iterator const end = region.end();
1251 while (it != end) {
1252 const Rect& r = *it++;
1253 vertices[0][0] = r.left;
1254 vertices[0][1] = r.top;
1255 vertices[1][0] = r.right;
1256 vertices[1][1] = r.top;
1257 vertices[2][0] = r.right;
1258 vertices[2][1] = r.bottom;
1259 vertices[3][0] = r.left;
1260 vertices[3][1] = r.bottom;
1261 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1262 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263}
1264
Mathias Agopian96f08192010-06-02 23:28:45 -07001265ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1266 const sp<LayerBaseClient>& lbc)
1267{
Mathias Agopian96f08192010-06-02 23:28:45 -07001268 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001269 size_t name = client->attachLayer(lbc);
1270
Mathias Agopian96f08192010-06-02 23:28:45 -07001271 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001272 Mutex::Autolock _l(mStateLock);
1273 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001274
Mathias Agopian4f113742011-05-03 16:21:41 -07001275 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001276}
1277
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001278status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279{
1280 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001281 status_t err = purgatorizeLayer_l(layer);
1282 if (err == NO_ERROR)
1283 setTransactionFlags(eTransactionNeeded);
1284 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285}
1286
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001287status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
1289 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1290 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001291 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 return NO_ERROR;
1293 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001294 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295}
1296
Mathias Agopian9a112062009-04-17 19:36:26 -07001297status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1298{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001299 // First add the layer to the purgatory list, which makes sure it won't
1300 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001301 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001302 if (err >= 0) {
1303 mLayerPurgatory.add(layerBase);
1304 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001305
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001306 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001307
Mathias Agopian3d579642009-06-04 18:46:21 -07001308 // it's possible that we don't find a layer, because it might
1309 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001310 // from the user because there is a race between Client::destroySurface(),
1311 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001312 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1313}
1314
Mathias Agopiandea20b12011-05-03 17:04:02 -07001315uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1316{
1317 return android_atomic_release_load(&mTransactionFlags);
1318}
1319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1321{
1322 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1323}
1324
Mathias Agopianbb641242010-05-18 17:06:55 -07001325uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326{
1327 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1328 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001329 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 }
1331 return old;
1332}
1333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334
Mathias Agopian8b33f032012-07-24 20:43:54 -07001335void SurfaceFlinger::setTransactionState(
1336 const Vector<ComposerState>& state,
1337 const Vector<DisplayState>& displays,
1338 uint32_t flags)
1339{
Mathias Agopian698c0872011-06-28 19:09:31 -07001340 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001341
Mathias Agopian8b33f032012-07-24 20:43:54 -07001342 int orientation = eOrientationUnchanged;
1343 if (displays.size()) {
1344 // TODO: handle all displays
1345 orientation = displays[0].orientation;
1346 }
1347
Jamie Gennis28378392011-10-12 17:39:00 -07001348 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001349 if (mCurrentState.orientation != orientation) {
1350 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1351 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001352 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001353 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001354 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001355 orientation);
1356 }
1357 }
1358
Mathias Agopian698c0872011-06-28 19:09:31 -07001359 const size_t count = state.size();
1360 for (size_t i=0 ; i<count ; i++) {
1361 const ComposerState& s(state[i]);
1362 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001363 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001364 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001365
Mathias Agopian386aa982011-11-07 21:58:03 -08001366 if (transactionFlags) {
1367 // this triggers the transaction
1368 setTransactionFlags(transactionFlags);
1369
1370 // if this is a synchronous transaction, wait for it to take effect
1371 // before returning.
1372 if (flags & eSynchronous) {
1373 mTransationPending = true;
1374 }
1375 while (mTransationPending) {
1376 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1377 if (CC_UNLIKELY(err != NO_ERROR)) {
1378 // just in case something goes wrong in SF, return to the
1379 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001380 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001381 mTransationPending = false;
1382 break;
1383 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001384 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 }
1386}
1387
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001388sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001389 ISurfaceComposerClient::surface_data_t* params,
1390 const String8& name,
1391 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1393 uint32_t flags)
1394{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001395 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001396 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001397
1398 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001399 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001400 int(w), int(h));
1401 return surfaceHandle;
1402 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001403
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001404 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 switch (flags & eFXSurfaceMask) {
1406 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001407 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 break;
1409 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001410 // for now we treat Blur as Dim, until we can implement it
1411 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001413 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001415 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001416 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001417 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 }
1419
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001420 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001421 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001422 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001423 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001425 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001426 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001427 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001428 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001429 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 }
1431
1432 return surfaceHandle;
1433}
1434
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001435sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001436 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001437 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001438 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
1440 // initialize the surfaces
1441 switch (format) { // TODO: take h/w into account
1442 case PIXEL_FORMAT_TRANSPARENT:
1443 case PIXEL_FORMAT_TRANSLUCENT:
1444 format = PIXEL_FORMAT_RGBA_8888;
1445 break;
1446 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001447#ifdef NO_RGBX_8888
1448 format = PIXEL_FORMAT_RGB_565;
1449#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001450 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001451#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 break;
1453 }
1454
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001455#ifdef NO_RGBX_8888
1456 if (format == PIXEL_FORMAT_RGBX_8888)
1457 format = PIXEL_FORMAT_RGBA_8888;
1458#endif
1459
Mathias Agopian96f08192010-06-02 23:28:45 -07001460 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001461 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001462 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001463 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001464 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 }
1466 return layer;
1467}
1468
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001469sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001470 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001471 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472{
Mathias Agopian96f08192010-06-02 23:28:45 -07001473 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001474 return layer;
1475}
1476
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001477sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001478 const sp<Client>& client, DisplayID display,
1479 uint32_t w, uint32_t h, uint32_t flags)
1480{
1481 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 return layer;
1483}
1484
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001485status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486{
Mathias Agopian9a112062009-04-17 19:36:26 -07001487 /*
1488 * called by the window manager, when a surface should be marked for
1489 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001490 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001491 * The surface is removed from the current and drawing lists, but placed
1492 * in the purgatory queue, so it's not destroyed right-away (we need
1493 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001494 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001495
Mathias Agopian48d819a2009-09-10 19:41:18 -07001496 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001497 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001498 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001499
Mathias Agopian48d819a2009-09-10 19:41:18 -07001500 if (layer != 0) {
1501 err = purgatorizeLayer_l(layer);
1502 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001503 setTransactionFlags(eTransactionNeeded);
1504 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001505 }
1506 return err;
1507}
1508
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001509status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001510{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001511 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001512 status_t err = NO_ERROR;
1513 sp<LayerBaseClient> l(layer.promote());
1514 if (l != NULL) {
1515 Mutex::Autolock _l(mStateLock);
1516 err = removeLayer_l(l);
1517 if (err == NAME_NOT_FOUND) {
1518 // The surface wasn't in the current list, which means it was
1519 // removed already, which means it is in the purgatory,
1520 // and need to be removed from there.
1521 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001522 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001523 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001524 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001525 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001526 "error removing layer=%p (%s)", l.get(), strerror(-err));
1527 }
1528 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529}
1530
Mathias Agopian698c0872011-06-28 19:09:31 -07001531uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001532 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001533 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001536 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1537 if (layer != 0) {
1538 const uint32_t what = s.what;
1539 if (what & ePositionChanged) {
1540 if (layer->setPosition(s.x, s.y))
1541 flags |= eTraversalNeeded;
1542 }
1543 if (what & eLayerChanged) {
1544 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1545 if (layer->setLayer(s.z)) {
1546 mCurrentState.layersSortedByZ.removeAt(idx);
1547 mCurrentState.layersSortedByZ.add(layer);
1548 // we need traversal (state changed)
1549 // AND transaction (list changed)
1550 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 }
1552 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001553 if (what & eSizeChanged) {
1554 if (layer->setSize(s.w, s.h)) {
1555 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001556 }
1557 }
1558 if (what & eAlphaChanged) {
1559 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1560 flags |= eTraversalNeeded;
1561 }
1562 if (what & eMatrixChanged) {
1563 if (layer->setMatrix(s.matrix))
1564 flags |= eTraversalNeeded;
1565 }
1566 if (what & eTransparentRegionChanged) {
1567 if (layer->setTransparentRegionHint(s.transparentRegion))
1568 flags |= eTraversalNeeded;
1569 }
1570 if (what & eVisibilityChanged) {
1571 if (layer->setFlags(s.flags, s.mask))
1572 flags |= eTraversalNeeded;
1573 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001574 if (what & eCropChanged) {
1575 if (layer->setCrop(s.crop))
1576 flags |= eTraversalNeeded;
1577 }
Mathias Agopian87855782012-07-24 21:41:09 -07001578 if (what & eLayerStackChanged) {
1579 if (layer->setLayerStack(s.layerStack))
1580 flags |= eTraversalNeeded;
1581 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001583 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584}
1585
Mathias Agopianb60314a2012-04-10 22:09:54 -07001586// ---------------------------------------------------------------------------
1587
1588void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001589 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001590 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopian86303202012-07-24 22:46:10 -07001591 getHwComposer().acquire();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001592 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001593 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001594 // this is a temporary work-around, eventually this should be called
1595 // by the power-manager
1596 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001597 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001598 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599}
1600
Mathias Agopianb60314a2012-04-10 22:09:54 -07001601void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001602 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001603 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001604 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001605 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001606 hw.releaseScreen();
Mathias Agopian86303202012-07-24 22:46:10 -07001607 getHwComposer().release();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001608 // from this point on, SF will stop drawing
1609 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610}
1611
Colin Cross8e533062012-06-07 13:17:52 -07001612void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001613 class MessageScreenAcquired : public MessageBase {
1614 SurfaceFlinger* flinger;
1615 public:
1616 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1617 virtual bool handler() {
1618 flinger->onScreenAcquired();
1619 return true;
1620 }
1621 };
1622 sp<MessageBase> msg = new MessageScreenAcquired(this);
1623 postMessageSync(msg);
1624}
1625
Colin Cross8e533062012-06-07 13:17:52 -07001626void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001627 class MessageScreenReleased : public MessageBase {
1628 SurfaceFlinger* flinger;
1629 public:
1630 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1631 virtual bool handler() {
1632 flinger->onScreenReleased();
1633 return true;
1634 }
1635 };
1636 sp<MessageBase> msg = new MessageScreenReleased(this);
1637 postMessageSync(msg);
1638}
1639
1640// ---------------------------------------------------------------------------
1641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1643{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001644 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 char buffer[SIZE];
1646 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001647
1648 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 snprintf(buffer, SIZE, "Permission Denial: "
1650 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1651 IPCThreadState::self()->getCallingPid(),
1652 IPCThreadState::self()->getCallingUid());
1653 result.append(buffer);
1654 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001655 // Try to get the main lock, but don't insist if we can't
1656 // (this would indicate SF is stuck, but we want to be able to
1657 // print something in dumpsys).
1658 int retry = 3;
1659 while (mStateLock.tryLock()<0 && --retry>=0) {
1660 usleep(1000000);
1661 }
1662 const bool locked(retry >= 0);
1663 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001664 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001665 "SurfaceFlinger appears to be unresponsive, "
1666 "dumping anyways (no locks held)\n");
1667 result.append(buffer);
1668 }
1669
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001670 bool dumpAll = true;
1671 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001672 size_t numArgs = args.size();
1673 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001674 if ((index < numArgs) &&
1675 (args[index] == String16("--list"))) {
1676 index++;
1677 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001678 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001679 }
1680
1681 if ((index < numArgs) &&
1682 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001683 index++;
1684 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001685 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001686 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001687
1688 if ((index < numArgs) &&
1689 (args[index] == String16("--latency-clear"))) {
1690 index++;
1691 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001692 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001693 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001695
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001696 if (dumpAll) {
1697 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001698 }
1699
Mathias Agopian9795c422009-08-26 16:36:26 -07001700 if (locked) {
1701 mStateLock.unlock();
1702 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 }
1704 write(fd, result.string(), result.size());
1705 return NO_ERROR;
1706}
1707
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001708void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1709 String8& result, char* buffer, size_t SIZE) const
1710{
1711 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1712 const size_t count = currentLayers.size();
1713 for (size_t i=0 ; i<count ; i++) {
1714 const sp<LayerBase>& layer(currentLayers[i]);
1715 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1716 result.append(buffer);
1717 }
1718}
1719
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001720void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1721 String8& result, char* buffer, size_t SIZE) const
1722{
1723 String8 name;
1724 if (index < args.size()) {
1725 name = String8(args[index]);
1726 index++;
1727 }
1728
1729 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1730 const size_t count = currentLayers.size();
1731 for (size_t i=0 ; i<count ; i++) {
1732 const sp<LayerBase>& layer(currentLayers[i]);
1733 if (name.isEmpty()) {
1734 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1735 result.append(buffer);
1736 }
1737 if (name.isEmpty() || (name == layer->getName())) {
1738 layer->dumpStats(result, buffer, SIZE);
1739 }
1740 }
1741}
1742
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001743void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1744 String8& result, char* buffer, size_t SIZE) const
1745{
1746 String8 name;
1747 if (index < args.size()) {
1748 name = String8(args[index]);
1749 index++;
1750 }
1751
1752 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1753 const size_t count = currentLayers.size();
1754 for (size_t i=0 ; i<count ; i++) {
1755 const sp<LayerBase>& layer(currentLayers[i]);
1756 if (name.isEmpty() || (name == layer->getName())) {
1757 layer->clearStats();
1758 }
1759 }
1760}
1761
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001762void SurfaceFlinger::dumpAllLocked(
1763 String8& result, char* buffer, size_t SIZE) const
1764{
1765 // figure out if we're stuck somewhere
1766 const nsecs_t now = systemTime();
1767 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1768 const nsecs_t inTransaction(mDebugInTransaction);
1769 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1770 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1771
1772 /*
1773 * Dump the visible layer list
1774 */
1775 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1776 const size_t count = currentLayers.size();
1777 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1778 result.append(buffer);
1779 for (size_t i=0 ; i<count ; i++) {
1780 const sp<LayerBase>& layer(currentLayers[i]);
1781 layer->dump(result, buffer, SIZE);
1782 }
1783
1784 /*
1785 * Dump the layers in the purgatory
1786 */
1787
1788 const size_t purgatorySize = mLayerPurgatory.size();
1789 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1790 result.append(buffer);
1791 for (size_t i=0 ; i<purgatorySize ; i++) {
1792 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1793 layer->shortDump(result, buffer, SIZE);
1794 }
1795
1796 /*
1797 * Dump SurfaceFlinger global state
1798 */
1799
1800 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1801 result.append(buffer);
1802
Mathias Agopian1b031492012-06-20 17:51:20 -07001803 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001804 const GLExtensions& extensions(GLExtensions::getInstance());
1805 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1806 extensions.getVendor(),
1807 extensions.getRenderer(),
1808 extensions.getVersion());
1809 result.append(buffer);
1810
1811 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001812 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001813 EGL_VERSION_HW_ANDROID));
1814 result.append(buffer);
1815
1816 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1817 result.append(buffer);
1818
1819 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001820 snprintf(buffer, SIZE,
1821 " orientation=%d, canDraw=%d\n",
1822 mCurrentState.orientation, hw.canDraw());
1823 result.append(buffer);
1824 snprintf(buffer, SIZE,
1825 " last eglSwapBuffers() time: %f us\n"
1826 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001827 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001828 " refresh-rate : %f fps\n"
1829 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001830 " y-dpi : %f\n"
1831 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001832 mLastSwapBufferTime/1000.0,
1833 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001834 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001835 hw.getRefreshRate(),
1836 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001837 hw.getDpiY(),
1838 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001839 result.append(buffer);
1840
1841 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1842 inSwapBuffersDuration/1000.0);
1843 result.append(buffer);
1844
1845 snprintf(buffer, SIZE, " transaction time: %f us\n",
1846 inTransactionDuration/1000.0);
1847 result.append(buffer);
1848
1849 /*
1850 * VSYNC state
1851 */
1852 mEventThread->dump(result, buffer, SIZE);
1853
1854 /*
1855 * Dump HWComposer state
1856 */
Mathias Agopian86303202012-07-24 22:46:10 -07001857 HWComposer& hwc(getHwComposer());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001858 snprintf(buffer, SIZE, "h/w composer state:\n");
1859 result.append(buffer);
1860 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1861 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1862 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1863 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001864 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001865
1866 /*
1867 * Dump gralloc state
1868 */
1869 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1870 alloc.dump(result);
1871 hw.dump(result);
1872}
1873
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874status_t SurfaceFlinger::onTransact(
1875 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1876{
1877 switch (code) {
1878 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001879 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001882 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001883 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001884 case BLANK:
1885 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001886 {
1887 // codes that require permission check
1888 IPCThreadState* ipc = IPCThreadState::self();
1889 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001890 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001891 if ((uid != AID_GRAPHICS) &&
1892 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001893 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001894 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1895 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001897 break;
1898 }
1899 case CAPTURE_SCREEN:
1900 {
1901 // codes that require permission check
1902 IPCThreadState* ipc = IPCThreadState::self();
1903 const int pid = ipc->getCallingPid();
1904 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001905 if ((uid != AID_GRAPHICS) &&
1906 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001907 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001908 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1909 return PERMISSION_DENIED;
1910 }
1911 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912 }
1913 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001914
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1916 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001917 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001918 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001919 IPCThreadState* ipc = IPCThreadState::self();
1920 const int pid = ipc->getCallingPid();
1921 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001922 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001923 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924 return PERMISSION_DENIED;
1925 }
1926 int n;
1927 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001928 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001929 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 return NO_ERROR;
1931 case 1002: // SHOW_UPDATES
1932 n = data.readInt32();
1933 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001934 invalidateHwcGeometry();
1935 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001938 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001939 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001941 case 1005:{ // force transaction
1942 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1943 return NO_ERROR;
1944 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001945 case 1006:{ // send empty update
1946 signalRefresh();
1947 return NO_ERROR;
1948 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001949 case 1008: // toggle use of hw composer
1950 n = data.readInt32();
1951 mDebugDisableHWC = n ? 1 : 0;
1952 invalidateHwcGeometry();
1953 repaintEverything();
1954 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001955 case 1009: // toggle use of transform hint
1956 n = data.readInt32();
1957 mDebugDisableTransformHint = n ? 1 : 0;
1958 invalidateHwcGeometry();
1959 repaintEverything();
1960 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001962 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963 reply->writeInt32(0);
1964 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001965 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001966 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001967 return NO_ERROR;
1968 case 1013: {
1969 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001970 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001971 reply->writeInt32(hw.getPageFlipCount());
1972 }
1973 return NO_ERROR;
1974 }
1975 }
1976 return err;
1977}
1978
Mathias Agopian53331da2011-08-22 21:44:41 -07001979void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001980 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001981 const Rect bounds(hw.getBounds());
1982 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001983 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001984}
1985
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001986void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1987 Mutex::Autolock _l(mInvalidateLock);
1988 mInvalidateRegion = reg;
1989}
1990
1991Region SurfaceFlinger::getAndClearInvalidateRegion() {
1992 Mutex::Autolock _l(mInvalidateLock);
1993 Region reg(mInvalidateRegion);
1994 mInvalidateRegion.clear();
1995 return reg;
1996}
1997
Mathias Agopian59119e62010-10-11 12:37:43 -07001998// ---------------------------------------------------------------------------
1999
Mathias Agopian118d0242011-10-13 16:02:48 -07002000status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
2001 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
2002{
2003 Mutex::Autolock _l(mStateLock);
2004 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2005}
2006
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002007status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2008 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002009{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002010 ATRACE_CALL();
2011
Mathias Agopian59119e62010-10-11 12:37:43 -07002012 if (!GLExtensions::getInstance().haveFramebufferObject())
2013 return INVALID_OPERATION;
2014
2015 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002016 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 const uint32_t hw_w = hw.getWidth();
2018 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002019 GLfloat u = 1;
2020 GLfloat v = 1;
2021
2022 // make sure to clear all GL error flags
2023 while ( glGetError() != GL_NO_ERROR ) ;
2024
2025 // create a FBO
2026 GLuint name, tname;
2027 glGenTextures(1, &tname);
2028 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002029 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2030 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002031 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2032 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002033 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002034 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002035 GLint tw = (2 << (31 - clz(hw_w)));
2036 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002037 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2038 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002039 u = GLfloat(hw_w) / tw;
2040 v = GLfloat(hw_h) / th;
2041 }
2042 glGenFramebuffersOES(1, &name);
2043 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2045 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002046
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002048 glDisable(GL_TEXTURE_EXTERNAL_OES);
2049 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002050 glClearColor(0,0,0,1);
2051 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002052 glMatrixMode(GL_MODELVIEW);
2053 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002054 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002055 const size_t count = layers.size();
2056 for (size_t i=0 ; i<count ; ++i) {
2057 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07002058 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002059 }
2060
Mathias Agopian118d0242011-10-13 16:02:48 -07002061 hw.compositionComplete();
2062
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002063 // back to main framebuffer
2064 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002065 glDeleteFramebuffersOES(1, &name);
2066
2067 *textureName = tname;
2068 *uOut = u;
2069 *vOut = v;
2070 return NO_ERROR;
2071}
2072
2073// ---------------------------------------------------------------------------
2074
Mathias Agopianed9807b2012-05-18 14:18:08 -07002075class VSyncWaiter {
2076 DisplayEventReceiver::Event buffer[4];
2077 sp<Looper> looper;
2078 sp<IDisplayEventConnection> events;
2079 sp<BitTube> eventTube;
2080public:
2081 VSyncWaiter(const sp<EventThread>& eventThread) {
2082 looper = new Looper(true);
2083 events = eventThread->createEventConnection();
2084 eventTube = events->getDataChannel();
2085 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
2086 events->requestNextVsync();
2087 }
2088
2089 void wait() {
2090 ssize_t n;
2091
2092 looper->pollOnce(-1);
2093 // we don't handle any errors here, it doesn't matter
2094 // and we don't want to take the risk to get stuck.
2095
2096 // drain the events...
2097 while ((n = DisplayEventReceiver::getEvents(
2098 eventTube, buffer, 4)) > 0) ;
2099
2100 events->requestNextVsync();
2101 }
2102};
2103
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002104status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
2105{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002107 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002108 const uint32_t hw_w = hw.getWidth();
2109 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07002110 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002111
2112 GLfloat u, v;
2113 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002114 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002115 if (result != NO_ERROR) {
2116 return result;
2117 }
2118
2119 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002120 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002121 glBindTexture(GL_TEXTURE_2D, tname);
2122 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2123 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2124 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002125 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2126 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002127 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2128 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2129 glVertexPointer(2, GL_FLOAT, 0, vtx);
2130
Mathias Agopianffcf4652011-07-07 17:30:31 -07002131 /*
2132 * Texture coordinate mapping
2133 *
2134 * u
2135 * 1 +----------+---+
2136 * | | | | image is inverted
2137 * | V | | w.r.t. the texture
2138 * 1-v +----------+ | coordinates
2139 * | |
2140 * | |
2141 * | |
2142 * 0 +--------------+
2143 * 0 1
2144 *
2145 */
2146
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002147 class s_curve_interpolator {
2148 const float nbFrames, s, v;
2149 public:
2150 s_curve_interpolator(int nbFrames, float s)
2151 : nbFrames(1.0f / (nbFrames-1)), s(s),
2152 v(1.0f + expf(-s + 0.5f*s)) {
2153 }
2154 float operator()(int f) {
2155 const float x = f * nbFrames;
2156 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2157 }
2158 };
2159
2160 class v_stretch {
2161 const GLfloat hw_w, hw_h;
2162 public:
2163 v_stretch(uint32_t hw_w, uint32_t hw_h)
2164 : hw_w(hw_w), hw_h(hw_h) {
2165 }
2166 void operator()(GLfloat* vtx, float v) {
2167 const GLfloat w = hw_w + (hw_w * v);
2168 const GLfloat h = hw_h - (hw_h * v);
2169 const GLfloat x = (hw_w - w) * 0.5f;
2170 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002171 vtx[0] = x; vtx[1] = y;
2172 vtx[2] = x; vtx[3] = y + h;
2173 vtx[4] = x + w; vtx[5] = y + h;
2174 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002175 }
2176 };
2177
2178 class h_stretch {
2179 const GLfloat hw_w, hw_h;
2180 public:
2181 h_stretch(uint32_t hw_w, uint32_t hw_h)
2182 : hw_w(hw_w), hw_h(hw_h) {
2183 }
2184 void operator()(GLfloat* vtx, float v) {
2185 const GLfloat w = hw_w - (hw_w * v);
2186 const GLfloat h = 1.0f;
2187 const GLfloat x = (hw_w - w) * 0.5f;
2188 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002189 vtx[0] = x; vtx[1] = y;
2190 vtx[2] = x; vtx[3] = y + h;
2191 vtx[4] = x + w; vtx[5] = y + h;
2192 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002193 }
2194 };
2195
Mathias Agopianed9807b2012-05-18 14:18:08 -07002196 VSyncWaiter vsync(mEventThread);
2197
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002199 char value[PROPERTY_VALUE_MAX];
2200 property_get("debug.sf.electron_frames", value, "24");
2201 int nbFrames = (atoi(value) + 1) >> 1;
2202 if (nbFrames <= 0) // just in case
2203 nbFrames = 24;
2204
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 s_curve_interpolator itr(nbFrames, 7.5f);
2206 s_curve_interpolator itg(nbFrames, 8.0f);
2207 s_curve_interpolator itb(nbFrames, 8.5f);
2208
2209 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002210
2211 glMatrixMode(GL_TEXTURE);
2212 glLoadIdentity();
2213 glMatrixMode(GL_MODELVIEW);
2214 glLoadIdentity();
2215
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002216 glEnable(GL_BLEND);
2217 glBlendFunc(GL_ONE, GL_ONE);
2218 for (int i=0 ; i<nbFrames ; i++) {
2219 float x, y, w, h;
2220 const float vr = itr(i);
2221 const float vg = itg(i);
2222 const float vb = itb(i);
2223
Mathias Agopianed9807b2012-05-18 14:18:08 -07002224 // wait for vsync
2225 vsync.wait();
2226
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002227 // clear screen
2228 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002229 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002230 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002231
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002232 // draw the red plane
2233 vverts(vtx, vr);
2234 glColorMask(1,0,0,1);
2235 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002236
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237 // draw the green plane
2238 vverts(vtx, vg);
2239 glColorMask(0,1,0,1);
2240 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002241
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002242 // draw the blue plane
2243 vverts(vtx, vb);
2244 glColorMask(0,0,1,1);
2245 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002246
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002247 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 glDisable(GL_TEXTURE_2D);
2249 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002250 glColor4f(vg, vg, vg, 1);
2251 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2252 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002253 }
2254
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002255 h_stretch hverts(hw_w, hw_h);
2256 glDisable(GL_BLEND);
2257 glDisable(GL_TEXTURE_2D);
2258 glColorMask(1,1,1,1);
2259 for (int i=0 ; i<nbFrames ; i++) {
2260 const float v = itg(i);
2261 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002262
2263 // wait for vsync
2264 vsync.wait();
2265
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002266 glClear(GL_COLOR_BUFFER_BIT);
2267 glColor4f(1-v, 1-v, 1-v, 1);
2268 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2269 hw.flip(screenBounds);
2270 }
2271
2272 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002273 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2274 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002275 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002276 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002277 return NO_ERROR;
2278}
2279
2280status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2281{
2282 status_t result = PERMISSION_DENIED;
2283
2284 if (!GLExtensions::getInstance().haveFramebufferObject())
2285 return INVALID_OPERATION;
2286
2287
2288 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002289 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290 const uint32_t hw_w = hw.getWidth();
2291 const uint32_t hw_h = hw.getHeight();
2292 const Region screenBounds(hw.bounds());
2293
2294 GLfloat u, v;
2295 GLuint tname;
2296 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2297 if (result != NO_ERROR) {
2298 return result;
2299 }
2300
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 GLfloat vtx[8];
2302 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002303 glBindTexture(GL_TEXTURE_2D, tname);
2304 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2305 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2306 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002307 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2308 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002309 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2310 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2311 glVertexPointer(2, GL_FLOAT, 0, vtx);
2312
2313 class s_curve_interpolator {
2314 const float nbFrames, s, v;
2315 public:
2316 s_curve_interpolator(int nbFrames, float s)
2317 : nbFrames(1.0f / (nbFrames-1)), s(s),
2318 v(1.0f + expf(-s + 0.5f*s)) {
2319 }
2320 float operator()(int f) {
2321 const float x = f * nbFrames;
2322 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2323 }
2324 };
2325
2326 class v_stretch {
2327 const GLfloat hw_w, hw_h;
2328 public:
2329 v_stretch(uint32_t hw_w, uint32_t hw_h)
2330 : hw_w(hw_w), hw_h(hw_h) {
2331 }
2332 void operator()(GLfloat* vtx, float v) {
2333 const GLfloat w = hw_w + (hw_w * v);
2334 const GLfloat h = hw_h - (hw_h * v);
2335 const GLfloat x = (hw_w - w) * 0.5f;
2336 const GLfloat y = (hw_h - h) * 0.5f;
2337 vtx[0] = x; vtx[1] = y;
2338 vtx[2] = x; vtx[3] = y + h;
2339 vtx[4] = x + w; vtx[5] = y + h;
2340 vtx[6] = x + w; vtx[7] = y;
2341 }
2342 };
2343
2344 class h_stretch {
2345 const GLfloat hw_w, hw_h;
2346 public:
2347 h_stretch(uint32_t hw_w, uint32_t hw_h)
2348 : hw_w(hw_w), hw_h(hw_h) {
2349 }
2350 void operator()(GLfloat* vtx, float v) {
2351 const GLfloat w = hw_w - (hw_w * v);
2352 const GLfloat h = 1.0f;
2353 const GLfloat x = (hw_w - w) * 0.5f;
2354 const GLfloat y = (hw_h - h) * 0.5f;
2355 vtx[0] = x; vtx[1] = y;
2356 vtx[2] = x; vtx[3] = y + h;
2357 vtx[4] = x + w; vtx[5] = y + h;
2358 vtx[6] = x + w; vtx[7] = y;
2359 }
2360 };
2361
Mathias Agopianed9807b2012-05-18 14:18:08 -07002362 VSyncWaiter vsync(mEventThread);
2363
Mathias Agopiana6546e52010-10-14 12:33:07 -07002364 // the full animation is 12 frames
2365 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002366 s_curve_interpolator itr(nbFrames, 7.5f);
2367 s_curve_interpolator itg(nbFrames, 8.0f);
2368 s_curve_interpolator itb(nbFrames, 8.5f);
2369
2370 h_stretch hverts(hw_w, hw_h);
2371 glDisable(GL_BLEND);
2372 glDisable(GL_TEXTURE_2D);
2373 glColorMask(1,1,1,1);
2374 for (int i=nbFrames-1 ; i>=0 ; i--) {
2375 const float v = itg(i);
2376 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002377
2378 // wait for vsync
2379 vsync.wait();
2380
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002381 glClear(GL_COLOR_BUFFER_BIT);
2382 glColor4f(1-v, 1-v, 1-v, 1);
2383 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2384 hw.flip(screenBounds);
2385 }
2386
Mathias Agopiana6546e52010-10-14 12:33:07 -07002387 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002388 v_stretch vverts(hw_w, hw_h);
2389 glEnable(GL_BLEND);
2390 glBlendFunc(GL_ONE, GL_ONE);
2391 for (int i=nbFrames-1 ; i>=0 ; i--) {
2392 float x, y, w, h;
2393 const float vr = itr(i);
2394 const float vg = itg(i);
2395 const float vb = itb(i);
2396
Mathias Agopianed9807b2012-05-18 14:18:08 -07002397 // wait for vsync
2398 vsync.wait();
2399
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002400 // clear screen
2401 glColorMask(1,1,1,1);
2402 glClear(GL_COLOR_BUFFER_BIT);
2403 glEnable(GL_TEXTURE_2D);
2404
2405 // draw the red plane
2406 vverts(vtx, vr);
2407 glColorMask(1,0,0,1);
2408 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2409
2410 // draw the green plane
2411 vverts(vtx, vg);
2412 glColorMask(0,1,0,1);
2413 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2414
2415 // draw the blue plane
2416 vverts(vtx, vb);
2417 glColorMask(0,0,1,1);
2418 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2419
2420 hw.flip(screenBounds);
2421 }
2422
2423 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002424 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002425 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002426 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002427 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002428
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002429 return NO_ERROR;
2430}
2431
2432// ---------------------------------------------------------------------------
2433
Mathias Agopianabd671a2010-10-14 14:54:06 -07002434status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002435{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002436 ATRACE_CALL();
2437
Mathias Agopian1b031492012-06-20 17:51:20 -07002438 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002439 if (!hw.canDraw()) {
2440 // we're already off
2441 return NO_ERROR;
2442 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002443
2444 // turn off hwc while we're doing the animation
Mathias Agopian86303202012-07-24 22:46:10 -07002445 getHwComposer().disable();
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002446 // and make sure to turn it back on (if needed) next time we compose
2447 invalidateHwcGeometry();
2448
Mathias Agopianabd671a2010-10-14 14:54:06 -07002449 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2450 electronBeamOffAnimationImplLocked();
2451 }
2452
2453 // always clear the whole screen at the end of the animation
2454 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002455 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002456 hw.flip( Region(hw.bounds()) );
2457
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002458 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002459}
2460
2461status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2462{
Mathias Agopian59119e62010-10-11 12:37:43 -07002463 class MessageTurnElectronBeamOff : public MessageBase {
2464 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002465 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002466 status_t result;
2467 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002468 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2469 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002470 }
2471 status_t getResult() const {
2472 return result;
2473 }
2474 virtual bool handler() {
2475 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002476 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002477 return true;
2478 }
2479 };
2480
Mathias Agopianabd671a2010-10-14 14:54:06 -07002481 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002482 status_t res = postMessageSync(msg);
2483 if (res == NO_ERROR) {
2484 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002485
2486 // work-around: when the power-manager calls us we activate the
2487 // animation. eventually, the "on" animation will be called
2488 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002489 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002490 }
2491 return res;
2492}
2493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002495
Mathias Agopianabd671a2010-10-14 14:54:06 -07002496status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002497{
Mathias Agopian1b031492012-06-20 17:51:20 -07002498 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002499 if (hw.canDraw()) {
2500 // we're already on
2501 return NO_ERROR;
2502 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002503 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2504 electronBeamOnAnimationImplLocked();
2505 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002506
2507 // make sure to redraw the whole screen when the animation is done
2508 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002509 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002510
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002511 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002512}
2513
2514status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2515{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002516 class MessageTurnElectronBeamOn : public MessageBase {
2517 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002518 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002519 status_t result;
2520 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002521 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2522 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002523 }
2524 status_t getResult() const {
2525 return result;
2526 }
2527 virtual bool handler() {
2528 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002529 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002530 return true;
2531 }
2532 };
2533
Mathias Agopianabd671a2010-10-14 14:54:06 -07002534 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002535 return NO_ERROR;
2536}
2537
2538// ---------------------------------------------------------------------------
2539
Mathias Agopian74c40c02010-09-29 13:02:36 -07002540status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2541 sp<IMemoryHeap>* heap,
2542 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002543 uint32_t sw, uint32_t sh,
2544 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002545{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002546 ATRACE_CALL();
2547
Mathias Agopian74c40c02010-09-29 13:02:36 -07002548 status_t result = PERMISSION_DENIED;
2549
2550 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002551 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002552 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002553 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002554
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002555 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002556 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002557 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002558
2559 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002560 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002561 const uint32_t hw_w = hw.getWidth();
2562 const uint32_t hw_h = hw.getHeight();
2563
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002564 // if we have secure windows on this display, never allow the screen capture
2565 if (hw.getSecureLayerVisible()) {
2566 return PERMISSION_DENIED;
2567 }
2568
2569 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002570 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002571 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002572
2573 sw = (!sw) ? hw_w : sw;
2574 sh = (!sh) ? hw_h : sh;
2575 const size_t size = sw * sh * 4;
2576
Steve Block9d453682011-12-20 16:23:08 +00002577 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002578 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002579
Mathias Agopian74c40c02010-09-29 13:02:36 -07002580 // make sure to clear all GL error flags
2581 while ( glGetError() != GL_NO_ERROR ) ;
2582
2583 // create a FBO
2584 GLuint name, tname;
2585 glGenRenderbuffersOES(1, &tname);
2586 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2587 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002588
Mathias Agopian74c40c02010-09-29 13:02:36 -07002589 glGenFramebuffersOES(1, &name);
2590 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2591 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2592 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2593
2594 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002595
Mathias Agopian74c40c02010-09-29 13:02:36 -07002596 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2597
2598 // invert everything, b/c glReadPixel() below will invert the FB
2599 glViewport(0, 0, sw, sh);
2600 glMatrixMode(GL_PROJECTION);
2601 glPushMatrix();
2602 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002603 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002604 glMatrixMode(GL_MODELVIEW);
2605
2606 // redraw the screen entirely...
2607 glClearColor(0,0,0,1);
2608 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002609
Jamie Gennis9575f602011-10-07 14:51:16 -07002610 const LayerVector& layers(mDrawingState.layersSortedByZ);
2611 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002612 for (size_t i=0 ; i<count ; ++i) {
2613 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002614 const uint32_t flags = layer->drawingState().flags;
2615 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2616 const uint32_t z = layer->drawingState().z;
2617 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002618 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002619 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002620 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002621 }
2622
Mathias Agopian74c40c02010-09-29 13:02:36 -07002623 // check for errors and return screen capture
2624 if (glGetError() != GL_NO_ERROR) {
2625 // error while rendering
2626 result = INVALID_OPERATION;
2627 } else {
2628 // allocate shared memory large enough to hold the
2629 // screen capture
2630 sp<MemoryHeapBase> base(
2631 new MemoryHeapBase(size, 0, "screen-capture") );
2632 void* const ptr = base->getBase();
2633 if (ptr) {
2634 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002635 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002636 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2637 if (glGetError() == GL_NO_ERROR) {
2638 *heap = base;
2639 *w = sw;
2640 *h = sh;
2641 *f = PIXEL_FORMAT_RGBA_8888;
2642 result = NO_ERROR;
2643 }
2644 } else {
2645 result = NO_MEMORY;
2646 }
2647 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002648 glViewport(0, 0, hw_w, hw_h);
2649 glMatrixMode(GL_PROJECTION);
2650 glPopMatrix();
2651 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002652 } else {
2653 result = BAD_VALUE;
2654 }
2655
2656 // release FBO resources
2657 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2658 glDeleteRenderbuffersOES(1, &tname);
2659 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002660
2661 hw.compositionComplete();
2662
Steve Block9d453682011-12-20 16:23:08 +00002663 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002664
Mathias Agopian74c40c02010-09-29 13:02:36 -07002665 return result;
2666}
2667
2668
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002669status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2670 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002671 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002672 uint32_t sw, uint32_t sh,
2673 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002674{
2675 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002676 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002677 return BAD_VALUE;
2678
2679 if (!GLExtensions::getInstance().haveFramebufferObject())
2680 return INVALID_OPERATION;
2681
2682 class MessageCaptureScreen : public MessageBase {
2683 SurfaceFlinger* flinger;
2684 DisplayID dpy;
2685 sp<IMemoryHeap>* heap;
2686 uint32_t* w;
2687 uint32_t* h;
2688 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002689 uint32_t sw;
2690 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002691 uint32_t minLayerZ;
2692 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002693 status_t result;
2694 public:
2695 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002696 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002697 uint32_t sw, uint32_t sh,
2698 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002699 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002700 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2701 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2702 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002703 {
2704 }
2705 status_t getResult() const {
2706 return result;
2707 }
2708 virtual bool handler() {
2709 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002710 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002711 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002712 return true;
2713 }
2714 };
2715
2716 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002717 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002718 status_t res = postMessageSync(msg);
2719 if (res == NO_ERROR) {
2720 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2721 }
2722 return res;
2723}
2724
2725// ---------------------------------------------------------------------------
2726
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002727SurfaceFlinger::LayerVector::LayerVector() {
2728}
2729
2730SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2731 : SortedVector<sp<LayerBase> >(rhs) {
2732}
2733
2734int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2735 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002736{
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002737 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2738 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
2739 // sort layers by Z order
2740 uint32_t lz = l->currentState().z;
2741 uint32_t rz = r->currentState().z;
2742 // then by sequence, so we get a stable ordering
2743 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
2744}
2745
2746// ---------------------------------------------------------------------------
2747
2748SurfaceFlinger::State::State()
2749 : orientation(ISurfaceComposer::eOrientationDefault),
2750 orientationFlags(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002751}
2752
2753// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002754
Jamie Gennis9a78c902011-01-12 18:30:40 -08002755GraphicBufferAlloc::GraphicBufferAlloc() {}
2756
2757GraphicBufferAlloc::~GraphicBufferAlloc() {}
2758
2759sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002760 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002761 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2762 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002763 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002764 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002765 if (err == NO_MEMORY) {
2766 GraphicBuffer::dumpAllocationsToSystemLog();
2767 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002768 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002769 "failed (%s), handle=%p",
2770 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002771 return 0;
2772 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002773 return graphicBuffer;
2774}
2775
Jamie Gennis9a78c902011-01-12 18:30:40 -08002776// ---------------------------------------------------------------------------
2777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002778}; // namespace android