blob: 6dfbe5b55f208f97e18c3e7e174ab386fa27e435 [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());
Mathias Agopiana44b0412011-10-16 18:46:35 -0700531 const nsecs_t now = systemTime();
532 mDebugInSwapBuffers = now;
533 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800534
535 size_t numLayers = mVisibleLayersSortedByZ.size();
Mathias Agopian1b031492012-06-20 17:51:20 -0700536 HWComposer& hwc(hw.getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700537 if (hwc.initCheck() == NO_ERROR) {
538 HWComposer::LayerListIterator cur = hwc.begin();
539 const HWComposer::LayerListIterator end = hwc.end();
540 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
541 mVisibleLayersSortedByZ[i]->onLayerDisplayed(&*cur);
542 }
543 } else {
544 for (size_t i = 0; i < numLayers; i++) {
545 mVisibleLayersSortedByZ[i]->onLayerDisplayed(NULL);
546 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800547 }
548
Mathias Agopiana44b0412011-10-16 18:46:35 -0700549 mLastSwapBufferTime = systemTime() - now;
550 mDebugInSwapBuffers = 0;
551 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552}
553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
555{
Mathias Agopian841cde52012-03-01 15:44:37 -0800556 ATRACE_CALL();
557
Mathias Agopianca4d3602011-05-19 15:38:14 -0700558 Mutex::Autolock _l(mStateLock);
559 const nsecs_t now = systemTime();
560 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561
Mathias Agopianca4d3602011-05-19 15:38:14 -0700562 // Here we're guaranteed that some transaction flags are set
563 // so we can call handleTransactionLocked() unconditionally.
564 // We call getTransactionFlags(), which will also clear the flags,
565 // with mStateLock held to guarantee that mCurrentState won't change
566 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700567
Mathias Agopianca4d3602011-05-19 15:38:14 -0700568 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
569 transactionFlags = getTransactionFlags(mask);
570 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700571
Mathias Agopianca4d3602011-05-19 15:38:14 -0700572 mLastTransactionTime = systemTime() - now;
573 mDebugInTransaction = 0;
574 invalidateHwcGeometry();
575 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700576}
577
Mathias Agopianca4d3602011-05-19 15:38:14 -0700578void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700579{
580 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 const size_t count = currentLayers.size();
582
583 /*
584 * Traversal of the children
585 * (perform the transaction for each of them if needed)
586 */
587
588 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
589 if (layersNeedTransaction) {
590 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
593 if (!trFlags) continue;
594
595 const uint32_t flags = layer->doTransaction(0);
596 if (flags & Layer::eVisibleRegion)
597 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 }
599 }
600
601 /*
602 * Perform our own transaction if needed
603 */
604
605 if (transactionFlags & eTransactionNeeded) {
606 if (mCurrentState.orientation != mDrawingState.orientation) {
607 // the orientation has changed, recompute all visible regions
608 // and invalidate everything.
609
Mathias Agopian1b031492012-06-20 17:51:20 -0700610 const int dpy = 0; // TODO: should be a parameter
611 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 const int orientation = mCurrentState.orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700613 hw.setOrientation(orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614
615 // update the shared control block
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
617 dcblk->orientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700618 dcblk->w = hw.getUserWidth();
619 dcblk->h = hw.getUserHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620
Mathias Agopian1b031492012-06-20 17:51:20 -0700621 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 mVisibleRegionsDirty = true;
623 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 }
625
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700626 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
627 // layers have been added
628 mVisibleRegionsDirty = true;
629 }
630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 // some layers might have been removed, so
632 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700633 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700634 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700636 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700637 const size_t count = previousLayers.size();
638 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700639 const sp<LayerBase>& layer(previousLayers[i]);
640 if (currentLayers.indexOf( layer ) < 0) {
641 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700642 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700643 }
644 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 }
647
648 commitTransaction();
649}
650
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800652 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653{
Mathias Agopian841cde52012-03-01 15:44:37 -0800654 ATRACE_CALL();
655
Mathias Agopian1b031492012-06-20 17:51:20 -0700656 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: we shouldn't rely on DisplayHardware here
657 const Transform& planeTransform(hw.getTransform());
Mathias Agopianab028732010-03-16 16:41:46 -0700658 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659
660 Region aboveOpaqueLayers;
661 Region aboveCoveredLayers;
662 Region dirty;
663
664 bool secureFrameBuffer = false;
665
666 size_t i = currentLayers.size();
667 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700668 const sp<LayerBase>& layer = currentLayers[i];
Mathias Agopian1b031492012-06-20 17:51:20 -0700669 layer->validateVisibility(planeTransform, hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670
671 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700672 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673
Mathias Agopianab028732010-03-16 16:41:46 -0700674 /*
675 * opaqueRegion: area of a surface that is fully opaque.
676 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700678
679 /*
680 * visibleRegion: area of a surface that is visible on screen
681 * and not fully transparent. This is essentially the layer's
682 * footprint minus the opaque regions above it.
683 * Areas covered by a translucent surface are considered visible.
684 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700686
687 /*
688 * coveredRegion: area of a surface that is covered by all
689 * visible regions above it (which includes the translucent areas).
690 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700692
693
694 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800695 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700696 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700697 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700699 visibleRegion.andSelf(screenRegion);
700 if (!visibleRegion.isEmpty()) {
701 // Remove the transparent area from the visible region
702 if (translucent) {
703 visibleRegion.subtractSelf(layer->transparentRegionScreen);
704 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705
Mathias Agopianab028732010-03-16 16:41:46 -0700706 // compute the opaque region
707 const int32_t layerOrientation = layer->getOrientation();
708 if (s.alpha==255 && !translucent &&
709 ((layerOrientation & Transform::ROT_INVALID) == false)) {
710 // the opaque region is the layer's footprint
711 opaqueRegion = visibleRegion;
712 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 }
714 }
715
Mathias Agopianab028732010-03-16 16:41:46 -0700716 // Clip the covered region to the visible region
717 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
718
719 // Update aboveCoveredLayers for next (lower) layer
720 aboveCoveredLayers.orSelf(visibleRegion);
721
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 // subtract the opaque region covered by the layers above us
723 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724
725 // compute this layer's dirty region
726 if (layer->contentDirty) {
727 // we need to invalidate the whole region
728 dirty = visibleRegion;
729 // as well, as the old visible region
730 dirty.orSelf(layer->visibleRegionScreen);
731 layer->contentDirty = false;
732 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700733 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700734 * the exposed region consists of two components:
735 * 1) what's VISIBLE now and was COVERED before
736 * 2) what's EXPOSED now less what was EXPOSED before
737 *
738 * note that (1) is conservative, we start with the whole
739 * visible region but only keep what used to be covered by
740 * something -- which mean it may have been exposed.
741 *
742 * (2) handles areas that were not covered by anything but got
743 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700744 */
Mathias Agopianab028732010-03-16 16:41:46 -0700745 const Region newExposed = visibleRegion - coveredRegion;
746 const Region oldVisibleRegion = layer->visibleRegionScreen;
747 const Region oldCoveredRegion = layer->coveredRegionScreen;
748 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
749 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 }
751 dirty.subtractSelf(aboveOpaqueLayers);
752
753 // accumulate to the screen dirty region
754 dirtyRegion.orSelf(dirty);
755
Mathias Agopianab028732010-03-16 16:41:46 -0700756 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700758
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 // Store the visible region is screen space
760 layer->setVisibleRegion(visibleRegion);
761 layer->setCoveredRegion(coveredRegion);
762
Mathias Agopian97011222009-07-28 10:57:27 -0700763 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 if (layer->isSecure() && !visibleRegion.isEmpty()) {
765 secureFrameBuffer = true;
766 }
767 }
768
Mathias Agopian97011222009-07-28 10:57:27 -0700769 // invalidate the areas where a layer was removed
770 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
771 mDirtyRegionRemovedLayer.clear();
772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 mSecureFrameBuffer = secureFrameBuffer;
774 opaqueRegion = aboveOpaqueLayers;
775}
776
777
778void SurfaceFlinger::commitTransaction()
779{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800780 if (!mLayersPendingRemoval.isEmpty()) {
781 // Notify removed layers now that they can't be drawn from
782 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
783 mLayersPendingRemoval[i]->onRemoved();
784 }
785 mLayersPendingRemoval.clear();
786 }
787
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700789 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700790 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791}
792
793void SurfaceFlinger::handlePageFlip()
794{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800795 ATRACE_CALL();
Mathias Agopian1b031492012-06-20 17:51:20 -0700796 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: it's a problem we need DisplayHardware here
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800797 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800799 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
800 const bool visibleRegions = lockPageFlip(currentLayers);
801
802 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 Region opaqueRegion;
804 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700805
806 /*
807 * rebuild the visible layer list
808 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800809 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700810 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700811 mVisibleLayersSortedByZ.setCapacity(count);
812 for (size_t i=0 ; i<count ; i++) {
813 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
814 mVisibleLayersSortedByZ.add(currentLayers[i]);
815 }
816
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 mWormholeRegion = screenRegion.subtract(opaqueRegion);
818 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800819 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820 }
821
822 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700823
824 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 mDirtyRegion.andSelf(screenRegion);
826}
827
Mathias Agopianad456f92011-01-13 17:53:01 -0800828void SurfaceFlinger::invalidateHwcGeometry()
829{
830 mHwWorkListDirty = true;
831}
832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
834{
835 bool recomputeVisibleRegions = false;
836 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700837 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700839 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 layer->lockPageFlip(recomputeVisibleRegions);
841 }
842 return recomputeVisibleRegions;
843}
844
845void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
846{
Mathias Agopian1b031492012-06-20 17:51:20 -0700847 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: it's a problem we need DisplayHardware here
848 const Transform& planeTransform(hw.getTransform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800849 const 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->unlockPageFlip(planeTransform, mDirtyRegion);
854 }
855}
856
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800857void SurfaceFlinger::handleRefresh()
858{
859 bool needInvalidate = false;
860 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
861 const size_t count = currentLayers.size();
862 for (size_t i=0 ; i<count ; i++) {
863 const sp<LayerBase>& layer(currentLayers[i]);
864 if (layer->onPreComposition()) {
865 needInvalidate = true;
866 }
867 }
868 if (needInvalidate) {
869 signalLayerUpdate();
870 }
871}
872
873
Mathias Agopian1b031492012-06-20 17:51:20 -0700874void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700875{
876 mHwWorkListDirty = false;
Mathias Agopian1b031492012-06-20 17:51:20 -0700877 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700878 if (hwc.initCheck() == NO_ERROR) {
879 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
880 const size_t count = currentLayers.size();
881 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700882
883 HWComposer::LayerListIterator cur = hwc.begin();
884 const HWComposer::LayerListIterator end = hwc.end();
885 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
886 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700887 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700888 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700889 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700890 }
891 }
892}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700893
Mathias Agopian1b031492012-06-20 17:51:20 -0700894void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895{
Mathias Agopian841cde52012-03-01 15:44:37 -0800896 ATRACE_CALL();
897
Mathias Agopianb8a55602009-06-26 19:06:36 -0700898 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700899 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900
Glenn Kasten99ed2242011-12-15 09:51:17 -0800901 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700902 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 }
904
Mathias Agopianb8a55602009-06-26 19:06:36 -0700905 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -0700906 glMatrixMode(GL_MODELVIEW);
907 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800908
909 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700910 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700911 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
912 // takes a rectangle, we must make sure to update that whole
913 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700914 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700915 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700916 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700917 // We need to redraw the rectangle that will be updated
918 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700919 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700920 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700921 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700922 } else {
923 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700925 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 }
927 }
928
Mathias Agopian1b031492012-06-20 17:51:20 -0700929 setupHardwareComposer(hw);
930 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700932 // update the swap region and clear the dirty region
933 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 mDirtyRegion.clear();
935}
936
Mathias Agopian1b031492012-06-20 17:51:20 -0700937void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700939 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700940 HWComposer::LayerListIterator cur = hwc.begin();
941 const HWComposer::LayerListIterator end = hwc.end();
942 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700943 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700944 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700945
Mathias Agopianf384cc32011-09-08 18:31:55 -0700946 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
947 size_t count = layers.size();
948
Steve Blocke6f43dd2012-01-06 19:20:56 +0000949 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700950 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
951 hwc.getNumLayers(), count);
952
953 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700954 if (hwc.initCheck() == NO_ERROR) {
955 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
956 }
Mathias Agopian45721772010-08-12 15:03:26 -0700957
958 /*
959 * update the per-frame h/w composer data for each layer
960 * and build the transparent region of the FB
961 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700962 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700963 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700964 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700965 }
966 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000967 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700968}
Mathias Agopian45721772010-08-12 15:03:26 -0700969
Mathias Agopian1b031492012-06-20 17:51:20 -0700970void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -0700971{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700972 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700973 HWComposer::LayerListIterator cur = hwc.begin();
974 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700975
976 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700977 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700978 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700979
Mathias Agopianb9494d52012-04-18 02:28:45 -0700980 if (hwc.getLayerCount(HWC_OVERLAY)) {
981 // when using overlays, we assume a fully transparent framebuffer
982 // NOTE: we could reduce how much we need to clear, for instance
983 // remove where there are opaque FB layers. however, on some
984 // GPUs doing a "clean slate" glClear might be more efficient.
985 // We'll revisit later if needed.
986 glClearColor(0, 0, 0, 0);
987 glClear(GL_COLOR_BUFFER_BIT);
988 } else {
989 // screen is already cleared here
990 if (!mWormholeRegion.isEmpty()) {
991 // can happen with SurfaceView
992 drawWormhole();
993 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700994 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700995
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700996 /*
997 * and then, render the layers targeted at the framebuffer
998 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700999
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001000 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1001 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001002 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001003 const sp<LayerBase>& layer(layers[i]);
1004 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1005 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001006 if (cur->getCompositionType() == HWC_OVERLAY) {
1007 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001008 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001009 // never clear the very first layer since we're
1010 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001011 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001012 }
1013 continue;
1014 }
1015 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001016 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001017 }
1018 }
1019 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020}
1021
Mathias Agopian1b031492012-06-20 17:51:20 -07001022void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023{
Mathias Agopian0a917752010-06-14 21:20:00 -07001024 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001025 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001026 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001027 return;
1028 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001029
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001030 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001031 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1032 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001033 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001034 }
1035
Mathias Agopianc492e672011-10-18 14:49:27 -07001036 glDisable(GL_TEXTURE_EXTERNAL_OES);
1037 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039
Mathias Agopian0926f502009-05-04 14:17:04 -07001040 static int toggle = 0;
1041 toggle = 1 - toggle;
1042 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001043 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001044 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001045 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001046 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047
Mathias Agopian20f68782009-05-11 00:03:41 -07001048 Region::const_iterator it = mDirtyRegion.begin();
1049 Region::const_iterator const end = mDirtyRegion.end();
1050 while (it != end) {
1051 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001053 { r.left, height - r.top },
1054 { r.left, height - r.bottom },
1055 { r.right, height - r.bottom },
1056 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 };
1058 glVertexPointer(2, GL_FLOAT, 0, vertices);
1059 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1060 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001061
Mathias Agopian0656a682011-09-20 17:22:44 -07001062 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063
1064 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001065 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066}
1067
1068void SurfaceFlinger::drawWormhole() const
1069{
1070 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1071 if (region.isEmpty())
1072 return;
1073
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001074 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001075 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001076 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001077 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001078
1079 GLfloat vertices[4][2];
1080 glVertexPointer(2, GL_FLOAT, 0, vertices);
1081 Region::const_iterator it = region.begin();
1082 Region::const_iterator const end = region.end();
1083 while (it != end) {
1084 const Rect& r = *it++;
1085 vertices[0][0] = r.left;
1086 vertices[0][1] = r.top;
1087 vertices[1][0] = r.right;
1088 vertices[1][1] = r.top;
1089 vertices[2][0] = r.right;
1090 vertices[2][1] = r.bottom;
1091 vertices[3][0] = r.left;
1092 vertices[3][1] = r.bottom;
1093 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1094 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001097status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098{
1099 Mutex::Autolock _l(mStateLock);
1100 addLayer_l(layer);
1101 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1102 return NO_ERROR;
1103}
1104
Mathias Agopian96f08192010-06-02 23:28:45 -07001105status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1106{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001107 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001108 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1109}
1110
1111ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1112 const sp<LayerBaseClient>& lbc)
1113{
Mathias Agopian96f08192010-06-02 23:28:45 -07001114 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001115 size_t name = client->attachLayer(lbc);
1116
1117 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001118
1119 // add this layer to the current state list
1120 addLayer_l(lbc);
1121
Mathias Agopian4f113742011-05-03 16:21:41 -07001122 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001123}
1124
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001125status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126{
1127 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001128 status_t err = purgatorizeLayer_l(layer);
1129 if (err == NO_ERROR)
1130 setTransactionFlags(eTransactionNeeded);
1131 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132}
1133
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001134status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001136 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1137 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001138 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001139 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1141 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001142 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 return NO_ERROR;
1144 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001145 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146}
1147
Mathias Agopian9a112062009-04-17 19:36:26 -07001148status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1149{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001150 // First add the layer to the purgatory list, which makes sure it won't
1151 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001152 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001153 if (err >= 0) {
1154 mLayerPurgatory.add(layerBase);
1155 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001156
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001157 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001158
Mathias Agopian3d579642009-06-04 18:46:21 -07001159 // it's possible that we don't find a layer, because it might
1160 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001161 // from the user because there is a race between Client::destroySurface(),
1162 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001163 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1164}
1165
Mathias Agopian96f08192010-06-02 23:28:45 -07001166status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167{
Mathias Agopian96f08192010-06-02 23:28:45 -07001168 layer->forceVisibilityTransaction();
1169 setTransactionFlags(eTraversalNeeded);
1170 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171}
1172
Mathias Agopiandea20b12011-05-03 17:04:02 -07001173uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1174{
1175 return android_atomic_release_load(&mTransactionFlags);
1176}
1177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1179{
1180 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1181}
1182
Mathias Agopianbb641242010-05-18 17:06:55 -07001183uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184{
1185 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1186 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001187 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 }
1189 return old;
1190}
1191
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001193void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001194 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001195 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001196
Jamie Gennis28378392011-10-12 17:39:00 -07001197 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001198 if (mCurrentState.orientation != orientation) {
1199 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1200 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001201 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001202 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001203 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001204 orientation);
1205 }
1206 }
1207
Mathias Agopian698c0872011-06-28 19:09:31 -07001208 const size_t count = state.size();
1209 for (size_t i=0 ; i<count ; i++) {
1210 const ComposerState& s(state[i]);
1211 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001212 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001213 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001214
Mathias Agopian386aa982011-11-07 21:58:03 -08001215 if (transactionFlags) {
1216 // this triggers the transaction
1217 setTransactionFlags(transactionFlags);
1218
1219 // if this is a synchronous transaction, wait for it to take effect
1220 // before returning.
1221 if (flags & eSynchronous) {
1222 mTransationPending = true;
1223 }
1224 while (mTransationPending) {
1225 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1226 if (CC_UNLIKELY(err != NO_ERROR)) {
1227 // just in case something goes wrong in SF, return to the
1228 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001229 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001230 mTransationPending = false;
1231 break;
1232 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001233 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 }
1235}
1236
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001237sp<ISurface> SurfaceFlinger::createSurface(
1238 ISurfaceComposerClient::surface_data_t* params,
1239 const String8& name,
1240 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1242 uint32_t flags)
1243{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001244 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001245 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001246
1247 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001248 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001249 int(w), int(h));
1250 return surfaceHandle;
1251 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001252
Mathias Agopianff615cc2012-02-24 14:58:36 -08001253 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001254 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 switch (flags & eFXSurfaceMask) {
1256 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001257 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1258 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 break;
1260 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001261 // for now we treat Blur as Dim, until we can implement it
1262 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001264 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001266 case eFXSurfaceScreenshot:
1267 layer = createScreenshotSurface(client, d, w, h, flags);
1268 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 }
1270
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001271 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001272 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001273 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001274 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001277 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001278 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001279 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001280 if (normalLayer != 0) {
1281 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001282 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001283 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001284 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001285
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 }
1288
1289 return surfaceHandle;
1290}
1291
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001292sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001293 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001294 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001295 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296{
1297 // initialize the surfaces
1298 switch (format) { // TODO: take h/w into account
1299 case PIXEL_FORMAT_TRANSPARENT:
1300 case PIXEL_FORMAT_TRANSLUCENT:
1301 format = PIXEL_FORMAT_RGBA_8888;
1302 break;
1303 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001304#ifdef NO_RGBX_8888
1305 format = PIXEL_FORMAT_RGB_565;
1306#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001307 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001308#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 break;
1310 }
1311
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001312#ifdef NO_RGBX_8888
1313 if (format == PIXEL_FORMAT_RGBX_8888)
1314 format = PIXEL_FORMAT_RGBA_8888;
1315#endif
1316
Mathias Agopian96f08192010-06-02 23:28:45 -07001317 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001318 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001319 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001320 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001321 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 }
1323 return layer;
1324}
1325
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001326sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001327 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001328 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329{
Mathias Agopian96f08192010-06-02 23:28:45 -07001330 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001331 return layer;
1332}
1333
1334sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1335 const sp<Client>& client, DisplayID display,
1336 uint32_t w, uint32_t h, uint32_t flags)
1337{
1338 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 return layer;
1340}
1341
Mathias Agopian96f08192010-06-02 23:28:45 -07001342status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343{
Mathias Agopian9a112062009-04-17 19:36:26 -07001344 /*
1345 * called by the window manager, when a surface should be marked for
1346 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001347 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001348 * The surface is removed from the current and drawing lists, but placed
1349 * in the purgatory queue, so it's not destroyed right-away (we need
1350 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001351 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001352
Mathias Agopian48d819a2009-09-10 19:41:18 -07001353 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001354 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001355 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001356
Mathias Agopian48d819a2009-09-10 19:41:18 -07001357 if (layer != 0) {
1358 err = purgatorizeLayer_l(layer);
1359 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001360 setTransactionFlags(eTransactionNeeded);
1361 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001362 }
1363 return err;
1364}
1365
Mathias Agopianca4d3602011-05-19 15:38:14 -07001366status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001367{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001368 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001369 status_t err = NO_ERROR;
1370 sp<LayerBaseClient> l(layer.promote());
1371 if (l != NULL) {
1372 Mutex::Autolock _l(mStateLock);
1373 err = removeLayer_l(l);
1374 if (err == NAME_NOT_FOUND) {
1375 // The surface wasn't in the current list, which means it was
1376 // removed already, which means it is in the purgatory,
1377 // and need to be removed from there.
1378 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001379 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001380 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001381 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001382 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001383 "error removing layer=%p (%s)", l.get(), strerror(-err));
1384 }
1385 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386}
1387
Mathias Agopian698c0872011-06-28 19:09:31 -07001388uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001389 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001390 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001393 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1394 if (layer != 0) {
1395 const uint32_t what = s.what;
1396 if (what & ePositionChanged) {
1397 if (layer->setPosition(s.x, s.y))
1398 flags |= eTraversalNeeded;
1399 }
1400 if (what & eLayerChanged) {
1401 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1402 if (layer->setLayer(s.z)) {
1403 mCurrentState.layersSortedByZ.removeAt(idx);
1404 mCurrentState.layersSortedByZ.add(layer);
1405 // we need traversal (state changed)
1406 // AND transaction (list changed)
1407 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 }
1409 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001410 if (what & eSizeChanged) {
1411 if (layer->setSize(s.w, s.h)) {
1412 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001413 }
1414 }
1415 if (what & eAlphaChanged) {
1416 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1417 flags |= eTraversalNeeded;
1418 }
1419 if (what & eMatrixChanged) {
1420 if (layer->setMatrix(s.matrix))
1421 flags |= eTraversalNeeded;
1422 }
1423 if (what & eTransparentRegionChanged) {
1424 if (layer->setTransparentRegionHint(s.transparentRegion))
1425 flags |= eTraversalNeeded;
1426 }
1427 if (what & eVisibilityChanged) {
1428 if (layer->setFlags(s.flags, s.mask))
1429 flags |= eTraversalNeeded;
1430 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001431 if (what & eCropChanged) {
1432 if (layer->setCrop(s.crop))
1433 flags |= eTraversalNeeded;
1434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001436 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437}
1438
Mathias Agopianb60314a2012-04-10 22:09:54 -07001439// ---------------------------------------------------------------------------
1440
1441void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001442 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001443 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001444 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001445 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001446 // this is a temporary work-around, eventually this should be called
1447 // by the power-manager
1448 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001449 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001450 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451}
1452
Mathias Agopianb60314a2012-04-10 22:09:54 -07001453void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001454 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001455 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001456 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001457 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001458 hw.releaseScreen();
1459 // from this point on, SF will stop drawing
1460 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461}
1462
Colin Cross8e533062012-06-07 13:17:52 -07001463void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001464 class MessageScreenAcquired : public MessageBase {
1465 SurfaceFlinger* flinger;
1466 public:
1467 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1468 virtual bool handler() {
1469 flinger->onScreenAcquired();
1470 return true;
1471 }
1472 };
1473 sp<MessageBase> msg = new MessageScreenAcquired(this);
1474 postMessageSync(msg);
1475}
1476
Colin Cross8e533062012-06-07 13:17:52 -07001477void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001478 class MessageScreenReleased : public MessageBase {
1479 SurfaceFlinger* flinger;
1480 public:
1481 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1482 virtual bool handler() {
1483 flinger->onScreenReleased();
1484 return true;
1485 }
1486 };
1487 sp<MessageBase> msg = new MessageScreenReleased(this);
1488 postMessageSync(msg);
1489}
1490
1491// ---------------------------------------------------------------------------
1492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1494{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001495 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 char buffer[SIZE];
1497 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001498
1499 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 snprintf(buffer, SIZE, "Permission Denial: "
1501 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1502 IPCThreadState::self()->getCallingPid(),
1503 IPCThreadState::self()->getCallingUid());
1504 result.append(buffer);
1505 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001506 // Try to get the main lock, but don't insist if we can't
1507 // (this would indicate SF is stuck, but we want to be able to
1508 // print something in dumpsys).
1509 int retry = 3;
1510 while (mStateLock.tryLock()<0 && --retry>=0) {
1511 usleep(1000000);
1512 }
1513 const bool locked(retry >= 0);
1514 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001515 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001516 "SurfaceFlinger appears to be unresponsive, "
1517 "dumping anyways (no locks held)\n");
1518 result.append(buffer);
1519 }
1520
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001521 bool dumpAll = true;
1522 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001523 size_t numArgs = args.size();
1524 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001525 if ((index < numArgs) &&
1526 (args[index] == String16("--list"))) {
1527 index++;
1528 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001529 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001530 }
1531
1532 if ((index < numArgs) &&
1533 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001534 index++;
1535 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001536 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001537 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001538
1539 if ((index < numArgs) &&
1540 (args[index] == String16("--latency-clear"))) {
1541 index++;
1542 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001543 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001544 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001546
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001547 if (dumpAll) {
1548 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001549 }
1550
Mathias Agopian9795c422009-08-26 16:36:26 -07001551 if (locked) {
1552 mStateLock.unlock();
1553 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554 }
1555 write(fd, result.string(), result.size());
1556 return NO_ERROR;
1557}
1558
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001559void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1560 String8& result, char* buffer, size_t SIZE) const
1561{
1562 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1563 const size_t count = currentLayers.size();
1564 for (size_t i=0 ; i<count ; i++) {
1565 const sp<LayerBase>& layer(currentLayers[i]);
1566 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1567 result.append(buffer);
1568 }
1569}
1570
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001571void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1572 String8& result, char* buffer, size_t SIZE) const
1573{
1574 String8 name;
1575 if (index < args.size()) {
1576 name = String8(args[index]);
1577 index++;
1578 }
1579
1580 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1581 const size_t count = currentLayers.size();
1582 for (size_t i=0 ; i<count ; i++) {
1583 const sp<LayerBase>& layer(currentLayers[i]);
1584 if (name.isEmpty()) {
1585 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1586 result.append(buffer);
1587 }
1588 if (name.isEmpty() || (name == layer->getName())) {
1589 layer->dumpStats(result, buffer, SIZE);
1590 }
1591 }
1592}
1593
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001594void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1595 String8& result, char* buffer, size_t SIZE) const
1596{
1597 String8 name;
1598 if (index < args.size()) {
1599 name = String8(args[index]);
1600 index++;
1601 }
1602
1603 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1604 const size_t count = currentLayers.size();
1605 for (size_t i=0 ; i<count ; i++) {
1606 const sp<LayerBase>& layer(currentLayers[i]);
1607 if (name.isEmpty() || (name == layer->getName())) {
1608 layer->clearStats();
1609 }
1610 }
1611}
1612
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001613void SurfaceFlinger::dumpAllLocked(
1614 String8& result, char* buffer, size_t SIZE) const
1615{
1616 // figure out if we're stuck somewhere
1617 const nsecs_t now = systemTime();
1618 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1619 const nsecs_t inTransaction(mDebugInTransaction);
1620 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1621 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1622
1623 /*
1624 * Dump the visible layer list
1625 */
1626 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1627 const size_t count = currentLayers.size();
1628 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1629 result.append(buffer);
1630 for (size_t i=0 ; i<count ; i++) {
1631 const sp<LayerBase>& layer(currentLayers[i]);
1632 layer->dump(result, buffer, SIZE);
1633 }
1634
1635 /*
1636 * Dump the layers in the purgatory
1637 */
1638
1639 const size_t purgatorySize = mLayerPurgatory.size();
1640 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1641 result.append(buffer);
1642 for (size_t i=0 ; i<purgatorySize ; i++) {
1643 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1644 layer->shortDump(result, buffer, SIZE);
1645 }
1646
1647 /*
1648 * Dump SurfaceFlinger global state
1649 */
1650
1651 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1652 result.append(buffer);
1653
Mathias Agopian1b031492012-06-20 17:51:20 -07001654 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001655 const GLExtensions& extensions(GLExtensions::getInstance());
1656 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1657 extensions.getVendor(),
1658 extensions.getRenderer(),
1659 extensions.getVersion());
1660 result.append(buffer);
1661
1662 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001663 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001664 EGL_VERSION_HW_ANDROID));
1665 result.append(buffer);
1666
1667 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1668 result.append(buffer);
1669
1670 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001671 snprintf(buffer, SIZE,
1672 " orientation=%d, canDraw=%d\n",
1673 mCurrentState.orientation, hw.canDraw());
1674 result.append(buffer);
1675 snprintf(buffer, SIZE,
1676 " last eglSwapBuffers() time: %f us\n"
1677 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001678 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001679 " refresh-rate : %f fps\n"
1680 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001681 " y-dpi : %f\n"
1682 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001683 mLastSwapBufferTime/1000.0,
1684 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001685 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001686 hw.getRefreshRate(),
1687 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001688 hw.getDpiY(),
1689 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001690 result.append(buffer);
1691
1692 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1693 inSwapBuffersDuration/1000.0);
1694 result.append(buffer);
1695
1696 snprintf(buffer, SIZE, " transaction time: %f us\n",
1697 inTransactionDuration/1000.0);
1698 result.append(buffer);
1699
1700 /*
1701 * VSYNC state
1702 */
1703 mEventThread->dump(result, buffer, SIZE);
1704
1705 /*
1706 * Dump HWComposer state
1707 */
1708 HWComposer& hwc(hw.getHwComposer());
1709 snprintf(buffer, SIZE, "h/w composer state:\n");
1710 result.append(buffer);
1711 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1712 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1713 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1714 result.append(buffer);
1715 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1716
1717 /*
1718 * Dump gralloc state
1719 */
1720 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1721 alloc.dump(result);
1722 hw.dump(result);
1723}
1724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725status_t SurfaceFlinger::onTransact(
1726 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1727{
1728 switch (code) {
1729 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001730 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001733 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001734 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001735 case BLANK:
1736 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 {
1738 // codes that require permission check
1739 IPCThreadState* ipc = IPCThreadState::self();
1740 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001741 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001742 if ((uid != AID_GRAPHICS) &&
1743 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001744 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001745 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1746 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001748 break;
1749 }
1750 case CAPTURE_SCREEN:
1751 {
1752 // codes that require permission check
1753 IPCThreadState* ipc = IPCThreadState::self();
1754 const int pid = ipc->getCallingPid();
1755 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001756 if ((uid != AID_GRAPHICS) &&
1757 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001758 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001759 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1760 return PERMISSION_DENIED;
1761 }
1762 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763 }
1764 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001765
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1767 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001768 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001769 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001770 IPCThreadState* ipc = IPCThreadState::self();
1771 const int pid = ipc->getCallingPid();
1772 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001773 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001774 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775 return PERMISSION_DENIED;
1776 }
1777 int n;
1778 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001779 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001780 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 return NO_ERROR;
1782 case 1002: // SHOW_UPDATES
1783 n = data.readInt32();
1784 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001785 invalidateHwcGeometry();
1786 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001787 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001789 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001790 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001792 case 1005:{ // force transaction
1793 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1794 return NO_ERROR;
1795 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001796 case 1006:{ // send empty update
1797 signalRefresh();
1798 return NO_ERROR;
1799 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001800 case 1008: // toggle use of hw composer
1801 n = data.readInt32();
1802 mDebugDisableHWC = n ? 1 : 0;
1803 invalidateHwcGeometry();
1804 repaintEverything();
1805 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001806 case 1009: // toggle use of transform hint
1807 n = data.readInt32();
1808 mDebugDisableTransformHint = n ? 1 : 0;
1809 invalidateHwcGeometry();
1810 repaintEverything();
1811 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001812 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001813 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814 reply->writeInt32(0);
1815 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001816 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001817 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 return NO_ERROR;
1819 case 1013: {
1820 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001821 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822 reply->writeInt32(hw.getPageFlipCount());
1823 }
1824 return NO_ERROR;
1825 }
1826 }
1827 return err;
1828}
1829
Mathias Agopian53331da2011-08-22 21:44:41 -07001830void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001831 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001832 const Rect bounds(hw.getBounds());
1833 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001834 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001835}
1836
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001837void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1838 Mutex::Autolock _l(mInvalidateLock);
1839 mInvalidateRegion = reg;
1840}
1841
1842Region SurfaceFlinger::getAndClearInvalidateRegion() {
1843 Mutex::Autolock _l(mInvalidateLock);
1844 Region reg(mInvalidateRegion);
1845 mInvalidateRegion.clear();
1846 return reg;
1847}
1848
Mathias Agopian59119e62010-10-11 12:37:43 -07001849// ---------------------------------------------------------------------------
1850
Mathias Agopian118d0242011-10-13 16:02:48 -07001851status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1852 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1853{
1854 Mutex::Autolock _l(mStateLock);
1855 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1856}
1857
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001858status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1859 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001860{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001861 ATRACE_CALL();
1862
Mathias Agopian59119e62010-10-11 12:37:43 -07001863 if (!GLExtensions::getInstance().haveFramebufferObject())
1864 return INVALID_OPERATION;
1865
1866 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001867 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07001868 const uint32_t hw_w = hw.getWidth();
1869 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001870 GLfloat u = 1;
1871 GLfloat v = 1;
1872
1873 // make sure to clear all GL error flags
1874 while ( glGetError() != GL_NO_ERROR ) ;
1875
1876 // create a FBO
1877 GLuint name, tname;
1878 glGenTextures(1, &tname);
1879 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001880 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1881 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001882 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1883 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001884 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001885 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001886 GLint tw = (2 << (31 - clz(hw_w)));
1887 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001888 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1889 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001890 u = GLfloat(hw_w) / tw;
1891 v = GLfloat(hw_h) / th;
1892 }
1893 glGenFramebuffersOES(1, &name);
1894 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001895 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1896 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001897
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001898 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001899 glDisable(GL_TEXTURE_EXTERNAL_OES);
1900 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001901 glClearColor(0,0,0,1);
1902 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001903 glMatrixMode(GL_MODELVIEW);
1904 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001905 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1906 const size_t count = layers.size();
1907 for (size_t i=0 ; i<count ; ++i) {
1908 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07001909 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001910 }
1911
Mathias Agopian118d0242011-10-13 16:02:48 -07001912 hw.compositionComplete();
1913
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001914 // back to main framebuffer
1915 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001916 glDeleteFramebuffersOES(1, &name);
1917
1918 *textureName = tname;
1919 *uOut = u;
1920 *vOut = v;
1921 return NO_ERROR;
1922}
1923
1924// ---------------------------------------------------------------------------
1925
Mathias Agopianed9807b2012-05-18 14:18:08 -07001926class VSyncWaiter {
1927 DisplayEventReceiver::Event buffer[4];
1928 sp<Looper> looper;
1929 sp<IDisplayEventConnection> events;
1930 sp<BitTube> eventTube;
1931public:
1932 VSyncWaiter(const sp<EventThread>& eventThread) {
1933 looper = new Looper(true);
1934 events = eventThread->createEventConnection();
1935 eventTube = events->getDataChannel();
1936 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1937 events->requestNextVsync();
1938 }
1939
1940 void wait() {
1941 ssize_t n;
1942
1943 looper->pollOnce(-1);
1944 // we don't handle any errors here, it doesn't matter
1945 // and we don't want to take the risk to get stuck.
1946
1947 // drain the events...
1948 while ((n = DisplayEventReceiver::getEvents(
1949 eventTube, buffer, 4)) > 0) ;
1950
1951 events->requestNextVsync();
1952 }
1953};
1954
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001955status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1956{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001957 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001958 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001959 const uint32_t hw_w = hw.getWidth();
1960 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001961 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001962
1963 GLfloat u, v;
1964 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001965 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001966 if (result != NO_ERROR) {
1967 return result;
1968 }
1969
1970 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001971 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 glBindTexture(GL_TEXTURE_2D, tname);
1973 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1974 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1975 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001976 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1977 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001978 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1979 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1980 glVertexPointer(2, GL_FLOAT, 0, vtx);
1981
Mathias Agopianffcf4652011-07-07 17:30:31 -07001982 /*
1983 * Texture coordinate mapping
1984 *
1985 * u
1986 * 1 +----------+---+
1987 * | | | | image is inverted
1988 * | V | | w.r.t. the texture
1989 * 1-v +----------+ | coordinates
1990 * | |
1991 * | |
1992 * | |
1993 * 0 +--------------+
1994 * 0 1
1995 *
1996 */
1997
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001998 class s_curve_interpolator {
1999 const float nbFrames, s, v;
2000 public:
2001 s_curve_interpolator(int nbFrames, float s)
2002 : nbFrames(1.0f / (nbFrames-1)), s(s),
2003 v(1.0f + expf(-s + 0.5f*s)) {
2004 }
2005 float operator()(int f) {
2006 const float x = f * nbFrames;
2007 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2008 }
2009 };
2010
2011 class v_stretch {
2012 const GLfloat hw_w, hw_h;
2013 public:
2014 v_stretch(uint32_t hw_w, uint32_t hw_h)
2015 : hw_w(hw_w), hw_h(hw_h) {
2016 }
2017 void operator()(GLfloat* vtx, float v) {
2018 const GLfloat w = hw_w + (hw_w * v);
2019 const GLfloat h = hw_h - (hw_h * v);
2020 const GLfloat x = (hw_w - w) * 0.5f;
2021 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002022 vtx[0] = x; vtx[1] = y;
2023 vtx[2] = x; vtx[3] = y + h;
2024 vtx[4] = x + w; vtx[5] = y + h;
2025 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 }
2027 };
2028
2029 class h_stretch {
2030 const GLfloat hw_w, hw_h;
2031 public:
2032 h_stretch(uint32_t hw_w, uint32_t hw_h)
2033 : hw_w(hw_w), hw_h(hw_h) {
2034 }
2035 void operator()(GLfloat* vtx, float v) {
2036 const GLfloat w = hw_w - (hw_w * v);
2037 const GLfloat h = 1.0f;
2038 const GLfloat x = (hw_w - w) * 0.5f;
2039 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002040 vtx[0] = x; vtx[1] = y;
2041 vtx[2] = x; vtx[3] = y + h;
2042 vtx[4] = x + w; vtx[5] = y + h;
2043 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 }
2045 };
2046
Mathias Agopianed9807b2012-05-18 14:18:08 -07002047 VSyncWaiter vsync(mEventThread);
2048
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002049 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002050 char value[PROPERTY_VALUE_MAX];
2051 property_get("debug.sf.electron_frames", value, "24");
2052 int nbFrames = (atoi(value) + 1) >> 1;
2053 if (nbFrames <= 0) // just in case
2054 nbFrames = 24;
2055
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002056 s_curve_interpolator itr(nbFrames, 7.5f);
2057 s_curve_interpolator itg(nbFrames, 8.0f);
2058 s_curve_interpolator itb(nbFrames, 8.5f);
2059
2060 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002061
2062 glMatrixMode(GL_TEXTURE);
2063 glLoadIdentity();
2064 glMatrixMode(GL_MODELVIEW);
2065 glLoadIdentity();
2066
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002067 glEnable(GL_BLEND);
2068 glBlendFunc(GL_ONE, GL_ONE);
2069 for (int i=0 ; i<nbFrames ; i++) {
2070 float x, y, w, h;
2071 const float vr = itr(i);
2072 const float vg = itg(i);
2073 const float vb = itb(i);
2074
Mathias Agopianed9807b2012-05-18 14:18:08 -07002075 // wait for vsync
2076 vsync.wait();
2077
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002078 // clear screen
2079 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002080 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002081 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002082
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002083 // draw the red plane
2084 vverts(vtx, vr);
2085 glColorMask(1,0,0,1);
2086 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002087
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002088 // draw the green plane
2089 vverts(vtx, vg);
2090 glColorMask(0,1,0,1);
2091 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002092
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002093 // draw the blue plane
2094 vverts(vtx, vb);
2095 glColorMask(0,0,1,1);
2096 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002097
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002099 glDisable(GL_TEXTURE_2D);
2100 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002101 glColor4f(vg, vg, vg, 1);
2102 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2103 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002104 }
2105
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 h_stretch hverts(hw_w, hw_h);
2107 glDisable(GL_BLEND);
2108 glDisable(GL_TEXTURE_2D);
2109 glColorMask(1,1,1,1);
2110 for (int i=0 ; i<nbFrames ; i++) {
2111 const float v = itg(i);
2112 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002113
2114 // wait for vsync
2115 vsync.wait();
2116
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002117 glClear(GL_COLOR_BUFFER_BIT);
2118 glColor4f(1-v, 1-v, 1-v, 1);
2119 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2120 hw.flip(screenBounds);
2121 }
2122
2123 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002124 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2125 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002126 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002127 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002128 return NO_ERROR;
2129}
2130
2131status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2132{
2133 status_t result = PERMISSION_DENIED;
2134
2135 if (!GLExtensions::getInstance().haveFramebufferObject())
2136 return INVALID_OPERATION;
2137
2138
2139 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002140 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002141 const uint32_t hw_w = hw.getWidth();
2142 const uint32_t hw_h = hw.getHeight();
2143 const Region screenBounds(hw.bounds());
2144
2145 GLfloat u, v;
2146 GLuint tname;
2147 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2148 if (result != NO_ERROR) {
2149 return result;
2150 }
2151
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002152 GLfloat vtx[8];
2153 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002154 glBindTexture(GL_TEXTURE_2D, tname);
2155 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2156 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2157 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002158 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2159 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002160 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2161 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2162 glVertexPointer(2, GL_FLOAT, 0, vtx);
2163
2164 class s_curve_interpolator {
2165 const float nbFrames, s, v;
2166 public:
2167 s_curve_interpolator(int nbFrames, float s)
2168 : nbFrames(1.0f / (nbFrames-1)), s(s),
2169 v(1.0f + expf(-s + 0.5f*s)) {
2170 }
2171 float operator()(int f) {
2172 const float x = f * nbFrames;
2173 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2174 }
2175 };
2176
2177 class v_stretch {
2178 const GLfloat hw_w, hw_h;
2179 public:
2180 v_stretch(uint32_t hw_w, uint32_t hw_h)
2181 : hw_w(hw_w), hw_h(hw_h) {
2182 }
2183 void operator()(GLfloat* vtx, float v) {
2184 const GLfloat w = hw_w + (hw_w * v);
2185 const GLfloat h = hw_h - (hw_h * v);
2186 const GLfloat x = (hw_w - w) * 0.5f;
2187 const GLfloat y = (hw_h - h) * 0.5f;
2188 vtx[0] = x; vtx[1] = y;
2189 vtx[2] = x; vtx[3] = y + h;
2190 vtx[4] = x + w; vtx[5] = y + h;
2191 vtx[6] = x + w; vtx[7] = y;
2192 }
2193 };
2194
2195 class h_stretch {
2196 const GLfloat hw_w, hw_h;
2197 public:
2198 h_stretch(uint32_t hw_w, uint32_t hw_h)
2199 : hw_w(hw_w), hw_h(hw_h) {
2200 }
2201 void operator()(GLfloat* vtx, float v) {
2202 const GLfloat w = hw_w - (hw_w * v);
2203 const GLfloat h = 1.0f;
2204 const GLfloat x = (hw_w - w) * 0.5f;
2205 const GLfloat y = (hw_h - h) * 0.5f;
2206 vtx[0] = x; vtx[1] = y;
2207 vtx[2] = x; vtx[3] = y + h;
2208 vtx[4] = x + w; vtx[5] = y + h;
2209 vtx[6] = x + w; vtx[7] = y;
2210 }
2211 };
2212
Mathias Agopianed9807b2012-05-18 14:18:08 -07002213 VSyncWaiter vsync(mEventThread);
2214
Mathias Agopiana6546e52010-10-14 12:33:07 -07002215 // the full animation is 12 frames
2216 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002217 s_curve_interpolator itr(nbFrames, 7.5f);
2218 s_curve_interpolator itg(nbFrames, 8.0f);
2219 s_curve_interpolator itb(nbFrames, 8.5f);
2220
2221 h_stretch hverts(hw_w, hw_h);
2222 glDisable(GL_BLEND);
2223 glDisable(GL_TEXTURE_2D);
2224 glColorMask(1,1,1,1);
2225 for (int i=nbFrames-1 ; i>=0 ; i--) {
2226 const float v = itg(i);
2227 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002228
2229 // wait for vsync
2230 vsync.wait();
2231
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002232 glClear(GL_COLOR_BUFFER_BIT);
2233 glColor4f(1-v, 1-v, 1-v, 1);
2234 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2235 hw.flip(screenBounds);
2236 }
2237
Mathias Agopiana6546e52010-10-14 12:33:07 -07002238 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002239 v_stretch vverts(hw_w, hw_h);
2240 glEnable(GL_BLEND);
2241 glBlendFunc(GL_ONE, GL_ONE);
2242 for (int i=nbFrames-1 ; i>=0 ; i--) {
2243 float x, y, w, h;
2244 const float vr = itr(i);
2245 const float vg = itg(i);
2246 const float vb = itb(i);
2247
Mathias Agopianed9807b2012-05-18 14:18:08 -07002248 // wait for vsync
2249 vsync.wait();
2250
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002251 // clear screen
2252 glColorMask(1,1,1,1);
2253 glClear(GL_COLOR_BUFFER_BIT);
2254 glEnable(GL_TEXTURE_2D);
2255
2256 // draw the red plane
2257 vverts(vtx, vr);
2258 glColorMask(1,0,0,1);
2259 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2260
2261 // draw the green plane
2262 vverts(vtx, vg);
2263 glColorMask(0,1,0,1);
2264 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2265
2266 // draw the blue plane
2267 vverts(vtx, vb);
2268 glColorMask(0,0,1,1);
2269 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2270
2271 hw.flip(screenBounds);
2272 }
2273
2274 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002275 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002276 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002277 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002278 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002279
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002280 return NO_ERROR;
2281}
2282
2283// ---------------------------------------------------------------------------
2284
Mathias Agopianabd671a2010-10-14 14:54:06 -07002285status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002286{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002287 ATRACE_CALL();
2288
Mathias Agopian1b031492012-06-20 17:51:20 -07002289 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290 if (!hw.canDraw()) {
2291 // we're already off
2292 return NO_ERROR;
2293 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002294
2295 // turn off hwc while we're doing the animation
2296 hw.getHwComposer().disable();
2297 // and make sure to turn it back on (if needed) next time we compose
2298 invalidateHwcGeometry();
2299
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2301 electronBeamOffAnimationImplLocked();
2302 }
2303
2304 // always clear the whole screen at the end of the animation
2305 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002306 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002307 hw.flip( Region(hw.bounds()) );
2308
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002309 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002310}
2311
2312status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2313{
Mathias Agopian59119e62010-10-11 12:37:43 -07002314 class MessageTurnElectronBeamOff : public MessageBase {
2315 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002316 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002317 status_t result;
2318 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002319 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2320 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002321 }
2322 status_t getResult() const {
2323 return result;
2324 }
2325 virtual bool handler() {
2326 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002327 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002328 return true;
2329 }
2330 };
2331
Mathias Agopianabd671a2010-10-14 14:54:06 -07002332 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002333 status_t res = postMessageSync(msg);
2334 if (res == NO_ERROR) {
2335 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002336
2337 // work-around: when the power-manager calls us we activate the
2338 // animation. eventually, the "on" animation will be called
2339 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002340 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002341 }
2342 return res;
2343}
2344
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002345// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002346
Mathias Agopianabd671a2010-10-14 14:54:06 -07002347status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002348{
Mathias Agopian1b031492012-06-20 17:51:20 -07002349 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002350 if (hw.canDraw()) {
2351 // we're already on
2352 return NO_ERROR;
2353 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002354 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2355 electronBeamOnAnimationImplLocked();
2356 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002357
2358 // make sure to redraw the whole screen when the animation is done
2359 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002360 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002361
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002362 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002363}
2364
2365status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2366{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002367 class MessageTurnElectronBeamOn : public MessageBase {
2368 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002369 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002370 status_t result;
2371 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002372 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2373 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002374 }
2375 status_t getResult() const {
2376 return result;
2377 }
2378 virtual bool handler() {
2379 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002380 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002381 return true;
2382 }
2383 };
2384
Mathias Agopianabd671a2010-10-14 14:54:06 -07002385 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002386 return NO_ERROR;
2387}
2388
2389// ---------------------------------------------------------------------------
2390
Mathias Agopian74c40c02010-09-29 13:02:36 -07002391status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2392 sp<IMemoryHeap>* heap,
2393 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002394 uint32_t sw, uint32_t sh,
2395 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002396{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002397 ATRACE_CALL();
2398
Mathias Agopian74c40c02010-09-29 13:02:36 -07002399 status_t result = PERMISSION_DENIED;
2400
2401 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002402 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002403 return BAD_VALUE;
2404
2405 if (!GLExtensions::getInstance().haveFramebufferObject())
2406 return INVALID_OPERATION;
2407
2408 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002409 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002410 const uint32_t hw_w = hw.getWidth();
2411 const uint32_t hw_h = hw.getHeight();
2412
2413 if ((sw > hw_w) || (sh > hw_h))
2414 return BAD_VALUE;
2415
2416 sw = (!sw) ? hw_w : sw;
2417 sh = (!sh) ? hw_h : sh;
2418 const size_t size = sw * sh * 4;
2419
Steve Block9d453682011-12-20 16:23:08 +00002420 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002421 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002422
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 // make sure to clear all GL error flags
2424 while ( glGetError() != GL_NO_ERROR ) ;
2425
2426 // create a FBO
2427 GLuint name, tname;
2428 glGenRenderbuffersOES(1, &tname);
2429 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2430 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002431
Mathias Agopian74c40c02010-09-29 13:02:36 -07002432 glGenFramebuffersOES(1, &name);
2433 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2434 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2435 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2436
2437 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002438
Mathias Agopian74c40c02010-09-29 13:02:36 -07002439 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2440
2441 // invert everything, b/c glReadPixel() below will invert the FB
2442 glViewport(0, 0, sw, sh);
2443 glMatrixMode(GL_PROJECTION);
2444 glPushMatrix();
2445 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002446 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002447 glMatrixMode(GL_MODELVIEW);
2448
2449 // redraw the screen entirely...
2450 glClearColor(0,0,0,1);
2451 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002452
Jamie Gennis9575f602011-10-07 14:51:16 -07002453 const LayerVector& layers(mDrawingState.layersSortedByZ);
2454 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002455 for (size_t i=0 ; i<count ; ++i) {
2456 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002457 const uint32_t flags = layer->drawingState().flags;
2458 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2459 const uint32_t z = layer->drawingState().z;
2460 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002461 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002462 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002463 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002464 }
2465
Mathias Agopian74c40c02010-09-29 13:02:36 -07002466 // check for errors and return screen capture
2467 if (glGetError() != GL_NO_ERROR) {
2468 // error while rendering
2469 result = INVALID_OPERATION;
2470 } else {
2471 // allocate shared memory large enough to hold the
2472 // screen capture
2473 sp<MemoryHeapBase> base(
2474 new MemoryHeapBase(size, 0, "screen-capture") );
2475 void* const ptr = base->getBase();
2476 if (ptr) {
2477 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002478 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002479 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2480 if (glGetError() == GL_NO_ERROR) {
2481 *heap = base;
2482 *w = sw;
2483 *h = sh;
2484 *f = PIXEL_FORMAT_RGBA_8888;
2485 result = NO_ERROR;
2486 }
2487 } else {
2488 result = NO_MEMORY;
2489 }
2490 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002491 glViewport(0, 0, hw_w, hw_h);
2492 glMatrixMode(GL_PROJECTION);
2493 glPopMatrix();
2494 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002495 } else {
2496 result = BAD_VALUE;
2497 }
2498
2499 // release FBO resources
2500 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2501 glDeleteRenderbuffersOES(1, &tname);
2502 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002503
2504 hw.compositionComplete();
2505
Steve Block9d453682011-12-20 16:23:08 +00002506 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002507
Mathias Agopian74c40c02010-09-29 13:02:36 -07002508 return result;
2509}
2510
2511
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002512status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2513 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002514 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002515 uint32_t sw, uint32_t sh,
2516 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002517{
2518 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002519 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002520 return BAD_VALUE;
2521
2522 if (!GLExtensions::getInstance().haveFramebufferObject())
2523 return INVALID_OPERATION;
2524
2525 class MessageCaptureScreen : public MessageBase {
2526 SurfaceFlinger* flinger;
2527 DisplayID dpy;
2528 sp<IMemoryHeap>* heap;
2529 uint32_t* w;
2530 uint32_t* h;
2531 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002532 uint32_t sw;
2533 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002534 uint32_t minLayerZ;
2535 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002536 status_t result;
2537 public:
2538 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002539 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002540 uint32_t sw, uint32_t sh,
2541 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002542 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002543 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2544 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2545 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002546 {
2547 }
2548 status_t getResult() const {
2549 return result;
2550 }
2551 virtual bool handler() {
2552 Mutex::Autolock _l(flinger->mStateLock);
2553
2554 // if we have secure windows, never allow the screen capture
2555 if (flinger->mSecureFrameBuffer)
2556 return true;
2557
Mathias Agopian74c40c02010-09-29 13:02:36 -07002558 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002559 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002560
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002561 return true;
2562 }
2563 };
2564
2565 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002566 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002567 status_t res = postMessageSync(msg);
2568 if (res == NO_ERROR) {
2569 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2570 }
2571 return res;
2572}
2573
2574// ---------------------------------------------------------------------------
2575
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002576sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2577{
2578 sp<Layer> result;
2579 Mutex::Autolock _l(mStateLock);
2580 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2581 return result;
2582}
2583
2584// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002585
Jamie Gennis9a78c902011-01-12 18:30:40 -08002586GraphicBufferAlloc::GraphicBufferAlloc() {}
2587
2588GraphicBufferAlloc::~GraphicBufferAlloc() {}
2589
2590sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002591 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002592 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2593 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002594 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002595 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002596 if (err == NO_MEMORY) {
2597 GraphicBuffer::dumpAllocationsToSystemLog();
2598 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002599 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002600 "failed (%s), handle=%p",
2601 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002602 return 0;
2603 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002604 return graphicBuffer;
2605}
2606
Jamie Gennis9a78c902011-01-12 18:30:40 -08002607// ---------------------------------------------------------------------------
2608
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609}; // namespace android