blob: 07ea3a060bb975269a8eab4ab2caa8ec397977aa [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 Agopiandb403e82012-06-18 16:47:56 -070053#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Glenn Kasten1db13d72011-12-19 13:55:34 -080064#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070066#include <gui/BitTube.h>
Mathias Agopian3094df32012-06-18 18:06:45 -070067#include <gui/SurfaceTextureClient.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070068
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080069#define EGL_VERSION_HW_ANDROID 0x3143
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#define DISPLAY_COUNT 1
72
73namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian99b49842011-06-27 16:05:52 -070076const String16 sHardwareTest("android.permission.HARDWARE_TEST");
77const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
78const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
79const String16 sDump("android.permission.DUMP");
80
81// ---------------------------------------------------------------------------
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083SurfaceFlinger::SurfaceFlinger()
84 : BnSurfaceComposer(), Thread(false),
85 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070086 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070090 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070091 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700100 mBootFinished(false),
Mathias Agopian3094df32012-06-18 18:06:45 -0700101 mSecureFrameBuffer(0),
102 mExternalDisplaySurface(EGL_NO_SURFACE)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
Steve Blocka19954a2012-01-04 20:05:49 +0000109 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
Colin Cross3854ed52012-03-23 14:17:18 -0700117#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700123#else
124#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700125#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126
Steve Blocka19954a2012-01-04 20:05:49 +0000127 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000128 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800131void SurfaceFlinger::onFirstRef()
132{
133 mEventQueue.init(this);
134
135 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
136
137 // Wait for the main thread to be done with its initialization
138 mReadyToRunBarrier.wait();
139}
140
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142SurfaceFlinger::~SurfaceFlinger()
143{
144 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145}
146
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800147void SurfaceFlinger::binderDied(const wp<IBinder>& who)
148{
149 // the window manager died on us. prepare its eulogy.
150
151 // reset screen orientation
152 Vector<ComposerState> state;
153 setTransactionState(state, eOrientationDefault, 0);
154
155 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700156 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800157}
158
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700159sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700161 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162}
163
Mathias Agopian7e27f052010-05-28 14:22:23 -0700164sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
Mathias Agopian96f08192010-06-02 23:28:45 -0700166 sp<ISurfaceComposerClient> bclient;
167 sp<Client> client(new Client(this));
168 status_t err = client->initCheck();
169 if (err == NO_ERROR) {
170 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 return bclient;
173}
174
Jamie Gennis9a78c902011-01-12 18:30:40 -0800175sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
176{
177 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
178 return gba;
179}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
182{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000183 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 const GraphicPlane& plane(mGraphicPlanes[dpy]);
185 return plane;
186}
187
188GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
189{
190 return const_cast<GraphicPlane&>(
191 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
192}
193
194void SurfaceFlinger::bootFinished()
195{
196 const nsecs_t now = systemTime();
197 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000198 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700199 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700200
201 // wait patiently for the window manager death
202 const String16 name("window");
203 sp<IBinder> window(defaultServiceManager()->getService(name));
204 if (window != 0) {
205 window->linkToDeath(this);
206 }
207
208 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700209 // formerly we would just kill the process, but we now ask it to exit so it
210 // can choose where to stop the animation.
211 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212}
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214static inline uint16_t pack565(int r, int g, int b) {
215 return (r<<11)|(g<<5)|b;
216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218status_t SurfaceFlinger::readyToRun()
219{
Steve Blocka19954a2012-01-04 20:05:49 +0000220 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 "Initializing graphics H/W...");
222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 // we only support one display currently
224 int dpy = 0;
225
226 {
227 // initialize the main display
228 GraphicPlane& plane(graphicPlane(dpy));
229 DisplayHardware* const hw = new DisplayHardware(this, dpy);
230 plane.setDisplayHardware(hw);
231 }
232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 // create the shared control-block
234 mServerHeap = new MemoryHeapBase(4096,
235 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000236 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700238 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000239 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700240
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700241 new(mServerCblk) surface_flinger_cblk_t;
242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 // initialize primary screen
244 // (other display should be initialized in the same manner, but
245 // asynchronously, as they could come and go. None of this is supported
246 // yet).
247 const GraphicPlane& plane(graphicPlane(dpy));
248 const DisplayHardware& hw = plane.displayHardware();
249 const uint32_t w = hw.getWidth();
250 const uint32_t h = hw.getHeight();
251 const uint32_t f = hw.getFormat();
252 hw.makeCurrent();
253
254 // initialize the shared control block
255 mServerCblk->connected |= 1<<dpy;
256 display_cblk_t* dcblk = mServerCblk->displays + dpy;
257 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800258 dcblk->w = plane.getWidth();
259 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 dcblk->format = f;
261 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
262 dcblk->xdpi = hw.getDpiX();
263 dcblk->ydpi = hw.getDpiY();
264 dcblk->fps = hw.getRefreshRate();
265 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266
267 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700269 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 glShadeModel(GL_FLAT);
272 glDisable(GL_DITHER);
273 glDisable(GL_CULL_FACE);
274
275 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
276 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 glGenTextures(1, &mWormholeTexName);
279 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700285 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
286
287 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
288 glGenTextures(1, &mProtectedTexName);
289 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
293 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
295 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
297 glViewport(0, 0, w, h);
298 glMatrixMode(GL_PROJECTION);
299 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700300 // put the origin in the left-bottom corner
301 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 -0800302
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800303
304 // start the EventThread
305 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800306 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
308 /*
309 * We're now ready to accept clients...
310 */
311
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800312 mReadyToRunBarrier.open();
313
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700314 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700315 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 return NO_ERROR;
318}
319
Mathias Agopiana67e4182012-06-19 17:26:12 -0700320void SurfaceFlinger::startBootAnim() {
321 // start boot animation
322 property_set("service.bootanim.exit", "0");
323 property_set("ctl.start", "bootanim");
324}
325
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800327
Jamie Gennis582270d2011-08-17 18:19:00 -0700328bool SurfaceFlinger::authenticateSurfaceTexture(
329 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800330 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700331 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800332
333 // Check the visible layer list for the ISurface
334 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
335 size_t count = currentLayers.size();
336 for (size_t i=0 ; i<count ; i++) {
337 const sp<LayerBase>& layer(currentLayers[i]);
338 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700339 if (lbc != NULL) {
340 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
341 if (lbcBinder == surfaceTextureBinder) {
342 return true;
343 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800344 }
345 }
346
347 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700348 // SurfaceTexture gets destroyed before all the clients are done using it,
349 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800350 // will instead fail later on when the client tries to use the surface,
351 // which should be reported as "surface XYZ returned an -ENODEV". The
352 // purgatorized layers are no less authentic than the visible ones, so this
353 // should not cause any harm.
354 size_t purgatorySize = mLayerPurgatory.size();
355 for (size_t i=0 ; i<purgatorySize ; i++) {
356 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
357 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700358 if (lbc != NULL) {
359 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
360 if (lbcBinder == surfaceTextureBinder) {
361 return true;
362 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800363 }
364 }
365
366 return false;
367}
368
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800369// ----------------------------------------------------------------------------
370
371sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800372 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700373}
374
Mathias Agopian3094df32012-06-18 18:06:45 -0700375void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) {
376 const DisplayHardware& hw(graphicPlane(0).displayHardware());
377 EGLSurface result = EGL_NO_SURFACE;
378 EGLSurface old_surface = EGL_NO_SURFACE;
379 sp<SurfaceTextureClient> stc;
380
381 if (display != NULL) {
382 stc = new SurfaceTextureClient(display);
383 result = eglCreateWindowSurface(hw.getEGLDisplay(),
384 hw.getEGLConfig(), (EGLNativeWindowType)stc.get(), NULL);
385 ALOGE_IF(result == EGL_NO_SURFACE,
386 "eglCreateWindowSurface failed (ISurfaceTexture=%p)",
387 display.get());
388 }
389
390 { // scope for the lock
391 Mutex::Autolock _l(mStateLock);
392 old_surface = mExternalDisplaySurface;
393 mExternalDisplayNativeWindow = stc;
394 mExternalDisplaySurface = result;
395 ALOGD("mExternalDisplaySurface = %p", result);
396 }
397
398 if (old_surface != EGL_NO_SURFACE) {
399 // Note: EGL allows to destroy an object while its current
400 // it will fail to become current next time though.
401 eglDestroySurface(hw.getEGLDisplay(), old_surface);
402 }
403}
404
405EGLSurface SurfaceFlinger::getExternalDisplaySurface() const {
406 Mutex::Autolock _l(mStateLock);
407 return mExternalDisplaySurface;
408}
409
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800411
412void SurfaceFlinger::waitForEvent() {
413 mEventQueue.waitMessage();
414}
415
416void SurfaceFlinger::signalTransaction() {
417 mEventQueue.invalidate();
418}
419
420void SurfaceFlinger::signalLayerUpdate() {
421 mEventQueue.invalidate();
422}
423
424void SurfaceFlinger::signalRefresh() {
425 mEventQueue.refresh();
426}
427
428status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
429 nsecs_t reltime, uint32_t flags) {
430 return mEventQueue.postMessage(msg, reltime);
431}
432
433status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
434 nsecs_t reltime, uint32_t flags) {
435 status_t res = mEventQueue.postMessage(msg, reltime);
436 if (res == NO_ERROR) {
437 msg->wait();
438 }
439 return res;
440}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441
442bool SurfaceFlinger::threadLoop()
443{
444 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 return true;
446}
447
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800448void SurfaceFlinger::onMessageReceived(int32_t what)
449{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800450 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800451 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700452 case MessageQueue::REFRESH: {
453// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800454 // if we're in a global transaction, don't do anything.
455 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
456 uint32_t transactionFlags = peekTransactionFlags(mask);
457 if (CC_UNLIKELY(transactionFlags)) {
458 handleTransaction(transactionFlags);
459 }
460
461 // post surfaces (if needed)
462 handlePageFlip();
463
Mathias Agopian69a655c2012-04-11 20:43:19 -0700464// signalRefresh();
465//
466// } break;
467//
468// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800469
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800470 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800471
Mathias Agopian303d5382012-02-05 01:49:16 -0800472 const DisplayHardware& hw(graphicPlane(0).displayHardware());
473
Mathias Agopian69a655c2012-04-11 20:43:19 -0700474// if (mDirtyRegion.isEmpty()) {
475// return;
476// }
477
Mathias Agopianb048cef2012-02-04 15:44:04 -0800478 if (CC_UNLIKELY(mHwWorkListDirty)) {
479 // build the h/w work list
480 handleWorkList();
481 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800482
Mathias Agopianb048cef2012-02-04 15:44:04 -0800483 if (CC_LIKELY(hw.canDraw())) {
484 // repaint the framebuffer (if needed)
485 handleRepaint();
486 // inform the h/w that we're done compositing
487 hw.compositionComplete();
488 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800489 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800490 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800491 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800492 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800493
Mathias Agopian3094df32012-06-18 18:06:45 -0700494 // render to the external display if we have one
495 EGLSurface externalDisplaySurface = getExternalDisplaySurface();
496 if (externalDisplaySurface != EGL_NO_SURFACE) {
497 EGLSurface cur = eglGetCurrentSurface(EGL_DRAW);
498 EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(),
499 externalDisplaySurface, externalDisplaySurface,
500 eglGetCurrentContext());
501
502 ALOGE_IF(!success, "eglMakeCurrent -> external failed");
503
504 if (success) {
505 // redraw the screen entirely...
506 glDisable(GL_TEXTURE_EXTERNAL_OES);
507 glDisable(GL_TEXTURE_2D);
508 glClearColor(0,0,0,1);
509 glClear(GL_COLOR_BUFFER_BIT);
510 glMatrixMode(GL_MODELVIEW);
511 glLoadIdentity();
512 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
513 const size_t count = layers.size();
514 for (size_t i=0 ; i<count ; ++i) {
515 const sp<LayerBase>& layer(layers[i]);
516 layer->drawForSreenShot();
517 }
518
519 success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface);
520 ALOGE_IF(!success, "external display eglSwapBuffers failed");
521
522 hw.compositionComplete();
523 }
524
525 success = eglMakeCurrent(eglGetCurrentDisplay(),
526 cur, cur, eglGetCurrentContext());
527
528 ALOGE_IF(!success, "eglMakeCurrent -> internal failed");
529 }
530
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800531 } break;
532 }
533}
534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535void SurfaceFlinger::postFramebuffer()
536{
Mathias Agopian841cde52012-03-01 15:44:37 -0800537 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800538 // mSwapRegion can be empty here is some cases, for instance if a hidden
539 // or fully transparent window is updating.
540 // in that case, we need to flip anyways to not risk a deadlock with
541 // h/w composer.
542
Mathias Agopiana44b0412011-10-16 18:46:35 -0700543 const DisplayHardware& hw(graphicPlane(0).displayHardware());
544 const nsecs_t now = systemTime();
545 mDebugInSwapBuffers = now;
546 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800547
548 size_t numLayers = mVisibleLayersSortedByZ.size();
Jesse Hallef194142012-06-14 14:45:17 -0700549 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
550 if (hwc.initCheck() == NO_ERROR) {
551 HWComposer::LayerListIterator cur = hwc.begin();
552 const HWComposer::LayerListIterator end = hwc.end();
553 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
554 mVisibleLayersSortedByZ[i]->onLayerDisplayed(&*cur);
555 }
556 } else {
557 for (size_t i = 0; i < numLayers; i++) {
558 mVisibleLayersSortedByZ[i]->onLayerDisplayed(NULL);
559 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800560 }
561
Mathias Agopiana44b0412011-10-16 18:46:35 -0700562 mLastSwapBufferTime = systemTime() - now;
563 mDebugInSwapBuffers = 0;
564 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565}
566
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
568{
Mathias Agopian841cde52012-03-01 15:44:37 -0800569 ATRACE_CALL();
570
Mathias Agopianca4d3602011-05-19 15:38:14 -0700571 Mutex::Autolock _l(mStateLock);
572 const nsecs_t now = systemTime();
573 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574
Mathias Agopianca4d3602011-05-19 15:38:14 -0700575 // Here we're guaranteed that some transaction flags are set
576 // so we can call handleTransactionLocked() unconditionally.
577 // We call getTransactionFlags(), which will also clear the flags,
578 // with mStateLock held to guarantee that mCurrentState won't change
579 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700580
Mathias Agopianca4d3602011-05-19 15:38:14 -0700581 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
582 transactionFlags = getTransactionFlags(mask);
583 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700584
Mathias Agopianca4d3602011-05-19 15:38:14 -0700585 mLastTransactionTime = systemTime() - now;
586 mDebugInTransaction = 0;
587 invalidateHwcGeometry();
588 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700589}
590
Mathias Agopianca4d3602011-05-19 15:38:14 -0700591void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700592{
593 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 const size_t count = currentLayers.size();
595
596 /*
597 * Traversal of the children
598 * (perform the transaction for each of them if needed)
599 */
600
601 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
602 if (layersNeedTransaction) {
603 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700604 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
606 if (!trFlags) continue;
607
608 const uint32_t flags = layer->doTransaction(0);
609 if (flags & Layer::eVisibleRegion)
610 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
612 }
613
614 /*
615 * Perform our own transaction if needed
616 */
617
618 if (transactionFlags & eTransactionNeeded) {
619 if (mCurrentState.orientation != mDrawingState.orientation) {
620 // the orientation has changed, recompute all visible regions
621 // and invalidate everything.
622
623 const int dpy = 0;
624 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700625 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 GraphicPlane& plane(graphicPlane(dpy));
627 plane.setOrientation(orientation);
628
629 // update the shared control block
630 const DisplayHardware& hw(plane.displayHardware());
631 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
632 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800633 dcblk->w = plane.getWidth();
634 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635
636 mVisibleRegionsDirty = true;
637 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 }
639
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700640 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
641 // layers have been added
642 mVisibleRegionsDirty = true;
643 }
644
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 // some layers might have been removed, so
646 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700648 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700650 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700651 const size_t count = previousLayers.size();
652 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700653 const sp<LayerBase>& layer(previousLayers[i]);
654 if (currentLayers.indexOf( layer ) < 0) {
655 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700656 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700657 }
658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 }
661
662 commitTransaction();
663}
664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800666 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667{
Mathias Agopian841cde52012-03-01 15:44:37 -0800668 ATRACE_CALL();
669
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670 const GraphicPlane& plane(graphicPlane(0));
671 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700672 const DisplayHardware& hw(plane.displayHardware());
673 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674
675 Region aboveOpaqueLayers;
676 Region aboveCoveredLayers;
677 Region dirty;
678
679 bool secureFrameBuffer = false;
680
681 size_t i = currentLayers.size();
682 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700683 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 layer->validateVisibility(planeTransform);
685
686 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700687 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688
Mathias Agopianab028732010-03-16 16:41:46 -0700689 /*
690 * opaqueRegion: area of a surface that is fully opaque.
691 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700693
694 /*
695 * visibleRegion: area of a surface that is visible on screen
696 * and not fully transparent. This is essentially the layer's
697 * footprint minus the opaque regions above it.
698 * Areas covered by a translucent surface are considered visible.
699 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700701
702 /*
703 * coveredRegion: area of a surface that is covered by all
704 * visible regions above it (which includes the translucent areas).
705 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700707
708
709 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800710 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700711 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700712 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700714 visibleRegion.andSelf(screenRegion);
715 if (!visibleRegion.isEmpty()) {
716 // Remove the transparent area from the visible region
717 if (translucent) {
718 visibleRegion.subtractSelf(layer->transparentRegionScreen);
719 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720
Mathias Agopianab028732010-03-16 16:41:46 -0700721 // compute the opaque region
722 const int32_t layerOrientation = layer->getOrientation();
723 if (s.alpha==255 && !translucent &&
724 ((layerOrientation & Transform::ROT_INVALID) == false)) {
725 // the opaque region is the layer's footprint
726 opaqueRegion = visibleRegion;
727 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 }
729 }
730
Mathias Agopianab028732010-03-16 16:41:46 -0700731 // Clip the covered region to the visible region
732 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
733
734 // Update aboveCoveredLayers for next (lower) layer
735 aboveCoveredLayers.orSelf(visibleRegion);
736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 // subtract the opaque region covered by the layers above us
738 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739
740 // compute this layer's dirty region
741 if (layer->contentDirty) {
742 // we need to invalidate the whole region
743 dirty = visibleRegion;
744 // as well, as the old visible region
745 dirty.orSelf(layer->visibleRegionScreen);
746 layer->contentDirty = false;
747 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700748 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700749 * the exposed region consists of two components:
750 * 1) what's VISIBLE now and was COVERED before
751 * 2) what's EXPOSED now less what was EXPOSED before
752 *
753 * note that (1) is conservative, we start with the whole
754 * visible region but only keep what used to be covered by
755 * something -- which mean it may have been exposed.
756 *
757 * (2) handles areas that were not covered by anything but got
758 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700759 */
Mathias Agopianab028732010-03-16 16:41:46 -0700760 const Region newExposed = visibleRegion - coveredRegion;
761 const Region oldVisibleRegion = layer->visibleRegionScreen;
762 const Region oldCoveredRegion = layer->coveredRegionScreen;
763 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
764 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 }
766 dirty.subtractSelf(aboveOpaqueLayers);
767
768 // accumulate to the screen dirty region
769 dirtyRegion.orSelf(dirty);
770
Mathias Agopianab028732010-03-16 16:41:46 -0700771 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700773
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 // Store the visible region is screen space
775 layer->setVisibleRegion(visibleRegion);
776 layer->setCoveredRegion(coveredRegion);
777
Mathias Agopian97011222009-07-28 10:57:27 -0700778 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 if (layer->isSecure() && !visibleRegion.isEmpty()) {
780 secureFrameBuffer = true;
781 }
782 }
783
Mathias Agopian97011222009-07-28 10:57:27 -0700784 // invalidate the areas where a layer was removed
785 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
786 mDirtyRegionRemovedLayer.clear();
787
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 mSecureFrameBuffer = secureFrameBuffer;
789 opaqueRegion = aboveOpaqueLayers;
790}
791
792
793void SurfaceFlinger::commitTransaction()
794{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800795 if (!mLayersPendingRemoval.isEmpty()) {
796 // Notify removed layers now that they can't be drawn from
797 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
798 mLayersPendingRemoval[i]->onRemoved();
799 }
800 mLayersPendingRemoval.clear();
801 }
802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700804 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700805 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806}
807
808void SurfaceFlinger::handlePageFlip()
809{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800810 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800811 const DisplayHardware& hw = graphicPlane(0).displayHardware();
812 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800814 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
815 const bool visibleRegions = lockPageFlip(currentLayers);
816
817 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 Region opaqueRegion;
819 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700820
821 /*
822 * rebuild the visible layer list
823 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800824 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700825 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700826 mVisibleLayersSortedByZ.setCapacity(count);
827 for (size_t i=0 ; i<count ; i++) {
828 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
829 mVisibleLayersSortedByZ.add(currentLayers[i]);
830 }
831
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 mWormholeRegion = screenRegion.subtract(opaqueRegion);
833 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800834 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835 }
836
837 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700838
839 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 mDirtyRegion.andSelf(screenRegion);
841}
842
Mathias Agopianad456f92011-01-13 17:53:01 -0800843void SurfaceFlinger::invalidateHwcGeometry()
844{
845 mHwWorkListDirty = true;
846}
847
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
849{
850 bool recomputeVisibleRegions = false;
851 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700852 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700854 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 layer->lockPageFlip(recomputeVisibleRegions);
856 }
857 return recomputeVisibleRegions;
858}
859
860void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
861{
862 const GraphicPlane& plane(graphicPlane(0));
863 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800864 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700865 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700867 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868 layer->unlockPageFlip(planeTransform, mDirtyRegion);
869 }
870}
871
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800872void SurfaceFlinger::handleRefresh()
873{
874 bool needInvalidate = false;
875 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
876 const size_t count = currentLayers.size();
877 for (size_t i=0 ; i<count ; i++) {
878 const sp<LayerBase>& layer(currentLayers[i]);
879 if (layer->onPreComposition()) {
880 needInvalidate = true;
881 }
882 }
883 if (needInvalidate) {
884 signalLayerUpdate();
885 }
886}
887
888
Mathias Agopiana350ff92010-08-10 17:14:02 -0700889void SurfaceFlinger::handleWorkList()
890{
891 mHwWorkListDirty = false;
892 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
893 if (hwc.initCheck() == NO_ERROR) {
894 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
895 const size_t count = currentLayers.size();
896 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700897
898 HWComposer::LayerListIterator cur = hwc.begin();
899 const HWComposer::LayerListIterator end = hwc.end();
900 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
901 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700902 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700903 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700904 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700905 }
906 }
907}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700908
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909void SurfaceFlinger::handleRepaint()
910{
Mathias Agopian841cde52012-03-01 15:44:37 -0800911 ATRACE_CALL();
912
Mathias Agopianb8a55602009-06-26 19:06:36 -0700913 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700914 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915
Glenn Kasten99ed2242011-12-15 09:51:17 -0800916 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 debugFlashRegions();
918 }
919
Mathias Agopianb8a55602009-06-26 19:06:36 -0700920 // set the frame buffer
921 const DisplayHardware& hw(graphicPlane(0).displayHardware());
922 glMatrixMode(GL_MODELVIEW);
923 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924
925 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700926 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700927 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
928 // takes a rectangle, we must make sure to update that whole
929 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700930 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700931 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700932 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700933 // We need to redraw the rectangle that will be updated
934 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700935 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700936 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700937 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700938 } else {
939 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800940 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700941 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 }
943 }
944
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700945 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 composeSurfaces(mDirtyRegion);
947
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700948 // update the swap region and clear the dirty region
949 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 mDirtyRegion.clear();
951}
952
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700953void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700955 const DisplayHardware& hw(graphicPlane(0).displayHardware());
956 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700957
958 HWComposer::LayerListIterator cur = hwc.begin();
959 const HWComposer::LayerListIterator end = hwc.end();
960 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700961 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700962 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700963
Mathias Agopianf384cc32011-09-08 18:31:55 -0700964 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
965 size_t count = layers.size();
966
Steve Blocke6f43dd2012-01-06 19:20:56 +0000967 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700968 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
969 hwc.getNumLayers(), count);
970
971 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700972 if (hwc.initCheck() == NO_ERROR) {
973 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
974 }
Mathias Agopian45721772010-08-12 15:03:26 -0700975
976 /*
977 * update the per-frame h/w composer data for each layer
978 * and build the transparent region of the FB
979 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700980 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700981 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700982 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700983 }
984 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000985 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700986}
Mathias Agopian45721772010-08-12 15:03:26 -0700987
Mathias Agopianf384cc32011-09-08 18:31:55 -0700988void SurfaceFlinger::composeSurfaces(const Region& dirty)
989{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700990 const DisplayHardware& hw(graphicPlane(0).displayHardware());
991 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700992 HWComposer::LayerListIterator cur = hwc.begin();
993 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700994
995 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700996 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700997 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700998
Mathias Agopianb9494d52012-04-18 02:28:45 -0700999 if (hwc.getLayerCount(HWC_OVERLAY)) {
1000 // when using overlays, we assume a fully transparent framebuffer
1001 // NOTE: we could reduce how much we need to clear, for instance
1002 // remove where there are opaque FB layers. however, on some
1003 // GPUs doing a "clean slate" glClear might be more efficient.
1004 // We'll revisit later if needed.
1005 glClearColor(0, 0, 0, 0);
1006 glClear(GL_COLOR_BUFFER_BIT);
1007 } else {
1008 // screen is already cleared here
1009 if (!mWormholeRegion.isEmpty()) {
1010 // can happen with SurfaceView
1011 drawWormhole();
1012 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001013 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001014
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001015 /*
1016 * and then, render the layers targeted at the framebuffer
1017 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001018
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001019 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1020 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001021 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001022 const sp<LayerBase>& layer(layers[i]);
1023 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1024 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -07001025 if (cur->getCompositionType() == HWC_OVERLAY) {
1026 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001027 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001028 // never clear the very first layer since we're
1029 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001030 layer->clearWithOpenGL(clip);
1031 }
1032 continue;
1033 }
1034 // render the layer
1035 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001036 }
1037 }
1038 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039}
1040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041void SurfaceFlinger::debugFlashRegions()
1042{
Mathias Agopian0a917752010-06-14 21:20:00 -07001043 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1044 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001045 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001046 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001047 return;
1048 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001049
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001050 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001051 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1052 mDirtyRegion.bounds() : hw.bounds());
1053 composeSurfaces(repaint);
1054 }
1055
Mathias Agopianc492e672011-10-18 14:49:27 -07001056 glDisable(GL_TEXTURE_EXTERNAL_OES);
1057 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059
Mathias Agopian0926f502009-05-04 14:17:04 -07001060 static int toggle = 0;
1061 toggle = 1 - toggle;
1062 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001063 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001064 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001065 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067
Mathias Agopian20f68782009-05-11 00:03:41 -07001068 Region::const_iterator it = mDirtyRegion.begin();
1069 Region::const_iterator const end = mDirtyRegion.end();
1070 while (it != end) {
1071 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001073 { r.left, height - r.top },
1074 { r.left, height - r.bottom },
1075 { r.right, height - r.bottom },
1076 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 };
1078 glVertexPointer(2, GL_FLOAT, 0, vertices);
1079 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1080 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001081
Mathias Agopian0656a682011-09-20 17:22:44 -07001082 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083
1084 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001085 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086}
1087
1088void SurfaceFlinger::drawWormhole() const
1089{
1090 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1091 if (region.isEmpty())
1092 return;
1093
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001094 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001095 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001096 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001097 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001098
1099 GLfloat vertices[4][2];
1100 glVertexPointer(2, GL_FLOAT, 0, vertices);
1101 Region::const_iterator it = region.begin();
1102 Region::const_iterator const end = region.end();
1103 while (it != end) {
1104 const Rect& r = *it++;
1105 vertices[0][0] = r.left;
1106 vertices[0][1] = r.top;
1107 vertices[1][0] = r.right;
1108 vertices[1][1] = r.top;
1109 vertices[2][0] = r.right;
1110 vertices[2][1] = r.bottom;
1111 vertices[3][0] = r.left;
1112 vertices[3][1] = r.bottom;
1113 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1114 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115}
1116
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001117status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118{
1119 Mutex::Autolock _l(mStateLock);
1120 addLayer_l(layer);
1121 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1122 return NO_ERROR;
1123}
1124
Mathias Agopian96f08192010-06-02 23:28:45 -07001125status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1126{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001127 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001128 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1129}
1130
1131ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1132 const sp<LayerBaseClient>& lbc)
1133{
Mathias Agopian96f08192010-06-02 23:28:45 -07001134 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001135 size_t name = client->attachLayer(lbc);
1136
1137 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001138
1139 // add this layer to the current state list
1140 addLayer_l(lbc);
1141
Mathias Agopian4f113742011-05-03 16:21:41 -07001142 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001143}
1144
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001145status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146{
1147 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001148 status_t err = purgatorizeLayer_l(layer);
1149 if (err == NO_ERROR)
1150 setTransactionFlags(eTransactionNeeded);
1151 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152}
1153
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001154status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001156 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1157 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001158 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1161 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001162 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 return NO_ERROR;
1164 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001165 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166}
1167
Mathias Agopian9a112062009-04-17 19:36:26 -07001168status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1169{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001170 // First add the layer to the purgatory list, which makes sure it won't
1171 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001172 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001173 if (err >= 0) {
1174 mLayerPurgatory.add(layerBase);
1175 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001176
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001177 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001178
Mathias Agopian3d579642009-06-04 18:46:21 -07001179 // it's possible that we don't find a layer, because it might
1180 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001181 // from the user because there is a race between Client::destroySurface(),
1182 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001183 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1184}
1185
Mathias Agopian96f08192010-06-02 23:28:45 -07001186status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187{
Mathias Agopian96f08192010-06-02 23:28:45 -07001188 layer->forceVisibilityTransaction();
1189 setTransactionFlags(eTraversalNeeded);
1190 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191}
1192
Mathias Agopiandea20b12011-05-03 17:04:02 -07001193uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1194{
1195 return android_atomic_release_load(&mTransactionFlags);
1196}
1197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1199{
1200 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1201}
1202
Mathias Agopianbb641242010-05-18 17:06:55 -07001203uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204{
1205 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1206 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001207 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 }
1209 return old;
1210}
1211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001213void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001214 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001215 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001216
Jamie Gennis28378392011-10-12 17:39:00 -07001217 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001218 if (mCurrentState.orientation != orientation) {
1219 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1220 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001221 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001222 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001223 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001224 orientation);
1225 }
1226 }
1227
Mathias Agopian698c0872011-06-28 19:09:31 -07001228 const size_t count = state.size();
1229 for (size_t i=0 ; i<count ; i++) {
1230 const ComposerState& s(state[i]);
1231 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001232 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001233 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001234
Mathias Agopian386aa982011-11-07 21:58:03 -08001235 if (transactionFlags) {
1236 // this triggers the transaction
1237 setTransactionFlags(transactionFlags);
1238
1239 // if this is a synchronous transaction, wait for it to take effect
1240 // before returning.
1241 if (flags & eSynchronous) {
1242 mTransationPending = true;
1243 }
1244 while (mTransationPending) {
1245 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1246 if (CC_UNLIKELY(err != NO_ERROR)) {
1247 // just in case something goes wrong in SF, return to the
1248 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001249 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001250 mTransationPending = false;
1251 break;
1252 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001253 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 }
1255}
1256
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001257sp<ISurface> SurfaceFlinger::createSurface(
1258 ISurfaceComposerClient::surface_data_t* params,
1259 const String8& name,
1260 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1262 uint32_t flags)
1263{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001264 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001265 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001266
1267 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001268 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001269 int(w), int(h));
1270 return surfaceHandle;
1271 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001272
Mathias Agopianff615cc2012-02-24 14:58:36 -08001273 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001274 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 switch (flags & eFXSurfaceMask) {
1276 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001277 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1278 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 break;
1280 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001281 // for now we treat Blur as Dim, until we can implement it
1282 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001284 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001286 case eFXSurfaceScreenshot:
1287 layer = createScreenshotSurface(client, d, w, h, flags);
1288 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 }
1290
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001291 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001293 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001294 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001297 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001299 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300 if (normalLayer != 0) {
1301 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001302 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001303 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001304 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001305
Mathias Agopian96f08192010-06-02 23:28:45 -07001306 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 }
1308
1309 return surfaceHandle;
1310}
1311
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001312sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001313 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001314 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001315 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316{
1317 // initialize the surfaces
1318 switch (format) { // TODO: take h/w into account
1319 case PIXEL_FORMAT_TRANSPARENT:
1320 case PIXEL_FORMAT_TRANSLUCENT:
1321 format = PIXEL_FORMAT_RGBA_8888;
1322 break;
1323 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001324#ifdef NO_RGBX_8888
1325 format = PIXEL_FORMAT_RGB_565;
1326#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001327 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001328#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 break;
1330 }
1331
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001332#ifdef NO_RGBX_8888
1333 if (format == PIXEL_FORMAT_RGBX_8888)
1334 format = PIXEL_FORMAT_RGBA_8888;
1335#endif
1336
Mathias Agopian96f08192010-06-02 23:28:45 -07001337 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001338 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001339 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001340 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001341 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 }
1343 return layer;
1344}
1345
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001346sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001347 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001348 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349{
Mathias Agopian96f08192010-06-02 23:28:45 -07001350 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001351 return layer;
1352}
1353
1354sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1355 const sp<Client>& client, DisplayID display,
1356 uint32_t w, uint32_t h, uint32_t flags)
1357{
1358 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 return layer;
1360}
1361
Mathias Agopian96f08192010-06-02 23:28:45 -07001362status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363{
Mathias Agopian9a112062009-04-17 19:36:26 -07001364 /*
1365 * called by the window manager, when a surface should be marked for
1366 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001367 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001368 * The surface is removed from the current and drawing lists, but placed
1369 * in the purgatory queue, so it's not destroyed right-away (we need
1370 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001371 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001372
Mathias Agopian48d819a2009-09-10 19:41:18 -07001373 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001374 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001375 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001376
Mathias Agopian48d819a2009-09-10 19:41:18 -07001377 if (layer != 0) {
1378 err = purgatorizeLayer_l(layer);
1379 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001380 setTransactionFlags(eTransactionNeeded);
1381 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001382 }
1383 return err;
1384}
1385
Mathias Agopianca4d3602011-05-19 15:38:14 -07001386status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001387{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001388 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001389 status_t err = NO_ERROR;
1390 sp<LayerBaseClient> l(layer.promote());
1391 if (l != NULL) {
1392 Mutex::Autolock _l(mStateLock);
1393 err = removeLayer_l(l);
1394 if (err == NAME_NOT_FOUND) {
1395 // The surface wasn't in the current list, which means it was
1396 // removed already, which means it is in the purgatory,
1397 // and need to be removed from there.
1398 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001399 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001400 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001401 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001402 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001403 "error removing layer=%p (%s)", l.get(), strerror(-err));
1404 }
1405 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406}
1407
Mathias Agopian698c0872011-06-28 19:09:31 -07001408uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001409 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001410 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001413 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1414 if (layer != 0) {
1415 const uint32_t what = s.what;
1416 if (what & ePositionChanged) {
1417 if (layer->setPosition(s.x, s.y))
1418 flags |= eTraversalNeeded;
1419 }
1420 if (what & eLayerChanged) {
1421 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1422 if (layer->setLayer(s.z)) {
1423 mCurrentState.layersSortedByZ.removeAt(idx);
1424 mCurrentState.layersSortedByZ.add(layer);
1425 // we need traversal (state changed)
1426 // AND transaction (list changed)
1427 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 }
1429 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001430 if (what & eSizeChanged) {
1431 if (layer->setSize(s.w, s.h)) {
1432 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001433 }
1434 }
1435 if (what & eAlphaChanged) {
1436 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1437 flags |= eTraversalNeeded;
1438 }
1439 if (what & eMatrixChanged) {
1440 if (layer->setMatrix(s.matrix))
1441 flags |= eTraversalNeeded;
1442 }
1443 if (what & eTransparentRegionChanged) {
1444 if (layer->setTransparentRegionHint(s.transparentRegion))
1445 flags |= eTraversalNeeded;
1446 }
1447 if (what & eVisibilityChanged) {
1448 if (layer->setFlags(s.flags, s.mask))
1449 flags |= eTraversalNeeded;
1450 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001451 if (what & eCropChanged) {
1452 if (layer->setCrop(s.crop))
1453 flags |= eTraversalNeeded;
1454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001456 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457}
1458
Mathias Agopianb60314a2012-04-10 22:09:54 -07001459// ---------------------------------------------------------------------------
1460
1461void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001462 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001463 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1464 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001465 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001466 // this is a temporary work-around, eventually this should be called
1467 // by the power-manager
1468 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001469 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001470 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471}
1472
Mathias Agopianb60314a2012-04-10 22:09:54 -07001473void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001474 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001475 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1476 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001477 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001478 hw.releaseScreen();
1479 // from this point on, SF will stop drawing
1480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481}
1482
Colin Cross8e533062012-06-07 13:17:52 -07001483void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001484 class MessageScreenAcquired : public MessageBase {
1485 SurfaceFlinger* flinger;
1486 public:
1487 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1488 virtual bool handler() {
1489 flinger->onScreenAcquired();
1490 return true;
1491 }
1492 };
1493 sp<MessageBase> msg = new MessageScreenAcquired(this);
1494 postMessageSync(msg);
1495}
1496
Colin Cross8e533062012-06-07 13:17:52 -07001497void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001498 class MessageScreenReleased : public MessageBase {
1499 SurfaceFlinger* flinger;
1500 public:
1501 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1502 virtual bool handler() {
1503 flinger->onScreenReleased();
1504 return true;
1505 }
1506 };
1507 sp<MessageBase> msg = new MessageScreenReleased(this);
1508 postMessageSync(msg);
1509}
1510
1511// ---------------------------------------------------------------------------
1512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1514{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001515 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 char buffer[SIZE];
1517 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001518
1519 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 snprintf(buffer, SIZE, "Permission Denial: "
1521 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1522 IPCThreadState::self()->getCallingPid(),
1523 IPCThreadState::self()->getCallingUid());
1524 result.append(buffer);
1525 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001526 // Try to get the main lock, but don't insist if we can't
1527 // (this would indicate SF is stuck, but we want to be able to
1528 // print something in dumpsys).
1529 int retry = 3;
1530 while (mStateLock.tryLock()<0 && --retry>=0) {
1531 usleep(1000000);
1532 }
1533 const bool locked(retry >= 0);
1534 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001535 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001536 "SurfaceFlinger appears to be unresponsive, "
1537 "dumping anyways (no locks held)\n");
1538 result.append(buffer);
1539 }
1540
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001541 bool dumpAll = true;
1542 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001543 size_t numArgs = args.size();
1544 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001545 if ((index < numArgs) &&
1546 (args[index] == String16("--list"))) {
1547 index++;
1548 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001549 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001550 }
1551
1552 if ((index < numArgs) &&
1553 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001554 index++;
1555 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001556 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001557 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001558
1559 if ((index < numArgs) &&
1560 (args[index] == String16("--latency-clear"))) {
1561 index++;
1562 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001563 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001566
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001567 if (dumpAll) {
1568 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001569 }
1570
Mathias Agopian9795c422009-08-26 16:36:26 -07001571 if (locked) {
1572 mStateLock.unlock();
1573 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 }
1575 write(fd, result.string(), result.size());
1576 return NO_ERROR;
1577}
1578
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001579void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1580 String8& result, char* buffer, size_t SIZE) const
1581{
1582 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1583 const size_t count = currentLayers.size();
1584 for (size_t i=0 ; i<count ; i++) {
1585 const sp<LayerBase>& layer(currentLayers[i]);
1586 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1587 result.append(buffer);
1588 }
1589}
1590
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001591void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1592 String8& result, char* buffer, size_t SIZE) const
1593{
1594 String8 name;
1595 if (index < args.size()) {
1596 name = String8(args[index]);
1597 index++;
1598 }
1599
1600 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1601 const size_t count = currentLayers.size();
1602 for (size_t i=0 ; i<count ; i++) {
1603 const sp<LayerBase>& layer(currentLayers[i]);
1604 if (name.isEmpty()) {
1605 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1606 result.append(buffer);
1607 }
1608 if (name.isEmpty() || (name == layer->getName())) {
1609 layer->dumpStats(result, buffer, SIZE);
1610 }
1611 }
1612}
1613
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001614void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1615 String8& result, char* buffer, size_t SIZE) const
1616{
1617 String8 name;
1618 if (index < args.size()) {
1619 name = String8(args[index]);
1620 index++;
1621 }
1622
1623 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1624 const size_t count = currentLayers.size();
1625 for (size_t i=0 ; i<count ; i++) {
1626 const sp<LayerBase>& layer(currentLayers[i]);
1627 if (name.isEmpty() || (name == layer->getName())) {
1628 layer->clearStats();
1629 }
1630 }
1631}
1632
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001633void SurfaceFlinger::dumpAllLocked(
1634 String8& result, char* buffer, size_t SIZE) const
1635{
1636 // figure out if we're stuck somewhere
1637 const nsecs_t now = systemTime();
1638 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1639 const nsecs_t inTransaction(mDebugInTransaction);
1640 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1641 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1642
1643 /*
1644 * Dump the visible layer list
1645 */
1646 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1647 const size_t count = currentLayers.size();
1648 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1649 result.append(buffer);
1650 for (size_t i=0 ; i<count ; i++) {
1651 const sp<LayerBase>& layer(currentLayers[i]);
1652 layer->dump(result, buffer, SIZE);
1653 }
1654
1655 /*
1656 * Dump the layers in the purgatory
1657 */
1658
1659 const size_t purgatorySize = mLayerPurgatory.size();
1660 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1661 result.append(buffer);
1662 for (size_t i=0 ; i<purgatorySize ; i++) {
1663 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1664 layer->shortDump(result, buffer, SIZE);
1665 }
1666
1667 /*
1668 * Dump SurfaceFlinger global state
1669 */
1670
1671 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1672 result.append(buffer);
1673
1674 const GLExtensions& extensions(GLExtensions::getInstance());
1675 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1676 extensions.getVendor(),
1677 extensions.getRenderer(),
1678 extensions.getVersion());
1679 result.append(buffer);
1680
1681 snprintf(buffer, SIZE, "EGL : %s\n",
1682 eglQueryString(graphicPlane(0).getEGLDisplay(),
1683 EGL_VERSION_HW_ANDROID));
1684 result.append(buffer);
1685
1686 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1687 result.append(buffer);
1688
1689 mWormholeRegion.dump(result, "WormholeRegion");
1690 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1691 snprintf(buffer, SIZE,
1692 " orientation=%d, canDraw=%d\n",
1693 mCurrentState.orientation, hw.canDraw());
1694 result.append(buffer);
1695 snprintf(buffer, SIZE,
1696 " last eglSwapBuffers() time: %f us\n"
1697 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001698 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001699 " refresh-rate : %f fps\n"
1700 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001701 " y-dpi : %f\n"
1702 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001703 mLastSwapBufferTime/1000.0,
1704 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001705 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001706 hw.getRefreshRate(),
1707 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001708 hw.getDpiY(),
1709 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001710 result.append(buffer);
1711
1712 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1713 inSwapBuffersDuration/1000.0);
1714 result.append(buffer);
1715
1716 snprintf(buffer, SIZE, " transaction time: %f us\n",
1717 inTransactionDuration/1000.0);
1718 result.append(buffer);
1719
1720 /*
1721 * VSYNC state
1722 */
1723 mEventThread->dump(result, buffer, SIZE);
1724
1725 /*
1726 * Dump HWComposer state
1727 */
1728 HWComposer& hwc(hw.getHwComposer());
1729 snprintf(buffer, SIZE, "h/w composer state:\n");
1730 result.append(buffer);
1731 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1732 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1733 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1734 result.append(buffer);
1735 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1736
1737 /*
1738 * Dump gralloc state
1739 */
1740 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1741 alloc.dump(result);
1742 hw.dump(result);
1743}
1744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745status_t SurfaceFlinger::onTransact(
1746 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1747{
1748 switch (code) {
1749 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001750 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001753 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001754 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001755 case BLANK:
1756 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 {
1758 // codes that require permission check
1759 IPCThreadState* ipc = IPCThreadState::self();
1760 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001761 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001762 if ((uid != AID_GRAPHICS) &&
1763 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001764 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001765 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1766 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001768 break;
1769 }
1770 case CAPTURE_SCREEN:
1771 {
1772 // codes that require permission check
1773 IPCThreadState* ipc = IPCThreadState::self();
1774 const int pid = ipc->getCallingPid();
1775 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001776 if ((uid != AID_GRAPHICS) &&
1777 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001778 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001779 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1780 return PERMISSION_DENIED;
1781 }
1782 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 }
1784 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001785
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1787 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001788 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001789 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001790 IPCThreadState* ipc = IPCThreadState::self();
1791 const int pid = ipc->getCallingPid();
1792 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001793 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001794 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001795 return PERMISSION_DENIED;
1796 }
1797 int n;
1798 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001799 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001800 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 return NO_ERROR;
1802 case 1002: // SHOW_UPDATES
1803 n = data.readInt32();
1804 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001805 invalidateHwcGeometry();
1806 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001808 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001809 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001810 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001812 case 1005:{ // force transaction
1813 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1814 return NO_ERROR;
1815 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001816 case 1006:{ // send empty update
1817 signalRefresh();
1818 return NO_ERROR;
1819 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001820 case 1008: // toggle use of hw composer
1821 n = data.readInt32();
1822 mDebugDisableHWC = n ? 1 : 0;
1823 invalidateHwcGeometry();
1824 repaintEverything();
1825 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001826 case 1009: // toggle use of transform hint
1827 n = data.readInt32();
1828 mDebugDisableTransformHint = n ? 1 : 0;
1829 invalidateHwcGeometry();
1830 repaintEverything();
1831 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001833 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834 reply->writeInt32(0);
1835 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001836 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001837 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001838 return NO_ERROR;
1839 case 1013: {
1840 Mutex::Autolock _l(mStateLock);
1841 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1842 reply->writeInt32(hw.getPageFlipCount());
1843 }
1844 return NO_ERROR;
1845 }
1846 }
1847 return err;
1848}
1849
Mathias Agopian53331da2011-08-22 21:44:41 -07001850void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001851 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001852 const Rect bounds(hw.getBounds());
1853 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001854 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001855}
1856
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001857void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1858 Mutex::Autolock _l(mInvalidateLock);
1859 mInvalidateRegion = reg;
1860}
1861
1862Region SurfaceFlinger::getAndClearInvalidateRegion() {
1863 Mutex::Autolock _l(mInvalidateLock);
1864 Region reg(mInvalidateRegion);
1865 mInvalidateRegion.clear();
1866 return reg;
1867}
1868
Mathias Agopian59119e62010-10-11 12:37:43 -07001869// ---------------------------------------------------------------------------
1870
Mathias Agopian118d0242011-10-13 16:02:48 -07001871status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1872 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1873{
1874 Mutex::Autolock _l(mStateLock);
1875 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1876}
1877
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001878status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1879 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001880{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001881 ATRACE_CALL();
1882
Mathias Agopian59119e62010-10-11 12:37:43 -07001883 if (!GLExtensions::getInstance().haveFramebufferObject())
1884 return INVALID_OPERATION;
1885
1886 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001887 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001888 const uint32_t hw_w = hw.getWidth();
1889 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001890 GLfloat u = 1;
1891 GLfloat v = 1;
1892
1893 // make sure to clear all GL error flags
1894 while ( glGetError() != GL_NO_ERROR ) ;
1895
1896 // create a FBO
1897 GLuint name, tname;
1898 glGenTextures(1, &tname);
1899 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001900 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1901 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001902 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1903 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001904 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001905 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001906 GLint tw = (2 << (31 - clz(hw_w)));
1907 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001908 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1909 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001910 u = GLfloat(hw_w) / tw;
1911 v = GLfloat(hw_h) / th;
1912 }
1913 glGenFramebuffersOES(1, &name);
1914 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001915 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1916 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001917
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001918 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001919 glDisable(GL_TEXTURE_EXTERNAL_OES);
1920 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001921 glClearColor(0,0,0,1);
1922 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001923 glMatrixMode(GL_MODELVIEW);
1924 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001925 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1926 const size_t count = layers.size();
1927 for (size_t i=0 ; i<count ; ++i) {
1928 const sp<LayerBase>& layer(layers[i]);
1929 layer->drawForSreenShot();
1930 }
1931
Mathias Agopian118d0242011-10-13 16:02:48 -07001932 hw.compositionComplete();
1933
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001934 // back to main framebuffer
1935 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001936 glDeleteFramebuffersOES(1, &name);
1937
1938 *textureName = tname;
1939 *uOut = u;
1940 *vOut = v;
1941 return NO_ERROR;
1942}
1943
1944// ---------------------------------------------------------------------------
1945
Mathias Agopianed9807b2012-05-18 14:18:08 -07001946class VSyncWaiter {
1947 DisplayEventReceiver::Event buffer[4];
1948 sp<Looper> looper;
1949 sp<IDisplayEventConnection> events;
1950 sp<BitTube> eventTube;
1951public:
1952 VSyncWaiter(const sp<EventThread>& eventThread) {
1953 looper = new Looper(true);
1954 events = eventThread->createEventConnection();
1955 eventTube = events->getDataChannel();
1956 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1957 events->requestNextVsync();
1958 }
1959
1960 void wait() {
1961 ssize_t n;
1962
1963 looper->pollOnce(-1);
1964 // we don't handle any errors here, it doesn't matter
1965 // and we don't want to take the risk to get stuck.
1966
1967 // drain the events...
1968 while ((n = DisplayEventReceiver::getEvents(
1969 eventTube, buffer, 4)) > 0) ;
1970
1971 events->requestNextVsync();
1972 }
1973};
1974
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001975status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1976{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977 // get screen geometry
1978 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1979 const uint32_t hw_w = hw.getWidth();
1980 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001981 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001982
1983 GLfloat u, v;
1984 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001985 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001986 if (result != NO_ERROR) {
1987 return result;
1988 }
1989
1990 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001991 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001992 glBindTexture(GL_TEXTURE_2D, tname);
1993 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1994 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1995 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001996 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1997 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001998 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1999 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2000 glVertexPointer(2, GL_FLOAT, 0, vtx);
2001
Mathias Agopianffcf4652011-07-07 17:30:31 -07002002 /*
2003 * Texture coordinate mapping
2004 *
2005 * u
2006 * 1 +----------+---+
2007 * | | | | image is inverted
2008 * | V | | w.r.t. the texture
2009 * 1-v +----------+ | coordinates
2010 * | |
2011 * | |
2012 * | |
2013 * 0 +--------------+
2014 * 0 1
2015 *
2016 */
2017
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002018 class s_curve_interpolator {
2019 const float nbFrames, s, v;
2020 public:
2021 s_curve_interpolator(int nbFrames, float s)
2022 : nbFrames(1.0f / (nbFrames-1)), s(s),
2023 v(1.0f + expf(-s + 0.5f*s)) {
2024 }
2025 float operator()(int f) {
2026 const float x = f * nbFrames;
2027 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2028 }
2029 };
2030
2031 class v_stretch {
2032 const GLfloat hw_w, hw_h;
2033 public:
2034 v_stretch(uint32_t hw_w, uint32_t hw_h)
2035 : hw_w(hw_w), hw_h(hw_h) {
2036 }
2037 void operator()(GLfloat* vtx, float v) {
2038 const GLfloat w = hw_w + (hw_w * v);
2039 const GLfloat h = hw_h - (hw_h * v);
2040 const GLfloat x = (hw_w - w) * 0.5f;
2041 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002042 vtx[0] = x; vtx[1] = y;
2043 vtx[2] = x; vtx[3] = y + h;
2044 vtx[4] = x + w; vtx[5] = y + h;
2045 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002046 }
2047 };
2048
2049 class h_stretch {
2050 const GLfloat hw_w, hw_h;
2051 public:
2052 h_stretch(uint32_t hw_w, uint32_t hw_h)
2053 : hw_w(hw_w), hw_h(hw_h) {
2054 }
2055 void operator()(GLfloat* vtx, float v) {
2056 const GLfloat w = hw_w - (hw_w * v);
2057 const GLfloat h = 1.0f;
2058 const GLfloat x = (hw_w - w) * 0.5f;
2059 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002060 vtx[0] = x; vtx[1] = y;
2061 vtx[2] = x; vtx[3] = y + h;
2062 vtx[4] = x + w; vtx[5] = y + h;
2063 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 }
2065 };
2066
Mathias Agopianed9807b2012-05-18 14:18:08 -07002067 VSyncWaiter vsync(mEventThread);
2068
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002069 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002070 char value[PROPERTY_VALUE_MAX];
2071 property_get("debug.sf.electron_frames", value, "24");
2072 int nbFrames = (atoi(value) + 1) >> 1;
2073 if (nbFrames <= 0) // just in case
2074 nbFrames = 24;
2075
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002076 s_curve_interpolator itr(nbFrames, 7.5f);
2077 s_curve_interpolator itg(nbFrames, 8.0f);
2078 s_curve_interpolator itb(nbFrames, 8.5f);
2079
2080 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002081
2082 glMatrixMode(GL_TEXTURE);
2083 glLoadIdentity();
2084 glMatrixMode(GL_MODELVIEW);
2085 glLoadIdentity();
2086
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002087 glEnable(GL_BLEND);
2088 glBlendFunc(GL_ONE, GL_ONE);
2089 for (int i=0 ; i<nbFrames ; i++) {
2090 float x, y, w, h;
2091 const float vr = itr(i);
2092 const float vg = itg(i);
2093 const float vb = itb(i);
2094
Mathias Agopianed9807b2012-05-18 14:18:08 -07002095 // wait for vsync
2096 vsync.wait();
2097
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098 // clear screen
2099 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002100 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002101 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002102
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002103 // draw the red plane
2104 vverts(vtx, vr);
2105 glColorMask(1,0,0,1);
2106 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002107
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002108 // draw the green plane
2109 vverts(vtx, vg);
2110 glColorMask(0,1,0,1);
2111 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002112
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002113 // draw the blue plane
2114 vverts(vtx, vb);
2115 glColorMask(0,0,1,1);
2116 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002117
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002119 glDisable(GL_TEXTURE_2D);
2120 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002121 glColor4f(vg, vg, vg, 1);
2122 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2123 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002124 }
2125
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002126 h_stretch hverts(hw_w, hw_h);
2127 glDisable(GL_BLEND);
2128 glDisable(GL_TEXTURE_2D);
2129 glColorMask(1,1,1,1);
2130 for (int i=0 ; i<nbFrames ; i++) {
2131 const float v = itg(i);
2132 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002133
2134 // wait for vsync
2135 vsync.wait();
2136
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002137 glClear(GL_COLOR_BUFFER_BIT);
2138 glColor4f(1-v, 1-v, 1-v, 1);
2139 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2140 hw.flip(screenBounds);
2141 }
2142
2143 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002144 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2145 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002146 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002147 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002148 return NO_ERROR;
2149}
2150
2151status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2152{
2153 status_t result = PERMISSION_DENIED;
2154
2155 if (!GLExtensions::getInstance().haveFramebufferObject())
2156 return INVALID_OPERATION;
2157
2158
2159 // get screen geometry
2160 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2161 const uint32_t hw_w = hw.getWidth();
2162 const uint32_t hw_h = hw.getHeight();
2163 const Region screenBounds(hw.bounds());
2164
2165 GLfloat u, v;
2166 GLuint tname;
2167 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2168 if (result != NO_ERROR) {
2169 return result;
2170 }
2171
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002172 GLfloat vtx[8];
2173 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002174 glBindTexture(GL_TEXTURE_2D, tname);
2175 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2176 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2177 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002178 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2179 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002180 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2181 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2182 glVertexPointer(2, GL_FLOAT, 0, vtx);
2183
2184 class s_curve_interpolator {
2185 const float nbFrames, s, v;
2186 public:
2187 s_curve_interpolator(int nbFrames, float s)
2188 : nbFrames(1.0f / (nbFrames-1)), s(s),
2189 v(1.0f + expf(-s + 0.5f*s)) {
2190 }
2191 float operator()(int f) {
2192 const float x = f * nbFrames;
2193 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2194 }
2195 };
2196
2197 class v_stretch {
2198 const GLfloat hw_w, hw_h;
2199 public:
2200 v_stretch(uint32_t hw_w, uint32_t hw_h)
2201 : hw_w(hw_w), hw_h(hw_h) {
2202 }
2203 void operator()(GLfloat* vtx, float v) {
2204 const GLfloat w = hw_w + (hw_w * v);
2205 const GLfloat h = hw_h - (hw_h * v);
2206 const GLfloat x = (hw_w - w) * 0.5f;
2207 const GLfloat y = (hw_h - h) * 0.5f;
2208 vtx[0] = x; vtx[1] = y;
2209 vtx[2] = x; vtx[3] = y + h;
2210 vtx[4] = x + w; vtx[5] = y + h;
2211 vtx[6] = x + w; vtx[7] = y;
2212 }
2213 };
2214
2215 class h_stretch {
2216 const GLfloat hw_w, hw_h;
2217 public:
2218 h_stretch(uint32_t hw_w, uint32_t hw_h)
2219 : hw_w(hw_w), hw_h(hw_h) {
2220 }
2221 void operator()(GLfloat* vtx, float v) {
2222 const GLfloat w = hw_w - (hw_w * v);
2223 const GLfloat h = 1.0f;
2224 const GLfloat x = (hw_w - w) * 0.5f;
2225 const GLfloat y = (hw_h - h) * 0.5f;
2226 vtx[0] = x; vtx[1] = y;
2227 vtx[2] = x; vtx[3] = y + h;
2228 vtx[4] = x + w; vtx[5] = y + h;
2229 vtx[6] = x + w; vtx[7] = y;
2230 }
2231 };
2232
Mathias Agopianed9807b2012-05-18 14:18:08 -07002233 VSyncWaiter vsync(mEventThread);
2234
Mathias Agopiana6546e52010-10-14 12:33:07 -07002235 // the full animation is 12 frames
2236 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237 s_curve_interpolator itr(nbFrames, 7.5f);
2238 s_curve_interpolator itg(nbFrames, 8.0f);
2239 s_curve_interpolator itb(nbFrames, 8.5f);
2240
2241 h_stretch hverts(hw_w, hw_h);
2242 glDisable(GL_BLEND);
2243 glDisable(GL_TEXTURE_2D);
2244 glColorMask(1,1,1,1);
2245 for (int i=nbFrames-1 ; i>=0 ; i--) {
2246 const float v = itg(i);
2247 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002248
2249 // wait for vsync
2250 vsync.wait();
2251
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002252 glClear(GL_COLOR_BUFFER_BIT);
2253 glColor4f(1-v, 1-v, 1-v, 1);
2254 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2255 hw.flip(screenBounds);
2256 }
2257
Mathias Agopiana6546e52010-10-14 12:33:07 -07002258 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 v_stretch vverts(hw_w, hw_h);
2260 glEnable(GL_BLEND);
2261 glBlendFunc(GL_ONE, GL_ONE);
2262 for (int i=nbFrames-1 ; i>=0 ; i--) {
2263 float x, y, w, h;
2264 const float vr = itr(i);
2265 const float vg = itg(i);
2266 const float vb = itb(i);
2267
Mathias Agopianed9807b2012-05-18 14:18:08 -07002268 // wait for vsync
2269 vsync.wait();
2270
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002271 // clear screen
2272 glColorMask(1,1,1,1);
2273 glClear(GL_COLOR_BUFFER_BIT);
2274 glEnable(GL_TEXTURE_2D);
2275
2276 // draw the red plane
2277 vverts(vtx, vr);
2278 glColorMask(1,0,0,1);
2279 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2280
2281 // draw the green plane
2282 vverts(vtx, vg);
2283 glColorMask(0,1,0,1);
2284 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2285
2286 // draw the blue plane
2287 vverts(vtx, vb);
2288 glColorMask(0,0,1,1);
2289 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2290
2291 hw.flip(screenBounds);
2292 }
2293
2294 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002295 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002296 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002297 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002298 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002299
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002300 return NO_ERROR;
2301}
2302
2303// ---------------------------------------------------------------------------
2304
Mathias Agopianabd671a2010-10-14 14:54:06 -07002305status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002306{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002307 ATRACE_CALL();
2308
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002309 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2310 if (!hw.canDraw()) {
2311 // we're already off
2312 return NO_ERROR;
2313 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002314
2315 // turn off hwc while we're doing the animation
2316 hw.getHwComposer().disable();
2317 // and make sure to turn it back on (if needed) next time we compose
2318 invalidateHwcGeometry();
2319
Mathias Agopianabd671a2010-10-14 14:54:06 -07002320 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2321 electronBeamOffAnimationImplLocked();
2322 }
2323
2324 // always clear the whole screen at the end of the animation
2325 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002326 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002327 hw.flip( Region(hw.bounds()) );
2328
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002329 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002330}
2331
2332status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2333{
Mathias Agopian59119e62010-10-11 12:37:43 -07002334 class MessageTurnElectronBeamOff : public MessageBase {
2335 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002336 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002337 status_t result;
2338 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002339 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2340 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002341 }
2342 status_t getResult() const {
2343 return result;
2344 }
2345 virtual bool handler() {
2346 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002347 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002348 return true;
2349 }
2350 };
2351
Mathias Agopianabd671a2010-10-14 14:54:06 -07002352 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002353 status_t res = postMessageSync(msg);
2354 if (res == NO_ERROR) {
2355 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002356
2357 // work-around: when the power-manager calls us we activate the
2358 // animation. eventually, the "on" animation will be called
2359 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002360 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002361 }
2362 return res;
2363}
2364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002365// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002366
Mathias Agopianabd671a2010-10-14 14:54:06 -07002367status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002368{
2369 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2370 if (hw.canDraw()) {
2371 // we're already on
2372 return NO_ERROR;
2373 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002374 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2375 electronBeamOnAnimationImplLocked();
2376 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002377
2378 // make sure to redraw the whole screen when the animation is done
2379 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002380 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002381
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002382 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002383}
2384
2385status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2386{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002387 class MessageTurnElectronBeamOn : public MessageBase {
2388 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002389 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002390 status_t result;
2391 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002392 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2393 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002394 }
2395 status_t getResult() const {
2396 return result;
2397 }
2398 virtual bool handler() {
2399 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002400 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002401 return true;
2402 }
2403 };
2404
Mathias Agopianabd671a2010-10-14 14:54:06 -07002405 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002406 return NO_ERROR;
2407}
2408
2409// ---------------------------------------------------------------------------
2410
Mathias Agopian74c40c02010-09-29 13:02:36 -07002411status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2412 sp<IMemoryHeap>* heap,
2413 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002414 uint32_t sw, uint32_t sh,
2415 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002416{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002417 ATRACE_CALL();
2418
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 status_t result = PERMISSION_DENIED;
2420
2421 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002422 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 return BAD_VALUE;
2424
2425 if (!GLExtensions::getInstance().haveFramebufferObject())
2426 return INVALID_OPERATION;
2427
2428 // get screen geometry
2429 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2430 const uint32_t hw_w = hw.getWidth();
2431 const uint32_t hw_h = hw.getHeight();
2432
2433 if ((sw > hw_w) || (sh > hw_h))
2434 return BAD_VALUE;
2435
2436 sw = (!sw) ? hw_w : sw;
2437 sh = (!sh) ? hw_h : sh;
2438 const size_t size = sw * sh * 4;
2439
Steve Block9d453682011-12-20 16:23:08 +00002440 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002441 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002442
Mathias Agopian74c40c02010-09-29 13:02:36 -07002443 // make sure to clear all GL error flags
2444 while ( glGetError() != GL_NO_ERROR ) ;
2445
2446 // create a FBO
2447 GLuint name, tname;
2448 glGenRenderbuffersOES(1, &tname);
2449 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2450 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002451
Mathias Agopian74c40c02010-09-29 13:02:36 -07002452 glGenFramebuffersOES(1, &name);
2453 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2454 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2455 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2456
2457 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002458
Mathias Agopian74c40c02010-09-29 13:02:36 -07002459 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2460
2461 // invert everything, b/c glReadPixel() below will invert the FB
2462 glViewport(0, 0, sw, sh);
2463 glMatrixMode(GL_PROJECTION);
2464 glPushMatrix();
2465 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002466 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002467 glMatrixMode(GL_MODELVIEW);
2468
2469 // redraw the screen entirely...
2470 glClearColor(0,0,0,1);
2471 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002472
Jamie Gennis9575f602011-10-07 14:51:16 -07002473 const LayerVector& layers(mDrawingState.layersSortedByZ);
2474 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002475 for (size_t i=0 ; i<count ; ++i) {
2476 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002477 const uint32_t flags = layer->drawingState().flags;
2478 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2479 const uint32_t z = layer->drawingState().z;
2480 if (z >= minLayerZ && z <= maxLayerZ) {
2481 layer->drawForSreenShot();
2482 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002483 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002484 }
2485
Mathias Agopian74c40c02010-09-29 13:02:36 -07002486 // check for errors and return screen capture
2487 if (glGetError() != GL_NO_ERROR) {
2488 // error while rendering
2489 result = INVALID_OPERATION;
2490 } else {
2491 // allocate shared memory large enough to hold the
2492 // screen capture
2493 sp<MemoryHeapBase> base(
2494 new MemoryHeapBase(size, 0, "screen-capture") );
2495 void* const ptr = base->getBase();
2496 if (ptr) {
2497 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002498 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002499 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2500 if (glGetError() == GL_NO_ERROR) {
2501 *heap = base;
2502 *w = sw;
2503 *h = sh;
2504 *f = PIXEL_FORMAT_RGBA_8888;
2505 result = NO_ERROR;
2506 }
2507 } else {
2508 result = NO_MEMORY;
2509 }
2510 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002511 glViewport(0, 0, hw_w, hw_h);
2512 glMatrixMode(GL_PROJECTION);
2513 glPopMatrix();
2514 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002515 } else {
2516 result = BAD_VALUE;
2517 }
2518
2519 // release FBO resources
2520 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2521 glDeleteRenderbuffersOES(1, &tname);
2522 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002523
2524 hw.compositionComplete();
2525
Steve Block9d453682011-12-20 16:23:08 +00002526 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002527
Mathias Agopian74c40c02010-09-29 13:02:36 -07002528 return result;
2529}
2530
2531
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002532status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2533 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002534 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002535 uint32_t sw, uint32_t sh,
2536 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002537{
2538 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002539 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002540 return BAD_VALUE;
2541
2542 if (!GLExtensions::getInstance().haveFramebufferObject())
2543 return INVALID_OPERATION;
2544
2545 class MessageCaptureScreen : public MessageBase {
2546 SurfaceFlinger* flinger;
2547 DisplayID dpy;
2548 sp<IMemoryHeap>* heap;
2549 uint32_t* w;
2550 uint32_t* h;
2551 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002552 uint32_t sw;
2553 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002554 uint32_t minLayerZ;
2555 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002556 status_t result;
2557 public:
2558 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002559 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002560 uint32_t sw, uint32_t sh,
2561 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002562 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002563 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2564 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2565 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002566 {
2567 }
2568 status_t getResult() const {
2569 return result;
2570 }
2571 virtual bool handler() {
2572 Mutex::Autolock _l(flinger->mStateLock);
2573
2574 // if we have secure windows, never allow the screen capture
2575 if (flinger->mSecureFrameBuffer)
2576 return true;
2577
Mathias Agopian74c40c02010-09-29 13:02:36 -07002578 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002579 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002580
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002581 return true;
2582 }
2583 };
2584
2585 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002586 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002587 status_t res = postMessageSync(msg);
2588 if (res == NO_ERROR) {
2589 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2590 }
2591 return res;
2592}
2593
2594// ---------------------------------------------------------------------------
2595
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002596sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2597{
2598 sp<Layer> result;
2599 Mutex::Autolock _l(mStateLock);
2600 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2601 return result;
2602}
2603
2604// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002605
Jamie Gennis9a78c902011-01-12 18:30:40 -08002606GraphicBufferAlloc::GraphicBufferAlloc() {}
2607
2608GraphicBufferAlloc::~GraphicBufferAlloc() {}
2609
2610sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002611 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002612 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2613 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002614 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002615 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002616 if (err == NO_MEMORY) {
2617 GraphicBuffer::dumpAllocationsToSystemLog();
2618 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002619 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002620 "failed (%s), handle=%p",
2621 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002622 return 0;
2623 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002624 return graphicBuffer;
2625}
2626
Jamie Gennis9a78c902011-01-12 18:30:40 -08002627// ---------------------------------------------------------------------------
2628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629GraphicPlane::GraphicPlane()
2630 : mHw(0)
2631{
2632}
2633
2634GraphicPlane::~GraphicPlane() {
2635 delete mHw;
2636}
2637
2638bool GraphicPlane::initialized() const {
2639 return mHw ? true : false;
2640}
2641
Mathias Agopian2b92d892010-02-08 15:49:35 -08002642int GraphicPlane::getWidth() const {
2643 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644}
2645
Mathias Agopian2b92d892010-02-08 15:49:35 -08002646int GraphicPlane::getHeight() const {
2647 return mHeight;
2648}
2649
2650void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2651{
2652 mHw = hw;
2653
2654 // initialize the display orientation transform.
2655 // it's a constant that should come from the display driver.
2656 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2657 char property[PROPERTY_VALUE_MAX];
2658 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2659 //displayOrientation
2660 switch (atoi(property)) {
2661 case 90:
2662 displayOrientation = ISurfaceComposer::eOrientation90;
2663 break;
2664 case 270:
2665 displayOrientation = ISurfaceComposer::eOrientation270;
2666 break;
2667 }
2668 }
2669
2670 const float w = hw->getWidth();
2671 const float h = hw->getHeight();
2672 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2673 &mDisplayTransform);
2674 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2675 mDisplayWidth = h;
2676 mDisplayHeight = w;
2677 } else {
2678 mDisplayWidth = w;
2679 mDisplayHeight = h;
2680 }
2681
2682 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002683}
2684
2685status_t GraphicPlane::orientationToTransfrom(
2686 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002687{
2688 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689 switch (orientation) {
2690 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002691 flags = Transform::ROT_0;
2692 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002693 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002694 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002695 break;
2696 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002697 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002698 break;
2699 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002700 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002701 break;
2702 default:
2703 return BAD_VALUE;
2704 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002705 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002706 return NO_ERROR;
2707}
2708
2709status_t GraphicPlane::setOrientation(int orientation)
2710{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002711 // If the rotation can be handled in hardware, this is where
2712 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002713
2714 const DisplayHardware& hw(displayHardware());
2715 const float w = mDisplayWidth;
2716 const float h = mDisplayHeight;
2717 mWidth = int(w);
2718 mHeight = int(h);
2719
2720 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002721 GraphicPlane::orientationToTransfrom(orientation, w, h,
2722 &orientationTransform);
2723 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2724 mWidth = int(h);
2725 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002727
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002728 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002729 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002730 return NO_ERROR;
2731}
2732
2733const DisplayHardware& GraphicPlane::displayHardware() const {
2734 return *mHw;
2735}
2736
Mathias Agopian59119e62010-10-11 12:37:43 -07002737DisplayHardware& GraphicPlane::editDisplayHardware() {
2738 return *mHw;
2739}
2740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002741const Transform& GraphicPlane::transform() const {
2742 return mGlobalTransform;
2743}
2744
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002745EGLDisplay GraphicPlane::getEGLDisplay() const {
2746 return mHw->getEGLDisplay();
2747}
2748
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002749// ---------------------------------------------------------------------------
2750
2751}; // namespace android