blob: 88666ea04f6c0eb5c4788dd41f9d13e062bd974a [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) {
Mathias Agopian303d5382012-02-05 01:49:16 -0800406 case MessageQueue::REFRESH: {
407// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800408 // check for transactions
409 if (CC_UNLIKELY(mConsoleSignals)) {
410 handleConsoleEvents();
411 }
412
413 // if we're in a global transaction, don't do anything.
414 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
415 uint32_t transactionFlags = peekTransactionFlags(mask);
416 if (CC_UNLIKELY(transactionFlags)) {
417 handleTransaction(transactionFlags);
418 }
419
420 // post surfaces (if needed)
421 handlePageFlip();
422
Mathias Agopian303d5382012-02-05 01:49:16 -0800423// signalRefresh();
424//
425// } break;
426//
427// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800428
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800429 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800430
Mathias Agopian303d5382012-02-05 01:49:16 -0800431 const DisplayHardware& hw(graphicPlane(0).displayHardware());
432
433// if (mDirtyRegion.isEmpty()) {
434// return;
435// }
436
Mathias Agopianb048cef2012-02-04 15:44:04 -0800437 if (CC_UNLIKELY(mHwWorkListDirty)) {
438 // build the h/w work list
439 handleWorkList();
440 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800441
Mathias Agopianb048cef2012-02-04 15:44:04 -0800442 if (CC_LIKELY(hw.canDraw())) {
443 // repaint the framebuffer (if needed)
444 handleRepaint();
445 // inform the h/w that we're done compositing
446 hw.compositionComplete();
447 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800448 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800449 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800450 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800451 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800452
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800453 } break;
454 }
455}
456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457void SurfaceFlinger::postFramebuffer()
458{
Mathias Agopianb048cef2012-02-04 15:44:04 -0800459 // mSwapRegion can be empty here is some cases, for instance if a hidden
460 // or fully transparent window is updating.
461 // in that case, we need to flip anyways to not risk a deadlock with
462 // h/w composer.
463
Mathias Agopiana44b0412011-10-16 18:46:35 -0700464 const DisplayHardware& hw(graphicPlane(0).displayHardware());
465 const nsecs_t now = systemTime();
466 mDebugInSwapBuffers = now;
467 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800468
469 size_t numLayers = mVisibleLayersSortedByZ.size();
470 for (size_t i = 0; i < numLayers; i++) {
471 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
472 }
473
Mathias Agopiana44b0412011-10-16 18:46:35 -0700474 mLastSwapBufferTime = systemTime() - now;
475 mDebugInSwapBuffers = 0;
476 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477}
478
479void SurfaceFlinger::handleConsoleEvents()
480{
481 // something to do with the console
482 const DisplayHardware& hw = graphicPlane(0).displayHardware();
483
484 int what = android_atomic_and(0, &mConsoleSignals);
485 if (what & eConsoleAcquired) {
486 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700487 // this is a temporary work-around, eventually this should be called
488 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700489 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490 }
491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700493 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 }
496 }
497
498 mDirtyRegion.set(hw.bounds());
499}
500
501void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
502{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700503 Mutex::Autolock _l(mStateLock);
504 const nsecs_t now = systemTime();
505 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506
Mathias Agopianca4d3602011-05-19 15:38:14 -0700507 // Here we're guaranteed that some transaction flags are set
508 // so we can call handleTransactionLocked() unconditionally.
509 // We call getTransactionFlags(), which will also clear the flags,
510 // with mStateLock held to guarantee that mCurrentState won't change
511 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700512
Mathias Agopianca4d3602011-05-19 15:38:14 -0700513 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
514 transactionFlags = getTransactionFlags(mask);
515 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700516
Mathias Agopianca4d3602011-05-19 15:38:14 -0700517 mLastTransactionTime = systemTime() - now;
518 mDebugInTransaction = 0;
519 invalidateHwcGeometry();
520 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700521}
522
Mathias Agopianca4d3602011-05-19 15:38:14 -0700523void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700524{
525 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 const size_t count = currentLayers.size();
527
528 /*
529 * Traversal of the children
530 * (perform the transaction for each of them if needed)
531 */
532
533 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
534 if (layersNeedTransaction) {
535 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
538 if (!trFlags) continue;
539
540 const uint32_t flags = layer->doTransaction(0);
541 if (flags & Layer::eVisibleRegion)
542 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 }
544 }
545
546 /*
547 * Perform our own transaction if needed
548 */
549
550 if (transactionFlags & eTransactionNeeded) {
551 if (mCurrentState.orientation != mDrawingState.orientation) {
552 // the orientation has changed, recompute all visible regions
553 // and invalidate everything.
554
555 const int dpy = 0;
556 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700557 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 GraphicPlane& plane(graphicPlane(dpy));
559 plane.setOrientation(orientation);
560
561 // update the shared control block
562 const DisplayHardware& hw(plane.displayHardware());
563 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
564 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800565 dcblk->w = plane.getWidth();
566 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567
568 mVisibleRegionsDirty = true;
569 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 }
571
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700572 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
573 // layers have been added
574 mVisibleRegionsDirty = true;
575 }
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 // some layers might have been removed, so
578 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700580 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700582 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700583 const size_t count = previousLayers.size();
584 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700585 const sp<LayerBase>& layer(previousLayers[i]);
586 if (currentLayers.indexOf( layer ) < 0) {
587 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700588 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700589 }
590 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 }
593
594 commitTransaction();
595}
596
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800598 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599{
600 const GraphicPlane& plane(graphicPlane(0));
601 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700602 const DisplayHardware& hw(plane.displayHardware());
603 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604
605 Region aboveOpaqueLayers;
606 Region aboveCoveredLayers;
607 Region dirty;
608
609 bool secureFrameBuffer = false;
610
611 size_t i = currentLayers.size();
612 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700613 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 layer->validateVisibility(planeTransform);
615
616 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700617 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618
Mathias Agopianab028732010-03-16 16:41:46 -0700619 /*
620 * opaqueRegion: area of a surface that is fully opaque.
621 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700623
624 /*
625 * visibleRegion: area of a surface that is visible on screen
626 * and not fully transparent. This is essentially the layer's
627 * footprint minus the opaque regions above it.
628 * Areas covered by a translucent surface are considered visible.
629 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700631
632 /*
633 * coveredRegion: area of a surface that is covered by all
634 * visible regions above it (which includes the translucent areas).
635 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700637
638
639 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800640 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700641 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700642 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700644 visibleRegion.andSelf(screenRegion);
645 if (!visibleRegion.isEmpty()) {
646 // Remove the transparent area from the visible region
647 if (translucent) {
648 visibleRegion.subtractSelf(layer->transparentRegionScreen);
649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
Mathias Agopianab028732010-03-16 16:41:46 -0700651 // compute the opaque region
652 const int32_t layerOrientation = layer->getOrientation();
653 if (s.alpha==255 && !translucent &&
654 ((layerOrientation & Transform::ROT_INVALID) == false)) {
655 // the opaque region is the layer's footprint
656 opaqueRegion = visibleRegion;
657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 }
659 }
660
Mathias Agopianab028732010-03-16 16:41:46 -0700661 // Clip the covered region to the visible region
662 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
663
664 // Update aboveCoveredLayers for next (lower) layer
665 aboveCoveredLayers.orSelf(visibleRegion);
666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 // subtract the opaque region covered by the layers above us
668 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669
670 // compute this layer's dirty region
671 if (layer->contentDirty) {
672 // we need to invalidate the whole region
673 dirty = visibleRegion;
674 // as well, as the old visible region
675 dirty.orSelf(layer->visibleRegionScreen);
676 layer->contentDirty = false;
677 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700678 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700679 * the exposed region consists of two components:
680 * 1) what's VISIBLE now and was COVERED before
681 * 2) what's EXPOSED now less what was EXPOSED before
682 *
683 * note that (1) is conservative, we start with the whole
684 * visible region but only keep what used to be covered by
685 * something -- which mean it may have been exposed.
686 *
687 * (2) handles areas that were not covered by anything but got
688 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700689 */
Mathias Agopianab028732010-03-16 16:41:46 -0700690 const Region newExposed = visibleRegion - coveredRegion;
691 const Region oldVisibleRegion = layer->visibleRegionScreen;
692 const Region oldCoveredRegion = layer->coveredRegionScreen;
693 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
694 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 }
696 dirty.subtractSelf(aboveOpaqueLayers);
697
698 // accumulate to the screen dirty region
699 dirtyRegion.orSelf(dirty);
700
Mathias Agopianab028732010-03-16 16:41:46 -0700701 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700703
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 // Store the visible region is screen space
705 layer->setVisibleRegion(visibleRegion);
706 layer->setCoveredRegion(coveredRegion);
707
Mathias Agopian97011222009-07-28 10:57:27 -0700708 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 if (layer->isSecure() && !visibleRegion.isEmpty()) {
710 secureFrameBuffer = true;
711 }
712 }
713
Mathias Agopian97011222009-07-28 10:57:27 -0700714 // invalidate the areas where a layer was removed
715 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
716 mDirtyRegionRemovedLayer.clear();
717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 mSecureFrameBuffer = secureFrameBuffer;
719 opaqueRegion = aboveOpaqueLayers;
720}
721
722
723void SurfaceFlinger::commitTransaction()
724{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800725 if (!mLayersPendingRemoval.isEmpty()) {
726 // Notify removed layers now that they can't be drawn from
727 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
728 mLayersPendingRemoval[i]->onRemoved();
729 }
730 mLayersPendingRemoval.clear();
731 }
732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700734 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700735 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736}
737
738void SurfaceFlinger::handlePageFlip()
739{
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800740 const DisplayHardware& hw = graphicPlane(0).displayHardware();
741 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800743 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
744 const bool visibleRegions = lockPageFlip(currentLayers);
745
746 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 Region opaqueRegion;
748 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700749
750 /*
751 * rebuild the visible layer list
752 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800753 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700754 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700755 mVisibleLayersSortedByZ.setCapacity(count);
756 for (size_t i=0 ; i<count ; i++) {
757 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
758 mVisibleLayersSortedByZ.add(currentLayers[i]);
759 }
760
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 mWormholeRegion = screenRegion.subtract(opaqueRegion);
762 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800763 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 }
765
766 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700767
768 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769 mDirtyRegion.andSelf(screenRegion);
770}
771
Mathias Agopianad456f92011-01-13 17:53:01 -0800772void SurfaceFlinger::invalidateHwcGeometry()
773{
774 mHwWorkListDirty = true;
775}
776
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
778{
779 bool recomputeVisibleRegions = false;
780 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700781 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700783 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 layer->lockPageFlip(recomputeVisibleRegions);
785 }
786 return recomputeVisibleRegions;
787}
788
789void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
790{
791 const GraphicPlane& plane(graphicPlane(0));
792 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800793 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700794 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700796 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797 layer->unlockPageFlip(planeTransform, mDirtyRegion);
798 }
799}
800
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800801void SurfaceFlinger::handleRefresh()
802{
803 bool needInvalidate = false;
804 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
805 const size_t count = currentLayers.size();
806 for (size_t i=0 ; i<count ; i++) {
807 const sp<LayerBase>& layer(currentLayers[i]);
808 if (layer->onPreComposition()) {
809 needInvalidate = true;
810 }
811 }
812 if (needInvalidate) {
813 signalLayerUpdate();
814 }
815}
816
817
Mathias Agopiana350ff92010-08-10 17:14:02 -0700818void SurfaceFlinger::handleWorkList()
819{
820 mHwWorkListDirty = false;
821 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
822 if (hwc.initCheck() == NO_ERROR) {
823 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
824 const size_t count = currentLayers.size();
825 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700826 hwc_layer_t* const cur(hwc.getLayers());
827 for (size_t i=0 ; cur && i<count ; i++) {
828 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700829 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700830 cur[i].compositionType = HWC_FRAMEBUFFER;
831 cur[i].flags |= HWC_SKIP_LAYER;
832 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700833 }
834 }
835}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700836
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837void SurfaceFlinger::handleRepaint()
838{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700839 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700840 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841
Glenn Kasten99ed2242011-12-15 09:51:17 -0800842 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 debugFlashRegions();
844 }
845
Mathias Agopianb8a55602009-06-26 19:06:36 -0700846 // set the frame buffer
847 const DisplayHardware& hw(graphicPlane(0).displayHardware());
848 glMatrixMode(GL_MODELVIEW);
849 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850
851 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700852 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
853 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700854 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700855 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
856 // takes a rectangle, we must make sure to update that whole
857 // rectangle in that case
858 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700859 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700860 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700861 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700863 // in the BUFFER_PRESERVED case, obviously, we can update only
864 // what's needed and nothing more.
865 // NOTE: this is NOT a common case, as preserving the backbuffer
866 // is costly and usually involves copying the whole update back.
867 }
868 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700869 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700870 // We need to redraw the rectangle that will be updated
871 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700872 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700873 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700874 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700875 } else {
876 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700878 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 }
880 }
881
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700882 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 composeSurfaces(mDirtyRegion);
884
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700885 // update the swap region and clear the dirty region
886 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 mDirtyRegion.clear();
888}
889
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700890void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700892 const DisplayHardware& hw(graphicPlane(0).displayHardware());
893 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700894 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700896 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700897 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700898
Mathias Agopianf384cc32011-09-08 18:31:55 -0700899 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
900 size_t count = layers.size();
901
Steve Blocke6f43dd2012-01-06 19:20:56 +0000902 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700903 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
904 hwc.getNumLayers(), count);
905
906 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700907 if (hwc.initCheck() == NO_ERROR) {
908 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
909 }
Mathias Agopian45721772010-08-12 15:03:26 -0700910
911 /*
912 * update the per-frame h/w composer data for each layer
913 * and build the transparent region of the FB
914 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700915 for (size_t i=0 ; i<count ; i++) {
916 const sp<LayerBase>& layer(layers[i]);
917 layer->setPerFrameData(&cur[i]);
918 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700919 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700920 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000921 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700922
Mathias Agopianf384cc32011-09-08 18:31:55 -0700923 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700924 // what's happening here is tricky.
925 // we want to clear all the layers with the CLEAR_FB flags
926 // that are opaque.
927 // however, since some GPU are efficient at preserving
928 // the backbuffer, we want to take advantage of that so we do the
929 // clear only in the dirty region (other areas will be preserved
930 // on those GPUs).
931 // NOTE: on non backbuffer preserving GPU, the dirty region
932 // has already been expanded as needed, so the code is correct
933 // there too.
934 //
935 // However, the content of the framebuffer cannot be trusted when
936 // we switch to/from FB/OVERLAY, in which case we need to
937 // expand the dirty region to those areas too.
938 //
939 // Note also that there is a special case when switching from
940 // "no layers in FB" to "some layers in FB", where we need to redraw
941 // the entire FB, since some areas might contain uninitialized
942 // data.
943 //
944 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700945 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700946 // that is, we can't erase anything outside the dirty region.
947
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700948 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700949
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700950 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
951 transparent.set(hw.getBounds());
952 dirtyInOut = transparent;
953 } else {
954 for (size_t i=0 ; i<count ; i++) {
955 const sp<LayerBase>& layer(layers[i]);
956 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
957 transparent.orSelf(layer->visibleRegionScreen);
958 }
959 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
960 if (isOverlay != layer->isOverlay()) {
961 // we transitioned to/from overlay, so add this layer
962 // to the dirty region so the framebuffer can be either
963 // cleared or redrawn.
964 dirtyInOut.orSelf(layer->visibleRegionScreen);
965 }
966 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800967 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700968 // don't erase stuff outside the dirty region
969 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700970 }
971
972 /*
973 * clear the area of the FB that need to be transparent
974 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700975 if (!transparent.isEmpty()) {
976 glClearColor(0,0,0,0);
977 Region::const_iterator it = transparent.begin();
978 Region::const_iterator const end = transparent.end();
979 const int32_t height = hw.getHeight();
980 while (it != end) {
981 const Rect& r(*it++);
982 const GLint sy = height - (r.top + r.height());
983 glScissor(r.left, sy, r.width(), r.height());
984 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800985 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700986 }
987 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700988}
Mathias Agopian45721772010-08-12 15:03:26 -0700989
Mathias Agopianf384cc32011-09-08 18:31:55 -0700990void SurfaceFlinger::composeSurfaces(const Region& dirty)
991{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700992 const DisplayHardware& hw(graphicPlane(0).displayHardware());
993 HWComposer& hwc(hw.getHwComposer());
994
995 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800996 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700997 // should never happen unless the window manager has a bug
998 // draw something...
999 drawWormhole();
1000 }
1001
Mathias Agopian45721772010-08-12 15:03:26 -07001002 /*
1003 * and then, render the layers targeted at the framebuffer
1004 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001005 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001006 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1007 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001008 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001009 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001010 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001011 }
1012 const sp<LayerBase>& layer(layers[i]);
1013 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1014 if (!clip.isEmpty()) {
1015 layer->draw(clip);
1016 }
1017 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018}
1019
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020void SurfaceFlinger::debugFlashRegions()
1021{
Mathias Agopian0a917752010-06-14 21:20:00 -07001022 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1023 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001024 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001025 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001026 return;
1027 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001028
Mathias Agopian0a917752010-06-14 21:20:00 -07001029 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1030 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1031 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1032 mDirtyRegion.bounds() : hw.bounds());
1033 composeSurfaces(repaint);
1034 }
1035
Mathias Agopianc492e672011-10-18 14:49:27 -07001036 glDisable(GL_TEXTURE_EXTERNAL_OES);
1037 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 glDisable(GL_SCISSOR_TEST);
1040
Mathias Agopian0926f502009-05-04 14:17:04 -07001041 static int toggle = 0;
1042 toggle = 1 - toggle;
1043 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001044 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001045 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001046 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001047 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048
Mathias Agopian20f68782009-05-11 00:03:41 -07001049 Region::const_iterator it = mDirtyRegion.begin();
1050 Region::const_iterator const end = mDirtyRegion.end();
1051 while (it != end) {
1052 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001054 { r.left, height - r.top },
1055 { r.left, height - r.bottom },
1056 { r.right, height - r.bottom },
1057 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 };
1059 glVertexPointer(2, GL_FLOAT, 0, vertices);
1060 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1061 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001062
Mathias Agopian0656a682011-09-20 17:22:44 -07001063 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
1065 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001066 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067
1068 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
1071void SurfaceFlinger::drawWormhole() const
1072{
1073 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1074 if (region.isEmpty())
1075 return;
1076
1077 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1078 const int32_t width = hw.getWidth();
1079 const int32_t height = hw.getHeight();
1080
Glenn Kasten99ed2242011-12-15 09:51:17 -08001081 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001082 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001083 Region::const_iterator it = region.begin();
1084 Region::const_iterator const end = region.end();
1085 while (it != end) {
1086 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 const GLint sy = height - (r.top + r.height());
1088 glScissor(r.left, sy, r.width(), r.height());
1089 glClear(GL_COLOR_BUFFER_BIT);
1090 }
1091 } else {
1092 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1093 { width, height }, { 0, height } };
1094 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096 glVertexPointer(2, GL_SHORT, 0, vertices);
1097 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1098 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001099
1100 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 glEnable(GL_TEXTURE_2D);
1102 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1103 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1104 glMatrixMode(GL_TEXTURE);
1105 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001106
1107 glDisable(GL_BLEND);
1108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001110 Region::const_iterator it = region.begin();
1111 Region::const_iterator const end = region.end();
1112 while (it != end) {
1113 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 const GLint sy = height - (r.top + r.height());
1115 glScissor(r.left, sy, r.width(), r.height());
1116 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1117 }
1118 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001119 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001120 glLoadIdentity();
1121 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 }
1123}
1124
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001125status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126{
1127 Mutex::Autolock _l(mStateLock);
1128 addLayer_l(layer);
1129 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1130 return NO_ERROR;
1131}
1132
Mathias Agopian96f08192010-06-02 23:28:45 -07001133status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1134{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001135 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001136 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1137}
1138
1139ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1140 const sp<LayerBaseClient>& lbc)
1141{
Mathias Agopian96f08192010-06-02 23:28:45 -07001142 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001143 size_t name = client->attachLayer(lbc);
1144
1145 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001146
1147 // add this layer to the current state list
1148 addLayer_l(lbc);
1149
Mathias Agopian4f113742011-05-03 16:21:41 -07001150 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001151}
1152
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001153status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154{
1155 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001156 status_t err = purgatorizeLayer_l(layer);
1157 if (err == NO_ERROR)
1158 setTransactionFlags(eTransactionNeeded);
1159 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160}
1161
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001162status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001164 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1165 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001166 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1169 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001170 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 return NO_ERROR;
1172 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001173 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174}
1175
Mathias Agopian9a112062009-04-17 19:36:26 -07001176status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1177{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001178 // First add the layer to the purgatory list, which makes sure it won't
1179 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001180 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001181 if (err >= 0) {
1182 mLayerPurgatory.add(layerBase);
1183 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001184
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001185 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001186
Mathias Agopian3d579642009-06-04 18:46:21 -07001187 // it's possible that we don't find a layer, because it might
1188 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001189 // from the user because there is a race between Client::destroySurface(),
1190 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001191 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1192}
1193
Mathias Agopian96f08192010-06-02 23:28:45 -07001194status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195{
Mathias Agopian96f08192010-06-02 23:28:45 -07001196 layer->forceVisibilityTransaction();
1197 setTransactionFlags(eTraversalNeeded);
1198 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199}
1200
Mathias Agopiandea20b12011-05-03 17:04:02 -07001201uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1202{
1203 return android_atomic_release_load(&mTransactionFlags);
1204}
1205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1207{
1208 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1209}
1210
Mathias Agopianbb641242010-05-18 17:06:55 -07001211uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212{
1213 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1214 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001215 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216 }
1217 return old;
1218}
1219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001221void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001222 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001223 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001224
Jamie Gennis28378392011-10-12 17:39:00 -07001225 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001226 if (mCurrentState.orientation != orientation) {
1227 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1228 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001229 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001230 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001231 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001232 orientation);
1233 }
1234 }
1235
Mathias Agopian698c0872011-06-28 19:09:31 -07001236 const size_t count = state.size();
1237 for (size_t i=0 ; i<count ; i++) {
1238 const ComposerState& s(state[i]);
1239 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001240 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001241 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001242
Mathias Agopian386aa982011-11-07 21:58:03 -08001243 if (transactionFlags) {
1244 // this triggers the transaction
1245 setTransactionFlags(transactionFlags);
1246
1247 // if this is a synchronous transaction, wait for it to take effect
1248 // before returning.
1249 if (flags & eSynchronous) {
1250 mTransationPending = true;
1251 }
1252 while (mTransationPending) {
1253 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1254 if (CC_UNLIKELY(err != NO_ERROR)) {
1255 // just in case something goes wrong in SF, return to the
1256 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001257 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001258 mTransationPending = false;
1259 break;
1260 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001261 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 }
1263}
1264
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001265sp<ISurface> SurfaceFlinger::createSurface(
1266 ISurfaceComposerClient::surface_data_t* params,
1267 const String8& name,
1268 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1270 uint32_t flags)
1271{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001272 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001273 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001274
1275 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001276 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001277 int(w), int(h));
1278 return surfaceHandle;
1279 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001280
Steve Block9d453682011-12-20 16:23:08 +00001281 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001282 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 switch (flags & eFXSurfaceMask) {
1284 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001285 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1286 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 break;
1288 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001289 // for now we treat Blur as Dim, until we can implement it
1290 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001294 case eFXSurfaceScreenshot:
1295 layer = createScreenshotSurface(client, d, w, h, flags);
1296 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 }
1298
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001299 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001300 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001301 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001305 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001306 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001307 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001308 if (normalLayer != 0) {
1309 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001310 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001311 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001312 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001313
Mathias Agopian96f08192010-06-02 23:28:45 -07001314 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 }
1316
1317 return surfaceHandle;
1318}
1319
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001320sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001321 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001323 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324{
1325 // initialize the surfaces
1326 switch (format) { // TODO: take h/w into account
1327 case PIXEL_FORMAT_TRANSPARENT:
1328 case PIXEL_FORMAT_TRANSLUCENT:
1329 format = PIXEL_FORMAT_RGBA_8888;
1330 break;
1331 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001332#ifdef NO_RGBX_8888
1333 format = PIXEL_FORMAT_RGB_565;
1334#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001335 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001336#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 break;
1338 }
1339
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001340#ifdef NO_RGBX_8888
1341 if (format == PIXEL_FORMAT_RGBX_8888)
1342 format = PIXEL_FORMAT_RGBA_8888;
1343#endif
1344
Mathias Agopian96f08192010-06-02 23:28:45 -07001345 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001346 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001347 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001348 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001349 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 }
1351 return layer;
1352}
1353
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001354sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001355 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001356 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357{
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001359 return layer;
1360}
1361
1362sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1363 const sp<Client>& client, DisplayID display,
1364 uint32_t w, uint32_t h, uint32_t flags)
1365{
1366 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 return layer;
1368}
1369
Mathias Agopian96f08192010-06-02 23:28:45 -07001370status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371{
Mathias Agopian9a112062009-04-17 19:36:26 -07001372 /*
1373 * called by the window manager, when a surface should be marked for
1374 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001375 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001376 * The surface is removed from the current and drawing lists, but placed
1377 * in the purgatory queue, so it's not destroyed right-away (we need
1378 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001379 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001380
Mathias Agopian48d819a2009-09-10 19:41:18 -07001381 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001382 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001383 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001384 if (layer != 0) {
1385 err = purgatorizeLayer_l(layer);
1386 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001387 setTransactionFlags(eTransactionNeeded);
1388 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001389 }
1390 return err;
1391}
1392
Mathias Agopianca4d3602011-05-19 15:38:14 -07001393status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001394{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001395 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001396 status_t err = NO_ERROR;
1397 sp<LayerBaseClient> l(layer.promote());
1398 if (l != NULL) {
1399 Mutex::Autolock _l(mStateLock);
1400 err = removeLayer_l(l);
1401 if (err == NAME_NOT_FOUND) {
1402 // The surface wasn't in the current list, which means it was
1403 // removed already, which means it is in the purgatory,
1404 // and need to be removed from there.
1405 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001406 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001407 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001408 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001409 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001410 "error removing layer=%p (%s)", l.get(), strerror(-err));
1411 }
1412 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413}
1414
Mathias Agopian698c0872011-06-28 19:09:31 -07001415uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001416 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001417 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001420 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1421 if (layer != 0) {
1422 const uint32_t what = s.what;
1423 if (what & ePositionChanged) {
1424 if (layer->setPosition(s.x, s.y))
1425 flags |= eTraversalNeeded;
1426 }
1427 if (what & eLayerChanged) {
1428 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1429 if (layer->setLayer(s.z)) {
1430 mCurrentState.layersSortedByZ.removeAt(idx);
1431 mCurrentState.layersSortedByZ.add(layer);
1432 // we need traversal (state changed)
1433 // AND transaction (list changed)
1434 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 }
1436 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001437 if (what & eSizeChanged) {
1438 if (layer->setSize(s.w, s.h)) {
1439 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001440 }
1441 }
1442 if (what & eAlphaChanged) {
1443 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eMatrixChanged) {
1447 if (layer->setMatrix(s.matrix))
1448 flags |= eTraversalNeeded;
1449 }
1450 if (what & eTransparentRegionChanged) {
1451 if (layer->setTransparentRegionHint(s.transparentRegion))
1452 flags |= eTraversalNeeded;
1453 }
1454 if (what & eVisibilityChanged) {
1455 if (layer->setFlags(s.flags, s.mask))
1456 flags |= eTraversalNeeded;
1457 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001459 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460}
1461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462void SurfaceFlinger::screenReleased(int dpy)
1463{
1464 // this may be called by a signal handler, we can't do too much in here
1465 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001466 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467}
1468
1469void SurfaceFlinger::screenAcquired(int dpy)
1470{
1471 // this may be called by a signal handler, we can't do too much in here
1472 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001473 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474}
1475
1476status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1477{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001478 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 char buffer[SIZE];
1480 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001481
1482 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 snprintf(buffer, SIZE, "Permission Denial: "
1484 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1485 IPCThreadState::self()->getCallingPid(),
1486 IPCThreadState::self()->getCallingUid());
1487 result.append(buffer);
1488 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001489 // Try to get the main lock, but don't insist if we can't
1490 // (this would indicate SF is stuck, but we want to be able to
1491 // print something in dumpsys).
1492 int retry = 3;
1493 while (mStateLock.tryLock()<0 && --retry>=0) {
1494 usleep(1000000);
1495 }
1496 const bool locked(retry >= 0);
1497 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001498 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001499 "SurfaceFlinger appears to be unresponsive, "
1500 "dumping anyways (no locks held)\n");
1501 result.append(buffer);
1502 }
1503
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001504 bool dumpAll = true;
1505 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001506 size_t numArgs = args.size();
1507 if (numArgs) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001508 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001509
1510 if ((index < numArgs) &&
1511 (args[index] == String16("--list"))) {
1512 index++;
1513 listLayersLocked(args, index, result, buffer, SIZE);
1514 }
1515
1516 if ((index < numArgs) &&
1517 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001518 index++;
1519 dumpStatsLocked(args, index, result, buffer, SIZE);
1520 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001521
1522 if ((index < numArgs) &&
1523 (args[index] == String16("--latency-clear"))) {
1524 index++;
1525 clearStatsLocked(args, index, result, buffer, SIZE);
1526 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001528
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001529 if (dumpAll) {
1530 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001531 }
1532
Mathias Agopian9795c422009-08-26 16:36:26 -07001533 if (locked) {
1534 mStateLock.unlock();
1535 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536 }
1537 write(fd, result.string(), result.size());
1538 return NO_ERROR;
1539}
1540
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001541void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1542 String8& result, char* buffer, size_t SIZE) const
1543{
1544 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1545 const size_t count = currentLayers.size();
1546 for (size_t i=0 ; i<count ; i++) {
1547 const sp<LayerBase>& layer(currentLayers[i]);
1548 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1549 result.append(buffer);
1550 }
1551}
1552
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001553void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1554 String8& result, char* buffer, size_t SIZE) const
1555{
1556 String8 name;
1557 if (index < args.size()) {
1558 name = String8(args[index]);
1559 index++;
1560 }
1561
1562 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1563 const size_t count = currentLayers.size();
1564 for (size_t i=0 ; i<count ; i++) {
1565 const sp<LayerBase>& layer(currentLayers[i]);
1566 if (name.isEmpty()) {
1567 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1568 result.append(buffer);
1569 }
1570 if (name.isEmpty() || (name == layer->getName())) {
1571 layer->dumpStats(result, buffer, SIZE);
1572 }
1573 }
1574}
1575
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001576void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1577 String8& result, char* buffer, size_t SIZE) const
1578{
1579 String8 name;
1580 if (index < args.size()) {
1581 name = String8(args[index]);
1582 index++;
1583 }
1584
1585 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1586 const size_t count = currentLayers.size();
1587 for (size_t i=0 ; i<count ; i++) {
1588 const sp<LayerBase>& layer(currentLayers[i]);
1589 if (name.isEmpty() || (name == layer->getName())) {
1590 layer->clearStats();
1591 }
1592 }
1593}
1594
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001595void SurfaceFlinger::dumpAllLocked(
1596 String8& result, char* buffer, size_t SIZE) const
1597{
1598 // figure out if we're stuck somewhere
1599 const nsecs_t now = systemTime();
1600 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1601 const nsecs_t inTransaction(mDebugInTransaction);
1602 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1603 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1604
1605 /*
1606 * Dump the visible layer list
1607 */
1608 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1609 const size_t count = currentLayers.size();
1610 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1611 result.append(buffer);
1612 for (size_t i=0 ; i<count ; i++) {
1613 const sp<LayerBase>& layer(currentLayers[i]);
1614 layer->dump(result, buffer, SIZE);
1615 }
1616
1617 /*
1618 * Dump the layers in the purgatory
1619 */
1620
1621 const size_t purgatorySize = mLayerPurgatory.size();
1622 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1623 result.append(buffer);
1624 for (size_t i=0 ; i<purgatorySize ; i++) {
1625 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1626 layer->shortDump(result, buffer, SIZE);
1627 }
1628
1629 /*
1630 * Dump SurfaceFlinger global state
1631 */
1632
1633 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1634 result.append(buffer);
1635
1636 const GLExtensions& extensions(GLExtensions::getInstance());
1637 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1638 extensions.getVendor(),
1639 extensions.getRenderer(),
1640 extensions.getVersion());
1641 result.append(buffer);
1642
1643 snprintf(buffer, SIZE, "EGL : %s\n",
1644 eglQueryString(graphicPlane(0).getEGLDisplay(),
1645 EGL_VERSION_HW_ANDROID));
1646 result.append(buffer);
1647
1648 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1649 result.append(buffer);
1650
1651 mWormholeRegion.dump(result, "WormholeRegion");
1652 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1653 snprintf(buffer, SIZE,
1654 " orientation=%d, canDraw=%d\n",
1655 mCurrentState.orientation, hw.canDraw());
1656 result.append(buffer);
1657 snprintf(buffer, SIZE,
1658 " last eglSwapBuffers() time: %f us\n"
1659 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001660 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001661 " refresh-rate : %f fps\n"
1662 " x-dpi : %f\n"
1663 " y-dpi : %f\n",
1664 mLastSwapBufferTime/1000.0,
1665 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001666 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001667 hw.getRefreshRate(),
1668 hw.getDpiX(),
1669 hw.getDpiY());
1670 result.append(buffer);
1671
1672 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1673 inSwapBuffersDuration/1000.0);
1674 result.append(buffer);
1675
1676 snprintf(buffer, SIZE, " transaction time: %f us\n",
1677 inTransactionDuration/1000.0);
1678 result.append(buffer);
1679
1680 /*
1681 * VSYNC state
1682 */
1683 mEventThread->dump(result, buffer, SIZE);
1684
1685 /*
1686 * Dump HWComposer state
1687 */
1688 HWComposer& hwc(hw.getHwComposer());
1689 snprintf(buffer, SIZE, "h/w composer state:\n");
1690 result.append(buffer);
1691 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1692 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1693 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1694 result.append(buffer);
1695 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1696
1697 /*
1698 * Dump gralloc state
1699 */
1700 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1701 alloc.dump(result);
1702 hw.dump(result);
1703}
1704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705status_t SurfaceFlinger::onTransact(
1706 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1707{
1708 switch (code) {
1709 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001710 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001713 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001714 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 {
1716 // codes that require permission check
1717 IPCThreadState* ipc = IPCThreadState::self();
1718 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001719 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001720 if ((uid != AID_GRAPHICS) &&
1721 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001722 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001723 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1724 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001726 break;
1727 }
1728 case CAPTURE_SCREEN:
1729 {
1730 // codes that require permission check
1731 IPCThreadState* ipc = IPCThreadState::self();
1732 const int pid = ipc->getCallingPid();
1733 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001734 if ((uid != AID_GRAPHICS) &&
1735 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001736 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001737 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1738 return PERMISSION_DENIED;
1739 }
1740 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 }
1742 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1745 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001746 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001747 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001748 IPCThreadState* ipc = IPCThreadState::self();
1749 const int pid = ipc->getCallingPid();
1750 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001751 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001752 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 return PERMISSION_DENIED;
1754 }
1755 int n;
1756 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001757 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001758 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001759 return NO_ERROR;
1760 case 1002: // SHOW_UPDATES
1761 n = data.readInt32();
1762 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001763 invalidateHwcGeometry();
1764 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 return NO_ERROR;
1766 case 1003: // SHOW_BACKGROUND
1767 n = data.readInt32();
1768 mDebugBackground = n ? 1 : 0;
1769 return NO_ERROR;
1770 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001771 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001772 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001774 case 1005:{ // force transaction
1775 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1776 return NO_ERROR;
1777 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001778 case 1008: // toggle use of hw composer
1779 n = data.readInt32();
1780 mDebugDisableHWC = n ? 1 : 0;
1781 invalidateHwcGeometry();
1782 repaintEverything();
1783 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001784 case 1009: // toggle use of transform hint
1785 n = data.readInt32();
1786 mDebugDisableTransformHint = n ? 1 : 0;
1787 invalidateHwcGeometry();
1788 repaintEverything();
1789 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001791 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001792 reply->writeInt32(0);
1793 reply->writeInt32(mDebugRegion);
1794 reply->writeInt32(mDebugBackground);
1795 return NO_ERROR;
1796 case 1013: {
1797 Mutex::Autolock _l(mStateLock);
1798 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1799 reply->writeInt32(hw.getPageFlipCount());
1800 }
1801 return NO_ERROR;
1802 }
1803 }
1804 return err;
1805}
1806
Mathias Agopian53331da2011-08-22 21:44:41 -07001807void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001808 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001809 const Rect bounds(hw.getBounds());
1810 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001811 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001812}
1813
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001814void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1815 Mutex::Autolock _l(mInvalidateLock);
1816 mInvalidateRegion = reg;
1817}
1818
1819Region SurfaceFlinger::getAndClearInvalidateRegion() {
1820 Mutex::Autolock _l(mInvalidateLock);
1821 Region reg(mInvalidateRegion);
1822 mInvalidateRegion.clear();
1823 return reg;
1824}
1825
Mathias Agopian59119e62010-10-11 12:37:43 -07001826// ---------------------------------------------------------------------------
1827
Mathias Agopian118d0242011-10-13 16:02:48 -07001828status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1829 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1830{
1831 Mutex::Autolock _l(mStateLock);
1832 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1833}
1834
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001835status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1836 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001837{
Mathias Agopian59119e62010-10-11 12:37:43 -07001838 if (!GLExtensions::getInstance().haveFramebufferObject())
1839 return INVALID_OPERATION;
1840
1841 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001842 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001843 const uint32_t hw_w = hw.getWidth();
1844 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001845 GLfloat u = 1;
1846 GLfloat v = 1;
1847
1848 // make sure to clear all GL error flags
1849 while ( glGetError() != GL_NO_ERROR ) ;
1850
1851 // create a FBO
1852 GLuint name, tname;
1853 glGenTextures(1, &tname);
1854 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001855 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1856 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001857 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001858 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001859 GLint tw = (2 << (31 - clz(hw_w)));
1860 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1862 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001863 u = GLfloat(hw_w) / tw;
1864 v = GLfloat(hw_h) / th;
1865 }
1866 glGenFramebuffersOES(1, &name);
1867 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001868 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1869 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001870
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001871 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001872 glDisable(GL_TEXTURE_EXTERNAL_OES);
1873 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001874 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001875 glClearColor(0,0,0,1);
1876 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001877 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001878 glMatrixMode(GL_MODELVIEW);
1879 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001880 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1881 const size_t count = layers.size();
1882 for (size_t i=0 ; i<count ; ++i) {
1883 const sp<LayerBase>& layer(layers[i]);
1884 layer->drawForSreenShot();
1885 }
1886
Mathias Agopian118d0242011-10-13 16:02:48 -07001887 hw.compositionComplete();
1888
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001889 // back to main framebuffer
1890 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1891 glDisable(GL_SCISSOR_TEST);
1892 glDeleteFramebuffersOES(1, &name);
1893
1894 *textureName = tname;
1895 *uOut = u;
1896 *vOut = v;
1897 return NO_ERROR;
1898}
1899
1900// ---------------------------------------------------------------------------
1901
1902status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1903{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001904 // get screen geometry
1905 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1906 const uint32_t hw_w = hw.getWidth();
1907 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001908 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001909
1910 GLfloat u, v;
1911 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001912 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001913 if (result != NO_ERROR) {
1914 return result;
1915 }
1916
1917 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001918 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001919 glBindTexture(GL_TEXTURE_2D, tname);
1920 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1921 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1922 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1923 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1924 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1925 glVertexPointer(2, GL_FLOAT, 0, vtx);
1926
Mathias Agopianffcf4652011-07-07 17:30:31 -07001927 /*
1928 * Texture coordinate mapping
1929 *
1930 * u
1931 * 1 +----------+---+
1932 * | | | | image is inverted
1933 * | V | | w.r.t. the texture
1934 * 1-v +----------+ | coordinates
1935 * | |
1936 * | |
1937 * | |
1938 * 0 +--------------+
1939 * 0 1
1940 *
1941 */
1942
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001943 class s_curve_interpolator {
1944 const float nbFrames, s, v;
1945 public:
1946 s_curve_interpolator(int nbFrames, float s)
1947 : nbFrames(1.0f / (nbFrames-1)), s(s),
1948 v(1.0f + expf(-s + 0.5f*s)) {
1949 }
1950 float operator()(int f) {
1951 const float x = f * nbFrames;
1952 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1953 }
1954 };
1955
1956 class v_stretch {
1957 const GLfloat hw_w, hw_h;
1958 public:
1959 v_stretch(uint32_t hw_w, uint32_t hw_h)
1960 : hw_w(hw_w), hw_h(hw_h) {
1961 }
1962 void operator()(GLfloat* vtx, float v) {
1963 const GLfloat w = hw_w + (hw_w * v);
1964 const GLfloat h = hw_h - (hw_h * v);
1965 const GLfloat x = (hw_w - w) * 0.5f;
1966 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001967 vtx[0] = x; vtx[1] = y;
1968 vtx[2] = x; vtx[3] = y + h;
1969 vtx[4] = x + w; vtx[5] = y + h;
1970 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 }
1972 };
1973
1974 class h_stretch {
1975 const GLfloat hw_w, hw_h;
1976 public:
1977 h_stretch(uint32_t hw_w, uint32_t hw_h)
1978 : hw_w(hw_w), hw_h(hw_h) {
1979 }
1980 void operator()(GLfloat* vtx, float v) {
1981 const GLfloat w = hw_w - (hw_w * v);
1982 const GLfloat h = 1.0f;
1983 const GLfloat x = (hw_w - w) * 0.5f;
1984 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001985 vtx[0] = x; vtx[1] = y;
1986 vtx[2] = x; vtx[3] = y + h;
1987 vtx[4] = x + w; vtx[5] = y + h;
1988 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001989 }
1990 };
1991
1992 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001993 char value[PROPERTY_VALUE_MAX];
1994 property_get("debug.sf.electron_frames", value, "24");
1995 int nbFrames = (atoi(value) + 1) >> 1;
1996 if (nbFrames <= 0) // just in case
1997 nbFrames = 24;
1998
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001999 s_curve_interpolator itr(nbFrames, 7.5f);
2000 s_curve_interpolator itg(nbFrames, 8.0f);
2001 s_curve_interpolator itb(nbFrames, 8.5f);
2002
2003 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002004
2005 glMatrixMode(GL_TEXTURE);
2006 glLoadIdentity();
2007 glMatrixMode(GL_MODELVIEW);
2008 glLoadIdentity();
2009
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002010 glEnable(GL_BLEND);
2011 glBlendFunc(GL_ONE, GL_ONE);
2012 for (int i=0 ; i<nbFrames ; i++) {
2013 float x, y, w, h;
2014 const float vr = itr(i);
2015 const float vg = itg(i);
2016 const float vb = itb(i);
2017
2018 // clear screen
2019 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002020 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002021 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002022
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002023 // draw the red plane
2024 vverts(vtx, vr);
2025 glColorMask(1,0,0,1);
2026 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002027
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002028 // draw the green plane
2029 vverts(vtx, vg);
2030 glColorMask(0,1,0,1);
2031 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002032
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 // draw the blue plane
2034 vverts(vtx, vb);
2035 glColorMask(0,0,1,1);
2036 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002037
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002039 glDisable(GL_TEXTURE_2D);
2040 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002041 glColor4f(vg, vg, vg, 1);
2042 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2043 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002044 }
2045
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002046 h_stretch hverts(hw_w, hw_h);
2047 glDisable(GL_BLEND);
2048 glDisable(GL_TEXTURE_2D);
2049 glColorMask(1,1,1,1);
2050 for (int i=0 ; i<nbFrames ; i++) {
2051 const float v = itg(i);
2052 hverts(vtx, v);
2053 glClear(GL_COLOR_BUFFER_BIT);
2054 glColor4f(1-v, 1-v, 1-v, 1);
2055 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2056 hw.flip(screenBounds);
2057 }
2058
2059 glColorMask(1,1,1,1);
2060 glEnable(GL_SCISSOR_TEST);
2061 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2062 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002063 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002064 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002065 return NO_ERROR;
2066}
2067
2068status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2069{
2070 status_t result = PERMISSION_DENIED;
2071
2072 if (!GLExtensions::getInstance().haveFramebufferObject())
2073 return INVALID_OPERATION;
2074
2075
2076 // get screen geometry
2077 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2078 const uint32_t hw_w = hw.getWidth();
2079 const uint32_t hw_h = hw.getHeight();
2080 const Region screenBounds(hw.bounds());
2081
2082 GLfloat u, v;
2083 GLuint tname;
2084 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2085 if (result != NO_ERROR) {
2086 return result;
2087 }
2088
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002089 GLfloat vtx[8];
2090 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002091 glBindTexture(GL_TEXTURE_2D, tname);
2092 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2093 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2094 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2095 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2096 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2097 glVertexPointer(2, GL_FLOAT, 0, vtx);
2098
2099 class s_curve_interpolator {
2100 const float nbFrames, s, v;
2101 public:
2102 s_curve_interpolator(int nbFrames, float s)
2103 : nbFrames(1.0f / (nbFrames-1)), s(s),
2104 v(1.0f + expf(-s + 0.5f*s)) {
2105 }
2106 float operator()(int f) {
2107 const float x = f * nbFrames;
2108 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2109 }
2110 };
2111
2112 class v_stretch {
2113 const GLfloat hw_w, hw_h;
2114 public:
2115 v_stretch(uint32_t hw_w, uint32_t hw_h)
2116 : hw_w(hw_w), hw_h(hw_h) {
2117 }
2118 void operator()(GLfloat* vtx, float v) {
2119 const GLfloat w = hw_w + (hw_w * v);
2120 const GLfloat h = hw_h - (hw_h * v);
2121 const GLfloat x = (hw_w - w) * 0.5f;
2122 const GLfloat y = (hw_h - h) * 0.5f;
2123 vtx[0] = x; vtx[1] = y;
2124 vtx[2] = x; vtx[3] = y + h;
2125 vtx[4] = x + w; vtx[5] = y + h;
2126 vtx[6] = x + w; vtx[7] = y;
2127 }
2128 };
2129
2130 class h_stretch {
2131 const GLfloat hw_w, hw_h;
2132 public:
2133 h_stretch(uint32_t hw_w, uint32_t hw_h)
2134 : hw_w(hw_w), hw_h(hw_h) {
2135 }
2136 void operator()(GLfloat* vtx, float v) {
2137 const GLfloat w = hw_w - (hw_w * v);
2138 const GLfloat h = 1.0f;
2139 const GLfloat x = (hw_w - w) * 0.5f;
2140 const GLfloat y = (hw_h - h) * 0.5f;
2141 vtx[0] = x; vtx[1] = y;
2142 vtx[2] = x; vtx[3] = y + h;
2143 vtx[4] = x + w; vtx[5] = y + h;
2144 vtx[6] = x + w; vtx[7] = y;
2145 }
2146 };
2147
Mathias Agopiana6546e52010-10-14 12:33:07 -07002148 // the full animation is 12 frames
2149 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002150 s_curve_interpolator itr(nbFrames, 7.5f);
2151 s_curve_interpolator itg(nbFrames, 8.0f);
2152 s_curve_interpolator itb(nbFrames, 8.5f);
2153
2154 h_stretch hverts(hw_w, hw_h);
2155 glDisable(GL_BLEND);
2156 glDisable(GL_TEXTURE_2D);
2157 glColorMask(1,1,1,1);
2158 for (int i=nbFrames-1 ; i>=0 ; i--) {
2159 const float v = itg(i);
2160 hverts(vtx, v);
2161 glClear(GL_COLOR_BUFFER_BIT);
2162 glColor4f(1-v, 1-v, 1-v, 1);
2163 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2164 hw.flip(screenBounds);
2165 }
2166
Mathias Agopiana6546e52010-10-14 12:33:07 -07002167 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002168 v_stretch vverts(hw_w, hw_h);
2169 glEnable(GL_BLEND);
2170 glBlendFunc(GL_ONE, GL_ONE);
2171 for (int i=nbFrames-1 ; i>=0 ; i--) {
2172 float x, y, w, h;
2173 const float vr = itr(i);
2174 const float vg = itg(i);
2175 const float vb = itb(i);
2176
2177 // clear screen
2178 glColorMask(1,1,1,1);
2179 glClear(GL_COLOR_BUFFER_BIT);
2180 glEnable(GL_TEXTURE_2D);
2181
2182 // draw the red plane
2183 vverts(vtx, vr);
2184 glColorMask(1,0,0,1);
2185 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2186
2187 // draw the green plane
2188 vverts(vtx, vg);
2189 glColorMask(0,1,0,1);
2190 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2191
2192 // draw the blue plane
2193 vverts(vtx, vb);
2194 glColorMask(0,0,1,1);
2195 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2196
2197 hw.flip(screenBounds);
2198 }
2199
2200 glColorMask(1,1,1,1);
2201 glEnable(GL_SCISSOR_TEST);
2202 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002203 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002204 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002205 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002206
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 return NO_ERROR;
2208}
2209
2210// ---------------------------------------------------------------------------
2211
Mathias Agopianabd671a2010-10-14 14:54:06 -07002212status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002213{
2214 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2215 if (!hw.canDraw()) {
2216 // we're already off
2217 return NO_ERROR;
2218 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002219
2220 // turn off hwc while we're doing the animation
2221 hw.getHwComposer().disable();
2222 // and make sure to turn it back on (if needed) next time we compose
2223 invalidateHwcGeometry();
2224
Mathias Agopianabd671a2010-10-14 14:54:06 -07002225 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2226 electronBeamOffAnimationImplLocked();
2227 }
2228
2229 // always clear the whole screen at the end of the animation
2230 glClearColor(0,0,0,1);
2231 glDisable(GL_SCISSOR_TEST);
2232 glClear(GL_COLOR_BUFFER_BIT);
2233 glEnable(GL_SCISSOR_TEST);
2234 hw.flip( Region(hw.bounds()) );
2235
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002236 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002237}
2238
2239status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2240{
Mathias Agopian59119e62010-10-11 12:37:43 -07002241 class MessageTurnElectronBeamOff : public MessageBase {
2242 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002243 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002244 status_t result;
2245 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002246 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2247 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 }
2249 status_t getResult() const {
2250 return result;
2251 }
2252 virtual bool handler() {
2253 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002254 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002255 return true;
2256 }
2257 };
2258
Mathias Agopianabd671a2010-10-14 14:54:06 -07002259 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002260 status_t res = postMessageSync(msg);
2261 if (res == NO_ERROR) {
2262 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002263
2264 // work-around: when the power-manager calls us we activate the
2265 // animation. eventually, the "on" animation will be called
2266 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002267 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002268 }
2269 return res;
2270}
2271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002272// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002273
Mathias Agopianabd671a2010-10-14 14:54:06 -07002274status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002275{
2276 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2277 if (hw.canDraw()) {
2278 // we're already on
2279 return NO_ERROR;
2280 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002281 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2282 electronBeamOnAnimationImplLocked();
2283 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002284
2285 // make sure to redraw the whole screen when the animation is done
2286 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002287 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002288
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002289 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290}
2291
2292status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2293{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002294 class MessageTurnElectronBeamOn : public MessageBase {
2295 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002296 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002297 status_t result;
2298 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002299 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2300 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 }
2302 status_t getResult() const {
2303 return result;
2304 }
2305 virtual bool handler() {
2306 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002307 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002308 return true;
2309 }
2310 };
2311
Mathias Agopianabd671a2010-10-14 14:54:06 -07002312 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002313 return NO_ERROR;
2314}
2315
2316// ---------------------------------------------------------------------------
2317
Mathias Agopian74c40c02010-09-29 13:02:36 -07002318status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2319 sp<IMemoryHeap>* heap,
2320 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002321 uint32_t sw, uint32_t sh,
2322 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002323{
2324 status_t result = PERMISSION_DENIED;
2325
2326 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002327 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 return BAD_VALUE;
2329
2330 if (!GLExtensions::getInstance().haveFramebufferObject())
2331 return INVALID_OPERATION;
2332
2333 // get screen geometry
2334 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2335 const uint32_t hw_w = hw.getWidth();
2336 const uint32_t hw_h = hw.getHeight();
2337
2338 if ((sw > hw_w) || (sh > hw_h))
2339 return BAD_VALUE;
2340
2341 sw = (!sw) ? hw_w : sw;
2342 sh = (!sh) ? hw_h : sh;
2343 const size_t size = sw * sh * 4;
2344
Steve Block9d453682011-12-20 16:23:08 +00002345 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002346 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002347
Mathias Agopian74c40c02010-09-29 13:02:36 -07002348 // make sure to clear all GL error flags
2349 while ( glGetError() != GL_NO_ERROR ) ;
2350
2351 // create a FBO
2352 GLuint name, tname;
2353 glGenRenderbuffersOES(1, &tname);
2354 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2355 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2356 glGenFramebuffersOES(1, &name);
2357 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2358 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2359 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2360
2361 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002362
Mathias Agopian74c40c02010-09-29 13:02:36 -07002363 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2364
2365 // invert everything, b/c glReadPixel() below will invert the FB
2366 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002367 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002368 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002369 glMatrixMode(GL_PROJECTION);
2370 glPushMatrix();
2371 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002372 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 glMatrixMode(GL_MODELVIEW);
2374
2375 // redraw the screen entirely...
2376 glClearColor(0,0,0,1);
2377 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002378
Jamie Gennis9575f602011-10-07 14:51:16 -07002379 const LayerVector& layers(mDrawingState.layersSortedByZ);
2380 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 for (size_t i=0 ; i<count ; ++i) {
2382 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002383 const uint32_t flags = layer->drawingState().flags;
2384 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2385 const uint32_t z = layer->drawingState().z;
2386 if (z >= minLayerZ && z <= maxLayerZ) {
2387 layer->drawForSreenShot();
2388 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002389 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002390 }
2391
2392 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002393 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002394 glScissor(0, 0, sw, sh);
2395
2396 // check for errors and return screen capture
2397 if (glGetError() != GL_NO_ERROR) {
2398 // error while rendering
2399 result = INVALID_OPERATION;
2400 } else {
2401 // allocate shared memory large enough to hold the
2402 // screen capture
2403 sp<MemoryHeapBase> base(
2404 new MemoryHeapBase(size, 0, "screen-capture") );
2405 void* const ptr = base->getBase();
2406 if (ptr) {
2407 // capture the screen with glReadPixels()
2408 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2409 if (glGetError() == GL_NO_ERROR) {
2410 *heap = base;
2411 *w = sw;
2412 *h = sh;
2413 *f = PIXEL_FORMAT_RGBA_8888;
2414 result = NO_ERROR;
2415 }
2416 } else {
2417 result = NO_MEMORY;
2418 }
2419 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002420 glEnable(GL_SCISSOR_TEST);
2421 glViewport(0, 0, hw_w, hw_h);
2422 glMatrixMode(GL_PROJECTION);
2423 glPopMatrix();
2424 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002425 } else {
2426 result = BAD_VALUE;
2427 }
2428
2429 // release FBO resources
2430 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2431 glDeleteRenderbuffersOES(1, &tname);
2432 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002433
2434 hw.compositionComplete();
2435
Steve Block9d453682011-12-20 16:23:08 +00002436 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002437
Mathias Agopian74c40c02010-09-29 13:02:36 -07002438 return result;
2439}
2440
2441
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002442status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2443 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002444 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002445 uint32_t sw, uint32_t sh,
2446 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447{
2448 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002449 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002450 return BAD_VALUE;
2451
2452 if (!GLExtensions::getInstance().haveFramebufferObject())
2453 return INVALID_OPERATION;
2454
2455 class MessageCaptureScreen : public MessageBase {
2456 SurfaceFlinger* flinger;
2457 DisplayID dpy;
2458 sp<IMemoryHeap>* heap;
2459 uint32_t* w;
2460 uint32_t* h;
2461 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002462 uint32_t sw;
2463 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002464 uint32_t minLayerZ;
2465 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002466 status_t result;
2467 public:
2468 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002469 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002470 uint32_t sw, uint32_t sh,
2471 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002472 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002473 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2474 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2475 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002476 {
2477 }
2478 status_t getResult() const {
2479 return result;
2480 }
2481 virtual bool handler() {
2482 Mutex::Autolock _l(flinger->mStateLock);
2483
2484 // if we have secure windows, never allow the screen capture
2485 if (flinger->mSecureFrameBuffer)
2486 return true;
2487
Mathias Agopian74c40c02010-09-29 13:02:36 -07002488 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002489 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002490
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002491 return true;
2492 }
2493 };
2494
2495 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002496 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002497 status_t res = postMessageSync(msg);
2498 if (res == NO_ERROR) {
2499 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2500 }
2501 return res;
2502}
2503
2504// ---------------------------------------------------------------------------
2505
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002506sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2507{
2508 sp<Layer> result;
2509 Mutex::Autolock _l(mStateLock);
2510 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2511 return result;
2512}
2513
2514// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515
Mathias Agopian96f08192010-06-02 23:28:45 -07002516Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002517 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519}
2520
Mathias Agopian96f08192010-06-02 23:28:45 -07002521Client::~Client()
2522{
Mathias Agopian96f08192010-06-02 23:28:45 -07002523 const size_t count = mLayers.size();
2524 for (size_t i=0 ; i<count ; i++) {
2525 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2526 if (layer != 0) {
2527 mFlinger->removeLayer(layer);
2528 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002529 }
2530}
2531
Mathias Agopian96f08192010-06-02 23:28:45 -07002532status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002533 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002535
Mathias Agopian4f113742011-05-03 16:21:41 -07002536size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002537{
Mathias Agopian4f113742011-05-03 16:21:41 -07002538 Mutex::Autolock _l(mLock);
2539 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002540 mLayers.add(name, layer);
2541 return name;
2542}
2543
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002544void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002545{
Mathias Agopian4f113742011-05-03 16:21:41 -07002546 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002547 // we do a linear search here, because this doesn't happen often
2548 const size_t count = mLayers.size();
2549 for (size_t i=0 ; i<count ; i++) {
2550 if (mLayers.valueAt(i) == layer) {
2551 mLayers.removeItemsAt(i, 1);
2552 break;
2553 }
2554 }
2555}
Mathias Agopian4f113742011-05-03 16:21:41 -07002556sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2557{
2558 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002559 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002560 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002561 if (layer != 0) {
2562 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002563 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002564 }
2565 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002566}
2567
Mathias Agopiana67932f2011-04-20 14:20:59 -07002568
2569status_t Client::onTransact(
2570 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2571{
2572 // these must be checked
2573 IPCThreadState* ipc = IPCThreadState::self();
2574 const int pid = ipc->getCallingPid();
2575 const int uid = ipc->getCallingUid();
2576 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002577 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002578 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002579 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002580 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002581 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002582 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2583 return PERMISSION_DENIED;
2584 }
2585 }
2586 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002587}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002588
2589
Mathias Agopian96f08192010-06-02 23:28:45 -07002590sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002591 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002592 const String8& name,
2593 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002594 uint32_t flags)
2595{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002596 /*
2597 * createSurface must be called from the GL thread so that it can
2598 * have access to the GL context.
2599 */
2600
2601 class MessageCreateSurface : public MessageBase {
2602 sp<ISurface> result;
2603 SurfaceFlinger* flinger;
2604 ISurfaceComposerClient::surface_data_t* params;
2605 Client* client;
2606 const String8& name;
2607 DisplayID display;
2608 uint32_t w, h;
2609 PixelFormat format;
2610 uint32_t flags;
2611 public:
2612 MessageCreateSurface(SurfaceFlinger* flinger,
2613 ISurfaceComposerClient::surface_data_t* params,
2614 const String8& name, Client* client,
2615 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2616 uint32_t flags)
2617 : flinger(flinger), params(params), client(client), name(name),
2618 display(display), w(w), h(h), format(format), flags(flags)
2619 {
2620 }
2621 sp<ISurface> getResult() const { return result; }
2622 virtual bool handler() {
2623 result = flinger->createSurface(params, name, client,
2624 display, w, h, format, flags);
2625 return true;
2626 }
2627 };
2628
2629 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2630 params, name, this, display, w, h, format, flags);
2631 mFlinger->postMessageSync(msg);
2632 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633}
Mathias Agopian96f08192010-06-02 23:28:45 -07002634status_t Client::destroySurface(SurfaceID sid) {
2635 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002636}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637
2638// ---------------------------------------------------------------------------
2639
Jamie Gennis9a78c902011-01-12 18:30:40 -08002640GraphicBufferAlloc::GraphicBufferAlloc() {}
2641
2642GraphicBufferAlloc::~GraphicBufferAlloc() {}
2643
2644sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002645 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002646 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2647 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002648 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002649 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002650 if (err == NO_MEMORY) {
2651 GraphicBuffer::dumpAllocationsToSystemLog();
2652 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002653 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002654 "failed (%s), handle=%p",
2655 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002656 return 0;
2657 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002658 return graphicBuffer;
2659}
2660
Jamie Gennis9a78c902011-01-12 18:30:40 -08002661// ---------------------------------------------------------------------------
2662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002663GraphicPlane::GraphicPlane()
2664 : mHw(0)
2665{
2666}
2667
2668GraphicPlane::~GraphicPlane() {
2669 delete mHw;
2670}
2671
2672bool GraphicPlane::initialized() const {
2673 return mHw ? true : false;
2674}
2675
Mathias Agopian2b92d892010-02-08 15:49:35 -08002676int GraphicPlane::getWidth() const {
2677 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678}
2679
Mathias Agopian2b92d892010-02-08 15:49:35 -08002680int GraphicPlane::getHeight() const {
2681 return mHeight;
2682}
2683
2684void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2685{
2686 mHw = hw;
2687
2688 // initialize the display orientation transform.
2689 // it's a constant that should come from the display driver.
2690 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2691 char property[PROPERTY_VALUE_MAX];
2692 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2693 //displayOrientation
2694 switch (atoi(property)) {
2695 case 90:
2696 displayOrientation = ISurfaceComposer::eOrientation90;
2697 break;
2698 case 270:
2699 displayOrientation = ISurfaceComposer::eOrientation270;
2700 break;
2701 }
2702 }
2703
2704 const float w = hw->getWidth();
2705 const float h = hw->getHeight();
2706 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2707 &mDisplayTransform);
2708 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2709 mDisplayWidth = h;
2710 mDisplayHeight = w;
2711 } else {
2712 mDisplayWidth = w;
2713 mDisplayHeight = h;
2714 }
2715
2716 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002717}
2718
2719status_t GraphicPlane::orientationToTransfrom(
2720 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002721{
2722 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002723 switch (orientation) {
2724 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002725 flags = Transform::ROT_0;
2726 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002727 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002728 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002729 break;
2730 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002731 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002732 break;
2733 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002734 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002735 break;
2736 default:
2737 return BAD_VALUE;
2738 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002739 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002740 return NO_ERROR;
2741}
2742
2743status_t GraphicPlane::setOrientation(int orientation)
2744{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002745 // If the rotation can be handled in hardware, this is where
2746 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002747
2748 const DisplayHardware& hw(displayHardware());
2749 const float w = mDisplayWidth;
2750 const float h = mDisplayHeight;
2751 mWidth = int(w);
2752 mHeight = int(h);
2753
2754 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002755 GraphicPlane::orientationToTransfrom(orientation, w, h,
2756 &orientationTransform);
2757 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2758 mWidth = int(h);
2759 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002760 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002761
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002762 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002763 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002764 return NO_ERROR;
2765}
2766
2767const DisplayHardware& GraphicPlane::displayHardware() const {
2768 return *mHw;
2769}
2770
Mathias Agopian59119e62010-10-11 12:37:43 -07002771DisplayHardware& GraphicPlane::editDisplayHardware() {
2772 return *mHw;
2773}
2774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002775const Transform& GraphicPlane::transform() const {
2776 return mGlobalTransform;
2777}
2778
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002779EGLDisplay GraphicPlane::getEGLDisplay() const {
2780 return mHw->getEGLDisplay();
2781}
2782
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002783// ---------------------------------------------------------------------------
2784
2785}; // namespace android