blob: ab09bfafc73ccb6642125a968e9a1af4eb5222a9 [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);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800303 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304
305 /*
306 * We're now ready to accept clients...
307 */
308
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800309 mReadyToRunBarrier.open();
310
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700311 // start boot animation
312 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700313
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314 return NO_ERROR;
315}
316
317// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800318
Jamie Gennis582270d2011-08-17 18:19:00 -0700319bool SurfaceFlinger::authenticateSurfaceTexture(
320 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800321 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700322 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800323
324 // Check the visible layer list for the ISurface
325 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
326 size_t count = currentLayers.size();
327 for (size_t i=0 ; i<count ; i++) {
328 const sp<LayerBase>& layer(currentLayers[i]);
329 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700330 if (lbc != NULL) {
331 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
332 if (lbcBinder == surfaceTextureBinder) {
333 return true;
334 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800335 }
336 }
337
338 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700339 // SurfaceTexture gets destroyed before all the clients are done using it,
340 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800341 // will instead fail later on when the client tries to use the surface,
342 // which should be reported as "surface XYZ returned an -ENODEV". The
343 // purgatorized layers are no less authentic than the visible ones, so this
344 // should not cause any harm.
345 size_t purgatorySize = mLayerPurgatory.size();
346 for (size_t i=0 ; i<purgatorySize ; i++) {
347 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
348 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700349 if (lbc != NULL) {
350 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
351 if (lbcBinder == surfaceTextureBinder) {
352 return true;
353 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800354 }
355 }
356
357 return false;
358}
359
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800360// ----------------------------------------------------------------------------
361
362sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800363 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700364}
365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800367
368void SurfaceFlinger::waitForEvent() {
369 mEventQueue.waitMessage();
370}
371
372void SurfaceFlinger::signalTransaction() {
373 mEventQueue.invalidate();
374}
375
376void SurfaceFlinger::signalLayerUpdate() {
377 mEventQueue.invalidate();
378}
379
380void SurfaceFlinger::signalRefresh() {
381 mEventQueue.refresh();
382}
383
384status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
385 nsecs_t reltime, uint32_t flags) {
386 return mEventQueue.postMessage(msg, reltime);
387}
388
389status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
390 nsecs_t reltime, uint32_t flags) {
391 status_t res = mEventQueue.postMessage(msg, reltime);
392 if (res == NO_ERROR) {
393 msg->wait();
394 }
395 return res;
396}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397
398bool SurfaceFlinger::threadLoop()
399{
400 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 return true;
402}
403
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800404void SurfaceFlinger::onMessageReceived(int32_t what)
405{
406 switch (what) {
Mathias Agopian303d5382012-02-05 01:49:16 -0800407 case MessageQueue::REFRESH: {
408// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800409 // check for transactions
410 if (CC_UNLIKELY(mConsoleSignals)) {
411 handleConsoleEvents();
412 }
413
414 // if we're in a global transaction, don't do anything.
415 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
416 uint32_t transactionFlags = peekTransactionFlags(mask);
417 if (CC_UNLIKELY(transactionFlags)) {
418 handleTransaction(transactionFlags);
419 }
420
421 // post surfaces (if needed)
422 handlePageFlip();
423
Mathias Agopian303d5382012-02-05 01:49:16 -0800424// signalRefresh();
425//
426// } break;
427//
428// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800429
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800430 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800431
Mathias Agopian303d5382012-02-05 01:49:16 -0800432 const DisplayHardware& hw(graphicPlane(0).displayHardware());
433
434// if (mDirtyRegion.isEmpty()) {
435// return;
436// }
437
Mathias Agopianb048cef2012-02-04 15:44:04 -0800438 if (CC_UNLIKELY(mHwWorkListDirty)) {
439 // build the h/w work list
440 handleWorkList();
441 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800442
Mathias Agopianb048cef2012-02-04 15:44:04 -0800443 if (CC_LIKELY(hw.canDraw())) {
444 // repaint the framebuffer (if needed)
445 handleRepaint();
446 // inform the h/w that we're done compositing
447 hw.compositionComplete();
448 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800449 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800450 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800451 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800452 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800453
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800454 } break;
455 }
456}
457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458void SurfaceFlinger::postFramebuffer()
459{
Mathias Agopianb048cef2012-02-04 15:44:04 -0800460 // mSwapRegion can be empty here is some cases, for instance if a hidden
461 // or fully transparent window is updating.
462 // in that case, we need to flip anyways to not risk a deadlock with
463 // h/w composer.
464
Mathias Agopiana44b0412011-10-16 18:46:35 -0700465 const DisplayHardware& hw(graphicPlane(0).displayHardware());
466 const nsecs_t now = systemTime();
467 mDebugInSwapBuffers = now;
468 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800469
470 size_t numLayers = mVisibleLayersSortedByZ.size();
471 for (size_t i = 0; i < numLayers; i++) {
472 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
473 }
474
Mathias Agopiana44b0412011-10-16 18:46:35 -0700475 mLastSwapBufferTime = systemTime() - now;
476 mDebugInSwapBuffers = 0;
477 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478}
479
480void SurfaceFlinger::handleConsoleEvents()
481{
482 // something to do with the console
483 const DisplayHardware& hw = graphicPlane(0).displayHardware();
484
485 int what = android_atomic_and(0, &mConsoleSignals);
486 if (what & eConsoleAcquired) {
487 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700488 // this is a temporary work-around, eventually this should be called
489 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700490 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 }
492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700494 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 }
497 }
498
499 mDirtyRegion.set(hw.bounds());
500}
501
502void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
503{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700504 Mutex::Autolock _l(mStateLock);
505 const nsecs_t now = systemTime();
506 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507
Mathias Agopianca4d3602011-05-19 15:38:14 -0700508 // Here we're guaranteed that some transaction flags are set
509 // so we can call handleTransactionLocked() unconditionally.
510 // We call getTransactionFlags(), which will also clear the flags,
511 // with mStateLock held to guarantee that mCurrentState won't change
512 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700513
Mathias Agopianca4d3602011-05-19 15:38:14 -0700514 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
515 transactionFlags = getTransactionFlags(mask);
516 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700517
Mathias Agopianca4d3602011-05-19 15:38:14 -0700518 mLastTransactionTime = systemTime() - now;
519 mDebugInTransaction = 0;
520 invalidateHwcGeometry();
521 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700522}
523
Mathias Agopianca4d3602011-05-19 15:38:14 -0700524void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700525{
526 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527 const size_t count = currentLayers.size();
528
529 /*
530 * Traversal of the children
531 * (perform the transaction for each of them if needed)
532 */
533
534 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
535 if (layersNeedTransaction) {
536 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
539 if (!trFlags) continue;
540
541 const uint32_t flags = layer->doTransaction(0);
542 if (flags & Layer::eVisibleRegion)
543 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 }
545 }
546
547 /*
548 * Perform our own transaction if needed
549 */
550
551 if (transactionFlags & eTransactionNeeded) {
552 if (mCurrentState.orientation != mDrawingState.orientation) {
553 // the orientation has changed, recompute all visible regions
554 // and invalidate everything.
555
556 const int dpy = 0;
557 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700558 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559 GraphicPlane& plane(graphicPlane(dpy));
560 plane.setOrientation(orientation);
561
562 // update the shared control block
563 const DisplayHardware& hw(plane.displayHardware());
564 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
565 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800566 dcblk->w = plane.getWidth();
567 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568
569 mVisibleRegionsDirty = true;
570 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
572
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700573 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
574 // layers have been added
575 mVisibleRegionsDirty = true;
576 }
577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 // some layers might have been removed, so
579 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700580 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700581 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700583 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700584 const size_t count = previousLayers.size();
585 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700586 const sp<LayerBase>& layer(previousLayers[i]);
587 if (currentLayers.indexOf( layer ) < 0) {
588 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700589 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700590 }
591 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 }
594
595 commitTransaction();
596}
597
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800599 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600{
601 const GraphicPlane& plane(graphicPlane(0));
602 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700603 const DisplayHardware& hw(plane.displayHardware());
604 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605
606 Region aboveOpaqueLayers;
607 Region aboveCoveredLayers;
608 Region dirty;
609
610 bool secureFrameBuffer = false;
611
612 size_t i = currentLayers.size();
613 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700614 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 layer->validateVisibility(planeTransform);
616
617 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700618 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619
Mathias Agopianab028732010-03-16 16:41:46 -0700620 /*
621 * opaqueRegion: area of a surface that is fully opaque.
622 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700624
625 /*
626 * visibleRegion: area of a surface that is visible on screen
627 * and not fully transparent. This is essentially the layer's
628 * footprint minus the opaque regions above it.
629 * Areas covered by a translucent surface are considered visible.
630 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700632
633 /*
634 * coveredRegion: area of a surface that is covered by all
635 * visible regions above it (which includes the translucent areas).
636 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700638
639
640 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800641 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700642 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700643 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700645 visibleRegion.andSelf(screenRegion);
646 if (!visibleRegion.isEmpty()) {
647 // Remove the transparent area from the visible region
648 if (translucent) {
649 visibleRegion.subtractSelf(layer->transparentRegionScreen);
650 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651
Mathias Agopianab028732010-03-16 16:41:46 -0700652 // compute the opaque region
653 const int32_t layerOrientation = layer->getOrientation();
654 if (s.alpha==255 && !translucent &&
655 ((layerOrientation & Transform::ROT_INVALID) == false)) {
656 // the opaque region is the layer's footprint
657 opaqueRegion = visibleRegion;
658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 }
660 }
661
Mathias Agopianab028732010-03-16 16:41:46 -0700662 // Clip the covered region to the visible region
663 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
664
665 // Update aboveCoveredLayers for next (lower) layer
666 aboveCoveredLayers.orSelf(visibleRegion);
667
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 // subtract the opaque region covered by the layers above us
669 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670
671 // compute this layer's dirty region
672 if (layer->contentDirty) {
673 // we need to invalidate the whole region
674 dirty = visibleRegion;
675 // as well, as the old visible region
676 dirty.orSelf(layer->visibleRegionScreen);
677 layer->contentDirty = false;
678 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700679 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700680 * the exposed region consists of two components:
681 * 1) what's VISIBLE now and was COVERED before
682 * 2) what's EXPOSED now less what was EXPOSED before
683 *
684 * note that (1) is conservative, we start with the whole
685 * visible region but only keep what used to be covered by
686 * something -- which mean it may have been exposed.
687 *
688 * (2) handles areas that were not covered by anything but got
689 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700690 */
Mathias Agopianab028732010-03-16 16:41:46 -0700691 const Region newExposed = visibleRegion - coveredRegion;
692 const Region oldVisibleRegion = layer->visibleRegionScreen;
693 const Region oldCoveredRegion = layer->coveredRegionScreen;
694 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
695 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 }
697 dirty.subtractSelf(aboveOpaqueLayers);
698
699 // accumulate to the screen dirty region
700 dirtyRegion.orSelf(dirty);
701
Mathias Agopianab028732010-03-16 16:41:46 -0700702 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 // Store the visible region is screen space
706 layer->setVisibleRegion(visibleRegion);
707 layer->setCoveredRegion(coveredRegion);
708
Mathias Agopian97011222009-07-28 10:57:27 -0700709 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 if (layer->isSecure() && !visibleRegion.isEmpty()) {
711 secureFrameBuffer = true;
712 }
713 }
714
Mathias Agopian97011222009-07-28 10:57:27 -0700715 // invalidate the areas where a layer was removed
716 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
717 mDirtyRegionRemovedLayer.clear();
718
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 mSecureFrameBuffer = secureFrameBuffer;
720 opaqueRegion = aboveOpaqueLayers;
721}
722
723
724void SurfaceFlinger::commitTransaction()
725{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800726 if (!mLayersPendingRemoval.isEmpty()) {
727 // Notify removed layers now that they can't be drawn from
728 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
729 mLayersPendingRemoval[i]->onRemoved();
730 }
731 mLayersPendingRemoval.clear();
732 }
733
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700735 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700736 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737}
738
739void SurfaceFlinger::handlePageFlip()
740{
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800741 const DisplayHardware& hw = graphicPlane(0).displayHardware();
742 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800744 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
745 const bool visibleRegions = lockPageFlip(currentLayers);
746
747 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 Region opaqueRegion;
749 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700750
751 /*
752 * rebuild the visible layer list
753 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800754 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700755 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700756 mVisibleLayersSortedByZ.setCapacity(count);
757 for (size_t i=0 ; i<count ; i++) {
758 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
759 mVisibleLayersSortedByZ.add(currentLayers[i]);
760 }
761
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 mWormholeRegion = screenRegion.subtract(opaqueRegion);
763 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800764 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 }
766
767 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700768
769 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 mDirtyRegion.andSelf(screenRegion);
771}
772
Mathias Agopianad456f92011-01-13 17:53:01 -0800773void SurfaceFlinger::invalidateHwcGeometry()
774{
775 mHwWorkListDirty = true;
776}
777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
779{
780 bool recomputeVisibleRegions = false;
781 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700782 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700784 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 layer->lockPageFlip(recomputeVisibleRegions);
786 }
787 return recomputeVisibleRegions;
788}
789
790void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
791{
792 const GraphicPlane& plane(graphicPlane(0));
793 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800794 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700795 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700797 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 layer->unlockPageFlip(planeTransform, mDirtyRegion);
799 }
800}
801
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800802void SurfaceFlinger::handleRefresh()
803{
804 bool needInvalidate = false;
805 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
806 const size_t count = currentLayers.size();
807 for (size_t i=0 ; i<count ; i++) {
808 const sp<LayerBase>& layer(currentLayers[i]);
809 if (layer->onPreComposition()) {
810 needInvalidate = true;
811 }
812 }
813 if (needInvalidate) {
814 signalLayerUpdate();
815 }
816}
817
818
Mathias Agopiana350ff92010-08-10 17:14:02 -0700819void SurfaceFlinger::handleWorkList()
820{
821 mHwWorkListDirty = false;
822 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
823 if (hwc.initCheck() == NO_ERROR) {
824 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
825 const size_t count = currentLayers.size();
826 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700827 hwc_layer_t* const cur(hwc.getLayers());
828 for (size_t i=0 ; cur && i<count ; i++) {
829 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700830 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700831 cur[i].compositionType = HWC_FRAMEBUFFER;
832 cur[i].flags |= HWC_SKIP_LAYER;
833 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700834 }
835 }
836}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700837
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838void SurfaceFlinger::handleRepaint()
839{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700840 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700841 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842
Glenn Kasten99ed2242011-12-15 09:51:17 -0800843 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844 debugFlashRegions();
845 }
846
Mathias Agopianb8a55602009-06-26 19:06:36 -0700847 // set the frame buffer
848 const DisplayHardware& hw(graphicPlane(0).displayHardware());
849 glMatrixMode(GL_MODELVIEW);
850 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851
852 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700853 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
854 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700855 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
857 // takes a rectangle, we must make sure to update that whole
858 // rectangle in that case
859 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700860 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700861 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700862 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700864 // in the BUFFER_PRESERVED case, obviously, we can update only
865 // what's needed and nothing more.
866 // NOTE: this is NOT a common case, as preserving the backbuffer
867 // is costly and usually involves copying the whole update back.
868 }
869 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700870 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700871 // We need to redraw the rectangle that will be updated
872 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700873 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700874 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700875 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700876 } else {
877 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700879 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 }
881 }
882
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700883 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 composeSurfaces(mDirtyRegion);
885
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700886 // update the swap region and clear the dirty region
887 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 mDirtyRegion.clear();
889}
890
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700891void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700893 const DisplayHardware& hw(graphicPlane(0).displayHardware());
894 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700895 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700896 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700897 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700898 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700899
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
901 size_t count = layers.size();
902
Steve Blocke6f43dd2012-01-06 19:20:56 +0000903 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700904 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
905 hwc.getNumLayers(), count);
906
907 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700908 if (hwc.initCheck() == NO_ERROR) {
909 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
910 }
Mathias Agopian45721772010-08-12 15:03:26 -0700911
912 /*
913 * update the per-frame h/w composer data for each layer
914 * and build the transparent region of the FB
915 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700916 for (size_t i=0 ; i<count ; i++) {
917 const sp<LayerBase>& layer(layers[i]);
918 layer->setPerFrameData(&cur[i]);
919 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700920 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700921 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000922 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700923
Mathias Agopianf384cc32011-09-08 18:31:55 -0700924 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700925 // what's happening here is tricky.
926 // we want to clear all the layers with the CLEAR_FB flags
927 // that are opaque.
928 // however, since some GPU are efficient at preserving
929 // the backbuffer, we want to take advantage of that so we do the
930 // clear only in the dirty region (other areas will be preserved
931 // on those GPUs).
932 // NOTE: on non backbuffer preserving GPU, the dirty region
933 // has already been expanded as needed, so the code is correct
934 // there too.
935 //
936 // However, the content of the framebuffer cannot be trusted when
937 // we switch to/from FB/OVERLAY, in which case we need to
938 // expand the dirty region to those areas too.
939 //
940 // Note also that there is a special case when switching from
941 // "no layers in FB" to "some layers in FB", where we need to redraw
942 // the entire FB, since some areas might contain uninitialized
943 // data.
944 //
945 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700946 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700947 // that is, we can't erase anything outside the dirty region.
948
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700949 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700950
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700951 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
952 transparent.set(hw.getBounds());
953 dirtyInOut = transparent;
954 } else {
955 for (size_t i=0 ; i<count ; i++) {
956 const sp<LayerBase>& layer(layers[i]);
957 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
958 transparent.orSelf(layer->visibleRegionScreen);
959 }
960 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
961 if (isOverlay != layer->isOverlay()) {
962 // we transitioned to/from overlay, so add this layer
963 // to the dirty region so the framebuffer can be either
964 // cleared or redrawn.
965 dirtyInOut.orSelf(layer->visibleRegionScreen);
966 }
967 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800968 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700969 // don't erase stuff outside the dirty region
970 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700971 }
972
973 /*
974 * clear the area of the FB that need to be transparent
975 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700976 if (!transparent.isEmpty()) {
977 glClearColor(0,0,0,0);
978 Region::const_iterator it = transparent.begin();
979 Region::const_iterator const end = transparent.end();
980 const int32_t height = hw.getHeight();
981 while (it != end) {
982 const Rect& r(*it++);
983 const GLint sy = height - (r.top + r.height());
984 glScissor(r.left, sy, r.width(), r.height());
985 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800986 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700987 }
988 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700989}
Mathias Agopian45721772010-08-12 15:03:26 -0700990
Mathias Agopianf384cc32011-09-08 18:31:55 -0700991void SurfaceFlinger::composeSurfaces(const Region& dirty)
992{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700993 const DisplayHardware& hw(graphicPlane(0).displayHardware());
994 HWComposer& hwc(hw.getHwComposer());
995
996 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800997 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700998 // should never happen unless the window manager has a bug
999 // draw something...
1000 drawWormhole();
1001 }
1002
Mathias Agopian45721772010-08-12 15:03:26 -07001003 /*
1004 * and then, render the layers targeted at the framebuffer
1005 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001006 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001007 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1008 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001009 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001010 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001011 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001012 }
1013 const sp<LayerBase>& layer(layers[i]);
1014 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1015 if (!clip.isEmpty()) {
1016 layer->draw(clip);
1017 }
1018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019}
1020
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021void SurfaceFlinger::debugFlashRegions()
1022{
Mathias Agopian0a917752010-06-14 21:20:00 -07001023 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1024 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001025 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001026 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001027 return;
1028 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001029
Mathias Agopian0a917752010-06-14 21:20:00 -07001030 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1031 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1032 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1033 mDirtyRegion.bounds() : hw.bounds());
1034 composeSurfaces(repaint);
1035 }
1036
Mathias Agopianc492e672011-10-18 14:49:27 -07001037 glDisable(GL_TEXTURE_EXTERNAL_OES);
1038 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 glDisable(GL_SCISSOR_TEST);
1041
Mathias Agopian0926f502009-05-04 14:17:04 -07001042 static int toggle = 0;
1043 toggle = 1 - toggle;
1044 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001045 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001046 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001047 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001048 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049
Mathias Agopian20f68782009-05-11 00:03:41 -07001050 Region::const_iterator it = mDirtyRegion.begin();
1051 Region::const_iterator const end = mDirtyRegion.end();
1052 while (it != end) {
1053 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001055 { r.left, height - r.top },
1056 { r.left, height - r.bottom },
1057 { r.right, height - r.bottom },
1058 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 };
1060 glVertexPointer(2, GL_FLOAT, 0, vertices);
1061 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1062 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001063
Mathias Agopian0656a682011-09-20 17:22:44 -07001064 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065
1066 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001067 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068
1069 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070}
1071
1072void SurfaceFlinger::drawWormhole() const
1073{
1074 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1075 if (region.isEmpty())
1076 return;
1077
1078 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1079 const int32_t width = hw.getWidth();
1080 const int32_t height = hw.getHeight();
1081
Glenn Kasten99ed2242011-12-15 09:51:17 -08001082 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001083 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001084 Region::const_iterator it = region.begin();
1085 Region::const_iterator const end = region.end();
1086 while (it != end) {
1087 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 const GLint sy = height - (r.top + r.height());
1089 glScissor(r.left, sy, r.width(), r.height());
1090 glClear(GL_COLOR_BUFFER_BIT);
1091 }
1092 } else {
1093 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1094 { width, height }, { 0, height } };
1095 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 glVertexPointer(2, GL_SHORT, 0, vertices);
1098 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1099 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001100
1101 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 glEnable(GL_TEXTURE_2D);
1103 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1104 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1105 glMatrixMode(GL_TEXTURE);
1106 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001107
1108 glDisable(GL_BLEND);
1109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001111 Region::const_iterator it = region.begin();
1112 Region::const_iterator const end = region.end();
1113 while (it != end) {
1114 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 const GLint sy = height - (r.top + r.height());
1116 glScissor(r.left, sy, r.width(), r.height());
1117 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1118 }
1119 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001120 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001121 glLoadIdentity();
1122 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123 }
1124}
1125
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001126status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127{
1128 Mutex::Autolock _l(mStateLock);
1129 addLayer_l(layer);
1130 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1131 return NO_ERROR;
1132}
1133
Mathias Agopian96f08192010-06-02 23:28:45 -07001134status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1135{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001136 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001137 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1138}
1139
1140ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1141 const sp<LayerBaseClient>& lbc)
1142{
Mathias Agopian96f08192010-06-02 23:28:45 -07001143 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001144 size_t name = client->attachLayer(lbc);
1145
1146 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001147
1148 // add this layer to the current state list
1149 addLayer_l(lbc);
1150
Mathias Agopian4f113742011-05-03 16:21:41 -07001151 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001152}
1153
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001154status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155{
1156 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001157 status_t err = purgatorizeLayer_l(layer);
1158 if (err == NO_ERROR)
1159 setTransactionFlags(eTransactionNeeded);
1160 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161}
1162
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001163status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001165 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1166 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001167 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1170 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001171 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172 return NO_ERROR;
1173 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001174 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175}
1176
Mathias Agopian9a112062009-04-17 19:36:26 -07001177status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1178{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001179 // First add the layer to the purgatory list, which makes sure it won't
1180 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001181 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001182 if (err >= 0) {
1183 mLayerPurgatory.add(layerBase);
1184 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001185
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001186 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001187
Mathias Agopian3d579642009-06-04 18:46:21 -07001188 // it's possible that we don't find a layer, because it might
1189 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001190 // from the user because there is a race between Client::destroySurface(),
1191 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001192 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1193}
1194
Mathias Agopian96f08192010-06-02 23:28:45 -07001195status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196{
Mathias Agopian96f08192010-06-02 23:28:45 -07001197 layer->forceVisibilityTransaction();
1198 setTransactionFlags(eTraversalNeeded);
1199 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200}
1201
Mathias Agopiandea20b12011-05-03 17:04:02 -07001202uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1203{
1204 return android_atomic_release_load(&mTransactionFlags);
1205}
1206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1208{
1209 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1210}
1211
Mathias Agopianbb641242010-05-18 17:06:55 -07001212uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213{
1214 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1215 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001216 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217 }
1218 return old;
1219}
1220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001222void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001223 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001224 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001225
Jamie Gennis28378392011-10-12 17:39:00 -07001226 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001227 if (mCurrentState.orientation != orientation) {
1228 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1229 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001230 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001231 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001232 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001233 orientation);
1234 }
1235 }
1236
Mathias Agopian698c0872011-06-28 19:09:31 -07001237 const size_t count = state.size();
1238 for (size_t i=0 ; i<count ; i++) {
1239 const ComposerState& s(state[i]);
1240 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001241 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001242 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001243
Mathias Agopian386aa982011-11-07 21:58:03 -08001244 if (transactionFlags) {
1245 // this triggers the transaction
1246 setTransactionFlags(transactionFlags);
1247
1248 // if this is a synchronous transaction, wait for it to take effect
1249 // before returning.
1250 if (flags & eSynchronous) {
1251 mTransationPending = true;
1252 }
1253 while (mTransationPending) {
1254 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1255 if (CC_UNLIKELY(err != NO_ERROR)) {
1256 // just in case something goes wrong in SF, return to the
1257 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001258 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001259 mTransationPending = false;
1260 break;
1261 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001262 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 }
1264}
1265
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001266sp<ISurface> SurfaceFlinger::createSurface(
1267 ISurfaceComposerClient::surface_data_t* params,
1268 const String8& name,
1269 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1271 uint32_t flags)
1272{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001273 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001274 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001275
1276 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001277 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001278 int(w), int(h));
1279 return surfaceHandle;
1280 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001281
Steve Block9d453682011-12-20 16:23:08 +00001282 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001283 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 switch (flags & eFXSurfaceMask) {
1285 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001286 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1287 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 break;
1289 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001290 // for now we treat Blur as Dim, until we can implement it
1291 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001293 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001295 case eFXSurfaceScreenshot:
1296 layer = createScreenshotSurface(client, d, w, h, flags);
1297 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 }
1299
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001300 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001301 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001302 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001303 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001306 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001307 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001308 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001309 if (normalLayer != 0) {
1310 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001311 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001312 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001313 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001314
Mathias Agopian96f08192010-06-02 23:28:45 -07001315 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 }
1317
1318 return surfaceHandle;
1319}
1320
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001321sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001322 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001323 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001324 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325{
1326 // initialize the surfaces
1327 switch (format) { // TODO: take h/w into account
1328 case PIXEL_FORMAT_TRANSPARENT:
1329 case PIXEL_FORMAT_TRANSLUCENT:
1330 format = PIXEL_FORMAT_RGBA_8888;
1331 break;
1332 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001333#ifdef NO_RGBX_8888
1334 format = PIXEL_FORMAT_RGB_565;
1335#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001336 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001337#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 break;
1339 }
1340
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001341#ifdef NO_RGBX_8888
1342 if (format == PIXEL_FORMAT_RGBX_8888)
1343 format = PIXEL_FORMAT_RGBA_8888;
1344#endif
1345
Mathias Agopian96f08192010-06-02 23:28:45 -07001346 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001347 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001348 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001349 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001350 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 }
1352 return layer;
1353}
1354
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001355sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001356 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001357 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358{
Mathias Agopian96f08192010-06-02 23:28:45 -07001359 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001360 return layer;
1361}
1362
1363sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1364 const sp<Client>& client, DisplayID display,
1365 uint32_t w, uint32_t h, uint32_t flags)
1366{
1367 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 return layer;
1369}
1370
Mathias Agopian96f08192010-06-02 23:28:45 -07001371status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372{
Mathias Agopian9a112062009-04-17 19:36:26 -07001373 /*
1374 * called by the window manager, when a surface should be marked for
1375 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001376 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001377 * The surface is removed from the current and drawing lists, but placed
1378 * in the purgatory queue, so it's not destroyed right-away (we need
1379 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001380 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001381
Mathias Agopian48d819a2009-09-10 19:41:18 -07001382 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001383 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001384 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001385 if (layer != 0) {
1386 err = purgatorizeLayer_l(layer);
1387 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001388 setTransactionFlags(eTransactionNeeded);
1389 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001390 }
1391 return err;
1392}
1393
Mathias Agopianca4d3602011-05-19 15:38:14 -07001394status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001395{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001396 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001397 status_t err = NO_ERROR;
1398 sp<LayerBaseClient> l(layer.promote());
1399 if (l != NULL) {
1400 Mutex::Autolock _l(mStateLock);
1401 err = removeLayer_l(l);
1402 if (err == NAME_NOT_FOUND) {
1403 // The surface wasn't in the current list, which means it was
1404 // removed already, which means it is in the purgatory,
1405 // and need to be removed from there.
1406 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001407 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001408 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001409 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001410 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001411 "error removing layer=%p (%s)", l.get(), strerror(-err));
1412 }
1413 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414}
1415
Mathias Agopian698c0872011-06-28 19:09:31 -07001416uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001417 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001418 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001421 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1422 if (layer != 0) {
1423 const uint32_t what = s.what;
1424 if (what & ePositionChanged) {
1425 if (layer->setPosition(s.x, s.y))
1426 flags |= eTraversalNeeded;
1427 }
1428 if (what & eLayerChanged) {
1429 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1430 if (layer->setLayer(s.z)) {
1431 mCurrentState.layersSortedByZ.removeAt(idx);
1432 mCurrentState.layersSortedByZ.add(layer);
1433 // we need traversal (state changed)
1434 // AND transaction (list changed)
1435 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 }
1437 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001438 if (what & eSizeChanged) {
1439 if (layer->setSize(s.w, s.h)) {
1440 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001441 }
1442 }
1443 if (what & eAlphaChanged) {
1444 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1445 flags |= eTraversalNeeded;
1446 }
1447 if (what & eMatrixChanged) {
1448 if (layer->setMatrix(s.matrix))
1449 flags |= eTraversalNeeded;
1450 }
1451 if (what & eTransparentRegionChanged) {
1452 if (layer->setTransparentRegionHint(s.transparentRegion))
1453 flags |= eTraversalNeeded;
1454 }
1455 if (what & eVisibilityChanged) {
1456 if (layer->setFlags(s.flags, s.mask))
1457 flags |= eTraversalNeeded;
1458 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001460 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461}
1462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463void SurfaceFlinger::screenReleased(int dpy)
1464{
1465 // this may be called by a signal handler, we can't do too much in here
1466 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001467 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
1470void SurfaceFlinger::screenAcquired(int dpy)
1471{
1472 // this may be called by a signal handler, we can't do too much in here
1473 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001474 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475}
1476
1477status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1478{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001479 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 char buffer[SIZE];
1481 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001482
1483 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 snprintf(buffer, SIZE, "Permission Denial: "
1485 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1486 IPCThreadState::self()->getCallingPid(),
1487 IPCThreadState::self()->getCallingUid());
1488 result.append(buffer);
1489 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001490 // Try to get the main lock, but don't insist if we can't
1491 // (this would indicate SF is stuck, but we want to be able to
1492 // print something in dumpsys).
1493 int retry = 3;
1494 while (mStateLock.tryLock()<0 && --retry>=0) {
1495 usleep(1000000);
1496 }
1497 const bool locked(retry >= 0);
1498 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001499 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001500 "SurfaceFlinger appears to be unresponsive, "
1501 "dumping anyways (no locks held)\n");
1502 result.append(buffer);
1503 }
1504
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001505 bool dumpAll = true;
1506 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001507 size_t numArgs = args.size();
1508 if (numArgs) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001509 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001510
1511 if ((index < numArgs) &&
1512 (args[index] == String16("--list"))) {
1513 index++;
1514 listLayersLocked(args, index, result, buffer, SIZE);
1515 }
1516
1517 if ((index < numArgs) &&
1518 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001519 index++;
1520 dumpStatsLocked(args, index, result, buffer, SIZE);
1521 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001522
1523 if ((index < numArgs) &&
1524 (args[index] == String16("--latency-clear"))) {
1525 index++;
1526 clearStatsLocked(args, index, result, buffer, SIZE);
1527 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001529
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001530 if (dumpAll) {
1531 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001532 }
1533
Mathias Agopian9795c422009-08-26 16:36:26 -07001534 if (locked) {
1535 mStateLock.unlock();
1536 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 }
1538 write(fd, result.string(), result.size());
1539 return NO_ERROR;
1540}
1541
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001542void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1543 String8& result, char* buffer, size_t SIZE) const
1544{
1545 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1546 const size_t count = currentLayers.size();
1547 for (size_t i=0 ; i<count ; i++) {
1548 const sp<LayerBase>& layer(currentLayers[i]);
1549 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1550 result.append(buffer);
1551 }
1552}
1553
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001554void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1555 String8& result, char* buffer, size_t SIZE) const
1556{
1557 String8 name;
1558 if (index < args.size()) {
1559 name = String8(args[index]);
1560 index++;
1561 }
1562
1563 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1564 const size_t count = currentLayers.size();
1565 for (size_t i=0 ; i<count ; i++) {
1566 const sp<LayerBase>& layer(currentLayers[i]);
1567 if (name.isEmpty()) {
1568 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1569 result.append(buffer);
1570 }
1571 if (name.isEmpty() || (name == layer->getName())) {
1572 layer->dumpStats(result, buffer, SIZE);
1573 }
1574 }
1575}
1576
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001577void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1578 String8& result, char* buffer, size_t SIZE) const
1579{
1580 String8 name;
1581 if (index < args.size()) {
1582 name = String8(args[index]);
1583 index++;
1584 }
1585
1586 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1587 const size_t count = currentLayers.size();
1588 for (size_t i=0 ; i<count ; i++) {
1589 const sp<LayerBase>& layer(currentLayers[i]);
1590 if (name.isEmpty() || (name == layer->getName())) {
1591 layer->clearStats();
1592 }
1593 }
1594}
1595
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001596void SurfaceFlinger::dumpAllLocked(
1597 String8& result, char* buffer, size_t SIZE) const
1598{
1599 // figure out if we're stuck somewhere
1600 const nsecs_t now = systemTime();
1601 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1602 const nsecs_t inTransaction(mDebugInTransaction);
1603 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1604 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1605
1606 /*
1607 * Dump the visible layer list
1608 */
1609 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1610 const size_t count = currentLayers.size();
1611 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1612 result.append(buffer);
1613 for (size_t i=0 ; i<count ; i++) {
1614 const sp<LayerBase>& layer(currentLayers[i]);
1615 layer->dump(result, buffer, SIZE);
1616 }
1617
1618 /*
1619 * Dump the layers in the purgatory
1620 */
1621
1622 const size_t purgatorySize = mLayerPurgatory.size();
1623 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1624 result.append(buffer);
1625 for (size_t i=0 ; i<purgatorySize ; i++) {
1626 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1627 layer->shortDump(result, buffer, SIZE);
1628 }
1629
1630 /*
1631 * Dump SurfaceFlinger global state
1632 */
1633
1634 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1635 result.append(buffer);
1636
1637 const GLExtensions& extensions(GLExtensions::getInstance());
1638 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1639 extensions.getVendor(),
1640 extensions.getRenderer(),
1641 extensions.getVersion());
1642 result.append(buffer);
1643
1644 snprintf(buffer, SIZE, "EGL : %s\n",
1645 eglQueryString(graphicPlane(0).getEGLDisplay(),
1646 EGL_VERSION_HW_ANDROID));
1647 result.append(buffer);
1648
1649 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1650 result.append(buffer);
1651
1652 mWormholeRegion.dump(result, "WormholeRegion");
1653 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1654 snprintf(buffer, SIZE,
1655 " orientation=%d, canDraw=%d\n",
1656 mCurrentState.orientation, hw.canDraw());
1657 result.append(buffer);
1658 snprintf(buffer, SIZE,
1659 " last eglSwapBuffers() time: %f us\n"
1660 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001661 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001662 " refresh-rate : %f fps\n"
1663 " x-dpi : %f\n"
1664 " y-dpi : %f\n",
1665 mLastSwapBufferTime/1000.0,
1666 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001667 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001668 hw.getRefreshRate(),
1669 hw.getDpiX(),
1670 hw.getDpiY());
1671 result.append(buffer);
1672
1673 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1674 inSwapBuffersDuration/1000.0);
1675 result.append(buffer);
1676
1677 snprintf(buffer, SIZE, " transaction time: %f us\n",
1678 inTransactionDuration/1000.0);
1679 result.append(buffer);
1680
1681 /*
1682 * VSYNC state
1683 */
1684 mEventThread->dump(result, buffer, SIZE);
1685
1686 /*
1687 * Dump HWComposer state
1688 */
1689 HWComposer& hwc(hw.getHwComposer());
1690 snprintf(buffer, SIZE, "h/w composer state:\n");
1691 result.append(buffer);
1692 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1693 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1694 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1695 result.append(buffer);
1696 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1697
1698 /*
1699 * Dump gralloc state
1700 */
1701 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1702 alloc.dump(result);
1703 hw.dump(result);
1704}
1705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706status_t SurfaceFlinger::onTransact(
1707 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1708{
1709 switch (code) {
1710 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001711 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001714 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001715 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 {
1717 // codes that require permission check
1718 IPCThreadState* ipc = IPCThreadState::self();
1719 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001720 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001721 if ((uid != AID_GRAPHICS) &&
1722 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001723 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001724 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1725 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001727 break;
1728 }
1729 case CAPTURE_SCREEN:
1730 {
1731 // codes that require permission check
1732 IPCThreadState* ipc = IPCThreadState::self();
1733 const int pid = ipc->getCallingPid();
1734 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001735 if ((uid != AID_GRAPHICS) &&
1736 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001737 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001738 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1739 return PERMISSION_DENIED;
1740 }
1741 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 }
1743 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1746 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001747 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001748 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001749 IPCThreadState* ipc = IPCThreadState::self();
1750 const int pid = ipc->getCallingPid();
1751 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001752 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001753 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 return PERMISSION_DENIED;
1755 }
1756 int n;
1757 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001758 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001759 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 return NO_ERROR;
1761 case 1002: // SHOW_UPDATES
1762 n = data.readInt32();
1763 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001764 invalidateHwcGeometry();
1765 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 return NO_ERROR;
1767 case 1003: // SHOW_BACKGROUND
1768 n = data.readInt32();
1769 mDebugBackground = n ? 1 : 0;
1770 return NO_ERROR;
1771 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001772 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001773 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001775 case 1005:{ // force transaction
1776 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1777 return NO_ERROR;
1778 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001779 case 1008: // toggle use of hw composer
1780 n = data.readInt32();
1781 mDebugDisableHWC = n ? 1 : 0;
1782 invalidateHwcGeometry();
1783 repaintEverything();
1784 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001785 case 1009: // toggle use of transform hint
1786 n = data.readInt32();
1787 mDebugDisableTransformHint = n ? 1 : 0;
1788 invalidateHwcGeometry();
1789 repaintEverything();
1790 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001792 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 reply->writeInt32(0);
1794 reply->writeInt32(mDebugRegion);
1795 reply->writeInt32(mDebugBackground);
1796 return NO_ERROR;
1797 case 1013: {
1798 Mutex::Autolock _l(mStateLock);
1799 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1800 reply->writeInt32(hw.getPageFlipCount());
1801 }
1802 return NO_ERROR;
1803 }
1804 }
1805 return err;
1806}
1807
Mathias Agopian53331da2011-08-22 21:44:41 -07001808void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001809 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001810 const Rect bounds(hw.getBounds());
1811 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001812 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001813}
1814
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001815void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1816 Mutex::Autolock _l(mInvalidateLock);
1817 mInvalidateRegion = reg;
1818}
1819
1820Region SurfaceFlinger::getAndClearInvalidateRegion() {
1821 Mutex::Autolock _l(mInvalidateLock);
1822 Region reg(mInvalidateRegion);
1823 mInvalidateRegion.clear();
1824 return reg;
1825}
1826
Mathias Agopian59119e62010-10-11 12:37:43 -07001827// ---------------------------------------------------------------------------
1828
Mathias Agopian118d0242011-10-13 16:02:48 -07001829status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1830 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1831{
1832 Mutex::Autolock _l(mStateLock);
1833 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1834}
1835
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001836status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1837 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001838{
Mathias Agopian59119e62010-10-11 12:37:43 -07001839 if (!GLExtensions::getInstance().haveFramebufferObject())
1840 return INVALID_OPERATION;
1841
1842 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001843 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001844 const uint32_t hw_w = hw.getWidth();
1845 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001846 GLfloat u = 1;
1847 GLfloat v = 1;
1848
1849 // make sure to clear all GL error flags
1850 while ( glGetError() != GL_NO_ERROR ) ;
1851
1852 // create a FBO
1853 GLuint name, tname;
1854 glGenTextures(1, &tname);
1855 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001856 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1857 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001858 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001859 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001860 GLint tw = (2 << (31 - clz(hw_w)));
1861 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001862 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1863 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001864 u = GLfloat(hw_w) / tw;
1865 v = GLfloat(hw_h) / th;
1866 }
1867 glGenFramebuffersOES(1, &name);
1868 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001869 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1870 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001871
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001872 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001873 glDisable(GL_TEXTURE_EXTERNAL_OES);
1874 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001875 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001876 glClearColor(0,0,0,1);
1877 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001878 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001879 glMatrixMode(GL_MODELVIEW);
1880 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001881 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1882 const size_t count = layers.size();
1883 for (size_t i=0 ; i<count ; ++i) {
1884 const sp<LayerBase>& layer(layers[i]);
1885 layer->drawForSreenShot();
1886 }
1887
Mathias Agopian118d0242011-10-13 16:02:48 -07001888 hw.compositionComplete();
1889
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001890 // back to main framebuffer
1891 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1892 glDisable(GL_SCISSOR_TEST);
1893 glDeleteFramebuffersOES(1, &name);
1894
1895 *textureName = tname;
1896 *uOut = u;
1897 *vOut = v;
1898 return NO_ERROR;
1899}
1900
1901// ---------------------------------------------------------------------------
1902
1903status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1904{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001905 // get screen geometry
1906 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1907 const uint32_t hw_w = hw.getWidth();
1908 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001909 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001910
1911 GLfloat u, v;
1912 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001913 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001914 if (result != NO_ERROR) {
1915 return result;
1916 }
1917
1918 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001919 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001920 glBindTexture(GL_TEXTURE_2D, tname);
1921 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1922 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1923 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1924 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1925 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1926 glVertexPointer(2, GL_FLOAT, 0, vtx);
1927
Mathias Agopianffcf4652011-07-07 17:30:31 -07001928 /*
1929 * Texture coordinate mapping
1930 *
1931 * u
1932 * 1 +----------+---+
1933 * | | | | image is inverted
1934 * | V | | w.r.t. the texture
1935 * 1-v +----------+ | coordinates
1936 * | |
1937 * | |
1938 * | |
1939 * 0 +--------------+
1940 * 0 1
1941 *
1942 */
1943
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001944 class s_curve_interpolator {
1945 const float nbFrames, s, v;
1946 public:
1947 s_curve_interpolator(int nbFrames, float s)
1948 : nbFrames(1.0f / (nbFrames-1)), s(s),
1949 v(1.0f + expf(-s + 0.5f*s)) {
1950 }
1951 float operator()(int f) {
1952 const float x = f * nbFrames;
1953 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1954 }
1955 };
1956
1957 class v_stretch {
1958 const GLfloat hw_w, hw_h;
1959 public:
1960 v_stretch(uint32_t hw_w, uint32_t hw_h)
1961 : hw_w(hw_w), hw_h(hw_h) {
1962 }
1963 void operator()(GLfloat* vtx, float v) {
1964 const GLfloat w = hw_w + (hw_w * v);
1965 const GLfloat h = hw_h - (hw_h * v);
1966 const GLfloat x = (hw_w - w) * 0.5f;
1967 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001968 vtx[0] = x; vtx[1] = y;
1969 vtx[2] = x; vtx[3] = y + h;
1970 vtx[4] = x + w; vtx[5] = y + h;
1971 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 }
1973 };
1974
1975 class h_stretch {
1976 const GLfloat hw_w, hw_h;
1977 public:
1978 h_stretch(uint32_t hw_w, uint32_t hw_h)
1979 : hw_w(hw_w), hw_h(hw_h) {
1980 }
1981 void operator()(GLfloat* vtx, float v) {
1982 const GLfloat w = hw_w - (hw_w * v);
1983 const GLfloat h = 1.0f;
1984 const GLfloat x = (hw_w - w) * 0.5f;
1985 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001986 vtx[0] = x; vtx[1] = y;
1987 vtx[2] = x; vtx[3] = y + h;
1988 vtx[4] = x + w; vtx[5] = y + h;
1989 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 }
1991 };
1992
1993 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001994 char value[PROPERTY_VALUE_MAX];
1995 property_get("debug.sf.electron_frames", value, "24");
1996 int nbFrames = (atoi(value) + 1) >> 1;
1997 if (nbFrames <= 0) // just in case
1998 nbFrames = 24;
1999
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002000 s_curve_interpolator itr(nbFrames, 7.5f);
2001 s_curve_interpolator itg(nbFrames, 8.0f);
2002 s_curve_interpolator itb(nbFrames, 8.5f);
2003
2004 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002005
2006 glMatrixMode(GL_TEXTURE);
2007 glLoadIdentity();
2008 glMatrixMode(GL_MODELVIEW);
2009 glLoadIdentity();
2010
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002011 glEnable(GL_BLEND);
2012 glBlendFunc(GL_ONE, GL_ONE);
2013 for (int i=0 ; i<nbFrames ; i++) {
2014 float x, y, w, h;
2015 const float vr = itr(i);
2016 const float vg = itg(i);
2017 const float vb = itb(i);
2018
2019 // clear screen
2020 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002021 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002022 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002023
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002024 // draw the red plane
2025 vverts(vtx, vr);
2026 glColorMask(1,0,0,1);
2027 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002028
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002029 // draw the green plane
2030 vverts(vtx, vg);
2031 glColorMask(0,1,0,1);
2032 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002033
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034 // draw the blue plane
2035 vverts(vtx, vb);
2036 glColorMask(0,0,1,1);
2037 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002038
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002040 glDisable(GL_TEXTURE_2D);
2041 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002042 glColor4f(vg, vg, vg, 1);
2043 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2044 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002045 }
2046
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 h_stretch hverts(hw_w, hw_h);
2048 glDisable(GL_BLEND);
2049 glDisable(GL_TEXTURE_2D);
2050 glColorMask(1,1,1,1);
2051 for (int i=0 ; i<nbFrames ; i++) {
2052 const float v = itg(i);
2053 hverts(vtx, v);
2054 glClear(GL_COLOR_BUFFER_BIT);
2055 glColor4f(1-v, 1-v, 1-v, 1);
2056 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2057 hw.flip(screenBounds);
2058 }
2059
2060 glColorMask(1,1,1,1);
2061 glEnable(GL_SCISSOR_TEST);
2062 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2063 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002064 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002065 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002066 return NO_ERROR;
2067}
2068
2069status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2070{
2071 status_t result = PERMISSION_DENIED;
2072
2073 if (!GLExtensions::getInstance().haveFramebufferObject())
2074 return INVALID_OPERATION;
2075
2076
2077 // get screen geometry
2078 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2079 const uint32_t hw_w = hw.getWidth();
2080 const uint32_t hw_h = hw.getHeight();
2081 const Region screenBounds(hw.bounds());
2082
2083 GLfloat u, v;
2084 GLuint tname;
2085 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2086 if (result != NO_ERROR) {
2087 return result;
2088 }
2089
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002090 GLfloat vtx[8];
2091 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002092 glBindTexture(GL_TEXTURE_2D, tname);
2093 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2094 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2095 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2096 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2097 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2098 glVertexPointer(2, GL_FLOAT, 0, vtx);
2099
2100 class s_curve_interpolator {
2101 const float nbFrames, s, v;
2102 public:
2103 s_curve_interpolator(int nbFrames, float s)
2104 : nbFrames(1.0f / (nbFrames-1)), s(s),
2105 v(1.0f + expf(-s + 0.5f*s)) {
2106 }
2107 float operator()(int f) {
2108 const float x = f * nbFrames;
2109 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2110 }
2111 };
2112
2113 class v_stretch {
2114 const GLfloat hw_w, hw_h;
2115 public:
2116 v_stretch(uint32_t hw_w, uint32_t hw_h)
2117 : hw_w(hw_w), hw_h(hw_h) {
2118 }
2119 void operator()(GLfloat* vtx, float v) {
2120 const GLfloat w = hw_w + (hw_w * v);
2121 const GLfloat h = hw_h - (hw_h * v);
2122 const GLfloat x = (hw_w - w) * 0.5f;
2123 const GLfloat y = (hw_h - h) * 0.5f;
2124 vtx[0] = x; vtx[1] = y;
2125 vtx[2] = x; vtx[3] = y + h;
2126 vtx[4] = x + w; vtx[5] = y + h;
2127 vtx[6] = x + w; vtx[7] = y;
2128 }
2129 };
2130
2131 class h_stretch {
2132 const GLfloat hw_w, hw_h;
2133 public:
2134 h_stretch(uint32_t hw_w, uint32_t hw_h)
2135 : hw_w(hw_w), hw_h(hw_h) {
2136 }
2137 void operator()(GLfloat* vtx, float v) {
2138 const GLfloat w = hw_w - (hw_w * v);
2139 const GLfloat h = 1.0f;
2140 const GLfloat x = (hw_w - w) * 0.5f;
2141 const GLfloat y = (hw_h - h) * 0.5f;
2142 vtx[0] = x; vtx[1] = y;
2143 vtx[2] = x; vtx[3] = y + h;
2144 vtx[4] = x + w; vtx[5] = y + h;
2145 vtx[6] = x + w; vtx[7] = y;
2146 }
2147 };
2148
Mathias Agopiana6546e52010-10-14 12:33:07 -07002149 // the full animation is 12 frames
2150 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002151 s_curve_interpolator itr(nbFrames, 7.5f);
2152 s_curve_interpolator itg(nbFrames, 8.0f);
2153 s_curve_interpolator itb(nbFrames, 8.5f);
2154
2155 h_stretch hverts(hw_w, hw_h);
2156 glDisable(GL_BLEND);
2157 glDisable(GL_TEXTURE_2D);
2158 glColorMask(1,1,1,1);
2159 for (int i=nbFrames-1 ; i>=0 ; i--) {
2160 const float v = itg(i);
2161 hverts(vtx, v);
2162 glClear(GL_COLOR_BUFFER_BIT);
2163 glColor4f(1-v, 1-v, 1-v, 1);
2164 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2165 hw.flip(screenBounds);
2166 }
2167
Mathias Agopiana6546e52010-10-14 12:33:07 -07002168 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169 v_stretch vverts(hw_w, hw_h);
2170 glEnable(GL_BLEND);
2171 glBlendFunc(GL_ONE, GL_ONE);
2172 for (int i=nbFrames-1 ; i>=0 ; i--) {
2173 float x, y, w, h;
2174 const float vr = itr(i);
2175 const float vg = itg(i);
2176 const float vb = itb(i);
2177
2178 // clear screen
2179 glColorMask(1,1,1,1);
2180 glClear(GL_COLOR_BUFFER_BIT);
2181 glEnable(GL_TEXTURE_2D);
2182
2183 // draw the red plane
2184 vverts(vtx, vr);
2185 glColorMask(1,0,0,1);
2186 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2187
2188 // draw the green plane
2189 vverts(vtx, vg);
2190 glColorMask(0,1,0,1);
2191 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2192
2193 // draw the blue plane
2194 vverts(vtx, vb);
2195 glColorMask(0,0,1,1);
2196 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2197
2198 hw.flip(screenBounds);
2199 }
2200
2201 glColorMask(1,1,1,1);
2202 glEnable(GL_SCISSOR_TEST);
2203 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002204 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002205 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002206 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002207
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002208 return NO_ERROR;
2209}
2210
2211// ---------------------------------------------------------------------------
2212
Mathias Agopianabd671a2010-10-14 14:54:06 -07002213status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002214{
2215 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2216 if (!hw.canDraw()) {
2217 // we're already off
2218 return NO_ERROR;
2219 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002220
2221 // turn off hwc while we're doing the animation
2222 hw.getHwComposer().disable();
2223 // and make sure to turn it back on (if needed) next time we compose
2224 invalidateHwcGeometry();
2225
Mathias Agopianabd671a2010-10-14 14:54:06 -07002226 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2227 electronBeamOffAnimationImplLocked();
2228 }
2229
2230 // always clear the whole screen at the end of the animation
2231 glClearColor(0,0,0,1);
2232 glDisable(GL_SCISSOR_TEST);
2233 glClear(GL_COLOR_BUFFER_BIT);
2234 glEnable(GL_SCISSOR_TEST);
2235 hw.flip( Region(hw.bounds()) );
2236
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002237 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002238}
2239
2240status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2241{
Mathias Agopian59119e62010-10-11 12:37:43 -07002242 class MessageTurnElectronBeamOff : public MessageBase {
2243 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002244 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002245 status_t result;
2246 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002247 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2248 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002249 }
2250 status_t getResult() const {
2251 return result;
2252 }
2253 virtual bool handler() {
2254 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002256 return true;
2257 }
2258 };
2259
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002261 status_t res = postMessageSync(msg);
2262 if (res == NO_ERROR) {
2263 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264
2265 // work-around: when the power-manager calls us we activate the
2266 // animation. eventually, the "on" animation will be called
2267 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002268 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002269 }
2270 return res;
2271}
2272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002273// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002274
Mathias Agopianabd671a2010-10-14 14:54:06 -07002275status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002276{
2277 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2278 if (hw.canDraw()) {
2279 // we're already on
2280 return NO_ERROR;
2281 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002282 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2283 electronBeamOnAnimationImplLocked();
2284 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002285
2286 // make sure to redraw the whole screen when the animation is done
2287 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002288 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002289
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002290 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002291}
2292
2293status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2294{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002295 class MessageTurnElectronBeamOn : public MessageBase {
2296 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002297 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002298 status_t result;
2299 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2301 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002302 }
2303 status_t getResult() const {
2304 return result;
2305 }
2306 virtual bool handler() {
2307 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002308 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002309 return true;
2310 }
2311 };
2312
Mathias Agopianabd671a2010-10-14 14:54:06 -07002313 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002314 return NO_ERROR;
2315}
2316
2317// ---------------------------------------------------------------------------
2318
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2320 sp<IMemoryHeap>* heap,
2321 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002322 uint32_t sw, uint32_t sh,
2323 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324{
2325 status_t result = PERMISSION_DENIED;
2326
2327 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002328 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 return BAD_VALUE;
2330
2331 if (!GLExtensions::getInstance().haveFramebufferObject())
2332 return INVALID_OPERATION;
2333
2334 // get screen geometry
2335 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2336 const uint32_t hw_w = hw.getWidth();
2337 const uint32_t hw_h = hw.getHeight();
2338
2339 if ((sw > hw_w) || (sh > hw_h))
2340 return BAD_VALUE;
2341
2342 sw = (!sw) ? hw_w : sw;
2343 sh = (!sh) ? hw_h : sh;
2344 const size_t size = sw * sh * 4;
2345
Steve Block9d453682011-12-20 16:23:08 +00002346 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002347 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002348
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 // make sure to clear all GL error flags
2350 while ( glGetError() != GL_NO_ERROR ) ;
2351
2352 // create a FBO
2353 GLuint name, tname;
2354 glGenRenderbuffersOES(1, &tname);
2355 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2356 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2357 glGenFramebuffersOES(1, &name);
2358 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2359 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2360 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2361
2362 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002363
Mathias Agopian74c40c02010-09-29 13:02:36 -07002364 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2365
2366 // invert everything, b/c glReadPixel() below will invert the FB
2367 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002368 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002369 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002370 glMatrixMode(GL_PROJECTION);
2371 glPushMatrix();
2372 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002373 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 glMatrixMode(GL_MODELVIEW);
2375
2376 // redraw the screen entirely...
2377 glClearColor(0,0,0,1);
2378 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002379
Jamie Gennis9575f602011-10-07 14:51:16 -07002380 const LayerVector& layers(mDrawingState.layersSortedByZ);
2381 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002382 for (size_t i=0 ; i<count ; ++i) {
2383 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002384 const uint32_t flags = layer->drawingState().flags;
2385 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2386 const uint32_t z = layer->drawingState().z;
2387 if (z >= minLayerZ && z <= maxLayerZ) {
2388 layer->drawForSreenShot();
2389 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002390 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002391 }
2392
2393 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002394 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002395 glScissor(0, 0, sw, sh);
2396
2397 // check for errors and return screen capture
2398 if (glGetError() != GL_NO_ERROR) {
2399 // error while rendering
2400 result = INVALID_OPERATION;
2401 } else {
2402 // allocate shared memory large enough to hold the
2403 // screen capture
2404 sp<MemoryHeapBase> base(
2405 new MemoryHeapBase(size, 0, "screen-capture") );
2406 void* const ptr = base->getBase();
2407 if (ptr) {
2408 // capture the screen with glReadPixels()
2409 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2410 if (glGetError() == GL_NO_ERROR) {
2411 *heap = base;
2412 *w = sw;
2413 *h = sh;
2414 *f = PIXEL_FORMAT_RGBA_8888;
2415 result = NO_ERROR;
2416 }
2417 } else {
2418 result = NO_MEMORY;
2419 }
2420 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002421 glEnable(GL_SCISSOR_TEST);
2422 glViewport(0, 0, hw_w, hw_h);
2423 glMatrixMode(GL_PROJECTION);
2424 glPopMatrix();
2425 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002426 } else {
2427 result = BAD_VALUE;
2428 }
2429
2430 // release FBO resources
2431 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2432 glDeleteRenderbuffersOES(1, &tname);
2433 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002434
2435 hw.compositionComplete();
2436
Steve Block9d453682011-12-20 16:23:08 +00002437 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002438
Mathias Agopian74c40c02010-09-29 13:02:36 -07002439 return result;
2440}
2441
2442
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2444 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002445 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002446 uint32_t sw, uint32_t sh,
2447 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002448{
2449 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002450 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002451 return BAD_VALUE;
2452
2453 if (!GLExtensions::getInstance().haveFramebufferObject())
2454 return INVALID_OPERATION;
2455
2456 class MessageCaptureScreen : public MessageBase {
2457 SurfaceFlinger* flinger;
2458 DisplayID dpy;
2459 sp<IMemoryHeap>* heap;
2460 uint32_t* w;
2461 uint32_t* h;
2462 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002463 uint32_t sw;
2464 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002465 uint32_t minLayerZ;
2466 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002467 status_t result;
2468 public:
2469 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002470 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002471 uint32_t sw, uint32_t sh,
2472 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002473 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002474 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2475 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2476 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002477 {
2478 }
2479 status_t getResult() const {
2480 return result;
2481 }
2482 virtual bool handler() {
2483 Mutex::Autolock _l(flinger->mStateLock);
2484
2485 // if we have secure windows, never allow the screen capture
2486 if (flinger->mSecureFrameBuffer)
2487 return true;
2488
Mathias Agopian74c40c02010-09-29 13:02:36 -07002489 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002490 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002491
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002492 return true;
2493 }
2494 };
2495
2496 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002497 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002498 status_t res = postMessageSync(msg);
2499 if (res == NO_ERROR) {
2500 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2501 }
2502 return res;
2503}
2504
2505// ---------------------------------------------------------------------------
2506
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002507sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2508{
2509 sp<Layer> result;
2510 Mutex::Autolock _l(mStateLock);
2511 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2512 return result;
2513}
2514
2515// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516
Mathias Agopian96f08192010-06-02 23:28:45 -07002517Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002518 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520}
2521
Mathias Agopian96f08192010-06-02 23:28:45 -07002522Client::~Client()
2523{
Mathias Agopian96f08192010-06-02 23:28:45 -07002524 const size_t count = mLayers.size();
2525 for (size_t i=0 ; i<count ; i++) {
2526 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2527 if (layer != 0) {
2528 mFlinger->removeLayer(layer);
2529 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 }
2531}
2532
Mathias Agopian96f08192010-06-02 23:28:45 -07002533status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002534 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002535}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002536
Mathias Agopian4f113742011-05-03 16:21:41 -07002537size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002538{
Mathias Agopian4f113742011-05-03 16:21:41 -07002539 Mutex::Autolock _l(mLock);
2540 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002541 mLayers.add(name, layer);
2542 return name;
2543}
2544
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002545void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002546{
Mathias Agopian4f113742011-05-03 16:21:41 -07002547 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002548 // we do a linear search here, because this doesn't happen often
2549 const size_t count = mLayers.size();
2550 for (size_t i=0 ; i<count ; i++) {
2551 if (mLayers.valueAt(i) == layer) {
2552 mLayers.removeItemsAt(i, 1);
2553 break;
2554 }
2555 }
2556}
Mathias Agopian4f113742011-05-03 16:21:41 -07002557sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2558{
2559 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002560 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002561 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002562 if (layer != 0) {
2563 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002564 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002565 }
2566 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567}
2568
Mathias Agopiana67932f2011-04-20 14:20:59 -07002569
2570status_t Client::onTransact(
2571 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2572{
2573 // these must be checked
2574 IPCThreadState* ipc = IPCThreadState::self();
2575 const int pid = ipc->getCallingPid();
2576 const int uid = ipc->getCallingUid();
2577 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002578 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002579 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002580 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002581 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002582 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002583 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2584 return PERMISSION_DENIED;
2585 }
2586 }
2587 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002588}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002589
2590
Mathias Agopian96f08192010-06-02 23:28:45 -07002591sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002592 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002593 const String8& name,
2594 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002595 uint32_t flags)
2596{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002597 /*
2598 * createSurface must be called from the GL thread so that it can
2599 * have access to the GL context.
2600 */
2601
2602 class MessageCreateSurface : public MessageBase {
2603 sp<ISurface> result;
2604 SurfaceFlinger* flinger;
2605 ISurfaceComposerClient::surface_data_t* params;
2606 Client* client;
2607 const String8& name;
2608 DisplayID display;
2609 uint32_t w, h;
2610 PixelFormat format;
2611 uint32_t flags;
2612 public:
2613 MessageCreateSurface(SurfaceFlinger* flinger,
2614 ISurfaceComposerClient::surface_data_t* params,
2615 const String8& name, Client* client,
2616 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2617 uint32_t flags)
2618 : flinger(flinger), params(params), client(client), name(name),
2619 display(display), w(w), h(h), format(format), flags(flags)
2620 {
2621 }
2622 sp<ISurface> getResult() const { return result; }
2623 virtual bool handler() {
2624 result = flinger->createSurface(params, name, client,
2625 display, w, h, format, flags);
2626 return true;
2627 }
2628 };
2629
2630 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2631 params, name, this, display, w, h, format, flags);
2632 mFlinger->postMessageSync(msg);
2633 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634}
Mathias Agopian96f08192010-06-02 23:28:45 -07002635status_t Client::destroySurface(SurfaceID sid) {
2636 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002638
2639// ---------------------------------------------------------------------------
2640
Jamie Gennis9a78c902011-01-12 18:30:40 -08002641GraphicBufferAlloc::GraphicBufferAlloc() {}
2642
2643GraphicBufferAlloc::~GraphicBufferAlloc() {}
2644
2645sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002646 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002647 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2648 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002649 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002650 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002651 if (err == NO_MEMORY) {
2652 GraphicBuffer::dumpAllocationsToSystemLog();
2653 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002654 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002655 "failed (%s), handle=%p",
2656 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002657 return 0;
2658 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002659 return graphicBuffer;
2660}
2661
Jamie Gennis9a78c902011-01-12 18:30:40 -08002662// ---------------------------------------------------------------------------
2663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002664GraphicPlane::GraphicPlane()
2665 : mHw(0)
2666{
2667}
2668
2669GraphicPlane::~GraphicPlane() {
2670 delete mHw;
2671}
2672
2673bool GraphicPlane::initialized() const {
2674 return mHw ? true : false;
2675}
2676
Mathias Agopian2b92d892010-02-08 15:49:35 -08002677int GraphicPlane::getWidth() const {
2678 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002679}
2680
Mathias Agopian2b92d892010-02-08 15:49:35 -08002681int GraphicPlane::getHeight() const {
2682 return mHeight;
2683}
2684
2685void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2686{
2687 mHw = hw;
2688
2689 // initialize the display orientation transform.
2690 // it's a constant that should come from the display driver.
2691 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2692 char property[PROPERTY_VALUE_MAX];
2693 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2694 //displayOrientation
2695 switch (atoi(property)) {
2696 case 90:
2697 displayOrientation = ISurfaceComposer::eOrientation90;
2698 break;
2699 case 270:
2700 displayOrientation = ISurfaceComposer::eOrientation270;
2701 break;
2702 }
2703 }
2704
2705 const float w = hw->getWidth();
2706 const float h = hw->getHeight();
2707 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2708 &mDisplayTransform);
2709 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2710 mDisplayWidth = h;
2711 mDisplayHeight = w;
2712 } else {
2713 mDisplayWidth = w;
2714 mDisplayHeight = h;
2715 }
2716
2717 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002718}
2719
2720status_t GraphicPlane::orientationToTransfrom(
2721 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002722{
2723 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002724 switch (orientation) {
2725 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002726 flags = Transform::ROT_0;
2727 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002728 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002729 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002730 break;
2731 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002732 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733 break;
2734 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002735 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002736 break;
2737 default:
2738 return BAD_VALUE;
2739 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002740 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002741 return NO_ERROR;
2742}
2743
2744status_t GraphicPlane::setOrientation(int orientation)
2745{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002746 // If the rotation can be handled in hardware, this is where
2747 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002748
2749 const DisplayHardware& hw(displayHardware());
2750 const float w = mDisplayWidth;
2751 const float h = mDisplayHeight;
2752 mWidth = int(w);
2753 mHeight = int(h);
2754
2755 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002756 GraphicPlane::orientationToTransfrom(orientation, w, h,
2757 &orientationTransform);
2758 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2759 mWidth = int(h);
2760 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002761 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002762
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002763 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002764 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002765 return NO_ERROR;
2766}
2767
2768const DisplayHardware& GraphicPlane::displayHardware() const {
2769 return *mHw;
2770}
2771
Mathias Agopian59119e62010-10-11 12:37:43 -07002772DisplayHardware& GraphicPlane::editDisplayHardware() {
2773 return *mHw;
2774}
2775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002776const Transform& GraphicPlane::transform() const {
2777 return mGlobalTransform;
2778}
2779
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002780EGLDisplay GraphicPlane::getEGLDisplay() const {
2781 return mHw->getEGLDisplay();
2782}
2783
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002784// ---------------------------------------------------------------------------
2785
2786}; // namespace android