blob: b30adafb177496863204ab123d65dae62f8eb6ca [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <utils/String8.h>
40#include <utils/String16.h>
41#include <utils/StopWatch.h>
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#include <pixelflinger/pixelflinger.h>
47#include <GLES/gl.h>
48
49#include "clz.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050#include "DisplayEventConnection.h"
51#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070052#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070053#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070056#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
59#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070060#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Glenn Kasten1db13d72011-12-19 13:55:34 -080062#include <private/android_filesystem_config.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070063#include <private/surfaceflinger/SharedBufferStack.h>
64
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080065#define EGL_VERSION_HW_ANDROID 0x3143
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian99b49842011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070082 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070087 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mConsoleSignals(0),
99 mSecureFrameBuffer(0)
100{
101 init();
102}
103
104void SurfaceFlinger::init()
105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showbackground", value, "0");
115 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117 property_get("debug.sf.ddms", value, "0");
118 mDebugDDMS = atoi(value);
119 if (mDebugDDMS) {
120 DdmConnection::start(getServiceName());
121 }
122
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI_IF(mDebugRegion, "showupdates enabled");
124 ALOGI_IF(mDebugBackground, "showbackground enabled");
125 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126}
127
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800128void SurfaceFlinger::onFirstRef()
129{
130 mEventQueue.init(this);
131
132 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
133
134 // Wait for the main thread to be done with its initialization
135 mReadyToRunBarrier.wait();
136}
137
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139SurfaceFlinger::~SurfaceFlinger()
140{
141 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800144void SurfaceFlinger::binderDied(const wp<IBinder>& who)
145{
146 // the window manager died on us. prepare its eulogy.
147
148 // reset screen orientation
149 Vector<ComposerState> state;
150 setTransactionState(state, eOrientationDefault, 0);
151
152 // restart the boot-animation
153 property_set("ctl.start", "bootanim");
154}
155
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700156sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159}
160
Mathias Agopian7e27f052010-05-28 14:22:23 -0700161sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Mathias Agopian96f08192010-06-02 23:28:45 -0700163 sp<ISurfaceComposerClient> bclient;
164 sp<Client> client(new Client(this));
165 status_t err = client->initCheck();
166 if (err == NO_ERROR) {
167 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 return bclient;
170}
171
Jamie Gennis9a78c902011-01-12 18:30:40 -0800172sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
173{
174 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
175 return gba;
176}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
179{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000180 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 const GraphicPlane& plane(mGraphicPlanes[dpy]);
182 return plane;
183}
184
185GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
186{
187 return const_cast<GraphicPlane&>(
188 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
189}
190
191void SurfaceFlinger::bootFinished()
192{
193 const nsecs_t now = systemTime();
194 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000195 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700196 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700197
198 // wait patiently for the window manager death
199 const String16 name("window");
200 sp<IBinder> window(defaultServiceManager()->getService(name));
201 if (window != 0) {
202 window->linkToDeath(this);
203 }
204
205 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700206 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209static inline uint16_t pack565(int r, int g, int b) {
210 return (r<<11)|(g<<5)|b;
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213status_t SurfaceFlinger::readyToRun()
214{
Steve Blocka19954a2012-01-04 20:05:49 +0000215 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 "Initializing graphics H/W...");
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 // we only support one display currently
219 int dpy = 0;
220
221 {
222 // initialize the main display
223 GraphicPlane& plane(graphicPlane(dpy));
224 DisplayHardware* const hw = new DisplayHardware(this, dpy);
225 plane.setDisplayHardware(hw);
226 }
227
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700228 // create the shared control-block
229 mServerHeap = new MemoryHeapBase(4096,
230 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000234 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700235
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700236 new(mServerCblk) surface_flinger_cblk_t;
237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 // initialize primary screen
239 // (other display should be initialized in the same manner, but
240 // asynchronously, as they could come and go. None of this is supported
241 // yet).
242 const GraphicPlane& plane(graphicPlane(dpy));
243 const DisplayHardware& hw = plane.displayHardware();
244 const uint32_t w = hw.getWidth();
245 const uint32_t h = hw.getHeight();
246 const uint32_t f = hw.getFormat();
247 hw.makeCurrent();
248
249 // initialize the shared control block
250 mServerCblk->connected |= 1<<dpy;
251 display_cblk_t* dcblk = mServerCblk->displays + dpy;
252 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800253 dcblk->w = plane.getWidth();
254 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 dcblk->format = f;
256 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
257 dcblk->xdpi = hw.getDpiX();
258 dcblk->ydpi = hw.getDpiY();
259 dcblk->fps = hw.getRefreshRate();
260 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
262 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700264 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glEnableClientState(GL_VERTEX_ARRAY);
266 glEnable(GL_SCISSOR_TEST);
267 glShadeModel(GL_FLAT);
268 glDisable(GL_DITHER);
269 glDisable(GL_CULL_FACE);
270
271 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
272 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700273 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 glGenTextures(1, &mWormholeTexName);
275 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700281 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
282
283 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
284 glGenTextures(1, &mProtectedTexName);
285 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
290 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
291 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
293 glViewport(0, 0, w, h);
294 glMatrixMode(GL_PROJECTION);
295 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700296 // put the origin in the left-bottom corner
297 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 -0800298
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800299
300 // start the EventThread
301 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800302 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
304 /*
305 * We're now ready to accept clients...
306 */
307
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800308 mReadyToRunBarrier.open();
309
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800317
Jamie Gennis582270d2011-08-17 18:19:00 -0700318bool SurfaceFlinger::authenticateSurfaceTexture(
319 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800320 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700321 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800322
323 // Check the visible layer list for the ISurface
324 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
325 size_t count = currentLayers.size();
326 for (size_t i=0 ; i<count ; i++) {
327 const sp<LayerBase>& layer(currentLayers[i]);
328 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700329 if (lbc != NULL) {
330 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
331 if (lbcBinder == surfaceTextureBinder) {
332 return true;
333 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800334 }
335 }
336
337 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700338 // SurfaceTexture gets destroyed before all the clients are done using it,
339 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800340 // will instead fail later on when the client tries to use the surface,
341 // which should be reported as "surface XYZ returned an -ENODEV". The
342 // purgatorized layers are no less authentic than the visible ones, so this
343 // should not cause any harm.
344 size_t purgatorySize = mLayerPurgatory.size();
345 for (size_t i=0 ; i<purgatorySize ; i++) {
346 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
347 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700348 if (lbc != NULL) {
349 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
350 if (lbcBinder == surfaceTextureBinder) {
351 return true;
352 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800353 }
354 }
355
356 return false;
357}
358
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800359// ----------------------------------------------------------------------------
360
361sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800362 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700363}
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800366
367void SurfaceFlinger::waitForEvent() {
368 mEventQueue.waitMessage();
369}
370
371void SurfaceFlinger::signalTransaction() {
372 mEventQueue.invalidate();
373}
374
375void SurfaceFlinger::signalLayerUpdate() {
376 mEventQueue.invalidate();
377}
378
379void SurfaceFlinger::signalRefresh() {
380 mEventQueue.refresh();
381}
382
383status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
384 nsecs_t reltime, uint32_t flags) {
385 return mEventQueue.postMessage(msg, reltime);
386}
387
388status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
389 nsecs_t reltime, uint32_t flags) {
390 status_t res = mEventQueue.postMessage(msg, reltime);
391 if (res == NO_ERROR) {
392 msg->wait();
393 }
394 return res;
395}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396
397bool SurfaceFlinger::threadLoop()
398{
399 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 return true;
401}
402
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800403void SurfaceFlinger::onMessageReceived(int32_t what)
404{
405 switch (what) {
406 case MessageQueue::INVALIDATE: {
407 // check for transactions
408 if (CC_UNLIKELY(mConsoleSignals)) {
409 handleConsoleEvents();
410 }
411
412 // if we're in a global transaction, don't do anything.
413 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
414 uint32_t transactionFlags = peekTransactionFlags(mask);
415 if (CC_UNLIKELY(transactionFlags)) {
416 handleTransaction(transactionFlags);
417 }
418
419 // post surfaces (if needed)
420 handlePageFlip();
421
Mathias Agopianb048cef2012-02-04 15:44:04 -0800422 signalRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800423 } break;
424
425 case MessageQueue::REFRESH: {
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800426 // NOTE: it is mandatory to call hw.compositionComplete()
427 // after handleRefresh()
428 const DisplayHardware& hw(graphicPlane(0).displayHardware());
429 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800430
Mathias Agopianb048cef2012-02-04 15:44:04 -0800431 if (CC_UNLIKELY(mHwWorkListDirty)) {
432 // build the h/w work list
433 handleWorkList();
434 }
435 if (CC_LIKELY(hw.canDraw())) {
436 // repaint the framebuffer (if needed)
437 handleRepaint();
438 // inform the h/w that we're done compositing
439 hw.compositionComplete();
440 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800441 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800442 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800443 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800444 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800445
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800446 } break;
447 }
448}
449
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450void SurfaceFlinger::postFramebuffer()
451{
Mathias Agopianb048cef2012-02-04 15:44:04 -0800452 // mSwapRegion can be empty here is some cases, for instance if a hidden
453 // or fully transparent window is updating.
454 // in that case, we need to flip anyways to not risk a deadlock with
455 // h/w composer.
456
Mathias Agopiana44b0412011-10-16 18:46:35 -0700457 const DisplayHardware& hw(graphicPlane(0).displayHardware());
458 const nsecs_t now = systemTime();
459 mDebugInSwapBuffers = now;
460 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800461
462 size_t numLayers = mVisibleLayersSortedByZ.size();
463 for (size_t i = 0; i < numLayers; i++) {
464 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
465 }
466
Mathias Agopiana44b0412011-10-16 18:46:35 -0700467 mLastSwapBufferTime = systemTime() - now;
468 mDebugInSwapBuffers = 0;
469 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470}
471
472void SurfaceFlinger::handleConsoleEvents()
473{
474 // something to do with the console
475 const DisplayHardware& hw = graphicPlane(0).displayHardware();
476
477 int what = android_atomic_and(0, &mConsoleSignals);
478 if (what & eConsoleAcquired) {
479 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700480 // this is a temporary work-around, eventually this should be called
481 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700482 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 }
484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700486 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 }
489 }
490
491 mDirtyRegion.set(hw.bounds());
492}
493
494void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
495{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700496 Mutex::Autolock _l(mStateLock);
497 const nsecs_t now = systemTime();
498 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499
Mathias Agopianca4d3602011-05-19 15:38:14 -0700500 // Here we're guaranteed that some transaction flags are set
501 // so we can call handleTransactionLocked() unconditionally.
502 // We call getTransactionFlags(), which will also clear the flags,
503 // with mStateLock held to guarantee that mCurrentState won't change
504 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700505
Mathias Agopianca4d3602011-05-19 15:38:14 -0700506 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
507 transactionFlags = getTransactionFlags(mask);
508 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700509
Mathias Agopianca4d3602011-05-19 15:38:14 -0700510 mLastTransactionTime = systemTime() - now;
511 mDebugInTransaction = 0;
512 invalidateHwcGeometry();
513 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700514}
515
Mathias Agopianca4d3602011-05-19 15:38:14 -0700516void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700517{
518 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 const size_t count = currentLayers.size();
520
521 /*
522 * Traversal of the children
523 * (perform the transaction for each of them if needed)
524 */
525
526 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
527 if (layersNeedTransaction) {
528 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700529 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
531 if (!trFlags) continue;
532
533 const uint32_t flags = layer->doTransaction(0);
534 if (flags & Layer::eVisibleRegion)
535 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 }
537 }
538
539 /*
540 * Perform our own transaction if needed
541 */
542
543 if (transactionFlags & eTransactionNeeded) {
544 if (mCurrentState.orientation != mDrawingState.orientation) {
545 // the orientation has changed, recompute all visible regions
546 // and invalidate everything.
547
548 const int dpy = 0;
549 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700550 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 GraphicPlane& plane(graphicPlane(dpy));
552 plane.setOrientation(orientation);
553
554 // update the shared control block
555 const DisplayHardware& hw(plane.displayHardware());
556 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
557 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800558 dcblk->w = plane.getWidth();
559 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560
561 mVisibleRegionsDirty = true;
562 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 }
564
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700565 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
566 // layers have been added
567 mVisibleRegionsDirty = true;
568 }
569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 // some layers might have been removed, so
571 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700572 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700573 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700575 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700576 const size_t count = previousLayers.size();
577 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700578 const sp<LayerBase>& layer(previousLayers[i]);
579 if (currentLayers.indexOf( layer ) < 0) {
580 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700581 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700582 }
583 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 }
586
587 commitTransaction();
588}
589
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800591 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592{
593 const GraphicPlane& plane(graphicPlane(0));
594 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700595 const DisplayHardware& hw(plane.displayHardware());
596 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597
598 Region aboveOpaqueLayers;
599 Region aboveCoveredLayers;
600 Region dirty;
601
602 bool secureFrameBuffer = false;
603
604 size_t i = currentLayers.size();
605 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 layer->validateVisibility(planeTransform);
608
609 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700610 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611
Mathias Agopianab028732010-03-16 16:41:46 -0700612 /*
613 * opaqueRegion: area of a surface that is fully opaque.
614 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700616
617 /*
618 * visibleRegion: area of a surface that is visible on screen
619 * and not fully transparent. This is essentially the layer's
620 * footprint minus the opaque regions above it.
621 * Areas covered by a translucent surface are considered visible.
622 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700624
625 /*
626 * coveredRegion: area of a surface that is covered by all
627 * visible regions above it (which includes the translucent areas).
628 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700630
631
632 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800633 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700634 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700635 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700637 visibleRegion.andSelf(screenRegion);
638 if (!visibleRegion.isEmpty()) {
639 // Remove the transparent area from the visible region
640 if (translucent) {
641 visibleRegion.subtractSelf(layer->transparentRegionScreen);
642 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
Mathias Agopianab028732010-03-16 16:41:46 -0700644 // compute the opaque region
645 const int32_t layerOrientation = layer->getOrientation();
646 if (s.alpha==255 && !translucent &&
647 ((layerOrientation & Transform::ROT_INVALID) == false)) {
648 // the opaque region is the layer's footprint
649 opaqueRegion = visibleRegion;
650 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651 }
652 }
653
Mathias Agopianab028732010-03-16 16:41:46 -0700654 // Clip the covered region to the visible region
655 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
656
657 // Update aboveCoveredLayers for next (lower) layer
658 aboveCoveredLayers.orSelf(visibleRegion);
659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 // subtract the opaque region covered by the layers above us
661 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662
663 // compute this layer's dirty region
664 if (layer->contentDirty) {
665 // we need to invalidate the whole region
666 dirty = visibleRegion;
667 // as well, as the old visible region
668 dirty.orSelf(layer->visibleRegionScreen);
669 layer->contentDirty = false;
670 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700671 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700672 * the exposed region consists of two components:
673 * 1) what's VISIBLE now and was COVERED before
674 * 2) what's EXPOSED now less what was EXPOSED before
675 *
676 * note that (1) is conservative, we start with the whole
677 * visible region but only keep what used to be covered by
678 * something -- which mean it may have been exposed.
679 *
680 * (2) handles areas that were not covered by anything but got
681 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700682 */
Mathias Agopianab028732010-03-16 16:41:46 -0700683 const Region newExposed = visibleRegion - coveredRegion;
684 const Region oldVisibleRegion = layer->visibleRegionScreen;
685 const Region oldCoveredRegion = layer->coveredRegionScreen;
686 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
687 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 }
689 dirty.subtractSelf(aboveOpaqueLayers);
690
691 // accumulate to the screen dirty region
692 dirtyRegion.orSelf(dirty);
693
Mathias Agopianab028732010-03-16 16:41:46 -0700694 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 // Store the visible region is screen space
698 layer->setVisibleRegion(visibleRegion);
699 layer->setCoveredRegion(coveredRegion);
700
Mathias Agopian97011222009-07-28 10:57:27 -0700701 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 if (layer->isSecure() && !visibleRegion.isEmpty()) {
703 secureFrameBuffer = true;
704 }
705 }
706
Mathias Agopian97011222009-07-28 10:57:27 -0700707 // invalidate the areas where a layer was removed
708 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
709 mDirtyRegionRemovedLayer.clear();
710
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711 mSecureFrameBuffer = secureFrameBuffer;
712 opaqueRegion = aboveOpaqueLayers;
713}
714
715
716void SurfaceFlinger::commitTransaction()
717{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800718 if (!mLayersPendingRemoval.isEmpty()) {
719 // Notify removed layers now that they can't be drawn from
720 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
721 mLayersPendingRemoval[i]->onRemoved();
722 }
723 mLayersPendingRemoval.clear();
724 }
725
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700727 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700728 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729}
730
731void SurfaceFlinger::handlePageFlip()
732{
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800733 const DisplayHardware& hw = graphicPlane(0).displayHardware();
734 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800736 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
737 const bool visibleRegions = lockPageFlip(currentLayers);
738
739 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 Region opaqueRegion;
741 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700742
743 /*
744 * rebuild the visible layer list
745 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800746 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700747 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700748 mVisibleLayersSortedByZ.setCapacity(count);
749 for (size_t i=0 ; i<count ; i++) {
750 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
751 mVisibleLayersSortedByZ.add(currentLayers[i]);
752 }
753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 mWormholeRegion = screenRegion.subtract(opaqueRegion);
755 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800756 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 }
758
759 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700760
761 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 mDirtyRegion.andSelf(screenRegion);
763}
764
Mathias Agopianad456f92011-01-13 17:53:01 -0800765void SurfaceFlinger::invalidateHwcGeometry()
766{
767 mHwWorkListDirty = true;
768}
769
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
771{
772 bool recomputeVisibleRegions = false;
773 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 layer->lockPageFlip(recomputeVisibleRegions);
778 }
779 return recomputeVisibleRegions;
780}
781
782void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
783{
784 const GraphicPlane& plane(graphicPlane(0));
785 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800786 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700787 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700789 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 layer->unlockPageFlip(planeTransform, mDirtyRegion);
791 }
792}
793
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800794void SurfaceFlinger::handleRefresh()
795{
796 bool needInvalidate = false;
797 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
798 const size_t count = currentLayers.size();
799 for (size_t i=0 ; i<count ; i++) {
800 const sp<LayerBase>& layer(currentLayers[i]);
801 if (layer->onPreComposition()) {
802 needInvalidate = true;
803 }
804 }
805 if (needInvalidate) {
806 signalLayerUpdate();
807 }
808}
809
810
Mathias Agopiana350ff92010-08-10 17:14:02 -0700811void SurfaceFlinger::handleWorkList()
812{
813 mHwWorkListDirty = false;
814 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
815 if (hwc.initCheck() == NO_ERROR) {
816 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
817 const size_t count = currentLayers.size();
818 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700819 hwc_layer_t* const cur(hwc.getLayers());
820 for (size_t i=0 ; cur && i<count ; i++) {
821 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700822 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700823 cur[i].compositionType = HWC_FRAMEBUFFER;
824 cur[i].flags |= HWC_SKIP_LAYER;
825 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700826 }
827 }
828}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830void SurfaceFlinger::handleRepaint()
831{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700832 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700833 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834
Glenn Kasten99ed2242011-12-15 09:51:17 -0800835 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836 debugFlashRegions();
837 }
838
Mathias Agopianb8a55602009-06-26 19:06:36 -0700839 // set the frame buffer
840 const DisplayHardware& hw(graphicPlane(0).displayHardware());
841 glMatrixMode(GL_MODELVIEW);
842 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843
844 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700845 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
846 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700847 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
849 // takes a rectangle, we must make sure to update that whole
850 // rectangle in that case
851 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700852 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700854 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // in the BUFFER_PRESERVED case, obviously, we can update only
857 // what's needed and nothing more.
858 // NOTE: this is NOT a common case, as preserving the backbuffer
859 // is costly and usually involves copying the whole update back.
860 }
861 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700862 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700863 // We need to redraw the rectangle that will be updated
864 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700865 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700866 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700867 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700868 } else {
869 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700871 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 }
873 }
874
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700875 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 composeSurfaces(mDirtyRegion);
877
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700878 // update the swap region and clear the dirty region
879 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 mDirtyRegion.clear();
881}
882
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700883void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700885 const DisplayHardware& hw(graphicPlane(0).displayHardware());
886 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700887 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700888 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700889 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700890 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700891
Mathias Agopianf384cc32011-09-08 18:31:55 -0700892 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
893 size_t count = layers.size();
894
Steve Blocke6f43dd2012-01-06 19:20:56 +0000895 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700896 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
897 hwc.getNumLayers(), count);
898
899 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700900 if (hwc.initCheck() == NO_ERROR) {
901 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
902 }
Mathias Agopian45721772010-08-12 15:03:26 -0700903
904 /*
905 * update the per-frame h/w composer data for each layer
906 * and build the transparent region of the FB
907 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700908 for (size_t i=0 ; i<count ; i++) {
909 const sp<LayerBase>& layer(layers[i]);
910 layer->setPerFrameData(&cur[i]);
911 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700912 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700913 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000914 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700915
Mathias Agopianf384cc32011-09-08 18:31:55 -0700916 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700917 // what's happening here is tricky.
918 // we want to clear all the layers with the CLEAR_FB flags
919 // that are opaque.
920 // however, since some GPU are efficient at preserving
921 // the backbuffer, we want to take advantage of that so we do the
922 // clear only in the dirty region (other areas will be preserved
923 // on those GPUs).
924 // NOTE: on non backbuffer preserving GPU, the dirty region
925 // has already been expanded as needed, so the code is correct
926 // there too.
927 //
928 // However, the content of the framebuffer cannot be trusted when
929 // we switch to/from FB/OVERLAY, in which case we need to
930 // expand the dirty region to those areas too.
931 //
932 // Note also that there is a special case when switching from
933 // "no layers in FB" to "some layers in FB", where we need to redraw
934 // the entire FB, since some areas might contain uninitialized
935 // data.
936 //
937 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700938 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700939 // that is, we can't erase anything outside the dirty region.
940
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700941 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700942
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700943 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
944 transparent.set(hw.getBounds());
945 dirtyInOut = transparent;
946 } else {
947 for (size_t i=0 ; i<count ; i++) {
948 const sp<LayerBase>& layer(layers[i]);
949 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
950 transparent.orSelf(layer->visibleRegionScreen);
951 }
952 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
953 if (isOverlay != layer->isOverlay()) {
954 // we transitioned to/from overlay, so add this layer
955 // to the dirty region so the framebuffer can be either
956 // cleared or redrawn.
957 dirtyInOut.orSelf(layer->visibleRegionScreen);
958 }
959 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800960 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700961 // don't erase stuff outside the dirty region
962 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700963 }
964
965 /*
966 * clear the area of the FB that need to be transparent
967 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700968 if (!transparent.isEmpty()) {
969 glClearColor(0,0,0,0);
970 Region::const_iterator it = transparent.begin();
971 Region::const_iterator const end = transparent.end();
972 const int32_t height = hw.getHeight();
973 while (it != end) {
974 const Rect& r(*it++);
975 const GLint sy = height - (r.top + r.height());
976 glScissor(r.left, sy, r.width(), r.height());
977 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800978 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700979 }
980 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700981}
Mathias Agopian45721772010-08-12 15:03:26 -0700982
Mathias Agopianf384cc32011-09-08 18:31:55 -0700983void SurfaceFlinger::composeSurfaces(const Region& dirty)
984{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700985 const DisplayHardware& hw(graphicPlane(0).displayHardware());
986 HWComposer& hwc(hw.getHwComposer());
987
988 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800989 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700990 // should never happen unless the window manager has a bug
991 // draw something...
992 drawWormhole();
993 }
994
Mathias Agopian45721772010-08-12 15:03:26 -0700995 /*
996 * and then, render the layers targeted at the framebuffer
997 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700998 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700999 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1000 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001001 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001002 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001003 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001004 }
1005 const sp<LayerBase>& layer(layers[i]);
1006 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1007 if (!clip.isEmpty()) {
1008 layer->draw(clip);
1009 }
1010 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011}
1012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013void SurfaceFlinger::debugFlashRegions()
1014{
Mathias Agopian0a917752010-06-14 21:20:00 -07001015 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1016 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001017 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001018 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001019 return;
1020 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001021
Mathias Agopian0a917752010-06-14 21:20:00 -07001022 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1023 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1024 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1025 mDirtyRegion.bounds() : hw.bounds());
1026 composeSurfaces(repaint);
1027 }
1028
Mathias Agopianc492e672011-10-18 14:49:27 -07001029 glDisable(GL_TEXTURE_EXTERNAL_OES);
1030 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 glDisable(GL_SCISSOR_TEST);
1033
Mathias Agopian0926f502009-05-04 14:17:04 -07001034 static int toggle = 0;
1035 toggle = 1 - toggle;
1036 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001037 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001038 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001039 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001040 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041
Mathias Agopian20f68782009-05-11 00:03:41 -07001042 Region::const_iterator it = mDirtyRegion.begin();
1043 Region::const_iterator const end = mDirtyRegion.end();
1044 while (it != end) {
1045 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001047 { r.left, height - r.top },
1048 { r.left, height - r.bottom },
1049 { r.right, height - r.bottom },
1050 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 };
1052 glVertexPointer(2, GL_FLOAT, 0, vertices);
1053 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1054 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001055
Mathias Agopian0656a682011-09-20 17:22:44 -07001056 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057
1058 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001059 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
1061 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062}
1063
1064void SurfaceFlinger::drawWormhole() const
1065{
1066 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1067 if (region.isEmpty())
1068 return;
1069
1070 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1071 const int32_t width = hw.getWidth();
1072 const int32_t height = hw.getHeight();
1073
Glenn Kasten99ed2242011-12-15 09:51:17 -08001074 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001075 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001076 Region::const_iterator it = region.begin();
1077 Region::const_iterator const end = region.end();
1078 while (it != end) {
1079 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 const GLint sy = height - (r.top + r.height());
1081 glScissor(r.left, sy, r.width(), r.height());
1082 glClear(GL_COLOR_BUFFER_BIT);
1083 }
1084 } else {
1085 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1086 { width, height }, { 0, height } };
1087 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 glVertexPointer(2, GL_SHORT, 0, vertices);
1090 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1091 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001092
1093 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 glEnable(GL_TEXTURE_2D);
1095 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1096 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1097 glMatrixMode(GL_TEXTURE);
1098 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001099
1100 glDisable(GL_BLEND);
1101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001103 Region::const_iterator it = region.begin();
1104 Region::const_iterator const end = region.end();
1105 while (it != end) {
1106 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107 const GLint sy = height - (r.top + r.height());
1108 glScissor(r.left, sy, r.width(), r.height());
1109 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1110 }
1111 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001112 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001113 glLoadIdentity();
1114 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 }
1116}
1117
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001118status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119{
1120 Mutex::Autolock _l(mStateLock);
1121 addLayer_l(layer);
1122 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1123 return NO_ERROR;
1124}
1125
Mathias Agopian96f08192010-06-02 23:28:45 -07001126status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1127{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001128 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001129 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1130}
1131
1132ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1133 const sp<LayerBaseClient>& lbc)
1134{
Mathias Agopian96f08192010-06-02 23:28:45 -07001135 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001136 size_t name = client->attachLayer(lbc);
1137
1138 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001139
1140 // add this layer to the current state list
1141 addLayer_l(lbc);
1142
Mathias Agopian4f113742011-05-03 16:21:41 -07001143 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001144}
1145
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001146status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147{
1148 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001149 status_t err = purgatorizeLayer_l(layer);
1150 if (err == NO_ERROR)
1151 setTransactionFlags(eTransactionNeeded);
1152 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153}
1154
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001155status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001157 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1158 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001159 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1162 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001163 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 return NO_ERROR;
1165 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001166 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167}
1168
Mathias Agopian9a112062009-04-17 19:36:26 -07001169status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1170{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001171 // First add the layer to the purgatory list, which makes sure it won't
1172 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001173 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001174 if (err >= 0) {
1175 mLayerPurgatory.add(layerBase);
1176 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001177
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001178 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001179
Mathias Agopian3d579642009-06-04 18:46:21 -07001180 // it's possible that we don't find a layer, because it might
1181 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001182 // from the user because there is a race between Client::destroySurface(),
1183 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001184 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1185}
1186
Mathias Agopian96f08192010-06-02 23:28:45 -07001187status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188{
Mathias Agopian96f08192010-06-02 23:28:45 -07001189 layer->forceVisibilityTransaction();
1190 setTransactionFlags(eTraversalNeeded);
1191 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192}
1193
Mathias Agopiandea20b12011-05-03 17:04:02 -07001194uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1195{
1196 return android_atomic_release_load(&mTransactionFlags);
1197}
1198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1200{
1201 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1202}
1203
Mathias Agopianbb641242010-05-18 17:06:55 -07001204uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205{
1206 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1207 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001208 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 }
1210 return old;
1211}
1212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001214void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001215 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001216 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001217
Jamie Gennis28378392011-10-12 17:39:00 -07001218 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001219 if (mCurrentState.orientation != orientation) {
1220 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1221 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001222 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001223 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001224 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001225 orientation);
1226 }
1227 }
1228
Mathias Agopian698c0872011-06-28 19:09:31 -07001229 const size_t count = state.size();
1230 for (size_t i=0 ; i<count ; i++) {
1231 const ComposerState& s(state[i]);
1232 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001233 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001234 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001235
Mathias Agopian386aa982011-11-07 21:58:03 -08001236 if (transactionFlags) {
1237 // this triggers the transaction
1238 setTransactionFlags(transactionFlags);
1239
1240 // if this is a synchronous transaction, wait for it to take effect
1241 // before returning.
1242 if (flags & eSynchronous) {
1243 mTransationPending = true;
1244 }
1245 while (mTransationPending) {
1246 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1247 if (CC_UNLIKELY(err != NO_ERROR)) {
1248 // just in case something goes wrong in SF, return to the
1249 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001250 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001251 mTransationPending = false;
1252 break;
1253 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001254 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 }
1256}
1257
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001258sp<ISurface> SurfaceFlinger::createSurface(
1259 ISurfaceComposerClient::surface_data_t* params,
1260 const String8& name,
1261 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1263 uint32_t flags)
1264{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001265 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001266 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001267
1268 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001269 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001270 int(w), int(h));
1271 return surfaceHandle;
1272 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001273
Steve Block9d453682011-12-20 16:23:08 +00001274 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001275 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 switch (flags & eFXSurfaceMask) {
1277 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001278 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1279 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 break;
1281 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001282 // for now we treat Blur as Dim, until we can implement it
1283 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001285 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001287 case eFXSurfaceScreenshot:
1288 layer = createScreenshotSurface(client, d, w, h, flags);
1289 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 }
1291
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001292 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001293 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001294 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001295 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001298 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001300 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001301 if (normalLayer != 0) {
1302 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001303 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001304 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001305 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001306
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 }
1309
1310 return surfaceHandle;
1311}
1312
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001313sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001314 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001315 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001316 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317{
1318 // initialize the surfaces
1319 switch (format) { // TODO: take h/w into account
1320 case PIXEL_FORMAT_TRANSPARENT:
1321 case PIXEL_FORMAT_TRANSLUCENT:
1322 format = PIXEL_FORMAT_RGBA_8888;
1323 break;
1324 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001325#ifdef NO_RGBX_8888
1326 format = PIXEL_FORMAT_RGB_565;
1327#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001328 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001329#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 break;
1331 }
1332
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001333#ifdef NO_RGBX_8888
1334 if (format == PIXEL_FORMAT_RGBX_8888)
1335 format = PIXEL_FORMAT_RGBA_8888;
1336#endif
1337
Mathias Agopian96f08192010-06-02 23:28:45 -07001338 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001339 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001340 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001341 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001342 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 }
1344 return layer;
1345}
1346
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001347sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001348 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001349 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350{
Mathias Agopian96f08192010-06-02 23:28:45 -07001351 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001352 return layer;
1353}
1354
1355sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1356 const sp<Client>& client, DisplayID display,
1357 uint32_t w, uint32_t h, uint32_t flags)
1358{
1359 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 return layer;
1361}
1362
Mathias Agopian96f08192010-06-02 23:28:45 -07001363status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364{
Mathias Agopian9a112062009-04-17 19:36:26 -07001365 /*
1366 * called by the window manager, when a surface should be marked for
1367 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001368 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001369 * The surface is removed from the current and drawing lists, but placed
1370 * in the purgatory queue, so it's not destroyed right-away (we need
1371 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001372 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001373
Mathias Agopian48d819a2009-09-10 19:41:18 -07001374 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001375 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001376 sp<LayerBaseClient> layer = client->getLayerUser(sid);
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 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001452 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453}
1454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455void SurfaceFlinger::screenReleased(int dpy)
1456{
1457 // this may be called by a signal handler, we can't do too much in here
1458 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001459 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460}
1461
1462void SurfaceFlinger::screenAcquired(int dpy)
1463{
1464 // this may be called by a signal handler, we can't do too much in here
1465 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001466 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467}
1468
1469status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1470{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001471 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472 char buffer[SIZE];
1473 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001474
1475 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 snprintf(buffer, SIZE, "Permission Denial: "
1477 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1478 IPCThreadState::self()->getCallingPid(),
1479 IPCThreadState::self()->getCallingUid());
1480 result.append(buffer);
1481 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001482 // Try to get the main lock, but don't insist if we can't
1483 // (this would indicate SF is stuck, but we want to be able to
1484 // print something in dumpsys).
1485 int retry = 3;
1486 while (mStateLock.tryLock()<0 && --retry>=0) {
1487 usleep(1000000);
1488 }
1489 const bool locked(retry >= 0);
1490 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001491 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001492 "SurfaceFlinger appears to be unresponsive, "
1493 "dumping anyways (no locks held)\n");
1494 result.append(buffer);
1495 }
1496
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001497 bool dumpAll = true;
1498 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001499 size_t numArgs = args.size();
1500 if (numArgs) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001501 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001502
1503 if ((index < numArgs) &&
1504 (args[index] == String16("--list"))) {
1505 index++;
1506 listLayersLocked(args, index, result, buffer, SIZE);
1507 }
1508
1509 if ((index < numArgs) &&
1510 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001511 index++;
1512 dumpStatsLocked(args, index, result, buffer, SIZE);
1513 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001514
1515 if ((index < numArgs) &&
1516 (args[index] == String16("--latency-clear"))) {
1517 index++;
1518 clearStatsLocked(args, index, result, buffer, SIZE);
1519 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001521
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001522 if (dumpAll) {
1523 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001524 }
1525
Mathias Agopian9795c422009-08-26 16:36:26 -07001526 if (locked) {
1527 mStateLock.unlock();
1528 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 }
1530 write(fd, result.string(), result.size());
1531 return NO_ERROR;
1532}
1533
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001534void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1535 String8& result, char* buffer, size_t SIZE) const
1536{
1537 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1538 const size_t count = currentLayers.size();
1539 for (size_t i=0 ; i<count ; i++) {
1540 const sp<LayerBase>& layer(currentLayers[i]);
1541 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1542 result.append(buffer);
1543 }
1544}
1545
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001546void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1547 String8& result, char* buffer, size_t SIZE) const
1548{
1549 String8 name;
1550 if (index < args.size()) {
1551 name = String8(args[index]);
1552 index++;
1553 }
1554
1555 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1556 const size_t count = currentLayers.size();
1557 for (size_t i=0 ; i<count ; i++) {
1558 const sp<LayerBase>& layer(currentLayers[i]);
1559 if (name.isEmpty()) {
1560 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1561 result.append(buffer);
1562 }
1563 if (name.isEmpty() || (name == layer->getName())) {
1564 layer->dumpStats(result, buffer, SIZE);
1565 }
1566 }
1567}
1568
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001569void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1570 String8& result, char* buffer, size_t SIZE) const
1571{
1572 String8 name;
1573 if (index < args.size()) {
1574 name = String8(args[index]);
1575 index++;
1576 }
1577
1578 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1579 const size_t count = currentLayers.size();
1580 for (size_t i=0 ; i<count ; i++) {
1581 const sp<LayerBase>& layer(currentLayers[i]);
1582 if (name.isEmpty() || (name == layer->getName())) {
1583 layer->clearStats();
1584 }
1585 }
1586}
1587
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001588void SurfaceFlinger::dumpAllLocked(
1589 String8& result, char* buffer, size_t SIZE) const
1590{
1591 // figure out if we're stuck somewhere
1592 const nsecs_t now = systemTime();
1593 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1594 const nsecs_t inTransaction(mDebugInTransaction);
1595 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1596 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1597
1598 /*
1599 * Dump the visible layer list
1600 */
1601 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1602 const size_t count = currentLayers.size();
1603 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1604 result.append(buffer);
1605 for (size_t i=0 ; i<count ; i++) {
1606 const sp<LayerBase>& layer(currentLayers[i]);
1607 layer->dump(result, buffer, SIZE);
1608 }
1609
1610 /*
1611 * Dump the layers in the purgatory
1612 */
1613
1614 const size_t purgatorySize = mLayerPurgatory.size();
1615 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1616 result.append(buffer);
1617 for (size_t i=0 ; i<purgatorySize ; i++) {
1618 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1619 layer->shortDump(result, buffer, SIZE);
1620 }
1621
1622 /*
1623 * Dump SurfaceFlinger global state
1624 */
1625
1626 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1627 result.append(buffer);
1628
1629 const GLExtensions& extensions(GLExtensions::getInstance());
1630 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1631 extensions.getVendor(),
1632 extensions.getRenderer(),
1633 extensions.getVersion());
1634 result.append(buffer);
1635
1636 snprintf(buffer, SIZE, "EGL : %s\n",
1637 eglQueryString(graphicPlane(0).getEGLDisplay(),
1638 EGL_VERSION_HW_ANDROID));
1639 result.append(buffer);
1640
1641 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1642 result.append(buffer);
1643
1644 mWormholeRegion.dump(result, "WormholeRegion");
1645 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1646 snprintf(buffer, SIZE,
1647 " orientation=%d, canDraw=%d\n",
1648 mCurrentState.orientation, hw.canDraw());
1649 result.append(buffer);
1650 snprintf(buffer, SIZE,
1651 " last eglSwapBuffers() time: %f us\n"
1652 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001653 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001654 " refresh-rate : %f fps\n"
1655 " x-dpi : %f\n"
1656 " y-dpi : %f\n",
1657 mLastSwapBufferTime/1000.0,
1658 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001659 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001660 hw.getRefreshRate(),
1661 hw.getDpiX(),
1662 hw.getDpiY());
1663 result.append(buffer);
1664
1665 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1666 inSwapBuffersDuration/1000.0);
1667 result.append(buffer);
1668
1669 snprintf(buffer, SIZE, " transaction time: %f us\n",
1670 inTransactionDuration/1000.0);
1671 result.append(buffer);
1672
1673 /*
1674 * VSYNC state
1675 */
1676 mEventThread->dump(result, buffer, SIZE);
1677
1678 /*
1679 * Dump HWComposer state
1680 */
1681 HWComposer& hwc(hw.getHwComposer());
1682 snprintf(buffer, SIZE, "h/w composer state:\n");
1683 result.append(buffer);
1684 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1685 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1686 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1687 result.append(buffer);
1688 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1689
1690 /*
1691 * Dump gralloc state
1692 */
1693 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1694 alloc.dump(result);
1695 hw.dump(result);
1696}
1697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698status_t SurfaceFlinger::onTransact(
1699 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1700{
1701 switch (code) {
1702 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001703 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001706 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001707 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 {
1709 // codes that require permission check
1710 IPCThreadState* ipc = IPCThreadState::self();
1711 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001712 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001713 if ((uid != AID_GRAPHICS) &&
1714 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001715 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001716 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1717 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001719 break;
1720 }
1721 case CAPTURE_SCREEN:
1722 {
1723 // codes that require permission check
1724 IPCThreadState* ipc = IPCThreadState::self();
1725 const int pid = ipc->getCallingPid();
1726 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001727 if ((uid != AID_GRAPHICS) &&
1728 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001729 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001730 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1731 return PERMISSION_DENIED;
1732 }
1733 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 }
1735 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1738 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001739 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001740 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001741 IPCThreadState* ipc = IPCThreadState::self();
1742 const int pid = ipc->getCallingPid();
1743 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001744 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001745 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 return PERMISSION_DENIED;
1747 }
1748 int n;
1749 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001750 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001751 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 return NO_ERROR;
1753 case 1002: // SHOW_UPDATES
1754 n = data.readInt32();
1755 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001756 invalidateHwcGeometry();
1757 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 return NO_ERROR;
1759 case 1003: // SHOW_BACKGROUND
1760 n = data.readInt32();
1761 mDebugBackground = n ? 1 : 0;
1762 return NO_ERROR;
1763 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001764 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001765 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001767 case 1005:{ // force transaction
1768 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1769 return NO_ERROR;
1770 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001771 case 1008: // toggle use of hw composer
1772 n = data.readInt32();
1773 mDebugDisableHWC = n ? 1 : 0;
1774 invalidateHwcGeometry();
1775 repaintEverything();
1776 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001777 case 1009: // toggle use of transform hint
1778 n = data.readInt32();
1779 mDebugDisableTransformHint = n ? 1 : 0;
1780 invalidateHwcGeometry();
1781 repaintEverything();
1782 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001784 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 reply->writeInt32(0);
1786 reply->writeInt32(mDebugRegion);
1787 reply->writeInt32(mDebugBackground);
1788 return NO_ERROR;
1789 case 1013: {
1790 Mutex::Autolock _l(mStateLock);
1791 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1792 reply->writeInt32(hw.getPageFlipCount());
1793 }
1794 return NO_ERROR;
1795 }
1796 }
1797 return err;
1798}
1799
Mathias Agopian53331da2011-08-22 21:44:41 -07001800void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001801 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001802 const Rect bounds(hw.getBounds());
1803 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001804 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001805}
1806
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001807void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1808 Mutex::Autolock _l(mInvalidateLock);
1809 mInvalidateRegion = reg;
1810}
1811
1812Region SurfaceFlinger::getAndClearInvalidateRegion() {
1813 Mutex::Autolock _l(mInvalidateLock);
1814 Region reg(mInvalidateRegion);
1815 mInvalidateRegion.clear();
1816 return reg;
1817}
1818
Mathias Agopian59119e62010-10-11 12:37:43 -07001819// ---------------------------------------------------------------------------
1820
Mathias Agopian118d0242011-10-13 16:02:48 -07001821status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1822 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1823{
1824 Mutex::Autolock _l(mStateLock);
1825 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1826}
1827
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001828status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1829 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001830{
Mathias Agopian59119e62010-10-11 12:37:43 -07001831 if (!GLExtensions::getInstance().haveFramebufferObject())
1832 return INVALID_OPERATION;
1833
1834 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001835 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001836 const uint32_t hw_w = hw.getWidth();
1837 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001838 GLfloat u = 1;
1839 GLfloat v = 1;
1840
1841 // make sure to clear all GL error flags
1842 while ( glGetError() != GL_NO_ERROR ) ;
1843
1844 // create a FBO
1845 GLuint name, tname;
1846 glGenTextures(1, &tname);
1847 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001848 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1849 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001850 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001851 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001852 GLint tw = (2 << (31 - clz(hw_w)));
1853 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001854 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1855 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001856 u = GLfloat(hw_w) / tw;
1857 v = GLfloat(hw_h) / th;
1858 }
1859 glGenFramebuffersOES(1, &name);
1860 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1862 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001863
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001864 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001865 glDisable(GL_TEXTURE_EXTERNAL_OES);
1866 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001867 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001868 glClearColor(0,0,0,1);
1869 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001870 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001871 glMatrixMode(GL_MODELVIEW);
1872 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001873 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1874 const size_t count = layers.size();
1875 for (size_t i=0 ; i<count ; ++i) {
1876 const sp<LayerBase>& layer(layers[i]);
1877 layer->drawForSreenShot();
1878 }
1879
Mathias Agopian118d0242011-10-13 16:02:48 -07001880 hw.compositionComplete();
1881
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001882 // back to main framebuffer
1883 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1884 glDisable(GL_SCISSOR_TEST);
1885 glDeleteFramebuffersOES(1, &name);
1886
1887 *textureName = tname;
1888 *uOut = u;
1889 *vOut = v;
1890 return NO_ERROR;
1891}
1892
1893// ---------------------------------------------------------------------------
1894
1895status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1896{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001897 // get screen geometry
1898 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1899 const uint32_t hw_w = hw.getWidth();
1900 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001901 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001902
1903 GLfloat u, v;
1904 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001905 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001906 if (result != NO_ERROR) {
1907 return result;
1908 }
1909
1910 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001911 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001912 glBindTexture(GL_TEXTURE_2D, tname);
1913 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1914 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1915 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1916 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1917 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1918 glVertexPointer(2, GL_FLOAT, 0, vtx);
1919
Mathias Agopianffcf4652011-07-07 17:30:31 -07001920 /*
1921 * Texture coordinate mapping
1922 *
1923 * u
1924 * 1 +----------+---+
1925 * | | | | image is inverted
1926 * | V | | w.r.t. the texture
1927 * 1-v +----------+ | coordinates
1928 * | |
1929 * | |
1930 * | |
1931 * 0 +--------------+
1932 * 0 1
1933 *
1934 */
1935
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001936 class s_curve_interpolator {
1937 const float nbFrames, s, v;
1938 public:
1939 s_curve_interpolator(int nbFrames, float s)
1940 : nbFrames(1.0f / (nbFrames-1)), s(s),
1941 v(1.0f + expf(-s + 0.5f*s)) {
1942 }
1943 float operator()(int f) {
1944 const float x = f * nbFrames;
1945 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1946 }
1947 };
1948
1949 class v_stretch {
1950 const GLfloat hw_w, hw_h;
1951 public:
1952 v_stretch(uint32_t hw_w, uint32_t hw_h)
1953 : hw_w(hw_w), hw_h(hw_h) {
1954 }
1955 void operator()(GLfloat* vtx, float v) {
1956 const GLfloat w = hw_w + (hw_w * v);
1957 const GLfloat h = hw_h - (hw_h * v);
1958 const GLfloat x = (hw_w - w) * 0.5f;
1959 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001960 vtx[0] = x; vtx[1] = y;
1961 vtx[2] = x; vtx[3] = y + h;
1962 vtx[4] = x + w; vtx[5] = y + h;
1963 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001964 }
1965 };
1966
1967 class h_stretch {
1968 const GLfloat hw_w, hw_h;
1969 public:
1970 h_stretch(uint32_t hw_w, uint32_t hw_h)
1971 : hw_w(hw_w), hw_h(hw_h) {
1972 }
1973 void operator()(GLfloat* vtx, float v) {
1974 const GLfloat w = hw_w - (hw_w * v);
1975 const GLfloat h = 1.0f;
1976 const GLfloat x = (hw_w - w) * 0.5f;
1977 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001978 vtx[0] = x; vtx[1] = y;
1979 vtx[2] = x; vtx[3] = y + h;
1980 vtx[4] = x + w; vtx[5] = y + h;
1981 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001982 }
1983 };
1984
1985 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001986 char value[PROPERTY_VALUE_MAX];
1987 property_get("debug.sf.electron_frames", value, "24");
1988 int nbFrames = (atoi(value) + 1) >> 1;
1989 if (nbFrames <= 0) // just in case
1990 nbFrames = 24;
1991
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001992 s_curve_interpolator itr(nbFrames, 7.5f);
1993 s_curve_interpolator itg(nbFrames, 8.0f);
1994 s_curve_interpolator itb(nbFrames, 8.5f);
1995
1996 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001997
1998 glMatrixMode(GL_TEXTURE);
1999 glLoadIdentity();
2000 glMatrixMode(GL_MODELVIEW);
2001 glLoadIdentity();
2002
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002003 glEnable(GL_BLEND);
2004 glBlendFunc(GL_ONE, GL_ONE);
2005 for (int i=0 ; i<nbFrames ; i++) {
2006 float x, y, w, h;
2007 const float vr = itr(i);
2008 const float vg = itg(i);
2009 const float vb = itb(i);
2010
2011 // clear screen
2012 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002014 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002015
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002016 // draw the red plane
2017 vverts(vtx, vr);
2018 glColorMask(1,0,0,1);
2019 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002020
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021 // draw the green plane
2022 vverts(vtx, vg);
2023 glColorMask(0,1,0,1);
2024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002025
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 // draw the blue plane
2027 vverts(vtx, vb);
2028 glColorMask(0,0,1,1);
2029 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002030
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002031 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002032 glDisable(GL_TEXTURE_2D);
2033 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034 glColor4f(vg, vg, vg, 1);
2035 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2036 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002037 }
2038
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 h_stretch hverts(hw_w, hw_h);
2040 glDisable(GL_BLEND);
2041 glDisable(GL_TEXTURE_2D);
2042 glColorMask(1,1,1,1);
2043 for (int i=0 ; i<nbFrames ; i++) {
2044 const float v = itg(i);
2045 hverts(vtx, v);
2046 glClear(GL_COLOR_BUFFER_BIT);
2047 glColor4f(1-v, 1-v, 1-v, 1);
2048 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2049 hw.flip(screenBounds);
2050 }
2051
2052 glColorMask(1,1,1,1);
2053 glEnable(GL_SCISSOR_TEST);
2054 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2055 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002056 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002057 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002058 return NO_ERROR;
2059}
2060
2061status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2062{
2063 status_t result = PERMISSION_DENIED;
2064
2065 if (!GLExtensions::getInstance().haveFramebufferObject())
2066 return INVALID_OPERATION;
2067
2068
2069 // get screen geometry
2070 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2071 const uint32_t hw_w = hw.getWidth();
2072 const uint32_t hw_h = hw.getHeight();
2073 const Region screenBounds(hw.bounds());
2074
2075 GLfloat u, v;
2076 GLuint tname;
2077 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2078 if (result != NO_ERROR) {
2079 return result;
2080 }
2081
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082 GLfloat vtx[8];
2083 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002084 glBindTexture(GL_TEXTURE_2D, tname);
2085 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2086 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2087 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2088 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2089 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2090 glVertexPointer(2, GL_FLOAT, 0, vtx);
2091
2092 class s_curve_interpolator {
2093 const float nbFrames, s, v;
2094 public:
2095 s_curve_interpolator(int nbFrames, float s)
2096 : nbFrames(1.0f / (nbFrames-1)), s(s),
2097 v(1.0f + expf(-s + 0.5f*s)) {
2098 }
2099 float operator()(int f) {
2100 const float x = f * nbFrames;
2101 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2102 }
2103 };
2104
2105 class v_stretch {
2106 const GLfloat hw_w, hw_h;
2107 public:
2108 v_stretch(uint32_t hw_w, uint32_t hw_h)
2109 : hw_w(hw_w), hw_h(hw_h) {
2110 }
2111 void operator()(GLfloat* vtx, float v) {
2112 const GLfloat w = hw_w + (hw_w * v);
2113 const GLfloat h = hw_h - (hw_h * v);
2114 const GLfloat x = (hw_w - w) * 0.5f;
2115 const GLfloat y = (hw_h - h) * 0.5f;
2116 vtx[0] = x; vtx[1] = y;
2117 vtx[2] = x; vtx[3] = y + h;
2118 vtx[4] = x + w; vtx[5] = y + h;
2119 vtx[6] = x + w; vtx[7] = y;
2120 }
2121 };
2122
2123 class h_stretch {
2124 const GLfloat hw_w, hw_h;
2125 public:
2126 h_stretch(uint32_t hw_w, uint32_t hw_h)
2127 : hw_w(hw_w), hw_h(hw_h) {
2128 }
2129 void operator()(GLfloat* vtx, float v) {
2130 const GLfloat w = hw_w - (hw_w * v);
2131 const GLfloat h = 1.0f;
2132 const GLfloat x = (hw_w - w) * 0.5f;
2133 const GLfloat y = (hw_h - h) * 0.5f;
2134 vtx[0] = x; vtx[1] = y;
2135 vtx[2] = x; vtx[3] = y + h;
2136 vtx[4] = x + w; vtx[5] = y + h;
2137 vtx[6] = x + w; vtx[7] = y;
2138 }
2139 };
2140
Mathias Agopiana6546e52010-10-14 12:33:07 -07002141 // the full animation is 12 frames
2142 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002143 s_curve_interpolator itr(nbFrames, 7.5f);
2144 s_curve_interpolator itg(nbFrames, 8.0f);
2145 s_curve_interpolator itb(nbFrames, 8.5f);
2146
2147 h_stretch hverts(hw_w, hw_h);
2148 glDisable(GL_BLEND);
2149 glDisable(GL_TEXTURE_2D);
2150 glColorMask(1,1,1,1);
2151 for (int i=nbFrames-1 ; i>=0 ; i--) {
2152 const float v = itg(i);
2153 hverts(vtx, v);
2154 glClear(GL_COLOR_BUFFER_BIT);
2155 glColor4f(1-v, 1-v, 1-v, 1);
2156 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2157 hw.flip(screenBounds);
2158 }
2159
Mathias Agopiana6546e52010-10-14 12:33:07 -07002160 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002161 v_stretch vverts(hw_w, hw_h);
2162 glEnable(GL_BLEND);
2163 glBlendFunc(GL_ONE, GL_ONE);
2164 for (int i=nbFrames-1 ; i>=0 ; i--) {
2165 float x, y, w, h;
2166 const float vr = itr(i);
2167 const float vg = itg(i);
2168 const float vb = itb(i);
2169
2170 // clear screen
2171 glColorMask(1,1,1,1);
2172 glClear(GL_COLOR_BUFFER_BIT);
2173 glEnable(GL_TEXTURE_2D);
2174
2175 // draw the red plane
2176 vverts(vtx, vr);
2177 glColorMask(1,0,0,1);
2178 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2179
2180 // draw the green plane
2181 vverts(vtx, vg);
2182 glColorMask(0,1,0,1);
2183 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2184
2185 // draw the blue plane
2186 vverts(vtx, vb);
2187 glColorMask(0,0,1,1);
2188 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2189
2190 hw.flip(screenBounds);
2191 }
2192
2193 glColorMask(1,1,1,1);
2194 glEnable(GL_SCISSOR_TEST);
2195 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002196 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002197 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002198 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002199
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 return NO_ERROR;
2201}
2202
2203// ---------------------------------------------------------------------------
2204
Mathias Agopianabd671a2010-10-14 14:54:06 -07002205status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206{
2207 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2208 if (!hw.canDraw()) {
2209 // we're already off
2210 return NO_ERROR;
2211 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002212
2213 // turn off hwc while we're doing the animation
2214 hw.getHwComposer().disable();
2215 // and make sure to turn it back on (if needed) next time we compose
2216 invalidateHwcGeometry();
2217
Mathias Agopianabd671a2010-10-14 14:54:06 -07002218 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2219 electronBeamOffAnimationImplLocked();
2220 }
2221
2222 // always clear the whole screen at the end of the animation
2223 glClearColor(0,0,0,1);
2224 glDisable(GL_SCISSOR_TEST);
2225 glClear(GL_COLOR_BUFFER_BIT);
2226 glEnable(GL_SCISSOR_TEST);
2227 hw.flip( Region(hw.bounds()) );
2228
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002229 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002230}
2231
2232status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2233{
Mathias Agopian59119e62010-10-11 12:37:43 -07002234 class MessageTurnElectronBeamOff : public MessageBase {
2235 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002236 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002237 status_t result;
2238 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002239 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2240 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002241 }
2242 status_t getResult() const {
2243 return result;
2244 }
2245 virtual bool handler() {
2246 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002247 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 return true;
2249 }
2250 };
2251
Mathias Agopianabd671a2010-10-14 14:54:06 -07002252 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002253 status_t res = postMessageSync(msg);
2254 if (res == NO_ERROR) {
2255 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002256
2257 // work-around: when the power-manager calls us we activate the
2258 // animation. eventually, the "on" animation will be called
2259 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002261 }
2262 return res;
2263}
2264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002265// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002266
Mathias Agopianabd671a2010-10-14 14:54:06 -07002267status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002268{
2269 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2270 if (hw.canDraw()) {
2271 // we're already on
2272 return NO_ERROR;
2273 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002274 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2275 electronBeamOnAnimationImplLocked();
2276 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002277
2278 // make sure to redraw the whole screen when the animation is done
2279 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002280 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002281
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002282 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002283}
2284
2285status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2286{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002287 class MessageTurnElectronBeamOn : public MessageBase {
2288 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002289 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290 status_t result;
2291 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002292 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2293 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002294 }
2295 status_t getResult() const {
2296 return result;
2297 }
2298 virtual bool handler() {
2299 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 return true;
2302 }
2303 };
2304
Mathias Agopianabd671a2010-10-14 14:54:06 -07002305 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002306 return NO_ERROR;
2307}
2308
2309// ---------------------------------------------------------------------------
2310
Mathias Agopian74c40c02010-09-29 13:02:36 -07002311status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2312 sp<IMemoryHeap>* heap,
2313 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002314 uint32_t sw, uint32_t sh,
2315 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002316{
2317 status_t result = PERMISSION_DENIED;
2318
2319 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002320 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002321 return BAD_VALUE;
2322
2323 if (!GLExtensions::getInstance().haveFramebufferObject())
2324 return INVALID_OPERATION;
2325
2326 // get screen geometry
2327 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2328 const uint32_t hw_w = hw.getWidth();
2329 const uint32_t hw_h = hw.getHeight();
2330
2331 if ((sw > hw_w) || (sh > hw_h))
2332 return BAD_VALUE;
2333
2334 sw = (!sw) ? hw_w : sw;
2335 sh = (!sh) ? hw_h : sh;
2336 const size_t size = sw * sh * 4;
2337
Steve Block9d453682011-12-20 16:23:08 +00002338 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002339 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002340
Mathias Agopian74c40c02010-09-29 13:02:36 -07002341 // make sure to clear all GL error flags
2342 while ( glGetError() != GL_NO_ERROR ) ;
2343
2344 // create a FBO
2345 GLuint name, tname;
2346 glGenRenderbuffersOES(1, &tname);
2347 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2348 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2349 glGenFramebuffersOES(1, &name);
2350 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2351 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2352 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2353
2354 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002355
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2357
2358 // invert everything, b/c glReadPixel() below will invert the FB
2359 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002360 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002361 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 glMatrixMode(GL_PROJECTION);
2363 glPushMatrix();
2364 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002365 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002366 glMatrixMode(GL_MODELVIEW);
2367
2368 // redraw the screen entirely...
2369 glClearColor(0,0,0,1);
2370 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002371
Jamie Gennis9575f602011-10-07 14:51:16 -07002372 const LayerVector& layers(mDrawingState.layersSortedByZ);
2373 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 for (size_t i=0 ; i<count ; ++i) {
2375 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002376 const uint32_t flags = layer->drawingState().flags;
2377 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2378 const uint32_t z = layer->drawingState().z;
2379 if (z >= minLayerZ && z <= maxLayerZ) {
2380 layer->drawForSreenShot();
2381 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002382 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 }
2384
2385 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002386 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002387 glScissor(0, 0, sw, sh);
2388
2389 // check for errors and return screen capture
2390 if (glGetError() != GL_NO_ERROR) {
2391 // error while rendering
2392 result = INVALID_OPERATION;
2393 } else {
2394 // allocate shared memory large enough to hold the
2395 // screen capture
2396 sp<MemoryHeapBase> base(
2397 new MemoryHeapBase(size, 0, "screen-capture") );
2398 void* const ptr = base->getBase();
2399 if (ptr) {
2400 // capture the screen with glReadPixels()
2401 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2402 if (glGetError() == GL_NO_ERROR) {
2403 *heap = base;
2404 *w = sw;
2405 *h = sh;
2406 *f = PIXEL_FORMAT_RGBA_8888;
2407 result = NO_ERROR;
2408 }
2409 } else {
2410 result = NO_MEMORY;
2411 }
2412 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002413 glEnable(GL_SCISSOR_TEST);
2414 glViewport(0, 0, hw_w, hw_h);
2415 glMatrixMode(GL_PROJECTION);
2416 glPopMatrix();
2417 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 } else {
2419 result = BAD_VALUE;
2420 }
2421
2422 // release FBO resources
2423 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2424 glDeleteRenderbuffersOES(1, &tname);
2425 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002426
2427 hw.compositionComplete();
2428
Steve Block9d453682011-12-20 16:23:08 +00002429 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002430
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431 return result;
2432}
2433
2434
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002435status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2436 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002437 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002438 uint32_t sw, uint32_t sh,
2439 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440{
2441 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002442 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443 return BAD_VALUE;
2444
2445 if (!GLExtensions::getInstance().haveFramebufferObject())
2446 return INVALID_OPERATION;
2447
2448 class MessageCaptureScreen : public MessageBase {
2449 SurfaceFlinger* flinger;
2450 DisplayID dpy;
2451 sp<IMemoryHeap>* heap;
2452 uint32_t* w;
2453 uint32_t* h;
2454 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002455 uint32_t sw;
2456 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002457 uint32_t minLayerZ;
2458 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002459 status_t result;
2460 public:
2461 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002462 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002463 uint32_t sw, uint32_t sh,
2464 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002465 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002466 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2467 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2468 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002469 {
2470 }
2471 status_t getResult() const {
2472 return result;
2473 }
2474 virtual bool handler() {
2475 Mutex::Autolock _l(flinger->mStateLock);
2476
2477 // if we have secure windows, never allow the screen capture
2478 if (flinger->mSecureFrameBuffer)
2479 return true;
2480
Mathias Agopian74c40c02010-09-29 13:02:36 -07002481 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002482 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002484 return true;
2485 }
2486 };
2487
2488 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002489 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002490 status_t res = postMessageSync(msg);
2491 if (res == NO_ERROR) {
2492 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2493 }
2494 return res;
2495}
2496
2497// ---------------------------------------------------------------------------
2498
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002499sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2500{
2501 sp<Layer> result;
2502 Mutex::Autolock _l(mStateLock);
2503 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2504 return result;
2505}
2506
2507// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508
Mathias Agopian96f08192010-06-02 23:28:45 -07002509Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002510 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512}
2513
Mathias Agopian96f08192010-06-02 23:28:45 -07002514Client::~Client()
2515{
Mathias Agopian96f08192010-06-02 23:28:45 -07002516 const size_t count = mLayers.size();
2517 for (size_t i=0 ; i<count ; i++) {
2518 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2519 if (layer != 0) {
2520 mFlinger->removeLayer(layer);
2521 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522 }
2523}
2524
Mathias Agopian96f08192010-06-02 23:28:45 -07002525status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002526 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002528
Mathias Agopian4f113742011-05-03 16:21:41 -07002529size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002530{
Mathias Agopian4f113742011-05-03 16:21:41 -07002531 Mutex::Autolock _l(mLock);
2532 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002533 mLayers.add(name, layer);
2534 return name;
2535}
2536
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002537void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002538{
Mathias Agopian4f113742011-05-03 16:21:41 -07002539 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002540 // we do a linear search here, because this doesn't happen often
2541 const size_t count = mLayers.size();
2542 for (size_t i=0 ; i<count ; i++) {
2543 if (mLayers.valueAt(i) == layer) {
2544 mLayers.removeItemsAt(i, 1);
2545 break;
2546 }
2547 }
2548}
Mathias Agopian4f113742011-05-03 16:21:41 -07002549sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2550{
2551 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002552 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002553 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002554 if (layer != 0) {
2555 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002556 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002557 }
2558 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559}
2560
Mathias Agopiana67932f2011-04-20 14:20:59 -07002561
2562status_t Client::onTransact(
2563 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2564{
2565 // these must be checked
2566 IPCThreadState* ipc = IPCThreadState::self();
2567 const int pid = ipc->getCallingPid();
2568 const int uid = ipc->getCallingUid();
2569 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002570 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002571 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002572 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002573 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002574 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002575 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2576 return PERMISSION_DENIED;
2577 }
2578 }
2579 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002580}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002581
2582
Mathias Agopian96f08192010-06-02 23:28:45 -07002583sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002584 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002585 const String8& name,
2586 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002587 uint32_t flags)
2588{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002589 /*
2590 * createSurface must be called from the GL thread so that it can
2591 * have access to the GL context.
2592 */
2593
2594 class MessageCreateSurface : public MessageBase {
2595 sp<ISurface> result;
2596 SurfaceFlinger* flinger;
2597 ISurfaceComposerClient::surface_data_t* params;
2598 Client* client;
2599 const String8& name;
2600 DisplayID display;
2601 uint32_t w, h;
2602 PixelFormat format;
2603 uint32_t flags;
2604 public:
2605 MessageCreateSurface(SurfaceFlinger* flinger,
2606 ISurfaceComposerClient::surface_data_t* params,
2607 const String8& name, Client* client,
2608 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2609 uint32_t flags)
2610 : flinger(flinger), params(params), client(client), name(name),
2611 display(display), w(w), h(h), format(format), flags(flags)
2612 {
2613 }
2614 sp<ISurface> getResult() const { return result; }
2615 virtual bool handler() {
2616 result = flinger->createSurface(params, name, client,
2617 display, w, h, format, flags);
2618 return true;
2619 }
2620 };
2621
2622 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2623 params, name, this, display, w, h, format, flags);
2624 mFlinger->postMessageSync(msg);
2625 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626}
Mathias Agopian96f08192010-06-02 23:28:45 -07002627status_t Client::destroySurface(SurfaceID sid) {
2628 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630
2631// ---------------------------------------------------------------------------
2632
Jamie Gennis9a78c902011-01-12 18:30:40 -08002633GraphicBufferAlloc::GraphicBufferAlloc() {}
2634
2635GraphicBufferAlloc::~GraphicBufferAlloc() {}
2636
2637sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002638 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002639 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2640 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002641 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002642 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002643 if (err == NO_MEMORY) {
2644 GraphicBuffer::dumpAllocationsToSystemLog();
2645 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002646 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002647 "failed (%s), handle=%p",
2648 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002649 return 0;
2650 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002651 return graphicBuffer;
2652}
2653
Jamie Gennis9a78c902011-01-12 18:30:40 -08002654// ---------------------------------------------------------------------------
2655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656GraphicPlane::GraphicPlane()
2657 : mHw(0)
2658{
2659}
2660
2661GraphicPlane::~GraphicPlane() {
2662 delete mHw;
2663}
2664
2665bool GraphicPlane::initialized() const {
2666 return mHw ? true : false;
2667}
2668
Mathias Agopian2b92d892010-02-08 15:49:35 -08002669int GraphicPlane::getWidth() const {
2670 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002671}
2672
Mathias Agopian2b92d892010-02-08 15:49:35 -08002673int GraphicPlane::getHeight() const {
2674 return mHeight;
2675}
2676
2677void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2678{
2679 mHw = hw;
2680
2681 // initialize the display orientation transform.
2682 // it's a constant that should come from the display driver.
2683 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2684 char property[PROPERTY_VALUE_MAX];
2685 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2686 //displayOrientation
2687 switch (atoi(property)) {
2688 case 90:
2689 displayOrientation = ISurfaceComposer::eOrientation90;
2690 break;
2691 case 270:
2692 displayOrientation = ISurfaceComposer::eOrientation270;
2693 break;
2694 }
2695 }
2696
2697 const float w = hw->getWidth();
2698 const float h = hw->getHeight();
2699 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2700 &mDisplayTransform);
2701 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2702 mDisplayWidth = h;
2703 mDisplayHeight = w;
2704 } else {
2705 mDisplayWidth = w;
2706 mDisplayHeight = h;
2707 }
2708
2709 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710}
2711
2712status_t GraphicPlane::orientationToTransfrom(
2713 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002714{
2715 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002716 switch (orientation) {
2717 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002718 flags = Transform::ROT_0;
2719 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002720 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002721 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002722 break;
2723 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002724 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 break;
2726 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002727 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002728 break;
2729 default:
2730 return BAD_VALUE;
2731 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002732 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733 return NO_ERROR;
2734}
2735
2736status_t GraphicPlane::setOrientation(int orientation)
2737{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002738 // If the rotation can be handled in hardware, this is where
2739 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002740
2741 const DisplayHardware& hw(displayHardware());
2742 const float w = mDisplayWidth;
2743 const float h = mDisplayHeight;
2744 mWidth = int(w);
2745 mHeight = int(h);
2746
2747 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002748 GraphicPlane::orientationToTransfrom(orientation, w, h,
2749 &orientationTransform);
2750 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2751 mWidth = int(h);
2752 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002753 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002754
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002755 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002756 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002757 return NO_ERROR;
2758}
2759
2760const DisplayHardware& GraphicPlane::displayHardware() const {
2761 return *mHw;
2762}
2763
Mathias Agopian59119e62010-10-11 12:37:43 -07002764DisplayHardware& GraphicPlane::editDisplayHardware() {
2765 return *mHw;
2766}
2767
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002768const Transform& GraphicPlane::transform() const {
2769 return mGlobalTransform;
2770}
2771
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002772EGLDisplay GraphicPlane::getEGLDisplay() const {
2773 return mHw->getEGLDisplay();
2774}
2775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002776// ---------------------------------------------------------------------------
2777
2778}; // namespace android