blob: 870235bffa1b56d51c1e261a78da23b6b64ff300 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <utils/String8.h>
40#include <utils/String16.h>
41#include <utils/StopWatch.h>
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46#include <pixelflinger/pixelflinger.h>
47#include <GLES/gl.h>
48
49#include "clz.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050#include "DisplayEventConnection.h"
51#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070052#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070053#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070056#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
59#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070060#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Glenn Kasten1db13d72011-12-19 13:55:34 -080062#include <private/android_filesystem_config.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070063#include <private/surfaceflinger/SharedBufferStack.h>
64
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080065#define EGL_VERSION_HW_ANDROID 0x3143
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian99b49842011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070082 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070087 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mConsoleSignals(0),
99 mSecureFrameBuffer(0)
100{
101 init();
102}
103
104void SurfaceFlinger::init()
105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showbackground", value, "0");
115 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117 property_get("debug.sf.ddms", value, "0");
118 mDebugDDMS = atoi(value);
119 if (mDebugDDMS) {
120 DdmConnection::start(getServiceName());
121 }
122
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI_IF(mDebugRegion, "showupdates enabled");
124 ALOGI_IF(mDebugBackground, "showbackground enabled");
125 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126}
127
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800128void SurfaceFlinger::onFirstRef()
129{
130 mEventQueue.init(this);
131
132 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
133
134 // Wait for the main thread to be done with its initialization
135 mReadyToRunBarrier.wait();
136}
137
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139SurfaceFlinger::~SurfaceFlinger()
140{
141 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800144void SurfaceFlinger::binderDied(const wp<IBinder>& who)
145{
146 // the window manager died on us. prepare its eulogy.
147
148 // reset screen orientation
149 Vector<ComposerState> state;
150 setTransactionState(state, eOrientationDefault, 0);
151
152 // restart the boot-animation
153 property_set("ctl.start", "bootanim");
154}
155
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700156sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159}
160
Mathias Agopian7e27f052010-05-28 14:22:23 -0700161sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Mathias Agopian96f08192010-06-02 23:28:45 -0700163 sp<ISurfaceComposerClient> bclient;
164 sp<Client> client(new Client(this));
165 status_t err = client->initCheck();
166 if (err == NO_ERROR) {
167 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 return bclient;
170}
171
Jamie Gennis9a78c902011-01-12 18:30:40 -0800172sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
173{
174 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
175 return gba;
176}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
179{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000180 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 const GraphicPlane& plane(mGraphicPlanes[dpy]);
182 return plane;
183}
184
185GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
186{
187 return const_cast<GraphicPlane&>(
188 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
189}
190
191void SurfaceFlinger::bootFinished()
192{
193 const nsecs_t now = systemTime();
194 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000195 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700196 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700197
198 // wait patiently for the window manager death
199 const String16 name("window");
200 sp<IBinder> window(defaultServiceManager()->getService(name));
201 if (window != 0) {
202 window->linkToDeath(this);
203 }
204
205 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700206 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209static inline uint16_t pack565(int r, int g, int b) {
210 return (r<<11)|(g<<5)|b;
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213status_t SurfaceFlinger::readyToRun()
214{
Steve Blocka19954a2012-01-04 20:05:49 +0000215 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 "Initializing graphics H/W...");
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 // we only support one display currently
219 int dpy = 0;
220
221 {
222 // initialize the main display
223 GraphicPlane& plane(graphicPlane(dpy));
224 DisplayHardware* const hw = new DisplayHardware(this, dpy);
225 plane.setDisplayHardware(hw);
226 }
227
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700228 // create the shared control-block
229 mServerHeap = new MemoryHeapBase(4096,
230 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000234 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700235
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700236 new(mServerCblk) surface_flinger_cblk_t;
237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 // initialize primary screen
239 // (other display should be initialized in the same manner, but
240 // asynchronously, as they could come and go. None of this is supported
241 // yet).
242 const GraphicPlane& plane(graphicPlane(dpy));
243 const DisplayHardware& hw = plane.displayHardware();
244 const uint32_t w = hw.getWidth();
245 const uint32_t h = hw.getHeight();
246 const uint32_t f = hw.getFormat();
247 hw.makeCurrent();
248
249 // initialize the shared control block
250 mServerCblk->connected |= 1<<dpy;
251 display_cblk_t* dcblk = mServerCblk->displays + dpy;
252 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800253 dcblk->w = plane.getWidth();
254 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 dcblk->format = f;
256 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
257 dcblk->xdpi = hw.getDpiX();
258 dcblk->ydpi = hw.getDpiY();
259 dcblk->fps = hw.getRefreshRate();
260 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
262 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700264 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glEnableClientState(GL_VERTEX_ARRAY);
266 glEnable(GL_SCISSOR_TEST);
267 glShadeModel(GL_FLAT);
268 glDisable(GL_DITHER);
269 glDisable(GL_CULL_FACE);
270
271 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
272 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700273 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 glGenTextures(1, &mWormholeTexName);
275 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700281 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
282
283 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
284 glGenTextures(1, &mProtectedTexName);
285 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
290 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
291 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
293 glViewport(0, 0, w, h);
294 glMatrixMode(GL_PROJECTION);
295 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700296 // put the origin in the left-bottom corner
297 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800299
300 // start the EventThread
301 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800302 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
304 /*
305 * We're now ready to accept clients...
306 */
307
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800308 mReadyToRunBarrier.open();
309
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800317
Jamie Gennis582270d2011-08-17 18:19:00 -0700318bool SurfaceFlinger::authenticateSurfaceTexture(
319 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800320 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700321 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800322
323 // Check the visible layer list for the ISurface
324 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
325 size_t count = currentLayers.size();
326 for (size_t i=0 ; i<count ; i++) {
327 const sp<LayerBase>& layer(currentLayers[i]);
328 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700329 if (lbc != NULL) {
330 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
331 if (lbcBinder == surfaceTextureBinder) {
332 return true;
333 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800334 }
335 }
336
337 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700338 // SurfaceTexture gets destroyed before all the clients are done using it,
339 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800340 // will instead fail later on when the client tries to use the surface,
341 // which should be reported as "surface XYZ returned an -ENODEV". The
342 // purgatorized layers are no less authentic than the visible ones, so this
343 // should not cause any harm.
344 size_t purgatorySize = mLayerPurgatory.size();
345 for (size_t i=0 ; i<purgatorySize ; i++) {
346 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
347 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700348 if (lbc != NULL) {
349 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
350 if (lbcBinder == surfaceTextureBinder) {
351 return true;
352 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800353 }
354 }
355
356 return false;
357}
358
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800359// ----------------------------------------------------------------------------
360
361sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800362 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700363}
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800366
367void SurfaceFlinger::waitForEvent() {
368 mEventQueue.waitMessage();
369}
370
371void SurfaceFlinger::signalTransaction() {
372 mEventQueue.invalidate();
373}
374
375void SurfaceFlinger::signalLayerUpdate() {
376 mEventQueue.invalidate();
377}
378
379void SurfaceFlinger::signalRefresh() {
380 mEventQueue.refresh();
381}
382
383status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
384 nsecs_t reltime, uint32_t flags) {
385 return mEventQueue.postMessage(msg, reltime);
386}
387
388status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
389 nsecs_t reltime, uint32_t flags) {
390 status_t res = mEventQueue.postMessage(msg, reltime);
391 if (res == NO_ERROR) {
392 msg->wait();
393 }
394 return res;
395}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396
397bool SurfaceFlinger::threadLoop()
398{
399 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 return true;
401}
402
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800403void SurfaceFlinger::onMessageReceived(int32_t what)
404{
405 switch (what) {
406 case MessageQueue::INVALIDATE: {
407 // check for transactions
408 if (CC_UNLIKELY(mConsoleSignals)) {
409 handleConsoleEvents();
410 }
411
412 // if we're in a global transaction, don't do anything.
413 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
414 uint32_t transactionFlags = peekTransactionFlags(mask);
415 if (CC_UNLIKELY(transactionFlags)) {
416 handleTransaction(transactionFlags);
417 }
418
419 // post surfaces (if needed)
420 handlePageFlip();
421
422 if (!mDirtyRegion.isEmpty()) {
423 signalRefresh();
424 }
425 } break;
426
427 case MessageQueue::REFRESH: {
428 if (!mDirtyRegion.isEmpty()) {
429 // NOTE: it is mandatory to call hw.compositionComplete()
430 // after handleRefresh()
431 handleRefresh();
432
433 const DisplayHardware& hw(graphicPlane(0).displayHardware());
434 if (CC_UNLIKELY(mHwWorkListDirty)) {
435 // build the h/w work list
436 handleWorkList();
437 }
438 if (CC_LIKELY(hw.canDraw())) {
439 // repaint the framebuffer (if needed)
440 handleRepaint();
441 // inform the h/w that we're done compositing
442 hw.compositionComplete();
443 postFramebuffer();
444 } else {
445 // pretend we did the post
446 hw.compositionComplete();
447 }
448 }
449 } break;
450 }
451}
452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453void SurfaceFlinger::postFramebuffer()
454{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700455 // this should never happen. we do the flip anyways so we don't
456 // risk to cause a deadlock with hwc
Steve Block32397c12012-01-05 23:22:43 +0000457 ALOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700458 const DisplayHardware& hw(graphicPlane(0).displayHardware());
459 const nsecs_t now = systemTime();
460 mDebugInSwapBuffers = now;
461 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800462
463 size_t numLayers = mVisibleLayersSortedByZ.size();
464 for (size_t i = 0; i < numLayers; i++) {
465 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
466 }
467
Mathias Agopiana44b0412011-10-16 18:46:35 -0700468 mLastSwapBufferTime = systemTime() - now;
469 mDebugInSwapBuffers = 0;
470 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
473void SurfaceFlinger::handleConsoleEvents()
474{
475 // something to do with the console
476 const DisplayHardware& hw = graphicPlane(0).displayHardware();
477
478 int what = android_atomic_and(0, &mConsoleSignals);
479 if (what & eConsoleAcquired) {
480 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700481 // this is a temporary work-around, eventually this should be called
482 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700483 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484 }
485
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700487 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 }
490 }
491
492 mDirtyRegion.set(hw.bounds());
493}
494
495void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
496{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497 Mutex::Autolock _l(mStateLock);
498 const nsecs_t now = systemTime();
499 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500
Mathias Agopianca4d3602011-05-19 15:38:14 -0700501 // Here we're guaranteed that some transaction flags are set
502 // so we can call handleTransactionLocked() unconditionally.
503 // We call getTransactionFlags(), which will also clear the flags,
504 // with mStateLock held to guarantee that mCurrentState won't change
505 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700506
Mathias Agopianca4d3602011-05-19 15:38:14 -0700507 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
508 transactionFlags = getTransactionFlags(mask);
509 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700510
Mathias Agopianca4d3602011-05-19 15:38:14 -0700511 mLastTransactionTime = systemTime() - now;
512 mDebugInTransaction = 0;
513 invalidateHwcGeometry();
514 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700515}
516
Mathias Agopianca4d3602011-05-19 15:38:14 -0700517void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700518{
519 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 const size_t count = currentLayers.size();
521
522 /*
523 * Traversal of the children
524 * (perform the transaction for each of them if needed)
525 */
526
527 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
528 if (layersNeedTransaction) {
529 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700530 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
532 if (!trFlags) continue;
533
534 const uint32_t flags = layer->doTransaction(0);
535 if (flags & Layer::eVisibleRegion)
536 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538 }
539
540 /*
541 * Perform our own transaction if needed
542 */
543
544 if (transactionFlags & eTransactionNeeded) {
545 if (mCurrentState.orientation != mDrawingState.orientation) {
546 // the orientation has changed, recompute all visible regions
547 // and invalidate everything.
548
549 const int dpy = 0;
550 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700551 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 GraphicPlane& plane(graphicPlane(dpy));
553 plane.setOrientation(orientation);
554
555 // update the shared control block
556 const DisplayHardware& hw(plane.displayHardware());
557 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
558 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800559 dcblk->w = plane.getWidth();
560 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561
562 mVisibleRegionsDirty = true;
563 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 }
565
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700566 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
567 // layers have been added
568 mVisibleRegionsDirty = true;
569 }
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 // some layers might have been removed, so
572 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700573 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700574 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700576 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700577 const size_t count = previousLayers.size();
578 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700579 const sp<LayerBase>& layer(previousLayers[i]);
580 if (currentLayers.indexOf( layer ) < 0) {
581 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700582 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700583 }
584 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 }
587
588 commitTransaction();
589}
590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800592 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593{
594 const GraphicPlane& plane(graphicPlane(0));
595 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700596 const DisplayHardware& hw(plane.displayHardware());
597 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598
599 Region aboveOpaqueLayers;
600 Region aboveCoveredLayers;
601 Region dirty;
602
603 bool secureFrameBuffer = false;
604
605 size_t i = currentLayers.size();
606 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 layer->validateVisibility(planeTransform);
609
610 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700611 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612
Mathias Agopianab028732010-03-16 16:41:46 -0700613 /*
614 * opaqueRegion: area of a surface that is fully opaque.
615 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700617
618 /*
619 * visibleRegion: area of a surface that is visible on screen
620 * and not fully transparent. This is essentially the layer's
621 * footprint minus the opaque regions above it.
622 * Areas covered by a translucent surface are considered visible.
623 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700625
626 /*
627 * coveredRegion: area of a surface that is covered by all
628 * visible regions above it (which includes the translucent areas).
629 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700631
632
633 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800634 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700635 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700636 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700638 visibleRegion.andSelf(screenRegion);
639 if (!visibleRegion.isEmpty()) {
640 // Remove the transparent area from the visible region
641 if (translucent) {
642 visibleRegion.subtractSelf(layer->transparentRegionScreen);
643 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644
Mathias Agopianab028732010-03-16 16:41:46 -0700645 // compute the opaque region
646 const int32_t layerOrientation = layer->getOrientation();
647 if (s.alpha==255 && !translucent &&
648 ((layerOrientation & Transform::ROT_INVALID) == false)) {
649 // the opaque region is the layer's footprint
650 opaqueRegion = visibleRegion;
651 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 }
653 }
654
Mathias Agopianab028732010-03-16 16:41:46 -0700655 // Clip the covered region to the visible region
656 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
657
658 // Update aboveCoveredLayers for next (lower) layer
659 aboveCoveredLayers.orSelf(visibleRegion);
660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 // subtract the opaque region covered by the layers above us
662 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
664 // compute this layer's dirty region
665 if (layer->contentDirty) {
666 // we need to invalidate the whole region
667 dirty = visibleRegion;
668 // as well, as the old visible region
669 dirty.orSelf(layer->visibleRegionScreen);
670 layer->contentDirty = false;
671 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700672 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700673 * the exposed region consists of two components:
674 * 1) what's VISIBLE now and was COVERED before
675 * 2) what's EXPOSED now less what was EXPOSED before
676 *
677 * note that (1) is conservative, we start with the whole
678 * visible region but only keep what used to be covered by
679 * something -- which mean it may have been exposed.
680 *
681 * (2) handles areas that were not covered by anything but got
682 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700683 */
Mathias Agopianab028732010-03-16 16:41:46 -0700684 const Region newExposed = visibleRegion - coveredRegion;
685 const Region oldVisibleRegion = layer->visibleRegionScreen;
686 const Region oldCoveredRegion = layer->coveredRegionScreen;
687 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
688 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 }
690 dirty.subtractSelf(aboveOpaqueLayers);
691
692 // accumulate to the screen dirty region
693 dirtyRegion.orSelf(dirty);
694
Mathias Agopianab028732010-03-16 16:41:46 -0700695 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 // Store the visible region is screen space
699 layer->setVisibleRegion(visibleRegion);
700 layer->setCoveredRegion(coveredRegion);
701
Mathias Agopian97011222009-07-28 10:57:27 -0700702 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 if (layer->isSecure() && !visibleRegion.isEmpty()) {
704 secureFrameBuffer = true;
705 }
706 }
707
Mathias Agopian97011222009-07-28 10:57:27 -0700708 // invalidate the areas where a layer was removed
709 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
710 mDirtyRegionRemovedLayer.clear();
711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 mSecureFrameBuffer = secureFrameBuffer;
713 opaqueRegion = aboveOpaqueLayers;
714}
715
716
717void SurfaceFlinger::commitTransaction()
718{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800719 if (!mLayersPendingRemoval.isEmpty()) {
720 // Notify removed layers now that they can't be drawn from
721 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
722 mLayersPendingRemoval[i]->onRemoved();
723 }
724 mLayersPendingRemoval.clear();
725 }
726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700728 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700729 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730}
731
732void SurfaceFlinger::handlePageFlip()
733{
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800734 const DisplayHardware& hw = graphicPlane(0).displayHardware();
735 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800737 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
738 const bool visibleRegions = lockPageFlip(currentLayers);
739
740 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 Region opaqueRegion;
742 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700743
744 /*
745 * rebuild the visible layer list
746 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800747 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700748 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700749 mVisibleLayersSortedByZ.setCapacity(count);
750 for (size_t i=0 ; i<count ; i++) {
751 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
752 mVisibleLayersSortedByZ.add(currentLayers[i]);
753 }
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 mWormholeRegion = screenRegion.subtract(opaqueRegion);
756 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800757 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 }
759
760 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700761
762 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 mDirtyRegion.andSelf(screenRegion);
764}
765
Mathias Agopianad456f92011-01-13 17:53:01 -0800766void SurfaceFlinger::invalidateHwcGeometry()
767{
768 mHwWorkListDirty = true;
769}
770
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
772{
773 bool recomputeVisibleRegions = false;
774 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700775 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700777 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 layer->lockPageFlip(recomputeVisibleRegions);
779 }
780 return recomputeVisibleRegions;
781}
782
783void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
784{
785 const GraphicPlane& plane(graphicPlane(0));
786 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800787 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700788 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700790 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791 layer->unlockPageFlip(planeTransform, mDirtyRegion);
792 }
793}
794
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800795void SurfaceFlinger::handleRefresh()
796{
797 bool needInvalidate = false;
798 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
799 const size_t count = currentLayers.size();
800 for (size_t i=0 ; i<count ; i++) {
801 const sp<LayerBase>& layer(currentLayers[i]);
802 if (layer->onPreComposition()) {
803 needInvalidate = true;
804 }
805 }
806 if (needInvalidate) {
807 signalLayerUpdate();
808 }
809}
810
811
Mathias Agopiana350ff92010-08-10 17:14:02 -0700812void SurfaceFlinger::handleWorkList()
813{
814 mHwWorkListDirty = false;
815 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
816 if (hwc.initCheck() == NO_ERROR) {
817 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
818 const size_t count = currentLayers.size();
819 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700820 hwc_layer_t* const cur(hwc.getLayers());
821 for (size_t i=0 ; cur && i<count ; i++) {
822 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700823 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700824 cur[i].compositionType = HWC_FRAMEBUFFER;
825 cur[i].flags |= HWC_SKIP_LAYER;
826 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700827 }
828 }
829}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831void SurfaceFlinger::handleRepaint()
832{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700833 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700834 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835
Glenn Kasten99ed2242011-12-15 09:51:17 -0800836 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 debugFlashRegions();
838 }
839
Mathias Agopianb8a55602009-06-26 19:06:36 -0700840 // set the frame buffer
841 const DisplayHardware& hw(graphicPlane(0).displayHardware());
842 glMatrixMode(GL_MODELVIEW);
843 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844
845 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700846 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
847 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700848 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700849 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
850 // takes a rectangle, we must make sure to update that whole
851 // rectangle in that case
852 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700853 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700854 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700855 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700857 // in the BUFFER_PRESERVED case, obviously, we can update only
858 // what's needed and nothing more.
859 // NOTE: this is NOT a common case, as preserving the backbuffer
860 // is costly and usually involves copying the whole update back.
861 }
862 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700863 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700864 // We need to redraw the rectangle that will be updated
865 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700866 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700867 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700868 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700869 } else {
870 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700872 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 }
874 }
875
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700876 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 composeSurfaces(mDirtyRegion);
878
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700879 // update the swap region and clear the dirty region
880 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 mDirtyRegion.clear();
882}
883
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700884void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700886 const DisplayHardware& hw(graphicPlane(0).displayHardware());
887 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700888 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700889 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700890 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700891 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700892
Mathias Agopianf384cc32011-09-08 18:31:55 -0700893 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
894 size_t count = layers.size();
895
Steve Blocke6f43dd2012-01-06 19:20:56 +0000896 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700897 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
898 hwc.getNumLayers(), count);
899
900 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700901 if (hwc.initCheck() == NO_ERROR) {
902 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
903 }
Mathias Agopian45721772010-08-12 15:03:26 -0700904
905 /*
906 * update the per-frame h/w composer data for each layer
907 * and build the transparent region of the FB
908 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700909 for (size_t i=0 ; i<count ; i++) {
910 const sp<LayerBase>& layer(layers[i]);
911 layer->setPerFrameData(&cur[i]);
912 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700913 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700914 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000915 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700916
Mathias Agopianf384cc32011-09-08 18:31:55 -0700917 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700918 // what's happening here is tricky.
919 // we want to clear all the layers with the CLEAR_FB flags
920 // that are opaque.
921 // however, since some GPU are efficient at preserving
922 // the backbuffer, we want to take advantage of that so we do the
923 // clear only in the dirty region (other areas will be preserved
924 // on those GPUs).
925 // NOTE: on non backbuffer preserving GPU, the dirty region
926 // has already been expanded as needed, so the code is correct
927 // there too.
928 //
929 // However, the content of the framebuffer cannot be trusted when
930 // we switch to/from FB/OVERLAY, in which case we need to
931 // expand the dirty region to those areas too.
932 //
933 // Note also that there is a special case when switching from
934 // "no layers in FB" to "some layers in FB", where we need to redraw
935 // the entire FB, since some areas might contain uninitialized
936 // data.
937 //
938 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700939 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700940 // that is, we can't erase anything outside the dirty region.
941
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700942 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700943
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700944 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
945 transparent.set(hw.getBounds());
946 dirtyInOut = transparent;
947 } else {
948 for (size_t i=0 ; i<count ; i++) {
949 const sp<LayerBase>& layer(layers[i]);
950 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
951 transparent.orSelf(layer->visibleRegionScreen);
952 }
953 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
954 if (isOverlay != layer->isOverlay()) {
955 // we transitioned to/from overlay, so add this layer
956 // to the dirty region so the framebuffer can be either
957 // cleared or redrawn.
958 dirtyInOut.orSelf(layer->visibleRegionScreen);
959 }
960 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800961 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700962 // don't erase stuff outside the dirty region
963 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700964 }
965
966 /*
967 * clear the area of the FB that need to be transparent
968 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700969 if (!transparent.isEmpty()) {
970 glClearColor(0,0,0,0);
971 Region::const_iterator it = transparent.begin();
972 Region::const_iterator const end = transparent.end();
973 const int32_t height = hw.getHeight();
974 while (it != end) {
975 const Rect& r(*it++);
976 const GLint sy = height - (r.top + r.height());
977 glScissor(r.left, sy, r.width(), r.height());
978 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800979 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700980 }
981 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700982}
Mathias Agopian45721772010-08-12 15:03:26 -0700983
Mathias Agopianf384cc32011-09-08 18:31:55 -0700984void SurfaceFlinger::composeSurfaces(const Region& dirty)
985{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700986 const DisplayHardware& hw(graphicPlane(0).displayHardware());
987 HWComposer& hwc(hw.getHwComposer());
988
989 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800990 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700991 // should never happen unless the window manager has a bug
992 // draw something...
993 drawWormhole();
994 }
995
Mathias Agopian45721772010-08-12 15:03:26 -0700996 /*
997 * and then, render the layers targeted at the framebuffer
998 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700999 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001000 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1001 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001002 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001003 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001004 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001005 }
1006 const sp<LayerBase>& layer(layers[i]);
1007 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1008 if (!clip.isEmpty()) {
1009 layer->draw(clip);
1010 }
1011 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012}
1013
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014void SurfaceFlinger::debugFlashRegions()
1015{
Mathias Agopian0a917752010-06-14 21:20:00 -07001016 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1017 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001018 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001019 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001020 return;
1021 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001022
Mathias Agopian0a917752010-06-14 21:20:00 -07001023 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1024 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1025 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1026 mDirtyRegion.bounds() : hw.bounds());
1027 composeSurfaces(repaint);
1028 }
1029
Mathias Agopianc492e672011-10-18 14:49:27 -07001030 glDisable(GL_TEXTURE_EXTERNAL_OES);
1031 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 glDisable(GL_SCISSOR_TEST);
1034
Mathias Agopian0926f502009-05-04 14:17:04 -07001035 static int toggle = 0;
1036 toggle = 1 - toggle;
1037 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001038 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001039 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001040 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001041 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042
Mathias Agopian20f68782009-05-11 00:03:41 -07001043 Region::const_iterator it = mDirtyRegion.begin();
1044 Region::const_iterator const end = mDirtyRegion.end();
1045 while (it != end) {
1046 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001048 { r.left, height - r.top },
1049 { r.left, height - r.bottom },
1050 { r.right, height - r.bottom },
1051 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 };
1053 glVertexPointer(2, GL_FLOAT, 0, vertices);
1054 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1055 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001056
Mathias Agopian0656a682011-09-20 17:22:44 -07001057 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058
1059 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001060 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061
1062 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063}
1064
1065void SurfaceFlinger::drawWormhole() const
1066{
1067 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1068 if (region.isEmpty())
1069 return;
1070
1071 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1072 const int32_t width = hw.getWidth();
1073 const int32_t height = hw.getHeight();
1074
Glenn Kasten99ed2242011-12-15 09:51:17 -08001075 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001076 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001077 Region::const_iterator it = region.begin();
1078 Region::const_iterator const end = region.end();
1079 while (it != end) {
1080 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 const GLint sy = height - (r.top + r.height());
1082 glScissor(r.left, sy, r.width(), r.height());
1083 glClear(GL_COLOR_BUFFER_BIT);
1084 }
1085 } else {
1086 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1087 { width, height }, { 0, height } };
1088 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 glVertexPointer(2, GL_SHORT, 0, vertices);
1091 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1092 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001093
1094 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 glEnable(GL_TEXTURE_2D);
1096 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1097 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1098 glMatrixMode(GL_TEXTURE);
1099 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001100
1101 glDisable(GL_BLEND);
1102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001104 Region::const_iterator it = region.begin();
1105 Region::const_iterator const end = region.end();
1106 while (it != end) {
1107 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 const GLint sy = height - (r.top + r.height());
1109 glScissor(r.left, sy, r.width(), r.height());
1110 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1111 }
1112 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001113 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001114 glLoadIdentity();
1115 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
1117}
1118
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001119status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120{
1121 Mutex::Autolock _l(mStateLock);
1122 addLayer_l(layer);
1123 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1124 return NO_ERROR;
1125}
1126
Mathias Agopian96f08192010-06-02 23:28:45 -07001127status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1128{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001129 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001130 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1131}
1132
1133ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1134 const sp<LayerBaseClient>& lbc)
1135{
Mathias Agopian96f08192010-06-02 23:28:45 -07001136 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001137 size_t name = client->attachLayer(lbc);
1138
1139 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001140
1141 // add this layer to the current state list
1142 addLayer_l(lbc);
1143
Mathias Agopian4f113742011-05-03 16:21:41 -07001144 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001145}
1146
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001147status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148{
1149 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001150 status_t err = purgatorizeLayer_l(layer);
1151 if (err == NO_ERROR)
1152 setTransactionFlags(eTransactionNeeded);
1153 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154}
1155
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001156status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001158 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1159 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001160 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1163 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001164 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 return NO_ERROR;
1166 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001167 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168}
1169
Mathias Agopian9a112062009-04-17 19:36:26 -07001170status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1171{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001172 // First add the layer to the purgatory list, which makes sure it won't
1173 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001174 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001175 if (err >= 0) {
1176 mLayerPurgatory.add(layerBase);
1177 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001178
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001179 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001180
Mathias Agopian3d579642009-06-04 18:46:21 -07001181 // it's possible that we don't find a layer, because it might
1182 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001183 // from the user because there is a race between Client::destroySurface(),
1184 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001185 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1186}
1187
Mathias Agopian96f08192010-06-02 23:28:45 -07001188status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189{
Mathias Agopian96f08192010-06-02 23:28:45 -07001190 layer->forceVisibilityTransaction();
1191 setTransactionFlags(eTraversalNeeded);
1192 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193}
1194
Mathias Agopiandea20b12011-05-03 17:04:02 -07001195uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1196{
1197 return android_atomic_release_load(&mTransactionFlags);
1198}
1199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1201{
1202 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1203}
1204
Mathias Agopianbb641242010-05-18 17:06:55 -07001205uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206{
1207 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1208 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001209 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 }
1211 return old;
1212}
1213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001215void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001216 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001217 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001218
Jamie Gennis28378392011-10-12 17:39:00 -07001219 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001220 if (mCurrentState.orientation != orientation) {
1221 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1222 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001223 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001224 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001225 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001226 orientation);
1227 }
1228 }
1229
Mathias Agopian698c0872011-06-28 19:09:31 -07001230 const size_t count = state.size();
1231 for (size_t i=0 ; i<count ; i++) {
1232 const ComposerState& s(state[i]);
1233 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001234 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001235 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001236
Mathias Agopian386aa982011-11-07 21:58:03 -08001237 if (transactionFlags) {
1238 // this triggers the transaction
1239 setTransactionFlags(transactionFlags);
1240
1241 // if this is a synchronous transaction, wait for it to take effect
1242 // before returning.
1243 if (flags & eSynchronous) {
1244 mTransationPending = true;
1245 }
1246 while (mTransationPending) {
1247 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1248 if (CC_UNLIKELY(err != NO_ERROR)) {
1249 // just in case something goes wrong in SF, return to the
1250 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001251 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001252 mTransationPending = false;
1253 break;
1254 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001255 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 }
1257}
1258
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001259sp<ISurface> SurfaceFlinger::createSurface(
1260 ISurfaceComposerClient::surface_data_t* params,
1261 const String8& name,
1262 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1264 uint32_t flags)
1265{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001266 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001267 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001268
1269 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001270 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001271 int(w), int(h));
1272 return surfaceHandle;
1273 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001274
Steve Block9d453682011-12-20 16:23:08 +00001275 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001276 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 switch (flags & eFXSurfaceMask) {
1278 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001279 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1280 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 break;
1282 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001283 // for now we treat Blur as Dim, until we can implement it
1284 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001288 case eFXSurfaceScreenshot:
1289 layer = createScreenshotSurface(client, d, w, h, flags);
1290 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 }
1292
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001293 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001294 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001295 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001296 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001299 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001300 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001301 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001302 if (normalLayer != 0) {
1303 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001304 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001305 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001306 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001307
Mathias Agopian96f08192010-06-02 23:28:45 -07001308 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 }
1310
1311 return surfaceHandle;
1312}
1313
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001314sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001315 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001316 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001317 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
1319 // initialize the surfaces
1320 switch (format) { // TODO: take h/w into account
1321 case PIXEL_FORMAT_TRANSPARENT:
1322 case PIXEL_FORMAT_TRANSLUCENT:
1323 format = PIXEL_FORMAT_RGBA_8888;
1324 break;
1325 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001326#ifdef NO_RGBX_8888
1327 format = PIXEL_FORMAT_RGB_565;
1328#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001329 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001330#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 break;
1332 }
1333
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001334#ifdef NO_RGBX_8888
1335 if (format == PIXEL_FORMAT_RGBX_8888)
1336 format = PIXEL_FORMAT_RGBA_8888;
1337#endif
1338
Mathias Agopian96f08192010-06-02 23:28:45 -07001339 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001340 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001341 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001342 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001343 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 }
1345 return layer;
1346}
1347
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001348sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001349 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001350 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351{
Mathias Agopian96f08192010-06-02 23:28:45 -07001352 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001353 return layer;
1354}
1355
1356sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1357 const sp<Client>& client, DisplayID display,
1358 uint32_t w, uint32_t h, uint32_t flags)
1359{
1360 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 return layer;
1362}
1363
Mathias Agopian96f08192010-06-02 23:28:45 -07001364status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365{
Mathias Agopian9a112062009-04-17 19:36:26 -07001366 /*
1367 * called by the window manager, when a surface should be marked for
1368 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001369 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001370 * The surface is removed from the current and drawing lists, but placed
1371 * in the purgatory queue, so it's not destroyed right-away (we need
1372 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001373 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001374
Mathias Agopian48d819a2009-09-10 19:41:18 -07001375 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001376 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001377 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001378 if (layer != 0) {
1379 err = purgatorizeLayer_l(layer);
1380 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001381 setTransactionFlags(eTransactionNeeded);
1382 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001383 }
1384 return err;
1385}
1386
Mathias Agopianca4d3602011-05-19 15:38:14 -07001387status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001388{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001389 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001390 status_t err = NO_ERROR;
1391 sp<LayerBaseClient> l(layer.promote());
1392 if (l != NULL) {
1393 Mutex::Autolock _l(mStateLock);
1394 err = removeLayer_l(l);
1395 if (err == NAME_NOT_FOUND) {
1396 // The surface wasn't in the current list, which means it was
1397 // removed already, which means it is in the purgatory,
1398 // and need to be removed from there.
1399 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001400 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001401 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001402 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001403 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001404 "error removing layer=%p (%s)", l.get(), strerror(-err));
1405 }
1406 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407}
1408
Mathias Agopian698c0872011-06-28 19:09:31 -07001409uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001410 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001411 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001414 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1415 if (layer != 0) {
1416 const uint32_t what = s.what;
1417 if (what & ePositionChanged) {
1418 if (layer->setPosition(s.x, s.y))
1419 flags |= eTraversalNeeded;
1420 }
1421 if (what & eLayerChanged) {
1422 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1423 if (layer->setLayer(s.z)) {
1424 mCurrentState.layersSortedByZ.removeAt(idx);
1425 mCurrentState.layersSortedByZ.add(layer);
1426 // we need traversal (state changed)
1427 // AND transaction (list changed)
1428 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 }
1430 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001431 if (what & eSizeChanged) {
1432 if (layer->setSize(s.w, s.h)) {
1433 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001434 }
1435 }
1436 if (what & eAlphaChanged) {
1437 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1438 flags |= eTraversalNeeded;
1439 }
1440 if (what & eMatrixChanged) {
1441 if (layer->setMatrix(s.matrix))
1442 flags |= eTraversalNeeded;
1443 }
1444 if (what & eTransparentRegionChanged) {
1445 if (layer->setTransparentRegionHint(s.transparentRegion))
1446 flags |= eTraversalNeeded;
1447 }
1448 if (what & eVisibilityChanged) {
1449 if (layer->setFlags(s.flags, s.mask))
1450 flags |= eTraversalNeeded;
1451 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001453 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454}
1455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456void SurfaceFlinger::screenReleased(int dpy)
1457{
1458 // this may be called by a signal handler, we can't do too much in here
1459 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001460 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461}
1462
1463void SurfaceFlinger::screenAcquired(int dpy)
1464{
1465 // this may be called by a signal handler, we can't do too much in here
1466 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001467 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
1470status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1471{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001472 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 char buffer[SIZE];
1474 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001475
1476 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 snprintf(buffer, SIZE, "Permission Denial: "
1478 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1479 IPCThreadState::self()->getCallingPid(),
1480 IPCThreadState::self()->getCallingUid());
1481 result.append(buffer);
1482 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001483 // Try to get the main lock, but don't insist if we can't
1484 // (this would indicate SF is stuck, but we want to be able to
1485 // print something in dumpsys).
1486 int retry = 3;
1487 while (mStateLock.tryLock()<0 && --retry>=0) {
1488 usleep(1000000);
1489 }
1490 const bool locked(retry >= 0);
1491 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001492 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001493 "SurfaceFlinger appears to be unresponsive, "
1494 "dumping anyways (no locks held)\n");
1495 result.append(buffer);
1496 }
1497
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001498 bool dumpAll = true;
1499 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001500 size_t numArgs = args.size();
1501 if (numArgs) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001502 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001503
1504 if ((index < numArgs) &&
1505 (args[index] == String16("--list"))) {
1506 index++;
1507 listLayersLocked(args, index, result, buffer, SIZE);
1508 }
1509
1510 if ((index < numArgs) &&
1511 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001512 index++;
1513 dumpStatsLocked(args, index, result, buffer, SIZE);
1514 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001515
1516 if ((index < numArgs) &&
1517 (args[index] == String16("--latency-clear"))) {
1518 index++;
1519 clearStatsLocked(args, index, result, buffer, SIZE);
1520 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001522
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001523 if (dumpAll) {
1524 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001525 }
1526
Mathias Agopian9795c422009-08-26 16:36:26 -07001527 if (locked) {
1528 mStateLock.unlock();
1529 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 }
1531 write(fd, result.string(), result.size());
1532 return NO_ERROR;
1533}
1534
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001535void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1536 String8& result, char* buffer, size_t SIZE) const
1537{
1538 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1539 const size_t count = currentLayers.size();
1540 for (size_t i=0 ; i<count ; i++) {
1541 const sp<LayerBase>& layer(currentLayers[i]);
1542 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1543 result.append(buffer);
1544 }
1545}
1546
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001547void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1548 String8& result, char* buffer, size_t SIZE) const
1549{
1550 String8 name;
1551 if (index < args.size()) {
1552 name = String8(args[index]);
1553 index++;
1554 }
1555
1556 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1557 const size_t count = currentLayers.size();
1558 for (size_t i=0 ; i<count ; i++) {
1559 const sp<LayerBase>& layer(currentLayers[i]);
1560 if (name.isEmpty()) {
1561 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1562 result.append(buffer);
1563 }
1564 if (name.isEmpty() || (name == layer->getName())) {
1565 layer->dumpStats(result, buffer, SIZE);
1566 }
1567 }
1568}
1569
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001570void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1571 String8& result, char* buffer, size_t SIZE) const
1572{
1573 String8 name;
1574 if (index < args.size()) {
1575 name = String8(args[index]);
1576 index++;
1577 }
1578
1579 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1580 const size_t count = currentLayers.size();
1581 for (size_t i=0 ; i<count ; i++) {
1582 const sp<LayerBase>& layer(currentLayers[i]);
1583 if (name.isEmpty() || (name == layer->getName())) {
1584 layer->clearStats();
1585 }
1586 }
1587}
1588
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001589void SurfaceFlinger::dumpAllLocked(
1590 String8& result, char* buffer, size_t SIZE) const
1591{
1592 // figure out if we're stuck somewhere
1593 const nsecs_t now = systemTime();
1594 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1595 const nsecs_t inTransaction(mDebugInTransaction);
1596 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1597 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1598
1599 /*
1600 * Dump the visible layer list
1601 */
1602 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1603 const size_t count = currentLayers.size();
1604 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1605 result.append(buffer);
1606 for (size_t i=0 ; i<count ; i++) {
1607 const sp<LayerBase>& layer(currentLayers[i]);
1608 layer->dump(result, buffer, SIZE);
1609 }
1610
1611 /*
1612 * Dump the layers in the purgatory
1613 */
1614
1615 const size_t purgatorySize = mLayerPurgatory.size();
1616 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1617 result.append(buffer);
1618 for (size_t i=0 ; i<purgatorySize ; i++) {
1619 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1620 layer->shortDump(result, buffer, SIZE);
1621 }
1622
1623 /*
1624 * Dump SurfaceFlinger global state
1625 */
1626
1627 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1628 result.append(buffer);
1629
1630 const GLExtensions& extensions(GLExtensions::getInstance());
1631 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1632 extensions.getVendor(),
1633 extensions.getRenderer(),
1634 extensions.getVersion());
1635 result.append(buffer);
1636
1637 snprintf(buffer, SIZE, "EGL : %s\n",
1638 eglQueryString(graphicPlane(0).getEGLDisplay(),
1639 EGL_VERSION_HW_ANDROID));
1640 result.append(buffer);
1641
1642 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1643 result.append(buffer);
1644
1645 mWormholeRegion.dump(result, "WormholeRegion");
1646 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1647 snprintf(buffer, SIZE,
1648 " orientation=%d, canDraw=%d\n",
1649 mCurrentState.orientation, hw.canDraw());
1650 result.append(buffer);
1651 snprintf(buffer, SIZE,
1652 " last eglSwapBuffers() time: %f us\n"
1653 " last transaction time : %f us\n"
1654 " refresh-rate : %f fps\n"
1655 " x-dpi : %f\n"
1656 " y-dpi : %f\n",
1657 mLastSwapBufferTime/1000.0,
1658 mLastTransactionTime/1000.0,
1659 hw.getRefreshRate(),
1660 hw.getDpiX(),
1661 hw.getDpiY());
1662 result.append(buffer);
1663
1664 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1665 inSwapBuffersDuration/1000.0);
1666 result.append(buffer);
1667
1668 snprintf(buffer, SIZE, " transaction time: %f us\n",
1669 inTransactionDuration/1000.0);
1670 result.append(buffer);
1671
1672 /*
1673 * VSYNC state
1674 */
1675 mEventThread->dump(result, buffer, SIZE);
1676
1677 /*
1678 * Dump HWComposer state
1679 */
1680 HWComposer& hwc(hw.getHwComposer());
1681 snprintf(buffer, SIZE, "h/w composer state:\n");
1682 result.append(buffer);
1683 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1684 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1685 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1686 result.append(buffer);
1687 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1688
1689 /*
1690 * Dump gralloc state
1691 */
1692 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1693 alloc.dump(result);
1694 hw.dump(result);
1695}
1696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697status_t SurfaceFlinger::onTransact(
1698 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1699{
1700 switch (code) {
1701 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001702 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001705 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001706 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 {
1708 // codes that require permission check
1709 IPCThreadState* ipc = IPCThreadState::self();
1710 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001711 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001712 if ((uid != AID_GRAPHICS) &&
1713 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001714 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001715 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1716 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001718 break;
1719 }
1720 case CAPTURE_SCREEN:
1721 {
1722 // codes that require permission check
1723 IPCThreadState* ipc = IPCThreadState::self();
1724 const int pid = ipc->getCallingPid();
1725 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001726 if ((uid != AID_GRAPHICS) &&
1727 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001728 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001729 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1730 return PERMISSION_DENIED;
1731 }
1732 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 }
1734 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001735
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1737 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001738 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001739 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001740 IPCThreadState* ipc = IPCThreadState::self();
1741 const int pid = ipc->getCallingPid();
1742 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001743 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001744 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 return PERMISSION_DENIED;
1746 }
1747 int n;
1748 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001749 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001750 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751 return NO_ERROR;
1752 case 1002: // SHOW_UPDATES
1753 n = data.readInt32();
1754 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001755 invalidateHwcGeometry();
1756 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 return NO_ERROR;
1758 case 1003: // SHOW_BACKGROUND
1759 n = data.readInt32();
1760 mDebugBackground = n ? 1 : 0;
1761 return NO_ERROR;
1762 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001763 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001764 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001766 case 1005:{ // force transaction
1767 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1768 return NO_ERROR;
1769 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001770 case 1008: // toggle use of hw composer
1771 n = data.readInt32();
1772 mDebugDisableHWC = n ? 1 : 0;
1773 invalidateHwcGeometry();
1774 repaintEverything();
1775 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001776 case 1009: // toggle use of transform hint
1777 n = data.readInt32();
1778 mDebugDisableTransformHint = n ? 1 : 0;
1779 invalidateHwcGeometry();
1780 repaintEverything();
1781 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001783 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784 reply->writeInt32(0);
1785 reply->writeInt32(mDebugRegion);
1786 reply->writeInt32(mDebugBackground);
1787 return NO_ERROR;
1788 case 1013: {
1789 Mutex::Autolock _l(mStateLock);
1790 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1791 reply->writeInt32(hw.getPageFlipCount());
1792 }
1793 return NO_ERROR;
1794 }
1795 }
1796 return err;
1797}
1798
Mathias Agopian53331da2011-08-22 21:44:41 -07001799void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001800 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001801 const Rect bounds(hw.getBounds());
1802 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001803 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001804}
1805
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001806void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1807 Mutex::Autolock _l(mInvalidateLock);
1808 mInvalidateRegion = reg;
1809}
1810
1811Region SurfaceFlinger::getAndClearInvalidateRegion() {
1812 Mutex::Autolock _l(mInvalidateLock);
1813 Region reg(mInvalidateRegion);
1814 mInvalidateRegion.clear();
1815 return reg;
1816}
1817
Mathias Agopian59119e62010-10-11 12:37:43 -07001818// ---------------------------------------------------------------------------
1819
Mathias Agopian118d0242011-10-13 16:02:48 -07001820status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1821 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1822{
1823 Mutex::Autolock _l(mStateLock);
1824 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1825}
1826
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001827status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1828 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001829{
Mathias Agopian59119e62010-10-11 12:37:43 -07001830 if (!GLExtensions::getInstance().haveFramebufferObject())
1831 return INVALID_OPERATION;
1832
1833 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001834 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001835 const uint32_t hw_w = hw.getWidth();
1836 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001837 GLfloat u = 1;
1838 GLfloat v = 1;
1839
1840 // make sure to clear all GL error flags
1841 while ( glGetError() != GL_NO_ERROR ) ;
1842
1843 // create a FBO
1844 GLuint name, tname;
1845 glGenTextures(1, &tname);
1846 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001847 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1848 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001849 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001850 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001851 GLint tw = (2 << (31 - clz(hw_w)));
1852 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001853 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1854 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001855 u = GLfloat(hw_w) / tw;
1856 v = GLfloat(hw_h) / th;
1857 }
1858 glGenFramebuffersOES(1, &name);
1859 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001860 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1861 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001862
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001863 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001864 glDisable(GL_TEXTURE_EXTERNAL_OES);
1865 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001866 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001867 glClearColor(0,0,0,1);
1868 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001869 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001870 glMatrixMode(GL_MODELVIEW);
1871 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001872 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1873 const size_t count = layers.size();
1874 for (size_t i=0 ; i<count ; ++i) {
1875 const sp<LayerBase>& layer(layers[i]);
1876 layer->drawForSreenShot();
1877 }
1878
Mathias Agopian118d0242011-10-13 16:02:48 -07001879 hw.compositionComplete();
1880
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001881 // back to main framebuffer
1882 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1883 glDisable(GL_SCISSOR_TEST);
1884 glDeleteFramebuffersOES(1, &name);
1885
1886 *textureName = tname;
1887 *uOut = u;
1888 *vOut = v;
1889 return NO_ERROR;
1890}
1891
1892// ---------------------------------------------------------------------------
1893
1894status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1895{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001896 // get screen geometry
1897 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1898 const uint32_t hw_w = hw.getWidth();
1899 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001900 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001901
1902 GLfloat u, v;
1903 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001904 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001905 if (result != NO_ERROR) {
1906 return result;
1907 }
1908
1909 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001910 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001911 glBindTexture(GL_TEXTURE_2D, tname);
1912 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1913 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1914 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1915 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1916 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1917 glVertexPointer(2, GL_FLOAT, 0, vtx);
1918
Mathias Agopianffcf4652011-07-07 17:30:31 -07001919 /*
1920 * Texture coordinate mapping
1921 *
1922 * u
1923 * 1 +----------+---+
1924 * | | | | image is inverted
1925 * | V | | w.r.t. the texture
1926 * 1-v +----------+ | coordinates
1927 * | |
1928 * | |
1929 * | |
1930 * 0 +--------------+
1931 * 0 1
1932 *
1933 */
1934
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001935 class s_curve_interpolator {
1936 const float nbFrames, s, v;
1937 public:
1938 s_curve_interpolator(int nbFrames, float s)
1939 : nbFrames(1.0f / (nbFrames-1)), s(s),
1940 v(1.0f + expf(-s + 0.5f*s)) {
1941 }
1942 float operator()(int f) {
1943 const float x = f * nbFrames;
1944 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1945 }
1946 };
1947
1948 class v_stretch {
1949 const GLfloat hw_w, hw_h;
1950 public:
1951 v_stretch(uint32_t hw_w, uint32_t hw_h)
1952 : hw_w(hw_w), hw_h(hw_h) {
1953 }
1954 void operator()(GLfloat* vtx, float v) {
1955 const GLfloat w = hw_w + (hw_w * v);
1956 const GLfloat h = hw_h - (hw_h * v);
1957 const GLfloat x = (hw_w - w) * 0.5f;
1958 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001959 vtx[0] = x; vtx[1] = y;
1960 vtx[2] = x; vtx[3] = y + h;
1961 vtx[4] = x + w; vtx[5] = y + h;
1962 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001963 }
1964 };
1965
1966 class h_stretch {
1967 const GLfloat hw_w, hw_h;
1968 public:
1969 h_stretch(uint32_t hw_w, uint32_t hw_h)
1970 : hw_w(hw_w), hw_h(hw_h) {
1971 }
1972 void operator()(GLfloat* vtx, float v) {
1973 const GLfloat w = hw_w - (hw_w * v);
1974 const GLfloat h = 1.0f;
1975 const GLfloat x = (hw_w - w) * 0.5f;
1976 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001977 vtx[0] = x; vtx[1] = y;
1978 vtx[2] = x; vtx[3] = y + h;
1979 vtx[4] = x + w; vtx[5] = y + h;
1980 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001981 }
1982 };
1983
1984 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001985 char value[PROPERTY_VALUE_MAX];
1986 property_get("debug.sf.electron_frames", value, "24");
1987 int nbFrames = (atoi(value) + 1) >> 1;
1988 if (nbFrames <= 0) // just in case
1989 nbFrames = 24;
1990
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001991 s_curve_interpolator itr(nbFrames, 7.5f);
1992 s_curve_interpolator itg(nbFrames, 8.0f);
1993 s_curve_interpolator itb(nbFrames, 8.5f);
1994
1995 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001996
1997 glMatrixMode(GL_TEXTURE);
1998 glLoadIdentity();
1999 glMatrixMode(GL_MODELVIEW);
2000 glLoadIdentity();
2001
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 glEnable(GL_BLEND);
2003 glBlendFunc(GL_ONE, GL_ONE);
2004 for (int i=0 ; i<nbFrames ; i++) {
2005 float x, y, w, h;
2006 const float vr = itr(i);
2007 const float vg = itg(i);
2008 const float vb = itb(i);
2009
2010 // clear screen
2011 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002012 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002013 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002014
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002015 // draw the red plane
2016 vverts(vtx, vr);
2017 glColorMask(1,0,0,1);
2018 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002019
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002020 // draw the green plane
2021 vverts(vtx, vg);
2022 glColorMask(0,1,0,1);
2023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002024
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025 // draw the blue plane
2026 vverts(vtx, vb);
2027 glColorMask(0,0,1,1);
2028 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002029
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002030 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002031 glDisable(GL_TEXTURE_2D);
2032 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 glColor4f(vg, vg, vg, 1);
2034 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2035 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002036 }
2037
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 h_stretch hverts(hw_w, hw_h);
2039 glDisable(GL_BLEND);
2040 glDisable(GL_TEXTURE_2D);
2041 glColorMask(1,1,1,1);
2042 for (int i=0 ; i<nbFrames ; i++) {
2043 const float v = itg(i);
2044 hverts(vtx, v);
2045 glClear(GL_COLOR_BUFFER_BIT);
2046 glColor4f(1-v, 1-v, 1-v, 1);
2047 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2048 hw.flip(screenBounds);
2049 }
2050
2051 glColorMask(1,1,1,1);
2052 glEnable(GL_SCISSOR_TEST);
2053 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2054 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002055 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002056 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 return NO_ERROR;
2058}
2059
2060status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2061{
2062 status_t result = PERMISSION_DENIED;
2063
2064 if (!GLExtensions::getInstance().haveFramebufferObject())
2065 return INVALID_OPERATION;
2066
2067
2068 // get screen geometry
2069 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2070 const uint32_t hw_w = hw.getWidth();
2071 const uint32_t hw_h = hw.getHeight();
2072 const Region screenBounds(hw.bounds());
2073
2074 GLfloat u, v;
2075 GLuint tname;
2076 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2077 if (result != NO_ERROR) {
2078 return result;
2079 }
2080
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002081 GLfloat vtx[8];
2082 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002083 glBindTexture(GL_TEXTURE_2D, tname);
2084 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2085 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2086 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2087 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2088 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2089 glVertexPointer(2, GL_FLOAT, 0, vtx);
2090
2091 class s_curve_interpolator {
2092 const float nbFrames, s, v;
2093 public:
2094 s_curve_interpolator(int nbFrames, float s)
2095 : nbFrames(1.0f / (nbFrames-1)), s(s),
2096 v(1.0f + expf(-s + 0.5f*s)) {
2097 }
2098 float operator()(int f) {
2099 const float x = f * nbFrames;
2100 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2101 }
2102 };
2103
2104 class v_stretch {
2105 const GLfloat hw_w, hw_h;
2106 public:
2107 v_stretch(uint32_t hw_w, uint32_t hw_h)
2108 : hw_w(hw_w), hw_h(hw_h) {
2109 }
2110 void operator()(GLfloat* vtx, float v) {
2111 const GLfloat w = hw_w + (hw_w * v);
2112 const GLfloat h = hw_h - (hw_h * v);
2113 const GLfloat x = (hw_w - w) * 0.5f;
2114 const GLfloat y = (hw_h - h) * 0.5f;
2115 vtx[0] = x; vtx[1] = y;
2116 vtx[2] = x; vtx[3] = y + h;
2117 vtx[4] = x + w; vtx[5] = y + h;
2118 vtx[6] = x + w; vtx[7] = y;
2119 }
2120 };
2121
2122 class h_stretch {
2123 const GLfloat hw_w, hw_h;
2124 public:
2125 h_stretch(uint32_t hw_w, uint32_t hw_h)
2126 : hw_w(hw_w), hw_h(hw_h) {
2127 }
2128 void operator()(GLfloat* vtx, float v) {
2129 const GLfloat w = hw_w - (hw_w * v);
2130 const GLfloat h = 1.0f;
2131 const GLfloat x = (hw_w - w) * 0.5f;
2132 const GLfloat y = (hw_h - h) * 0.5f;
2133 vtx[0] = x; vtx[1] = y;
2134 vtx[2] = x; vtx[3] = y + h;
2135 vtx[4] = x + w; vtx[5] = y + h;
2136 vtx[6] = x + w; vtx[7] = y;
2137 }
2138 };
2139
Mathias Agopiana6546e52010-10-14 12:33:07 -07002140 // the full animation is 12 frames
2141 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002142 s_curve_interpolator itr(nbFrames, 7.5f);
2143 s_curve_interpolator itg(nbFrames, 8.0f);
2144 s_curve_interpolator itb(nbFrames, 8.5f);
2145
2146 h_stretch hverts(hw_w, hw_h);
2147 glDisable(GL_BLEND);
2148 glDisable(GL_TEXTURE_2D);
2149 glColorMask(1,1,1,1);
2150 for (int i=nbFrames-1 ; i>=0 ; i--) {
2151 const float v = itg(i);
2152 hverts(vtx, v);
2153 glClear(GL_COLOR_BUFFER_BIT);
2154 glColor4f(1-v, 1-v, 1-v, 1);
2155 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2156 hw.flip(screenBounds);
2157 }
2158
Mathias Agopiana6546e52010-10-14 12:33:07 -07002159 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002160 v_stretch vverts(hw_w, hw_h);
2161 glEnable(GL_BLEND);
2162 glBlendFunc(GL_ONE, GL_ONE);
2163 for (int i=nbFrames-1 ; i>=0 ; i--) {
2164 float x, y, w, h;
2165 const float vr = itr(i);
2166 const float vg = itg(i);
2167 const float vb = itb(i);
2168
2169 // clear screen
2170 glColorMask(1,1,1,1);
2171 glClear(GL_COLOR_BUFFER_BIT);
2172 glEnable(GL_TEXTURE_2D);
2173
2174 // draw the red plane
2175 vverts(vtx, vr);
2176 glColorMask(1,0,0,1);
2177 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2178
2179 // draw the green plane
2180 vverts(vtx, vg);
2181 glColorMask(0,1,0,1);
2182 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2183
2184 // draw the blue plane
2185 vverts(vtx, vb);
2186 glColorMask(0,0,1,1);
2187 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2188
2189 hw.flip(screenBounds);
2190 }
2191
2192 glColorMask(1,1,1,1);
2193 glEnable(GL_SCISSOR_TEST);
2194 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002195 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002196 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002197 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002198
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002199 return NO_ERROR;
2200}
2201
2202// ---------------------------------------------------------------------------
2203
Mathias Agopianabd671a2010-10-14 14:54:06 -07002204status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205{
2206 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2207 if (!hw.canDraw()) {
2208 // we're already off
2209 return NO_ERROR;
2210 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002211
2212 // turn off hwc while we're doing the animation
2213 hw.getHwComposer().disable();
2214 // and make sure to turn it back on (if needed) next time we compose
2215 invalidateHwcGeometry();
2216
Mathias Agopianabd671a2010-10-14 14:54:06 -07002217 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2218 electronBeamOffAnimationImplLocked();
2219 }
2220
2221 // always clear the whole screen at the end of the animation
2222 glClearColor(0,0,0,1);
2223 glDisable(GL_SCISSOR_TEST);
2224 glClear(GL_COLOR_BUFFER_BIT);
2225 glEnable(GL_SCISSOR_TEST);
2226 hw.flip( Region(hw.bounds()) );
2227
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002228 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002229}
2230
2231status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2232{
Mathias Agopian59119e62010-10-11 12:37:43 -07002233 class MessageTurnElectronBeamOff : public MessageBase {
2234 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002235 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002236 status_t result;
2237 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002238 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2239 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002240 }
2241 status_t getResult() const {
2242 return result;
2243 }
2244 virtual bool handler() {
2245 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002246 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002247 return true;
2248 }
2249 };
2250
Mathias Agopianabd671a2010-10-14 14:54:06 -07002251 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002252 status_t res = postMessageSync(msg);
2253 if (res == NO_ERROR) {
2254 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002255
2256 // work-around: when the power-manager calls us we activate the
2257 // animation. eventually, the "on" animation will be called
2258 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002259 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002260 }
2261 return res;
2262}
2263
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002264// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002265
Mathias Agopianabd671a2010-10-14 14:54:06 -07002266status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002267{
2268 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2269 if (hw.canDraw()) {
2270 // we're already on
2271 return NO_ERROR;
2272 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002273 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2274 electronBeamOnAnimationImplLocked();
2275 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002276
2277 // make sure to redraw the whole screen when the animation is done
2278 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002279 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002280
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002281 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002282}
2283
2284status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2285{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002286 class MessageTurnElectronBeamOn : public MessageBase {
2287 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002288 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002289 status_t result;
2290 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002291 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2292 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002293 }
2294 status_t getResult() const {
2295 return result;
2296 }
2297 virtual bool handler() {
2298 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002299 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002300 return true;
2301 }
2302 };
2303
Mathias Agopianabd671a2010-10-14 14:54:06 -07002304 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002305 return NO_ERROR;
2306}
2307
2308// ---------------------------------------------------------------------------
2309
Mathias Agopian74c40c02010-09-29 13:02:36 -07002310status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2311 sp<IMemoryHeap>* heap,
2312 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002313 uint32_t sw, uint32_t sh,
2314 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002315{
2316 status_t result = PERMISSION_DENIED;
2317
2318 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002319 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002320 return BAD_VALUE;
2321
2322 if (!GLExtensions::getInstance().haveFramebufferObject())
2323 return INVALID_OPERATION;
2324
2325 // get screen geometry
2326 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2327 const uint32_t hw_w = hw.getWidth();
2328 const uint32_t hw_h = hw.getHeight();
2329
2330 if ((sw > hw_w) || (sh > hw_h))
2331 return BAD_VALUE;
2332
2333 sw = (!sw) ? hw_w : sw;
2334 sh = (!sh) ? hw_h : sh;
2335 const size_t size = sw * sh * 4;
2336
Steve Block9d453682011-12-20 16:23:08 +00002337 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002338 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002339
Mathias Agopian74c40c02010-09-29 13:02:36 -07002340 // make sure to clear all GL error flags
2341 while ( glGetError() != GL_NO_ERROR ) ;
2342
2343 // create a FBO
2344 GLuint name, tname;
2345 glGenRenderbuffersOES(1, &tname);
2346 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2347 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2348 glGenFramebuffersOES(1, &name);
2349 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2350 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2351 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2352
2353 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002354
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2356
2357 // invert everything, b/c glReadPixel() below will invert the FB
2358 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002359 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002360 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361 glMatrixMode(GL_PROJECTION);
2362 glPushMatrix();
2363 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002364 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002365 glMatrixMode(GL_MODELVIEW);
2366
2367 // redraw the screen entirely...
2368 glClearColor(0,0,0,1);
2369 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002370
Jamie Gennis9575f602011-10-07 14:51:16 -07002371 const LayerVector& layers(mDrawingState.layersSortedByZ);
2372 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 for (size_t i=0 ; i<count ; ++i) {
2374 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002375 const uint32_t flags = layer->drawingState().flags;
2376 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2377 const uint32_t z = layer->drawingState().z;
2378 if (z >= minLayerZ && z <= maxLayerZ) {
2379 layer->drawForSreenShot();
2380 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002381 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002382 }
2383
2384 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002385 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002386 glScissor(0, 0, sw, sh);
2387
2388 // check for errors and return screen capture
2389 if (glGetError() != GL_NO_ERROR) {
2390 // error while rendering
2391 result = INVALID_OPERATION;
2392 } else {
2393 // allocate shared memory large enough to hold the
2394 // screen capture
2395 sp<MemoryHeapBase> base(
2396 new MemoryHeapBase(size, 0, "screen-capture") );
2397 void* const ptr = base->getBase();
2398 if (ptr) {
2399 // capture the screen with glReadPixels()
2400 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2401 if (glGetError() == GL_NO_ERROR) {
2402 *heap = base;
2403 *w = sw;
2404 *h = sh;
2405 *f = PIXEL_FORMAT_RGBA_8888;
2406 result = NO_ERROR;
2407 }
2408 } else {
2409 result = NO_MEMORY;
2410 }
2411 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 glEnable(GL_SCISSOR_TEST);
2413 glViewport(0, 0, hw_w, hw_h);
2414 glMatrixMode(GL_PROJECTION);
2415 glPopMatrix();
2416 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417 } else {
2418 result = BAD_VALUE;
2419 }
2420
2421 // release FBO resources
2422 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2423 glDeleteRenderbuffersOES(1, &tname);
2424 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002425
2426 hw.compositionComplete();
2427
Steve Block9d453682011-12-20 16:23:08 +00002428 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002429
Mathias Agopian74c40c02010-09-29 13:02:36 -07002430 return result;
2431}
2432
2433
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002434status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2435 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002437 uint32_t sw, uint32_t sh,
2438 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002439{
2440 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002441 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002442 return BAD_VALUE;
2443
2444 if (!GLExtensions::getInstance().haveFramebufferObject())
2445 return INVALID_OPERATION;
2446
2447 class MessageCaptureScreen : public MessageBase {
2448 SurfaceFlinger* flinger;
2449 DisplayID dpy;
2450 sp<IMemoryHeap>* heap;
2451 uint32_t* w;
2452 uint32_t* h;
2453 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002454 uint32_t sw;
2455 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002456 uint32_t minLayerZ;
2457 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458 status_t result;
2459 public:
2460 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002461 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002462 uint32_t sw, uint32_t sh,
2463 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002464 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002465 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2466 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2467 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002468 {
2469 }
2470 status_t getResult() const {
2471 return result;
2472 }
2473 virtual bool handler() {
2474 Mutex::Autolock _l(flinger->mStateLock);
2475
2476 // if we have secure windows, never allow the screen capture
2477 if (flinger->mSecureFrameBuffer)
2478 return true;
2479
Mathias Agopian74c40c02010-09-29 13:02:36 -07002480 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002481 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002482
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483 return true;
2484 }
2485 };
2486
2487 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002488 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489 status_t res = postMessageSync(msg);
2490 if (res == NO_ERROR) {
2491 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2492 }
2493 return res;
2494}
2495
2496// ---------------------------------------------------------------------------
2497
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002498sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2499{
2500 sp<Layer> result;
2501 Mutex::Autolock _l(mStateLock);
2502 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2503 return result;
2504}
2505
2506// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507
Mathias Agopian96f08192010-06-02 23:28:45 -07002508Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002509 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511}
2512
Mathias Agopian96f08192010-06-02 23:28:45 -07002513Client::~Client()
2514{
Mathias Agopian96f08192010-06-02 23:28:45 -07002515 const size_t count = mLayers.size();
2516 for (size_t i=0 ; i<count ; i++) {
2517 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2518 if (layer != 0) {
2519 mFlinger->removeLayer(layer);
2520 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521 }
2522}
2523
Mathias Agopian96f08192010-06-02 23:28:45 -07002524status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002525 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002527
Mathias Agopian4f113742011-05-03 16:21:41 -07002528size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002529{
Mathias Agopian4f113742011-05-03 16:21:41 -07002530 Mutex::Autolock _l(mLock);
2531 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002532 mLayers.add(name, layer);
2533 return name;
2534}
2535
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002536void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002537{
Mathias Agopian4f113742011-05-03 16:21:41 -07002538 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002539 // we do a linear search here, because this doesn't happen often
2540 const size_t count = mLayers.size();
2541 for (size_t i=0 ; i<count ; i++) {
2542 if (mLayers.valueAt(i) == layer) {
2543 mLayers.removeItemsAt(i, 1);
2544 break;
2545 }
2546 }
2547}
Mathias Agopian4f113742011-05-03 16:21:41 -07002548sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2549{
2550 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002551 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002552 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002553 if (layer != 0) {
2554 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002555 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002556 }
2557 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002558}
2559
Mathias Agopiana67932f2011-04-20 14:20:59 -07002560
2561status_t Client::onTransact(
2562 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2563{
2564 // these must be checked
2565 IPCThreadState* ipc = IPCThreadState::self();
2566 const int pid = ipc->getCallingPid();
2567 const int uid = ipc->getCallingUid();
2568 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002569 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002570 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002571 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002572 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002573 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002574 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2575 return PERMISSION_DENIED;
2576 }
2577 }
2578 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002579}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002580
2581
Mathias Agopian96f08192010-06-02 23:28:45 -07002582sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002583 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002584 const String8& name,
2585 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002586 uint32_t flags)
2587{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002588 /*
2589 * createSurface must be called from the GL thread so that it can
2590 * have access to the GL context.
2591 */
2592
2593 class MessageCreateSurface : public MessageBase {
2594 sp<ISurface> result;
2595 SurfaceFlinger* flinger;
2596 ISurfaceComposerClient::surface_data_t* params;
2597 Client* client;
2598 const String8& name;
2599 DisplayID display;
2600 uint32_t w, h;
2601 PixelFormat format;
2602 uint32_t flags;
2603 public:
2604 MessageCreateSurface(SurfaceFlinger* flinger,
2605 ISurfaceComposerClient::surface_data_t* params,
2606 const String8& name, Client* client,
2607 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2608 uint32_t flags)
2609 : flinger(flinger), params(params), client(client), name(name),
2610 display(display), w(w), h(h), format(format), flags(flags)
2611 {
2612 }
2613 sp<ISurface> getResult() const { return result; }
2614 virtual bool handler() {
2615 result = flinger->createSurface(params, name, client,
2616 display, w, h, format, flags);
2617 return true;
2618 }
2619 };
2620
2621 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2622 params, name, this, display, w, h, format, flags);
2623 mFlinger->postMessageSync(msg);
2624 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002625}
Mathias Agopian96f08192010-06-02 23:28:45 -07002626status_t Client::destroySurface(SurfaceID sid) {
2627 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629
2630// ---------------------------------------------------------------------------
2631
Jamie Gennis9a78c902011-01-12 18:30:40 -08002632GraphicBufferAlloc::GraphicBufferAlloc() {}
2633
2634GraphicBufferAlloc::~GraphicBufferAlloc() {}
2635
2636sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002637 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002638 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2639 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002640 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002641 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002642 if (err == NO_MEMORY) {
2643 GraphicBuffer::dumpAllocationsToSystemLog();
2644 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002645 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002646 "failed (%s), handle=%p",
2647 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002648 return 0;
2649 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002650 return graphicBuffer;
2651}
2652
Jamie Gennis9a78c902011-01-12 18:30:40 -08002653// ---------------------------------------------------------------------------
2654
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002655GraphicPlane::GraphicPlane()
2656 : mHw(0)
2657{
2658}
2659
2660GraphicPlane::~GraphicPlane() {
2661 delete mHw;
2662}
2663
2664bool GraphicPlane::initialized() const {
2665 return mHw ? true : false;
2666}
2667
Mathias Agopian2b92d892010-02-08 15:49:35 -08002668int GraphicPlane::getWidth() const {
2669 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002670}
2671
Mathias Agopian2b92d892010-02-08 15:49:35 -08002672int GraphicPlane::getHeight() const {
2673 return mHeight;
2674}
2675
2676void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2677{
2678 mHw = hw;
2679
2680 // initialize the display orientation transform.
2681 // it's a constant that should come from the display driver.
2682 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2683 char property[PROPERTY_VALUE_MAX];
2684 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2685 //displayOrientation
2686 switch (atoi(property)) {
2687 case 90:
2688 displayOrientation = ISurfaceComposer::eOrientation90;
2689 break;
2690 case 270:
2691 displayOrientation = ISurfaceComposer::eOrientation270;
2692 break;
2693 }
2694 }
2695
2696 const float w = hw->getWidth();
2697 const float h = hw->getHeight();
2698 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2699 &mDisplayTransform);
2700 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2701 mDisplayWidth = h;
2702 mDisplayHeight = w;
2703 } else {
2704 mDisplayWidth = w;
2705 mDisplayHeight = h;
2706 }
2707
2708 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002709}
2710
2711status_t GraphicPlane::orientationToTransfrom(
2712 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002713{
2714 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002715 switch (orientation) {
2716 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002717 flags = Transform::ROT_0;
2718 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002719 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002720 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002721 break;
2722 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002723 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002724 break;
2725 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002726 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002727 break;
2728 default:
2729 return BAD_VALUE;
2730 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002731 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002732 return NO_ERROR;
2733}
2734
2735status_t GraphicPlane::setOrientation(int orientation)
2736{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002737 // If the rotation can be handled in hardware, this is where
2738 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002739
2740 const DisplayHardware& hw(displayHardware());
2741 const float w = mDisplayWidth;
2742 const float h = mDisplayHeight;
2743 mWidth = int(w);
2744 mHeight = int(h);
2745
2746 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002747 GraphicPlane::orientationToTransfrom(orientation, w, h,
2748 &orientationTransform);
2749 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2750 mWidth = int(h);
2751 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002752 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002753
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002754 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002755 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002756 return NO_ERROR;
2757}
2758
2759const DisplayHardware& GraphicPlane::displayHardware() const {
2760 return *mHw;
2761}
2762
Mathias Agopian59119e62010-10-11 12:37:43 -07002763DisplayHardware& GraphicPlane::editDisplayHardware() {
2764 return *mHw;
2765}
2766
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002767const Transform& GraphicPlane::transform() const {
2768 return mGlobalTransform;
2769}
2770
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002771EGLDisplay GraphicPlane::getEGLDisplay() const {
2772 return mHw->getEGLDisplay();
2773}
2774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002775// ---------------------------------------------------------------------------
2776
2777}; // namespace android