blob: 0563999831438998d43bdcd074d76f12c35f1024 [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
128SurfaceFlinger::~SurfaceFlinger()
129{
130 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700133sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700135 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136}
137
Mathias Agopian7e27f052010-05-28 14:22:23 -0700138sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139{
Mathias Agopian96f08192010-06-02 23:28:45 -0700140 sp<ISurfaceComposerClient> bclient;
141 sp<Client> client(new Client(this));
142 status_t err = client->initCheck();
143 if (err == NO_ERROR) {
144 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 return bclient;
147}
148
Jamie Gennis9a78c902011-01-12 18:30:40 -0800149sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
150{
151 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
152 return gba;
153}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700154
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
156{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000157 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 const GraphicPlane& plane(mGraphicPlanes[dpy]);
159 return plane;
160}
161
162GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
163{
164 return const_cast<GraphicPlane&>(
165 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
166}
167
168void SurfaceFlinger::bootFinished()
169{
170 const nsecs_t now = systemTime();
171 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000172 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700174
175 // wait patiently for the window manager death
176 const String16 name("window");
177 sp<IBinder> window(defaultServiceManager()->getService(name));
178 if (window != 0) {
179 window->linkToDeath(this);
180 }
181
182 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700183 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700186void SurfaceFlinger::binderDied(const wp<IBinder>& who)
187{
188 // the window manager died on us. prepare its eulogy.
189
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700190 // reset screen orientation
191 setOrientation(0, eOrientationDefault, 0);
192
193 // restart the boot-animation
194 property_set("ctl.start", "bootanim");
195}
196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197void SurfaceFlinger::onFirstRef()
198{
199 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
200
201 // Wait for the main thread to be done with its initialization
202 mReadyToRunBarrier.wait();
203}
204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205static inline uint16_t pack565(int r, int g, int b) {
206 return (r<<11)|(g<<5)|b;
207}
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209status_t SurfaceFlinger::readyToRun()
210{
Steve Blocka19954a2012-01-04 20:05:49 +0000211 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 "Initializing graphics H/W...");
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 // we only support one display currently
215 int dpy = 0;
216
217 {
218 // initialize the main display
219 GraphicPlane& plane(graphicPlane(dpy));
220 DisplayHardware* const hw = new DisplayHardware(this, dpy);
221 plane.setDisplayHardware(hw);
222 }
223
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700224 // create the shared control-block
225 mServerHeap = new MemoryHeapBase(4096,
226 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700228
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700229 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000230 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700231
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700232 new(mServerCblk) surface_flinger_cblk_t;
233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 // initialize primary screen
235 // (other display should be initialized in the same manner, but
236 // asynchronously, as they could come and go. None of this is supported
237 // yet).
238 const GraphicPlane& plane(graphicPlane(dpy));
239 const DisplayHardware& hw = plane.displayHardware();
240 const uint32_t w = hw.getWidth();
241 const uint32_t h = hw.getHeight();
242 const uint32_t f = hw.getFormat();
243 hw.makeCurrent();
244
245 // initialize the shared control block
246 mServerCblk->connected |= 1<<dpy;
247 display_cblk_t* dcblk = mServerCblk->displays + dpy;
248 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800249 dcblk->w = plane.getWidth();
250 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 dcblk->format = f;
252 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
253 dcblk->xdpi = hw.getDpiX();
254 dcblk->ydpi = hw.getDpiY();
255 dcblk->fps = hw.getRefreshRate();
256 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
258 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700260 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 glEnableClientState(GL_VERTEX_ARRAY);
262 glEnable(GL_SCISSOR_TEST);
263 glShadeModel(GL_FLAT);
264 glDisable(GL_DITHER);
265 glDisable(GL_CULL_FACE);
266
267 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
268 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700269 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 glGenTextures(1, &mWormholeTexName);
271 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
272 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
276 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
278
279 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
280 glGenTextures(1, &mProtectedTexName);
281 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
287 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
289 glViewport(0, 0, w, h);
290 glMatrixMode(GL_PROJECTION);
291 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700292 // put the origin in the left-bottom corner
293 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 -0800294
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800295
296 // start the EventThread
297 mEventThread = new EventThread(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
299 /*
300 * We're now ready to accept clients...
301 */
302
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800303 mReadyToRunBarrier.open();
304
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700305 // start boot animation
306 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700307
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 return NO_ERROR;
309}
310
311// ----------------------------------------------------------------------------
312#if 0
313#pragma mark -
314#pragma mark Events Handler
315#endif
316
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800317void SurfaceFlinger::waitForEvent() {
318 mEventQueue.waitMessage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319}
320
321void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700322 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}
324
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800325status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
326 nsecs_t reltime, uint32_t flags) {
327 return mEventQueue.postMessage(msg, reltime);
328}
329
330status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
331 nsecs_t reltime, uint32_t flags) {
332 status_t res = mEventQueue.postMessage(msg, reltime);
333 if (res == NO_ERROR) {
334 msg->wait();
335 }
336 return res;
337}
338
339// ----------------------------------------------------------------------------
340
Jamie Gennis582270d2011-08-17 18:19:00 -0700341bool SurfaceFlinger::authenticateSurfaceTexture(
342 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800343 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700344 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800345
346 // Check the visible layer list for the ISurface
347 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
348 size_t count = currentLayers.size();
349 for (size_t i=0 ; i<count ; i++) {
350 const sp<LayerBase>& layer(currentLayers[i]);
351 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700352 if (lbc != NULL) {
353 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
354 if (lbcBinder == surfaceTextureBinder) {
355 return true;
356 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800357 }
358 }
359
360 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700361 // SurfaceTexture gets destroyed before all the clients are done using it,
362 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800363 // will instead fail later on when the client tries to use the surface,
364 // which should be reported as "surface XYZ returned an -ENODEV". The
365 // purgatorized layers are no less authentic than the visible ones, so this
366 // should not cause any harm.
367 size_t purgatorySize = mLayerPurgatory.size();
368 for (size_t i=0 ; i<purgatorySize ; i++) {
369 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
370 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700371 if (lbc != NULL) {
372 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
373 if (lbcBinder == surfaceTextureBinder) {
374 return true;
375 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800376 }
377 }
378
379 return false;
380}
381
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800382// ----------------------------------------------------------------------------
383
384sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800385 sp<DisplayEventConnection> result(new DisplayEventConnection(mEventThread));
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800386 return result;
Mathias Agopianbb641242010-05-18 17:06:55 -0700387}
388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389// ----------------------------------------------------------------------------
390#if 0
391#pragma mark -
392#pragma mark Main loop
393#endif
394
395bool SurfaceFlinger::threadLoop()
396{
397 waitForEvent();
398
399 // check for transactions
Glenn Kasten99ed2242011-12-15 09:51:17 -0800400 if (CC_UNLIKELY(mConsoleSignals)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401 handleConsoleEvents();
402 }
403
Mathias Agopian698c0872011-06-28 19:09:31 -0700404 // if we're in a global transaction, don't do anything.
405 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
406 uint32_t transactionFlags = peekTransactionFlags(mask);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800407 if (CC_UNLIKELY(transactionFlags)) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700408 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 }
410
411 // post surfaces (if needed)
412 handlePageFlip();
413
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700414 if (mDirtyRegion.isEmpty()) {
415 // nothing new to do.
416 return true;
417 }
418
Glenn Kasten99ed2242011-12-15 09:51:17 -0800419 if (CC_UNLIKELY(mHwWorkListDirty)) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700420 // build the h/w work list
421 handleWorkList();
422 }
423
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Glenn Kasten99ed2242011-12-15 09:51:17 -0800425 if (CC_LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 // repaint the framebuffer (if needed)
427 handleRepaint();
Mathias Agopian74faca22009-09-17 16:18:16 -0700428 // inform the h/w that we're done compositing
429 hw.compositionComplete();
Antti Hatala8392b502010-09-08 06:37:14 -0700430 postFramebuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 } else {
432 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800433 hw.compositionComplete();
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800434 hw.waitForRefresh();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 }
436 return true;
437}
438
439void SurfaceFlinger::postFramebuffer()
440{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700441 // this should never happen. we do the flip anyways so we don't
442 // risk to cause a deadlock with hwc
Steve Block32397c12012-01-05 23:22:43 +0000443 ALOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700444 const DisplayHardware& hw(graphicPlane(0).displayHardware());
445 const nsecs_t now = systemTime();
446 mDebugInSwapBuffers = now;
447 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800448
449 size_t numLayers = mVisibleLayersSortedByZ.size();
450 for (size_t i = 0; i < numLayers; i++) {
451 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
452 }
453
Mathias Agopiana44b0412011-10-16 18:46:35 -0700454 mLastSwapBufferTime = systemTime() - now;
455 mDebugInSwapBuffers = 0;
456 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457}
458
459void SurfaceFlinger::handleConsoleEvents()
460{
461 // something to do with the console
462 const DisplayHardware& hw = graphicPlane(0).displayHardware();
463
464 int what = android_atomic_and(0, &mConsoleSignals);
465 if (what & eConsoleAcquired) {
466 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700467 // this is a temporary work-around, eventually this should be called
468 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700469 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 }
471
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700473 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 }
476 }
477
478 mDirtyRegion.set(hw.bounds());
479}
480
481void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
482{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700483 Mutex::Autolock _l(mStateLock);
484 const nsecs_t now = systemTime();
485 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486
Mathias Agopianca4d3602011-05-19 15:38:14 -0700487 // Here we're guaranteed that some transaction flags are set
488 // so we can call handleTransactionLocked() unconditionally.
489 // We call getTransactionFlags(), which will also clear the flags,
490 // with mStateLock held to guarantee that mCurrentState won't change
491 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700492
Mathias Agopianca4d3602011-05-19 15:38:14 -0700493 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
494 transactionFlags = getTransactionFlags(mask);
495 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700496
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497 mLastTransactionTime = systemTime() - now;
498 mDebugInTransaction = 0;
499 invalidateHwcGeometry();
500 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700501}
502
Mathias Agopianca4d3602011-05-19 15:38:14 -0700503void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700504{
505 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 const size_t count = currentLayers.size();
507
508 /*
509 * Traversal of the children
510 * (perform the transaction for each of them if needed)
511 */
512
513 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
514 if (layersNeedTransaction) {
515 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700516 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
518 if (!trFlags) continue;
519
520 const uint32_t flags = layer->doTransaction(0);
521 if (flags & Layer::eVisibleRegion)
522 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 }
524 }
525
526 /*
527 * Perform our own transaction if needed
528 */
529
530 if (transactionFlags & eTransactionNeeded) {
531 if (mCurrentState.orientation != mDrawingState.orientation) {
532 // the orientation has changed, recompute all visible regions
533 // and invalidate everything.
534
535 const int dpy = 0;
536 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700537 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 GraphicPlane& plane(graphicPlane(dpy));
539 plane.setOrientation(orientation);
540
541 // update the shared control block
542 const DisplayHardware& hw(plane.displayHardware());
543 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
544 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800545 dcblk->w = plane.getWidth();
546 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547
548 mVisibleRegionsDirty = true;
549 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 }
551
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700552 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
553 // layers have been added
554 mVisibleRegionsDirty = true;
555 }
556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 // some layers might have been removed, so
558 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700560 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700562 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700563 const size_t count = previousLayers.size();
564 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700565 const sp<LayerBase>& layer(previousLayers[i]);
566 if (currentLayers.indexOf( layer ) < 0) {
567 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700568 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700569 }
570 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 }
573
574 commitTransaction();
575}
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800578 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579{
580 const GraphicPlane& plane(graphicPlane(0));
581 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700582 const DisplayHardware& hw(plane.displayHardware());
583 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584
585 Region aboveOpaqueLayers;
586 Region aboveCoveredLayers;
587 Region dirty;
588
589 bool secureFrameBuffer = false;
590
591 size_t i = currentLayers.size();
592 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594 layer->validateVisibility(planeTransform);
595
596 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700597 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598
Mathias Agopianab028732010-03-16 16:41:46 -0700599 /*
600 * opaqueRegion: area of a surface that is fully opaque.
601 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700603
604 /*
605 * visibleRegion: area of a surface that is visible on screen
606 * and not fully transparent. This is essentially the layer's
607 * footprint minus the opaque regions above it.
608 * Areas covered by a translucent surface are considered visible.
609 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700611
612 /*
613 * coveredRegion: area of a surface that is covered by all
614 * visible regions above it (which includes the translucent areas).
615 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700617
618
619 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800620 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700621 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700622 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700624 visibleRegion.andSelf(screenRegion);
625 if (!visibleRegion.isEmpty()) {
626 // Remove the transparent area from the visible region
627 if (translucent) {
628 visibleRegion.subtractSelf(layer->transparentRegionScreen);
629 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630
Mathias Agopianab028732010-03-16 16:41:46 -0700631 // compute the opaque region
632 const int32_t layerOrientation = layer->getOrientation();
633 if (s.alpha==255 && !translucent &&
634 ((layerOrientation & Transform::ROT_INVALID) == false)) {
635 // the opaque region is the layer's footprint
636 opaqueRegion = visibleRegion;
637 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 }
639 }
640
Mathias Agopianab028732010-03-16 16:41:46 -0700641 // Clip the covered region to the visible region
642 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
643
644 // Update aboveCoveredLayers for next (lower) layer
645 aboveCoveredLayers.orSelf(visibleRegion);
646
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 // subtract the opaque region covered by the layers above us
648 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649
650 // compute this layer's dirty region
651 if (layer->contentDirty) {
652 // we need to invalidate the whole region
653 dirty = visibleRegion;
654 // as well, as the old visible region
655 dirty.orSelf(layer->visibleRegionScreen);
656 layer->contentDirty = false;
657 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700658 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700659 * the exposed region consists of two components:
660 * 1) what's VISIBLE now and was COVERED before
661 * 2) what's EXPOSED now less what was EXPOSED before
662 *
663 * note that (1) is conservative, we start with the whole
664 * visible region but only keep what used to be covered by
665 * something -- which mean it may have been exposed.
666 *
667 * (2) handles areas that were not covered by anything but got
668 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700669 */
Mathias Agopianab028732010-03-16 16:41:46 -0700670 const Region newExposed = visibleRegion - coveredRegion;
671 const Region oldVisibleRegion = layer->visibleRegionScreen;
672 const Region oldCoveredRegion = layer->coveredRegionScreen;
673 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
674 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 }
676 dirty.subtractSelf(aboveOpaqueLayers);
677
678 // accumulate to the screen dirty region
679 dirtyRegion.orSelf(dirty);
680
Mathias Agopianab028732010-03-16 16:41:46 -0700681 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700683
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 // Store the visible region is screen space
685 layer->setVisibleRegion(visibleRegion);
686 layer->setCoveredRegion(coveredRegion);
687
Mathias Agopian97011222009-07-28 10:57:27 -0700688 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 if (layer->isSecure() && !visibleRegion.isEmpty()) {
690 secureFrameBuffer = true;
691 }
692 }
693
Mathias Agopian97011222009-07-28 10:57:27 -0700694 // invalidate the areas where a layer was removed
695 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
696 mDirtyRegionRemovedLayer.clear();
697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 mSecureFrameBuffer = secureFrameBuffer;
699 opaqueRegion = aboveOpaqueLayers;
700}
701
702
703void SurfaceFlinger::commitTransaction()
704{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800705 if (!mLayersPendingRemoval.isEmpty()) {
706 // Notify removed layers now that they can't be drawn from
707 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
708 mLayersPendingRemoval[i]->onRemoved();
709 }
710 mLayersPendingRemoval.clear();
711 }
712
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700714 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700715 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716}
717
718void SurfaceFlinger::handlePageFlip()
719{
720 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 visibleRegions |= lockPageFlip(currentLayers);
723
724 const DisplayHardware& hw = graphicPlane(0).displayHardware();
725 const Region screenRegion(hw.bounds());
726 if (visibleRegions) {
727 Region opaqueRegion;
728 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700729
730 /*
731 * rebuild the visible layer list
732 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800733 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700734 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700735 mVisibleLayersSortedByZ.setCapacity(count);
736 for (size_t i=0 ; i<count ; i++) {
737 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
738 mVisibleLayersSortedByZ.add(currentLayers[i]);
739 }
740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 mWormholeRegion = screenRegion.subtract(opaqueRegion);
742 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800743 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 }
745
746 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700747
748 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 mDirtyRegion.andSelf(screenRegion);
750}
751
Mathias Agopianad456f92011-01-13 17:53:01 -0800752void SurfaceFlinger::invalidateHwcGeometry()
753{
754 mHwWorkListDirty = true;
755}
756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
758{
759 bool recomputeVisibleRegions = false;
760 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700761 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700763 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 layer->lockPageFlip(recomputeVisibleRegions);
765 }
766 return recomputeVisibleRegions;
767}
768
769void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
770{
771 const GraphicPlane& plane(graphicPlane(0));
772 const Transform& planeTransform(plane.transform());
773 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 layer->unlockPageFlip(planeTransform, mDirtyRegion);
778 }
779}
780
Mathias Agopiana350ff92010-08-10 17:14:02 -0700781void SurfaceFlinger::handleWorkList()
782{
783 mHwWorkListDirty = false;
784 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
785 if (hwc.initCheck() == NO_ERROR) {
786 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
787 const size_t count = currentLayers.size();
788 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700789 hwc_layer_t* const cur(hwc.getLayers());
790 for (size_t i=0 ; cur && i<count ; i++) {
791 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700792 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700793 cur[i].compositionType = HWC_FRAMEBUFFER;
794 cur[i].flags |= HWC_SKIP_LAYER;
795 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700796 }
797 }
798}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700799
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800void SurfaceFlinger::handleRepaint()
801{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700802 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700803 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804
Glenn Kasten99ed2242011-12-15 09:51:17 -0800805 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 debugFlashRegions();
807 }
808
Mathias Agopianb8a55602009-06-26 19:06:36 -0700809 // set the frame buffer
810 const DisplayHardware& hw(graphicPlane(0).displayHardware());
811 glMatrixMode(GL_MODELVIEW);
812 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813
814 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700815 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
816 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700817 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700818 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
819 // takes a rectangle, we must make sure to update that whole
820 // rectangle in that case
821 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700822 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700823 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700824 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700826 // in the BUFFER_PRESERVED case, obviously, we can update only
827 // what's needed and nothing more.
828 // NOTE: this is NOT a common case, as preserving the backbuffer
829 // is costly and usually involves copying the whole update back.
830 }
831 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700832 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700833 // We need to redraw the rectangle that will be updated
834 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700835 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700837 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 } else {
839 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700841 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 }
843 }
844
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700845 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 composeSurfaces(mDirtyRegion);
847
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700848 // update the swap region and clear the dirty region
849 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mDirtyRegion.clear();
851}
852
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700853void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700855 const DisplayHardware& hw(graphicPlane(0).displayHardware());
856 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700857 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700858 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700859 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700860 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700861
Mathias Agopianf384cc32011-09-08 18:31:55 -0700862 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
863 size_t count = layers.size();
864
Steve Blocke6f43dd2012-01-06 19:20:56 +0000865 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700866 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
867 hwc.getNumLayers(), count);
868
869 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700870 if (hwc.initCheck() == NO_ERROR) {
871 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
872 }
Mathias Agopian45721772010-08-12 15:03:26 -0700873
874 /*
875 * update the per-frame h/w composer data for each layer
876 * and build the transparent region of the FB
877 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700878 for (size_t i=0 ; i<count ; i++) {
879 const sp<LayerBase>& layer(layers[i]);
880 layer->setPerFrameData(&cur[i]);
881 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700882 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700883 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000884 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700885
Mathias Agopianf384cc32011-09-08 18:31:55 -0700886 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700887 // what's happening here is tricky.
888 // we want to clear all the layers with the CLEAR_FB flags
889 // that are opaque.
890 // however, since some GPU are efficient at preserving
891 // the backbuffer, we want to take advantage of that so we do the
892 // clear only in the dirty region (other areas will be preserved
893 // on those GPUs).
894 // NOTE: on non backbuffer preserving GPU, the dirty region
895 // has already been expanded as needed, so the code is correct
896 // there too.
897 //
898 // However, the content of the framebuffer cannot be trusted when
899 // we switch to/from FB/OVERLAY, in which case we need to
900 // expand the dirty region to those areas too.
901 //
902 // Note also that there is a special case when switching from
903 // "no layers in FB" to "some layers in FB", where we need to redraw
904 // the entire FB, since some areas might contain uninitialized
905 // data.
906 //
907 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700908 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700909 // that is, we can't erase anything outside the dirty region.
910
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700911 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700912
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700913 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
914 transparent.set(hw.getBounds());
915 dirtyInOut = transparent;
916 } else {
917 for (size_t i=0 ; i<count ; i++) {
918 const sp<LayerBase>& layer(layers[i]);
919 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
920 transparent.orSelf(layer->visibleRegionScreen);
921 }
922 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
923 if (isOverlay != layer->isOverlay()) {
924 // we transitioned to/from overlay, so add this layer
925 // to the dirty region so the framebuffer can be either
926 // cleared or redrawn.
927 dirtyInOut.orSelf(layer->visibleRegionScreen);
928 }
929 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800930 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700931 // don't erase stuff outside the dirty region
932 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700933 }
934
935 /*
936 * clear the area of the FB that need to be transparent
937 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700938 if (!transparent.isEmpty()) {
939 glClearColor(0,0,0,0);
940 Region::const_iterator it = transparent.begin();
941 Region::const_iterator const end = transparent.end();
942 const int32_t height = hw.getHeight();
943 while (it != end) {
944 const Rect& r(*it++);
945 const GLint sy = height - (r.top + r.height());
946 glScissor(r.left, sy, r.width(), r.height());
947 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800948 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700949 }
950 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700951}
Mathias Agopian45721772010-08-12 15:03:26 -0700952
Mathias Agopianf384cc32011-09-08 18:31:55 -0700953void SurfaceFlinger::composeSurfaces(const Region& dirty)
954{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700955 const DisplayHardware& hw(graphicPlane(0).displayHardware());
956 HWComposer& hwc(hw.getHwComposer());
957
958 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800959 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700960 // should never happen unless the window manager has a bug
961 // draw something...
962 drawWormhole();
963 }
964
Mathias Agopian45721772010-08-12 15:03:26 -0700965 /*
966 * and then, render the layers targeted at the framebuffer
967 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700968 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700969 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
970 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700971 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700972 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700973 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700974 }
975 const sp<LayerBase>& layer(layers[i]);
976 const Region clip(dirty.intersect(layer->visibleRegionScreen));
977 if (!clip.isEmpty()) {
978 layer->draw(clip);
979 }
980 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981}
982
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983void SurfaceFlinger::debugFlashRegions()
984{
Mathias Agopian0a917752010-06-14 21:20:00 -0700985 const DisplayHardware& hw(graphicPlane(0).displayHardware());
986 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700987 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700988 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700989 return;
990 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700991
Mathias Agopian0a917752010-06-14 21:20:00 -0700992 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
993 (flags & DisplayHardware::BUFFER_PRESERVED))) {
994 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
995 mDirtyRegion.bounds() : hw.bounds());
996 composeSurfaces(repaint);
997 }
998
Mathias Agopianc492e672011-10-18 14:49:27 -0700999 glDisable(GL_TEXTURE_EXTERNAL_OES);
1000 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 glDisable(GL_SCISSOR_TEST);
1003
Mathias Agopian0926f502009-05-04 14:17:04 -07001004 static int toggle = 0;
1005 toggle = 1 - toggle;
1006 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001007 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001008 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001009 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001010 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
Mathias Agopian20f68782009-05-11 00:03:41 -07001012 Region::const_iterator it = mDirtyRegion.begin();
1013 Region::const_iterator const end = mDirtyRegion.end();
1014 while (it != end) {
1015 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001017 { r.left, height - r.top },
1018 { r.left, height - r.bottom },
1019 { r.right, height - r.bottom },
1020 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 };
1022 glVertexPointer(2, GL_FLOAT, 0, vertices);
1023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1024 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001025
Mathias Agopian0656a682011-09-20 17:22:44 -07001026 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027
1028 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001029 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030
1031 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032}
1033
1034void SurfaceFlinger::drawWormhole() const
1035{
1036 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1037 if (region.isEmpty())
1038 return;
1039
1040 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1041 const int32_t width = hw.getWidth();
1042 const int32_t height = hw.getHeight();
1043
Glenn Kasten99ed2242011-12-15 09:51:17 -08001044 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001045 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001046 Region::const_iterator it = region.begin();
1047 Region::const_iterator const end = region.end();
1048 while (it != end) {
1049 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 const GLint sy = height - (r.top + r.height());
1051 glScissor(r.left, sy, r.width(), r.height());
1052 glClear(GL_COLOR_BUFFER_BIT);
1053 }
1054 } else {
1055 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1056 { width, height }, { 0, height } };
1057 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 glVertexPointer(2, GL_SHORT, 0, vertices);
1060 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1061 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001062
1063 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 glEnable(GL_TEXTURE_2D);
1065 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1066 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1067 glMatrixMode(GL_TEXTURE);
1068 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001069
1070 glDisable(GL_BLEND);
1071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001073 Region::const_iterator it = region.begin();
1074 Region::const_iterator const end = region.end();
1075 while (it != end) {
1076 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 const GLint sy = height - (r.top + r.height());
1078 glScissor(r.left, sy, r.width(), r.height());
1079 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1080 }
1081 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001082 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001083 glLoadIdentity();
1084 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 }
1086}
1087
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001088status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089{
1090 Mutex::Autolock _l(mStateLock);
1091 addLayer_l(layer);
1092 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1093 return NO_ERROR;
1094}
1095
Mathias Agopian96f08192010-06-02 23:28:45 -07001096status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1097{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001098 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001099 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1100}
1101
1102ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1103 const sp<LayerBaseClient>& lbc)
1104{
Mathias Agopian96f08192010-06-02 23:28:45 -07001105 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001106 size_t name = client->attachLayer(lbc);
1107
1108 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001109
1110 // add this layer to the current state list
1111 addLayer_l(lbc);
1112
Mathias Agopian4f113742011-05-03 16:21:41 -07001113 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001114}
1115
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001116status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117{
1118 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001119 status_t err = purgatorizeLayer_l(layer);
1120 if (err == NO_ERROR)
1121 setTransactionFlags(eTransactionNeeded);
1122 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123}
1124
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001125status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001127 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1128 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001129 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001130 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1132 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001133 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 return NO_ERROR;
1135 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001136 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137}
1138
Mathias Agopian9a112062009-04-17 19:36:26 -07001139status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1140{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001141 // First add the layer to the purgatory list, which makes sure it won't
1142 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001143 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001144 if (err >= 0) {
1145 mLayerPurgatory.add(layerBase);
1146 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001147
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001148 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001149
Mathias Agopian3d579642009-06-04 18:46:21 -07001150 // it's possible that we don't find a layer, because it might
1151 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001152 // from the user because there is a race between Client::destroySurface(),
1153 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001154 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1155}
1156
Mathias Agopian96f08192010-06-02 23:28:45 -07001157status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158{
Mathias Agopian96f08192010-06-02 23:28:45 -07001159 layer->forceVisibilityTransaction();
1160 setTransactionFlags(eTraversalNeeded);
1161 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162}
1163
Mathias Agopiandea20b12011-05-03 17:04:02 -07001164uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1165{
1166 return android_atomic_release_load(&mTransactionFlags);
1167}
1168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1170{
1171 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1172}
1173
Mathias Agopianbb641242010-05-18 17:06:55 -07001174uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175{
1176 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1177 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001178 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 }
1180 return old;
1181}
1182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001184void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001185 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001186 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001187
Jamie Gennis28378392011-10-12 17:39:00 -07001188 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001189 if (mCurrentState.orientation != orientation) {
1190 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1191 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001192 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001193 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001194 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001195 orientation);
1196 }
1197 }
1198
Mathias Agopian698c0872011-06-28 19:09:31 -07001199 const size_t count = state.size();
1200 for (size_t i=0 ; i<count ; i++) {
1201 const ComposerState& s(state[i]);
1202 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001203 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001204 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001205
Mathias Agopian386aa982011-11-07 21:58:03 -08001206 if (transactionFlags) {
1207 // this triggers the transaction
1208 setTransactionFlags(transactionFlags);
1209
1210 // if this is a synchronous transaction, wait for it to take effect
1211 // before returning.
1212 if (flags & eSynchronous) {
1213 mTransationPending = true;
1214 }
1215 while (mTransationPending) {
1216 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1217 if (CC_UNLIKELY(err != NO_ERROR)) {
1218 // just in case something goes wrong in SF, return to the
1219 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001220 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001221 mTransationPending = false;
1222 break;
1223 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001224 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 }
1226}
1227
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001228int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001229 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230{
Glenn Kasten99ed2242011-12-15 09:51:17 -08001231 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 return BAD_VALUE;
1233
1234 Mutex::Autolock _l(mStateLock);
1235 if (mCurrentState.orientation != orientation) {
1236 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001237 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 mCurrentState.orientation = orientation;
1239 setTransactionFlags(eTransactionNeeded);
1240 mTransactionCV.wait(mStateLock);
1241 } else {
1242 orientation = BAD_VALUE;
1243 }
1244 }
1245 return orientation;
1246}
1247
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001248sp<ISurface> SurfaceFlinger::createSurface(
1249 ISurfaceComposerClient::surface_data_t* params,
1250 const String8& name,
1251 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1253 uint32_t flags)
1254{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001255 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001256 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001257
1258 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001259 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001260 int(w), int(h));
1261 return surfaceHandle;
1262 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001263
Steve Block9d453682011-12-20 16:23:08 +00001264 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001265 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 switch (flags & eFXSurfaceMask) {
1267 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001268 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1269 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 break;
1271 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001272 // for now we treat Blur as Dim, until we can implement it
1273 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001275 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001277 case eFXSurfaceScreenshot:
1278 layer = createScreenshotSurface(client, d, w, h, flags);
1279 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 }
1281
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001282 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001283 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001284 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001285 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001288 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001290 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001291 if (normalLayer != 0) {
1292 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001293 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001294 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001295 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001296
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 }
1299
1300 return surfaceHandle;
1301}
1302
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001303sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001304 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001305 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001306 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307{
1308 // initialize the surfaces
1309 switch (format) { // TODO: take h/w into account
1310 case PIXEL_FORMAT_TRANSPARENT:
1311 case PIXEL_FORMAT_TRANSLUCENT:
1312 format = PIXEL_FORMAT_RGBA_8888;
1313 break;
1314 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001315#ifdef NO_RGBX_8888
1316 format = PIXEL_FORMAT_RGB_565;
1317#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001318 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001319#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 break;
1321 }
1322
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001323#ifdef NO_RGBX_8888
1324 if (format == PIXEL_FORMAT_RGBX_8888)
1325 format = PIXEL_FORMAT_RGBA_8888;
1326#endif
1327
Mathias Agopian96f08192010-06-02 23:28:45 -07001328 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001329 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001330 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001331 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001332 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 }
1334 return layer;
1335}
1336
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001337sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001338 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001339 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340{
Mathias Agopian96f08192010-06-02 23:28:45 -07001341 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001342 return layer;
1343}
1344
1345sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1346 const sp<Client>& client, DisplayID display,
1347 uint32_t w, uint32_t h, uint32_t flags)
1348{
1349 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 return layer;
1351}
1352
Mathias Agopian96f08192010-06-02 23:28:45 -07001353status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354{
Mathias Agopian9a112062009-04-17 19:36:26 -07001355 /*
1356 * called by the window manager, when a surface should be marked for
1357 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001358 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001359 * The surface is removed from the current and drawing lists, but placed
1360 * in the purgatory queue, so it's not destroyed right-away (we need
1361 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001362 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001363
Mathias Agopian48d819a2009-09-10 19:41:18 -07001364 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001365 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001366 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001367 if (layer != 0) {
1368 err = purgatorizeLayer_l(layer);
1369 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001370 setTransactionFlags(eTransactionNeeded);
1371 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001372 }
1373 return err;
1374}
1375
Mathias Agopianca4d3602011-05-19 15:38:14 -07001376status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001377{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001378 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001379 status_t err = NO_ERROR;
1380 sp<LayerBaseClient> l(layer.promote());
1381 if (l != NULL) {
1382 Mutex::Autolock _l(mStateLock);
1383 err = removeLayer_l(l);
1384 if (err == NAME_NOT_FOUND) {
1385 // The surface wasn't in the current list, which means it was
1386 // removed already, which means it is in the purgatory,
1387 // and need to be removed from there.
1388 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001389 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001390 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001391 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001392 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001393 "error removing layer=%p (%s)", l.get(), strerror(-err));
1394 }
1395 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396}
1397
Mathias Agopian698c0872011-06-28 19:09:31 -07001398uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001399 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001400 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001403 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1404 if (layer != 0) {
1405 const uint32_t what = s.what;
1406 if (what & ePositionChanged) {
1407 if (layer->setPosition(s.x, s.y))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eLayerChanged) {
1411 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1412 if (layer->setLayer(s.z)) {
1413 mCurrentState.layersSortedByZ.removeAt(idx);
1414 mCurrentState.layersSortedByZ.add(layer);
1415 // we need traversal (state changed)
1416 // AND transaction (list changed)
1417 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 }
1419 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001420 if (what & eSizeChanged) {
1421 if (layer->setSize(s.w, s.h)) {
1422 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001423 }
1424 }
1425 if (what & eAlphaChanged) {
1426 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1427 flags |= eTraversalNeeded;
1428 }
1429 if (what & eMatrixChanged) {
1430 if (layer->setMatrix(s.matrix))
1431 flags |= eTraversalNeeded;
1432 }
1433 if (what & eTransparentRegionChanged) {
1434 if (layer->setTransparentRegionHint(s.transparentRegion))
1435 flags |= eTraversalNeeded;
1436 }
1437 if (what & eVisibilityChanged) {
1438 if (layer->setFlags(s.flags, s.mask))
1439 flags |= eTraversalNeeded;
1440 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001442 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443}
1444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445void SurfaceFlinger::screenReleased(int dpy)
1446{
1447 // this may be called by a signal handler, we can't do too much in here
1448 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1449 signalEvent();
1450}
1451
1452void SurfaceFlinger::screenAcquired(int dpy)
1453{
1454 // this may be called by a signal handler, we can't do too much in here
1455 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1456 signalEvent();
1457}
1458
1459status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1460{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001461 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 char buffer[SIZE];
1463 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001464
1465 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 snprintf(buffer, SIZE, "Permission Denial: "
1467 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1468 IPCThreadState::self()->getCallingPid(),
1469 IPCThreadState::self()->getCallingUid());
1470 result.append(buffer);
1471 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001472 // Try to get the main lock, but don't insist if we can't
1473 // (this would indicate SF is stuck, but we want to be able to
1474 // print something in dumpsys).
1475 int retry = 3;
1476 while (mStateLock.tryLock()<0 && --retry>=0) {
1477 usleep(1000000);
1478 }
1479 const bool locked(retry >= 0);
1480 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001481 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001482 "SurfaceFlinger appears to be unresponsive, "
1483 "dumping anyways (no locks held)\n");
1484 result.append(buffer);
1485 }
1486
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001487 bool dumpAll = true;
1488 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001489 size_t numArgs = args.size();
1490 if (numArgs) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001491 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001492
1493 if ((index < numArgs) &&
1494 (args[index] == String16("--list"))) {
1495 index++;
1496 listLayersLocked(args, index, result, buffer, SIZE);
1497 }
1498
1499 if ((index < numArgs) &&
1500 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001501 index++;
1502 dumpStatsLocked(args, index, result, buffer, SIZE);
1503 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001504
1505 if ((index < numArgs) &&
1506 (args[index] == String16("--latency-clear"))) {
1507 index++;
1508 clearStatsLocked(args, index, result, buffer, SIZE);
1509 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001511
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001512 if (dumpAll) {
1513 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001514 }
1515
Mathias Agopian9795c422009-08-26 16:36:26 -07001516 if (locked) {
1517 mStateLock.unlock();
1518 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 }
1520 write(fd, result.string(), result.size());
1521 return NO_ERROR;
1522}
1523
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001524void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1525 String8& result, char* buffer, size_t SIZE) const
1526{
1527 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1528 const size_t count = currentLayers.size();
1529 for (size_t i=0 ; i<count ; i++) {
1530 const sp<LayerBase>& layer(currentLayers[i]);
1531 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1532 result.append(buffer);
1533 }
1534}
1535
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001536void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1537 String8& result, char* buffer, size_t SIZE) const
1538{
1539 String8 name;
1540 if (index < args.size()) {
1541 name = String8(args[index]);
1542 index++;
1543 }
1544
1545 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1546 const size_t count = currentLayers.size();
1547 for (size_t i=0 ; i<count ; i++) {
1548 const sp<LayerBase>& layer(currentLayers[i]);
1549 if (name.isEmpty()) {
1550 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1551 result.append(buffer);
1552 }
1553 if (name.isEmpty() || (name == layer->getName())) {
1554 layer->dumpStats(result, buffer, SIZE);
1555 }
1556 }
1557}
1558
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001559void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1560 String8& result, char* buffer, size_t SIZE) const
1561{
1562 String8 name;
1563 if (index < args.size()) {
1564 name = String8(args[index]);
1565 index++;
1566 }
1567
1568 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1569 const size_t count = currentLayers.size();
1570 for (size_t i=0 ; i<count ; i++) {
1571 const sp<LayerBase>& layer(currentLayers[i]);
1572 if (name.isEmpty() || (name == layer->getName())) {
1573 layer->clearStats();
1574 }
1575 }
1576}
1577
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001578void SurfaceFlinger::dumpAllLocked(
1579 String8& result, char* buffer, size_t SIZE) const
1580{
1581 // figure out if we're stuck somewhere
1582 const nsecs_t now = systemTime();
1583 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1584 const nsecs_t inTransaction(mDebugInTransaction);
1585 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1586 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1587
1588 /*
1589 * Dump the visible layer list
1590 */
1591 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1592 const size_t count = currentLayers.size();
1593 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1594 result.append(buffer);
1595 for (size_t i=0 ; i<count ; i++) {
1596 const sp<LayerBase>& layer(currentLayers[i]);
1597 layer->dump(result, buffer, SIZE);
1598 }
1599
1600 /*
1601 * Dump the layers in the purgatory
1602 */
1603
1604 const size_t purgatorySize = mLayerPurgatory.size();
1605 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1606 result.append(buffer);
1607 for (size_t i=0 ; i<purgatorySize ; i++) {
1608 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1609 layer->shortDump(result, buffer, SIZE);
1610 }
1611
1612 /*
1613 * Dump SurfaceFlinger global state
1614 */
1615
1616 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1617 result.append(buffer);
1618
1619 const GLExtensions& extensions(GLExtensions::getInstance());
1620 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1621 extensions.getVendor(),
1622 extensions.getRenderer(),
1623 extensions.getVersion());
1624 result.append(buffer);
1625
1626 snprintf(buffer, SIZE, "EGL : %s\n",
1627 eglQueryString(graphicPlane(0).getEGLDisplay(),
1628 EGL_VERSION_HW_ANDROID));
1629 result.append(buffer);
1630
1631 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1632 result.append(buffer);
1633
1634 mWormholeRegion.dump(result, "WormholeRegion");
1635 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1636 snprintf(buffer, SIZE,
1637 " orientation=%d, canDraw=%d\n",
1638 mCurrentState.orientation, hw.canDraw());
1639 result.append(buffer);
1640 snprintf(buffer, SIZE,
1641 " last eglSwapBuffers() time: %f us\n"
1642 " last transaction time : %f us\n"
1643 " refresh-rate : %f fps\n"
1644 " x-dpi : %f\n"
1645 " y-dpi : %f\n",
1646 mLastSwapBufferTime/1000.0,
1647 mLastTransactionTime/1000.0,
1648 hw.getRefreshRate(),
1649 hw.getDpiX(),
1650 hw.getDpiY());
1651 result.append(buffer);
1652
1653 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1654 inSwapBuffersDuration/1000.0);
1655 result.append(buffer);
1656
1657 snprintf(buffer, SIZE, " transaction time: %f us\n",
1658 inTransactionDuration/1000.0);
1659 result.append(buffer);
1660
1661 /*
1662 * VSYNC state
1663 */
1664 mEventThread->dump(result, buffer, SIZE);
1665
1666 /*
1667 * Dump HWComposer state
1668 */
1669 HWComposer& hwc(hw.getHwComposer());
1670 snprintf(buffer, SIZE, "h/w composer state:\n");
1671 result.append(buffer);
1672 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1673 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1674 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1675 result.append(buffer);
1676 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1677
1678 /*
1679 * Dump gralloc state
1680 */
1681 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1682 alloc.dump(result);
1683 hw.dump(result);
1684}
1685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686status_t SurfaceFlinger::onTransact(
1687 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1688{
1689 switch (code) {
1690 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001691 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001694 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001695 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696 {
1697 // codes that require permission check
1698 IPCThreadState* ipc = IPCThreadState::self();
1699 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001700 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001701 if ((uid != AID_GRAPHICS) &&
1702 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001703 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001704 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1705 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001707 break;
1708 }
1709 case CAPTURE_SCREEN:
1710 {
1711 // codes that require permission check
1712 IPCThreadState* ipc = IPCThreadState::self();
1713 const int pid = ipc->getCallingPid();
1714 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001715 if ((uid != AID_GRAPHICS) &&
1716 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001717 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001718 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1719 return PERMISSION_DENIED;
1720 }
1721 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722 }
1723 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1726 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001727 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001728 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001729 IPCThreadState* ipc = IPCThreadState::self();
1730 const int pid = ipc->getCallingPid();
1731 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001732 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001733 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 return PERMISSION_DENIED;
1735 }
1736 int n;
1737 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001738 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001739 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 return NO_ERROR;
1741 case 1002: // SHOW_UPDATES
1742 n = data.readInt32();
1743 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001744 invalidateHwcGeometry();
1745 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 return NO_ERROR;
1747 case 1003: // SHOW_BACKGROUND
1748 n = data.readInt32();
1749 mDebugBackground = n ? 1 : 0;
1750 return NO_ERROR;
1751 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001752 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001753 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001755 case 1005:{ // force transaction
1756 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1757 return NO_ERROR;
1758 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001759 case 1008: // toggle use of hw composer
1760 n = data.readInt32();
1761 mDebugDisableHWC = n ? 1 : 0;
1762 invalidateHwcGeometry();
1763 repaintEverything();
1764 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001765 case 1009: // toggle use of transform hint
1766 n = data.readInt32();
1767 mDebugDisableTransformHint = n ? 1 : 0;
1768 invalidateHwcGeometry();
1769 repaintEverything();
1770 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001772 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773 reply->writeInt32(0);
1774 reply->writeInt32(mDebugRegion);
1775 reply->writeInt32(mDebugBackground);
1776 return NO_ERROR;
1777 case 1013: {
1778 Mutex::Autolock _l(mStateLock);
1779 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1780 reply->writeInt32(hw.getPageFlipCount());
1781 }
1782 return NO_ERROR;
1783 }
1784 }
1785 return err;
1786}
1787
Mathias Agopian53331da2011-08-22 21:44:41 -07001788void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001789 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001790 const Rect bounds(hw.getBounds());
1791 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001792 signalEvent();
1793}
1794
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001795void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1796 Mutex::Autolock _l(mInvalidateLock);
1797 mInvalidateRegion = reg;
1798}
1799
1800Region SurfaceFlinger::getAndClearInvalidateRegion() {
1801 Mutex::Autolock _l(mInvalidateLock);
1802 Region reg(mInvalidateRegion);
1803 mInvalidateRegion.clear();
1804 return reg;
1805}
1806
Mathias Agopian59119e62010-10-11 12:37:43 -07001807// ---------------------------------------------------------------------------
1808
Mathias Agopian118d0242011-10-13 16:02:48 -07001809status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1810 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1811{
1812 Mutex::Autolock _l(mStateLock);
1813 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1814}
1815
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001816status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1817 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001818{
Mathias Agopian59119e62010-10-11 12:37:43 -07001819 if (!GLExtensions::getInstance().haveFramebufferObject())
1820 return INVALID_OPERATION;
1821
1822 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001823 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001824 const uint32_t hw_w = hw.getWidth();
1825 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001826 GLfloat u = 1;
1827 GLfloat v = 1;
1828
1829 // make sure to clear all GL error flags
1830 while ( glGetError() != GL_NO_ERROR ) ;
1831
1832 // create a FBO
1833 GLuint name, tname;
1834 glGenTextures(1, &tname);
1835 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001836 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1837 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001838 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001839 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001840 GLint tw = (2 << (31 - clz(hw_w)));
1841 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1843 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001844 u = GLfloat(hw_w) / tw;
1845 v = GLfloat(hw_h) / th;
1846 }
1847 glGenFramebuffersOES(1, &name);
1848 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001849 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1850 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001851
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001852 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001853 glDisable(GL_TEXTURE_EXTERNAL_OES);
1854 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001855 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001856 glClearColor(0,0,0,1);
1857 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001858 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001859 glMatrixMode(GL_MODELVIEW);
1860 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1862 const size_t count = layers.size();
1863 for (size_t i=0 ; i<count ; ++i) {
1864 const sp<LayerBase>& layer(layers[i]);
1865 layer->drawForSreenShot();
1866 }
1867
Mathias Agopian118d0242011-10-13 16:02:48 -07001868 hw.compositionComplete();
1869
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001870 // back to main framebuffer
1871 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1872 glDisable(GL_SCISSOR_TEST);
1873 glDeleteFramebuffersOES(1, &name);
1874
1875 *textureName = tname;
1876 *uOut = u;
1877 *vOut = v;
1878 return NO_ERROR;
1879}
1880
1881// ---------------------------------------------------------------------------
1882
1883status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1884{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001885 // get screen geometry
1886 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1887 const uint32_t hw_w = hw.getWidth();
1888 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001889 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001890
1891 GLfloat u, v;
1892 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001893 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001894 if (result != NO_ERROR) {
1895 return result;
1896 }
1897
1898 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001899 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001900 glBindTexture(GL_TEXTURE_2D, tname);
1901 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1902 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1903 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1904 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1905 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1906 glVertexPointer(2, GL_FLOAT, 0, vtx);
1907
Mathias Agopianffcf4652011-07-07 17:30:31 -07001908 /*
1909 * Texture coordinate mapping
1910 *
1911 * u
1912 * 1 +----------+---+
1913 * | | | | image is inverted
1914 * | V | | w.r.t. the texture
1915 * 1-v +----------+ | coordinates
1916 * | |
1917 * | |
1918 * | |
1919 * 0 +--------------+
1920 * 0 1
1921 *
1922 */
1923
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001924 class s_curve_interpolator {
1925 const float nbFrames, s, v;
1926 public:
1927 s_curve_interpolator(int nbFrames, float s)
1928 : nbFrames(1.0f / (nbFrames-1)), s(s),
1929 v(1.0f + expf(-s + 0.5f*s)) {
1930 }
1931 float operator()(int f) {
1932 const float x = f * nbFrames;
1933 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1934 }
1935 };
1936
1937 class v_stretch {
1938 const GLfloat hw_w, hw_h;
1939 public:
1940 v_stretch(uint32_t hw_w, uint32_t hw_h)
1941 : hw_w(hw_w), hw_h(hw_h) {
1942 }
1943 void operator()(GLfloat* vtx, float v) {
1944 const GLfloat w = hw_w + (hw_w * v);
1945 const GLfloat h = hw_h - (hw_h * v);
1946 const GLfloat x = (hw_w - w) * 0.5f;
1947 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001948 vtx[0] = x; vtx[1] = y;
1949 vtx[2] = x; vtx[3] = y + h;
1950 vtx[4] = x + w; vtx[5] = y + h;
1951 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001952 }
1953 };
1954
1955 class h_stretch {
1956 const GLfloat hw_w, hw_h;
1957 public:
1958 h_stretch(uint32_t hw_w, uint32_t hw_h)
1959 : hw_w(hw_w), hw_h(hw_h) {
1960 }
1961 void operator()(GLfloat* vtx, float v) {
1962 const GLfloat w = hw_w - (hw_w * v);
1963 const GLfloat h = 1.0f;
1964 const GLfloat x = (hw_w - w) * 0.5f;
1965 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001966 vtx[0] = x; vtx[1] = y;
1967 vtx[2] = x; vtx[3] = y + h;
1968 vtx[4] = x + w; vtx[5] = y + h;
1969 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001970 }
1971 };
1972
1973 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001974 char value[PROPERTY_VALUE_MAX];
1975 property_get("debug.sf.electron_frames", value, "24");
1976 int nbFrames = (atoi(value) + 1) >> 1;
1977 if (nbFrames <= 0) // just in case
1978 nbFrames = 24;
1979
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001980 s_curve_interpolator itr(nbFrames, 7.5f);
1981 s_curve_interpolator itg(nbFrames, 8.0f);
1982 s_curve_interpolator itb(nbFrames, 8.5f);
1983
1984 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001985
1986 glMatrixMode(GL_TEXTURE);
1987 glLoadIdentity();
1988 glMatrixMode(GL_MODELVIEW);
1989 glLoadIdentity();
1990
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001991 glEnable(GL_BLEND);
1992 glBlendFunc(GL_ONE, GL_ONE);
1993 for (int i=0 ; i<nbFrames ; i++) {
1994 float x, y, w, h;
1995 const float vr = itr(i);
1996 const float vg = itg(i);
1997 const float vb = itb(i);
1998
1999 // clear screen
2000 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002001 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002002 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002003
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 // draw the red plane
2005 vverts(vtx, vr);
2006 glColorMask(1,0,0,1);
2007 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002008
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 // draw the green plane
2010 vverts(vtx, vg);
2011 glColorMask(0,1,0,1);
2012 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002013
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002014 // draw the blue plane
2015 vverts(vtx, vb);
2016 glColorMask(0,0,1,1);
2017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002018
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002019 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002020 glDisable(GL_TEXTURE_2D);
2021 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002022 glColor4f(vg, vg, vg, 1);
2023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2024 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002025 }
2026
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002027 h_stretch hverts(hw_w, hw_h);
2028 glDisable(GL_BLEND);
2029 glDisable(GL_TEXTURE_2D);
2030 glColorMask(1,1,1,1);
2031 for (int i=0 ; i<nbFrames ; i++) {
2032 const float v = itg(i);
2033 hverts(vtx, v);
2034 glClear(GL_COLOR_BUFFER_BIT);
2035 glColor4f(1-v, 1-v, 1-v, 1);
2036 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2037 hw.flip(screenBounds);
2038 }
2039
2040 glColorMask(1,1,1,1);
2041 glEnable(GL_SCISSOR_TEST);
2042 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2043 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002044 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002045 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002046 return NO_ERROR;
2047}
2048
2049status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2050{
2051 status_t result = PERMISSION_DENIED;
2052
2053 if (!GLExtensions::getInstance().haveFramebufferObject())
2054 return INVALID_OPERATION;
2055
2056
2057 // get screen geometry
2058 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2059 const uint32_t hw_w = hw.getWidth();
2060 const uint32_t hw_h = hw.getHeight();
2061 const Region screenBounds(hw.bounds());
2062
2063 GLfloat u, v;
2064 GLuint tname;
2065 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2066 if (result != NO_ERROR) {
2067 return result;
2068 }
2069
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070 GLfloat vtx[8];
2071 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 glBindTexture(GL_TEXTURE_2D, tname);
2073 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2074 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2075 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2076 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2077 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2078 glVertexPointer(2, GL_FLOAT, 0, vtx);
2079
2080 class s_curve_interpolator {
2081 const float nbFrames, s, v;
2082 public:
2083 s_curve_interpolator(int nbFrames, float s)
2084 : nbFrames(1.0f / (nbFrames-1)), s(s),
2085 v(1.0f + expf(-s + 0.5f*s)) {
2086 }
2087 float operator()(int f) {
2088 const float x = f * nbFrames;
2089 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2090 }
2091 };
2092
2093 class v_stretch {
2094 const GLfloat hw_w, hw_h;
2095 public:
2096 v_stretch(uint32_t hw_w, uint32_t hw_h)
2097 : hw_w(hw_w), hw_h(hw_h) {
2098 }
2099 void operator()(GLfloat* vtx, float v) {
2100 const GLfloat w = hw_w + (hw_w * v);
2101 const GLfloat h = hw_h - (hw_h * v);
2102 const GLfloat x = (hw_w - w) * 0.5f;
2103 const GLfloat y = (hw_h - h) * 0.5f;
2104 vtx[0] = x; vtx[1] = y;
2105 vtx[2] = x; vtx[3] = y + h;
2106 vtx[4] = x + w; vtx[5] = y + h;
2107 vtx[6] = x + w; vtx[7] = y;
2108 }
2109 };
2110
2111 class h_stretch {
2112 const GLfloat hw_w, hw_h;
2113 public:
2114 h_stretch(uint32_t hw_w, uint32_t hw_h)
2115 : hw_w(hw_w), hw_h(hw_h) {
2116 }
2117 void operator()(GLfloat* vtx, float v) {
2118 const GLfloat w = hw_w - (hw_w * v);
2119 const GLfloat h = 1.0f;
2120 const GLfloat x = (hw_w - w) * 0.5f;
2121 const GLfloat y = (hw_h - h) * 0.5f;
2122 vtx[0] = x; vtx[1] = y;
2123 vtx[2] = x; vtx[3] = y + h;
2124 vtx[4] = x + w; vtx[5] = y + h;
2125 vtx[6] = x + w; vtx[7] = y;
2126 }
2127 };
2128
Mathias Agopiana6546e52010-10-14 12:33:07 -07002129 // the full animation is 12 frames
2130 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002131 s_curve_interpolator itr(nbFrames, 7.5f);
2132 s_curve_interpolator itg(nbFrames, 8.0f);
2133 s_curve_interpolator itb(nbFrames, 8.5f);
2134
2135 h_stretch hverts(hw_w, hw_h);
2136 glDisable(GL_BLEND);
2137 glDisable(GL_TEXTURE_2D);
2138 glColorMask(1,1,1,1);
2139 for (int i=nbFrames-1 ; i>=0 ; i--) {
2140 const float v = itg(i);
2141 hverts(vtx, v);
2142 glClear(GL_COLOR_BUFFER_BIT);
2143 glColor4f(1-v, 1-v, 1-v, 1);
2144 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2145 hw.flip(screenBounds);
2146 }
2147
Mathias Agopiana6546e52010-10-14 12:33:07 -07002148 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002149 v_stretch vverts(hw_w, hw_h);
2150 glEnable(GL_BLEND);
2151 glBlendFunc(GL_ONE, GL_ONE);
2152 for (int i=nbFrames-1 ; i>=0 ; i--) {
2153 float x, y, w, h;
2154 const float vr = itr(i);
2155 const float vg = itg(i);
2156 const float vb = itb(i);
2157
2158 // clear screen
2159 glColorMask(1,1,1,1);
2160 glClear(GL_COLOR_BUFFER_BIT);
2161 glEnable(GL_TEXTURE_2D);
2162
2163 // draw the red plane
2164 vverts(vtx, vr);
2165 glColorMask(1,0,0,1);
2166 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2167
2168 // draw the green plane
2169 vverts(vtx, vg);
2170 glColorMask(0,1,0,1);
2171 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2172
2173 // draw the blue plane
2174 vverts(vtx, vb);
2175 glColorMask(0,0,1,1);
2176 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2177
2178 hw.flip(screenBounds);
2179 }
2180
2181 glColorMask(1,1,1,1);
2182 glEnable(GL_SCISSOR_TEST);
2183 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002184 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002185 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002186 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002187
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 return NO_ERROR;
2189}
2190
2191// ---------------------------------------------------------------------------
2192
Mathias Agopianabd671a2010-10-14 14:54:06 -07002193status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002194{
2195 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2196 if (!hw.canDraw()) {
2197 // we're already off
2198 return NO_ERROR;
2199 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002200
2201 // turn off hwc while we're doing the animation
2202 hw.getHwComposer().disable();
2203 // and make sure to turn it back on (if needed) next time we compose
2204 invalidateHwcGeometry();
2205
Mathias Agopianabd671a2010-10-14 14:54:06 -07002206 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2207 electronBeamOffAnimationImplLocked();
2208 }
2209
2210 // always clear the whole screen at the end of the animation
2211 glClearColor(0,0,0,1);
2212 glDisable(GL_SCISSOR_TEST);
2213 glClear(GL_COLOR_BUFFER_BIT);
2214 glEnable(GL_SCISSOR_TEST);
2215 hw.flip( Region(hw.bounds()) );
2216
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002217 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002218}
2219
2220status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2221{
Mathias Agopian59119e62010-10-11 12:37:43 -07002222 class MessageTurnElectronBeamOff : public MessageBase {
2223 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002224 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002225 status_t result;
2226 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002227 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2228 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002229 }
2230 status_t getResult() const {
2231 return result;
2232 }
2233 virtual bool handler() {
2234 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002235 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002236 return true;
2237 }
2238 };
2239
Mathias Agopianabd671a2010-10-14 14:54:06 -07002240 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002241 status_t res = postMessageSync(msg);
2242 if (res == NO_ERROR) {
2243 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244
2245 // work-around: when the power-manager calls us we activate the
2246 // animation. eventually, the "on" animation will be called
2247 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002248 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002249 }
2250 return res;
2251}
2252
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002253// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002254
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002256{
2257 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2258 if (hw.canDraw()) {
2259 // we're already on
2260 return NO_ERROR;
2261 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002262 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2263 electronBeamOnAnimationImplLocked();
2264 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002265
2266 // make sure to redraw the whole screen when the animation is done
2267 mDirtyRegion.set(hw.bounds());
2268 signalEvent();
2269
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002270 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002271}
2272
2273status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2274{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002275 class MessageTurnElectronBeamOn : public MessageBase {
2276 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002277 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002278 status_t result;
2279 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002280 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2281 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002282 }
2283 status_t getResult() const {
2284 return result;
2285 }
2286 virtual bool handler() {
2287 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002288 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002289 return true;
2290 }
2291 };
2292
Mathias Agopianabd671a2010-10-14 14:54:06 -07002293 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002294 return NO_ERROR;
2295}
2296
2297// ---------------------------------------------------------------------------
2298
Mathias Agopian74c40c02010-09-29 13:02:36 -07002299status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2300 sp<IMemoryHeap>* heap,
2301 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002302 uint32_t sw, uint32_t sh,
2303 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304{
2305 status_t result = PERMISSION_DENIED;
2306
2307 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002308 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002309 return BAD_VALUE;
2310
2311 if (!GLExtensions::getInstance().haveFramebufferObject())
2312 return INVALID_OPERATION;
2313
2314 // get screen geometry
2315 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2316 const uint32_t hw_w = hw.getWidth();
2317 const uint32_t hw_h = hw.getHeight();
2318
2319 if ((sw > hw_w) || (sh > hw_h))
2320 return BAD_VALUE;
2321
2322 sw = (!sw) ? hw_w : sw;
2323 sh = (!sh) ? hw_h : sh;
2324 const size_t size = sw * sh * 4;
2325
Steve Block9d453682011-12-20 16:23:08 +00002326 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002327 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002328
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 // make sure to clear all GL error flags
2330 while ( glGetError() != GL_NO_ERROR ) ;
2331
2332 // create a FBO
2333 GLuint name, tname;
2334 glGenRenderbuffersOES(1, &tname);
2335 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2336 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2337 glGenFramebuffersOES(1, &name);
2338 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2339 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2340 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2341
2342 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002343
Mathias Agopian74c40c02010-09-29 13:02:36 -07002344 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2345
2346 // invert everything, b/c glReadPixel() below will invert the FB
2347 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002348 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002349 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002350 glMatrixMode(GL_PROJECTION);
2351 glPushMatrix();
2352 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002353 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002354 glMatrixMode(GL_MODELVIEW);
2355
2356 // redraw the screen entirely...
2357 glClearColor(0,0,0,1);
2358 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002359
Jamie Gennis9575f602011-10-07 14:51:16 -07002360 const LayerVector& layers(mDrawingState.layersSortedByZ);
2361 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 for (size_t i=0 ; i<count ; ++i) {
2363 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002364 const uint32_t flags = layer->drawingState().flags;
2365 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2366 const uint32_t z = layer->drawingState().z;
2367 if (z >= minLayerZ && z <= maxLayerZ) {
2368 layer->drawForSreenShot();
2369 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002370 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002371 }
2372
2373 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002374 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 glScissor(0, 0, sw, sh);
2376
2377 // check for errors and return screen capture
2378 if (glGetError() != GL_NO_ERROR) {
2379 // error while rendering
2380 result = INVALID_OPERATION;
2381 } else {
2382 // allocate shared memory large enough to hold the
2383 // screen capture
2384 sp<MemoryHeapBase> base(
2385 new MemoryHeapBase(size, 0, "screen-capture") );
2386 void* const ptr = base->getBase();
2387 if (ptr) {
2388 // capture the screen with glReadPixels()
2389 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2390 if (glGetError() == GL_NO_ERROR) {
2391 *heap = base;
2392 *w = sw;
2393 *h = sh;
2394 *f = PIXEL_FORMAT_RGBA_8888;
2395 result = NO_ERROR;
2396 }
2397 } else {
2398 result = NO_MEMORY;
2399 }
2400 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 glEnable(GL_SCISSOR_TEST);
2402 glViewport(0, 0, hw_w, hw_h);
2403 glMatrixMode(GL_PROJECTION);
2404 glPopMatrix();
2405 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002406 } else {
2407 result = BAD_VALUE;
2408 }
2409
2410 // release FBO resources
2411 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2412 glDeleteRenderbuffersOES(1, &tname);
2413 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002414
2415 hw.compositionComplete();
2416
Steve Block9d453682011-12-20 16:23:08 +00002417 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002418
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 return result;
2420}
2421
2422
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002423status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2424 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002425 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002426 uint32_t sw, uint32_t sh,
2427 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002428{
2429 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002430 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002431 return BAD_VALUE;
2432
2433 if (!GLExtensions::getInstance().haveFramebufferObject())
2434 return INVALID_OPERATION;
2435
2436 class MessageCaptureScreen : public MessageBase {
2437 SurfaceFlinger* flinger;
2438 DisplayID dpy;
2439 sp<IMemoryHeap>* heap;
2440 uint32_t* w;
2441 uint32_t* h;
2442 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002443 uint32_t sw;
2444 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002445 uint32_t minLayerZ;
2446 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447 status_t result;
2448 public:
2449 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002450 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002451 uint32_t sw, uint32_t sh,
2452 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002453 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002454 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2455 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2456 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002457 {
2458 }
2459 status_t getResult() const {
2460 return result;
2461 }
2462 virtual bool handler() {
2463 Mutex::Autolock _l(flinger->mStateLock);
2464
2465 // if we have secure windows, never allow the screen capture
2466 if (flinger->mSecureFrameBuffer)
2467 return true;
2468
Mathias Agopian74c40c02010-09-29 13:02:36 -07002469 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002470 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002471
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002472 return true;
2473 }
2474 };
2475
2476 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002477 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002478 status_t res = postMessageSync(msg);
2479 if (res == NO_ERROR) {
2480 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2481 }
2482 return res;
2483}
2484
2485// ---------------------------------------------------------------------------
2486
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002487sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2488{
2489 sp<Layer> result;
2490 Mutex::Autolock _l(mStateLock);
2491 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2492 return result;
2493}
2494
2495// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496
Mathias Agopian96f08192010-06-02 23:28:45 -07002497Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002498 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002499{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500}
2501
Mathias Agopian96f08192010-06-02 23:28:45 -07002502Client::~Client()
2503{
Mathias Agopian96f08192010-06-02 23:28:45 -07002504 const size_t count = mLayers.size();
2505 for (size_t i=0 ; i<count ; i++) {
2506 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2507 if (layer != 0) {
2508 mFlinger->removeLayer(layer);
2509 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 }
2511}
2512
Mathias Agopian96f08192010-06-02 23:28:45 -07002513status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002514 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002516
Mathias Agopian4f113742011-05-03 16:21:41 -07002517size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002518{
Mathias Agopian4f113742011-05-03 16:21:41 -07002519 Mutex::Autolock _l(mLock);
2520 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002521 mLayers.add(name, layer);
2522 return name;
2523}
2524
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002525void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002526{
Mathias Agopian4f113742011-05-03 16:21:41 -07002527 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002528 // we do a linear search here, because this doesn't happen often
2529 const size_t count = mLayers.size();
2530 for (size_t i=0 ; i<count ; i++) {
2531 if (mLayers.valueAt(i) == layer) {
2532 mLayers.removeItemsAt(i, 1);
2533 break;
2534 }
2535 }
2536}
Mathias Agopian4f113742011-05-03 16:21:41 -07002537sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2538{
2539 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002540 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002541 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002542 if (layer != 0) {
2543 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002544 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002545 }
2546 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002547}
2548
Mathias Agopiana67932f2011-04-20 14:20:59 -07002549
2550status_t Client::onTransact(
2551 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2552{
2553 // these must be checked
2554 IPCThreadState* ipc = IPCThreadState::self();
2555 const int pid = ipc->getCallingPid();
2556 const int uid = ipc->getCallingUid();
2557 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002558 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002559 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002560 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002561 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002562 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002563 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2564 return PERMISSION_DENIED;
2565 }
2566 }
2567 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002568}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002569
2570
Mathias Agopian96f08192010-06-02 23:28:45 -07002571sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002572 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002573 const String8& name,
2574 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002575 uint32_t flags)
2576{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002577 /*
2578 * createSurface must be called from the GL thread so that it can
2579 * have access to the GL context.
2580 */
2581
2582 class MessageCreateSurface : public MessageBase {
2583 sp<ISurface> result;
2584 SurfaceFlinger* flinger;
2585 ISurfaceComposerClient::surface_data_t* params;
2586 Client* client;
2587 const String8& name;
2588 DisplayID display;
2589 uint32_t w, h;
2590 PixelFormat format;
2591 uint32_t flags;
2592 public:
2593 MessageCreateSurface(SurfaceFlinger* flinger,
2594 ISurfaceComposerClient::surface_data_t* params,
2595 const String8& name, Client* client,
2596 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2597 uint32_t flags)
2598 : flinger(flinger), params(params), client(client), name(name),
2599 display(display), w(w), h(h), format(format), flags(flags)
2600 {
2601 }
2602 sp<ISurface> getResult() const { return result; }
2603 virtual bool handler() {
2604 result = flinger->createSurface(params, name, client,
2605 display, w, h, format, flags);
2606 return true;
2607 }
2608 };
2609
2610 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2611 params, name, this, display, w, h, format, flags);
2612 mFlinger->postMessageSync(msg);
2613 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002614}
Mathias Agopian96f08192010-06-02 23:28:45 -07002615status_t Client::destroySurface(SurfaceID sid) {
2616 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002617}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618
2619// ---------------------------------------------------------------------------
2620
Jamie Gennis9a78c902011-01-12 18:30:40 -08002621GraphicBufferAlloc::GraphicBufferAlloc() {}
2622
2623GraphicBufferAlloc::~GraphicBufferAlloc() {}
2624
2625sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002626 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002627 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2628 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002629 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002630 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002631 if (err == NO_MEMORY) {
2632 GraphicBuffer::dumpAllocationsToSystemLog();
2633 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002634 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002635 "failed (%s), handle=%p",
2636 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002637 return 0;
2638 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002639 return graphicBuffer;
2640}
2641
Jamie Gennis9a78c902011-01-12 18:30:40 -08002642// ---------------------------------------------------------------------------
2643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644GraphicPlane::GraphicPlane()
2645 : mHw(0)
2646{
2647}
2648
2649GraphicPlane::~GraphicPlane() {
2650 delete mHw;
2651}
2652
2653bool GraphicPlane::initialized() const {
2654 return mHw ? true : false;
2655}
2656
Mathias Agopian2b92d892010-02-08 15:49:35 -08002657int GraphicPlane::getWidth() const {
2658 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002659}
2660
Mathias Agopian2b92d892010-02-08 15:49:35 -08002661int GraphicPlane::getHeight() const {
2662 return mHeight;
2663}
2664
2665void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2666{
2667 mHw = hw;
2668
2669 // initialize the display orientation transform.
2670 // it's a constant that should come from the display driver.
2671 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2672 char property[PROPERTY_VALUE_MAX];
2673 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2674 //displayOrientation
2675 switch (atoi(property)) {
2676 case 90:
2677 displayOrientation = ISurfaceComposer::eOrientation90;
2678 break;
2679 case 270:
2680 displayOrientation = ISurfaceComposer::eOrientation270;
2681 break;
2682 }
2683 }
2684
2685 const float w = hw->getWidth();
2686 const float h = hw->getHeight();
2687 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2688 &mDisplayTransform);
2689 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2690 mDisplayWidth = h;
2691 mDisplayHeight = w;
2692 } else {
2693 mDisplayWidth = w;
2694 mDisplayHeight = h;
2695 }
2696
2697 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002698}
2699
2700status_t GraphicPlane::orientationToTransfrom(
2701 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002702{
2703 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002704 switch (orientation) {
2705 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002706 flags = Transform::ROT_0;
2707 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002708 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002709 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710 break;
2711 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002712 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002713 break;
2714 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002715 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002716 break;
2717 default:
2718 return BAD_VALUE;
2719 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002720 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002721 return NO_ERROR;
2722}
2723
2724status_t GraphicPlane::setOrientation(int orientation)
2725{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726 // If the rotation can be handled in hardware, this is where
2727 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002728
2729 const DisplayHardware& hw(displayHardware());
2730 const float w = mDisplayWidth;
2731 const float h = mDisplayHeight;
2732 mWidth = int(w);
2733 mHeight = int(h);
2734
2735 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002736 GraphicPlane::orientationToTransfrom(orientation, w, h,
2737 &orientationTransform);
2738 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2739 mWidth = int(h);
2740 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002741 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002742
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002743 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002744 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002745 return NO_ERROR;
2746}
2747
2748const DisplayHardware& GraphicPlane::displayHardware() const {
2749 return *mHw;
2750}
2751
Mathias Agopian59119e62010-10-11 12:37:43 -07002752DisplayHardware& GraphicPlane::editDisplayHardware() {
2753 return *mHw;
2754}
2755
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002756const Transform& GraphicPlane::transform() const {
2757 return mGlobalTransform;
2758}
2759
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002760EGLDisplay GraphicPlane::getEGLDisplay() const {
2761 return mHw->getEGLDisplay();
2762}
2763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002764// ---------------------------------------------------------------------------
2765
2766}; // namespace android