blob: 46a711d6b97c6cf13163cde87bee271b9de1824d [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 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
103 init();
104}
105
106void SurfaceFlinger::init()
107{
Steve Blocka19954a2012-01-04 20:05:49 +0000108 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 // debugging stuff...
111 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 property_get("debug.sf.showupdates", value, "0");
114 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115
Colin Cross3854ed52012-03-23 14:17:18 -0700116#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117 property_get("debug.sf.ddms", value, "0");
118 mDebugDDMS = atoi(value);
119 if (mDebugDDMS) {
120 DdmConnection::start(getServiceName());
121 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700122#else
123#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700124#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700125
Steve Blocka19954a2012-01-04 20:05:49 +0000126 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000127 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128}
129
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800130void SurfaceFlinger::onFirstRef()
131{
132 mEventQueue.init(this);
133
134 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
135
136 // Wait for the main thread to be done with its initialization
137 mReadyToRunBarrier.wait();
138}
139
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141SurfaceFlinger::~SurfaceFlinger()
142{
143 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}
145
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146void SurfaceFlinger::binderDied(const wp<IBinder>& who)
147{
148 // the window manager died on us. prepare its eulogy.
149
150 // reset screen orientation
151 Vector<ComposerState> state;
152 setTransactionState(state, eOrientationDefault, 0);
153
154 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700155 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800156}
157
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700160 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Mathias Agopian7e27f052010-05-28 14:22:23 -0700163sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopian96f08192010-06-02 23:28:45 -0700165 sp<ISurfaceComposerClient> bclient;
166 sp<Client> client(new Client(this));
167 status_t err = client->initCheck();
168 if (err == NO_ERROR) {
169 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 return bclient;
172}
173
Jamie Gennis9a78c902011-01-12 18:30:40 -0800174sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
175{
176 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
177 return gba;
178}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180void SurfaceFlinger::bootFinished()
181{
182 const nsecs_t now = systemTime();
183 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000184 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700186
187 // wait patiently for the window manager death
188 const String16 name("window");
189 sp<IBinder> window(defaultServiceManager()->getService(name));
190 if (window != 0) {
191 window->linkToDeath(this);
192 }
193
194 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700195 // formerly we would just kill the process, but we now ask it to exit so it
196 // can choose where to stop the animation.
197 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200static inline uint16_t pack565(int r, int g, int b) {
201 return (r<<11)|(g<<5)|b;
202}
203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204status_t SurfaceFlinger::readyToRun()
205{
Mathias Agopian1b031492012-06-20 17:51:20 -0700206 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 "Initializing graphics H/W...");
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 // we only support one display currently
210 int dpy = 0;
211
212 {
213 // initialize the main display
Mathias Agopian1b031492012-06-20 17:51:20 -0700214 // TODO: initialize all displays
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 DisplayHardware* const hw = new DisplayHardware(this, dpy);
Mathias Agopian1b031492012-06-20 17:51:20 -0700216 mDisplayHardwares[0] = hw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 }
218
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700219 // create the shared control-block
220 mServerHeap = new MemoryHeapBase(4096,
221 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000222 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700223
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700224 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000225 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700226
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700227 new(mServerCblk) surface_flinger_cblk_t;
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 // initialize primary screen
230 // (other display should be initialized in the same manner, but
231 // asynchronously, as they could come and go. None of this is supported
232 // yet).
Mathias Agopian1b031492012-06-20 17:51:20 -0700233 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 const uint32_t w = hw.getWidth();
235 const uint32_t h = hw.getHeight();
236 const uint32_t f = hw.getFormat();
237 hw.makeCurrent();
238
239 // initialize the shared control block
240 mServerCblk->connected |= 1<<dpy;
241 display_cblk_t* dcblk = mServerCblk->displays + dpy;
242 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian1b031492012-06-20 17:51:20 -0700243 dcblk->w = w; // XXX: plane.getWidth();
244 dcblk->h = h; // XXX: plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 dcblk->format = f;
246 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
247 dcblk->xdpi = hw.getDpiX();
248 dcblk->ydpi = hw.getDpiY();
249 dcblk->fps = hw.getRefreshRate();
250 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
252 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700254 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 glShadeModel(GL_FLAT);
257 glDisable(GL_DITHER);
258 glDisable(GL_CULL_FACE);
259
260 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
261 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700262 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glGenTextures(1, &mWormholeTexName);
264 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
265 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
266 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
267 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
268 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
269 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700270 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
271
272 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
273 glGenTextures(1, &mProtectedTexName);
274 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
280 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281
282 glViewport(0, 0, w, h);
283 glMatrixMode(GL_PROJECTION);
284 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700285 // put the origin in the left-bottom corner
286 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 -0800287
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800288
289 // start the EventThread
290 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800291 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
293 /*
294 * We're now ready to accept clients...
295 */
296
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800297 mReadyToRunBarrier.open();
298
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700299 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700300 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 return NO_ERROR;
303}
304
Mathias Agopiana67e4182012-06-19 17:26:12 -0700305void SurfaceFlinger::startBootAnim() {
306 // start boot animation
307 property_set("service.bootanim.exit", "0");
308 property_set("ctl.start", "bootanim");
309}
310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800312
Jamie Gennis582270d2011-08-17 18:19:00 -0700313bool SurfaceFlinger::authenticateSurfaceTexture(
314 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800315 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700316 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800317
318 // Check the visible layer list for the ISurface
319 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
320 size_t count = currentLayers.size();
321 for (size_t i=0 ; i<count ; i++) {
322 const sp<LayerBase>& layer(currentLayers[i]);
323 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700324 if (lbc != NULL) {
325 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
326 if (lbcBinder == surfaceTextureBinder) {
327 return true;
328 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800329 }
330 }
331
332 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700333 // SurfaceTexture gets destroyed before all the clients are done using it,
334 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800335 // will instead fail later on when the client tries to use the surface,
336 // which should be reported as "surface XYZ returned an -ENODEV". The
337 // purgatorized layers are no less authentic than the visible ones, so this
338 // should not cause any harm.
339 size_t purgatorySize = mLayerPurgatory.size();
340 for (size_t i=0 ; i<purgatorySize ; i++) {
341 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
342 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700343 if (lbc != NULL) {
344 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
345 if (lbcBinder == surfaceTextureBinder) {
346 return true;
347 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800348 }
349 }
350
351 return false;
352}
353
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800354// ----------------------------------------------------------------------------
355
356sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800357 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700358}
359
Mathias Agopian3094df32012-06-18 18:06:45 -0700360void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700361 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian3094df32012-06-18 18:06:45 -0700362 EGLSurface result = EGL_NO_SURFACE;
363 EGLSurface old_surface = EGL_NO_SURFACE;
364 sp<SurfaceTextureClient> stc;
365
366 if (display != NULL) {
367 stc = new SurfaceTextureClient(display);
368 result = eglCreateWindowSurface(hw.getEGLDisplay(),
369 hw.getEGLConfig(), (EGLNativeWindowType)stc.get(), NULL);
370 ALOGE_IF(result == EGL_NO_SURFACE,
371 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
372 display.get());
373 }
374
375 { // scope for the lock
376 Mutex::Autolock _l(mStateLock);
377 old_surface = mExternalDisplaySurface;
378 mExternalDisplayNativeWindow = stc;
379 mExternalDisplaySurface = result;
380 ALOGD("mExternalDisplaySurface = %p", result);
381 }
382
383 if (old_surface != EGL_NO_SURFACE) {
384 // Note: EGL allows to destroy an object while its current
385 // it will fail to become current next time though.
386 eglDestroySurface(hw.getEGLDisplay(), old_surface);
387 }
388}
389
390EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
391 Mutex::Autolock _l(mStateLock);
392 return mExternalDisplaySurface;
393}
394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800396
397void SurfaceFlinger::waitForEvent() {
398 mEventQueue.waitMessage();
399}
400
401void SurfaceFlinger::signalTransaction() {
402 mEventQueue.invalidate();
403}
404
405void SurfaceFlinger::signalLayerUpdate() {
406 mEventQueue.invalidate();
407}
408
409void SurfaceFlinger::signalRefresh() {
410 mEventQueue.refresh();
411}
412
413status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
414 nsecs_t reltime, uint32_t flags) {
415 return mEventQueue.postMessage(msg, reltime);
416}
417
418status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
419 nsecs_t reltime, uint32_t flags) {
420 status_t res = mEventQueue.postMessage(msg, reltime);
421 if (res == NO_ERROR) {
422 msg->wait();
423 }
424 return res;
425}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426
Mathias Agopian4fec8732012-06-29 14:12:52 -0700427bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 return true;
430}
431
Mathias Agopian4fec8732012-06-29 14:12:52 -0700432void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800433 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800434 switch (what) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700435 case MessageQueue::INVALIDATE:
436 handleMessageTransaction();
437 handleMessageInvalidate();
438 signalRefresh();
439 break;
440 case MessageQueue::REFRESH:
441 handleMessageRefresh();
442 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800443 }
444}
445
Mathias Agopian4fec8732012-06-29 14:12:52 -0700446void SurfaceFlinger::handleMessageTransaction() {
447 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
448 uint32_t transactionFlags = peekTransactionFlags(mask);
449 if (transactionFlags) {
450 Region dirtyRegion;
451 dirtyRegion = handleTransaction(transactionFlags);
452 // XXX: dirtyRegion should be per screen
453 mDirtyRegion |= dirtyRegion;
454 }
455}
456
457void SurfaceFlinger::handleMessageInvalidate() {
458 Region dirtyRegion;
459 dirtyRegion = handlePageFlip();
460 // XXX: dirtyRegion should be per screen
461 mDirtyRegion |= dirtyRegion;
462}
463
464void SurfaceFlinger::handleMessageRefresh() {
465 handleRefresh();
466
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700467 if (mVisibleRegionsDirty) {
468 Region opaqueRegion;
469 Region dirtyRegion;
470 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
471 computeVisibleRegions(currentLayers, dirtyRegion, opaqueRegion);
472 mDirtyRegion.orSelf(dirtyRegion);
473
474 /*
475 * rebuild the visible layer list per screen
476 */
477
478 // TODO: iterate through all displays
479 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(0)));
480
481 Vector< sp<LayerBase> > layersSortedByZ;
482 const size_t count = currentLayers.size();
483 for (size_t i=0 ; i<count ; i++) {
484 if (!currentLayers[i]->visibleRegion.isEmpty()) {
485 // TODO: also check that this layer is associated to this display
486 layersSortedByZ.add(currentLayers[i]);
487 }
488 }
489 hw.setVisibleLayersSortedByZ(layersSortedByZ);
490
491
492 // FIXME: mWormholeRegion needs to be calculated per screen
493 //const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: we can't keep that here
494 mWormholeRegion = Region(hw.getBounds()).subtract(
495 hw.getTransform().transform(opaqueRegion) );
496 mVisibleRegionsDirty = false;
497 invalidateHwcGeometry();
498 }
499
500
Mathias Agopian4fec8732012-06-29 14:12:52 -0700501 // XXX: dirtyRegion should be per screen, we should check all of them
502 if (mDirtyRegion.isEmpty()) {
503 return;
504 }
505
506 // TODO: iterate through all displays
507 const DisplayHardware& hw(getDisplayHardware(0));
508
509 // XXX: dirtyRegion should be per screen
510 // transform the dirty region into this screen's coordinate space
511 const Transform& planeTransform(hw.getTransform());
512 mDirtyRegion = planeTransform.transform(mDirtyRegion);
513 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
514 mDirtyRegion.andSelf(hw.bounds());
515
516
517 if (CC_UNLIKELY(mHwWorkListDirty)) {
518 // build the h/w work list
519 handleWorkList(hw);
520 }
521
522 if (CC_LIKELY(hw.canDraw())) {
523 // repaint the framebuffer (if needed)
524 handleRepaint(hw);
525 // inform the h/w that we're done compositing
526 hw.compositionComplete();
527 postFramebuffer();
528 } else {
529 // pretend we did the post
530 hw.compositionComplete();
531 }
532
533 // render to the external display if we have one
534 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
535 if (externalDisplaySurface != EGL_NO_SURFACE) {
536 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
537 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
538 externalDisplaySurface, externalDisplaySurface,
539 eglGetCurrentContext());
540
541 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
542
543 if (success) {
544 // redraw the screen entirely...
545 glDisable(GL_TEXTURE_EXTERNAL_OES);
546 glDisable(GL_TEXTURE_2D);
547 glClearColor(0,0,0,1);
548 glClear(GL_COLOR_BUFFER_BIT);
549 glMatrixMode(GL_MODELVIEW);
550 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700551
552 const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() );
Mathias Agopian4fec8732012-06-29 14:12:52 -0700553 const size_t count = layers.size();
554 for (size_t i=0 ; i<count ; ++i) {
555 const sp<LayerBase>& layer(layers[i]);
556 layer->drawForSreenShot(hw);
557 }
558
559 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
560 ALOGE_IF(!success, "external display eglSwapBuffers failed");
561
562 hw.compositionComplete();
563 }
564
565 success = eglMakeCurrent(eglGetCurrentDisplay(),
566 cur, cur, eglGetCurrentContext());
567
568 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
569 }
570
571}
572
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573void SurfaceFlinger::postFramebuffer()
574{
Mathias Agopian841cde52012-03-01 15:44:37 -0800575 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800576 // mSwapRegion can be empty here is some cases, for instance if a hidden
577 // or fully transparent window is updating.
578 // in that case, we need to flip anyways to not risk a deadlock with
579 // h/w composer.
580
Mathias Agopian1b031492012-06-20 17:51:20 -0700581 const DisplayHardware& hw(getDefaultDisplayHardware());
Jesse Hallc5c5a142012-07-02 16:49:28 -0700582 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700583 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
584 size_t numLayers = layers.size();
Mathias Agopiana44b0412011-10-16 18:46:35 -0700585 const nsecs_t now = systemTime();
586 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700587
588 if (hwc.initCheck() == NO_ERROR) {
589 HWComposer::LayerListIterator cur = hwc.begin();
590 const HWComposer::LayerListIterator end = hwc.end();
591 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
592 if (cur->getCompositionType() == HWC_OVERLAY) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700593 layers[i]->setAcquireFence(*cur);
Jesse Hallc5c5a142012-07-02 16:49:28 -0700594 } else {
595 cur->setAcquireFenceFd(-1);
596 }
597 }
598 }
599
Mathias Agopiana44b0412011-10-16 18:46:35 -0700600 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800601
Jesse Hallef194142012-06-14 14:45:17 -0700602 if (hwc.initCheck() == NO_ERROR) {
603 HWComposer::LayerListIterator cur = hwc.begin();
604 const HWComposer::LayerListIterator end = hwc.end();
605 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700606 layers[i]->onLayerDisplayed(&*cur);
Jesse Hallef194142012-06-14 14:45:17 -0700607 }
608 } else {
609 for (size_t i = 0; i < numLayers; i++) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700610 layers[i]->onLayerDisplayed(NULL);
Jesse Hallef194142012-06-14 14:45:17 -0700611 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800612 }
613
Mathias Agopiana44b0412011-10-16 18:46:35 -0700614 mLastSwapBufferTime = systemTime() - now;
615 mDebugInSwapBuffers = 0;
616 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617}
618
Mathias Agopian4fec8732012-06-29 14:12:52 -0700619Region SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620{
Mathias Agopian841cde52012-03-01 15:44:37 -0800621 ATRACE_CALL();
622
Mathias Agopian4fec8732012-06-29 14:12:52 -0700623 Region dirtyRegion;
624
Mathias Agopianca4d3602011-05-19 15:38:14 -0700625 Mutex::Autolock _l(mStateLock);
626 const nsecs_t now = systemTime();
627 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628
Mathias Agopianca4d3602011-05-19 15:38:14 -0700629 // Here we're guaranteed that some transaction flags are set
630 // so we can call handleTransactionLocked() unconditionally.
631 // We call getTransactionFlags(), which will also clear the flags,
632 // with mStateLock held to guarantee that mCurrentState won't change
633 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700634
Mathias Agopianca4d3602011-05-19 15:38:14 -0700635 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
636 transactionFlags = getTransactionFlags(mask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700637 dirtyRegion = handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700638
Mathias Agopianca4d3602011-05-19 15:38:14 -0700639 mLastTransactionTime = systemTime() - now;
640 mDebugInTransaction = 0;
641 invalidateHwcGeometry();
642 // here the transaction has been committed
Mathias Agopian4fec8732012-06-29 14:12:52 -0700643
644 return dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700645}
646
Mathias Agopian4fec8732012-06-29 14:12:52 -0700647Region SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700648{
Mathias Agopian4fec8732012-06-29 14:12:52 -0700649 Region dirtyRegion;
Mathias Agopian3d579642009-06-04 18:46:21 -0700650 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651 const size_t count = currentLayers.size();
652
653 /*
654 * Traversal of the children
655 * (perform the transaction for each of them if needed)
656 */
657
658 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
659 if (layersNeedTransaction) {
660 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
663 if (!trFlags) continue;
664
665 const uint32_t flags = layer->doTransaction(0);
666 if (flags & Layer::eVisibleRegion)
667 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 }
669 }
670
671 /*
672 * Perform our own transaction if needed
673 */
674
675 if (transactionFlags & eTransactionNeeded) {
676 if (mCurrentState.orientation != mDrawingState.orientation) {
677 // the orientation has changed, recompute all visible regions
678 // and invalidate everything.
679
Mathias Agopian1b031492012-06-20 17:51:20 -0700680 const int dpy = 0; // TODO: should be a parameter
681 DisplayHardware& hw(const_cast<DisplayHardware&>(getDisplayHardware(dpy)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 const int orientation = mCurrentState.orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700683 hw.setOrientation(orientation);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684
685 // update the shared control block
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
687 dcblk->orientation = orientation;
Mathias Agopian1b031492012-06-20 17:51:20 -0700688 dcblk->w = hw.getUserWidth();
689 dcblk->h = hw.getUserHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690
Mathias Agopian1b031492012-06-20 17:51:20 -0700691 // FIXME: mVisibleRegionsDirty & mDirtyRegion should this be per DisplayHardware?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 mVisibleRegionsDirty = true;
693 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 }
695
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700696 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
697 // layers have been added
698 mVisibleRegionsDirty = true;
699 }
700
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 // some layers might have been removed, so
702 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700703 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700704 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700706 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700707 const size_t count = previousLayers.size();
708 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700709 const sp<LayerBase>& layer(previousLayers[i]);
710 if (currentLayers.indexOf( layer ) < 0) {
711 // this layer is not visible anymore
Mathias Agopian4fec8732012-06-29 14:12:52 -0700712 // TODO: we could traverse the tree from front to back and compute the actual visible region
713 // TODO: we could cache the transformed region
714 Layer::State front(layer->drawingState());
715 Region visibleReg = front.transform.transform(
716 Region(Rect(front.active.w, front.active.h)));
717 dirtyRegion.orSelf(visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700718 }
719 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 }
722
723 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724 return dirtyRegion;
725}
726
727void SurfaceFlinger::commitTransaction()
728{
729 if (!mLayersPendingRemoval.isEmpty()) {
730 // Notify removed layers now that they can't be drawn from
731 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
732 mLayersPendingRemoval[i]->onRemoved();
733 }
734 mLayersPendingRemoval.clear();
735 }
736
737 mDrawingState = mCurrentState;
738 mTransationPending = false;
739 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800743 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744{
Mathias Agopian841cde52012-03-01 15:44:37 -0800745 ATRACE_CALL();
746
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 Region aboveOpaqueLayers;
748 Region aboveCoveredLayers;
749 Region dirty;
750
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700751 dirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752
753 size_t i = currentLayers.size();
754 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756
757 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700758 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759
Mathias Agopianab028732010-03-16 16:41:46 -0700760 /*
761 * opaqueRegion: area of a surface that is fully opaque.
762 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700764
765 /*
766 * visibleRegion: area of a surface that is visible on screen
767 * and not fully transparent. This is essentially the layer's
768 * footprint minus the opaque regions above it.
769 * Areas covered by a translucent surface are considered visible.
770 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700772
773 /*
774 * coveredRegion: area of a surface that is covered by all
775 * visible regions above it (which includes the translucent areas).
776 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700778
779
780 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800781 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700782 const bool translucent = !layer->isOpaque();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783 Rect bounds(layer->computeBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700785 if (!visibleRegion.isEmpty()) {
786 // Remove the transparent area from the visible region
787 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700788 Region transparentRegionScreen;
789 const Transform tr(s.transform);
790 if (tr.transformed()) {
791 if (tr.preserveRects()) {
792 // transform the transparent region
793 transparentRegionScreen = tr.transform(s.transparentRegion);
794 } else {
795 // transformation too complex, can't do the
796 // transparent region optimization.
797 transparentRegionScreen.clear();
798 }
799 } else {
800 transparentRegionScreen = s.transparentRegion;
801 }
802 visibleRegion.subtractSelf(transparentRegionScreen);
Mathias Agopianab028732010-03-16 16:41:46 -0700803 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804
Mathias Agopianab028732010-03-16 16:41:46 -0700805 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700806 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -0700807 if (s.alpha==255 && !translucent &&
808 ((layerOrientation & Transform::ROT_INVALID) == false)) {
809 // the opaque region is the layer's footprint
810 opaqueRegion = visibleRegion;
811 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 }
813 }
814
Mathias Agopianab028732010-03-16 16:41:46 -0700815 // Clip the covered region to the visible region
816 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
817
818 // Update aboveCoveredLayers for next (lower) layer
819 aboveCoveredLayers.orSelf(visibleRegion);
820
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 // subtract the opaque region covered by the layers above us
822 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823
824 // compute this layer's dirty region
825 if (layer->contentDirty) {
826 // we need to invalidate the whole region
827 dirty = visibleRegion;
828 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -0700829 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830 layer->contentDirty = false;
831 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700832 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700833 * the exposed region consists of two components:
834 * 1) what's VISIBLE now and was COVERED before
835 * 2) what's EXPOSED now less what was EXPOSED before
836 *
837 * note that (1) is conservative, we start with the whole
838 * visible region but only keep what used to be covered by
839 * something -- which mean it may have been exposed.
840 *
841 * (2) handles areas that were not covered by anything but got
842 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700843 */
Mathias Agopianab028732010-03-16 16:41:46 -0700844 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700845 const Region oldVisibleRegion = layer->visibleRegion;
846 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700847 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
848 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 }
850 dirty.subtractSelf(aboveOpaqueLayers);
851
852 // accumulate to the screen dirty region
853 dirtyRegion.orSelf(dirty);
854
Mathias Agopianab028732010-03-16 16:41:46 -0700855 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700857
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 // Store the visible region is screen space
859 layer->setVisibleRegion(visibleRegion);
860 layer->setCoveredRegion(coveredRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 }
862
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 opaqueRegion = aboveOpaqueLayers;
864}
865
Mathias Agopian4fec8732012-06-29 14:12:52 -0700866Region SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800868 ATRACE_CALL();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700869 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800871 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800872
Mathias Agopian4fec8732012-06-29 14:12:52 -0700873 bool visibleRegions = false;
874 const size_t count = currentLayers.size();
875 sp<LayerBase> const* layers = currentLayers.array();
876 for (size_t i=0 ; i<count ; i++) {
877 const sp<LayerBase>& layer(layers[i]);
878 dirtyRegion.orSelf( layer->latchBuffer(visibleRegions) );
879 }
Mathias Agopian4da75192010-08-10 17:19:56 -0700880
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700881 mVisibleRegionsDirty |= visibleRegions;
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700882
Mathias Agopian4fec8732012-06-29 14:12:52 -0700883 return dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884}
885
Mathias Agopianad456f92011-01-13 17:53:01 -0800886void SurfaceFlinger::invalidateHwcGeometry()
887{
888 mHwWorkListDirty = true;
889}
890
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800891void SurfaceFlinger::handleRefresh()
892{
893 bool needInvalidate = false;
894 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
895 const size_t count = currentLayers.size();
896 for (size_t i=0 ; i<count ; i++) {
897 const sp<LayerBase>& layer(currentLayers[i]);
898 if (layer->onPreComposition()) {
899 needInvalidate = true;
900 }
901 }
902 if (needInvalidate) {
903 signalLayerUpdate();
904 }
905}
906
907
Mathias Agopian1b031492012-06-20 17:51:20 -0700908void SurfaceFlinger::handleWorkList(const DisplayHardware& hw)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700909{
910 mHwWorkListDirty = false;
Mathias Agopian1b031492012-06-20 17:51:20 -0700911 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700912 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700913 const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana350ff92010-08-10 17:14:02 -0700914 const size_t count = currentLayers.size();
915 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700916
917 HWComposer::LayerListIterator cur = hwc.begin();
918 const HWComposer::LayerListIterator end = hwc.end();
919 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian4fec8732012-06-29 14:12:52 -0700920 currentLayers[i]->setGeometry(hw, *cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700921 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700922 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700923 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700924 }
925 }
926}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700927
Mathias Agopian1b031492012-06-20 17:51:20 -0700928void SurfaceFlinger::handleRepaint(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929{
Mathias Agopian841cde52012-03-01 15:44:37 -0800930 ATRACE_CALL();
931
Mathias Agopianb8a55602009-06-26 19:06:36 -0700932 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700933 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934
Glenn Kasten99ed2242011-12-15 09:51:17 -0800935 if (CC_UNLIKELY(mDebugRegion)) {
Mathias Agopian1b031492012-06-20 17:51:20 -0700936 debugFlashRegions(hw);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 }
938
Mathias Agopianb8a55602009-06-26 19:06:36 -0700939 // set the frame buffer
Mathias Agopianb8a55602009-06-26 19:06:36 -0700940 glMatrixMode(GL_MODELVIEW);
941 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942
943 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700944 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700945 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
946 // takes a rectangle, we must make sure to update that whole
947 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700948 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700949 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700950 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700951 // We need to redraw the rectangle that will be updated
952 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700953 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700954 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700955 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700956 } else {
957 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700959 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 }
961 }
962
Mathias Agopian1b031492012-06-20 17:51:20 -0700963 setupHardwareComposer(hw);
964 composeSurfaces(hw, mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700966 // update the swap region and clear the dirty region
967 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 mDirtyRegion.clear();
969}
970
Mathias Agopian1b031492012-06-20 17:51:20 -0700971void SurfaceFlinger::setupHardwareComposer(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700973 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700974 HWComposer::LayerListIterator cur = hwc.begin();
975 const HWComposer::LayerListIterator end = hwc.end();
976 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700977 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700978 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700979
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700980 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700981 size_t count = layers.size();
982
Steve Blocke6f43dd2012-01-06 19:20:56 +0000983 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700984 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
985 hwc.getNumLayers(), count);
986
987 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700988 if (hwc.initCheck() == NO_ERROR) {
989 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
990 }
Mathias Agopian45721772010-08-12 15:03:26 -0700991
992 /*
993 * update the per-frame h/w composer data for each layer
994 * and build the transparent region of the FB
995 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700996 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700997 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700998 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700999 }
1000 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001001 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -07001002}
Mathias Agopian45721772010-08-12 15:03:26 -07001003
Mathias Agopian1b031492012-06-20 17:51:20 -07001004void SurfaceFlinger::composeSurfaces(const DisplayHardware& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001005{
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001006 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001007 HWComposer::LayerListIterator cur = hwc.begin();
1008 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001009
1010 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001011 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001012 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -07001013
Mathias Agopianb9494d52012-04-18 02:28:45 -07001014 if (hwc.getLayerCount(HWC_OVERLAY)) {
1015 // when using overlays, we assume a fully transparent framebuffer
1016 // NOTE: we could reduce how much we need to clear, for instance
1017 // remove where there are opaque FB layers. however, on some
1018 // GPUs doing a "clean slate" glClear might be more efficient.
1019 // We'll revisit later if needed.
1020 glClearColor(0, 0, 0, 0);
1021 glClear(GL_COLOR_BUFFER_BIT);
1022 } else {
1023 // screen is already cleared here
1024 if (!mWormholeRegion.isEmpty()) {
1025 // can happen with SurfaceView
1026 drawWormhole();
1027 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001028 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001029
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001030 /*
1031 * and then, render the layers targeted at the framebuffer
1032 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001033
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001034 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001035 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001036 const Transform& tr = hw.getTransform();
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001037 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001038 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001039 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001040 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001041 if (cur->getCompositionType() == HWC_OVERLAY) {
1042 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001043 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001044 // never clear the very first layer since we're
1045 // guaranteed the FB is already cleared
Mathias Agopian1b031492012-06-20 17:51:20 -07001046 layer->clearWithOpenGL(hw, clip);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001047 }
1048 continue;
1049 }
1050 // render the layer
Mathias Agopian1b031492012-06-20 17:51:20 -07001051 layer->draw(hw, clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001052 }
1053 }
1054 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055}
1056
Mathias Agopian1b031492012-06-20 17:51:20 -07001057void SurfaceFlinger::debugFlashRegions(const DisplayHardware& hw)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
Mathias Agopian0a917752010-06-14 21:20:00 -07001059 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001060 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001061 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001062 return;
1063 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001064
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001065 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001066 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1067 mDirtyRegion.bounds() : hw.bounds());
Mathias Agopian1b031492012-06-20 17:51:20 -07001068 composeSurfaces(hw, repaint);
Mathias Agopian0a917752010-06-14 21:20:00 -07001069 }
1070
Mathias Agopianc492e672011-10-18 14:49:27 -07001071 glDisable(GL_TEXTURE_EXTERNAL_OES);
1072 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074
Mathias Agopian0926f502009-05-04 14:17:04 -07001075 static int toggle = 0;
1076 toggle = 1 - toggle;
1077 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001078 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001079 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001080 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001081 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082
Mathias Agopian20f68782009-05-11 00:03:41 -07001083 Region::const_iterator it = mDirtyRegion.begin();
1084 Region::const_iterator const end = mDirtyRegion.end();
1085 while (it != end) {
1086 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001088 { r.left, height - r.top },
1089 { r.left, height - r.bottom },
1090 { r.right, height - r.bottom },
1091 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 };
1093 glVertexPointer(2, GL_FLOAT, 0, vertices);
1094 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1095 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001096
Mathias Agopian0656a682011-09-20 17:22:44 -07001097 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098
1099 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001100 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101}
1102
1103void SurfaceFlinger::drawWormhole() const
1104{
1105 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1106 if (region.isEmpty())
1107 return;
1108
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001109 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001110 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001111 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001112 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001113
1114 GLfloat vertices[4][2];
1115 glVertexPointer(2, GL_FLOAT, 0, vertices);
1116 Region::const_iterator it = region.begin();
1117 Region::const_iterator const end = region.end();
1118 while (it != end) {
1119 const Rect& r = *it++;
1120 vertices[0][0] = r.left;
1121 vertices[0][1] = r.top;
1122 vertices[1][0] = r.right;
1123 vertices[1][1] = r.top;
1124 vertices[2][0] = r.right;
1125 vertices[2][1] = r.bottom;
1126 vertices[3][0] = r.left;
1127 vertices[3][1] = r.bottom;
1128 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1129 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130}
1131
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001132status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133{
1134 Mutex::Autolock _l(mStateLock);
1135 addLayer_l(layer);
1136 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1137 return NO_ERROR;
1138}
1139
Mathias Agopian96f08192010-06-02 23:28:45 -07001140status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1141{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001142 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001143 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1144}
1145
1146ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1147 const sp<LayerBaseClient>& lbc)
1148{
Mathias Agopian96f08192010-06-02 23:28:45 -07001149 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001150 size_t name = client->attachLayer(lbc);
1151
1152 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001153
1154 // add this layer to the current state list
1155 addLayer_l(lbc);
1156
Mathias Agopian4f113742011-05-03 16:21:41 -07001157 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001158}
1159
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001160status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161{
1162 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001163 status_t err = purgatorizeLayer_l(layer);
1164 if (err == NO_ERROR)
1165 setTransactionFlags(eTransactionNeeded);
1166 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167}
1168
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001169status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001171 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1172 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001173 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001174 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1176 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001177 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 return NO_ERROR;
1179 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001180 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181}
1182
Mathias Agopian9a112062009-04-17 19:36:26 -07001183status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1184{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001185 // First add the layer to the purgatory list, which makes sure it won't
1186 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001187 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001188 if (err >= 0) {
1189 mLayerPurgatory.add(layerBase);
1190 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001191
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001192 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001193
Mathias Agopian3d579642009-06-04 18:46:21 -07001194 // it's possible that we don't find a layer, because it might
1195 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001196 // from the user because there is a race between Client::destroySurface(),
1197 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001198 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1199}
1200
Mathias Agopian96f08192010-06-02 23:28:45 -07001201status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202{
Mathias Agopian96f08192010-06-02 23:28:45 -07001203 layer->forceVisibilityTransaction();
1204 setTransactionFlags(eTraversalNeeded);
1205 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206}
1207
Mathias Agopiandea20b12011-05-03 17:04:02 -07001208uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1209{
1210 return android_atomic_release_load(&mTransactionFlags);
1211}
1212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1214{
1215 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1216}
1217
Mathias Agopianbb641242010-05-18 17:06:55 -07001218uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219{
1220 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1221 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001222 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 }
1224 return old;
1225}
1226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001228void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001229 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001230 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001231
Jamie Gennis28378392011-10-12 17:39:00 -07001232 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001233 if (mCurrentState.orientation != orientation) {
1234 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1235 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001236 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001237 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001238 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001239 orientation);
1240 }
1241 }
1242
Mathias Agopian698c0872011-06-28 19:09:31 -07001243 const size_t count = state.size();
1244 for (size_t i=0 ; i<count ; i++) {
1245 const ComposerState& s(state[i]);
1246 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001247 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001248 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001249
Mathias Agopian386aa982011-11-07 21:58:03 -08001250 if (transactionFlags) {
1251 // this triggers the transaction
1252 setTransactionFlags(transactionFlags);
1253
1254 // if this is a synchronous transaction, wait for it to take effect
1255 // before returning.
1256 if (flags & eSynchronous) {
1257 mTransationPending = true;
1258 }
1259 while (mTransationPending) {
1260 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1261 if (CC_UNLIKELY(err != NO_ERROR)) {
1262 // just in case something goes wrong in SF, return to the
1263 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001264 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001265 mTransationPending = false;
1266 break;
1267 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001268 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 }
1270}
1271
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001272sp<ISurface> SurfaceFlinger::createSurface(
1273 ISurfaceComposerClient::surface_data_t* params,
1274 const String8& name,
1275 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1277 uint32_t flags)
1278{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001279 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001280 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001281
1282 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001283 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001284 int(w), int(h));
1285 return surfaceHandle;
1286 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001287
Mathias Agopianff615cc2012-02-24 14:58:36 -08001288 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001289 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 switch (flags & eFXSurfaceMask) {
1291 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001292 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1293 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 break;
1295 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001296 // for now we treat Blur as Dim, until we can implement it
1297 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001301 case eFXSurfaceScreenshot:
1302 layer = createScreenshotSurface(client, d, w, h, flags);
1303 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 }
1305
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001306 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001308 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001309 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001312 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001313 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001314 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001315 if (normalLayer != 0) {
1316 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001317 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001318 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001319 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001320
Mathias Agopian96f08192010-06-02 23:28:45 -07001321 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 }
1323
1324 return surfaceHandle;
1325}
1326
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001327sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001328 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001329 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001330 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331{
1332 // initialize the surfaces
1333 switch (format) { // TODO: take h/w into account
1334 case PIXEL_FORMAT_TRANSPARENT:
1335 case PIXEL_FORMAT_TRANSLUCENT:
1336 format = PIXEL_FORMAT_RGBA_8888;
1337 break;
1338 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001339#ifdef NO_RGBX_8888
1340 format = PIXEL_FORMAT_RGB_565;
1341#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001342 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001343#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 break;
1345 }
1346
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001347#ifdef NO_RGBX_8888
1348 if (format == PIXEL_FORMAT_RGBX_8888)
1349 format = PIXEL_FORMAT_RGBA_8888;
1350#endif
1351
Mathias Agopian96f08192010-06-02 23:28:45 -07001352 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001353 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001354 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001355 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001356 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 }
1358 return layer;
1359}
1360
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001361sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001362 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001363 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364{
Mathias Agopian96f08192010-06-02 23:28:45 -07001365 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001366 return layer;
1367}
1368
1369sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1370 const sp<Client>& client, DisplayID display,
1371 uint32_t w, uint32_t h, uint32_t flags)
1372{
1373 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 return layer;
1375}
1376
Mathias Agopian96f08192010-06-02 23:28:45 -07001377status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378{
Mathias Agopian9a112062009-04-17 19:36:26 -07001379 /*
1380 * called by the window manager, when a surface should be marked for
1381 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001382 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001383 * The surface is removed from the current and drawing lists, but placed
1384 * in the purgatory queue, so it's not destroyed right-away (we need
1385 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001386 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001387
Mathias Agopian48d819a2009-09-10 19:41:18 -07001388 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001389 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001390 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001391
Mathias Agopian48d819a2009-09-10 19:41:18 -07001392 if (layer != 0) {
1393 err = purgatorizeLayer_l(layer);
1394 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001395 setTransactionFlags(eTransactionNeeded);
1396 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001397 }
1398 return err;
1399}
1400
Mathias Agopianca4d3602011-05-19 15:38:14 -07001401status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001402{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001403 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001404 status_t err = NO_ERROR;
1405 sp<LayerBaseClient> l(layer.promote());
1406 if (l != NULL) {
1407 Mutex::Autolock _l(mStateLock);
1408 err = removeLayer_l(l);
1409 if (err == NAME_NOT_FOUND) {
1410 // The surface wasn't in the current list, which means it was
1411 // removed already, which means it is in the purgatory,
1412 // and need to be removed from there.
1413 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001414 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001415 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001416 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001417 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001418 "error removing layer=%p (%s)", l.get(), strerror(-err));
1419 }
1420 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421}
1422
Mathias Agopian698c0872011-06-28 19:09:31 -07001423uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001424 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001425 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001428 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1429 if (layer != 0) {
1430 const uint32_t what = s.what;
1431 if (what & ePositionChanged) {
1432 if (layer->setPosition(s.x, s.y))
1433 flags |= eTraversalNeeded;
1434 }
1435 if (what & eLayerChanged) {
1436 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1437 if (layer->setLayer(s.z)) {
1438 mCurrentState.layersSortedByZ.removeAt(idx);
1439 mCurrentState.layersSortedByZ.add(layer);
1440 // we need traversal (state changed)
1441 // AND transaction (list changed)
1442 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001445 if (what & eSizeChanged) {
1446 if (layer->setSize(s.w, s.h)) {
1447 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001448 }
1449 }
1450 if (what & eAlphaChanged) {
1451 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1452 flags |= eTraversalNeeded;
1453 }
1454 if (what & eMatrixChanged) {
1455 if (layer->setMatrix(s.matrix))
1456 flags |= eTraversalNeeded;
1457 }
1458 if (what & eTransparentRegionChanged) {
1459 if (layer->setTransparentRegionHint(s.transparentRegion))
1460 flags |= eTraversalNeeded;
1461 }
1462 if (what & eVisibilityChanged) {
1463 if (layer->setFlags(s.flags, s.mask))
1464 flags |= eTraversalNeeded;
1465 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001466 if (what & eCropChanged) {
1467 if (layer->setCrop(s.crop))
1468 flags |= eTraversalNeeded;
1469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001471 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472}
1473
Mathias Agopianb60314a2012-04-10 22:09:54 -07001474// ---------------------------------------------------------------------------
1475
1476void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001477 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001478 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001479 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001480 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001481 // this is a temporary work-around, eventually this should be called
1482 // by the power-manager
1483 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001484 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001485 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486}
1487
Mathias Agopianb60314a2012-04-10 22:09:54 -07001488void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001489 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopian1b031492012-06-20 17:51:20 -07001490 const DisplayHardware& hw(getDefaultDisplayHardware()); // XXX: this should be per DisplayHardware
Mathias Agopianb60314a2012-04-10 22:09:54 -07001491 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001492 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001493 hw.releaseScreen();
1494 // from this point on, SF will stop drawing
1495 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496}
1497
Colin Cross8e533062012-06-07 13:17:52 -07001498void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001499 class MessageScreenAcquired : public MessageBase {
1500 SurfaceFlinger* flinger;
1501 public:
1502 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1503 virtual bool handler() {
1504 flinger->onScreenAcquired();
1505 return true;
1506 }
1507 };
1508 sp<MessageBase> msg = new MessageScreenAcquired(this);
1509 postMessageSync(msg);
1510}
1511
Colin Cross8e533062012-06-07 13:17:52 -07001512void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001513 class MessageScreenReleased : public MessageBase {
1514 SurfaceFlinger* flinger;
1515 public:
1516 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1517 virtual bool handler() {
1518 flinger->onScreenReleased();
1519 return true;
1520 }
1521 };
1522 sp<MessageBase> msg = new MessageScreenReleased(this);
1523 postMessageSync(msg);
1524}
1525
1526// ---------------------------------------------------------------------------
1527
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1529{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001530 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 char buffer[SIZE];
1532 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001533
1534 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 snprintf(buffer, SIZE, "Permission Denial: "
1536 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1537 IPCThreadState::self()->getCallingPid(),
1538 IPCThreadState::self()->getCallingUid());
1539 result.append(buffer);
1540 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001541 // Try to get the main lock, but don't insist if we can't
1542 // (this would indicate SF is stuck, but we want to be able to
1543 // print something in dumpsys).
1544 int retry = 3;
1545 while (mStateLock.tryLock()<0 && --retry>=0) {
1546 usleep(1000000);
1547 }
1548 const bool locked(retry >= 0);
1549 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001550 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001551 "SurfaceFlinger appears to be unresponsive, "
1552 "dumping anyways (no locks held)\n");
1553 result.append(buffer);
1554 }
1555
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001556 bool dumpAll = true;
1557 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001558 size_t numArgs = args.size();
1559 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001560 if ((index < numArgs) &&
1561 (args[index] == String16("--list"))) {
1562 index++;
1563 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001564 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001565 }
1566
1567 if ((index < numArgs) &&
1568 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001569 index++;
1570 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001571 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001572 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001573
1574 if ((index < numArgs) &&
1575 (args[index] == String16("--latency-clear"))) {
1576 index++;
1577 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001578 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001581
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001582 if (dumpAll) {
1583 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001584 }
1585
Mathias Agopian9795c422009-08-26 16:36:26 -07001586 if (locked) {
1587 mStateLock.unlock();
1588 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 }
1590 write(fd, result.string(), result.size());
1591 return NO_ERROR;
1592}
1593
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001594void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1595 String8& result, char* buffer, size_t SIZE) const
1596{
1597 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1598 const size_t count = currentLayers.size();
1599 for (size_t i=0 ; i<count ; i++) {
1600 const sp<LayerBase>& layer(currentLayers[i]);
1601 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1602 result.append(buffer);
1603 }
1604}
1605
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001606void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1607 String8& result, char* buffer, size_t SIZE) const
1608{
1609 String8 name;
1610 if (index < args.size()) {
1611 name = String8(args[index]);
1612 index++;
1613 }
1614
1615 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1616 const size_t count = currentLayers.size();
1617 for (size_t i=0 ; i<count ; i++) {
1618 const sp<LayerBase>& layer(currentLayers[i]);
1619 if (name.isEmpty()) {
1620 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1621 result.append(buffer);
1622 }
1623 if (name.isEmpty() || (name == layer->getName())) {
1624 layer->dumpStats(result, buffer, SIZE);
1625 }
1626 }
1627}
1628
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001629void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1630 String8& result, char* buffer, size_t SIZE) const
1631{
1632 String8 name;
1633 if (index < args.size()) {
1634 name = String8(args[index]);
1635 index++;
1636 }
1637
1638 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1639 const size_t count = currentLayers.size();
1640 for (size_t i=0 ; i<count ; i++) {
1641 const sp<LayerBase>& layer(currentLayers[i]);
1642 if (name.isEmpty() || (name == layer->getName())) {
1643 layer->clearStats();
1644 }
1645 }
1646}
1647
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001648void SurfaceFlinger::dumpAllLocked(
1649 String8& result, char* buffer, size_t SIZE) const
1650{
1651 // figure out if we're stuck somewhere
1652 const nsecs_t now = systemTime();
1653 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1654 const nsecs_t inTransaction(mDebugInTransaction);
1655 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1656 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1657
1658 /*
1659 * Dump the visible layer list
1660 */
1661 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1662 const size_t count = currentLayers.size();
1663 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1664 result.append(buffer);
1665 for (size_t i=0 ; i<count ; i++) {
1666 const sp<LayerBase>& layer(currentLayers[i]);
1667 layer->dump(result, buffer, SIZE);
1668 }
1669
1670 /*
1671 * Dump the layers in the purgatory
1672 */
1673
1674 const size_t purgatorySize = mLayerPurgatory.size();
1675 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1676 result.append(buffer);
1677 for (size_t i=0 ; i<purgatorySize ; i++) {
1678 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1679 layer->shortDump(result, buffer, SIZE);
1680 }
1681
1682 /*
1683 * Dump SurfaceFlinger global state
1684 */
1685
1686 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1687 result.append(buffer);
1688
Mathias Agopian1b031492012-06-20 17:51:20 -07001689 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001690 const GLExtensions& extensions(GLExtensions::getInstance());
1691 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1692 extensions.getVendor(),
1693 extensions.getRenderer(),
1694 extensions.getVersion());
1695 result.append(buffer);
1696
1697 snprintf(buffer, SIZE, "EGL : %s\n",
Mathias Agopian1b031492012-06-20 17:51:20 -07001698 eglQueryString(hw.getEGLDisplay(),
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001699 EGL_VERSION_HW_ANDROID));
1700 result.append(buffer);
1701
1702 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1703 result.append(buffer);
1704
1705 mWormholeRegion.dump(result, "WormholeRegion");
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001706 snprintf(buffer, SIZE,
1707 " orientation=%d, canDraw=%d\n",
1708 mCurrentState.orientation, hw.canDraw());
1709 result.append(buffer);
1710 snprintf(buffer, SIZE,
1711 " last eglSwapBuffers() time: %f us\n"
1712 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001713 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001714 " refresh-rate : %f fps\n"
1715 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001716 " y-dpi : %f\n"
1717 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001718 mLastSwapBufferTime/1000.0,
1719 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001720 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001721 hw.getRefreshRate(),
1722 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001723 hw.getDpiY(),
1724 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001725 result.append(buffer);
1726
1727 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1728 inSwapBuffersDuration/1000.0);
1729 result.append(buffer);
1730
1731 snprintf(buffer, SIZE, " transaction time: %f us\n",
1732 inTransactionDuration/1000.0);
1733 result.append(buffer);
1734
1735 /*
1736 * VSYNC state
1737 */
1738 mEventThread->dump(result, buffer, SIZE);
1739
1740 /*
1741 * Dump HWComposer state
1742 */
1743 HWComposer& hwc(hw.getHwComposer());
1744 snprintf(buffer, SIZE, "h/w composer state:\n");
1745 result.append(buffer);
1746 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1747 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1748 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1749 result.append(buffer);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001750 hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001751
1752 /*
1753 * Dump gralloc state
1754 */
1755 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1756 alloc.dump(result);
1757 hw.dump(result);
1758}
1759
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760status_t SurfaceFlinger::onTransact(
1761 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1762{
1763 switch (code) {
1764 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001765 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001768 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001769 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001770 case BLANK:
1771 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772 {
1773 // codes that require permission check
1774 IPCThreadState* ipc = IPCThreadState::self();
1775 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001776 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001777 if ((uid != AID_GRAPHICS) &&
1778 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001779 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001780 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1781 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001783 break;
1784 }
1785 case CAPTURE_SCREEN:
1786 {
1787 // codes that require permission check
1788 IPCThreadState* ipc = IPCThreadState::self();
1789 const int pid = ipc->getCallingPid();
1790 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001791 if ((uid != AID_GRAPHICS) &&
1792 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001793 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001794 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1795 return PERMISSION_DENIED;
1796 }
1797 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798 }
1799 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1802 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001803 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001804 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001805 IPCThreadState* ipc = IPCThreadState::self();
1806 const int pid = ipc->getCallingPid();
1807 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001808 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001809 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 return PERMISSION_DENIED;
1811 }
1812 int n;
1813 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001814 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001815 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 return NO_ERROR;
1817 case 1002: // SHOW_UPDATES
1818 n = data.readInt32();
1819 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001820 invalidateHwcGeometry();
1821 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001823 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001824 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001825 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001827 case 1005:{ // force transaction
1828 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1829 return NO_ERROR;
1830 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001831 case 1006:{ // send empty update
1832 signalRefresh();
1833 return NO_ERROR;
1834 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001835 case 1008: // toggle use of hw composer
1836 n = data.readInt32();
1837 mDebugDisableHWC = n ? 1 : 0;
1838 invalidateHwcGeometry();
1839 repaintEverything();
1840 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001841 case 1009: // toggle use of transform hint
1842 n = data.readInt32();
1843 mDebugDisableTransformHint = n ? 1 : 0;
1844 invalidateHwcGeometry();
1845 repaintEverything();
1846 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001847 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001848 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849 reply->writeInt32(0);
1850 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001851 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001852 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853 return NO_ERROR;
1854 case 1013: {
1855 Mutex::Autolock _l(mStateLock);
Mathias Agopian1b031492012-06-20 17:51:20 -07001856 const DisplayHardware& hw(getDefaultDisplayHardware());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857 reply->writeInt32(hw.getPageFlipCount());
1858 }
1859 return NO_ERROR;
1860 }
1861 }
1862 return err;
1863}
1864
Mathias Agopian53331da2011-08-22 21:44:41 -07001865void SurfaceFlinger::repaintEverything() {
Mathias Agopian1b031492012-06-20 17:51:20 -07001866 const DisplayHardware& hw(getDefaultDisplayHardware()); // FIXME: this cannot be bound the default display
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001867 const Rect bounds(hw.getBounds());
1868 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001869 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001870}
1871
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001872void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1873 Mutex::Autolock _l(mInvalidateLock);
1874 mInvalidateRegion = reg;
1875}
1876
1877Region SurfaceFlinger::getAndClearInvalidateRegion() {
1878 Mutex::Autolock _l(mInvalidateLock);
1879 Region reg(mInvalidateRegion);
1880 mInvalidateRegion.clear();
1881 return reg;
1882}
1883
Mathias Agopian59119e62010-10-11 12:37:43 -07001884// ---------------------------------------------------------------------------
1885
Mathias Agopian118d0242011-10-13 16:02:48 -07001886status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1887 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1888{
1889 Mutex::Autolock _l(mStateLock);
1890 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1891}
1892
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001893status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1894 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001895{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001896 ATRACE_CALL();
1897
Mathias Agopian59119e62010-10-11 12:37:43 -07001898 if (!GLExtensions::getInstance().haveFramebufferObject())
1899 return INVALID_OPERATION;
1900
1901 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001902 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian59119e62010-10-11 12:37:43 -07001903 const uint32_t hw_w = hw.getWidth();
1904 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001905 GLfloat u = 1;
1906 GLfloat v = 1;
1907
1908 // make sure to clear all GL error flags
1909 while ( glGetError() != GL_NO_ERROR ) ;
1910
1911 // create a FBO
1912 GLuint name, tname;
1913 glGenTextures(1, &tname);
1914 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001915 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1916 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001917 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1918 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001919 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001920 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001921 GLint tw = (2 << (31 - clz(hw_w)));
1922 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001923 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1924 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001925 u = GLfloat(hw_w) / tw;
1926 v = GLfloat(hw_h) / th;
1927 }
1928 glGenFramebuffersOES(1, &name);
1929 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001930 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1931 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001932
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001933 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001934 glDisable(GL_TEXTURE_EXTERNAL_OES);
1935 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001936 glClearColor(0,0,0,1);
1937 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001938 glMatrixMode(GL_MODELVIEW);
1939 glLoadIdentity();
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001940 const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001941 const size_t count = layers.size();
1942 for (size_t i=0 ; i<count ; ++i) {
1943 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian1b031492012-06-20 17:51:20 -07001944 layer->drawForSreenShot(hw);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001945 }
1946
Mathias Agopian118d0242011-10-13 16:02:48 -07001947 hw.compositionComplete();
1948
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001949 // back to main framebuffer
1950 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001951 glDeleteFramebuffersOES(1, &name);
1952
1953 *textureName = tname;
1954 *uOut = u;
1955 *vOut = v;
1956 return NO_ERROR;
1957}
1958
1959// ---------------------------------------------------------------------------
1960
Mathias Agopianed9807b2012-05-18 14:18:08 -07001961class VSyncWaiter {
1962 DisplayEventReceiver::Event buffer[4];
1963 sp<Looper> looper;
1964 sp<IDisplayEventConnection> events;
1965 sp<BitTube> eventTube;
1966public:
1967 VSyncWaiter(const sp<EventThread>& eventThread) {
1968 looper = new Looper(true);
1969 events = eventThread->createEventConnection();
1970 eventTube = events->getDataChannel();
1971 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1972 events->requestNextVsync();
1973 }
1974
1975 void wait() {
1976 ssize_t n;
1977
1978 looper->pollOnce(-1);
1979 // we don't handle any errors here, it doesn't matter
1980 // and we don't want to take the risk to get stuck.
1981
1982 // drain the events...
1983 while ((n = DisplayEventReceiver::getEvents(
1984 eventTube, buffer, 4)) > 0) ;
1985
1986 events->requestNextVsync();
1987 }
1988};
1989
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1991{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001992 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07001993 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001994 const uint32_t hw_w = hw.getWidth();
1995 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001996 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001997
1998 GLfloat u, v;
1999 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07002000 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002001 if (result != NO_ERROR) {
2002 return result;
2003 }
2004
2005 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07002006 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002007 glBindTexture(GL_TEXTURE_2D, tname);
2008 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2009 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2010 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002011 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2012 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002013 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2014 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2015 glVertexPointer(2, GL_FLOAT, 0, vtx);
2016
Mathias Agopianffcf4652011-07-07 17:30:31 -07002017 /*
2018 * Texture coordinate mapping
2019 *
2020 * u
2021 * 1 +----------+---+
2022 * | | | | image is inverted
2023 * | V | | w.r.t. the texture
2024 * 1-v +----------+ | coordinates
2025 * | |
2026 * | |
2027 * | |
2028 * 0 +--------------+
2029 * 0 1
2030 *
2031 */
2032
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 class s_curve_interpolator {
2034 const float nbFrames, s, v;
2035 public:
2036 s_curve_interpolator(int nbFrames, float s)
2037 : nbFrames(1.0f / (nbFrames-1)), s(s),
2038 v(1.0f + expf(-s + 0.5f*s)) {
2039 }
2040 float operator()(int f) {
2041 const float x = f * nbFrames;
2042 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2043 }
2044 };
2045
2046 class v_stretch {
2047 const GLfloat hw_w, hw_h;
2048 public:
2049 v_stretch(uint32_t hw_w, uint32_t hw_h)
2050 : hw_w(hw_w), hw_h(hw_h) {
2051 }
2052 void operator()(GLfloat* vtx, float v) {
2053 const GLfloat w = hw_w + (hw_w * v);
2054 const GLfloat h = hw_h - (hw_h * v);
2055 const GLfloat x = (hw_w - w) * 0.5f;
2056 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002057 vtx[0] = x; vtx[1] = y;
2058 vtx[2] = x; vtx[3] = y + h;
2059 vtx[4] = x + w; vtx[5] = y + h;
2060 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002061 }
2062 };
2063
2064 class h_stretch {
2065 const GLfloat hw_w, hw_h;
2066 public:
2067 h_stretch(uint32_t hw_w, uint32_t hw_h)
2068 : hw_w(hw_w), hw_h(hw_h) {
2069 }
2070 void operator()(GLfloat* vtx, float v) {
2071 const GLfloat w = hw_w - (hw_w * v);
2072 const GLfloat h = 1.0f;
2073 const GLfloat x = (hw_w - w) * 0.5f;
2074 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002075 vtx[0] = x; vtx[1] = y;
2076 vtx[2] = x; vtx[3] = y + h;
2077 vtx[4] = x + w; vtx[5] = y + h;
2078 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002079 }
2080 };
2081
Mathias Agopianed9807b2012-05-18 14:18:08 -07002082 VSyncWaiter vsync(mEventThread);
2083
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002084 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002085 char value[PROPERTY_VALUE_MAX];
2086 property_get("debug.sf.electron_frames", value, "24");
2087 int nbFrames = (atoi(value) + 1) >> 1;
2088 if (nbFrames <= 0) // just in case
2089 nbFrames = 24;
2090
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002091 s_curve_interpolator itr(nbFrames, 7.5f);
2092 s_curve_interpolator itg(nbFrames, 8.0f);
2093 s_curve_interpolator itb(nbFrames, 8.5f);
2094
2095 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002096
2097 glMatrixMode(GL_TEXTURE);
2098 glLoadIdentity();
2099 glMatrixMode(GL_MODELVIEW);
2100 glLoadIdentity();
2101
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002102 glEnable(GL_BLEND);
2103 glBlendFunc(GL_ONE, GL_ONE);
2104 for (int i=0 ; i<nbFrames ; i++) {
2105 float x, y, w, h;
2106 const float vr = itr(i);
2107 const float vg = itg(i);
2108 const float vb = itb(i);
2109
Mathias Agopianed9807b2012-05-18 14:18:08 -07002110 // wait for vsync
2111 vsync.wait();
2112
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002113 // clear screen
2114 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002115 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002116 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002117
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118 // draw the red plane
2119 vverts(vtx, vr);
2120 glColorMask(1,0,0,1);
2121 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002122
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002123 // draw the green plane
2124 vverts(vtx, vg);
2125 glColorMask(0,1,0,1);
2126 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002127
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002128 // draw the blue plane
2129 vverts(vtx, vb);
2130 glColorMask(0,0,1,1);
2131 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002132
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002133 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002134 glDisable(GL_TEXTURE_2D);
2135 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002136 glColor4f(vg, vg, vg, 1);
2137 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2138 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002139 }
2140
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002141 h_stretch hverts(hw_w, hw_h);
2142 glDisable(GL_BLEND);
2143 glDisable(GL_TEXTURE_2D);
2144 glColorMask(1,1,1,1);
2145 for (int i=0 ; i<nbFrames ; i++) {
2146 const float v = itg(i);
2147 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002148
2149 // wait for vsync
2150 vsync.wait();
2151
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002152 glClear(GL_COLOR_BUFFER_BIT);
2153 glColor4f(1-v, 1-v, 1-v, 1);
2154 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2155 hw.flip(screenBounds);
2156 }
2157
2158 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002159 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2160 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002161 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002162 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002163 return NO_ERROR;
2164}
2165
2166status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2167{
2168 status_t result = PERMISSION_DENIED;
2169
2170 if (!GLExtensions::getInstance().haveFramebufferObject())
2171 return INVALID_OPERATION;
2172
2173
2174 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002175 const DisplayHardware& hw(getDefaultDisplayHardware());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002176 const uint32_t hw_w = hw.getWidth();
2177 const uint32_t hw_h = hw.getHeight();
2178 const Region screenBounds(hw.bounds());
2179
2180 GLfloat u, v;
2181 GLuint tname;
2182 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2183 if (result != NO_ERROR) {
2184 return result;
2185 }
2186
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002187 GLfloat vtx[8];
2188 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002189 glBindTexture(GL_TEXTURE_2D, tname);
2190 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2191 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2192 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002193 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2194 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002195 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2196 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2197 glVertexPointer(2, GL_FLOAT, 0, vtx);
2198
2199 class s_curve_interpolator {
2200 const float nbFrames, s, v;
2201 public:
2202 s_curve_interpolator(int nbFrames, float s)
2203 : nbFrames(1.0f / (nbFrames-1)), s(s),
2204 v(1.0f + expf(-s + 0.5f*s)) {
2205 }
2206 float operator()(int f) {
2207 const float x = f * nbFrames;
2208 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2209 }
2210 };
2211
2212 class v_stretch {
2213 const GLfloat hw_w, hw_h;
2214 public:
2215 v_stretch(uint32_t hw_w, uint32_t hw_h)
2216 : hw_w(hw_w), hw_h(hw_h) {
2217 }
2218 void operator()(GLfloat* vtx, float v) {
2219 const GLfloat w = hw_w + (hw_w * v);
2220 const GLfloat h = hw_h - (hw_h * v);
2221 const GLfloat x = (hw_w - w) * 0.5f;
2222 const GLfloat y = (hw_h - h) * 0.5f;
2223 vtx[0] = x; vtx[1] = y;
2224 vtx[2] = x; vtx[3] = y + h;
2225 vtx[4] = x + w; vtx[5] = y + h;
2226 vtx[6] = x + w; vtx[7] = y;
2227 }
2228 };
2229
2230 class h_stretch {
2231 const GLfloat hw_w, hw_h;
2232 public:
2233 h_stretch(uint32_t hw_w, uint32_t hw_h)
2234 : hw_w(hw_w), hw_h(hw_h) {
2235 }
2236 void operator()(GLfloat* vtx, float v) {
2237 const GLfloat w = hw_w - (hw_w * v);
2238 const GLfloat h = 1.0f;
2239 const GLfloat x = (hw_w - w) * 0.5f;
2240 const GLfloat y = (hw_h - h) * 0.5f;
2241 vtx[0] = x; vtx[1] = y;
2242 vtx[2] = x; vtx[3] = y + h;
2243 vtx[4] = x + w; vtx[5] = y + h;
2244 vtx[6] = x + w; vtx[7] = y;
2245 }
2246 };
2247
Mathias Agopianed9807b2012-05-18 14:18:08 -07002248 VSyncWaiter vsync(mEventThread);
2249
Mathias Agopiana6546e52010-10-14 12:33:07 -07002250 // the full animation is 12 frames
2251 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002252 s_curve_interpolator itr(nbFrames, 7.5f);
2253 s_curve_interpolator itg(nbFrames, 8.0f);
2254 s_curve_interpolator itb(nbFrames, 8.5f);
2255
2256 h_stretch hverts(hw_w, hw_h);
2257 glDisable(GL_BLEND);
2258 glDisable(GL_TEXTURE_2D);
2259 glColorMask(1,1,1,1);
2260 for (int i=nbFrames-1 ; i>=0 ; i--) {
2261 const float v = itg(i);
2262 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002263
2264 // wait for vsync
2265 vsync.wait();
2266
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002267 glClear(GL_COLOR_BUFFER_BIT);
2268 glColor4f(1-v, 1-v, 1-v, 1);
2269 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2270 hw.flip(screenBounds);
2271 }
2272
Mathias Agopiana6546e52010-10-14 12:33:07 -07002273 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002274 v_stretch vverts(hw_w, hw_h);
2275 glEnable(GL_BLEND);
2276 glBlendFunc(GL_ONE, GL_ONE);
2277 for (int i=nbFrames-1 ; i>=0 ; i--) {
2278 float x, y, w, h;
2279 const float vr = itr(i);
2280 const float vg = itg(i);
2281 const float vb = itb(i);
2282
Mathias Agopianed9807b2012-05-18 14:18:08 -07002283 // wait for vsync
2284 vsync.wait();
2285
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002286 // clear screen
2287 glColorMask(1,1,1,1);
2288 glClear(GL_COLOR_BUFFER_BIT);
2289 glEnable(GL_TEXTURE_2D);
2290
2291 // draw the red plane
2292 vverts(vtx, vr);
2293 glColorMask(1,0,0,1);
2294 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2295
2296 // draw the green plane
2297 vverts(vtx, vg);
2298 glColorMask(0,1,0,1);
2299 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2300
2301 // draw the blue plane
2302 vverts(vtx, vb);
2303 glColorMask(0,0,1,1);
2304 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2305
2306 hw.flip(screenBounds);
2307 }
2308
2309 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002310 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002311 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002312 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002313 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002314
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002315 return NO_ERROR;
2316}
2317
2318// ---------------------------------------------------------------------------
2319
Mathias Agopianabd671a2010-10-14 14:54:06 -07002320status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002321{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002322 ATRACE_CALL();
2323
Mathias Agopian1b031492012-06-20 17:51:20 -07002324 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002325 if (!hw.canDraw()) {
2326 // we're already off
2327 return NO_ERROR;
2328 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002329
2330 // turn off hwc while we're doing the animation
2331 hw.getHwComposer().disable();
2332 // and make sure to turn it back on (if needed) next time we compose
2333 invalidateHwcGeometry();
2334
Mathias Agopianabd671a2010-10-14 14:54:06 -07002335 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2336 electronBeamOffAnimationImplLocked();
2337 }
2338
2339 // always clear the whole screen at the end of the animation
2340 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002341 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002342 hw.flip( Region(hw.bounds()) );
2343
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002344 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002345}
2346
2347status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2348{
Mathias Agopian59119e62010-10-11 12:37:43 -07002349 class MessageTurnElectronBeamOff : public MessageBase {
2350 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002351 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002352 status_t result;
2353 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002354 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2355 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002356 }
2357 status_t getResult() const {
2358 return result;
2359 }
2360 virtual bool handler() {
2361 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002362 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002363 return true;
2364 }
2365 };
2366
Mathias Agopianabd671a2010-10-14 14:54:06 -07002367 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002368 status_t res = postMessageSync(msg);
2369 if (res == NO_ERROR) {
2370 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002371
2372 // work-around: when the power-manager calls us we activate the
2373 // animation. eventually, the "on" animation will be called
2374 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002375 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002376 }
2377 return res;
2378}
2379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002380// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002381
Mathias Agopianabd671a2010-10-14 14:54:06 -07002382status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002383{
Mathias Agopian1b031492012-06-20 17:51:20 -07002384 DisplayHardware& hw(const_cast<DisplayHardware&>(getDefaultDisplayHardware()));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002385 if (hw.canDraw()) {
2386 // we're already on
2387 return NO_ERROR;
2388 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002389 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2390 electronBeamOnAnimationImplLocked();
2391 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002392
2393 // make sure to redraw the whole screen when the animation is done
2394 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002395 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002396
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002397 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002398}
2399
2400status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2401{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002402 class MessageTurnElectronBeamOn : public MessageBase {
2403 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002404 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002405 status_t result;
2406 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002407 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2408 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002409 }
2410 status_t getResult() const {
2411 return result;
2412 }
2413 virtual bool handler() {
2414 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002415 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002416 return true;
2417 }
2418 };
2419
Mathias Agopianabd671a2010-10-14 14:54:06 -07002420 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002421 return NO_ERROR;
2422}
2423
2424// ---------------------------------------------------------------------------
2425
Mathias Agopian74c40c02010-09-29 13:02:36 -07002426status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2427 sp<IMemoryHeap>* heap,
2428 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002429 uint32_t sw, uint32_t sh,
2430 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002432 ATRACE_CALL();
2433
Mathias Agopian74c40c02010-09-29 13:02:36 -07002434 status_t result = PERMISSION_DENIED;
2435
2436 // only one display supported for now
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002437 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002438 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002439 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002440
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002441 if (!GLExtensions::getInstance().haveFramebufferObject()) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002442 return INVALID_OPERATION;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002443 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002444
2445 // get screen geometry
Mathias Agopian1b031492012-06-20 17:51:20 -07002446 const DisplayHardware& hw(getDisplayHardware(dpy));
Mathias Agopian74c40c02010-09-29 13:02:36 -07002447 const uint32_t hw_w = hw.getWidth();
2448 const uint32_t hw_h = hw.getHeight();
2449
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002450 // if we have secure windows on this display, never allow the screen capture
2451 if (hw.getSecureLayerVisible()) {
2452 return PERMISSION_DENIED;
2453 }
2454
2455 if ((sw > hw_w) || (sh > hw_h)) {
Mathias Agopian74c40c02010-09-29 13:02:36 -07002456 return BAD_VALUE;
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002457 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002458
2459 sw = (!sw) ? hw_w : sw;
2460 sh = (!sh) ? hw_h : sh;
2461 const size_t size = sw * sh * 4;
2462
Steve Block9d453682011-12-20 16:23:08 +00002463 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002464 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002465
Mathias Agopian74c40c02010-09-29 13:02:36 -07002466 // make sure to clear all GL error flags
2467 while ( glGetError() != GL_NO_ERROR ) ;
2468
2469 // create a FBO
2470 GLuint name, tname;
2471 glGenRenderbuffersOES(1, &tname);
2472 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2473 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002474
Mathias Agopian74c40c02010-09-29 13:02:36 -07002475 glGenFramebuffersOES(1, &name);
2476 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2477 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2478 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2479
2480 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002481
Mathias Agopian74c40c02010-09-29 13:02:36 -07002482 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2483
2484 // invert everything, b/c glReadPixel() below will invert the FB
2485 glViewport(0, 0, sw, sh);
2486 glMatrixMode(GL_PROJECTION);
2487 glPushMatrix();
2488 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002489 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002490 glMatrixMode(GL_MODELVIEW);
2491
2492 // redraw the screen entirely...
2493 glClearColor(0,0,0,1);
2494 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002495
Jamie Gennis9575f602011-10-07 14:51:16 -07002496 const LayerVector& layers(mDrawingState.layersSortedByZ);
2497 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002498 for (size_t i=0 ; i<count ; ++i) {
2499 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002500 const uint32_t flags = layer->drawingState().flags;
2501 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2502 const uint32_t z = layer->drawingState().z;
2503 if (z >= minLayerZ && z <= maxLayerZ) {
Mathias Agopian1b031492012-06-20 17:51:20 -07002504 layer->drawForSreenShot(hw);
Mathias Agopianb0610332011-08-25 14:36:43 -07002505 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002506 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002507 }
2508
Mathias Agopian74c40c02010-09-29 13:02:36 -07002509 // check for errors and return screen capture
2510 if (glGetError() != GL_NO_ERROR) {
2511 // error while rendering
2512 result = INVALID_OPERATION;
2513 } else {
2514 // allocate shared memory large enough to hold the
2515 // screen capture
2516 sp<MemoryHeapBase> base(
2517 new MemoryHeapBase(size, 0, "screen-capture") );
2518 void* const ptr = base->getBase();
2519 if (ptr) {
2520 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002521 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002522 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2523 if (glGetError() == GL_NO_ERROR) {
2524 *heap = base;
2525 *w = sw;
2526 *h = sh;
2527 *f = PIXEL_FORMAT_RGBA_8888;
2528 result = NO_ERROR;
2529 }
2530 } else {
2531 result = NO_MEMORY;
2532 }
2533 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002534 glViewport(0, 0, hw_w, hw_h);
2535 glMatrixMode(GL_PROJECTION);
2536 glPopMatrix();
2537 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002538 } else {
2539 result = BAD_VALUE;
2540 }
2541
2542 // release FBO resources
2543 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2544 glDeleteRenderbuffersOES(1, &tname);
2545 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002546
2547 hw.compositionComplete();
2548
Steve Block9d453682011-12-20 16:23:08 +00002549 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002550
Mathias Agopian74c40c02010-09-29 13:02:36 -07002551 return result;
2552}
2553
2554
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002555status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2556 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002557 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002558 uint32_t sw, uint32_t sh,
2559 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002560{
2561 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002562 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002563 return BAD_VALUE;
2564
2565 if (!GLExtensions::getInstance().haveFramebufferObject())
2566 return INVALID_OPERATION;
2567
2568 class MessageCaptureScreen : public MessageBase {
2569 SurfaceFlinger* flinger;
2570 DisplayID dpy;
2571 sp<IMemoryHeap>* heap;
2572 uint32_t* w;
2573 uint32_t* h;
2574 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002575 uint32_t sw;
2576 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002577 uint32_t minLayerZ;
2578 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002579 status_t result;
2580 public:
2581 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002582 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002583 uint32_t sw, uint32_t sh,
2584 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002585 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002586 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2587 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2588 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002589 {
2590 }
2591 status_t getResult() const {
2592 return result;
2593 }
2594 virtual bool handler() {
2595 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002596 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002597 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002598 return true;
2599 }
2600 };
2601
2602 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002603 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002604 status_t res = postMessageSync(msg);
2605 if (res == NO_ERROR) {
2606 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2607 }
2608 return res;
2609}
2610
2611// ---------------------------------------------------------------------------
2612
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002613sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2614{
2615 sp<Layer> result;
2616 Mutex::Autolock _l(mStateLock);
2617 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2618 return result;
2619}
2620
2621// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002622
Jamie Gennis9a78c902011-01-12 18:30:40 -08002623GraphicBufferAlloc::GraphicBufferAlloc() {}
2624
2625GraphicBufferAlloc::~GraphicBufferAlloc() {}
2626
2627sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002628 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002629 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2630 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002631 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002632 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002633 if (err == NO_MEMORY) {
2634 GraphicBuffer::dumpAllocationsToSystemLog();
2635 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002636 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002637 "failed (%s), handle=%p",
2638 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002639 return 0;
2640 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002641 return graphicBuffer;
2642}
2643
Jamie Gennis9a78c902011-01-12 18:30:40 -08002644// ---------------------------------------------------------------------------
2645
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002646}; // namespace android