blob: b70c720d7c70baadb06ee7a9debc9b314c104017 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070023
24#include <EGL/egl.h>
25#include <GLES/gl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopiand0566bc2011-11-17 17:49:17 -080035#include <gui/IDisplayEventConnection.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070036#include <gui/BitTube.h>
37#include <gui/SurfaceTextureClient.h>
38
39#include <ui/GraphicBufferAllocator.h>
40#include <ui/PixelFormat.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <utils/String8.h>
43#include <utils/String16.h>
44#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080045#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047#include <private/android_filesystem_config.h>
48#include <private/gui/SharedBufferStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080051#include "DdmConnection.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070052#include "DisplayHardware.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070053#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopiana4912602012-07-12 14:25:33 -070061#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Mathias Agopiana67932f2011-04-20 14:20:59 -070064
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080065#define EGL_VERSION_HW_ANDROID 0x3143
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian99b49842011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070082 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070087 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070089 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070090 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070091 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070092 mDebugInSwapBuffers(0),
93 mLastSwapBufferTime(0),
94 mDebugInTransaction(0),
95 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070096 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -070097 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098{
Steve Blocka19954a2012-01-04 20:05:49 +000099 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
101 // debugging stuff...
102 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 property_get("debug.sf.showupdates", value, "0");
105 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700106
Colin Cross3854ed52012-03-23 14:17:18 -0700107#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 property_get("debug.sf.ddms", value, "0");
109 mDebugDDMS = atoi(value);
110 if (mDebugDDMS) {
111 DdmConnection::start(getServiceName());
112 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700113#else
114#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700115#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000118 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800121void SurfaceFlinger::onFirstRef()
122{
123 mEventQueue.init(this);
124
125 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
126
127 // Wait for the main thread to be done with its initialization
128 mReadyToRunBarrier.wait();
129}
130
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::~SurfaceFlinger()
133{
134 glDeleteTextures(1, &mWormholeTexName);
Mathias Agopiana4912602012-07-12 14:25:33 -0700135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
136 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
137 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::binderDied(const wp<IBinder>& who)
141{
142 // the window manager died on us. prepare its eulogy.
143
144 // reset screen orientation
145 Vector<ComposerState> state;
Mathias Agopian8b33f032012-07-24 20:43:54 -0700146 Vector<DisplayState> displays;
147 DisplayState d;
148 d.orientation = eOrientationDefault;
149 displays.add(d);
150 setTransactionState(state, displays, 0);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800151
152 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700153 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800154}
155
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700156sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159}
160
Mathias Agopian7e27f052010-05-28 14:22:23 -0700161sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Mathias Agopian96f08192010-06-02 23:28:45 -0700163 sp<ISurfaceComposerClient> bclient;
164 sp<Client> client(new Client(this));
165 status_t err = client->initCheck();
166 if (err == NO_ERROR) {
167 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 return bclient;
170}
171
Jamie Gennis9a78c902011-01-12 18:30:40 -0800172sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
173{
174 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
175 return gba;
176}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178void SurfaceFlinger::bootFinished()
179{
180 const nsecs_t now = systemTime();
181 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000182 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700183 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700184
185 // wait patiently for the window manager death
186 const String16 name("window");
187 sp<IBinder> window(defaultServiceManager()->getService(name));
188 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700189 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700190 }
191
192 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700193 // formerly we would just kill the process, but we now ask it to exit so it
194 // can choose where to stop the animation.
195 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196}
197
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700198void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
199 class MessageDestroyGLTexture : public MessageBase {
200 GLuint texture;
201 public:
202 MessageDestroyGLTexture(GLuint texture)
203 : texture(texture) {
204 }
205 virtual bool handler() {
206 glDeleteTextures(1, &texture);
207 return true;
208 }
209 };
210 postMessageAsync(new MessageDestroyGLTexture(texture));
211}
212
Mathias Agopiana4912602012-07-12 14:25:33 -0700213status_t SurfaceFlinger::selectConfigForPixelFormat(
214 EGLDisplay dpy,
215 EGLint const* attrs,
216 PixelFormat format,
217 EGLConfig* outConfig)
218{
219 EGLConfig config = NULL;
220 EGLint numConfigs = -1, n=0;
221 eglGetConfigs(dpy, NULL, 0, &numConfigs);
222 EGLConfig* const configs = new EGLConfig[numConfigs];
223 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
224 for (int i=0 ; i<n ; i++) {
225 EGLint nativeVisualId = 0;
226 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
227 if (nativeVisualId>0 && format == nativeVisualId) {
228 *outConfig = configs[i];
229 delete [] configs;
230 return NO_ERROR;
231 }
232 }
233 delete [] configs;
234 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235}
236
Mathias Agopiana4912602012-07-12 14:25:33 -0700237EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
238 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
239 // it is to be used with WIFI displays
240 EGLConfig config;
241 EGLint dummy;
242 status_t err;
243 EGLint attribs[] = {
244 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
245 EGL_RECORDABLE_ANDROID, EGL_TRUE,
246 EGL_NONE
247 };
248 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
249 if (err) {
250 // maybe we failed because of EGL_RECORDABLE_ANDROID
251 ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
252 attribs[2] = EGL_NONE;
253 err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
254 }
255 ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
256 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
257 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
258 }
259 return config;
260}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
Mathias Agopiana4912602012-07-12 14:25:33 -0700262EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
263 // Also create our EGLContext
264 EGLint contextAttributes[] = {
265#ifdef EGL_IMG_context_priority
266#ifdef HAS_CONTEXT_PRIORITY
267#warning "using EGL_IMG_context_priority"
268 EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
269#endif
270#endif
271 EGL_NONE, EGL_NONE
272 };
273 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
274 ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
275 return ctxt;
276}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277
Mathias Agopiana4912602012-07-12 14:25:33 -0700278void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
279 EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
280 if (!result) {
281 ALOGE("Couldn't create a working GLES context. check logs. exiting...");
282 exit(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 }
284
Mathias Agopiana4912602012-07-12 14:25:33 -0700285 GLExtensions& extensions(GLExtensions::getInstance());
286 extensions.initWithGLStrings(
287 glGetString(GL_VENDOR),
288 glGetString(GL_RENDERER),
289 glGetString(GL_VERSION),
290 glGetString(GL_EXTENSIONS),
291 eglQueryString(display, EGL_VENDOR),
292 eglQueryString(display, EGL_VERSION),
293 eglQueryString(display, EGL_EXTENSIONS));
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700294
Mathias Agopiana4912602012-07-12 14:25:33 -0700295 EGLint w, h;
296 eglQuerySurface(display, surface, EGL_WIDTH, &w);
297 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700298
Mathias Agopiana4912602012-07-12 14:25:33 -0700299 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
300 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700303 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 glShadeModel(GL_FLAT);
306 glDisable(GL_DITHER);
307 glDisable(GL_CULL_FACE);
308
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 struct pack565 {
310 inline uint16_t operator() (int r, int g, int b) const {
311 return (r<<11)|(g<<5)|b;
312 }
313 } pack565;
314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
316 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700317 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 glGenTextures(1, &mWormholeTexName);
319 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
320 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
321 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
322 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
323 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
324 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700325 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
326
327 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
328 glGenTextures(1, &mProtectedTexName);
329 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
330 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
331 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
332 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
333 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
334 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
335 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336
337 glViewport(0, 0, w, h);
338 glMatrixMode(GL_PROJECTION);
339 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700340 // put the origin in the left-bottom corner
341 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 -0800342
Mathias Agopiana4912602012-07-12 14:25:33 -0700343 // print some debugging info
344 EGLint r,g,b,a;
345 eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
346 eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
347 eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
348 eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
349 ALOGI("EGL informations:");
350 ALOGI("vendor : %s", extensions.getEglVendor());
351 ALOGI("version : %s", extensions.getEglVersion());
352 ALOGI("extensions: %s", extensions.getEglExtension());
353 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
354 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
355 ALOGI("OpenGL ES informations:");
356 ALOGI("vendor : %s", extensions.getVendor());
357 ALOGI("renderer : %s", extensions.getRenderer());
358 ALOGI("version : %s", extensions.getVersion());
359 ALOGI("extensions: %s", extensions.getExtension());
360 ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
361 ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
362}
363
364surface_flinger_cblk_t* SurfaceFlinger::getControlBlock() const {
365 return mServerCblk;
366}
367
368status_t SurfaceFlinger::readyToRun()
369{
370 ALOGI( "SurfaceFlinger's main thread ready to run. "
371 "Initializing graphics H/W...");
372
373 // create the shared control-block
374 mServerHeap = new MemoryHeapBase(4096,
375 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
376 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
377 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
378 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
379 new(mServerCblk) surface_flinger_cblk_t;
380
381
382 // initialize EGL
383 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
384 eglInitialize(display, NULL, NULL);
385
386 // Initialize the main display
387 // create native window to main display
388 sp<FramebufferSurface> anw = FramebufferSurface::create();
389 ANativeWindow* const window = anw.get();
390 if (!window) {
391 ALOGE("Display subsystem failed to initialize. check logs. exiting...");
392 exit(0);
393 }
394
395 // initialize the config and context
396 int format;
397 window->query(window, NATIVE_WINDOW_FORMAT, &format);
398 mEGLConfig = selectEGLConfig(display, format);
399 mEGLContext = createGLContext(display, mEGLConfig);
400
401 // initialize our main display hardware
402 DisplayHardware* const hw = new DisplayHardware(this, 0, anw, mEGLConfig);
403 mDisplayHardwares[0] = hw;
404
405 // initialize OpenGL ES
406 EGLSurface surface = hw->getEGLSurface();
407 initializeGL(display, surface);
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800408
409 // start the EventThread
410 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800411 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800414 mReadyToRunBarrier.open();
415
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700416 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700417 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 return NO_ERROR;
420}
421
Mathias Agopiana67e4182012-06-19 17:26:12 -0700422void SurfaceFlinger::startBootAnim() {
423 // start boot animation
424 property_set("service.bootanim.exit", "0");
425 property_set("ctl.start", "bootanim");
426}
427
Mathias Agopiana4912602012-07-12 14:25:33 -0700428uint32_t SurfaceFlinger::getMaxTextureSize() const {
429 return mMaxTextureSize;
430}
431
432uint32_t SurfaceFlinger::getMaxViewportDims() const {
433 return mMaxViewportDims[0] < mMaxViewportDims[1] ?
434 mMaxViewportDims[0] : mMaxViewportDims[1];
435}
436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800438
Jamie Gennis582270d2011-08-17 18:19:00 -0700439bool SurfaceFlinger::authenticateSurfaceTexture(
440 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800441 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700442 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800443
444 // Check the visible layer list for the ISurface
445 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
446 size_t count = currentLayers.size();
447 for (size_t i=0 ; i<count ; i++) {
448 const sp<LayerBase>& layer(currentLayers[i]);
449 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700450 if (lbc != NULL) {
451 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
452 if (lbcBinder == surfaceTextureBinder) {
453 return true;
454 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800455 }
456 }
457
458 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700459 // SurfaceTexture gets destroyed before all the clients are done using it,
460 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800461 // will instead fail later on when the client tries to use the surface,
462 // which should be reported as "surface XYZ returned an -ENODEV". The
463 // purgatorized layers are no less authentic than the visible ones, so this
464 // should not cause any harm.
465 size_t purgatorySize = mLayerPurgatory.size();
466 for (size_t i=0 ; i<purgatorySize ; i++) {
467 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
468 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700469 if (lbc != NULL) {
470 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
471 if (lbcBinder == surfaceTextureBinder) {
472 return true;
473 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800474 }
475 }
476
477 return false;
478}
479
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800480// ----------------------------------------------------------------------------
481
482sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800483 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700484}
485
Mathias Agopian3094df32012-06-18 18:06:45 -0700486void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700487 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700488 EGLSurface result = EGL_NO_SURFACE;
489 EGLSurface old_surface = EGL_NO_SURFACE;
490 sp<SurfaceTextureClient> stc;
491
492 if (display != NULL) {
493 stc = new SurfaceTextureClient(display);
494 result = eglCreateWindowSurface(hw.getEGLDisplay(),
Mathias Agopiana4912602012-07-12 14:25:33 -0700495 mEGLConfig, (EGLNativeWindowType)stc.get(), NULL);
Mathias Agopian3094df32012-06-18 18:06:45 -0700496 ALOGE_IF(result == EGL_NO_SURFACE,
497 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
498 display.get());
499 }
500
501 { // scope for the lock
502 Mutex::Autolock _l(mStateLock);
503 old_surface = mExternalDisplaySurface;
504 mExternalDisplayNativeWindow = stc;
505 mExternalDisplaySurface = result;
506 ALOGD("mExternalDisplaySurface = %p", result);
507 }
508
509 if (old_surface != EGL_NO_SURFACE) {
510 // Note: EGL allows to destroy an object while its current
511 // it will fail to become current next time though.
512 eglDestroySurface(hw.getEGLDisplay(), old_surface);
513 }
514}
515
516EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
517 Mutex::Autolock _l(mStateLock);
518 return mExternalDisplaySurface;
519}
520
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800522
523void SurfaceFlinger::waitForEvent() {
524 mEventQueue.waitMessage();
525}
526
527void SurfaceFlinger::signalTransaction() {
528 mEventQueue.invalidate();
529}
530
531void SurfaceFlinger::signalLayerUpdate() {
532 mEventQueue.invalidate();
533}
534
535void SurfaceFlinger::signalRefresh() {
536 mEventQueue.refresh();
537}
538
539status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
540 nsecs_t reltime, uint32_t flags) {
541 return mEventQueue.postMessage(msg, reltime);
542}
543
544status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
545 nsecs_t reltime, uint32_t flags) {
546 status_t res = mEventQueue.postMessage(msg, reltime);
547 if (res == NO_ERROR) {
548 msg->wait();
549 }
550 return res;
551}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552
Mathias Agopian4fec8732012-06-29 14:12:52 -0700553bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 return true;
556}
557
Mathias Agopian4fec8732012-06-29 14:12:52 -0700558void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800559 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800560 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700561 case MessageQueue::INVALIDATE:
562 handleMessageTransaction();
563 handleMessageInvalidate();
564 signalRefresh();
565 break;
566 case MessageQueue::REFRESH:
567 handleMessageRefresh();
568 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800569 }
570}
571
Mathias Agopian4fec8732012-06-29 14:12:52 -0700572void SurfaceFlinger::handleMessageTransaction() {
573 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
574 uint32_t transactionFlags = peekTransactionFlags(mask);
575 if (transactionFlags) {
576 Region dirtyRegion;
577 dirtyRegion = handleTransaction(transactionFlags);
578 // XXX: dirtyRegion should be per screen
579 mDirtyRegion |= dirtyRegion;
580 }
581}
582
583void SurfaceFlinger::handleMessageInvalidate() {
584 Region dirtyRegion;
585 dirtyRegion = handlePageFlip();
586 // XXX: dirtyRegion should be per screen
587 mDirtyRegion |= dirtyRegion;
588}
589
590void SurfaceFlinger::handleMessageRefresh() {
591 handleRefresh();
592
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700593 if (mVisibleRegionsDirty) {
594 Region opaqueRegion;
595 Region dirtyRegion;
596 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
597 computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
598 mDirtyRegion.orSelf(dirtyRegion);
599
600 /*
601 * rebuild the visible layer list per screen
602 */
603
604 // TODO: iterate through all displays
605 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
606
607 Vector< sp<LayerBase> > layersSortedByZ;
608 const size_t count = currentLayers.size();
609 for (size_t i=0 ; i<count ; i++) {
610 if (!currentLayers[i]->visibleRegion.isEmpty()) {
611 // TODO: also check that this layer is associated to this display
612 layersSortedByZ.add(currentLayers[i]);
613 }
614 }
615 hw.setVisibleLayersSortedByZ(layersSortedByZ);
616
617
618 // FIXME: mWormholeRegion needs to be calculated per screen
619 //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
620 mWormholeRegion = Region(hw.getBounds()).subtract(
621 hw.getTransform().transform(opaqueRegion) );
622 mVisibleRegionsDirty = false;
623 invalidateHwcGeometry();
624 }
625
626
Mathias Agopian4fec8732012-06-29 14:12:52 -0700627 // XXX: dirtyRegion should be per screen, we should check all of them
628 if (mDirtyRegion.isEmpty()) {
629 return;
630 }
631
632 // TODO: iterate through all displays
633 const DisplayHardware& hw(getDisplayHardware(0));
634
635 // XXX: dirtyRegion should be per screen
636 // transform the dirty region into this screen's coordinate space
637 const Transform& planeTransform(hw.getTransform());
638 mDirtyRegion = planeTransform.transform(mDirtyRegion);
639 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
640 mDirtyRegion.andSelf(hw.bounds());
641
642
643 if (CC_UNLIKELY(mHwWorkListDirty)) {
644 // build the h/w work list
645 handleWorkList(hw);
646 }
647
648 if (CC_LIKELY(hw.canDraw())) {
649 // repaint the framebuffer (if needed)
650 handleRepaint(hw);
651 // inform the h/w that we're done compositing
652 hw.compositionComplete();
653 postFramebuffer();
654 } else {
655 // pretend we did the post
656 hw.compositionComplete();
657 }
658
659 // render to the external display if we have one
660 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
661 if (externalDisplaySurface != EGL_NO_SURFACE) {
662 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
663 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
664 externalDisplaySurface, externalDisplaySurface,
665 eglGetCurrentContext());
666
667 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
668
669 if (success) {
670 // redraw the screen entirely...
671 glDisable(GL_TEXTURE_EXTERNAL_OES);
672 glDisable(GL_TEXTURE_2D);
673 glClearColor(0,0,0,1);
674 glClear(GL_COLOR_BUFFER_BIT);
675 glMatrixMode(GL_MODELVIEW);
676 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700677
678 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700679 const size_t count = layers.size();
680 for (size_t i=0 ; i<count ; ++i) {
681 const sp<LayerBase>& layer(layers[i]);
682 layer->drawForSreenShot(hw);
683 }
684
685 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
686 ALOGE_IF(!success, "external display eglSwapBuffers failed");
687
688 hw.compositionComplete();
689 }
690
691 success = eglMakeCurrent(eglGetCurrentDisplay(),
692 cur, cur, eglGetCurrentContext());
693
694 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
695 }
696
697}
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699void SurfaceFlinger::postFramebuffer()
700{
Mathias Agopian841cde52012-03-01 15:44:37 -0800701 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800702 // mSwapRegion can be empty here is some cases, for instance if a hidden
703 // or fully transparent window is updating.
704 // in that case, we need to flip anyways to not risk a deadlock with
705 // h/w composer.
706
Mathias Agopian1b031492012-06-20 17:51:20 -0700707 const DisplayHardware& hw(getDefaultDisplayHardware());
Jesse Hallc5c5a142012-07-02 16:49:28 -0700708 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700709 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
710 size_t numLayers = layers.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700711 const nsecs_t now = systemTime();
712 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700713
714 if (hwc.initCheck() == NO_ERROR) {
715 HWComposer::LayerListIterator cur = hwc.begin();
716 const HWComposer::LayerListIterator end = hwc.end();
717 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
718 if (cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700719 layers[i]->setAcquireFence(*cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700720 } else {
721 cur->setAcquireFenceFd(-1);
722 }
723 }
724 }
725
Mathias Agopiana44b0412011-10-16 18:46:35 -0700726 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800727
Jesse Hallef194142012-06-14 14:45:17 -0700728 if (hwc.initCheck() == NO_ERROR) {
729 HWComposer::LayerListIterator cur = hwc.begin();
730 const HWComposer::LayerListIterator end = hwc.end();
731 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700732 layers[i]->onLayerDisplayed(&*cur);
Jesse Hallef194142012-06-14 14:45:17 -0700733 }
734 } else {
735 for (size_t i = 0; i < numLayers; i++) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700736 layers[i]->onLayerDisplayed(NULL);
Jesse Hallef194142012-06-14 14:45:17 -0700737 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800738 }
739
Mathias Agopiana44b0412011-10-16 18:46:35 -0700740 mLastSwapBufferTime = systemTime() - now;
741 mDebugInSwapBuffers = 0;
742 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743}
744
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746{
Mathias Agopian841cde52012-03-01 15:44:37 -0800747 ATRACE_CALL();
748
Mathias Agopian4fec8732012-06-29 14:12:52 -0700749 Region dirtyRegion;
750
Mathias Agopianca4d3602011-05-19 15:38:14 -0700751 Mutex::Autolock _l(mStateLock);
752 const nsecs_t now = systemTime();
753 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754
Mathias Agopianca4d3602011-05-19 15:38:14 -0700755 // Here we're guaranteed that some transaction flags are set
756 // so we can call handleTransactionLocked() unconditionally.
757 // We call getTransactionFlags(), which will also clear the flags,
758 // with mStateLock held to guarantee that mCurrentState won't change
759 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700760
Mathias Agopianca4d3602011-05-19 15:38:14 -0700761 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
762 transactionFlags = getTransactionFlags(mask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700763 dirtyRegion = handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700764
Mathias Agopianca4d3602011-05-19 15:38:14 -0700765 mLastTransactionTime = systemTime() - now;
766 mDebugInTransaction = 0;
767 invalidateHwcGeometry();
768 // here the transaction has been committed
Mathias Agopian4fec8732012-06-29 14:12:52 -0700769
770 return dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700771}
772
Mathias Agopian4fec8732012-06-29 14:12:52 -0700773Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700774{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700775 Region dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700776 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 const size_t count = currentLayers.size();
778
779 /*
780 * Traversal of the children
781 * (perform the transaction for each of them if needed)
782 */
783
784 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
785 if (layersNeedTransaction) {
786 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700787 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
789 if (!trFlags) continue;
790
791 const uint32_t flags = layer->doTransaction(0);
792 if (flags & Layer::eVisibleRegion)
793 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 }
795 }
796
797 /*
798 * Perform our own transaction if needed
799 */
800
801 if (transactionFlags & eTransactionNeeded) {
802 if (mCurrentState.orientation != mDrawingState.orientation) {
803 // the orientation has changed, recompute all visible regions
804 // and invalidate everything.
805
Mathias Agopian1b031492012-06-20 17:51:20 -0700806 const int dpy = 0; // TODO: should be a parameter
807 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 const int orientation = mCurrentState.orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700809 hw.setOrientation(orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810
811 // update the shared control block
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
813 dcblk->orientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700814 dcblk->w = hw.getUserWidth();
815 dcblk->h = hw.getUserHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816
Mathias Agopian1b031492012-06-20 17:51:20 -0700817 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 mVisibleRegionsDirty = true;
819 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820 }
821
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700822 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
823 // layers have been added
824 mVisibleRegionsDirty = true;
825 }
826
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 // some layers might have been removed, so
828 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700829 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700830 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700832 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700833 const size_t count = previousLayers.size();
834 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700835 const sp<LayerBase>& layer(previousLayers[i]);
836 if (currentLayers.indexOf( layer ) < 0) {
837 // this layer is not visible anymore
Mathias Agopian4fec8732012-06-29 14:12:52 -0700838 // TODO: we could traverse the tree from front to back and compute the actual visible region
839 // TODO: we could cache the transformed region
840 Layer::State front(layer->drawingState());
841 Region visibleReg = front.transform.transform(
842 Region(Rect(front.active.w, front.active.h)));
843 dirtyRegion.orSelf(visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700844 }
845 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
848
849 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850 return dirtyRegion;
851}
852
853void SurfaceFlinger::commitTransaction()
854{
855 if (!mLayersPendingRemoval.isEmpty()) {
856 // Notify removed layers now that they can't be drawn from
857 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
858 mLayersPendingRemoval[i]->onRemoved();
859 }
860 mLayersPendingRemoval.clear();
861 }
862
863 mDrawingState = mCurrentState;
864 mTransationPending = false;
865 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866}
867
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800869 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870{
Mathias Agopian841cde52012-03-01 15:44:37 -0800871 ATRACE_CALL();
872
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 Region aboveOpaqueLayers;
874 Region aboveCoveredLayers;
875 Region dirty;
876
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700877 dirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878
879 size_t i = currentLayers.size();
880 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700881 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882
883 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700884 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885
Mathias Agopianab028732010-03-16 16:41:46 -0700886 /*
887 * opaqueRegion: area of a surface that is fully opaque.
888 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700890
891 /*
892 * visibleRegion: area of a surface that is visible on screen
893 * and not fully transparent. This is essentially the layer's
894 * footprint minus the opaque regions above it.
895 * Areas covered by a translucent surface are considered visible.
896 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700898
899 /*
900 * coveredRegion: area of a surface that is covered by all
901 * visible regions above it (which includes the translucent areas).
902 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700904
905
906 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800907 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700908 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700909 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700911 if (!visibleRegion.isEmpty()) {
912 // Remove the transparent area from the visible region
913 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700914 Region transparentRegionScreen;
915 const Transform tr(s.transform);
916 if (tr.transformed()) {
917 if (tr.preserveRects()) {
918 // transform the transparent region
919 transparentRegionScreen = tr.transform(s.transparentRegion);
920 } else {
921 // transformation too complex, can't do the
922 // transparent region optimization.
923 transparentRegionScreen.clear();
924 }
925 } else {
926 transparentRegionScreen = s.transparentRegion;
927 }
928 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700929 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930
Mathias Agopianab028732010-03-16 16:41:46 -0700931 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700932 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700933 if (s.alpha==255 && !translucent &&
934 ((layerOrientation & Transform::ROT_INVALID) == false)) {
935 // the opaque region is the layer's footprint
936 opaqueRegion = visibleRegion;
937 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 }
939 }
940
Mathias Agopianab028732010-03-16 16:41:46 -0700941 // Clip the covered region to the visible region
942 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
943
944 // Update aboveCoveredLayers for next (lower) layer
945 aboveCoveredLayers.orSelf(visibleRegion);
946
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 // subtract the opaque region covered by the layers above us
948 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949
950 // compute this layer's dirty region
951 if (layer->contentDirty) {
952 // we need to invalidate the whole region
953 dirty = visibleRegion;
954 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700955 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 layer->contentDirty = false;
957 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700958 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700959 * the exposed region consists of two components:
960 * 1) what's VISIBLE now and was COVERED before
961 * 2) what's EXPOSED now less what was EXPOSED before
962 *
963 * note that (1) is conservative, we start with the whole
964 * visible region but only keep what used to be covered by
965 * something -- which mean it may have been exposed.
966 *
967 * (2) handles areas that were not covered by anything but got
968 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700969 */
Mathias Agopianab028732010-03-16 16:41:46 -0700970 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700971 const Region oldVisibleRegion = layer->visibleRegion;
972 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700973 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
974 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 }
976 dirty.subtractSelf(aboveOpaqueLayers);
977
978 // accumulate to the screen dirty region
979 dirtyRegion.orSelf(dirty);
980
Mathias Agopianab028732010-03-16 16:41:46 -0700981 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700983
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 // Store the visible region is screen space
985 layer->setVisibleRegion(visibleRegion);
986 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 }
988
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 opaqueRegion = aboveOpaqueLayers;
990}
991
Mathias Agopian4fec8732012-06-29 14:12:52 -0700992Region SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800994 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700995 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800997 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800998
Mathias Agopian4fec8732012-06-29 14:12:52 -0700999 bool visibleRegions = false;
1000 const size_t count = currentLayers.size();
1001 sp<LayerBase> const* layers = currentLayers.array();
1002 for (size_t i=0 ; i<count ; i++) {
1003 const sp<LayerBase>& layer(layers[i]);
1004 dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
1005 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001006
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001007 mVisibleRegionsDirty |= visibleRegions;
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001008
Mathias Agopian4fec8732012-06-29 14:12:52 -07001009 return dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010}
1011
Mathias Agopianad456f92011-01-13 17:53:01 -08001012void SurfaceFlinger::invalidateHwcGeometry()
1013{
1014 mHwWorkListDirty = true;
1015}
1016
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001017void SurfaceFlinger::handleRefresh()
1018{
1019 bool needInvalidate = false;
1020 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
1021 const size_t count = currentLayers.size();
1022 for (size_t i=0 ; i<count ; i++) {
1023 const sp<LayerBase>& layer(currentLayers[i]);
1024 if (layer->onPreComposition()) {
1025 needInvalidate = true;
1026 }
1027 }
1028 if (needInvalidate) {
1029 signalLayerUpdate();
1030 }
1031}
1032
1033
Mathias Agopian1b031492012-06-20 17:51:20 -07001034void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -07001035{
1036 mHwWorkListDirty = false;
Mathias Agopian1b031492012-06-20 17:51:20 -07001037 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001038 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001039 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana350ff92010-08-10 17:14:02 -07001040 const size_t count = currentLayers.size();
1041 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001042
1043 HWComposer::LayerListIterator cur = hwc.begin();
1044 const HWComposer::LayerListIterator end = hwc.end();
1045 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001046 currentLayers[i]->setGeometry(hw, *cur);
Mathias Agopian53331da2011-08-22 21:44:41 -07001047 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001048 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001049 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001050 }
1051 }
1052}
Mathias Agopianb8a55602009-06-26 19:06:36 -07001053
Mathias Agopian1b031492012-06-20 17:51:20 -07001054void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055{
Mathias Agopian841cde52012-03-01 15:44:37 -08001056 ATRACE_CALL();
1057
Mathias Agopianb8a55602009-06-26 19:06:36 -07001058 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -07001059 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
Glenn Kasten99ed2242011-12-15 09:51:17 -08001061 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -07001062 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 }
1064
Mathias Agopianb8a55602009-06-26 19:06:36 -07001065 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -07001066 glMatrixMode(GL_MODELVIEW);
1067 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068
1069 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001070 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001071 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1072 // takes a rectangle, we must make sure to update that whole
1073 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001074 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001075 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -07001076 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001077 // We need to redraw the rectangle that will be updated
1078 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001079 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001080 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -07001081 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001082 } else {
1083 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -07001085 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 }
1087 }
1088
Mathias Agopian1b031492012-06-20 17:51:20 -07001089 setupHardwareComposer(hw);
1090 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001092 // update the swap region and clear the dirty region
1093 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 mDirtyRegion.clear();
1095}
1096
Mathias Agopian1b031492012-06-20 17:51:20 -07001097void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098{
Mathias Agopiana350ff92010-08-10 17:14:02 -07001099 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001100 HWComposer::LayerListIterator cur = hwc.begin();
1101 const HWComposer::LayerListIterator end = hwc.end();
1102 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001103 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -07001104 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001105
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001106 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001107 size_t count = layers.size();
1108
Steve Blocke6f43dd2012-01-06 19:20:56 +00001109 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -07001110 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
1111 hwc.getNumLayers(), count);
1112
1113 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -07001114 if (hwc.initCheck() == NO_ERROR) {
1115 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
1116 }
Mathias Agopian45721772010-08-12 15:03:26 -07001117
1118 /*
1119 * update the per-frame h/w composer data for each layer
1120 * and build the transparent region of the FB
1121 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001122 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001123 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001124 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -07001125 }
1126 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001127 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -07001128}
Mathias Agopian45721772010-08-12 15:03:26 -07001129
Mathias Agopian1b031492012-06-20 17:51:20 -07001130void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001131{
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001132 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001133 HWComposer::LayerListIterator cur = hwc.begin();
1134 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001135
1136 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001137 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001138 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -07001139
Mathias Agopianb9494d52012-04-18 02:28:45 -07001140 if (hwc.getLayerCount(HWC_OVERLAY)) {
1141 // when using overlays, we assume a fully transparent framebuffer
1142 // NOTE: we could reduce how much we need to clear, for instance
1143 // remove where there are opaque FB layers. however, on some
1144 // GPUs doing a "clean slate" glClear might be more efficient.
1145 // We'll revisit later if needed.
1146 glClearColor(0, 0, 0, 0);
1147 glClear(GL_COLOR_BUFFER_BIT);
1148 } else {
1149 // screen is already cleared here
1150 if (!mWormholeRegion.isEmpty()) {
1151 // can happen with SurfaceView
1152 drawWormhole();
1153 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001154 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001155
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001156 /*
1157 * and then, render the layers targeted at the framebuffer
1158 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001159
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001160 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001161 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001162 const Transform& tr = hw.getTransform();
Jesse Halla6b32db2012-07-19 16:44:38 -07001163 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001164 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001165 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001166 if (!clip.isEmpty()) {
Jesse Halla6b32db2012-07-19 16:44:38 -07001167 if (cur != end && cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001168 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001169 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001170 // never clear the very first layer since we're
1171 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001172 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001173 }
Jesse Hall6ee93c02012-07-23 13:11:19 -07001174 ++cur;
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001175 continue;
1176 }
1177 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001178 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001179 }
Jesse Halla6b32db2012-07-19 16:44:38 -07001180 if (cur != end) {
1181 ++cur;
1182 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001183 }
1184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185}
1186
Mathias Agopian1b031492012-06-20 17:51:20 -07001187void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188{
Mathias Agopian0a917752010-06-14 21:20:00 -07001189 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001190 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001191 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001192 return;
1193 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001194
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001195 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001196 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1197 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001198 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001199 }
1200
Mathias Agopianc492e672011-10-18 14:49:27 -07001201 glDisable(GL_TEXTURE_EXTERNAL_OES);
1202 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204
Mathias Agopian0926f502009-05-04 14:17:04 -07001205 static int toggle = 0;
1206 toggle = 1 - toggle;
1207 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001208 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001209 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001210 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001211 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212
Mathias Agopian20f68782009-05-11 00:03:41 -07001213 Region::const_iterator it = mDirtyRegion.begin();
1214 Region::const_iterator const end = mDirtyRegion.end();
1215 while (it != end) {
1216 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001218 { r.left, height - r.top },
1219 { r.left, height - r.bottom },
1220 { r.right, height - r.bottom },
1221 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 };
1223 glVertexPointer(2, GL_FLOAT, 0, vertices);
1224 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1225 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001226
Mathias Agopian0656a682011-09-20 17:22:44 -07001227 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228
1229 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001230 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231}
1232
1233void SurfaceFlinger::drawWormhole() const
1234{
1235 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1236 if (region.isEmpty())
1237 return;
1238
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001239 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001240 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001241 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001242 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001243
1244 GLfloat vertices[4][2];
1245 glVertexPointer(2, GL_FLOAT, 0, vertices);
1246 Region::const_iterator it = region.begin();
1247 Region::const_iterator const end = region.end();
1248 while (it != end) {
1249 const Rect& r = *it++;
1250 vertices[0][0] = r.left;
1251 vertices[0][1] = r.top;
1252 vertices[1][0] = r.right;
1253 vertices[1][1] = r.top;
1254 vertices[2][0] = r.right;
1255 vertices[2][1] = r.bottom;
1256 vertices[3][0] = r.left;
1257 vertices[3][1] = r.bottom;
1258 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260}
1261
Mathias Agopian96f08192010-06-02 23:28:45 -07001262ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1263 const sp<LayerBaseClient>& lbc)
1264{
Mathias Agopian96f08192010-06-02 23:28:45 -07001265 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001266 size_t name = client->attachLayer(lbc);
1267
Mathias Agopian96f08192010-06-02 23:28:45 -07001268 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001269 Mutex::Autolock _l(mStateLock);
1270 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian96f08192010-06-02 23:28:45 -07001271
Mathias Agopian4f113742011-05-03 16:21:41 -07001272 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001273}
1274
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001275status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276{
1277 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001278 status_t err = purgatorizeLayer_l(layer);
1279 if (err == NO_ERROR)
1280 setTransactionFlags(eTransactionNeeded);
1281 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282}
1283
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001284status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285{
1286 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1287 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001288 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 return NO_ERROR;
1290 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001291 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292}
1293
Mathias Agopian9a112062009-04-17 19:36:26 -07001294status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1295{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001296 // First add the layer to the purgatory list, which makes sure it won't
1297 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001298 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001299 if (err >= 0) {
1300 mLayerPurgatory.add(layerBase);
1301 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001302
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001303 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001304
Mathias Agopian3d579642009-06-04 18:46:21 -07001305 // it's possible that we don't find a layer, because it might
1306 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 // from the user because there is a race between Client::destroySurface(),
1308 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001309 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1310}
1311
Mathias Agopiandea20b12011-05-03 17:04:02 -07001312uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1313{
1314 return android_atomic_release_load(&mTransactionFlags);
1315}
1316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1318{
1319 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1320}
1321
Mathias Agopianbb641242010-05-18 17:06:55 -07001322uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323{
1324 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1325 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001326 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
1328 return old;
1329}
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331
Mathias Agopian8b33f032012-07-24 20:43:54 -07001332void SurfaceFlinger::setTransactionState(
1333 const Vector<ComposerState>& state,
1334 const Vector<DisplayState>& displays,
1335 uint32_t flags)
1336{
Mathias Agopian698c0872011-06-28 19:09:31 -07001337 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001338
Mathias Agopian8b33f032012-07-24 20:43:54 -07001339 int orientation = eOrientationUnchanged;
1340 if (displays.size()) {
1341 // TODO: handle all displays
1342 orientation = displays[0].orientation;
1343 }
1344
Jamie Gennis28378392011-10-12 17:39:00 -07001345 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001346 if (mCurrentState.orientation != orientation) {
1347 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1348 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001349 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001350 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001351 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001352 orientation);
1353 }
1354 }
1355
Mathias Agopian698c0872011-06-28 19:09:31 -07001356 const size_t count = state.size();
1357 for (size_t i=0 ; i<count ; i++) {
1358 const ComposerState& s(state[i]);
1359 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001360 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001361 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001362
Mathias Agopian386aa982011-11-07 21:58:03 -08001363 if (transactionFlags) {
1364 // this triggers the transaction
1365 setTransactionFlags(transactionFlags);
1366
1367 // if this is a synchronous transaction, wait for it to take effect
1368 // before returning.
1369 if (flags & eSynchronous) {
1370 mTransationPending = true;
1371 }
1372 while (mTransationPending) {
1373 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1374 if (CC_UNLIKELY(err != NO_ERROR)) {
1375 // just in case something goes wrong in SF, return to the
1376 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001377 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001378 mTransationPending = false;
1379 break;
1380 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001381 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 }
1383}
1384
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001385sp<ISurface> SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001386 ISurfaceComposerClient::surface_data_t* params,
1387 const String8& name,
1388 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1390 uint32_t flags)
1391{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001392 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001393 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001394
1395 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001396 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001397 int(w), int(h));
1398 return surfaceHandle;
1399 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001400
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001401 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 switch (flags & eFXSurfaceMask) {
1403 case eFXSurfaceNormal:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001404 layer = createNormalLayer(client, d, w, h, flags, format);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 break;
1406 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001407 // for now we treat Blur as Dim, until we can implement it
1408 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 case eFXSurfaceDim:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001410 layer = createDimLayer(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001412 case eFXSurfaceScreenshot:
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001413 layer = createScreenshotLayer(client, d, w, h, flags);
Mathias Agopian118d0242011-10-13 16:02:48 -07001414 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 }
1416
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001417 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001418 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001419 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001420 ssize_t token = addClientLayer(client, layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001422 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001423 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001424 params->identity = layer->getIdentity();
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001425 }
Mathias Agopian96f08192010-06-02 23:28:45 -07001426 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 }
1428
1429 return surfaceHandle;
1430}
1431
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001432sp<Layer> SurfaceFlinger::createNormalLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001433 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001434 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001435 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436{
1437 // initialize the surfaces
1438 switch (format) { // TODO: take h/w into account
1439 case PIXEL_FORMAT_TRANSPARENT:
1440 case PIXEL_FORMAT_TRANSLUCENT:
1441 format = PIXEL_FORMAT_RGBA_8888;
1442 break;
1443 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001444#ifdef NO_RGBX_8888
1445 format = PIXEL_FORMAT_RGB_565;
1446#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001447 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001448#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449 break;
1450 }
1451
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001452#ifdef NO_RGBX_8888
1453 if (format == PIXEL_FORMAT_RGBX_8888)
1454 format = PIXEL_FORMAT_RGBA_8888;
1455#endif
1456
Mathias Agopian96f08192010-06-02 23:28:45 -07001457 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001458 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001459 if (CC_LIKELY(err != NO_ERROR)) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001460 ALOGE("createNormalLayer() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001461 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 }
1463 return layer;
1464}
1465
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001466sp<LayerDim> SurfaceFlinger::createDimLayer(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001467 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001468 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469{
Mathias Agopian96f08192010-06-02 23:28:45 -07001470 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001471 return layer;
1472}
1473
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001474sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
Mathias Agopian118d0242011-10-13 16:02:48 -07001475 const sp<Client>& client, DisplayID display,
1476 uint32_t w, uint32_t h, uint32_t flags)
1477{
1478 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 return layer;
1480}
1481
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001482status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483{
Mathias Agopian9a112062009-04-17 19:36:26 -07001484 /*
1485 * called by the window manager, when a surface should be marked for
1486 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001487 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001488 * The surface is removed from the current and drawing lists, but placed
1489 * in the purgatory queue, so it's not destroyed right-away (we need
1490 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001491 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001492
Mathias Agopian48d819a2009-09-10 19:41:18 -07001493 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001494 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001495 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001496
Mathias Agopian48d819a2009-09-10 19:41:18 -07001497 if (layer != 0) {
1498 err = purgatorizeLayer_l(layer);
1499 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001500 setTransactionFlags(eTransactionNeeded);
1501 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001502 }
1503 return err;
1504}
1505
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001506status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001507{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001508 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001509 status_t err = NO_ERROR;
1510 sp<LayerBaseClient> l(layer.promote());
1511 if (l != NULL) {
1512 Mutex::Autolock _l(mStateLock);
1513 err = removeLayer_l(l);
1514 if (err == NAME_NOT_FOUND) {
1515 // The surface wasn't in the current list, which means it was
1516 // removed already, which means it is in the purgatory,
1517 // and need to be removed from there.
1518 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001519 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001520 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001521 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001522 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001523 "error removing layer=%p (%s)", l.get(), strerror(-err));
1524 }
1525 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526}
1527
Mathias Agopian698c0872011-06-28 19:09:31 -07001528uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001529 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001530 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001533 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1534 if (layer != 0) {
1535 const uint32_t what = s.what;
1536 if (what & ePositionChanged) {
1537 if (layer->setPosition(s.x, s.y))
1538 flags |= eTraversalNeeded;
1539 }
1540 if (what & eLayerChanged) {
1541 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1542 if (layer->setLayer(s.z)) {
1543 mCurrentState.layersSortedByZ.removeAt(idx);
1544 mCurrentState.layersSortedByZ.add(layer);
1545 // we need traversal (state changed)
1546 // AND transaction (list changed)
1547 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 }
1549 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001550 if (what & eSizeChanged) {
1551 if (layer->setSize(s.w, s.h)) {
1552 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001553 }
1554 }
1555 if (what & eAlphaChanged) {
1556 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1557 flags |= eTraversalNeeded;
1558 }
1559 if (what & eMatrixChanged) {
1560 if (layer->setMatrix(s.matrix))
1561 flags |= eTraversalNeeded;
1562 }
1563 if (what & eTransparentRegionChanged) {
1564 if (layer->setTransparentRegionHint(s.transparentRegion))
1565 flags |= eTraversalNeeded;
1566 }
1567 if (what & eVisibilityChanged) {
1568 if (layer->setFlags(s.flags, s.mask))
1569 flags |= eTraversalNeeded;
1570 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001571 if (what & eCropChanged) {
1572 if (layer->setCrop(s.crop))
1573 flags |= eTraversalNeeded;
1574 }
Mathias Agopian87855782012-07-24 21:41:09 -07001575 if (what & eLayerStackChanged) {
1576 if (layer->setLayerStack(s.layerStack))
1577 flags |= eTraversalNeeded;
1578 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001580 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581}
1582
Mathias Agopianb60314a2012-04-10 22:09:54 -07001583// ---------------------------------------------------------------------------
1584
1585void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001586 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001587 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001588 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001589 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001590 // this is a temporary work-around, eventually this should be called
1591 // by the power-manager
1592 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001593 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001594 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595}
1596
Mathias Agopianb60314a2012-04-10 22:09:54 -07001597void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001598 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001599 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001600 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001601 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001602 hw.releaseScreen();
1603 // from this point on, SF will stop drawing
1604 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605}
1606
Colin Cross8e533062012-06-07 13:17:52 -07001607void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001608 class MessageScreenAcquired : public MessageBase {
1609 SurfaceFlinger* flinger;
1610 public:
1611 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1612 virtual bool handler() {
1613 flinger->onScreenAcquired();
1614 return true;
1615 }
1616 };
1617 sp<MessageBase> msg = new MessageScreenAcquired(this);
1618 postMessageSync(msg);
1619}
1620
Colin Cross8e533062012-06-07 13:17:52 -07001621void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001622 class MessageScreenReleased : public MessageBase {
1623 SurfaceFlinger* flinger;
1624 public:
1625 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1626 virtual bool handler() {
1627 flinger->onScreenReleased();
1628 return true;
1629 }
1630 };
1631 sp<MessageBase> msg = new MessageScreenReleased(this);
1632 postMessageSync(msg);
1633}
1634
1635// ---------------------------------------------------------------------------
1636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1638{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001639 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640 char buffer[SIZE];
1641 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001642
1643 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 snprintf(buffer, SIZE, "Permission Denial: "
1645 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1646 IPCThreadState::self()->getCallingPid(),
1647 IPCThreadState::self()->getCallingUid());
1648 result.append(buffer);
1649 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001650 // Try to get the main lock, but don't insist if we can't
1651 // (this would indicate SF is stuck, but we want to be able to
1652 // print something in dumpsys).
1653 int retry = 3;
1654 while (mStateLock.tryLock()<0 && --retry>=0) {
1655 usleep(1000000);
1656 }
1657 const bool locked(retry >= 0);
1658 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001659 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001660 "SurfaceFlinger appears to be unresponsive, "
1661 "dumping anyways (no locks held)\n");
1662 result.append(buffer);
1663 }
1664
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001665 bool dumpAll = true;
1666 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001667 size_t numArgs = args.size();
1668 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001669 if ((index < numArgs) &&
1670 (args[index] == String16("--list"))) {
1671 index++;
1672 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001673 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001674 }
1675
1676 if ((index < numArgs) &&
1677 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001678 index++;
1679 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001680 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001681 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001682
1683 if ((index < numArgs) &&
1684 (args[index] == String16("--latency-clear"))) {
1685 index++;
1686 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001687 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001688 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001690
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001691 if (dumpAll) {
1692 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001693 }
1694
Mathias Agopian9795c422009-08-26 16:36:26 -07001695 if (locked) {
1696 mStateLock.unlock();
1697 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698 }
1699 write(fd, result.string(), result.size());
1700 return NO_ERROR;
1701}
1702
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001703void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1704 String8& result, char* buffer, size_t SIZE) const
1705{
1706 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1707 const size_t count = currentLayers.size();
1708 for (size_t i=0 ; i<count ; i++) {
1709 const sp<LayerBase>& layer(currentLayers[i]);
1710 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1711 result.append(buffer);
1712 }
1713}
1714
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001715void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1716 String8& result, char* buffer, size_t SIZE) const
1717{
1718 String8 name;
1719 if (index < args.size()) {
1720 name = String8(args[index]);
1721 index++;
1722 }
1723
1724 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1725 const size_t count = currentLayers.size();
1726 for (size_t i=0 ; i<count ; i++) {
1727 const sp<LayerBase>& layer(currentLayers[i]);
1728 if (name.isEmpty()) {
1729 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1730 result.append(buffer);
1731 }
1732 if (name.isEmpty() || (name == layer->getName())) {
1733 layer->dumpStats(result, buffer, SIZE);
1734 }
1735 }
1736}
1737
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001738void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1739 String8& result, char* buffer, size_t SIZE) const
1740{
1741 String8 name;
1742 if (index < args.size()) {
1743 name = String8(args[index]);
1744 index++;
1745 }
1746
1747 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1748 const size_t count = currentLayers.size();
1749 for (size_t i=0 ; i<count ; i++) {
1750 const sp<LayerBase>& layer(currentLayers[i]);
1751 if (name.isEmpty() || (name == layer->getName())) {
1752 layer->clearStats();
1753 }
1754 }
1755}
1756
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001757void SurfaceFlinger::dumpAllLocked(
1758 String8& result, char* buffer, size_t SIZE) const
1759{
1760 // figure out if we're stuck somewhere
1761 const nsecs_t now = systemTime();
1762 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1763 const nsecs_t inTransaction(mDebugInTransaction);
1764 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1765 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1766
1767 /*
1768 * Dump the visible layer list
1769 */
1770 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1771 const size_t count = currentLayers.size();
1772 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1773 result.append(buffer);
1774 for (size_t i=0 ; i<count ; i++) {
1775 const sp<LayerBase>& layer(currentLayers[i]);
1776 layer->dump(result, buffer, SIZE);
1777 }
1778
1779 /*
1780 * Dump the layers in the purgatory
1781 */
1782
1783 const size_t purgatorySize = mLayerPurgatory.size();
1784 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1785 result.append(buffer);
1786 for (size_t i=0 ; i<purgatorySize ; i++) {
1787 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1788 layer->shortDump(result, buffer, SIZE);
1789 }
1790
1791 /*
1792 * Dump SurfaceFlinger global state
1793 */
1794
1795 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1796 result.append(buffer);
1797
Mathias Agopian1b031492012-06-20 17:51:20 -07001798 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001799 const GLExtensions& extensions(GLExtensions::getInstance());
1800 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1801 extensions.getVendor(),
1802 extensions.getRenderer(),
1803 extensions.getVersion());
1804 result.append(buffer);
1805
1806 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001807 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001808 EGL_VERSION_HW_ANDROID));
1809 result.append(buffer);
1810
1811 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1812 result.append(buffer);
1813
1814 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001815 snprintf(buffer, SIZE,
1816 " orientation=%d, canDraw=%d\n",
1817 mCurrentState.orientation, hw.canDraw());
1818 result.append(buffer);
1819 snprintf(buffer, SIZE,
1820 " last eglSwapBuffers() time: %f us\n"
1821 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001822 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001823 " refresh-rate : %f fps\n"
1824 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001825 " y-dpi : %f\n"
1826 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001827 mLastSwapBufferTime/1000.0,
1828 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001829 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001830 hw.getRefreshRate(),
1831 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001832 hw.getDpiY(),
1833 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001834 result.append(buffer);
1835
1836 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1837 inSwapBuffersDuration/1000.0);
1838 result.append(buffer);
1839
1840 snprintf(buffer, SIZE, " transaction time: %f us\n",
1841 inTransactionDuration/1000.0);
1842 result.append(buffer);
1843
1844 /*
1845 * VSYNC state
1846 */
1847 mEventThread->dump(result, buffer, SIZE);
1848
1849 /*
1850 * Dump HWComposer state
1851 */
1852 HWComposer& hwc(hw.getHwComposer());
1853 snprintf(buffer, SIZE, "h/w composer state:\n");
1854 result.append(buffer);
1855 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1856 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1857 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1858 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001859 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001860
1861 /*
1862 * Dump gralloc state
1863 */
1864 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1865 alloc.dump(result);
1866 hw.dump(result);
1867}
1868
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001869status_t SurfaceFlinger::onTransact(
1870 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1871{
1872 switch (code) {
1873 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001874 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001876 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001877 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001878 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001879 case BLANK:
1880 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 {
1882 // codes that require permission check
1883 IPCThreadState* ipc = IPCThreadState::self();
1884 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001885 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001886 if ((uid != AID_GRAPHICS) &&
1887 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001888 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001889 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1890 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001892 break;
1893 }
1894 case CAPTURE_SCREEN:
1895 {
1896 // codes that require permission check
1897 IPCThreadState* ipc = IPCThreadState::self();
1898 const int pid = ipc->getCallingPid();
1899 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001900 if ((uid != AID_GRAPHICS) &&
1901 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001902 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001903 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1904 return PERMISSION_DENIED;
1905 }
1906 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 }
1908 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001909
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1911 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001912 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001913 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001914 IPCThreadState* ipc = IPCThreadState::self();
1915 const int pid = ipc->getCallingPid();
1916 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001917 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001918 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 return PERMISSION_DENIED;
1920 }
1921 int n;
1922 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001923 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001924 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 return NO_ERROR;
1926 case 1002: // SHOW_UPDATES
1927 n = data.readInt32();
1928 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001929 invalidateHwcGeometry();
1930 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001932 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001933 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001934 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001936 case 1005:{ // force transaction
1937 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1938 return NO_ERROR;
1939 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001940 case 1006:{ // send empty update
1941 signalRefresh();
1942 return NO_ERROR;
1943 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001944 case 1008: // toggle use of hw composer
1945 n = data.readInt32();
1946 mDebugDisableHWC = n ? 1 : 0;
1947 invalidateHwcGeometry();
1948 repaintEverything();
1949 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001950 case 1009: // toggle use of transform hint
1951 n = data.readInt32();
1952 mDebugDisableTransformHint = n ? 1 : 0;
1953 invalidateHwcGeometry();
1954 repaintEverything();
1955 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001957 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958 reply->writeInt32(0);
1959 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001960 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001961 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962 return NO_ERROR;
1963 case 1013: {
1964 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001965 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001966 reply->writeInt32(hw.getPageFlipCount());
1967 }
1968 return NO_ERROR;
1969 }
1970 }
1971 return err;
1972}
1973
Mathias Agopian53331da2011-08-22 21:44:41 -07001974void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001975 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001976 const Rect bounds(hw.getBounds());
1977 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001978 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001979}
1980
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001981void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1982 Mutex::Autolock _l(mInvalidateLock);
1983 mInvalidateRegion = reg;
1984}
1985
1986Region SurfaceFlinger::getAndClearInvalidateRegion() {
1987 Mutex::Autolock _l(mInvalidateLock);
1988 Region reg(mInvalidateRegion);
1989 mInvalidateRegion.clear();
1990 return reg;
1991}
1992
Mathias Agopian59119e62010-10-11 12:37:43 -07001993// ---------------------------------------------------------------------------
1994
Mathias Agopian118d0242011-10-13 16:02:48 -07001995status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1996 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1997{
1998 Mutex::Autolock _l(mStateLock);
1999 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
2000}
2001
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
2003 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07002004{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002005 ATRACE_CALL();
2006
Mathias Agopian59119e62010-10-11 12:37:43 -07002007 if (!GLExtensions::getInstance().haveFramebufferObject())
2008 return INVALID_OPERATION;
2009
2010 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002011 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07002012 const uint32_t hw_w = hw.getWidth();
2013 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07002014 GLfloat u = 1;
2015 GLfloat v = 1;
2016
2017 // make sure to clear all GL error flags
2018 while ( glGetError() != GL_NO_ERROR ) ;
2019
2020 // create a FBO
2021 GLuint name, tname;
2022 glGenTextures(1, &tname);
2023 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002024 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2025 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2027 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002028 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002029 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07002030 GLint tw = (2 << (31 - clz(hw_w)));
2031 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002032 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
2033 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002034 u = GLfloat(hw_w) / tw;
2035 v = GLfloat(hw_h) / th;
2036 }
2037 glGenFramebuffersOES(1, &name);
2038 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2040 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07002041
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002042 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07002043 glDisable(GL_TEXTURE_EXTERNAL_OES);
2044 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 glClearColor(0,0,0,1);
2046 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002047 glMatrixMode(GL_MODELVIEW);
2048 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002049 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002050 const size_t count = layers.size();
2051 for (size_t i=0 ; i<count ; ++i) {
2052 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07002053 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 }
2055
Mathias Agopian118d0242011-10-13 16:02:48 -07002056 hw.compositionComplete();
2057
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002058 // back to main framebuffer
2059 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002060 glDeleteFramebuffersOES(1, &name);
2061
2062 *textureName = tname;
2063 *uOut = u;
2064 *vOut = v;
2065 return NO_ERROR;
2066}
2067
2068// ---------------------------------------------------------------------------
2069
Mathias Agopianed9807b2012-05-18 14:18:08 -07002070class VSyncWaiter {
2071 DisplayEventReceiver::Event buffer[4];
2072 sp<Looper> looper;
2073 sp<IDisplayEventConnection> events;
2074 sp<BitTube> eventTube;
2075public:
2076 VSyncWaiter(const sp<EventThread>& eventThread) {
2077 looper = new Looper(true);
2078 events = eventThread->createEventConnection();
2079 eventTube = events->getDataChannel();
2080 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
2081 events->requestNextVsync();
2082 }
2083
2084 void wait() {
2085 ssize_t n;
2086
2087 looper->pollOnce(-1);
2088 // we don't handle any errors here, it doesn't matter
2089 // and we don't want to take the risk to get stuck.
2090
2091 // drain the events...
2092 while ((n = DisplayEventReceiver::getEvents(
2093 eventTube, buffer, 4)) > 0) ;
2094
2095 events->requestNextVsync();
2096 }
2097};
2098
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002099status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
2100{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002101 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002102 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002103 const uint32_t hw_w = hw.getWidth();
2104 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07002105 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106
2107 GLfloat u, v;
2108 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002109 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002110 if (result != NO_ERROR) {
2111 return result;
2112 }
2113
2114 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002115 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002116 glBindTexture(GL_TEXTURE_2D, tname);
2117 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2118 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2119 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002120 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2121 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002122 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2123 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2124 glVertexPointer(2, GL_FLOAT, 0, vtx);
2125
Mathias Agopianffcf4652011-07-07 17:30:31 -07002126 /*
2127 * Texture coordinate mapping
2128 *
2129 * u
2130 * 1 +----------+---+
2131 * | | | | image is inverted
2132 * | V | | w.r.t. the texture
2133 * 1-v +----------+ | coordinates
2134 * | |
2135 * | |
2136 * | |
2137 * 0 +--------------+
2138 * 0 1
2139 *
2140 */
2141
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002142 class s_curve_interpolator {
2143 const float nbFrames, s, v;
2144 public:
2145 s_curve_interpolator(int nbFrames, float s)
2146 : nbFrames(1.0f / (nbFrames-1)), s(s),
2147 v(1.0f + expf(-s + 0.5f*s)) {
2148 }
2149 float operator()(int f) {
2150 const float x = f * nbFrames;
2151 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2152 }
2153 };
2154
2155 class v_stretch {
2156 const GLfloat hw_w, hw_h;
2157 public:
2158 v_stretch(uint32_t hw_w, uint32_t hw_h)
2159 : hw_w(hw_w), hw_h(hw_h) {
2160 }
2161 void operator()(GLfloat* vtx, float v) {
2162 const GLfloat w = hw_w + (hw_w * v);
2163 const GLfloat h = hw_h - (hw_h * v);
2164 const GLfloat x = (hw_w - w) * 0.5f;
2165 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002166 vtx[0] = x; vtx[1] = y;
2167 vtx[2] = x; vtx[3] = y + h;
2168 vtx[4] = x + w; vtx[5] = y + h;
2169 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002170 }
2171 };
2172
2173 class h_stretch {
2174 const GLfloat hw_w, hw_h;
2175 public:
2176 h_stretch(uint32_t hw_w, uint32_t hw_h)
2177 : hw_w(hw_w), hw_h(hw_h) {
2178 }
2179 void operator()(GLfloat* vtx, float v) {
2180 const GLfloat w = hw_w - (hw_w * v);
2181 const GLfloat h = 1.0f;
2182 const GLfloat x = (hw_w - w) * 0.5f;
2183 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002184 vtx[0] = x; vtx[1] = y;
2185 vtx[2] = x; vtx[3] = y + h;
2186 vtx[4] = x + w; vtx[5] = y + h;
2187 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 }
2189 };
2190
Mathias Agopianed9807b2012-05-18 14:18:08 -07002191 VSyncWaiter vsync(mEventThread);
2192
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002193 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002194 char value[PROPERTY_VALUE_MAX];
2195 property_get("debug.sf.electron_frames", value, "24");
2196 int nbFrames = (atoi(value) + 1) >> 1;
2197 if (nbFrames <= 0) // just in case
2198 nbFrames = 24;
2199
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 s_curve_interpolator itr(nbFrames, 7.5f);
2201 s_curve_interpolator itg(nbFrames, 8.0f);
2202 s_curve_interpolator itb(nbFrames, 8.5f);
2203
2204 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002205
2206 glMatrixMode(GL_TEXTURE);
2207 glLoadIdentity();
2208 glMatrixMode(GL_MODELVIEW);
2209 glLoadIdentity();
2210
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002211 glEnable(GL_BLEND);
2212 glBlendFunc(GL_ONE, GL_ONE);
2213 for (int i=0 ; i<nbFrames ; i++) {
2214 float x, y, w, h;
2215 const float vr = itr(i);
2216 const float vg = itg(i);
2217 const float vb = itb(i);
2218
Mathias Agopianed9807b2012-05-18 14:18:08 -07002219 // wait for vsync
2220 vsync.wait();
2221
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002222 // clear screen
2223 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002224 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002225 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002226
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002227 // draw the red plane
2228 vverts(vtx, vr);
2229 glColorMask(1,0,0,1);
2230 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002231
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002232 // draw the green plane
2233 vverts(vtx, vg);
2234 glColorMask(0,1,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 blue plane
2238 vverts(vtx, vb);
2239 glColorMask(0,0,1,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 white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002243 glDisable(GL_TEXTURE_2D);
2244 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002245 glColor4f(vg, vg, vg, 1);
2246 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2247 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 }
2249
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002250 h_stretch hverts(hw_w, hw_h);
2251 glDisable(GL_BLEND);
2252 glDisable(GL_TEXTURE_2D);
2253 glColorMask(1,1,1,1);
2254 for (int i=0 ; i<nbFrames ; i++) {
2255 const float v = itg(i);
2256 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002257
2258 // wait for vsync
2259 vsync.wait();
2260
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002261 glClear(GL_COLOR_BUFFER_BIT);
2262 glColor4f(1-v, 1-v, 1-v, 1);
2263 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2264 hw.flip(screenBounds);
2265 }
2266
2267 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002268 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2269 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002270 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002271 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002272 return NO_ERROR;
2273}
2274
2275status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2276{
2277 status_t result = PERMISSION_DENIED;
2278
2279 if (!GLExtensions::getInstance().haveFramebufferObject())
2280 return INVALID_OPERATION;
2281
2282
2283 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002284 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002285 const uint32_t hw_w = hw.getWidth();
2286 const uint32_t hw_h = hw.getHeight();
2287 const Region screenBounds(hw.bounds());
2288
2289 GLfloat u, v;
2290 GLuint tname;
2291 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2292 if (result != NO_ERROR) {
2293 return result;
2294 }
2295
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002296 GLfloat vtx[8];
2297 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002298 glBindTexture(GL_TEXTURE_2D, tname);
2299 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2300 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2301 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002302 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2303 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002304 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2305 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2306 glVertexPointer(2, GL_FLOAT, 0, vtx);
2307
2308 class s_curve_interpolator {
2309 const float nbFrames, s, v;
2310 public:
2311 s_curve_interpolator(int nbFrames, float s)
2312 : nbFrames(1.0f / (nbFrames-1)), s(s),
2313 v(1.0f + expf(-s + 0.5f*s)) {
2314 }
2315 float operator()(int f) {
2316 const float x = f * nbFrames;
2317 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2318 }
2319 };
2320
2321 class v_stretch {
2322 const GLfloat hw_w, hw_h;
2323 public:
2324 v_stretch(uint32_t hw_w, uint32_t hw_h)
2325 : hw_w(hw_w), hw_h(hw_h) {
2326 }
2327 void operator()(GLfloat* vtx, float v) {
2328 const GLfloat w = hw_w + (hw_w * v);
2329 const GLfloat h = hw_h - (hw_h * v);
2330 const GLfloat x = (hw_w - w) * 0.5f;
2331 const GLfloat y = (hw_h - h) * 0.5f;
2332 vtx[0] = x; vtx[1] = y;
2333 vtx[2] = x; vtx[3] = y + h;
2334 vtx[4] = x + w; vtx[5] = y + h;
2335 vtx[6] = x + w; vtx[7] = y;
2336 }
2337 };
2338
2339 class h_stretch {
2340 const GLfloat hw_w, hw_h;
2341 public:
2342 h_stretch(uint32_t hw_w, uint32_t hw_h)
2343 : hw_w(hw_w), hw_h(hw_h) {
2344 }
2345 void operator()(GLfloat* vtx, float v) {
2346 const GLfloat w = hw_w - (hw_w * v);
2347 const GLfloat h = 1.0f;
2348 const GLfloat x = (hw_w - w) * 0.5f;
2349 const GLfloat y = (hw_h - h) * 0.5f;
2350 vtx[0] = x; vtx[1] = y;
2351 vtx[2] = x; vtx[3] = y + h;
2352 vtx[4] = x + w; vtx[5] = y + h;
2353 vtx[6] = x + w; vtx[7] = y;
2354 }
2355 };
2356
Mathias Agopianed9807b2012-05-18 14:18:08 -07002357 VSyncWaiter vsync(mEventThread);
2358
Mathias Agopiana6546e52010-10-14 12:33:07 -07002359 // the full animation is 12 frames
2360 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002361 s_curve_interpolator itr(nbFrames, 7.5f);
2362 s_curve_interpolator itg(nbFrames, 8.0f);
2363 s_curve_interpolator itb(nbFrames, 8.5f);
2364
2365 h_stretch hverts(hw_w, hw_h);
2366 glDisable(GL_BLEND);
2367 glDisable(GL_TEXTURE_2D);
2368 glColorMask(1,1,1,1);
2369 for (int i=nbFrames-1 ; i>=0 ; i--) {
2370 const float v = itg(i);
2371 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002372
2373 // wait for vsync
2374 vsync.wait();
2375
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002376 glClear(GL_COLOR_BUFFER_BIT);
2377 glColor4f(1-v, 1-v, 1-v, 1);
2378 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2379 hw.flip(screenBounds);
2380 }
2381
Mathias Agopiana6546e52010-10-14 12:33:07 -07002382 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002383 v_stretch vverts(hw_w, hw_h);
2384 glEnable(GL_BLEND);
2385 glBlendFunc(GL_ONE, GL_ONE);
2386 for (int i=nbFrames-1 ; i>=0 ; i--) {
2387 float x, y, w, h;
2388 const float vr = itr(i);
2389 const float vg = itg(i);
2390 const float vb = itb(i);
2391
Mathias Agopianed9807b2012-05-18 14:18:08 -07002392 // wait for vsync
2393 vsync.wait();
2394
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002395 // clear screen
2396 glColorMask(1,1,1,1);
2397 glClear(GL_COLOR_BUFFER_BIT);
2398 glEnable(GL_TEXTURE_2D);
2399
2400 // draw the red plane
2401 vverts(vtx, vr);
2402 glColorMask(1,0,0,1);
2403 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2404
2405 // draw the green plane
2406 vverts(vtx, vg);
2407 glColorMask(0,1,0,1);
2408 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2409
2410 // draw the blue plane
2411 vverts(vtx, vb);
2412 glColorMask(0,0,1,1);
2413 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2414
2415 hw.flip(screenBounds);
2416 }
2417
2418 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002419 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002420 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002421 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002422 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002423
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002424 return NO_ERROR;
2425}
2426
2427// ---------------------------------------------------------------------------
2428
Mathias Agopianabd671a2010-10-14 14:54:06 -07002429status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002430{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002431 ATRACE_CALL();
2432
Mathias Agopian1b031492012-06-20 17:51:20 -07002433 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002434 if (!hw.canDraw()) {
2435 // we're already off
2436 return NO_ERROR;
2437 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002438
2439 // turn off hwc while we're doing the animation
2440 hw.getHwComposer().disable();
2441 // and make sure to turn it back on (if needed) next time we compose
2442 invalidateHwcGeometry();
2443
Mathias Agopianabd671a2010-10-14 14:54:06 -07002444 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2445 electronBeamOffAnimationImplLocked();
2446 }
2447
2448 // always clear the whole screen at the end of the animation
2449 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002450 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002451 hw.flip( Region(hw.bounds()) );
2452
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002453 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002454}
2455
2456status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2457{
Mathias Agopian59119e62010-10-11 12:37:43 -07002458 class MessageTurnElectronBeamOff : public MessageBase {
2459 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002460 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002461 status_t result;
2462 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002463 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2464 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002465 }
2466 status_t getResult() const {
2467 return result;
2468 }
2469 virtual bool handler() {
2470 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002471 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002472 return true;
2473 }
2474 };
2475
Mathias Agopianabd671a2010-10-14 14:54:06 -07002476 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002477 status_t res = postMessageSync(msg);
2478 if (res == NO_ERROR) {
2479 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002480
2481 // work-around: when the power-manager calls us we activate the
2482 // animation. eventually, the "on" animation will be called
2483 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002484 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002485 }
2486 return res;
2487}
2488
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002490
Mathias Agopianabd671a2010-10-14 14:54:06 -07002491status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002492{
Mathias Agopian1b031492012-06-20 17:51:20 -07002493 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002494 if (hw.canDraw()) {
2495 // we're already on
2496 return NO_ERROR;
2497 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002498 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2499 electronBeamOnAnimationImplLocked();
2500 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002501
2502 // make sure to redraw the whole screen when the animation is done
2503 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002504 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002505
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002506 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002507}
2508
2509status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2510{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002511 class MessageTurnElectronBeamOn : public MessageBase {
2512 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002513 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002514 status_t result;
2515 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002516 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2517 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002518 }
2519 status_t getResult() const {
2520 return result;
2521 }
2522 virtual bool handler() {
2523 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002524 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002525 return true;
2526 }
2527 };
2528
Mathias Agopianabd671a2010-10-14 14:54:06 -07002529 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002530 return NO_ERROR;
2531}
2532
2533// ---------------------------------------------------------------------------
2534
Mathias Agopian74c40c02010-09-29 13:02:36 -07002535status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2536 sp<IMemoryHeap>* heap,
2537 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002538 uint32_t sw, uint32_t sh,
2539 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002540{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002541 ATRACE_CALL();
2542
Mathias Agopian74c40c02010-09-29 13:02:36 -07002543 status_t result = PERMISSION_DENIED;
2544
2545 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002546 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002547 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002548 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002549
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002550 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002551 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002552 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002553
2554 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002555 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002556 const uint32_t hw_w = hw.getWidth();
2557 const uint32_t hw_h = hw.getHeight();
2558
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002559 // if we have secure windows on this display, never allow the screen capture
2560 if (hw.getSecureLayerVisible()) {
2561 return PERMISSION_DENIED;
2562 }
2563
2564 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002565 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002566 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002567
2568 sw = (!sw) ? hw_w : sw;
2569 sh = (!sh) ? hw_h : sh;
2570 const size_t size = sw * sh * 4;
2571
Steve Block9d453682011-12-20 16:23:08 +00002572 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002573 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002574
Mathias Agopian74c40c02010-09-29 13:02:36 -07002575 // make sure to clear all GL error flags
2576 while ( glGetError() != GL_NO_ERROR ) ;
2577
2578 // create a FBO
2579 GLuint name, tname;
2580 glGenRenderbuffersOES(1, &tname);
2581 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2582 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002583
Mathias Agopian74c40c02010-09-29 13:02:36 -07002584 glGenFramebuffersOES(1, &name);
2585 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2586 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2587 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2588
2589 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002590
Mathias Agopian74c40c02010-09-29 13:02:36 -07002591 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2592
2593 // invert everything, b/c glReadPixel() below will invert the FB
2594 glViewport(0, 0, sw, sh);
2595 glMatrixMode(GL_PROJECTION);
2596 glPushMatrix();
2597 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002598 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002599 glMatrixMode(GL_MODELVIEW);
2600
2601 // redraw the screen entirely...
2602 glClearColor(0,0,0,1);
2603 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002604
Jamie Gennis9575f602011-10-07 14:51:16 -07002605 const LayerVector& layers(mDrawingState.layersSortedByZ);
2606 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002607 for (size_t i=0 ; i<count ; ++i) {
2608 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002609 const uint32_t flags = layer->drawingState().flags;
2610 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2611 const uint32_t z = layer->drawingState().z;
2612 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002613 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002614 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002615 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002616 }
2617
Mathias Agopian74c40c02010-09-29 13:02:36 -07002618 // check for errors and return screen capture
2619 if (glGetError() != GL_NO_ERROR) {
2620 // error while rendering
2621 result = INVALID_OPERATION;
2622 } else {
2623 // allocate shared memory large enough to hold the
2624 // screen capture
2625 sp<MemoryHeapBase> base(
2626 new MemoryHeapBase(size, 0, "screen-capture") );
2627 void* const ptr = base->getBase();
2628 if (ptr) {
2629 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002630 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002631 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2632 if (glGetError() == GL_NO_ERROR) {
2633 *heap = base;
2634 *w = sw;
2635 *h = sh;
2636 *f = PIXEL_FORMAT_RGBA_8888;
2637 result = NO_ERROR;
2638 }
2639 } else {
2640 result = NO_MEMORY;
2641 }
2642 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002643 glViewport(0, 0, hw_w, hw_h);
2644 glMatrixMode(GL_PROJECTION);
2645 glPopMatrix();
2646 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002647 } else {
2648 result = BAD_VALUE;
2649 }
2650
2651 // release FBO resources
2652 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2653 glDeleteRenderbuffersOES(1, &tname);
2654 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002655
2656 hw.compositionComplete();
2657
Steve Block9d453682011-12-20 16:23:08 +00002658 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002659
Mathias Agopian74c40c02010-09-29 13:02:36 -07002660 return result;
2661}
2662
2663
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002664status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2665 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002666 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002667 uint32_t sw, uint32_t sh,
2668 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002669{
2670 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002671 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002672 return BAD_VALUE;
2673
2674 if (!GLExtensions::getInstance().haveFramebufferObject())
2675 return INVALID_OPERATION;
2676
2677 class MessageCaptureScreen : public MessageBase {
2678 SurfaceFlinger* flinger;
2679 DisplayID dpy;
2680 sp<IMemoryHeap>* heap;
2681 uint32_t* w;
2682 uint32_t* h;
2683 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002684 uint32_t sw;
2685 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002686 uint32_t minLayerZ;
2687 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002688 status_t result;
2689 public:
2690 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002691 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002692 uint32_t sw, uint32_t sh,
2693 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002694 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002695 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2696 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2697 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002698 {
2699 }
2700 status_t getResult() const {
2701 return result;
2702 }
2703 virtual bool handler() {
2704 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002705 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002706 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002707 return true;
2708 }
2709 };
2710
2711 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002712 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002713 status_t res = postMessageSync(msg);
2714 if (res == NO_ERROR) {
2715 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2716 }
2717 return res;
2718}
2719
2720// ---------------------------------------------------------------------------
2721
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002722SurfaceFlinger::LayerVector::LayerVector() {
2723}
2724
2725SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
2726 : SortedVector<sp<LayerBase> >(rhs) {
2727}
2728
2729int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2730 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002731{
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002732 const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
2733 const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
2734 // sort layers by Z order
2735 uint32_t lz = l->currentState().z;
2736 uint32_t rz = r->currentState().z;
2737 // then by sequence, so we get a stable ordering
2738 return (lz != rz) ? (lz - rz) : (l->sequence - r->sequence);
2739}
2740
2741// ---------------------------------------------------------------------------
2742
2743SurfaceFlinger::State::State()
2744 : orientation(ISurfaceComposer::eOrientationDefault),
2745 orientationFlags(0) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002746}
2747
2748// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002749
Jamie Gennis9a78c902011-01-12 18:30:40 -08002750GraphicBufferAlloc::GraphicBufferAlloc() {}
2751
2752GraphicBufferAlloc::~GraphicBufferAlloc() {}
2753
2754sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002755 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002756 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2757 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002758 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002759 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002760 if (err == NO_MEMORY) {
2761 GraphicBuffer::dumpAllocationsToSystemLog();
2762 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002763 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002764 "failed (%s), handle=%p",
2765 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002766 return 0;
2767 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002768 return graphicBuffer;
2769}
2770
Jamie Gennis9a78c902011-01-12 18:30:40 -08002771// ---------------------------------------------------------------------------
2772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002773}; // namespace android