blob: 3b2bf00ed04305d6faf5bfff973f80b8fefcb9cd [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070053#include "DisplayHardware.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070054#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080055#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070056#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070059#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Glenn Kasten1db13d72011-12-19 13:55:34 -080064#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070066#include <gui/BitTube.h>
Mathias Agopian3094df32012-06-18 18:06:45 -070067#include <gui/SurfaceTextureClient.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070068
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080069#define EGL_VERSION_HW_ANDROID 0x3143
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#define DISPLAY_COUNT 1
72
73namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian99b49842011-06-27 16:05:52 -070076const String16 sHardwareTest("android.permission.HARDWARE_TEST");
77const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
78const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
79const String16 sDump("android.permission.DUMP");
80
81// ---------------------------------------------------------------------------
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083SurfaceFlinger::SurfaceFlinger()
84 : BnSurfaceComposer(), Thread(false),
85 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070086 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070090 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070091 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700100 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -0700101 mSecureFrameBuffer(0),
102 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
Steve Blocka19954a2012-01-04 20:05:49 +0000109 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
Colin Cross3854ed52012-03-23 14:17:18 -0700117#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700123#else
124#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700125#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126
Steve Blocka19954a2012-01-04 20:05:49 +0000127 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000128 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800131void SurfaceFlinger::onFirstRef()
132{
133 mEventQueue.init(this);
134
135 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
136
137 // Wait for the main thread to be done with its initialization
138 mReadyToRunBarrier.wait();
139}
140
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142SurfaceFlinger::~SurfaceFlinger()
143{
144 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145}
146
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800147void SurfaceFlinger::binderDied(const wp<IBinder>& who)
148{
149 // the window manager died on us. prepare its eulogy.
150
151 // reset screen orientation
152 Vector<ComposerState> state;
153 setTransactionState(state, eOrientationDefault, 0);
154
155 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700156 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800157}
158
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700159sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700161 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162}
163
Mathias Agopian7e27f052010-05-28 14:22:23 -0700164sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
Mathias Agopian96f08192010-06-02 23:28:45 -0700166 sp<ISurfaceComposerClient> bclient;
167 sp<Client> client(new Client(this));
168 status_t err = client->initCheck();
169 if (err == NO_ERROR) {
170 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 return bclient;
173}
174
Jamie Gennis9a78c902011-01-12 18:30:40 -0800175sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
176{
177 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
178 return gba;
179}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181void SurfaceFlinger::bootFinished()
182{
183 const nsecs_t now = systemTime();
184 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000185 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700186 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700187
188 // wait patiently for the window manager death
189 const String16 name("window");
190 sp<IBinder> window(defaultServiceManager()->getService(name));
191 if (window != 0) {
192 window->linkToDeath(this);
193 }
194
195 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700196 // formerly we would just kill the process, but we now ask it to exit so it
197 // can choose where to stop the animation.
198 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201static inline uint16_t pack565(int r, int g, int b) {
202 return (r<<11)|(g<<5)|b;
203}
204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205status_t SurfaceFlinger::readyToRun()
206{
Mathias Agopian1b031492012-06-20 17:51:20 -0700207 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 "Initializing graphics H/W...");
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 // we only support one display currently
211 int dpy = 0;
212
213 {
214 // initialize the main display
Mathias Agopian1b031492012-06-20 17:51:20 -0700215 // TODO: initialize all displays
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 DisplayHardware* const hw = new DisplayHardware(this, dpy);
Mathias Agopian1b031492012-06-20 17:51:20 -0700217 mDisplayHardwares[0] = hw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 }
219
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700220 // create the shared control-block
221 mServerHeap = new MemoryHeapBase(4096,
222 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000223 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700224
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000226 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700227
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700228 new(mServerCblk) surface_flinger_cblk_t;
229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 // initialize primary screen
231 // (other display should be initialized in the same manner, but
232 // asynchronously, as they could come and go. None of this is supported
233 // yet).
Mathias Agopian1b031492012-06-20 17:51:20 -0700234 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 const uint32_t w = hw.getWidth();
236 const uint32_t h = hw.getHeight();
237 const uint32_t f = hw.getFormat();
238 hw.makeCurrent();
239
240 // initialize the shared control block
241 mServerCblk->connected |= 1<<dpy;
242 display_cblk_t* dcblk = mServerCblk->displays + dpy;
243 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian1b031492012-06-20 17:51:20 -0700244 dcblk->w = w; // XXX: plane.getWidth();
245 dcblk->h = h; // XXX: plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 dcblk->format = f;
247 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
248 dcblk->xdpi = hw.getDpiX();
249 dcblk->ydpi = hw.getDpiY();
250 dcblk->fps = hw.getRefreshRate();
251 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
253 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700255 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 glShadeModel(GL_FLAT);
258 glDisable(GL_DITHER);
259 glDisable(GL_CULL_FACE);
260
261 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
262 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700263 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 glGenTextures(1, &mWormholeTexName);
265 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
266 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
267 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
268 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
269 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
270 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700271 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
272
273 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
274 glGenTextures(1, &mProtectedTexName);
275 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
281 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
283 glViewport(0, 0, w, h);
284 glMatrixMode(GL_PROJECTION);
285 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700286 // put the origin in the left-bottom corner
287 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 -0800288
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800289
290 // start the EventThread
291 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800292 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293
294 /*
295 * We're now ready to accept clients...
296 */
297
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800298 mReadyToRunBarrier.open();
299
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700300 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700301 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 return NO_ERROR;
304}
305
Mathias Agopiana67e4182012-06-19 17:26:12 -0700306void SurfaceFlinger::startBootAnim() {
307 // start boot animation
308 property_set("service.bootanim.exit", "0");
309 property_set("ctl.start", "bootanim");
310}
311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800313
Jamie Gennis582270d2011-08-17 18:19:00 -0700314bool SurfaceFlinger::authenticateSurfaceTexture(
315 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800316 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700317 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800318
319 // Check the visible layer list for the ISurface
320 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
321 size_t count = currentLayers.size();
322 for (size_t i=0 ; i<count ; i++) {
323 const sp<LayerBase>& layer(currentLayers[i]);
324 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700325 if (lbc != NULL) {
326 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
327 if (lbcBinder == surfaceTextureBinder) {
328 return true;
329 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800330 }
331 }
332
333 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700334 // SurfaceTexture gets destroyed before all the clients are done using it,
335 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800336 // will instead fail later on when the client tries to use the surface,
337 // which should be reported as "surface XYZ returned an -ENODEV". The
338 // purgatorized layers are no less authentic than the visible ones, so this
339 // should not cause any harm.
340 size_t purgatorySize = mLayerPurgatory.size();
341 for (size_t i=0 ; i<purgatorySize ; i++) {
342 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
343 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700344 if (lbc != NULL) {
345 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
346 if (lbcBinder == surfaceTextureBinder) {
347 return true;
348 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800349 }
350 }
351
352 return false;
353}
354
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800355// ----------------------------------------------------------------------------
356
357sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800358 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700359}
360
Mathias Agopian3094df32012-06-18 18:06:45 -0700361void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700362 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700363 EGLSurface result = EGL_NO_SURFACE;
364 EGLSurface old_surface = EGL_NO_SURFACE;
365 sp<SurfaceTextureClient> stc;
366
367 if (display != NULL) {
368 stc = new SurfaceTextureClient(display);
369 result = eglCreateWindowSurface(hw.getEGLDisplay(),
370 hw.getEGLConfig(), (EGLNativeWindowType)stc.get(), NULL);
371 ALOGE_IF(result == EGL_NO_SURFACE,
372 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
373 display.get());
374 }
375
376 { // scope for the lock
377 Mutex::Autolock _l(mStateLock);
378 old_surface = mExternalDisplaySurface;
379 mExternalDisplayNativeWindow = stc;
380 mExternalDisplaySurface = result;
381 ALOGD("mExternalDisplaySurface = %p", result);
382 }
383
384 if (old_surface != EGL_NO_SURFACE) {
385 // Note: EGL allows to destroy an object while its current
386 // it will fail to become current next time though.
387 eglDestroySurface(hw.getEGLDisplay(), old_surface);
388 }
389}
390
391EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
392 Mutex::Autolock _l(mStateLock);
393 return mExternalDisplaySurface;
394}
395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800397
398void SurfaceFlinger::waitForEvent() {
399 mEventQueue.waitMessage();
400}
401
402void SurfaceFlinger::signalTransaction() {
403 mEventQueue.invalidate();
404}
405
406void SurfaceFlinger::signalLayerUpdate() {
407 mEventQueue.invalidate();
408}
409
410void SurfaceFlinger::signalRefresh() {
411 mEventQueue.refresh();
412}
413
414status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
415 nsecs_t reltime, uint32_t flags) {
416 return mEventQueue.postMessage(msg, reltime);
417}
418
419status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
420 nsecs_t reltime, uint32_t flags) {
421 status_t res = mEventQueue.postMessage(msg, reltime);
422 if (res == NO_ERROR) {
423 msg->wait();
424 }
425 return res;
426}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427
428bool SurfaceFlinger::threadLoop()
429{
430 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 return true;
432}
433
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800434void SurfaceFlinger::onMessageReceived(int32_t what)
435{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800436 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800437 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700438 case MessageQueue::REFRESH: {
439// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800440 // if we're in a global transaction, don't do anything.
441 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
442 uint32_t transactionFlags = peekTransactionFlags(mask);
443 if (CC_UNLIKELY(transactionFlags)) {
444 handleTransaction(transactionFlags);
445 }
446
447 // post surfaces (if needed)
448 handlePageFlip();
449
Mathias Agopian69a655c2012-04-11 20:43:19 -0700450// signalRefresh();
451//
452// } break;
453//
454// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800455
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800456 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800457
Mathias Agopian1b031492012-06-20 17:51:20 -0700458 // TODO: iterate through all displays
459 const DisplayHardware& hw(getDisplayHardware(0));
Mathias Agopian303d5382012-02-05 01:49:16 -0800460
Mathias Agopian69a655c2012-04-11 20:43:19 -0700461// if (mDirtyRegion.isEmpty()) {
462// return;
463// }
464
Mathias Agopianb048cef2012-02-04 15:44:04 -0800465 if (CC_UNLIKELY(mHwWorkListDirty)) {
466 // build the h/w work list
Mathias Agopian1b031492012-06-20 17:51:20 -0700467 handleWorkList(hw);
Mathias Agopianb048cef2012-02-04 15:44:04 -0800468 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800469
Mathias Agopianb048cef2012-02-04 15:44:04 -0800470 if (CC_LIKELY(hw.canDraw())) {
471 // repaint the framebuffer (if needed)
Mathias Agopian1b031492012-06-20 17:51:20 -0700472 handleRepaint(hw);
Mathias Agopianb048cef2012-02-04 15:44:04 -0800473 // inform the h/w that we're done compositing
474 hw.compositionComplete();
475 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800476 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800477 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800478 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800479 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800480
Mathias Agopian3094df32012-06-18 18:06:45 -0700481 // render to the external display if we have one
482 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
483 if (externalDisplaySurface != EGL_NO_SURFACE) {
484 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
485 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
486 externalDisplaySurface, externalDisplaySurface,
487 eglGetCurrentContext());
488
489 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
490
491 if (success) {
492 // redraw the screen entirely...
493 glDisable(GL_TEXTURE_EXTERNAL_OES);
494 glDisable(GL_TEXTURE_2D);
495 glClearColor(0,0,0,1);
496 glClear(GL_COLOR_BUFFER_BIT);
497 glMatrixMode(GL_MODELVIEW);
498 glLoadIdentity();
499 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
500 const size_t count = layers.size();
501 for (size_t i=0 ; i<count ; ++i) {
502 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -0700503 layer->drawForSreenShot(hw);
Mathias Agopian3094df32012-06-18 18:06:45 -0700504 }
505
506 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
507 ALOGE_IF(!success, "external display eglSwapBuffers failed");
508
509 hw.compositionComplete();
510 }
511
512 success = eglMakeCurrent(eglGetCurrentDisplay(),
513 cur, cur, eglGetCurrentContext());
514
515 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
516 }
517
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800518 } break;
519 }
520}
521
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522void SurfaceFlinger::postFramebuffer()
523{
Mathias Agopian841cde52012-03-01 15:44:37 -0800524 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800525 // mSwapRegion can be empty here is some cases, for instance if a hidden
526 // or fully transparent window is updating.
527 // in that case, we need to flip anyways to not risk a deadlock with
528 // h/w composer.
529
Mathias Agopian1b031492012-06-20 17:51:20 -0700530 const DisplayHardware& hw(getDefaultDisplayHardware());
Jesse Hallc5c5a142012-07-02 16:49:28 -0700531 HWComposer& hwc(hw.getHwComposer());
532 size_t numLayers = mVisibleLayersSortedByZ.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700533 const nsecs_t now = systemTime();
534 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700535
536 if (hwc.initCheck() == NO_ERROR) {
537 HWComposer::LayerListIterator cur = hwc.begin();
538 const HWComposer::LayerListIterator end = hwc.end();
539 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
540 if (cur->getCompositionType() == HWC_OVERLAY) {
541 mVisibleLayersSortedByZ[i]->setAcquireFence(*cur);
542 } else {
543 cur->setAcquireFenceFd(-1);
544 }
545 }
546 }
547
Mathias Agopiana44b0412011-10-16 18:46:35 -0700548 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800549
Jesse Hallef194142012-06-14 14:45:17 -0700550 if (hwc.initCheck() == NO_ERROR) {
551 HWComposer::LayerListIterator cur = hwc.begin();
552 const HWComposer::LayerListIterator end = hwc.end();
553 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
554 mVisibleLayersSortedByZ[i]->onLayerDisplayed(&*cur);
555 }
556 } else {
557 for (size_t i = 0; i < numLayers; i++) {
558 mVisibleLayersSortedByZ[i]->onLayerDisplayed(NULL);
559 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800560 }
561
Mathias Agopiana44b0412011-10-16 18:46:35 -0700562 mLastSwapBufferTime = systemTime() - now;
563 mDebugInSwapBuffers = 0;
564 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565}
566
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
568{
Mathias Agopian841cde52012-03-01 15:44:37 -0800569 ATRACE_CALL();
570
Mathias Agopianca4d3602011-05-19 15:38:14 -0700571 Mutex::Autolock _l(mStateLock);
572 const nsecs_t now = systemTime();
573 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574
Mathias Agopianca4d3602011-05-19 15:38:14 -0700575 // Here we're guaranteed that some transaction flags are set
576 // so we can call handleTransactionLocked() unconditionally.
577 // We call getTransactionFlags(), which will also clear the flags,
578 // with mStateLock held to guarantee that mCurrentState won't change
579 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700580
Mathias Agopianca4d3602011-05-19 15:38:14 -0700581 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
582 transactionFlags = getTransactionFlags(mask);
583 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700584
Mathias Agopianca4d3602011-05-19 15:38:14 -0700585 mLastTransactionTime = systemTime() - now;
586 mDebugInTransaction = 0;
587 invalidateHwcGeometry();
588 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700589}
590
Mathias Agopianca4d3602011-05-19 15:38:14 -0700591void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700592{
593 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 const size_t count = currentLayers.size();
595
596 /*
597 * Traversal of the children
598 * (perform the transaction for each of them if needed)
599 */
600
601 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
602 if (layersNeedTransaction) {
603 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
606 if (!trFlags) continue;
607
608 const uint32_t flags = layer->doTransaction(0);
609 if (flags & Layer::eVisibleRegion)
610 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
612 }
613
614 /*
615 * Perform our own transaction if needed
616 */
617
618 if (transactionFlags & eTransactionNeeded) {
619 if (mCurrentState.orientation != mDrawingState.orientation) {
620 // the orientation has changed, recompute all visible regions
621 // and invalidate everything.
622
Mathias Agopian1b031492012-06-20 17:51:20 -0700623 const int dpy = 0; // TODO: should be a parameter
624 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 const int orientation = mCurrentState.orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700626 hw.setOrientation(orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
628 // update the shared control block
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
630 dcblk->orientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700631 dcblk->w = hw.getUserWidth();
632 dcblk->h = hw.getUserHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633
Mathias Agopian1b031492012-06-20 17:51:20 -0700634 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 mVisibleRegionsDirty = true;
636 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700639 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
640 // layers have been added
641 mVisibleRegionsDirty = true;
642 }
643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 // some layers might have been removed, so
645 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700646 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700647 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700649 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700650 const size_t count = previousLayers.size();
651 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700652 const sp<LayerBase>& layer(previousLayers[i]);
653 if (currentLayers.indexOf( layer ) < 0) {
654 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700655 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700656 }
657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 }
660
661 commitTransaction();
662}
663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800665 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666{
Mathias Agopian841cde52012-03-01 15:44:37 -0800667 ATRACE_CALL();
668
Mathias Agopian1b031492012-06-20 17:51:20 -0700669 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: we shouldn't rely on DisplayHardware here
670 const Transform& planeTransform(hw.getTransform());
Mathias Agopianab028732010-03-16 16:41:46 -0700671 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672
673 Region aboveOpaqueLayers;
674 Region aboveCoveredLayers;
675 Region dirty;
676
677 bool secureFrameBuffer = false;
678
679 size_t i = currentLayers.size();
680 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700681 const sp<LayerBase>& layer = currentLayers[i];
Mathias Agopian1b031492012-06-20 17:51:20 -0700682 layer->validateVisibility(planeTransform, hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683
684 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700685 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686
Mathias Agopianab028732010-03-16 16:41:46 -0700687 /*
688 * opaqueRegion: area of a surface that is fully opaque.
689 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700691
692 /*
693 * visibleRegion: area of a surface that is visible on screen
694 * and not fully transparent. This is essentially the layer's
695 * footprint minus the opaque regions above it.
696 * Areas covered by a translucent surface are considered visible.
697 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700699
700 /*
701 * coveredRegion: area of a surface that is covered by all
702 * visible regions above it (which includes the translucent areas).
703 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700705
706
707 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800708 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700709 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700710 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700712 visibleRegion.andSelf(screenRegion);
713 if (!visibleRegion.isEmpty()) {
714 // Remove the transparent area from the visible region
715 if (translucent) {
716 visibleRegion.subtractSelf(layer->transparentRegionScreen);
717 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718
Mathias Agopianab028732010-03-16 16:41:46 -0700719 // compute the opaque region
720 const int32_t layerOrientation = layer->getOrientation();
721 if (s.alpha==255 && !translucent &&
722 ((layerOrientation & Transform::ROT_INVALID) == false)) {
723 // the opaque region is the layer's footprint
724 opaqueRegion = visibleRegion;
725 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 }
727 }
728
Mathias Agopianab028732010-03-16 16:41:46 -0700729 // Clip the covered region to the visible region
730 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
731
732 // Update aboveCoveredLayers for next (lower) layer
733 aboveCoveredLayers.orSelf(visibleRegion);
734
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 // subtract the opaque region covered by the layers above us
736 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737
738 // compute this layer's dirty region
739 if (layer->contentDirty) {
740 // we need to invalidate the whole region
741 dirty = visibleRegion;
742 // as well, as the old visible region
743 dirty.orSelf(layer->visibleRegionScreen);
744 layer->contentDirty = false;
745 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700746 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700747 * the exposed region consists of two components:
748 * 1) what's VISIBLE now and was COVERED before
749 * 2) what's EXPOSED now less what was EXPOSED before
750 *
751 * note that (1) is conservative, we start with the whole
752 * visible region but only keep what used to be covered by
753 * something -- which mean it may have been exposed.
754 *
755 * (2) handles areas that were not covered by anything but got
756 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700757 */
Mathias Agopianab028732010-03-16 16:41:46 -0700758 const Region newExposed = visibleRegion - coveredRegion;
759 const Region oldVisibleRegion = layer->visibleRegionScreen;
760 const Region oldCoveredRegion = layer->coveredRegionScreen;
761 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
762 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 }
764 dirty.subtractSelf(aboveOpaqueLayers);
765
766 // accumulate to the screen dirty region
767 dirtyRegion.orSelf(dirty);
768
Mathias Agopianab028732010-03-16 16:41:46 -0700769 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 // Store the visible region is screen space
773 layer->setVisibleRegion(visibleRegion);
774 layer->setCoveredRegion(coveredRegion);
775
Mathias Agopian97011222009-07-28 10:57:27 -0700776 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 if (layer->isSecure() && !visibleRegion.isEmpty()) {
778 secureFrameBuffer = true;
779 }
780 }
781
Mathias Agopian97011222009-07-28 10:57:27 -0700782 // invalidate the areas where a layer was removed
783 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
784 mDirtyRegionRemovedLayer.clear();
785
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 mSecureFrameBuffer = secureFrameBuffer;
787 opaqueRegion = aboveOpaqueLayers;
788}
789
790
791void SurfaceFlinger::commitTransaction()
792{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800793 if (!mLayersPendingRemoval.isEmpty()) {
794 // Notify removed layers now that they can't be drawn from
795 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
796 mLayersPendingRemoval[i]->onRemoved();
797 }
798 mLayersPendingRemoval.clear();
799 }
800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700802 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700803 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804}
805
806void SurfaceFlinger::handlePageFlip()
807{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800808 ATRACE_CALL();
Mathias Agopian1b031492012-06-20 17:51:20 -0700809 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: it's a problem we need DisplayHardware here
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800810 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800812 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
813 const bool visibleRegions = lockPageFlip(currentLayers);
814
815 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 Region opaqueRegion;
817 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700818
819 /*
820 * rebuild the visible layer list
821 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800822 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700823 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700824 mVisibleLayersSortedByZ.setCapacity(count);
825 for (size_t i=0 ; i<count ; i++) {
826 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
827 mVisibleLayersSortedByZ.add(currentLayers[i]);
828 }
829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 mWormholeRegion = screenRegion.subtract(opaqueRegion);
831 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800832 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833 }
834
835 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700836
837 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 mDirtyRegion.andSelf(screenRegion);
839}
840
Mathias Agopianad456f92011-01-13 17:53:01 -0800841void SurfaceFlinger::invalidateHwcGeometry()
842{
843 mHwWorkListDirty = true;
844}
845
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
847{
848 bool recomputeVisibleRegions = false;
849 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700850 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700852 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 layer->lockPageFlip(recomputeVisibleRegions);
854 }
855 return recomputeVisibleRegions;
856}
857
858void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
859{
Mathias Agopian1b031492012-06-20 17:51:20 -0700860 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: it's a problem we need DisplayHardware here
861 const Transform& planeTransform(hw.getTransform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800862 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700863 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700865 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866 layer->unlockPageFlip(planeTransform, mDirtyRegion);
867 }
868}
869
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800870void SurfaceFlinger::handleRefresh()
871{
872 bool needInvalidate = false;
873 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
874 const size_t count = currentLayers.size();
875 for (size_t i=0 ; i<count ; i++) {
876 const sp<LayerBase>& layer(currentLayers[i]);
877 if (layer->onPreComposition()) {
878 needInvalidate = true;
879 }
880 }
881 if (needInvalidate) {
882 signalLayerUpdate();
883 }
884}
885
886
Mathias Agopian1b031492012-06-20 17:51:20 -0700887void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700888{
889 mHwWorkListDirty = false;
Mathias Agopian1b031492012-06-20 17:51:20 -0700890 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700891 if (hwc.initCheck() == NO_ERROR) {
892 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
893 const size_t count = currentLayers.size();
894 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700895
896 HWComposer::LayerListIterator cur = hwc.begin();
897 const HWComposer::LayerListIterator end = hwc.end();
898 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
899 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700900 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700901 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700902 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700903 }
904 }
905}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700906
Mathias Agopian1b031492012-06-20 17:51:20 -0700907void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908{
Mathias Agopian841cde52012-03-01 15:44:37 -0800909 ATRACE_CALL();
910
Mathias Agopianb8a55602009-06-26 19:06:36 -0700911 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700912 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913
Glenn Kasten99ed2242011-12-15 09:51:17 -0800914 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700915 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 }
917
Mathias Agopianb8a55602009-06-26 19:06:36 -0700918 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -0700919 glMatrixMode(GL_MODELVIEW);
920 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921
922 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700923 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700924 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
925 // takes a rectangle, we must make sure to update that whole
926 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700927 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700928 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700929 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700930 // We need to redraw the rectangle that will be updated
931 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700932 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700933 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700934 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700935 } else {
936 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700938 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 }
940 }
941
Mathias Agopian1b031492012-06-20 17:51:20 -0700942 setupHardwareComposer(hw);
943 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700945 // update the swap region and clear the dirty region
946 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 mDirtyRegion.clear();
948}
949
Mathias Agopian1b031492012-06-20 17:51:20 -0700950void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700952 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700953 HWComposer::LayerListIterator cur = hwc.begin();
954 const HWComposer::LayerListIterator end = hwc.end();
955 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700956 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700957 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700958
Mathias Agopianf384cc32011-09-08 18:31:55 -0700959 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
960 size_t count = layers.size();
961
Steve Blocke6f43dd2012-01-06 19:20:56 +0000962 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700963 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
964 hwc.getNumLayers(), count);
965
966 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700967 if (hwc.initCheck() == NO_ERROR) {
968 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
969 }
Mathias Agopian45721772010-08-12 15:03:26 -0700970
971 /*
972 * update the per-frame h/w composer data for each layer
973 * and build the transparent region of the FB
974 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700975 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700976 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700977 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700978 }
979 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000980 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700981}
Mathias Agopian45721772010-08-12 15:03:26 -0700982
Mathias Agopian1b031492012-06-20 17:51:20 -0700983void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -0700984{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700985 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700986 HWComposer::LayerListIterator cur = hwc.begin();
987 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700988
989 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700990 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700991 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700992
Mathias Agopianb9494d52012-04-18 02:28:45 -0700993 if (hwc.getLayerCount(HWC_OVERLAY)) {
994 // when using overlays, we assume a fully transparent framebuffer
995 // NOTE: we could reduce how much we need to clear, for instance
996 // remove where there are opaque FB layers. however, on some
997 // GPUs doing a "clean slate" glClear might be more efficient.
998 // We'll revisit later if needed.
999 glClearColor(0, 0, 0, 0);
1000 glClear(GL_COLOR_BUFFER_BIT);
1001 } else {
1002 // screen is already cleared here
1003 if (!mWormholeRegion.isEmpty()) {
1004 // can happen with SurfaceView
1005 drawWormhole();
1006 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001007 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001008
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001009 /*
1010 * and then, render the layers targeted at the framebuffer
1011 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001012
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001013 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1014 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001015 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001016 const sp<LayerBase>& layer(layers[i]);
1017 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1018 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001019 if (cur->getCompositionType() == HWC_OVERLAY) {
1020 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001021 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001022 // never clear the very first layer since we're
1023 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001024 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001025 }
1026 continue;
1027 }
1028 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001029 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001030 }
1031 }
1032 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033}
1034
Mathias Agopian1b031492012-06-20 17:51:20 -07001035void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036{
Mathias Agopian0a917752010-06-14 21:20:00 -07001037 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001038 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001039 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001040 return;
1041 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001042
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001043 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001044 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1045 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001046 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001047 }
1048
Mathias Agopianc492e672011-10-18 14:49:27 -07001049 glDisable(GL_TEXTURE_EXTERNAL_OES);
1050 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052
Mathias Agopian0926f502009-05-04 14:17:04 -07001053 static int toggle = 0;
1054 toggle = 1 - toggle;
1055 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001056 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001057 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001058 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001059 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
Mathias Agopian20f68782009-05-11 00:03:41 -07001061 Region::const_iterator it = mDirtyRegion.begin();
1062 Region::const_iterator const end = mDirtyRegion.end();
1063 while (it != end) {
1064 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001066 { r.left, height - r.top },
1067 { r.left, height - r.bottom },
1068 { r.right, height - r.bottom },
1069 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 };
1071 glVertexPointer(2, GL_FLOAT, 0, vertices);
1072 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1073 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001074
Mathias Agopian0656a682011-09-20 17:22:44 -07001075 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076
1077 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001078 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079}
1080
1081void SurfaceFlinger::drawWormhole() const
1082{
1083 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1084 if (region.isEmpty())
1085 return;
1086
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001087 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001088 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001089 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001090 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001091
1092 GLfloat vertices[4][2];
1093 glVertexPointer(2, GL_FLOAT, 0, vertices);
1094 Region::const_iterator it = region.begin();
1095 Region::const_iterator const end = region.end();
1096 while (it != end) {
1097 const Rect& r = *it++;
1098 vertices[0][0] = r.left;
1099 vertices[0][1] = r.top;
1100 vertices[1][0] = r.right;
1101 vertices[1][1] = r.top;
1102 vertices[2][0] = r.right;
1103 vertices[2][1] = r.bottom;
1104 vertices[3][0] = r.left;
1105 vertices[3][1] = r.bottom;
1106 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1107 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108}
1109
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001110status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111{
1112 Mutex::Autolock _l(mStateLock);
1113 addLayer_l(layer);
1114 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1115 return NO_ERROR;
1116}
1117
Mathias Agopian96f08192010-06-02 23:28:45 -07001118status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1119{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001120 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001121 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1122}
1123
1124ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1125 const sp<LayerBaseClient>& lbc)
1126{
Mathias Agopian96f08192010-06-02 23:28:45 -07001127 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001128 size_t name = client->attachLayer(lbc);
1129
1130 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001131
1132 // add this layer to the current state list
1133 addLayer_l(lbc);
1134
Mathias Agopian4f113742011-05-03 16:21:41 -07001135 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001136}
1137
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001138status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139{
1140 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001141 status_t err = purgatorizeLayer_l(layer);
1142 if (err == NO_ERROR)
1143 setTransactionFlags(eTransactionNeeded);
1144 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145}
1146
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001147status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001149 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1150 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001151 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001152 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1154 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001155 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 return NO_ERROR;
1157 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001158 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159}
1160
Mathias Agopian9a112062009-04-17 19:36:26 -07001161status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1162{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001163 // First add the layer to the purgatory list, which makes sure it won't
1164 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001165 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001166 if (err >= 0) {
1167 mLayerPurgatory.add(layerBase);
1168 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001169
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001170 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001171
Mathias Agopian3d579642009-06-04 18:46:21 -07001172 // it's possible that we don't find a layer, because it might
1173 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001174 // from the user because there is a race between Client::destroySurface(),
1175 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001176 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1177}
1178
Mathias Agopian96f08192010-06-02 23:28:45 -07001179status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180{
Mathias Agopian96f08192010-06-02 23:28:45 -07001181 layer->forceVisibilityTransaction();
1182 setTransactionFlags(eTraversalNeeded);
1183 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184}
1185
Mathias Agopiandea20b12011-05-03 17:04:02 -07001186uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1187{
1188 return android_atomic_release_load(&mTransactionFlags);
1189}
1190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1192{
1193 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1194}
1195
Mathias Agopianbb641242010-05-18 17:06:55 -07001196uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197{
1198 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1199 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001200 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 }
1202 return old;
1203}
1204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001206void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001207 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001208 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001209
Jamie Gennis28378392011-10-12 17:39:00 -07001210 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001211 if (mCurrentState.orientation != orientation) {
1212 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1213 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001214 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001215 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001216 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001217 orientation);
1218 }
1219 }
1220
Mathias Agopian698c0872011-06-28 19:09:31 -07001221 const size_t count = state.size();
1222 for (size_t i=0 ; i<count ; i++) {
1223 const ComposerState& s(state[i]);
1224 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001225 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001226 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001227
Mathias Agopian386aa982011-11-07 21:58:03 -08001228 if (transactionFlags) {
1229 // this triggers the transaction
1230 setTransactionFlags(transactionFlags);
1231
1232 // if this is a synchronous transaction, wait for it to take effect
1233 // before returning.
1234 if (flags & eSynchronous) {
1235 mTransationPending = true;
1236 }
1237 while (mTransationPending) {
1238 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1239 if (CC_UNLIKELY(err != NO_ERROR)) {
1240 // just in case something goes wrong in SF, return to the
1241 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001242 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001243 mTransationPending = false;
1244 break;
1245 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001246 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 }
1248}
1249
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001250sp<ISurface> SurfaceFlinger::createSurface(
1251 ISurfaceComposerClient::surface_data_t* params,
1252 const String8& name,
1253 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1255 uint32_t flags)
1256{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001257 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001258 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001259
1260 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001261 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001262 int(w), int(h));
1263 return surfaceHandle;
1264 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001265
Mathias Agopianff615cc2012-02-24 14:58:36 -08001266 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001267 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 switch (flags & eFXSurfaceMask) {
1269 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001270 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1271 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 break;
1273 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001274 // for now we treat Blur as Dim, until we can implement it
1275 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001277 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001279 case eFXSurfaceScreenshot:
1280 layer = createScreenshotSurface(client, d, w, h, flags);
1281 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 }
1283
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001284 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001285 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001286 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001290 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001292 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001293 if (normalLayer != 0) {
1294 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001295 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001296 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001297 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001298
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 }
1301
1302 return surfaceHandle;
1303}
1304
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001305sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001306 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001308 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309{
1310 // initialize the surfaces
1311 switch (format) { // TODO: take h/w into account
1312 case PIXEL_FORMAT_TRANSPARENT:
1313 case PIXEL_FORMAT_TRANSLUCENT:
1314 format = PIXEL_FORMAT_RGBA_8888;
1315 break;
1316 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001317#ifdef NO_RGBX_8888
1318 format = PIXEL_FORMAT_RGB_565;
1319#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001320 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001321#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 break;
1323 }
1324
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001325#ifdef NO_RGBX_8888
1326 if (format == PIXEL_FORMAT_RGBX_8888)
1327 format = PIXEL_FORMAT_RGBA_8888;
1328#endif
1329
Mathias Agopian96f08192010-06-02 23:28:45 -07001330 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001331 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001332 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001333 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001334 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 }
1336 return layer;
1337}
1338
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001339sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001340 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001341 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342{
Mathias Agopian96f08192010-06-02 23:28:45 -07001343 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001344 return layer;
1345}
1346
1347sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1348 const sp<Client>& client, DisplayID display,
1349 uint32_t w, uint32_t h, uint32_t flags)
1350{
1351 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 return layer;
1353}
1354
Mathias Agopian96f08192010-06-02 23:28:45 -07001355status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356{
Mathias Agopian9a112062009-04-17 19:36:26 -07001357 /*
1358 * called by the window manager, when a surface should be marked for
1359 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001360 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001361 * The surface is removed from the current and drawing lists, but placed
1362 * in the purgatory queue, so it's not destroyed right-away (we need
1363 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001364 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001365
Mathias Agopian48d819a2009-09-10 19:41:18 -07001366 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001367 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001368 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001369
Mathias Agopian48d819a2009-09-10 19:41:18 -07001370 if (layer != 0) {
1371 err = purgatorizeLayer_l(layer);
1372 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001373 setTransactionFlags(eTransactionNeeded);
1374 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001375 }
1376 return err;
1377}
1378
Mathias Agopianca4d3602011-05-19 15:38:14 -07001379status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001380{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001381 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001382 status_t err = NO_ERROR;
1383 sp<LayerBaseClient> l(layer.promote());
1384 if (l != NULL) {
1385 Mutex::Autolock _l(mStateLock);
1386 err = removeLayer_l(l);
1387 if (err == NAME_NOT_FOUND) {
1388 // The surface wasn't in the current list, which means it was
1389 // removed already, which means it is in the purgatory,
1390 // and need to be removed from there.
1391 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001392 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001393 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001394 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001395 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001396 "error removing layer=%p (%s)", l.get(), strerror(-err));
1397 }
1398 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399}
1400
Mathias Agopian698c0872011-06-28 19:09:31 -07001401uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001402 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001403 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001406 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1407 if (layer != 0) {
1408 const uint32_t what = s.what;
1409 if (what & ePositionChanged) {
1410 if (layer->setPosition(s.x, s.y))
1411 flags |= eTraversalNeeded;
1412 }
1413 if (what & eLayerChanged) {
1414 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1415 if (layer->setLayer(s.z)) {
1416 mCurrentState.layersSortedByZ.removeAt(idx);
1417 mCurrentState.layersSortedByZ.add(layer);
1418 // we need traversal (state changed)
1419 // AND transaction (list changed)
1420 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 }
1422 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001423 if (what & eSizeChanged) {
1424 if (layer->setSize(s.w, s.h)) {
1425 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001426 }
1427 }
1428 if (what & eAlphaChanged) {
1429 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1430 flags |= eTraversalNeeded;
1431 }
1432 if (what & eMatrixChanged) {
1433 if (layer->setMatrix(s.matrix))
1434 flags |= eTraversalNeeded;
1435 }
1436 if (what & eTransparentRegionChanged) {
1437 if (layer->setTransparentRegionHint(s.transparentRegion))
1438 flags |= eTraversalNeeded;
1439 }
1440 if (what & eVisibilityChanged) {
1441 if (layer->setFlags(s.flags, s.mask))
1442 flags |= eTraversalNeeded;
1443 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001444 if (what & eCropChanged) {
1445 if (layer->setCrop(s.crop))
1446 flags |= eTraversalNeeded;
1447 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001449 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450}
1451
Mathias Agopianb60314a2012-04-10 22:09:54 -07001452// ---------------------------------------------------------------------------
1453
1454void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001455 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001456 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001457 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001458 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001459 // this is a temporary work-around, eventually this should be called
1460 // by the power-manager
1461 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001462 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001463 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464}
1465
Mathias Agopianb60314a2012-04-10 22:09:54 -07001466void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001467 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001468 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001469 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001470 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001471 hw.releaseScreen();
1472 // from this point on, SF will stop drawing
1473 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474}
1475
Colin Cross8e533062012-06-07 13:17:52 -07001476void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001477 class MessageScreenAcquired : public MessageBase {
1478 SurfaceFlinger* flinger;
1479 public:
1480 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1481 virtual bool handler() {
1482 flinger->onScreenAcquired();
1483 return true;
1484 }
1485 };
1486 sp<MessageBase> msg = new MessageScreenAcquired(this);
1487 postMessageSync(msg);
1488}
1489
Colin Cross8e533062012-06-07 13:17:52 -07001490void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001491 class MessageScreenReleased : public MessageBase {
1492 SurfaceFlinger* flinger;
1493 public:
1494 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1495 virtual bool handler() {
1496 flinger->onScreenReleased();
1497 return true;
1498 }
1499 };
1500 sp<MessageBase> msg = new MessageScreenReleased(this);
1501 postMessageSync(msg);
1502}
1503
1504// ---------------------------------------------------------------------------
1505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1507{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001508 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 char buffer[SIZE];
1510 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001511
1512 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513 snprintf(buffer, SIZE, "Permission Denial: "
1514 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1515 IPCThreadState::self()->getCallingPid(),
1516 IPCThreadState::self()->getCallingUid());
1517 result.append(buffer);
1518 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001519 // Try to get the main lock, but don't insist if we can't
1520 // (this would indicate SF is stuck, but we want to be able to
1521 // print something in dumpsys).
1522 int retry = 3;
1523 while (mStateLock.tryLock()<0 && --retry>=0) {
1524 usleep(1000000);
1525 }
1526 const bool locked(retry >= 0);
1527 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001528 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001529 "SurfaceFlinger appears to be unresponsive, "
1530 "dumping anyways (no locks held)\n");
1531 result.append(buffer);
1532 }
1533
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001534 bool dumpAll = true;
1535 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001536 size_t numArgs = args.size();
1537 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001538 if ((index < numArgs) &&
1539 (args[index] == String16("--list"))) {
1540 index++;
1541 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001542 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001543 }
1544
1545 if ((index < numArgs) &&
1546 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001547 index++;
1548 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001549 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001550 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001551
1552 if ((index < numArgs) &&
1553 (args[index] == String16("--latency-clear"))) {
1554 index++;
1555 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001556 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001559
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001560 if (dumpAll) {
1561 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001562 }
1563
Mathias Agopian9795c422009-08-26 16:36:26 -07001564 if (locked) {
1565 mStateLock.unlock();
1566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 }
1568 write(fd, result.string(), result.size());
1569 return NO_ERROR;
1570}
1571
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001572void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1573 String8& result, char* buffer, size_t SIZE) const
1574{
1575 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1576 const size_t count = currentLayers.size();
1577 for (size_t i=0 ; i<count ; i++) {
1578 const sp<LayerBase>& layer(currentLayers[i]);
1579 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1580 result.append(buffer);
1581 }
1582}
1583
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001584void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1585 String8& result, char* buffer, size_t SIZE) const
1586{
1587 String8 name;
1588 if (index < args.size()) {
1589 name = String8(args[index]);
1590 index++;
1591 }
1592
1593 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1594 const size_t count = currentLayers.size();
1595 for (size_t i=0 ; i<count ; i++) {
1596 const sp<LayerBase>& layer(currentLayers[i]);
1597 if (name.isEmpty()) {
1598 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1599 result.append(buffer);
1600 }
1601 if (name.isEmpty() || (name == layer->getName())) {
1602 layer->dumpStats(result, buffer, SIZE);
1603 }
1604 }
1605}
1606
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001607void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1608 String8& result, char* buffer, size_t SIZE) const
1609{
1610 String8 name;
1611 if (index < args.size()) {
1612 name = String8(args[index]);
1613 index++;
1614 }
1615
1616 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1617 const size_t count = currentLayers.size();
1618 for (size_t i=0 ; i<count ; i++) {
1619 const sp<LayerBase>& layer(currentLayers[i]);
1620 if (name.isEmpty() || (name == layer->getName())) {
1621 layer->clearStats();
1622 }
1623 }
1624}
1625
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001626void SurfaceFlinger::dumpAllLocked(
1627 String8& result, char* buffer, size_t SIZE) const
1628{
1629 // figure out if we're stuck somewhere
1630 const nsecs_t now = systemTime();
1631 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1632 const nsecs_t inTransaction(mDebugInTransaction);
1633 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1634 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1635
1636 /*
1637 * Dump the visible layer list
1638 */
1639 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1640 const size_t count = currentLayers.size();
1641 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1642 result.append(buffer);
1643 for (size_t i=0 ; i<count ; i++) {
1644 const sp<LayerBase>& layer(currentLayers[i]);
1645 layer->dump(result, buffer, SIZE);
1646 }
1647
1648 /*
1649 * Dump the layers in the purgatory
1650 */
1651
1652 const size_t purgatorySize = mLayerPurgatory.size();
1653 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1654 result.append(buffer);
1655 for (size_t i=0 ; i<purgatorySize ; i++) {
1656 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1657 layer->shortDump(result, buffer, SIZE);
1658 }
1659
1660 /*
1661 * Dump SurfaceFlinger global state
1662 */
1663
1664 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1665 result.append(buffer);
1666
Mathias Agopian1b031492012-06-20 17:51:20 -07001667 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001668 const GLExtensions& extensions(GLExtensions::getInstance());
1669 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1670 extensions.getVendor(),
1671 extensions.getRenderer(),
1672 extensions.getVersion());
1673 result.append(buffer);
1674
1675 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001676 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001677 EGL_VERSION_HW_ANDROID));
1678 result.append(buffer);
1679
1680 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1681 result.append(buffer);
1682
1683 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001684 snprintf(buffer, SIZE,
1685 " orientation=%d, canDraw=%d\n",
1686 mCurrentState.orientation, hw.canDraw());
1687 result.append(buffer);
1688 snprintf(buffer, SIZE,
1689 " last eglSwapBuffers() time: %f us\n"
1690 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001691 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001692 " refresh-rate : %f fps\n"
1693 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001694 " y-dpi : %f\n"
1695 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001696 mLastSwapBufferTime/1000.0,
1697 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001698 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001699 hw.getRefreshRate(),
1700 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001701 hw.getDpiY(),
1702 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001703 result.append(buffer);
1704
1705 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1706 inSwapBuffersDuration/1000.0);
1707 result.append(buffer);
1708
1709 snprintf(buffer, SIZE, " transaction time: %f us\n",
1710 inTransactionDuration/1000.0);
1711 result.append(buffer);
1712
1713 /*
1714 * VSYNC state
1715 */
1716 mEventThread->dump(result, buffer, SIZE);
1717
1718 /*
1719 * Dump HWComposer state
1720 */
1721 HWComposer& hwc(hw.getHwComposer());
1722 snprintf(buffer, SIZE, "h/w composer state:\n");
1723 result.append(buffer);
1724 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1725 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1726 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1727 result.append(buffer);
1728 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1729
1730 /*
1731 * Dump gralloc state
1732 */
1733 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1734 alloc.dump(result);
1735 hw.dump(result);
1736}
1737
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738status_t SurfaceFlinger::onTransact(
1739 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1740{
1741 switch (code) {
1742 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001743 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001746 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001747 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001748 case BLANK:
1749 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 {
1751 // codes that require permission check
1752 IPCThreadState* ipc = IPCThreadState::self();
1753 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001754 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001755 if ((uid != AID_GRAPHICS) &&
1756 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001757 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001758 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1759 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001761 break;
1762 }
1763 case CAPTURE_SCREEN:
1764 {
1765 // codes that require permission check
1766 IPCThreadState* ipc = IPCThreadState::self();
1767 const int pid = ipc->getCallingPid();
1768 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001769 if ((uid != AID_GRAPHICS) &&
1770 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001771 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001772 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1773 return PERMISSION_DENIED;
1774 }
1775 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776 }
1777 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1780 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001781 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001782 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001783 IPCThreadState* ipc = IPCThreadState::self();
1784 const int pid = ipc->getCallingPid();
1785 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001786 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001787 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 return PERMISSION_DENIED;
1789 }
1790 int n;
1791 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001792 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001793 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794 return NO_ERROR;
1795 case 1002: // SHOW_UPDATES
1796 n = data.readInt32();
1797 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001798 invalidateHwcGeometry();
1799 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001800 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001802 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001803 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001804 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001805 case 1005:{ // force transaction
1806 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1807 return NO_ERROR;
1808 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001809 case 1006:{ // send empty update
1810 signalRefresh();
1811 return NO_ERROR;
1812 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001813 case 1008: // toggle use of hw composer
1814 n = data.readInt32();
1815 mDebugDisableHWC = n ? 1 : 0;
1816 invalidateHwcGeometry();
1817 repaintEverything();
1818 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001819 case 1009: // toggle use of transform hint
1820 n = data.readInt32();
1821 mDebugDisableTransformHint = n ? 1 : 0;
1822 invalidateHwcGeometry();
1823 repaintEverything();
1824 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001826 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001827 reply->writeInt32(0);
1828 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001829 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001830 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831 return NO_ERROR;
1832 case 1013: {
1833 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001834 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835 reply->writeInt32(hw.getPageFlipCount());
1836 }
1837 return NO_ERROR;
1838 }
1839 }
1840 return err;
1841}
1842
Mathias Agopian53331da2011-08-22 21:44:41 -07001843void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001844 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001845 const Rect bounds(hw.getBounds());
1846 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001847 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001848}
1849
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001850void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1851 Mutex::Autolock _l(mInvalidateLock);
1852 mInvalidateRegion = reg;
1853}
1854
1855Region SurfaceFlinger::getAndClearInvalidateRegion() {
1856 Mutex::Autolock _l(mInvalidateLock);
1857 Region reg(mInvalidateRegion);
1858 mInvalidateRegion.clear();
1859 return reg;
1860}
1861
Mathias Agopian59119e62010-10-11 12:37:43 -07001862// ---------------------------------------------------------------------------
1863
Mathias Agopian118d0242011-10-13 16:02:48 -07001864status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1865 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1866{
1867 Mutex::Autolock _l(mStateLock);
1868 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1869}
1870
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001871status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1872 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001873{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001874 ATRACE_CALL();
1875
Mathias Agopian59119e62010-10-11 12:37:43 -07001876 if (!GLExtensions::getInstance().haveFramebufferObject())
1877 return INVALID_OPERATION;
1878
1879 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001880 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07001881 const uint32_t hw_w = hw.getWidth();
1882 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001883 GLfloat u = 1;
1884 GLfloat v = 1;
1885
1886 // make sure to clear all GL error flags
1887 while ( glGetError() != GL_NO_ERROR ) ;
1888
1889 // create a FBO
1890 GLuint name, tname;
1891 glGenTextures(1, &tname);
1892 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001893 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1894 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001895 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1896 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001897 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001898 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001899 GLint tw = (2 << (31 - clz(hw_w)));
1900 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001901 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1902 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001903 u = GLfloat(hw_w) / tw;
1904 v = GLfloat(hw_h) / th;
1905 }
1906 glGenFramebuffersOES(1, &name);
1907 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001908 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1909 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001910
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001911 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001912 glDisable(GL_TEXTURE_EXTERNAL_OES);
1913 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001914 glClearColor(0,0,0,1);
1915 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001916 glMatrixMode(GL_MODELVIEW);
1917 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001918 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1919 const size_t count = layers.size();
1920 for (size_t i=0 ; i<count ; ++i) {
1921 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07001922 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001923 }
1924
Mathias Agopian118d0242011-10-13 16:02:48 -07001925 hw.compositionComplete();
1926
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001927 // back to main framebuffer
1928 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001929 glDeleteFramebuffersOES(1, &name);
1930
1931 *textureName = tname;
1932 *uOut = u;
1933 *vOut = v;
1934 return NO_ERROR;
1935}
1936
1937// ---------------------------------------------------------------------------
1938
Mathias Agopianed9807b2012-05-18 14:18:08 -07001939class VSyncWaiter {
1940 DisplayEventReceiver::Event buffer[4];
1941 sp<Looper> looper;
1942 sp<IDisplayEventConnection> events;
1943 sp<BitTube> eventTube;
1944public:
1945 VSyncWaiter(const sp<EventThread>& eventThread) {
1946 looper = new Looper(true);
1947 events = eventThread->createEventConnection();
1948 eventTube = events->getDataChannel();
1949 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1950 events->requestNextVsync();
1951 }
1952
1953 void wait() {
1954 ssize_t n;
1955
1956 looper->pollOnce(-1);
1957 // we don't handle any errors here, it doesn't matter
1958 // and we don't want to take the risk to get stuck.
1959
1960 // drain the events...
1961 while ((n = DisplayEventReceiver::getEvents(
1962 eventTube, buffer, 4)) > 0) ;
1963
1964 events->requestNextVsync();
1965 }
1966};
1967
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001968status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1969{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001970 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001971 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 const uint32_t hw_w = hw.getWidth();
1973 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001974 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001975
1976 GLfloat u, v;
1977 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001978 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001979 if (result != NO_ERROR) {
1980 return result;
1981 }
1982
1983 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001984 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 glBindTexture(GL_TEXTURE_2D, tname);
1986 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1987 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1988 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001989 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1990 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001991 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1992 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1993 glVertexPointer(2, GL_FLOAT, 0, vtx);
1994
Mathias Agopianffcf4652011-07-07 17:30:31 -07001995 /*
1996 * Texture coordinate mapping
1997 *
1998 * u
1999 * 1 +----------+---+
2000 * | | | | image is inverted
2001 * | V | | w.r.t. the texture
2002 * 1-v +----------+ | coordinates
2003 * | |
2004 * | |
2005 * | |
2006 * 0 +--------------+
2007 * 0 1
2008 *
2009 */
2010
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002011 class s_curve_interpolator {
2012 const float nbFrames, s, v;
2013 public:
2014 s_curve_interpolator(int nbFrames, float s)
2015 : nbFrames(1.0f / (nbFrames-1)), s(s),
2016 v(1.0f + expf(-s + 0.5f*s)) {
2017 }
2018 float operator()(int f) {
2019 const float x = f * nbFrames;
2020 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2021 }
2022 };
2023
2024 class v_stretch {
2025 const GLfloat hw_w, hw_h;
2026 public:
2027 v_stretch(uint32_t hw_w, uint32_t hw_h)
2028 : hw_w(hw_w), hw_h(hw_h) {
2029 }
2030 void operator()(GLfloat* vtx, float v) {
2031 const GLfloat w = hw_w + (hw_w * v);
2032 const GLfloat h = hw_h - (hw_h * v);
2033 const GLfloat x = (hw_w - w) * 0.5f;
2034 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002035 vtx[0] = x; vtx[1] = y;
2036 vtx[2] = x; vtx[3] = y + h;
2037 vtx[4] = x + w; vtx[5] = y + h;
2038 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 }
2040 };
2041
2042 class h_stretch {
2043 const GLfloat hw_w, hw_h;
2044 public:
2045 h_stretch(uint32_t hw_w, uint32_t hw_h)
2046 : hw_w(hw_w), hw_h(hw_h) {
2047 }
2048 void operator()(GLfloat* vtx, float v) {
2049 const GLfloat w = hw_w - (hw_w * v);
2050 const GLfloat h = 1.0f;
2051 const GLfloat x = (hw_w - w) * 0.5f;
2052 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002053 vtx[0] = x; vtx[1] = y;
2054 vtx[2] = x; vtx[3] = y + h;
2055 vtx[4] = x + w; vtx[5] = y + h;
2056 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 }
2058 };
2059
Mathias Agopianed9807b2012-05-18 14:18:08 -07002060 VSyncWaiter vsync(mEventThread);
2061
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002062 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002063 char value[PROPERTY_VALUE_MAX];
2064 property_get("debug.sf.electron_frames", value, "24");
2065 int nbFrames = (atoi(value) + 1) >> 1;
2066 if (nbFrames <= 0) // just in case
2067 nbFrames = 24;
2068
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002069 s_curve_interpolator itr(nbFrames, 7.5f);
2070 s_curve_interpolator itg(nbFrames, 8.0f);
2071 s_curve_interpolator itb(nbFrames, 8.5f);
2072
2073 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002074
2075 glMatrixMode(GL_TEXTURE);
2076 glLoadIdentity();
2077 glMatrixMode(GL_MODELVIEW);
2078 glLoadIdentity();
2079
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002080 glEnable(GL_BLEND);
2081 glBlendFunc(GL_ONE, GL_ONE);
2082 for (int i=0 ; i<nbFrames ; i++) {
2083 float x, y, w, h;
2084 const float vr = itr(i);
2085 const float vg = itg(i);
2086 const float vb = itb(i);
2087
Mathias Agopianed9807b2012-05-18 14:18:08 -07002088 // wait for vsync
2089 vsync.wait();
2090
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002091 // clear screen
2092 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002093 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002094 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002095
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002096 // draw the red plane
2097 vverts(vtx, vr);
2098 glColorMask(1,0,0,1);
2099 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002100
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002101 // draw the green plane
2102 vverts(vtx, vg);
2103 glColorMask(0,1,0,1);
2104 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002105
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 // draw the blue plane
2107 vverts(vtx, vb);
2108 glColorMask(0,0,1,1);
2109 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002110
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002111 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002112 glDisable(GL_TEXTURE_2D);
2113 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002114 glColor4f(vg, vg, vg, 1);
2115 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2116 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002117 }
2118
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002119 h_stretch hverts(hw_w, hw_h);
2120 glDisable(GL_BLEND);
2121 glDisable(GL_TEXTURE_2D);
2122 glColorMask(1,1,1,1);
2123 for (int i=0 ; i<nbFrames ; i++) {
2124 const float v = itg(i);
2125 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002126
2127 // wait for vsync
2128 vsync.wait();
2129
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002130 glClear(GL_COLOR_BUFFER_BIT);
2131 glColor4f(1-v, 1-v, 1-v, 1);
2132 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2133 hw.flip(screenBounds);
2134 }
2135
2136 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002137 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2138 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002139 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002140 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002141 return NO_ERROR;
2142}
2143
2144status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2145{
2146 status_t result = PERMISSION_DENIED;
2147
2148 if (!GLExtensions::getInstance().haveFramebufferObject())
2149 return INVALID_OPERATION;
2150
2151
2152 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002153 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002154 const uint32_t hw_w = hw.getWidth();
2155 const uint32_t hw_h = hw.getHeight();
2156 const Region screenBounds(hw.bounds());
2157
2158 GLfloat u, v;
2159 GLuint tname;
2160 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2161 if (result != NO_ERROR) {
2162 return result;
2163 }
2164
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002165 GLfloat vtx[8];
2166 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002167 glBindTexture(GL_TEXTURE_2D, tname);
2168 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2169 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2170 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002171 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2172 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002173 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2174 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2175 glVertexPointer(2, GL_FLOAT, 0, vtx);
2176
2177 class s_curve_interpolator {
2178 const float nbFrames, s, v;
2179 public:
2180 s_curve_interpolator(int nbFrames, float s)
2181 : nbFrames(1.0f / (nbFrames-1)), s(s),
2182 v(1.0f + expf(-s + 0.5f*s)) {
2183 }
2184 float operator()(int f) {
2185 const float x = f * nbFrames;
2186 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2187 }
2188 };
2189
2190 class v_stretch {
2191 const GLfloat hw_w, hw_h;
2192 public:
2193 v_stretch(uint32_t hw_w, uint32_t hw_h)
2194 : hw_w(hw_w), hw_h(hw_h) {
2195 }
2196 void operator()(GLfloat* vtx, float v) {
2197 const GLfloat w = hw_w + (hw_w * v);
2198 const GLfloat h = hw_h - (hw_h * v);
2199 const GLfloat x = (hw_w - w) * 0.5f;
2200 const GLfloat y = (hw_h - h) * 0.5f;
2201 vtx[0] = x; vtx[1] = y;
2202 vtx[2] = x; vtx[3] = y + h;
2203 vtx[4] = x + w; vtx[5] = y + h;
2204 vtx[6] = x + w; vtx[7] = y;
2205 }
2206 };
2207
2208 class h_stretch {
2209 const GLfloat hw_w, hw_h;
2210 public:
2211 h_stretch(uint32_t hw_w, uint32_t hw_h)
2212 : hw_w(hw_w), hw_h(hw_h) {
2213 }
2214 void operator()(GLfloat* vtx, float v) {
2215 const GLfloat w = hw_w - (hw_w * v);
2216 const GLfloat h = 1.0f;
2217 const GLfloat x = (hw_w - w) * 0.5f;
2218 const GLfloat y = (hw_h - h) * 0.5f;
2219 vtx[0] = x; vtx[1] = y;
2220 vtx[2] = x; vtx[3] = y + h;
2221 vtx[4] = x + w; vtx[5] = y + h;
2222 vtx[6] = x + w; vtx[7] = y;
2223 }
2224 };
2225
Mathias Agopianed9807b2012-05-18 14:18:08 -07002226 VSyncWaiter vsync(mEventThread);
2227
Mathias Agopiana6546e52010-10-14 12:33:07 -07002228 // the full animation is 12 frames
2229 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002230 s_curve_interpolator itr(nbFrames, 7.5f);
2231 s_curve_interpolator itg(nbFrames, 8.0f);
2232 s_curve_interpolator itb(nbFrames, 8.5f);
2233
2234 h_stretch hverts(hw_w, hw_h);
2235 glDisable(GL_BLEND);
2236 glDisable(GL_TEXTURE_2D);
2237 glColorMask(1,1,1,1);
2238 for (int i=nbFrames-1 ; i>=0 ; i--) {
2239 const float v = itg(i);
2240 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002241
2242 // wait for vsync
2243 vsync.wait();
2244
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002245 glClear(GL_COLOR_BUFFER_BIT);
2246 glColor4f(1-v, 1-v, 1-v, 1);
2247 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2248 hw.flip(screenBounds);
2249 }
2250
Mathias Agopiana6546e52010-10-14 12:33:07 -07002251 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002252 v_stretch vverts(hw_w, hw_h);
2253 glEnable(GL_BLEND);
2254 glBlendFunc(GL_ONE, GL_ONE);
2255 for (int i=nbFrames-1 ; i>=0 ; i--) {
2256 float x, y, w, h;
2257 const float vr = itr(i);
2258 const float vg = itg(i);
2259 const float vb = itb(i);
2260
Mathias Agopianed9807b2012-05-18 14:18:08 -07002261 // wait for vsync
2262 vsync.wait();
2263
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264 // clear screen
2265 glColorMask(1,1,1,1);
2266 glClear(GL_COLOR_BUFFER_BIT);
2267 glEnable(GL_TEXTURE_2D);
2268
2269 // draw the red plane
2270 vverts(vtx, vr);
2271 glColorMask(1,0,0,1);
2272 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2273
2274 // draw the green plane
2275 vverts(vtx, vg);
2276 glColorMask(0,1,0,1);
2277 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2278
2279 // draw the blue plane
2280 vverts(vtx, vb);
2281 glColorMask(0,0,1,1);
2282 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2283
2284 hw.flip(screenBounds);
2285 }
2286
2287 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002288 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002289 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002290 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002291 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002292
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002293 return NO_ERROR;
2294}
2295
2296// ---------------------------------------------------------------------------
2297
Mathias Agopianabd671a2010-10-14 14:54:06 -07002298status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002299{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002300 ATRACE_CALL();
2301
Mathias Agopian1b031492012-06-20 17:51:20 -07002302 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002303 if (!hw.canDraw()) {
2304 // we're already off
2305 return NO_ERROR;
2306 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002307
2308 // turn off hwc while we're doing the animation
2309 hw.getHwComposer().disable();
2310 // and make sure to turn it back on (if needed) next time we compose
2311 invalidateHwcGeometry();
2312
Mathias Agopianabd671a2010-10-14 14:54:06 -07002313 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2314 electronBeamOffAnimationImplLocked();
2315 }
2316
2317 // always clear the whole screen at the end of the animation
2318 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002319 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002320 hw.flip( Region(hw.bounds()) );
2321
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002322 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002323}
2324
2325status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2326{
Mathias Agopian59119e62010-10-11 12:37:43 -07002327 class MessageTurnElectronBeamOff : public MessageBase {
2328 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002329 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002330 status_t result;
2331 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002332 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2333 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002334 }
2335 status_t getResult() const {
2336 return result;
2337 }
2338 virtual bool handler() {
2339 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002340 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002341 return true;
2342 }
2343 };
2344
Mathias Agopianabd671a2010-10-14 14:54:06 -07002345 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002346 status_t res = postMessageSync(msg);
2347 if (res == NO_ERROR) {
2348 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002349
2350 // work-around: when the power-manager calls us we activate the
2351 // animation. eventually, the "on" animation will be called
2352 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002353 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002354 }
2355 return res;
2356}
2357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002358// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002359
Mathias Agopianabd671a2010-10-14 14:54:06 -07002360status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002361{
Mathias Agopian1b031492012-06-20 17:51:20 -07002362 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002363 if (hw.canDraw()) {
2364 // we're already on
2365 return NO_ERROR;
2366 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002367 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2368 electronBeamOnAnimationImplLocked();
2369 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002370
2371 // make sure to redraw the whole screen when the animation is done
2372 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002373 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002374
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002375 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002376}
2377
2378status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2379{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002380 class MessageTurnElectronBeamOn : public MessageBase {
2381 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002382 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002383 status_t result;
2384 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002385 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2386 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002387 }
2388 status_t getResult() const {
2389 return result;
2390 }
2391 virtual bool handler() {
2392 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002393 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002394 return true;
2395 }
2396 };
2397
Mathias Agopianabd671a2010-10-14 14:54:06 -07002398 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002399 return NO_ERROR;
2400}
2401
2402// ---------------------------------------------------------------------------
2403
Mathias Agopian74c40c02010-09-29 13:02:36 -07002404status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2405 sp<IMemoryHeap>* heap,
2406 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 uint32_t sw, uint32_t sh,
2408 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002409{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002410 ATRACE_CALL();
2411
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 status_t result = PERMISSION_DENIED;
2413
2414 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002415 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002416 return BAD_VALUE;
2417
2418 if (!GLExtensions::getInstance().haveFramebufferObject())
2419 return INVALID_OPERATION;
2420
2421 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002422 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 const uint32_t hw_w = hw.getWidth();
2424 const uint32_t hw_h = hw.getHeight();
2425
2426 if ((sw > hw_w) || (sh > hw_h))
2427 return BAD_VALUE;
2428
2429 sw = (!sw) ? hw_w : sw;
2430 sh = (!sh) ? hw_h : sh;
2431 const size_t size = sw * sh * 4;
2432
Steve Block9d453682011-12-20 16:23:08 +00002433 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002434 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002435
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 // make sure to clear all GL error flags
2437 while ( glGetError() != GL_NO_ERROR ) ;
2438
2439 // create a FBO
2440 GLuint name, tname;
2441 glGenRenderbuffersOES(1, &tname);
2442 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2443 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002444
Mathias Agopian74c40c02010-09-29 13:02:36 -07002445 glGenFramebuffersOES(1, &name);
2446 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2447 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2448 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2449
2450 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002451
Mathias Agopian74c40c02010-09-29 13:02:36 -07002452 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2453
2454 // invert everything, b/c glReadPixel() below will invert the FB
2455 glViewport(0, 0, sw, sh);
2456 glMatrixMode(GL_PROJECTION);
2457 glPushMatrix();
2458 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002459 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002460 glMatrixMode(GL_MODELVIEW);
2461
2462 // redraw the screen entirely...
2463 glClearColor(0,0,0,1);
2464 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002465
Jamie Gennis9575f602011-10-07 14:51:16 -07002466 const LayerVector& layers(mDrawingState.layersSortedByZ);
2467 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002468 for (size_t i=0 ; i<count ; ++i) {
2469 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002470 const uint32_t flags = layer->drawingState().flags;
2471 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2472 const uint32_t z = layer->drawingState().z;
2473 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002474 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002475 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002476 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002477 }
2478
Mathias Agopian74c40c02010-09-29 13:02:36 -07002479 // check for errors and return screen capture
2480 if (glGetError() != GL_NO_ERROR) {
2481 // error while rendering
2482 result = INVALID_OPERATION;
2483 } else {
2484 // allocate shared memory large enough to hold the
2485 // screen capture
2486 sp<MemoryHeapBase> base(
2487 new MemoryHeapBase(size, 0, "screen-capture") );
2488 void* const ptr = base->getBase();
2489 if (ptr) {
2490 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002491 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002492 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2493 if (glGetError() == GL_NO_ERROR) {
2494 *heap = base;
2495 *w = sw;
2496 *h = sh;
2497 *f = PIXEL_FORMAT_RGBA_8888;
2498 result = NO_ERROR;
2499 }
2500 } else {
2501 result = NO_MEMORY;
2502 }
2503 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002504 glViewport(0, 0, hw_w, hw_h);
2505 glMatrixMode(GL_PROJECTION);
2506 glPopMatrix();
2507 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002508 } else {
2509 result = BAD_VALUE;
2510 }
2511
2512 // release FBO resources
2513 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2514 glDeleteRenderbuffersOES(1, &tname);
2515 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002516
2517 hw.compositionComplete();
2518
Steve Block9d453682011-12-20 16:23:08 +00002519 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002520
Mathias Agopian74c40c02010-09-29 13:02:36 -07002521 return result;
2522}
2523
2524
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002525status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2526 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002527 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002528 uint32_t sw, uint32_t sh,
2529 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002530{
2531 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002532 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002533 return BAD_VALUE;
2534
2535 if (!GLExtensions::getInstance().haveFramebufferObject())
2536 return INVALID_OPERATION;
2537
2538 class MessageCaptureScreen : public MessageBase {
2539 SurfaceFlinger* flinger;
2540 DisplayID dpy;
2541 sp<IMemoryHeap>* heap;
2542 uint32_t* w;
2543 uint32_t* h;
2544 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002545 uint32_t sw;
2546 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002547 uint32_t minLayerZ;
2548 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002549 status_t result;
2550 public:
2551 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002552 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002553 uint32_t sw, uint32_t sh,
2554 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002555 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002556 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2557 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2558 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002559 {
2560 }
2561 status_t getResult() const {
2562 return result;
2563 }
2564 virtual bool handler() {
2565 Mutex::Autolock _l(flinger->mStateLock);
2566
2567 // if we have secure windows, never allow the screen capture
2568 if (flinger->mSecureFrameBuffer)
2569 return true;
2570
Mathias Agopian74c40c02010-09-29 13:02:36 -07002571 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002572 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002573
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002574 return true;
2575 }
2576 };
2577
2578 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002579 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002580 status_t res = postMessageSync(msg);
2581 if (res == NO_ERROR) {
2582 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2583 }
2584 return res;
2585}
2586
2587// ---------------------------------------------------------------------------
2588
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002589sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2590{
2591 sp<Layer> result;
2592 Mutex::Autolock _l(mStateLock);
2593 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2594 return result;
2595}
2596
2597// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598
Jamie Gennis9a78c902011-01-12 18:30:40 -08002599GraphicBufferAlloc::GraphicBufferAlloc() {}
2600
2601GraphicBufferAlloc::~GraphicBufferAlloc() {}
2602
2603sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002604 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002605 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2606 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002607 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002608 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002609 if (err == NO_MEMORY) {
2610 GraphicBuffer::dumpAllocationsToSystemLog();
2611 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002612 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002613 "failed (%s), handle=%p",
2614 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002615 return 0;
2616 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002617 return graphicBuffer;
2618}
2619
Jamie Gennis9a78c902011-01-12 18:30:40 -08002620// ---------------------------------------------------------------------------
2621
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002622}; // namespace android