blob: af474022dddffc3dceccc4010b875b622e70ef23 [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 Agopiand0566bc2011-11-17 17:49:17 -0800434 hw.waitForVSync();
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);
448 mLastSwapBufferTime = systemTime() - now;
449 mDebugInSwapBuffers = 0;
450 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451}
452
453void SurfaceFlinger::handleConsoleEvents()
454{
455 // something to do with the console
456 const DisplayHardware& hw = graphicPlane(0).displayHardware();
457
458 int what = android_atomic_and(0, &mConsoleSignals);
459 if (what & eConsoleAcquired) {
460 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700461 // this is a temporary work-around, eventually this should be called
462 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700463 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 }
465
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700467 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 }
470 }
471
472 mDirtyRegion.set(hw.bounds());
473}
474
475void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
476{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700477 Mutex::Autolock _l(mStateLock);
478 const nsecs_t now = systemTime();
479 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480
Mathias Agopianca4d3602011-05-19 15:38:14 -0700481 // Here we're guaranteed that some transaction flags are set
482 // so we can call handleTransactionLocked() unconditionally.
483 // We call getTransactionFlags(), which will also clear the flags,
484 // with mStateLock held to guarantee that mCurrentState won't change
485 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700486
Mathias Agopianca4d3602011-05-19 15:38:14 -0700487 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
488 transactionFlags = getTransactionFlags(mask);
489 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700490
Mathias Agopianca4d3602011-05-19 15:38:14 -0700491 mLastTransactionTime = systemTime() - now;
492 mDebugInTransaction = 0;
493 invalidateHwcGeometry();
494 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700495}
496
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700498{
499 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 const size_t count = currentLayers.size();
501
502 /*
503 * Traversal of the children
504 * (perform the transaction for each of them if needed)
505 */
506
507 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
508 if (layersNeedTransaction) {
509 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
512 if (!trFlags) continue;
513
514 const uint32_t flags = layer->doTransaction(0);
515 if (flags & Layer::eVisibleRegion)
516 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
518 }
519
520 /*
521 * Perform our own transaction if needed
522 */
523
524 if (transactionFlags & eTransactionNeeded) {
525 if (mCurrentState.orientation != mDrawingState.orientation) {
526 // the orientation has changed, recompute all visible regions
527 // and invalidate everything.
528
529 const int dpy = 0;
530 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700531 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 GraphicPlane& plane(graphicPlane(dpy));
533 plane.setOrientation(orientation);
534
535 // update the shared control block
536 const DisplayHardware& hw(plane.displayHardware());
537 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
538 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800539 dcblk->w = plane.getWidth();
540 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541
542 mVisibleRegionsDirty = true;
543 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 }
545
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700546 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
547 // layers have been added
548 mVisibleRegionsDirty = true;
549 }
550
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 // some layers might have been removed, so
552 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700554 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700556 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700557 const size_t count = previousLayers.size();
558 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700559 const sp<LayerBase>& layer(previousLayers[i]);
560 if (currentLayers.indexOf( layer ) < 0) {
561 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700562 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700563 }
564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
567
568 commitTransaction();
569}
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800572 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573{
574 const GraphicPlane& plane(graphicPlane(0));
575 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700576 const DisplayHardware& hw(plane.displayHardware());
577 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578
579 Region aboveOpaqueLayers;
580 Region aboveCoveredLayers;
581 Region dirty;
582
583 bool secureFrameBuffer = false;
584
585 size_t i = currentLayers.size();
586 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700587 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 layer->validateVisibility(planeTransform);
589
590 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700591 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592
Mathias Agopianab028732010-03-16 16:41:46 -0700593 /*
594 * opaqueRegion: area of a surface that is fully opaque.
595 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700597
598 /*
599 * visibleRegion: area of a surface that is visible on screen
600 * and not fully transparent. This is essentially the layer's
601 * footprint minus the opaque regions above it.
602 * Areas covered by a translucent surface are considered visible.
603 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700605
606 /*
607 * coveredRegion: area of a surface that is covered by all
608 * visible regions above it (which includes the translucent areas).
609 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700611
612
613 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800614 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700615 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700616 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700618 visibleRegion.andSelf(screenRegion);
619 if (!visibleRegion.isEmpty()) {
620 // Remove the transparent area from the visible region
621 if (translucent) {
622 visibleRegion.subtractSelf(layer->transparentRegionScreen);
623 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624
Mathias Agopianab028732010-03-16 16:41:46 -0700625 // compute the opaque region
626 const int32_t layerOrientation = layer->getOrientation();
627 if (s.alpha==255 && !translucent &&
628 ((layerOrientation & Transform::ROT_INVALID) == false)) {
629 // the opaque region is the layer's footprint
630 opaqueRegion = visibleRegion;
631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 }
633 }
634
Mathias Agopianab028732010-03-16 16:41:46 -0700635 // Clip the covered region to the visible region
636 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
637
638 // Update aboveCoveredLayers for next (lower) layer
639 aboveCoveredLayers.orSelf(visibleRegion);
640
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 // subtract the opaque region covered by the layers above us
642 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
644 // compute this layer's dirty region
645 if (layer->contentDirty) {
646 // we need to invalidate the whole region
647 dirty = visibleRegion;
648 // as well, as the old visible region
649 dirty.orSelf(layer->visibleRegionScreen);
650 layer->contentDirty = false;
651 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700652 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700653 * the exposed region consists of two components:
654 * 1) what's VISIBLE now and was COVERED before
655 * 2) what's EXPOSED now less what was EXPOSED before
656 *
657 * note that (1) is conservative, we start with the whole
658 * visible region but only keep what used to be covered by
659 * something -- which mean it may have been exposed.
660 *
661 * (2) handles areas that were not covered by anything but got
662 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700663 */
Mathias Agopianab028732010-03-16 16:41:46 -0700664 const Region newExposed = visibleRegion - coveredRegion;
665 const Region oldVisibleRegion = layer->visibleRegionScreen;
666 const Region oldCoveredRegion = layer->coveredRegionScreen;
667 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
668 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 }
670 dirty.subtractSelf(aboveOpaqueLayers);
671
672 // accumulate to the screen dirty region
673 dirtyRegion.orSelf(dirty);
674
Mathias Agopianab028732010-03-16 16:41:46 -0700675 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700677
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 // Store the visible region is screen space
679 layer->setVisibleRegion(visibleRegion);
680 layer->setCoveredRegion(coveredRegion);
681
Mathias Agopian97011222009-07-28 10:57:27 -0700682 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 if (layer->isSecure() && !visibleRegion.isEmpty()) {
684 secureFrameBuffer = true;
685 }
686 }
687
Mathias Agopian97011222009-07-28 10:57:27 -0700688 // invalidate the areas where a layer was removed
689 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
690 mDirtyRegionRemovedLayer.clear();
691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 mSecureFrameBuffer = secureFrameBuffer;
693 opaqueRegion = aboveOpaqueLayers;
694}
695
696
697void SurfaceFlinger::commitTransaction()
698{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800699 if (!mLayersPendingRemoval.isEmpty()) {
700 // Notify removed layers now that they can't be drawn from
701 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
702 mLayersPendingRemoval[i]->onRemoved();
703 }
704 mLayersPendingRemoval.clear();
705 }
706
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700708 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700709 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710}
711
712void SurfaceFlinger::handlePageFlip()
713{
714 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800715 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716 visibleRegions |= lockPageFlip(currentLayers);
717
718 const DisplayHardware& hw = graphicPlane(0).displayHardware();
719 const Region screenRegion(hw.bounds());
720 if (visibleRegions) {
721 Region opaqueRegion;
722 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700723
724 /*
725 * rebuild the visible layer list
726 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800727 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700728 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700729 mVisibleLayersSortedByZ.setCapacity(count);
730 for (size_t i=0 ; i<count ; i++) {
731 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
732 mVisibleLayersSortedByZ.add(currentLayers[i]);
733 }
734
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 mWormholeRegion = screenRegion.subtract(opaqueRegion);
736 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800737 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 }
739
740 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700741
742 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 mDirtyRegion.andSelf(screenRegion);
744}
745
Mathias Agopianad456f92011-01-13 17:53:01 -0800746void SurfaceFlinger::invalidateHwcGeometry()
747{
748 mHwWorkListDirty = true;
749}
750
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
752{
753 bool recomputeVisibleRegions = false;
754 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700757 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 layer->lockPageFlip(recomputeVisibleRegions);
759 }
760 return recomputeVisibleRegions;
761}
762
763void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
764{
765 const GraphicPlane& plane(graphicPlane(0));
766 const Transform& planeTransform(plane.transform());
767 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700768 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700770 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 layer->unlockPageFlip(planeTransform, mDirtyRegion);
772 }
773}
774
Mathias Agopiana350ff92010-08-10 17:14:02 -0700775void SurfaceFlinger::handleWorkList()
776{
777 mHwWorkListDirty = false;
778 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
779 if (hwc.initCheck() == NO_ERROR) {
780 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
781 const size_t count = currentLayers.size();
782 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700783 hwc_layer_t* const cur(hwc.getLayers());
784 for (size_t i=0 ; cur && i<count ; i++) {
785 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700786 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700787 cur[i].compositionType = HWC_FRAMEBUFFER;
788 cur[i].flags |= HWC_SKIP_LAYER;
789 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700790 }
791 }
792}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700793
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794void SurfaceFlinger::handleRepaint()
795{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700796 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700797 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798
Glenn Kasten99ed2242011-12-15 09:51:17 -0800799 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 debugFlashRegions();
801 }
802
Mathias Agopianb8a55602009-06-26 19:06:36 -0700803 // set the frame buffer
804 const DisplayHardware& hw(graphicPlane(0).displayHardware());
805 glMatrixMode(GL_MODELVIEW);
806 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807
808 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700809 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
810 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700811 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700812 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
813 // takes a rectangle, we must make sure to update that whole
814 // rectangle in that case
815 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700816 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700817 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700818 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700820 // in the BUFFER_PRESERVED case, obviously, we can update only
821 // what's needed and nothing more.
822 // NOTE: this is NOT a common case, as preserving the backbuffer
823 // is costly and usually involves copying the whole update back.
824 }
825 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700826 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700827 // We need to redraw the rectangle that will be updated
828 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700829 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700830 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700831 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700832 } else {
833 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700835 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836 }
837 }
838
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700839 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 composeSurfaces(mDirtyRegion);
841
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700842 // update the swap region and clear the dirty region
843 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844 mDirtyRegion.clear();
845}
846
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700847void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700849 const DisplayHardware& hw(graphicPlane(0).displayHardware());
850 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700851 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700852 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700853 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700854 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700855
Mathias Agopianf384cc32011-09-08 18:31:55 -0700856 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
857 size_t count = layers.size();
858
Steve Blocke6f43dd2012-01-06 19:20:56 +0000859 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700860 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
861 hwc.getNumLayers(), count);
862
863 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700864 if (hwc.initCheck() == NO_ERROR) {
865 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
866 }
Mathias Agopian45721772010-08-12 15:03:26 -0700867
868 /*
869 * update the per-frame h/w composer data for each layer
870 * and build the transparent region of the FB
871 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700872 for (size_t i=0 ; i<count ; i++) {
873 const sp<LayerBase>& layer(layers[i]);
874 layer->setPerFrameData(&cur[i]);
875 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700876 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700877 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000878 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700879
Mathias Agopianf384cc32011-09-08 18:31:55 -0700880 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700881 // what's happening here is tricky.
882 // we want to clear all the layers with the CLEAR_FB flags
883 // that are opaque.
884 // however, since some GPU are efficient at preserving
885 // the backbuffer, we want to take advantage of that so we do the
886 // clear only in the dirty region (other areas will be preserved
887 // on those GPUs).
888 // NOTE: on non backbuffer preserving GPU, the dirty region
889 // has already been expanded as needed, so the code is correct
890 // there too.
891 //
892 // However, the content of the framebuffer cannot be trusted when
893 // we switch to/from FB/OVERLAY, in which case we need to
894 // expand the dirty region to those areas too.
895 //
896 // Note also that there is a special case when switching from
897 // "no layers in FB" to "some layers in FB", where we need to redraw
898 // the entire FB, since some areas might contain uninitialized
899 // data.
900 //
901 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700902 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700903 // that is, we can't erase anything outside the dirty region.
904
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700905 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700906
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700907 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
908 transparent.set(hw.getBounds());
909 dirtyInOut = transparent;
910 } else {
911 for (size_t i=0 ; i<count ; i++) {
912 const sp<LayerBase>& layer(layers[i]);
913 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
914 transparent.orSelf(layer->visibleRegionScreen);
915 }
916 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
917 if (isOverlay != layer->isOverlay()) {
918 // we transitioned to/from overlay, so add this layer
919 // to the dirty region so the framebuffer can be either
920 // cleared or redrawn.
921 dirtyInOut.orSelf(layer->visibleRegionScreen);
922 }
923 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800924 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700925 // don't erase stuff outside the dirty region
926 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700927 }
928
929 /*
930 * clear the area of the FB that need to be transparent
931 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700932 if (!transparent.isEmpty()) {
933 glClearColor(0,0,0,0);
934 Region::const_iterator it = transparent.begin();
935 Region::const_iterator const end = transparent.end();
936 const int32_t height = hw.getHeight();
937 while (it != end) {
938 const Rect& r(*it++);
939 const GLint sy = height - (r.top + r.height());
940 glScissor(r.left, sy, r.width(), r.height());
941 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800942 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700943 }
944 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700945}
Mathias Agopian45721772010-08-12 15:03:26 -0700946
Mathias Agopianf384cc32011-09-08 18:31:55 -0700947void SurfaceFlinger::composeSurfaces(const Region& dirty)
948{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700949 const DisplayHardware& hw(graphicPlane(0).displayHardware());
950 HWComposer& hwc(hw.getHwComposer());
951
952 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800953 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700954 // should never happen unless the window manager has a bug
955 // draw something...
956 drawWormhole();
957 }
958
Mathias Agopian45721772010-08-12 15:03:26 -0700959 /*
960 * and then, render the layers targeted at the framebuffer
961 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700962 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700963 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
964 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700965 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700966 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700967 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700968 }
969 const sp<LayerBase>& layer(layers[i]);
970 const Region clip(dirty.intersect(layer->visibleRegionScreen));
971 if (!clip.isEmpty()) {
972 layer->draw(clip);
973 }
974 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975}
976
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977void SurfaceFlinger::debugFlashRegions()
978{
Mathias Agopian0a917752010-06-14 21:20:00 -0700979 const DisplayHardware& hw(graphicPlane(0).displayHardware());
980 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700981 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700982 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700983 return;
984 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700985
Mathias Agopian0a917752010-06-14 21:20:00 -0700986 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
987 (flags & DisplayHardware::BUFFER_PRESERVED))) {
988 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
989 mDirtyRegion.bounds() : hw.bounds());
990 composeSurfaces(repaint);
991 }
992
Mathias Agopianc492e672011-10-18 14:49:27 -0700993 glDisable(GL_TEXTURE_EXTERNAL_OES);
994 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 glDisable(GL_SCISSOR_TEST);
997
Mathias Agopian0926f502009-05-04 14:17:04 -0700998 static int toggle = 0;
999 toggle = 1 - toggle;
1000 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001001 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001002 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001003 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001004 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005
Mathias Agopian20f68782009-05-11 00:03:41 -07001006 Region::const_iterator it = mDirtyRegion.begin();
1007 Region::const_iterator const end = mDirtyRegion.end();
1008 while (it != end) {
1009 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001011 { r.left, height - r.top },
1012 { r.left, height - r.bottom },
1013 { r.right, height - r.bottom },
1014 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 };
1016 glVertexPointer(2, GL_FLOAT, 0, vertices);
1017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1018 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001019
Mathias Agopian0656a682011-09-20 17:22:44 -07001020 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021
1022 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001023 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024
1025 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026}
1027
1028void SurfaceFlinger::drawWormhole() const
1029{
1030 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1031 if (region.isEmpty())
1032 return;
1033
1034 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1035 const int32_t width = hw.getWidth();
1036 const int32_t height = hw.getHeight();
1037
Glenn Kasten99ed2242011-12-15 09:51:17 -08001038 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001039 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001040 Region::const_iterator it = region.begin();
1041 Region::const_iterator const end = region.end();
1042 while (it != end) {
1043 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 const GLint sy = height - (r.top + r.height());
1045 glScissor(r.left, sy, r.width(), r.height());
1046 glClear(GL_COLOR_BUFFER_BIT);
1047 }
1048 } else {
1049 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1050 { width, height }, { 0, height } };
1051 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001052
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 glVertexPointer(2, GL_SHORT, 0, vertices);
1054 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1055 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001056
1057 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 glEnable(GL_TEXTURE_2D);
1059 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1060 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1061 glMatrixMode(GL_TEXTURE);
1062 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001063
1064 glDisable(GL_BLEND);
1065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001067 Region::const_iterator it = region.begin();
1068 Region::const_iterator const end = region.end();
1069 while (it != end) {
1070 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 const GLint sy = height - (r.top + r.height());
1072 glScissor(r.left, sy, r.width(), r.height());
1073 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1074 }
1075 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001076 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001077 glLoadIdentity();
1078 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 }
1080}
1081
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001082status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083{
1084 Mutex::Autolock _l(mStateLock);
1085 addLayer_l(layer);
1086 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1087 return NO_ERROR;
1088}
1089
Mathias Agopian96f08192010-06-02 23:28:45 -07001090status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1091{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001092 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001093 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1094}
1095
1096ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1097 const sp<LayerBaseClient>& lbc)
1098{
Mathias Agopian96f08192010-06-02 23:28:45 -07001099 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001100 size_t name = client->attachLayer(lbc);
1101
1102 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001103
1104 // add this layer to the current state list
1105 addLayer_l(lbc);
1106
Mathias Agopian4f113742011-05-03 16:21:41 -07001107 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001108}
1109
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001110status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111{
1112 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001113 status_t err = purgatorizeLayer_l(layer);
1114 if (err == NO_ERROR)
1115 setTransactionFlags(eTransactionNeeded);
1116 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117}
1118
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001119status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001121 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1122 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001123 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001124 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1126 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001127 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 return NO_ERROR;
1129 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001130 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131}
1132
Mathias Agopian9a112062009-04-17 19:36:26 -07001133status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1134{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001135 // First add the layer to the purgatory list, which makes sure it won't
1136 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001137 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001138 if (err >= 0) {
1139 mLayerPurgatory.add(layerBase);
1140 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001141
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001142 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001143
Mathias Agopian3d579642009-06-04 18:46:21 -07001144 // it's possible that we don't find a layer, because it might
1145 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001146 // from the user because there is a race between Client::destroySurface(),
1147 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001148 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1149}
1150
Mathias Agopian96f08192010-06-02 23:28:45 -07001151status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152{
Mathias Agopian96f08192010-06-02 23:28:45 -07001153 layer->forceVisibilityTransaction();
1154 setTransactionFlags(eTraversalNeeded);
1155 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156}
1157
Mathias Agopiandea20b12011-05-03 17:04:02 -07001158uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1159{
1160 return android_atomic_release_load(&mTransactionFlags);
1161}
1162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1164{
1165 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1166}
1167
Mathias Agopianbb641242010-05-18 17:06:55 -07001168uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169{
1170 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1171 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001172 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 }
1174 return old;
1175}
1176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001178void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001179 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001180 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001181
Jamie Gennis28378392011-10-12 17:39:00 -07001182 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001183 if (mCurrentState.orientation != orientation) {
1184 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1185 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001186 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001187 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001188 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001189 orientation);
1190 }
1191 }
1192
Mathias Agopian698c0872011-06-28 19:09:31 -07001193 const size_t count = state.size();
1194 for (size_t i=0 ; i<count ; i++) {
1195 const ComposerState& s(state[i]);
1196 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001197 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001198 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001199
Mathias Agopian386aa982011-11-07 21:58:03 -08001200 if (transactionFlags) {
1201 // this triggers the transaction
1202 setTransactionFlags(transactionFlags);
1203
1204 // if this is a synchronous transaction, wait for it to take effect
1205 // before returning.
1206 if (flags & eSynchronous) {
1207 mTransationPending = true;
1208 }
1209 while (mTransationPending) {
1210 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1211 if (CC_UNLIKELY(err != NO_ERROR)) {
1212 // just in case something goes wrong in SF, return to the
1213 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001214 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001215 mTransationPending = false;
1216 break;
1217 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 }
1220}
1221
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001222int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001223 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224{
Glenn Kasten99ed2242011-12-15 09:51:17 -08001225 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 return BAD_VALUE;
1227
1228 Mutex::Autolock _l(mStateLock);
1229 if (mCurrentState.orientation != orientation) {
1230 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001231 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 mCurrentState.orientation = orientation;
1233 setTransactionFlags(eTransactionNeeded);
1234 mTransactionCV.wait(mStateLock);
1235 } else {
1236 orientation = BAD_VALUE;
1237 }
1238 }
1239 return orientation;
1240}
1241
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001242sp<ISurface> SurfaceFlinger::createSurface(
1243 ISurfaceComposerClient::surface_data_t* params,
1244 const String8& name,
1245 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1247 uint32_t flags)
1248{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001249 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001250 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001251
1252 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001253 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001254 int(w), int(h));
1255 return surfaceHandle;
1256 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001257
Steve Block9d453682011-12-20 16:23:08 +00001258 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001259 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 switch (flags & eFXSurfaceMask) {
1261 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001262 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1263 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 break;
1265 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001266 // for now we treat Blur as Dim, until we can implement it
1267 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001269 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001271 case eFXSurfaceScreenshot:
1272 layer = createScreenshotSurface(client, d, w, h, flags);
1273 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 }
1275
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001276 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001277 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001278 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001279 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001282 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001283 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001284 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001285 if (normalLayer != 0) {
1286 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001287 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001288 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001289 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001290
Mathias Agopian96f08192010-06-02 23:28:45 -07001291 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 }
1293
1294 return surfaceHandle;
1295}
1296
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001297sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001298 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001300 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301{
1302 // initialize the surfaces
1303 switch (format) { // TODO: take h/w into account
1304 case PIXEL_FORMAT_TRANSPARENT:
1305 case PIXEL_FORMAT_TRANSLUCENT:
1306 format = PIXEL_FORMAT_RGBA_8888;
1307 break;
1308 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001309#ifdef NO_RGBX_8888
1310 format = PIXEL_FORMAT_RGB_565;
1311#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001312 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001313#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 break;
1315 }
1316
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001317#ifdef NO_RGBX_8888
1318 if (format == PIXEL_FORMAT_RGBX_8888)
1319 format = PIXEL_FORMAT_RGBA_8888;
1320#endif
1321
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001323 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001324 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001325 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001326 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
1328 return layer;
1329}
1330
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001331sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001332 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001333 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334{
Mathias Agopian96f08192010-06-02 23:28:45 -07001335 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001336 return layer;
1337}
1338
1339sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1340 const sp<Client>& client, DisplayID display,
1341 uint32_t w, uint32_t h, uint32_t flags)
1342{
1343 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 return layer;
1345}
1346
Mathias Agopian96f08192010-06-02 23:28:45 -07001347status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348{
Mathias Agopian9a112062009-04-17 19:36:26 -07001349 /*
1350 * called by the window manager, when a surface should be marked for
1351 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001352 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001353 * The surface is removed from the current and drawing lists, but placed
1354 * in the purgatory queue, so it's not destroyed right-away (we need
1355 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001356 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001357
Mathias Agopian48d819a2009-09-10 19:41:18 -07001358 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001359 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001360 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001361 if (layer != 0) {
1362 err = purgatorizeLayer_l(layer);
1363 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001364 setTransactionFlags(eTransactionNeeded);
1365 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001366 }
1367 return err;
1368}
1369
Mathias Agopianca4d3602011-05-19 15:38:14 -07001370status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001371{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001372 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001373 status_t err = NO_ERROR;
1374 sp<LayerBaseClient> l(layer.promote());
1375 if (l != NULL) {
1376 Mutex::Autolock _l(mStateLock);
1377 err = removeLayer_l(l);
1378 if (err == NAME_NOT_FOUND) {
1379 // The surface wasn't in the current list, which means it was
1380 // removed already, which means it is in the purgatory,
1381 // and need to be removed from there.
1382 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001383 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001384 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001385 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001386 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001387 "error removing layer=%p (%s)", l.get(), strerror(-err));
1388 }
1389 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390}
1391
Mathias Agopian698c0872011-06-28 19:09:31 -07001392uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001393 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001394 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001397 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1398 if (layer != 0) {
1399 const uint32_t what = s.what;
1400 if (what & ePositionChanged) {
1401 if (layer->setPosition(s.x, s.y))
1402 flags |= eTraversalNeeded;
1403 }
1404 if (what & eLayerChanged) {
1405 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1406 if (layer->setLayer(s.z)) {
1407 mCurrentState.layersSortedByZ.removeAt(idx);
1408 mCurrentState.layersSortedByZ.add(layer);
1409 // we need traversal (state changed)
1410 // AND transaction (list changed)
1411 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 }
1413 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001414 if (what & eSizeChanged) {
1415 if (layer->setSize(s.w, s.h)) {
1416 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001417 }
1418 }
1419 if (what & eAlphaChanged) {
1420 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1421 flags |= eTraversalNeeded;
1422 }
1423 if (what & eMatrixChanged) {
1424 if (layer->setMatrix(s.matrix))
1425 flags |= eTraversalNeeded;
1426 }
1427 if (what & eTransparentRegionChanged) {
1428 if (layer->setTransparentRegionHint(s.transparentRegion))
1429 flags |= eTraversalNeeded;
1430 }
1431 if (what & eVisibilityChanged) {
1432 if (layer->setFlags(s.flags, s.mask))
1433 flags |= eTraversalNeeded;
1434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001436 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437}
1438
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439void SurfaceFlinger::screenReleased(int dpy)
1440{
1441 // this may be called by a signal handler, we can't do too much in here
1442 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1443 signalEvent();
1444}
1445
1446void SurfaceFlinger::screenAcquired(int dpy)
1447{
1448 // this may be called by a signal handler, we can't do too much in here
1449 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1450 signalEvent();
1451}
1452
1453status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1454{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001455 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 char buffer[SIZE];
1457 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001458
1459 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 snprintf(buffer, SIZE, "Permission Denial: "
1461 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1462 IPCThreadState::self()->getCallingPid(),
1463 IPCThreadState::self()->getCallingUid());
1464 result.append(buffer);
1465 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001466
1467 // figure out if we're stuck somewhere
1468 const nsecs_t now = systemTime();
1469 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1470 const nsecs_t inTransaction(mDebugInTransaction);
1471 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1472 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1473
1474 // Try to get the main lock, but don't insist if we can't
1475 // (this would indicate SF is stuck, but we want to be able to
1476 // print something in dumpsys).
1477 int retry = 3;
1478 while (mStateLock.tryLock()<0 && --retry>=0) {
1479 usleep(1000000);
1480 }
1481 const bool locked(retry >= 0);
1482 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001483 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001484 "SurfaceFlinger appears to be unresponsive, "
1485 "dumping anyways (no locks held)\n");
1486 result.append(buffer);
1487 }
1488
Mathias Agopian48b888a2011-01-19 16:15:53 -08001489 /*
1490 * Dump the visible layer list
1491 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1493 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001494 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1495 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001497 const sp<LayerBase>& layer(currentLayers[i]);
1498 layer->dump(result, buffer, SIZE);
1499 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 s.transparentRegion.dump(result, "transparentRegion");
1501 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1502 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1503 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001504
Mathias Agopian48b888a2011-01-19 16:15:53 -08001505 /*
1506 * Dump the layers in the purgatory
1507 */
1508
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001509 const size_t purgatorySize = mLayerPurgatory.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001510 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1511 result.append(buffer);
1512 for (size_t i=0 ; i<purgatorySize ; i++) {
1513 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1514 layer->shortDump(result, buffer, SIZE);
1515 }
1516
1517 /*
1518 * Dump SurfaceFlinger global state
1519 */
1520
Mathias Agopianad701862011-07-14 18:01:49 -07001521 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001522 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001523
1524 const GLExtensions& extensions(GLExtensions::getInstance());
1525 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1526 extensions.getVendor(),
1527 extensions.getRenderer(),
1528 extensions.getVersion());
1529 result.append(buffer);
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001530
1531 snprintf(buffer, SIZE, "EGL : %s\n",
1532 eglQueryString(graphicPlane(0).getEGLDisplay(),
1533 EGL_VERSION_HW_ANDROID));
1534 result.append(buffer);
1535
Mathias Agopianad701862011-07-14 18:01:49 -07001536 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1537 result.append(buffer);
1538
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539 mWormholeRegion.dump(result, "WormholeRegion");
1540 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1541 snprintf(buffer, SIZE,
Jamie Gennisa402c4c2011-10-14 16:44:08 -07001542 " orientation=%d, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 mCurrentState.orientation, hw.canDraw());
1544 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001545 snprintf(buffer, SIZE,
1546 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001547 " last transaction time : %f us\n"
1548 " refresh-rate : %f fps\n"
1549 " x-dpi : %f\n"
1550 " y-dpi : %f\n",
1551 mLastSwapBufferTime/1000.0,
1552 mLastTransactionTime/1000.0,
1553 hw.getRefreshRate(),
1554 hw.getDpiX(),
1555 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001556 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001557
Mathias Agopian9795c422009-08-26 16:36:26 -07001558 if (inSwapBuffersDuration || !locked) {
1559 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1560 inSwapBuffersDuration/1000.0);
1561 result.append(buffer);
1562 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001563
Mathias Agopian9795c422009-08-26 16:36:26 -07001564 if (inTransactionDuration || !locked) {
1565 snprintf(buffer, SIZE, " transaction time: %f us\n",
1566 inTransactionDuration/1000.0);
1567 result.append(buffer);
1568 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001569
Mathias Agopian48b888a2011-01-19 16:15:53 -08001570 /*
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001571 * VSYNC state
1572 */
1573 mEventThread->dump(result, buffer, SIZE);
1574
1575 /*
Mathias Agopian48b888a2011-01-19 16:15:53 -08001576 * Dump HWComposer state
1577 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001578 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001579 snprintf(buffer, SIZE, "h/w composer state:\n");
1580 result.append(buffer);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001581 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1582 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001583 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001584 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001585 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001586
Mathias Agopian48b888a2011-01-19 16:15:53 -08001587 /*
1588 * Dump gralloc state
1589 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001590 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001591 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001592 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001593
1594 if (locked) {
1595 mStateLock.unlock();
1596 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 }
1598 write(fd, result.string(), result.size());
1599 return NO_ERROR;
1600}
1601
1602status_t SurfaceFlinger::onTransact(
1603 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1604{
1605 switch (code) {
1606 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001607 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001610 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001611 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 {
1613 // codes that require permission check
1614 IPCThreadState* ipc = IPCThreadState::self();
1615 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001616 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001617 if ((uid != AID_GRAPHICS) &&
1618 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001619 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001620 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1621 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001623 break;
1624 }
1625 case CAPTURE_SCREEN:
1626 {
1627 // codes that require permission check
1628 IPCThreadState* ipc = IPCThreadState::self();
1629 const int pid = ipc->getCallingPid();
1630 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001631 if ((uid != AID_GRAPHICS) &&
1632 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001633 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001634 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1635 return PERMISSION_DENIED;
1636 }
1637 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
1639 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001640
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1642 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001643 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001644 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001645 IPCThreadState* ipc = IPCThreadState::self();
1646 const int pid = ipc->getCallingPid();
1647 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001648 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001649 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 return PERMISSION_DENIED;
1651 }
1652 int n;
1653 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001654 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001655 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 return NO_ERROR;
1657 case 1002: // SHOW_UPDATES
1658 n = data.readInt32();
1659 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001660 invalidateHwcGeometry();
1661 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 return NO_ERROR;
1663 case 1003: // SHOW_BACKGROUND
1664 n = data.readInt32();
1665 mDebugBackground = n ? 1 : 0;
1666 return NO_ERROR;
1667 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001668 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001669 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001671 case 1005:{ // force transaction
1672 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1673 return NO_ERROR;
1674 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001675 case 1008: // toggle use of hw composer
1676 n = data.readInt32();
1677 mDebugDisableHWC = n ? 1 : 0;
1678 invalidateHwcGeometry();
1679 repaintEverything();
1680 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001681 case 1009: // toggle use of transform hint
1682 n = data.readInt32();
1683 mDebugDisableTransformHint = n ? 1 : 0;
1684 invalidateHwcGeometry();
1685 repaintEverything();
1686 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001688 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 reply->writeInt32(0);
1690 reply->writeInt32(mDebugRegion);
1691 reply->writeInt32(mDebugBackground);
1692 return NO_ERROR;
1693 case 1013: {
1694 Mutex::Autolock _l(mStateLock);
1695 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1696 reply->writeInt32(hw.getPageFlipCount());
1697 }
1698 return NO_ERROR;
1699 }
1700 }
1701 return err;
1702}
1703
Mathias Agopian53331da2011-08-22 21:44:41 -07001704void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001705 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001706 const Rect bounds(hw.getBounds());
1707 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001708 signalEvent();
1709}
1710
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001711void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1712 Mutex::Autolock _l(mInvalidateLock);
1713 mInvalidateRegion = reg;
1714}
1715
1716Region SurfaceFlinger::getAndClearInvalidateRegion() {
1717 Mutex::Autolock _l(mInvalidateLock);
1718 Region reg(mInvalidateRegion);
1719 mInvalidateRegion.clear();
1720 return reg;
1721}
1722
Mathias Agopian59119e62010-10-11 12:37:43 -07001723// ---------------------------------------------------------------------------
1724
Mathias Agopian118d0242011-10-13 16:02:48 -07001725status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1726 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1727{
1728 Mutex::Autolock _l(mStateLock);
1729 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1730}
1731
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001732status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1733 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001734{
Mathias Agopian59119e62010-10-11 12:37:43 -07001735 if (!GLExtensions::getInstance().haveFramebufferObject())
1736 return INVALID_OPERATION;
1737
1738 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001739 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001740 const uint32_t hw_w = hw.getWidth();
1741 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001742 GLfloat u = 1;
1743 GLfloat v = 1;
1744
1745 // make sure to clear all GL error flags
1746 while ( glGetError() != GL_NO_ERROR ) ;
1747
1748 // create a FBO
1749 GLuint name, tname;
1750 glGenTextures(1, &tname);
1751 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001752 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1753 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001754 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001755 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001756 GLint tw = (2 << (31 - clz(hw_w)));
1757 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001758 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1759 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001760 u = GLfloat(hw_w) / tw;
1761 v = GLfloat(hw_h) / th;
1762 }
1763 glGenFramebuffersOES(1, &name);
1764 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001765 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1766 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001767
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001768 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001769 glDisable(GL_TEXTURE_EXTERNAL_OES);
1770 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001771 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001772 glClearColor(0,0,0,1);
1773 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001774 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001775 glMatrixMode(GL_MODELVIEW);
1776 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001777 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1778 const size_t count = layers.size();
1779 for (size_t i=0 ; i<count ; ++i) {
1780 const sp<LayerBase>& layer(layers[i]);
1781 layer->drawForSreenShot();
1782 }
1783
Mathias Agopian118d0242011-10-13 16:02:48 -07001784 hw.compositionComplete();
1785
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001786 // back to main framebuffer
1787 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1788 glDisable(GL_SCISSOR_TEST);
1789 glDeleteFramebuffersOES(1, &name);
1790
1791 *textureName = tname;
1792 *uOut = u;
1793 *vOut = v;
1794 return NO_ERROR;
1795}
1796
1797// ---------------------------------------------------------------------------
1798
1799status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1800{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001801 // get screen geometry
1802 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1803 const uint32_t hw_w = hw.getWidth();
1804 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001805 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001806
1807 GLfloat u, v;
1808 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001809 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001810 if (result != NO_ERROR) {
1811 return result;
1812 }
1813
1814 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001815 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001816 glBindTexture(GL_TEXTURE_2D, tname);
1817 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1818 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1819 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1820 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1821 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1822 glVertexPointer(2, GL_FLOAT, 0, vtx);
1823
Mathias Agopianffcf4652011-07-07 17:30:31 -07001824 /*
1825 * Texture coordinate mapping
1826 *
1827 * u
1828 * 1 +----------+---+
1829 * | | | | image is inverted
1830 * | V | | w.r.t. the texture
1831 * 1-v +----------+ | coordinates
1832 * | |
1833 * | |
1834 * | |
1835 * 0 +--------------+
1836 * 0 1
1837 *
1838 */
1839
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001840 class s_curve_interpolator {
1841 const float nbFrames, s, v;
1842 public:
1843 s_curve_interpolator(int nbFrames, float s)
1844 : nbFrames(1.0f / (nbFrames-1)), s(s),
1845 v(1.0f + expf(-s + 0.5f*s)) {
1846 }
1847 float operator()(int f) {
1848 const float x = f * nbFrames;
1849 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1850 }
1851 };
1852
1853 class v_stretch {
1854 const GLfloat hw_w, hw_h;
1855 public:
1856 v_stretch(uint32_t hw_w, uint32_t hw_h)
1857 : hw_w(hw_w), hw_h(hw_h) {
1858 }
1859 void operator()(GLfloat* vtx, float v) {
1860 const GLfloat w = hw_w + (hw_w * v);
1861 const GLfloat h = hw_h - (hw_h * v);
1862 const GLfloat x = (hw_w - w) * 0.5f;
1863 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001864 vtx[0] = x; vtx[1] = y;
1865 vtx[2] = x; vtx[3] = y + h;
1866 vtx[4] = x + w; vtx[5] = y + h;
1867 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001868 }
1869 };
1870
1871 class h_stretch {
1872 const GLfloat hw_w, hw_h;
1873 public:
1874 h_stretch(uint32_t hw_w, uint32_t hw_h)
1875 : hw_w(hw_w), hw_h(hw_h) {
1876 }
1877 void operator()(GLfloat* vtx, float v) {
1878 const GLfloat w = hw_w - (hw_w * v);
1879 const GLfloat h = 1.0f;
1880 const GLfloat x = (hw_w - w) * 0.5f;
1881 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001882 vtx[0] = x; vtx[1] = y;
1883 vtx[2] = x; vtx[3] = y + h;
1884 vtx[4] = x + w; vtx[5] = y + h;
1885 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001886 }
1887 };
1888
1889 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001890 char value[PROPERTY_VALUE_MAX];
1891 property_get("debug.sf.electron_frames", value, "24");
1892 int nbFrames = (atoi(value) + 1) >> 1;
1893 if (nbFrames <= 0) // just in case
1894 nbFrames = 24;
1895
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001896 s_curve_interpolator itr(nbFrames, 7.5f);
1897 s_curve_interpolator itg(nbFrames, 8.0f);
1898 s_curve_interpolator itb(nbFrames, 8.5f);
1899
1900 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001901
1902 glMatrixMode(GL_TEXTURE);
1903 glLoadIdentity();
1904 glMatrixMode(GL_MODELVIEW);
1905 glLoadIdentity();
1906
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001907 glEnable(GL_BLEND);
1908 glBlendFunc(GL_ONE, GL_ONE);
1909 for (int i=0 ; i<nbFrames ; i++) {
1910 float x, y, w, h;
1911 const float vr = itr(i);
1912 const float vg = itg(i);
1913 const float vb = itb(i);
1914
1915 // clear screen
1916 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001917 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001918 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001919
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001920 // draw the red plane
1921 vverts(vtx, vr);
1922 glColorMask(1,0,0,1);
1923 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001924
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001925 // draw the green plane
1926 vverts(vtx, vg);
1927 glColorMask(0,1,0,1);
1928 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001929
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001930 // draw the blue plane
1931 vverts(vtx, vb);
1932 glColorMask(0,0,1,1);
1933 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001934
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001935 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001936 glDisable(GL_TEXTURE_2D);
1937 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001938 glColor4f(vg, vg, vg, 1);
1939 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1940 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001941 }
1942
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001943 h_stretch hverts(hw_w, hw_h);
1944 glDisable(GL_BLEND);
1945 glDisable(GL_TEXTURE_2D);
1946 glColorMask(1,1,1,1);
1947 for (int i=0 ; i<nbFrames ; i++) {
1948 const float v = itg(i);
1949 hverts(vtx, v);
1950 glClear(GL_COLOR_BUFFER_BIT);
1951 glColor4f(1-v, 1-v, 1-v, 1);
1952 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1953 hw.flip(screenBounds);
1954 }
1955
1956 glColorMask(1,1,1,1);
1957 glEnable(GL_SCISSOR_TEST);
1958 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1959 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001960 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07001961 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001962 return NO_ERROR;
1963}
1964
1965status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1966{
1967 status_t result = PERMISSION_DENIED;
1968
1969 if (!GLExtensions::getInstance().haveFramebufferObject())
1970 return INVALID_OPERATION;
1971
1972
1973 // get screen geometry
1974 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1975 const uint32_t hw_w = hw.getWidth();
1976 const uint32_t hw_h = hw.getHeight();
1977 const Region screenBounds(hw.bounds());
1978
1979 GLfloat u, v;
1980 GLuint tname;
1981 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1982 if (result != NO_ERROR) {
1983 return result;
1984 }
1985
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001986 GLfloat vtx[8];
1987 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001988 glBindTexture(GL_TEXTURE_2D, tname);
1989 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1990 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1991 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1992 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1993 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1994 glVertexPointer(2, GL_FLOAT, 0, vtx);
1995
1996 class s_curve_interpolator {
1997 const float nbFrames, s, v;
1998 public:
1999 s_curve_interpolator(int nbFrames, float s)
2000 : nbFrames(1.0f / (nbFrames-1)), s(s),
2001 v(1.0f + expf(-s + 0.5f*s)) {
2002 }
2003 float operator()(int f) {
2004 const float x = f * nbFrames;
2005 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2006 }
2007 };
2008
2009 class v_stretch {
2010 const GLfloat hw_w, hw_h;
2011 public:
2012 v_stretch(uint32_t hw_w, uint32_t hw_h)
2013 : hw_w(hw_w), hw_h(hw_h) {
2014 }
2015 void operator()(GLfloat* vtx, float v) {
2016 const GLfloat w = hw_w + (hw_w * v);
2017 const GLfloat h = hw_h - (hw_h * v);
2018 const GLfloat x = (hw_w - w) * 0.5f;
2019 const GLfloat y = (hw_h - h) * 0.5f;
2020 vtx[0] = x; vtx[1] = y;
2021 vtx[2] = x; vtx[3] = y + h;
2022 vtx[4] = x + w; vtx[5] = y + h;
2023 vtx[6] = x + w; vtx[7] = y;
2024 }
2025 };
2026
2027 class h_stretch {
2028 const GLfloat hw_w, hw_h;
2029 public:
2030 h_stretch(uint32_t hw_w, uint32_t hw_h)
2031 : hw_w(hw_w), hw_h(hw_h) {
2032 }
2033 void operator()(GLfloat* vtx, float v) {
2034 const GLfloat w = hw_w - (hw_w * v);
2035 const GLfloat h = 1.0f;
2036 const GLfloat x = (hw_w - w) * 0.5f;
2037 const GLfloat y = (hw_h - h) * 0.5f;
2038 vtx[0] = x; vtx[1] = y;
2039 vtx[2] = x; vtx[3] = y + h;
2040 vtx[4] = x + w; vtx[5] = y + h;
2041 vtx[6] = x + w; vtx[7] = y;
2042 }
2043 };
2044
Mathias Agopiana6546e52010-10-14 12:33:07 -07002045 // the full animation is 12 frames
2046 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 s_curve_interpolator itr(nbFrames, 7.5f);
2048 s_curve_interpolator itg(nbFrames, 8.0f);
2049 s_curve_interpolator itb(nbFrames, 8.5f);
2050
2051 h_stretch hverts(hw_w, hw_h);
2052 glDisable(GL_BLEND);
2053 glDisable(GL_TEXTURE_2D);
2054 glColorMask(1,1,1,1);
2055 for (int i=nbFrames-1 ; i>=0 ; i--) {
2056 const float v = itg(i);
2057 hverts(vtx, v);
2058 glClear(GL_COLOR_BUFFER_BIT);
2059 glColor4f(1-v, 1-v, 1-v, 1);
2060 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2061 hw.flip(screenBounds);
2062 }
2063
Mathias Agopiana6546e52010-10-14 12:33:07 -07002064 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002065 v_stretch vverts(hw_w, hw_h);
2066 glEnable(GL_BLEND);
2067 glBlendFunc(GL_ONE, GL_ONE);
2068 for (int i=nbFrames-1 ; i>=0 ; i--) {
2069 float x, y, w, h;
2070 const float vr = itr(i);
2071 const float vg = itg(i);
2072 const float vb = itb(i);
2073
2074 // clear screen
2075 glColorMask(1,1,1,1);
2076 glClear(GL_COLOR_BUFFER_BIT);
2077 glEnable(GL_TEXTURE_2D);
2078
2079 // draw the red plane
2080 vverts(vtx, vr);
2081 glColorMask(1,0,0,1);
2082 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2083
2084 // draw the green plane
2085 vverts(vtx, vg);
2086 glColorMask(0,1,0,1);
2087 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2088
2089 // draw the blue plane
2090 vverts(vtx, vb);
2091 glColorMask(0,0,1,1);
2092 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2093
2094 hw.flip(screenBounds);
2095 }
2096
2097 glColorMask(1,1,1,1);
2098 glEnable(GL_SCISSOR_TEST);
2099 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002100 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002101 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002102 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002103
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002104 return NO_ERROR;
2105}
2106
2107// ---------------------------------------------------------------------------
2108
Mathias Agopianabd671a2010-10-14 14:54:06 -07002109status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002110{
2111 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2112 if (!hw.canDraw()) {
2113 // we're already off
2114 return NO_ERROR;
2115 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002116
2117 // turn off hwc while we're doing the animation
2118 hw.getHwComposer().disable();
2119 // and make sure to turn it back on (if needed) next time we compose
2120 invalidateHwcGeometry();
2121
Mathias Agopianabd671a2010-10-14 14:54:06 -07002122 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2123 electronBeamOffAnimationImplLocked();
2124 }
2125
2126 // always clear the whole screen at the end of the animation
2127 glClearColor(0,0,0,1);
2128 glDisable(GL_SCISSOR_TEST);
2129 glClear(GL_COLOR_BUFFER_BIT);
2130 glEnable(GL_SCISSOR_TEST);
2131 hw.flip( Region(hw.bounds()) );
2132
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002133 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002134}
2135
2136status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2137{
Mathias Agopian59119e62010-10-11 12:37:43 -07002138 class MessageTurnElectronBeamOff : public MessageBase {
2139 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002140 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002141 status_t result;
2142 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002143 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2144 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002145 }
2146 status_t getResult() const {
2147 return result;
2148 }
2149 virtual bool handler() {
2150 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002151 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002152 return true;
2153 }
2154 };
2155
Mathias Agopianabd671a2010-10-14 14:54:06 -07002156 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002157 status_t res = postMessageSync(msg);
2158 if (res == NO_ERROR) {
2159 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002160
2161 // work-around: when the power-manager calls us we activate the
2162 // animation. eventually, the "on" animation will be called
2163 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002164 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002165 }
2166 return res;
2167}
2168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002169// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002170
Mathias Agopianabd671a2010-10-14 14:54:06 -07002171status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002172{
2173 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2174 if (hw.canDraw()) {
2175 // we're already on
2176 return NO_ERROR;
2177 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002178 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2179 electronBeamOnAnimationImplLocked();
2180 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002181
2182 // make sure to redraw the whole screen when the animation is done
2183 mDirtyRegion.set(hw.bounds());
2184 signalEvent();
2185
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002186 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002187}
2188
2189status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2190{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002191 class MessageTurnElectronBeamOn : public MessageBase {
2192 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002193 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002194 status_t result;
2195 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002196 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2197 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 }
2199 status_t getResult() const {
2200 return result;
2201 }
2202 virtual bool handler() {
2203 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002204 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002205 return true;
2206 }
2207 };
2208
Mathias Agopianabd671a2010-10-14 14:54:06 -07002209 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002210 return NO_ERROR;
2211}
2212
2213// ---------------------------------------------------------------------------
2214
Mathias Agopian74c40c02010-09-29 13:02:36 -07002215status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2216 sp<IMemoryHeap>* heap,
2217 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002218 uint32_t sw, uint32_t sh,
2219 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002220{
2221 status_t result = PERMISSION_DENIED;
2222
2223 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002224 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002225 return BAD_VALUE;
2226
2227 if (!GLExtensions::getInstance().haveFramebufferObject())
2228 return INVALID_OPERATION;
2229
2230 // get screen geometry
2231 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2232 const uint32_t hw_w = hw.getWidth();
2233 const uint32_t hw_h = hw.getHeight();
2234
2235 if ((sw > hw_w) || (sh > hw_h))
2236 return BAD_VALUE;
2237
2238 sw = (!sw) ? hw_w : sw;
2239 sh = (!sh) ? hw_h : sh;
2240 const size_t size = sw * sh * 4;
2241
Steve Block9d453682011-12-20 16:23:08 +00002242 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002243 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002244
Mathias Agopian74c40c02010-09-29 13:02:36 -07002245 // make sure to clear all GL error flags
2246 while ( glGetError() != GL_NO_ERROR ) ;
2247
2248 // create a FBO
2249 GLuint name, tname;
2250 glGenRenderbuffersOES(1, &tname);
2251 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2252 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2253 glGenFramebuffersOES(1, &name);
2254 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2255 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2256 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2257
2258 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002259
Mathias Agopian74c40c02010-09-29 13:02:36 -07002260 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2261
2262 // invert everything, b/c glReadPixel() below will invert the FB
2263 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002264 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002265 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002266 glMatrixMode(GL_PROJECTION);
2267 glPushMatrix();
2268 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002269 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270 glMatrixMode(GL_MODELVIEW);
2271
2272 // redraw the screen entirely...
2273 glClearColor(0,0,0,1);
2274 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002275
Jamie Gennis9575f602011-10-07 14:51:16 -07002276 const LayerVector& layers(mDrawingState.layersSortedByZ);
2277 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002278 for (size_t i=0 ; i<count ; ++i) {
2279 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002280 const uint32_t flags = layer->drawingState().flags;
2281 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2282 const uint32_t z = layer->drawingState().z;
2283 if (z >= minLayerZ && z <= maxLayerZ) {
2284 layer->drawForSreenShot();
2285 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002286 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287 }
2288
2289 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002290 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291 glScissor(0, 0, sw, sh);
2292
2293 // check for errors and return screen capture
2294 if (glGetError() != GL_NO_ERROR) {
2295 // error while rendering
2296 result = INVALID_OPERATION;
2297 } else {
2298 // allocate shared memory large enough to hold the
2299 // screen capture
2300 sp<MemoryHeapBase> base(
2301 new MemoryHeapBase(size, 0, "screen-capture") );
2302 void* const ptr = base->getBase();
2303 if (ptr) {
2304 // capture the screen with glReadPixels()
2305 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2306 if (glGetError() == GL_NO_ERROR) {
2307 *heap = base;
2308 *w = sw;
2309 *h = sh;
2310 *f = PIXEL_FORMAT_RGBA_8888;
2311 result = NO_ERROR;
2312 }
2313 } else {
2314 result = NO_MEMORY;
2315 }
2316 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002317 glEnable(GL_SCISSOR_TEST);
2318 glViewport(0, 0, hw_w, hw_h);
2319 glMatrixMode(GL_PROJECTION);
2320 glPopMatrix();
2321 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 } else {
2323 result = BAD_VALUE;
2324 }
2325
2326 // release FBO resources
2327 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2328 glDeleteRenderbuffersOES(1, &tname);
2329 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002330
2331 hw.compositionComplete();
2332
Steve Block9d453682011-12-20 16:23:08 +00002333 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002334
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 return result;
2336}
2337
2338
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002339status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2340 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002341 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002342 uint32_t sw, uint32_t sh,
2343 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002344{
2345 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002346 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002347 return BAD_VALUE;
2348
2349 if (!GLExtensions::getInstance().haveFramebufferObject())
2350 return INVALID_OPERATION;
2351
2352 class MessageCaptureScreen : public MessageBase {
2353 SurfaceFlinger* flinger;
2354 DisplayID dpy;
2355 sp<IMemoryHeap>* heap;
2356 uint32_t* w;
2357 uint32_t* h;
2358 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002359 uint32_t sw;
2360 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002361 uint32_t minLayerZ;
2362 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002363 status_t result;
2364 public:
2365 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002366 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002367 uint32_t sw, uint32_t sh,
2368 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002369 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002370 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2371 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2372 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002373 {
2374 }
2375 status_t getResult() const {
2376 return result;
2377 }
2378 virtual bool handler() {
2379 Mutex::Autolock _l(flinger->mStateLock);
2380
2381 // if we have secure windows, never allow the screen capture
2382 if (flinger->mSecureFrameBuffer)
2383 return true;
2384
Mathias Agopian74c40c02010-09-29 13:02:36 -07002385 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002386 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002387
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002388 return true;
2389 }
2390 };
2391
2392 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002393 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002394 status_t res = postMessageSync(msg);
2395 if (res == NO_ERROR) {
2396 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2397 }
2398 return res;
2399}
2400
2401// ---------------------------------------------------------------------------
2402
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002403sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2404{
2405 sp<Layer> result;
2406 Mutex::Autolock _l(mStateLock);
2407 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2408 return result;
2409}
2410
2411// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002412
Mathias Agopian96f08192010-06-02 23:28:45 -07002413Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002414 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002415{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002416}
2417
Mathias Agopian96f08192010-06-02 23:28:45 -07002418Client::~Client()
2419{
Mathias Agopian96f08192010-06-02 23:28:45 -07002420 const size_t count = mLayers.size();
2421 for (size_t i=0 ; i<count ; i++) {
2422 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2423 if (layer != 0) {
2424 mFlinger->removeLayer(layer);
2425 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002426 }
2427}
2428
Mathias Agopian96f08192010-06-02 23:28:45 -07002429status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002430 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002432
Mathias Agopian4f113742011-05-03 16:21:41 -07002433size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002434{
Mathias Agopian4f113742011-05-03 16:21:41 -07002435 Mutex::Autolock _l(mLock);
2436 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002437 mLayers.add(name, layer);
2438 return name;
2439}
2440
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002441void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002442{
Mathias Agopian4f113742011-05-03 16:21:41 -07002443 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002444 // we do a linear search here, because this doesn't happen often
2445 const size_t count = mLayers.size();
2446 for (size_t i=0 ; i<count ; i++) {
2447 if (mLayers.valueAt(i) == layer) {
2448 mLayers.removeItemsAt(i, 1);
2449 break;
2450 }
2451 }
2452}
Mathias Agopian4f113742011-05-03 16:21:41 -07002453sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2454{
2455 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002456 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002457 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002458 if (layer != 0) {
2459 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002460 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002461 }
2462 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002463}
2464
Mathias Agopiana67932f2011-04-20 14:20:59 -07002465
2466status_t Client::onTransact(
2467 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2468{
2469 // these must be checked
2470 IPCThreadState* ipc = IPCThreadState::self();
2471 const int pid = ipc->getCallingPid();
2472 const int uid = ipc->getCallingUid();
2473 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002474 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002475 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002476 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002477 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002478 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002479 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2480 return PERMISSION_DENIED;
2481 }
2482 }
2483 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002484}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002485
2486
Mathias Agopian96f08192010-06-02 23:28:45 -07002487sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002488 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002489 const String8& name,
2490 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 uint32_t flags)
2492{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002493 /*
2494 * createSurface must be called from the GL thread so that it can
2495 * have access to the GL context.
2496 */
2497
2498 class MessageCreateSurface : public MessageBase {
2499 sp<ISurface> result;
2500 SurfaceFlinger* flinger;
2501 ISurfaceComposerClient::surface_data_t* params;
2502 Client* client;
2503 const String8& name;
2504 DisplayID display;
2505 uint32_t w, h;
2506 PixelFormat format;
2507 uint32_t flags;
2508 public:
2509 MessageCreateSurface(SurfaceFlinger* flinger,
2510 ISurfaceComposerClient::surface_data_t* params,
2511 const String8& name, Client* client,
2512 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2513 uint32_t flags)
2514 : flinger(flinger), params(params), client(client), name(name),
2515 display(display), w(w), h(h), format(format), flags(flags)
2516 {
2517 }
2518 sp<ISurface> getResult() const { return result; }
2519 virtual bool handler() {
2520 result = flinger->createSurface(params, name, client,
2521 display, w, h, format, flags);
2522 return true;
2523 }
2524 };
2525
2526 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2527 params, name, this, display, w, h, format, flags);
2528 mFlinger->postMessageSync(msg);
2529 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530}
Mathias Agopian96f08192010-06-02 23:28:45 -07002531status_t Client::destroySurface(SurfaceID sid) {
2532 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534
2535// ---------------------------------------------------------------------------
2536
Jamie Gennis9a78c902011-01-12 18:30:40 -08002537GraphicBufferAlloc::GraphicBufferAlloc() {}
2538
2539GraphicBufferAlloc::~GraphicBufferAlloc() {}
2540
2541sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002542 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002543 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2544 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002545 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002546 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002547 if (err == NO_MEMORY) {
2548 GraphicBuffer::dumpAllocationsToSystemLog();
2549 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002550 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002551 "failed (%s), handle=%p",
2552 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002553 return 0;
2554 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002555 return graphicBuffer;
2556}
2557
Jamie Gennis9a78c902011-01-12 18:30:40 -08002558// ---------------------------------------------------------------------------
2559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560GraphicPlane::GraphicPlane()
2561 : mHw(0)
2562{
2563}
2564
2565GraphicPlane::~GraphicPlane() {
2566 delete mHw;
2567}
2568
2569bool GraphicPlane::initialized() const {
2570 return mHw ? true : false;
2571}
2572
Mathias Agopian2b92d892010-02-08 15:49:35 -08002573int GraphicPlane::getWidth() const {
2574 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002575}
2576
Mathias Agopian2b92d892010-02-08 15:49:35 -08002577int GraphicPlane::getHeight() const {
2578 return mHeight;
2579}
2580
2581void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2582{
2583 mHw = hw;
2584
2585 // initialize the display orientation transform.
2586 // it's a constant that should come from the display driver.
2587 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2588 char property[PROPERTY_VALUE_MAX];
2589 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2590 //displayOrientation
2591 switch (atoi(property)) {
2592 case 90:
2593 displayOrientation = ISurfaceComposer::eOrientation90;
2594 break;
2595 case 270:
2596 displayOrientation = ISurfaceComposer::eOrientation270;
2597 break;
2598 }
2599 }
2600
2601 const float w = hw->getWidth();
2602 const float h = hw->getHeight();
2603 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2604 &mDisplayTransform);
2605 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2606 mDisplayWidth = h;
2607 mDisplayHeight = w;
2608 } else {
2609 mDisplayWidth = w;
2610 mDisplayHeight = h;
2611 }
2612
2613 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002614}
2615
2616status_t GraphicPlane::orientationToTransfrom(
2617 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002618{
2619 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002620 switch (orientation) {
2621 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002622 flags = Transform::ROT_0;
2623 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002624 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002625 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626 break;
2627 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002628 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629 break;
2630 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002631 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632 break;
2633 default:
2634 return BAD_VALUE;
2635 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002636 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 return NO_ERROR;
2638}
2639
2640status_t GraphicPlane::setOrientation(int orientation)
2641{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002642 // If the rotation can be handled in hardware, this is where
2643 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002644
2645 const DisplayHardware& hw(displayHardware());
2646 const float w = mDisplayWidth;
2647 const float h = mDisplayHeight;
2648 mWidth = int(w);
2649 mHeight = int(h);
2650
2651 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002652 GraphicPlane::orientationToTransfrom(orientation, w, h,
2653 &orientationTransform);
2654 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2655 mWidth = int(h);
2656 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002657 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002658
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002659 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002660 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002661 return NO_ERROR;
2662}
2663
2664const DisplayHardware& GraphicPlane::displayHardware() const {
2665 return *mHw;
2666}
2667
Mathias Agopian59119e62010-10-11 12:37:43 -07002668DisplayHardware& GraphicPlane::editDisplayHardware() {
2669 return *mHw;
2670}
2671
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002672const Transform& GraphicPlane::transform() const {
2673 return mGlobalTransform;
2674}
2675
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002676EGLDisplay GraphicPlane::getEGLDisplay() const {
2677 return mHw->getEGLDisplay();
2678}
2679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002680// ---------------------------------------------------------------------------
2681
2682}; // namespace android