blob: 887aee7bf44032deb0c48b89ae5f9d177a890974 [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
Mathias Agopianad8d13c2012-01-29 22:40:08 -0800191 Vector<ComposerState> state;
192 setTransactionState(state, eOrientationDefault, 0);
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700193
194 // restart the boot-animation
195 property_set("ctl.start", "bootanim");
196}
197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198void SurfaceFlinger::onFirstRef()
199{
200 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
201
202 // Wait for the main thread to be done with its initialization
203 mReadyToRunBarrier.wait();
204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206static inline uint16_t pack565(int r, int g, int b) {
207 return (r<<11)|(g<<5)|b;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210status_t SurfaceFlinger::readyToRun()
211{
Steve Blocka19954a2012-01-04 20:05:49 +0000212 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 "Initializing graphics H/W...");
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 // we only support one display currently
216 int dpy = 0;
217
218 {
219 // initialize the main display
220 GraphicPlane& plane(graphicPlane(dpy));
221 DisplayHardware* const hw = new DisplayHardware(this, dpy);
222 plane.setDisplayHardware(hw);
223 }
224
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 // create the shared control-block
226 mServerHeap = new MemoryHeapBase(4096,
227 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000228 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 new(mServerCblk) surface_flinger_cblk_t;
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 // initialize primary screen
236 // (other display should be initialized in the same manner, but
237 // asynchronously, as they could come and go. None of this is supported
238 // yet).
239 const GraphicPlane& plane(graphicPlane(dpy));
240 const DisplayHardware& hw = plane.displayHardware();
241 const uint32_t w = hw.getWidth();
242 const uint32_t h = hw.getHeight();
243 const uint32_t f = hw.getFormat();
244 hw.makeCurrent();
245
246 // initialize the shared control block
247 mServerCblk->connected |= 1<<dpy;
248 display_cblk_t* dcblk = mServerCblk->displays + dpy;
249 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800250 dcblk->w = plane.getWidth();
251 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 dcblk->format = f;
253 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
254 dcblk->xdpi = hw.getDpiX();
255 dcblk->ydpi = hw.getDpiY();
256 dcblk->fps = hw.getRefreshRate();
257 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
259 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700261 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 glEnableClientState(GL_VERTEX_ARRAY);
263 glEnable(GL_SCISSOR_TEST);
264 glShadeModel(GL_FLAT);
265 glDisable(GL_DITHER);
266 glDisable(GL_CULL_FACE);
267
268 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
269 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700270 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 glGenTextures(1, &mWormholeTexName);
272 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
279
280 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
281 glGenTextures(1, &mProtectedTexName);
282 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
287 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
288 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
290 glViewport(0, 0, w, h);
291 glMatrixMode(GL_PROJECTION);
292 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700293 // put the origin in the left-bottom corner
294 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 -0800295
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800296
297 // start the EventThread
298 mEventThread = new EventThread(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
300 /*
301 * We're now ready to accept clients...
302 */
303
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800304 mReadyToRunBarrier.open();
305
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700306 // start boot animation
307 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 return NO_ERROR;
310}
311
312// ----------------------------------------------------------------------------
313#if 0
314#pragma mark -
315#pragma mark Events Handler
316#endif
317
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800318void SurfaceFlinger::waitForEvent() {
319 mEventQueue.waitMessage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320}
321
322void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700323 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324}
325
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800326status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
327 nsecs_t reltime, uint32_t flags) {
328 return mEventQueue.postMessage(msg, reltime);
329}
330
331status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
332 nsecs_t reltime, uint32_t flags) {
333 status_t res = mEventQueue.postMessage(msg, reltime);
334 if (res == NO_ERROR) {
335 msg->wait();
336 }
337 return res;
338}
339
340// ----------------------------------------------------------------------------
341
Jamie Gennis582270d2011-08-17 18:19:00 -0700342bool SurfaceFlinger::authenticateSurfaceTexture(
343 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800344 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700345 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800346
347 // Check the visible layer list for the ISurface
348 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
349 size_t count = currentLayers.size();
350 for (size_t i=0 ; i<count ; i++) {
351 const sp<LayerBase>& layer(currentLayers[i]);
352 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700353 if (lbc != NULL) {
354 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
355 if (lbcBinder == surfaceTextureBinder) {
356 return true;
357 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800358 }
359 }
360
361 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700362 // SurfaceTexture gets destroyed before all the clients are done using it,
363 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800364 // will instead fail later on when the client tries to use the surface,
365 // which should be reported as "surface XYZ returned an -ENODEV". The
366 // purgatorized layers are no less authentic than the visible ones, so this
367 // should not cause any harm.
368 size_t purgatorySize = mLayerPurgatory.size();
369 for (size_t i=0 ; i<purgatorySize ; i++) {
370 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
371 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700372 if (lbc != NULL) {
373 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
374 if (lbcBinder == surfaceTextureBinder) {
375 return true;
376 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800377 }
378 }
379
380 return false;
381}
382
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800383// ----------------------------------------------------------------------------
384
385sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800386 sp<DisplayEventConnection> result(new DisplayEventConnection(mEventThread));
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800387 return result;
Mathias Agopianbb641242010-05-18 17:06:55 -0700388}
389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390// ----------------------------------------------------------------------------
391#if 0
392#pragma mark -
393#pragma mark Main loop
394#endif
395
396bool SurfaceFlinger::threadLoop()
397{
398 waitForEvent();
399
400 // check for transactions
Glenn Kasten99ed2242011-12-15 09:51:17 -0800401 if (CC_UNLIKELY(mConsoleSignals)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 handleConsoleEvents();
403 }
404
Mathias Agopian698c0872011-06-28 19:09:31 -0700405 // if we're in a global transaction, don't do anything.
406 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
407 uint32_t transactionFlags = peekTransactionFlags(mask);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800408 if (CC_UNLIKELY(transactionFlags)) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700409 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 }
411
412 // post surfaces (if needed)
413 handlePageFlip();
414
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700415 if (mDirtyRegion.isEmpty()) {
416 // nothing new to do.
417 return true;
418 }
419
Glenn Kasten99ed2242011-12-15 09:51:17 -0800420 if (CC_UNLIKELY(mHwWorkListDirty)) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700421 // build the h/w work list
422 handleWorkList();
423 }
424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Glenn Kasten99ed2242011-12-15 09:51:17 -0800426 if (CC_LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 // repaint the framebuffer (if needed)
428 handleRepaint();
Mathias Agopian74faca22009-09-17 16:18:16 -0700429 // inform the h/w that we're done compositing
430 hw.compositionComplete();
Antti Hatala8392b502010-09-08 06:37:14 -0700431 postFramebuffer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 } else {
433 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800434 hw.compositionComplete();
Mathias Agopian82d7ab62012-01-19 18:34:40 -0800435 hw.waitForRefresh();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 }
437 return true;
438}
439
440void SurfaceFlinger::postFramebuffer()
441{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700442 // this should never happen. we do the flip anyways so we don't
443 // risk to cause a deadlock with hwc
Steve Block32397c12012-01-05 23:22:43 +0000444 ALOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700445 const DisplayHardware& hw(graphicPlane(0).displayHardware());
446 const nsecs_t now = systemTime();
447 mDebugInSwapBuffers = now;
448 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800449
450 size_t numLayers = mVisibleLayersSortedByZ.size();
451 for (size_t i = 0; i < numLayers; i++) {
452 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
453 }
454
Mathias Agopiana44b0412011-10-16 18:46:35 -0700455 mLastSwapBufferTime = systemTime() - now;
456 mDebugInSwapBuffers = 0;
457 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458}
459
460void SurfaceFlinger::handleConsoleEvents()
461{
462 // something to do with the console
463 const DisplayHardware& hw = graphicPlane(0).displayHardware();
464
465 int what = android_atomic_and(0, &mConsoleSignals);
466 if (what & eConsoleAcquired) {
467 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700468 // this is a temporary work-around, eventually this should be called
469 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700470 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 }
472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700474 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 }
477 }
478
479 mDirtyRegion.set(hw.bounds());
480}
481
482void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
483{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700484 Mutex::Autolock _l(mStateLock);
485 const nsecs_t now = systemTime();
486 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487
Mathias Agopianca4d3602011-05-19 15:38:14 -0700488 // Here we're guaranteed that some transaction flags are set
489 // so we can call handleTransactionLocked() unconditionally.
490 // We call getTransactionFlags(), which will also clear the flags,
491 // with mStateLock held to guarantee that mCurrentState won't change
492 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700493
Mathias Agopianca4d3602011-05-19 15:38:14 -0700494 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
495 transactionFlags = getTransactionFlags(mask);
496 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700497
Mathias Agopianca4d3602011-05-19 15:38:14 -0700498 mLastTransactionTime = systemTime() - now;
499 mDebugInTransaction = 0;
500 invalidateHwcGeometry();
501 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700502}
503
Mathias Agopianca4d3602011-05-19 15:38:14 -0700504void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700505{
506 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 const size_t count = currentLayers.size();
508
509 /*
510 * Traversal of the children
511 * (perform the transaction for each of them if needed)
512 */
513
514 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
515 if (layersNeedTransaction) {
516 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700517 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
519 if (!trFlags) continue;
520
521 const uint32_t flags = layer->doTransaction(0);
522 if (flags & Layer::eVisibleRegion)
523 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 }
525 }
526
527 /*
528 * Perform our own transaction if needed
529 */
530
531 if (transactionFlags & eTransactionNeeded) {
532 if (mCurrentState.orientation != mDrawingState.orientation) {
533 // the orientation has changed, recompute all visible regions
534 // and invalidate everything.
535
536 const int dpy = 0;
537 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700538 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 GraphicPlane& plane(graphicPlane(dpy));
540 plane.setOrientation(orientation);
541
542 // update the shared control block
543 const DisplayHardware& hw(plane.displayHardware());
544 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
545 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800546 dcblk->w = plane.getWidth();
547 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548
549 mVisibleRegionsDirty = true;
550 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 }
552
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700553 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
554 // layers have been added
555 mVisibleRegionsDirty = true;
556 }
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 // some layers might have been removed, so
559 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700561 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700563 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700564 const size_t count = previousLayers.size();
565 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700566 const sp<LayerBase>& layer(previousLayers[i]);
567 if (currentLayers.indexOf( layer ) < 0) {
568 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700569 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700570 }
571 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 }
574
575 commitTransaction();
576}
577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800579 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580{
581 const GraphicPlane& plane(graphicPlane(0));
582 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700583 const DisplayHardware& hw(plane.displayHardware());
584 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585
586 Region aboveOpaqueLayers;
587 Region aboveCoveredLayers;
588 Region dirty;
589
590 bool secureFrameBuffer = false;
591
592 size_t i = currentLayers.size();
593 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700594 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 layer->validateVisibility(planeTransform);
596
597 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700598 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599
Mathias Agopianab028732010-03-16 16:41:46 -0700600 /*
601 * opaqueRegion: area of a surface that is fully opaque.
602 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700604
605 /*
606 * visibleRegion: area of a surface that is visible on screen
607 * and not fully transparent. This is essentially the layer's
608 * footprint minus the opaque regions above it.
609 * Areas covered by a translucent surface are considered visible.
610 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700612
613 /*
614 * coveredRegion: area of a surface that is covered by all
615 * visible regions above it (which includes the translucent areas).
616 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700618
619
620 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800621 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700622 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700623 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700625 visibleRegion.andSelf(screenRegion);
626 if (!visibleRegion.isEmpty()) {
627 // Remove the transparent area from the visible region
628 if (translucent) {
629 visibleRegion.subtractSelf(layer->transparentRegionScreen);
630 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631
Mathias Agopianab028732010-03-16 16:41:46 -0700632 // compute the opaque region
633 const int32_t layerOrientation = layer->getOrientation();
634 if (s.alpha==255 && !translucent &&
635 ((layerOrientation & Transform::ROT_INVALID) == false)) {
636 // the opaque region is the layer's footprint
637 opaqueRegion = visibleRegion;
638 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 }
640 }
641
Mathias Agopianab028732010-03-16 16:41:46 -0700642 // Clip the covered region to the visible region
643 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
644
645 // Update aboveCoveredLayers for next (lower) layer
646 aboveCoveredLayers.orSelf(visibleRegion);
647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 // subtract the opaque region covered by the layers above us
649 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
651 // compute this layer's dirty region
652 if (layer->contentDirty) {
653 // we need to invalidate the whole region
654 dirty = visibleRegion;
655 // as well, as the old visible region
656 dirty.orSelf(layer->visibleRegionScreen);
657 layer->contentDirty = false;
658 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700659 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700660 * the exposed region consists of two components:
661 * 1) what's VISIBLE now and was COVERED before
662 * 2) what's EXPOSED now less what was EXPOSED before
663 *
664 * note that (1) is conservative, we start with the whole
665 * visible region but only keep what used to be covered by
666 * something -- which mean it may have been exposed.
667 *
668 * (2) handles areas that were not covered by anything but got
669 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700670 */
Mathias Agopianab028732010-03-16 16:41:46 -0700671 const Region newExposed = visibleRegion - coveredRegion;
672 const Region oldVisibleRegion = layer->visibleRegionScreen;
673 const Region oldCoveredRegion = layer->coveredRegionScreen;
674 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
675 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 }
677 dirty.subtractSelf(aboveOpaqueLayers);
678
679 // accumulate to the screen dirty region
680 dirtyRegion.orSelf(dirty);
681
Mathias Agopianab028732010-03-16 16:41:46 -0700682 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 // Store the visible region is screen space
686 layer->setVisibleRegion(visibleRegion);
687 layer->setCoveredRegion(coveredRegion);
688
Mathias Agopian97011222009-07-28 10:57:27 -0700689 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 if (layer->isSecure() && !visibleRegion.isEmpty()) {
691 secureFrameBuffer = true;
692 }
693 }
694
Mathias Agopian97011222009-07-28 10:57:27 -0700695 // invalidate the areas where a layer was removed
696 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
697 mDirtyRegionRemovedLayer.clear();
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 mSecureFrameBuffer = secureFrameBuffer;
700 opaqueRegion = aboveOpaqueLayers;
701}
702
703
704void SurfaceFlinger::commitTransaction()
705{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800706 if (!mLayersPendingRemoval.isEmpty()) {
707 // Notify removed layers now that they can't be drawn from
708 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
709 mLayersPendingRemoval[i]->onRemoved();
710 }
711 mLayersPendingRemoval.clear();
712 }
713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700715 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700716 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717}
718
719void SurfaceFlinger::handlePageFlip()
720{
721 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800722 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 visibleRegions |= lockPageFlip(currentLayers);
724
725 const DisplayHardware& hw = graphicPlane(0).displayHardware();
726 const Region screenRegion(hw.bounds());
727 if (visibleRegions) {
728 Region opaqueRegion;
729 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700730
731 /*
732 * rebuild the visible layer list
733 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800734 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700735 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700736 mVisibleLayersSortedByZ.setCapacity(count);
737 for (size_t i=0 ; i<count ; i++) {
738 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
739 mVisibleLayersSortedByZ.add(currentLayers[i]);
740 }
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 mWormholeRegion = screenRegion.subtract(opaqueRegion);
743 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800744 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 }
746
747 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700748
749 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 mDirtyRegion.andSelf(screenRegion);
751}
752
Mathias Agopianad456f92011-01-13 17:53:01 -0800753void SurfaceFlinger::invalidateHwcGeometry()
754{
755 mHwWorkListDirty = true;
756}
757
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
759{
760 bool recomputeVisibleRegions = false;
761 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700762 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700764 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 layer->lockPageFlip(recomputeVisibleRegions);
766 }
767 return recomputeVisibleRegions;
768}
769
770void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
771{
772 const GraphicPlane& plane(graphicPlane(0));
773 const Transform& planeTransform(plane.transform());
774 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700775 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700777 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 layer->unlockPageFlip(planeTransform, mDirtyRegion);
779 }
780}
781
Mathias Agopiana350ff92010-08-10 17:14:02 -0700782void SurfaceFlinger::handleWorkList()
783{
784 mHwWorkListDirty = false;
785 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
786 if (hwc.initCheck() == NO_ERROR) {
787 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
788 const size_t count = currentLayers.size();
789 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700790 hwc_layer_t* const cur(hwc.getLayers());
791 for (size_t i=0 ; cur && i<count ; i++) {
792 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700793 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700794 cur[i].compositionType = HWC_FRAMEBUFFER;
795 cur[i].flags |= HWC_SKIP_LAYER;
796 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700797 }
798 }
799}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801void SurfaceFlinger::handleRepaint()
802{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700803 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700804 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805
Glenn Kasten99ed2242011-12-15 09:51:17 -0800806 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 debugFlashRegions();
808 }
809
Mathias Agopianb8a55602009-06-26 19:06:36 -0700810 // set the frame buffer
811 const DisplayHardware& hw(graphicPlane(0).displayHardware());
812 glMatrixMode(GL_MODELVIEW);
813 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814
815 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700816 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
817 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700818 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700819 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
820 // takes a rectangle, we must make sure to update that whole
821 // rectangle in that case
822 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700823 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700824 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700825 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700827 // in the BUFFER_PRESERVED case, obviously, we can update only
828 // what's needed and nothing more.
829 // NOTE: this is NOT a common case, as preserving the backbuffer
830 // is costly and usually involves copying the whole update back.
831 }
832 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700833 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700834 // We need to redraw the rectangle that will be updated
835 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700836 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700837 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700838 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700839 } else {
840 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700842 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 }
844 }
845
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700846 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 composeSurfaces(mDirtyRegion);
848
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700849 // update the swap region and clear the dirty region
850 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 mDirtyRegion.clear();
852}
853
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700854void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700856 const DisplayHardware& hw(graphicPlane(0).displayHardware());
857 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700858 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700859 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700860 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700861 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700862
Mathias Agopianf384cc32011-09-08 18:31:55 -0700863 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
864 size_t count = layers.size();
865
Steve Blocke6f43dd2012-01-06 19:20:56 +0000866 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700867 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
868 hwc.getNumLayers(), count);
869
870 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700871 if (hwc.initCheck() == NO_ERROR) {
872 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
873 }
Mathias Agopian45721772010-08-12 15:03:26 -0700874
875 /*
876 * update the per-frame h/w composer data for each layer
877 * and build the transparent region of the FB
878 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700879 for (size_t i=0 ; i<count ; i++) {
880 const sp<LayerBase>& layer(layers[i]);
881 layer->setPerFrameData(&cur[i]);
882 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700883 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700884 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000885 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700886
Mathias Agopianf384cc32011-09-08 18:31:55 -0700887 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700888 // what's happening here is tricky.
889 // we want to clear all the layers with the CLEAR_FB flags
890 // that are opaque.
891 // however, since some GPU are efficient at preserving
892 // the backbuffer, we want to take advantage of that so we do the
893 // clear only in the dirty region (other areas will be preserved
894 // on those GPUs).
895 // NOTE: on non backbuffer preserving GPU, the dirty region
896 // has already been expanded as needed, so the code is correct
897 // there too.
898 //
899 // However, the content of the framebuffer cannot be trusted when
900 // we switch to/from FB/OVERLAY, in which case we need to
901 // expand the dirty region to those areas too.
902 //
903 // Note also that there is a special case when switching from
904 // "no layers in FB" to "some layers in FB", where we need to redraw
905 // the entire FB, since some areas might contain uninitialized
906 // data.
907 //
908 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700909 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700910 // that is, we can't erase anything outside the dirty region.
911
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700912 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700913
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700914 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
915 transparent.set(hw.getBounds());
916 dirtyInOut = transparent;
917 } else {
918 for (size_t i=0 ; i<count ; i++) {
919 const sp<LayerBase>& layer(layers[i]);
920 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
921 transparent.orSelf(layer->visibleRegionScreen);
922 }
923 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
924 if (isOverlay != layer->isOverlay()) {
925 // we transitioned to/from overlay, so add this layer
926 // to the dirty region so the framebuffer can be either
927 // cleared or redrawn.
928 dirtyInOut.orSelf(layer->visibleRegionScreen);
929 }
930 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800931 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700932 // don't erase stuff outside the dirty region
933 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700934 }
935
936 /*
937 * clear the area of the FB that need to be transparent
938 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700939 if (!transparent.isEmpty()) {
940 glClearColor(0,0,0,0);
941 Region::const_iterator it = transparent.begin();
942 Region::const_iterator const end = transparent.end();
943 const int32_t height = hw.getHeight();
944 while (it != end) {
945 const Rect& r(*it++);
946 const GLint sy = height - (r.top + r.height());
947 glScissor(r.left, sy, r.width(), r.height());
948 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800949 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700950 }
951 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700952}
Mathias Agopian45721772010-08-12 15:03:26 -0700953
Mathias Agopianf384cc32011-09-08 18:31:55 -0700954void SurfaceFlinger::composeSurfaces(const Region& dirty)
955{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700956 const DisplayHardware& hw(graphicPlane(0).displayHardware());
957 HWComposer& hwc(hw.getHwComposer());
958
959 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800960 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700961 // should never happen unless the window manager has a bug
962 // draw something...
963 drawWormhole();
964 }
965
Mathias Agopian45721772010-08-12 15:03:26 -0700966 /*
967 * and then, render the layers targeted at the framebuffer
968 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700969 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700970 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
971 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700972 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700973 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700974 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700975 }
976 const sp<LayerBase>& layer(layers[i]);
977 const Region clip(dirty.intersect(layer->visibleRegionScreen));
978 if (!clip.isEmpty()) {
979 layer->draw(clip);
980 }
981 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982}
983
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984void SurfaceFlinger::debugFlashRegions()
985{
Mathias Agopian0a917752010-06-14 21:20:00 -0700986 const DisplayHardware& hw(graphicPlane(0).displayHardware());
987 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700988 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700989 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700990 return;
991 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700992
Mathias Agopian0a917752010-06-14 21:20:00 -0700993 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
994 (flags & DisplayHardware::BUFFER_PRESERVED))) {
995 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
996 mDirtyRegion.bounds() : hw.bounds());
997 composeSurfaces(repaint);
998 }
999
Mathias Agopianc492e672011-10-18 14:49:27 -07001000 glDisable(GL_TEXTURE_EXTERNAL_OES);
1001 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 glDisable(GL_SCISSOR_TEST);
1004
Mathias Agopian0926f502009-05-04 14:17:04 -07001005 static int toggle = 0;
1006 toggle = 1 - toggle;
1007 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001008 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001009 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001010 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001011 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012
Mathias Agopian20f68782009-05-11 00:03:41 -07001013 Region::const_iterator it = mDirtyRegion.begin();
1014 Region::const_iterator const end = mDirtyRegion.end();
1015 while (it != end) {
1016 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001018 { r.left, height - r.top },
1019 { r.left, height - r.bottom },
1020 { r.right, height - r.bottom },
1021 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 };
1023 glVertexPointer(2, GL_FLOAT, 0, vertices);
1024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1025 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001026
Mathias Agopian0656a682011-09-20 17:22:44 -07001027 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028
1029 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001030 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031
1032 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033}
1034
1035void SurfaceFlinger::drawWormhole() const
1036{
1037 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1038 if (region.isEmpty())
1039 return;
1040
1041 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1042 const int32_t width = hw.getWidth();
1043 const int32_t height = hw.getHeight();
1044
Glenn Kasten99ed2242011-12-15 09:51:17 -08001045 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001046 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001047 Region::const_iterator it = region.begin();
1048 Region::const_iterator const end = region.end();
1049 while (it != end) {
1050 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 const GLint sy = height - (r.top + r.height());
1052 glScissor(r.left, sy, r.width(), r.height());
1053 glClear(GL_COLOR_BUFFER_BIT);
1054 }
1055 } else {
1056 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1057 { width, height }, { 0, height } };
1058 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 glVertexPointer(2, GL_SHORT, 0, vertices);
1061 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1062 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001063
1064 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 glEnable(GL_TEXTURE_2D);
1066 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1067 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1068 glMatrixMode(GL_TEXTURE);
1069 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001070
1071 glDisable(GL_BLEND);
1072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001074 Region::const_iterator it = region.begin();
1075 Region::const_iterator const end = region.end();
1076 while (it != end) {
1077 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 const GLint sy = height - (r.top + r.height());
1079 glScissor(r.left, sy, r.width(), r.height());
1080 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1081 }
1082 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001083 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001084 glLoadIdentity();
1085 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 }
1087}
1088
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001089status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090{
1091 Mutex::Autolock _l(mStateLock);
1092 addLayer_l(layer);
1093 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1094 return NO_ERROR;
1095}
1096
Mathias Agopian96f08192010-06-02 23:28:45 -07001097status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1098{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001099 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001100 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1101}
1102
1103ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1104 const sp<LayerBaseClient>& lbc)
1105{
Mathias Agopian96f08192010-06-02 23:28:45 -07001106 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001107 size_t name = client->attachLayer(lbc);
1108
1109 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001110
1111 // add this layer to the current state list
1112 addLayer_l(lbc);
1113
Mathias Agopian4f113742011-05-03 16:21:41 -07001114 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001115}
1116
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001117status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118{
1119 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001120 status_t err = purgatorizeLayer_l(layer);
1121 if (err == NO_ERROR)
1122 setTransactionFlags(eTransactionNeeded);
1123 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124}
1125
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001126status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001128 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1129 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001130 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001131 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1133 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001134 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 return NO_ERROR;
1136 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001137 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138}
1139
Mathias Agopian9a112062009-04-17 19:36:26 -07001140status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1141{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001142 // First add the layer to the purgatory list, which makes sure it won't
1143 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001144 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001145 if (err >= 0) {
1146 mLayerPurgatory.add(layerBase);
1147 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001148
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001149 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001150
Mathias Agopian3d579642009-06-04 18:46:21 -07001151 // it's possible that we don't find a layer, because it might
1152 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001153 // from the user because there is a race between Client::destroySurface(),
1154 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001155 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1156}
1157
Mathias Agopian96f08192010-06-02 23:28:45 -07001158status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159{
Mathias Agopian96f08192010-06-02 23:28:45 -07001160 layer->forceVisibilityTransaction();
1161 setTransactionFlags(eTraversalNeeded);
1162 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163}
1164
Mathias Agopiandea20b12011-05-03 17:04:02 -07001165uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1166{
1167 return android_atomic_release_load(&mTransactionFlags);
1168}
1169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1171{
1172 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1173}
1174
Mathias Agopianbb641242010-05-18 17:06:55 -07001175uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176{
1177 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1178 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001179 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 }
1181 return old;
1182}
1183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001185void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001186 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001187 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001188
Jamie Gennis28378392011-10-12 17:39:00 -07001189 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001190 if (mCurrentState.orientation != orientation) {
1191 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1192 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001193 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001194 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001195 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001196 orientation);
1197 }
1198 }
1199
Mathias Agopian698c0872011-06-28 19:09:31 -07001200 const size_t count = state.size();
1201 for (size_t i=0 ; i<count ; i++) {
1202 const ComposerState& s(state[i]);
1203 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001204 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001205 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001206
Mathias Agopian386aa982011-11-07 21:58:03 -08001207 if (transactionFlags) {
1208 // this triggers the transaction
1209 setTransactionFlags(transactionFlags);
1210
1211 // if this is a synchronous transaction, wait for it to take effect
1212 // before returning.
1213 if (flags & eSynchronous) {
1214 mTransationPending = true;
1215 }
1216 while (mTransationPending) {
1217 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1218 if (CC_UNLIKELY(err != NO_ERROR)) {
1219 // just in case something goes wrong in SF, return to the
1220 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001221 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001222 mTransationPending = false;
1223 break;
1224 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 }
1227}
1228
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001229sp<ISurface> SurfaceFlinger::createSurface(
1230 ISurfaceComposerClient::surface_data_t* params,
1231 const String8& name,
1232 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1234 uint32_t flags)
1235{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001236 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001237 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001238
1239 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001240 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001241 int(w), int(h));
1242 return surfaceHandle;
1243 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001244
Steve Block9d453682011-12-20 16:23:08 +00001245 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001246 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 switch (flags & eFXSurfaceMask) {
1248 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001249 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1250 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 break;
1252 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001253 // for now we treat Blur as Dim, until we can implement it
1254 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001258 case eFXSurfaceScreenshot:
1259 layer = createScreenshotSurface(client, d, w, h, flags);
1260 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 }
1262
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001263 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001264 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001265 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001266 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001269 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001270 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001271 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001272 if (normalLayer != 0) {
1273 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001274 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001275 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001276 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001277
Mathias Agopian96f08192010-06-02 23:28:45 -07001278 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 }
1280
1281 return surfaceHandle;
1282}
1283
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001284sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001285 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001286 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001287 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
1289 // initialize the surfaces
1290 switch (format) { // TODO: take h/w into account
1291 case PIXEL_FORMAT_TRANSPARENT:
1292 case PIXEL_FORMAT_TRANSLUCENT:
1293 format = PIXEL_FORMAT_RGBA_8888;
1294 break;
1295 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001296#ifdef NO_RGBX_8888
1297 format = PIXEL_FORMAT_RGB_565;
1298#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001299 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001300#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 break;
1302 }
1303
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001304#ifdef NO_RGBX_8888
1305 if (format == PIXEL_FORMAT_RGBX_8888)
1306 format = PIXEL_FORMAT_RGBA_8888;
1307#endif
1308
Mathias Agopian96f08192010-06-02 23:28:45 -07001309 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001310 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001311 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001312 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001313 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 }
1315 return layer;
1316}
1317
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001318sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001319 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321{
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001323 return layer;
1324}
1325
1326sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1327 const sp<Client>& client, DisplayID display,
1328 uint32_t w, uint32_t h, uint32_t flags)
1329{
1330 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 return layer;
1332}
1333
Mathias Agopian96f08192010-06-02 23:28:45 -07001334status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335{
Mathias Agopian9a112062009-04-17 19:36:26 -07001336 /*
1337 * called by the window manager, when a surface should be marked for
1338 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001339 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001340 * The surface is removed from the current and drawing lists, but placed
1341 * in the purgatory queue, so it's not destroyed right-away (we need
1342 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001343 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001344
Mathias Agopian48d819a2009-09-10 19:41:18 -07001345 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001346 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001347 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001348 if (layer != 0) {
1349 err = purgatorizeLayer_l(layer);
1350 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001351 setTransactionFlags(eTransactionNeeded);
1352 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001353 }
1354 return err;
1355}
1356
Mathias Agopianca4d3602011-05-19 15:38:14 -07001357status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001358{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001359 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001360 status_t err = NO_ERROR;
1361 sp<LayerBaseClient> l(layer.promote());
1362 if (l != NULL) {
1363 Mutex::Autolock _l(mStateLock);
1364 err = removeLayer_l(l);
1365 if (err == NAME_NOT_FOUND) {
1366 // The surface wasn't in the current list, which means it was
1367 // removed already, which means it is in the purgatory,
1368 // and need to be removed from there.
1369 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001370 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001371 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001372 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001373 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001374 "error removing layer=%p (%s)", l.get(), strerror(-err));
1375 }
1376 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377}
1378
Mathias Agopian698c0872011-06-28 19:09:31 -07001379uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001380 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001381 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001384 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1385 if (layer != 0) {
1386 const uint32_t what = s.what;
1387 if (what & ePositionChanged) {
1388 if (layer->setPosition(s.x, s.y))
1389 flags |= eTraversalNeeded;
1390 }
1391 if (what & eLayerChanged) {
1392 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1393 if (layer->setLayer(s.z)) {
1394 mCurrentState.layersSortedByZ.removeAt(idx);
1395 mCurrentState.layersSortedByZ.add(layer);
1396 // we need traversal (state changed)
1397 // AND transaction (list changed)
1398 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 }
1400 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001401 if (what & eSizeChanged) {
1402 if (layer->setSize(s.w, s.h)) {
1403 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001404 }
1405 }
1406 if (what & eAlphaChanged) {
1407 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1408 flags |= eTraversalNeeded;
1409 }
1410 if (what & eMatrixChanged) {
1411 if (layer->setMatrix(s.matrix))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eTransparentRegionChanged) {
1415 if (layer->setTransparentRegionHint(s.transparentRegion))
1416 flags |= eTraversalNeeded;
1417 }
1418 if (what & eVisibilityChanged) {
1419 if (layer->setFlags(s.flags, s.mask))
1420 flags |= eTraversalNeeded;
1421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001423 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424}
1425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426void SurfaceFlinger::screenReleased(int dpy)
1427{
1428 // this may be called by a signal handler, we can't do too much in here
1429 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1430 signalEvent();
1431}
1432
1433void SurfaceFlinger::screenAcquired(int dpy)
1434{
1435 // this may be called by a signal handler, we can't do too much in here
1436 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1437 signalEvent();
1438}
1439
1440status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1441{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001442 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 char buffer[SIZE];
1444 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001445
1446 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 snprintf(buffer, SIZE, "Permission Denial: "
1448 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1449 IPCThreadState::self()->getCallingPid(),
1450 IPCThreadState::self()->getCallingUid());
1451 result.append(buffer);
1452 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001453 // Try to get the main lock, but don't insist if we can't
1454 // (this would indicate SF is stuck, but we want to be able to
1455 // print something in dumpsys).
1456 int retry = 3;
1457 while (mStateLock.tryLock()<0 && --retry>=0) {
1458 usleep(1000000);
1459 }
1460 const bool locked(retry >= 0);
1461 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001462 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001463 "SurfaceFlinger appears to be unresponsive, "
1464 "dumping anyways (no locks held)\n");
1465 result.append(buffer);
1466 }
1467
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001468 bool dumpAll = true;
1469 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001470 size_t numArgs = args.size();
1471 if (numArgs) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001472 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001473
1474 if ((index < numArgs) &&
1475 (args[index] == String16("--list"))) {
1476 index++;
1477 listLayersLocked(args, index, result, buffer, SIZE);
1478 }
1479
1480 if ((index < numArgs) &&
1481 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001482 index++;
1483 dumpStatsLocked(args, index, result, buffer, SIZE);
1484 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001485
1486 if ((index < numArgs) &&
1487 (args[index] == String16("--latency-clear"))) {
1488 index++;
1489 clearStatsLocked(args, index, result, buffer, SIZE);
1490 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001492
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001493 if (dumpAll) {
1494 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001495 }
1496
Mathias Agopian9795c422009-08-26 16:36:26 -07001497 if (locked) {
1498 mStateLock.unlock();
1499 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 }
1501 write(fd, result.string(), result.size());
1502 return NO_ERROR;
1503}
1504
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001505void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1506 String8& result, char* buffer, size_t SIZE) const
1507{
1508 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1509 const size_t count = currentLayers.size();
1510 for (size_t i=0 ; i<count ; i++) {
1511 const sp<LayerBase>& layer(currentLayers[i]);
1512 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1513 result.append(buffer);
1514 }
1515}
1516
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001517void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1518 String8& result, char* buffer, size_t SIZE) const
1519{
1520 String8 name;
1521 if (index < args.size()) {
1522 name = String8(args[index]);
1523 index++;
1524 }
1525
1526 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1527 const size_t count = currentLayers.size();
1528 for (size_t i=0 ; i<count ; i++) {
1529 const sp<LayerBase>& layer(currentLayers[i]);
1530 if (name.isEmpty()) {
1531 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1532 result.append(buffer);
1533 }
1534 if (name.isEmpty() || (name == layer->getName())) {
1535 layer->dumpStats(result, buffer, SIZE);
1536 }
1537 }
1538}
1539
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001540void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1541 String8& result, char* buffer, size_t SIZE) const
1542{
1543 String8 name;
1544 if (index < args.size()) {
1545 name = String8(args[index]);
1546 index++;
1547 }
1548
1549 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1550 const size_t count = currentLayers.size();
1551 for (size_t i=0 ; i<count ; i++) {
1552 const sp<LayerBase>& layer(currentLayers[i]);
1553 if (name.isEmpty() || (name == layer->getName())) {
1554 layer->clearStats();
1555 }
1556 }
1557}
1558
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001559void SurfaceFlinger::dumpAllLocked(
1560 String8& result, char* buffer, size_t SIZE) const
1561{
1562 // figure out if we're stuck somewhere
1563 const nsecs_t now = systemTime();
1564 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1565 const nsecs_t inTransaction(mDebugInTransaction);
1566 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1567 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1568
1569 /*
1570 * Dump the visible layer list
1571 */
1572 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1573 const size_t count = currentLayers.size();
1574 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1575 result.append(buffer);
1576 for (size_t i=0 ; i<count ; i++) {
1577 const sp<LayerBase>& layer(currentLayers[i]);
1578 layer->dump(result, buffer, SIZE);
1579 }
1580
1581 /*
1582 * Dump the layers in the purgatory
1583 */
1584
1585 const size_t purgatorySize = mLayerPurgatory.size();
1586 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1587 result.append(buffer);
1588 for (size_t i=0 ; i<purgatorySize ; i++) {
1589 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1590 layer->shortDump(result, buffer, SIZE);
1591 }
1592
1593 /*
1594 * Dump SurfaceFlinger global state
1595 */
1596
1597 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1598 result.append(buffer);
1599
1600 const GLExtensions& extensions(GLExtensions::getInstance());
1601 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1602 extensions.getVendor(),
1603 extensions.getRenderer(),
1604 extensions.getVersion());
1605 result.append(buffer);
1606
1607 snprintf(buffer, SIZE, "EGL : %s\n",
1608 eglQueryString(graphicPlane(0).getEGLDisplay(),
1609 EGL_VERSION_HW_ANDROID));
1610 result.append(buffer);
1611
1612 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1613 result.append(buffer);
1614
1615 mWormholeRegion.dump(result, "WormholeRegion");
1616 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1617 snprintf(buffer, SIZE,
1618 " orientation=%d, canDraw=%d\n",
1619 mCurrentState.orientation, hw.canDraw());
1620 result.append(buffer);
1621 snprintf(buffer, SIZE,
1622 " last eglSwapBuffers() time: %f us\n"
1623 " last transaction time : %f us\n"
1624 " refresh-rate : %f fps\n"
1625 " x-dpi : %f\n"
1626 " y-dpi : %f\n",
1627 mLastSwapBufferTime/1000.0,
1628 mLastTransactionTime/1000.0,
1629 hw.getRefreshRate(),
1630 hw.getDpiX(),
1631 hw.getDpiY());
1632 result.append(buffer);
1633
1634 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1635 inSwapBuffersDuration/1000.0);
1636 result.append(buffer);
1637
1638 snprintf(buffer, SIZE, " transaction time: %f us\n",
1639 inTransactionDuration/1000.0);
1640 result.append(buffer);
1641
1642 /*
1643 * VSYNC state
1644 */
1645 mEventThread->dump(result, buffer, SIZE);
1646
1647 /*
1648 * Dump HWComposer state
1649 */
1650 HWComposer& hwc(hw.getHwComposer());
1651 snprintf(buffer, SIZE, "h/w composer state:\n");
1652 result.append(buffer);
1653 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1654 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1655 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1656 result.append(buffer);
1657 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1658
1659 /*
1660 * Dump gralloc state
1661 */
1662 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1663 alloc.dump(result);
1664 hw.dump(result);
1665}
1666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667status_t SurfaceFlinger::onTransact(
1668 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1669{
1670 switch (code) {
1671 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001672 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001675 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001676 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 {
1678 // codes that require permission check
1679 IPCThreadState* ipc = IPCThreadState::self();
1680 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001681 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001682 if ((uid != AID_GRAPHICS) &&
1683 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001684 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001685 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1686 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001688 break;
1689 }
1690 case CAPTURE_SCREEN:
1691 {
1692 // codes that require permission check
1693 IPCThreadState* ipc = IPCThreadState::self();
1694 const int pid = ipc->getCallingPid();
1695 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001696 if ((uid != AID_GRAPHICS) &&
1697 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001698 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001699 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1700 return PERMISSION_DENIED;
1701 }
1702 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 }
1704 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1707 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001708 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001709 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001710 IPCThreadState* ipc = IPCThreadState::self();
1711 const int pid = ipc->getCallingPid();
1712 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001713 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001714 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 return PERMISSION_DENIED;
1716 }
1717 int n;
1718 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001719 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001720 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 return NO_ERROR;
1722 case 1002: // SHOW_UPDATES
1723 n = data.readInt32();
1724 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001725 invalidateHwcGeometry();
1726 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 return NO_ERROR;
1728 case 1003: // SHOW_BACKGROUND
1729 n = data.readInt32();
1730 mDebugBackground = n ? 1 : 0;
1731 return NO_ERROR;
1732 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001733 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001734 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001736 case 1005:{ // force transaction
1737 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1738 return NO_ERROR;
1739 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001740 case 1008: // toggle use of hw composer
1741 n = data.readInt32();
1742 mDebugDisableHWC = n ? 1 : 0;
1743 invalidateHwcGeometry();
1744 repaintEverything();
1745 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001746 case 1009: // toggle use of transform hint
1747 n = data.readInt32();
1748 mDebugDisableTransformHint = n ? 1 : 0;
1749 invalidateHwcGeometry();
1750 repaintEverything();
1751 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001753 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 reply->writeInt32(0);
1755 reply->writeInt32(mDebugRegion);
1756 reply->writeInt32(mDebugBackground);
1757 return NO_ERROR;
1758 case 1013: {
1759 Mutex::Autolock _l(mStateLock);
1760 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1761 reply->writeInt32(hw.getPageFlipCount());
1762 }
1763 return NO_ERROR;
1764 }
1765 }
1766 return err;
1767}
1768
Mathias Agopian53331da2011-08-22 21:44:41 -07001769void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001770 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001771 const Rect bounds(hw.getBounds());
1772 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001773 signalEvent();
1774}
1775
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001776void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1777 Mutex::Autolock _l(mInvalidateLock);
1778 mInvalidateRegion = reg;
1779}
1780
1781Region SurfaceFlinger::getAndClearInvalidateRegion() {
1782 Mutex::Autolock _l(mInvalidateLock);
1783 Region reg(mInvalidateRegion);
1784 mInvalidateRegion.clear();
1785 return reg;
1786}
1787
Mathias Agopian59119e62010-10-11 12:37:43 -07001788// ---------------------------------------------------------------------------
1789
Mathias Agopian118d0242011-10-13 16:02:48 -07001790status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1791 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1792{
1793 Mutex::Autolock _l(mStateLock);
1794 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1795}
1796
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001797status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1798 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001799{
Mathias Agopian59119e62010-10-11 12:37:43 -07001800 if (!GLExtensions::getInstance().haveFramebufferObject())
1801 return INVALID_OPERATION;
1802
1803 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001804 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001805 const uint32_t hw_w = hw.getWidth();
1806 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001807 GLfloat u = 1;
1808 GLfloat v = 1;
1809
1810 // make sure to clear all GL error flags
1811 while ( glGetError() != GL_NO_ERROR ) ;
1812
1813 // create a FBO
1814 GLuint name, tname;
1815 glGenTextures(1, &tname);
1816 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001817 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1818 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001819 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001820 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001821 GLint tw = (2 << (31 - clz(hw_w)));
1822 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001823 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1824 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001825 u = GLfloat(hw_w) / tw;
1826 v = GLfloat(hw_h) / th;
1827 }
1828 glGenFramebuffersOES(1, &name);
1829 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001830 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1831 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001832
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001833 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001834 glDisable(GL_TEXTURE_EXTERNAL_OES);
1835 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001836 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001837 glClearColor(0,0,0,1);
1838 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001839 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001840 glMatrixMode(GL_MODELVIEW);
1841 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1843 const size_t count = layers.size();
1844 for (size_t i=0 ; i<count ; ++i) {
1845 const sp<LayerBase>& layer(layers[i]);
1846 layer->drawForSreenShot();
1847 }
1848
Mathias Agopian118d0242011-10-13 16:02:48 -07001849 hw.compositionComplete();
1850
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001851 // back to main framebuffer
1852 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1853 glDisable(GL_SCISSOR_TEST);
1854 glDeleteFramebuffersOES(1, &name);
1855
1856 *textureName = tname;
1857 *uOut = u;
1858 *vOut = v;
1859 return NO_ERROR;
1860}
1861
1862// ---------------------------------------------------------------------------
1863
1864status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1865{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001866 // get screen geometry
1867 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1868 const uint32_t hw_w = hw.getWidth();
1869 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001870 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001871
1872 GLfloat u, v;
1873 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001874 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001875 if (result != NO_ERROR) {
1876 return result;
1877 }
1878
1879 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001880 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001881 glBindTexture(GL_TEXTURE_2D, tname);
1882 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1883 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1884 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1885 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1886 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1887 glVertexPointer(2, GL_FLOAT, 0, vtx);
1888
Mathias Agopianffcf4652011-07-07 17:30:31 -07001889 /*
1890 * Texture coordinate mapping
1891 *
1892 * u
1893 * 1 +----------+---+
1894 * | | | | image is inverted
1895 * | V | | w.r.t. the texture
1896 * 1-v +----------+ | coordinates
1897 * | |
1898 * | |
1899 * | |
1900 * 0 +--------------+
1901 * 0 1
1902 *
1903 */
1904
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001905 class s_curve_interpolator {
1906 const float nbFrames, s, v;
1907 public:
1908 s_curve_interpolator(int nbFrames, float s)
1909 : nbFrames(1.0f / (nbFrames-1)), s(s),
1910 v(1.0f + expf(-s + 0.5f*s)) {
1911 }
1912 float operator()(int f) {
1913 const float x = f * nbFrames;
1914 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1915 }
1916 };
1917
1918 class v_stretch {
1919 const GLfloat hw_w, hw_h;
1920 public:
1921 v_stretch(uint32_t hw_w, uint32_t hw_h)
1922 : hw_w(hw_w), hw_h(hw_h) {
1923 }
1924 void operator()(GLfloat* vtx, float v) {
1925 const GLfloat w = hw_w + (hw_w * v);
1926 const GLfloat h = hw_h - (hw_h * v);
1927 const GLfloat x = (hw_w - w) * 0.5f;
1928 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001929 vtx[0] = x; vtx[1] = y;
1930 vtx[2] = x; vtx[3] = y + h;
1931 vtx[4] = x + w; vtx[5] = y + h;
1932 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001933 }
1934 };
1935
1936 class h_stretch {
1937 const GLfloat hw_w, hw_h;
1938 public:
1939 h_stretch(uint32_t hw_w, uint32_t hw_h)
1940 : hw_w(hw_w), hw_h(hw_h) {
1941 }
1942 void operator()(GLfloat* vtx, float v) {
1943 const GLfloat w = hw_w - (hw_w * v);
1944 const GLfloat h = 1.0f;
1945 const GLfloat x = (hw_w - w) * 0.5f;
1946 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001947 vtx[0] = x; vtx[1] = y;
1948 vtx[2] = x; vtx[3] = y + h;
1949 vtx[4] = x + w; vtx[5] = y + h;
1950 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001951 }
1952 };
1953
1954 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001955 char value[PROPERTY_VALUE_MAX];
1956 property_get("debug.sf.electron_frames", value, "24");
1957 int nbFrames = (atoi(value) + 1) >> 1;
1958 if (nbFrames <= 0) // just in case
1959 nbFrames = 24;
1960
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001961 s_curve_interpolator itr(nbFrames, 7.5f);
1962 s_curve_interpolator itg(nbFrames, 8.0f);
1963 s_curve_interpolator itb(nbFrames, 8.5f);
1964
1965 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001966
1967 glMatrixMode(GL_TEXTURE);
1968 glLoadIdentity();
1969 glMatrixMode(GL_MODELVIEW);
1970 glLoadIdentity();
1971
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 glEnable(GL_BLEND);
1973 glBlendFunc(GL_ONE, GL_ONE);
1974 for (int i=0 ; i<nbFrames ; i++) {
1975 float x, y, w, h;
1976 const float vr = itr(i);
1977 const float vg = itg(i);
1978 const float vb = itb(i);
1979
1980 // clear screen
1981 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001982 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001983 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001984
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 // draw the red plane
1986 vverts(vtx, vr);
1987 glColorMask(1,0,0,1);
1988 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001989
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 // draw the green plane
1991 vverts(vtx, vg);
1992 glColorMask(0,1,0,1);
1993 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001994
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001995 // draw the blue plane
1996 vverts(vtx, vb);
1997 glColorMask(0,0,1,1);
1998 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001999
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002000 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002001 glDisable(GL_TEXTURE_2D);
2002 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002003 glColor4f(vg, vg, vg, 1);
2004 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2005 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002006 }
2007
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002008 h_stretch hverts(hw_w, hw_h);
2009 glDisable(GL_BLEND);
2010 glDisable(GL_TEXTURE_2D);
2011 glColorMask(1,1,1,1);
2012 for (int i=0 ; i<nbFrames ; i++) {
2013 const float v = itg(i);
2014 hverts(vtx, v);
2015 glClear(GL_COLOR_BUFFER_BIT);
2016 glColor4f(1-v, 1-v, 1-v, 1);
2017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2018 hw.flip(screenBounds);
2019 }
2020
2021 glColorMask(1,1,1,1);
2022 glEnable(GL_SCISSOR_TEST);
2023 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2024 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002025 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002026 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002027 return NO_ERROR;
2028}
2029
2030status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2031{
2032 status_t result = PERMISSION_DENIED;
2033
2034 if (!GLExtensions::getInstance().haveFramebufferObject())
2035 return INVALID_OPERATION;
2036
2037
2038 // get screen geometry
2039 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2040 const uint32_t hw_w = hw.getWidth();
2041 const uint32_t hw_h = hw.getHeight();
2042 const Region screenBounds(hw.bounds());
2043
2044 GLfloat u, v;
2045 GLuint tname;
2046 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2047 if (result != NO_ERROR) {
2048 return result;
2049 }
2050
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 GLfloat vtx[8];
2052 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053 glBindTexture(GL_TEXTURE_2D, tname);
2054 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2055 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2056 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2057 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2058 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2059 glVertexPointer(2, GL_FLOAT, 0, vtx);
2060
2061 class s_curve_interpolator {
2062 const float nbFrames, s, v;
2063 public:
2064 s_curve_interpolator(int nbFrames, float s)
2065 : nbFrames(1.0f / (nbFrames-1)), s(s),
2066 v(1.0f + expf(-s + 0.5f*s)) {
2067 }
2068 float operator()(int f) {
2069 const float x = f * nbFrames;
2070 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2071 }
2072 };
2073
2074 class v_stretch {
2075 const GLfloat hw_w, hw_h;
2076 public:
2077 v_stretch(uint32_t hw_w, uint32_t hw_h)
2078 : hw_w(hw_w), hw_h(hw_h) {
2079 }
2080 void operator()(GLfloat* vtx, float v) {
2081 const GLfloat w = hw_w + (hw_w * v);
2082 const GLfloat h = hw_h - (hw_h * v);
2083 const GLfloat x = (hw_w - w) * 0.5f;
2084 const GLfloat y = (hw_h - h) * 0.5f;
2085 vtx[0] = x; vtx[1] = y;
2086 vtx[2] = x; vtx[3] = y + h;
2087 vtx[4] = x + w; vtx[5] = y + h;
2088 vtx[6] = x + w; vtx[7] = y;
2089 }
2090 };
2091
2092 class h_stretch {
2093 const GLfloat hw_w, hw_h;
2094 public:
2095 h_stretch(uint32_t hw_w, uint32_t hw_h)
2096 : hw_w(hw_w), hw_h(hw_h) {
2097 }
2098 void operator()(GLfloat* vtx, float v) {
2099 const GLfloat w = hw_w - (hw_w * v);
2100 const GLfloat h = 1.0f;
2101 const GLfloat x = (hw_w - w) * 0.5f;
2102 const GLfloat y = (hw_h - h) * 0.5f;
2103 vtx[0] = x; vtx[1] = y;
2104 vtx[2] = x; vtx[3] = y + h;
2105 vtx[4] = x + w; vtx[5] = y + h;
2106 vtx[6] = x + w; vtx[7] = y;
2107 }
2108 };
2109
Mathias Agopiana6546e52010-10-14 12:33:07 -07002110 // the full animation is 12 frames
2111 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002112 s_curve_interpolator itr(nbFrames, 7.5f);
2113 s_curve_interpolator itg(nbFrames, 8.0f);
2114 s_curve_interpolator itb(nbFrames, 8.5f);
2115
2116 h_stretch hverts(hw_w, hw_h);
2117 glDisable(GL_BLEND);
2118 glDisable(GL_TEXTURE_2D);
2119 glColorMask(1,1,1,1);
2120 for (int i=nbFrames-1 ; i>=0 ; i--) {
2121 const float v = itg(i);
2122 hverts(vtx, v);
2123 glClear(GL_COLOR_BUFFER_BIT);
2124 glColor4f(1-v, 1-v, 1-v, 1);
2125 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2126 hw.flip(screenBounds);
2127 }
2128
Mathias Agopiana6546e52010-10-14 12:33:07 -07002129 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002130 v_stretch vverts(hw_w, hw_h);
2131 glEnable(GL_BLEND);
2132 glBlendFunc(GL_ONE, GL_ONE);
2133 for (int i=nbFrames-1 ; i>=0 ; i--) {
2134 float x, y, w, h;
2135 const float vr = itr(i);
2136 const float vg = itg(i);
2137 const float vb = itb(i);
2138
2139 // clear screen
2140 glColorMask(1,1,1,1);
2141 glClear(GL_COLOR_BUFFER_BIT);
2142 glEnable(GL_TEXTURE_2D);
2143
2144 // draw the red plane
2145 vverts(vtx, vr);
2146 glColorMask(1,0,0,1);
2147 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2148
2149 // draw the green plane
2150 vverts(vtx, vg);
2151 glColorMask(0,1,0,1);
2152 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2153
2154 // draw the blue plane
2155 vverts(vtx, vb);
2156 glColorMask(0,0,1,1);
2157 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2158
2159 hw.flip(screenBounds);
2160 }
2161
2162 glColorMask(1,1,1,1);
2163 glEnable(GL_SCISSOR_TEST);
2164 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002165 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002166 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002167 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002168
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169 return NO_ERROR;
2170}
2171
2172// ---------------------------------------------------------------------------
2173
Mathias Agopianabd671a2010-10-14 14:54:06 -07002174status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002175{
2176 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2177 if (!hw.canDraw()) {
2178 // we're already off
2179 return NO_ERROR;
2180 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002181
2182 // turn off hwc while we're doing the animation
2183 hw.getHwComposer().disable();
2184 // and make sure to turn it back on (if needed) next time we compose
2185 invalidateHwcGeometry();
2186
Mathias Agopianabd671a2010-10-14 14:54:06 -07002187 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2188 electronBeamOffAnimationImplLocked();
2189 }
2190
2191 // always clear the whole screen at the end of the animation
2192 glClearColor(0,0,0,1);
2193 glDisable(GL_SCISSOR_TEST);
2194 glClear(GL_COLOR_BUFFER_BIT);
2195 glEnable(GL_SCISSOR_TEST);
2196 hw.flip( Region(hw.bounds()) );
2197
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002198 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002199}
2200
2201status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2202{
Mathias Agopian59119e62010-10-11 12:37:43 -07002203 class MessageTurnElectronBeamOff : public MessageBase {
2204 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002205 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002206 status_t result;
2207 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002208 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2209 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002210 }
2211 status_t getResult() const {
2212 return result;
2213 }
2214 virtual bool handler() {
2215 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002216 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002217 return true;
2218 }
2219 };
2220
Mathias Agopianabd671a2010-10-14 14:54:06 -07002221 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002222 status_t res = postMessageSync(msg);
2223 if (res == NO_ERROR) {
2224 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002225
2226 // work-around: when the power-manager calls us we activate the
2227 // animation. eventually, the "on" animation will be called
2228 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002229 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002230 }
2231 return res;
2232}
2233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002234// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002235
Mathias Agopianabd671a2010-10-14 14:54:06 -07002236status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237{
2238 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2239 if (hw.canDraw()) {
2240 // we're already on
2241 return NO_ERROR;
2242 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002243 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2244 electronBeamOnAnimationImplLocked();
2245 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002246
2247 // make sure to redraw the whole screen when the animation is done
2248 mDirtyRegion.set(hw.bounds());
2249 signalEvent();
2250
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002251 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002252}
2253
2254status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2255{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002256 class MessageTurnElectronBeamOn : public MessageBase {
2257 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002258 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 status_t result;
2260 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002261 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2262 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002263 }
2264 status_t getResult() const {
2265 return result;
2266 }
2267 virtual bool handler() {
2268 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002269 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002270 return true;
2271 }
2272 };
2273
Mathias Agopianabd671a2010-10-14 14:54:06 -07002274 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002275 return NO_ERROR;
2276}
2277
2278// ---------------------------------------------------------------------------
2279
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2281 sp<IMemoryHeap>* heap,
2282 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002283 uint32_t sw, uint32_t sh,
2284 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002285{
2286 status_t result = PERMISSION_DENIED;
2287
2288 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002289 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002290 return BAD_VALUE;
2291
2292 if (!GLExtensions::getInstance().haveFramebufferObject())
2293 return INVALID_OPERATION;
2294
2295 // get screen geometry
2296 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2297 const uint32_t hw_w = hw.getWidth();
2298 const uint32_t hw_h = hw.getHeight();
2299
2300 if ((sw > hw_w) || (sh > hw_h))
2301 return BAD_VALUE;
2302
2303 sw = (!sw) ? hw_w : sw;
2304 sh = (!sh) ? hw_h : sh;
2305 const size_t size = sw * sh * 4;
2306
Steve Block9d453682011-12-20 16:23:08 +00002307 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002308 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002309
Mathias Agopian74c40c02010-09-29 13:02:36 -07002310 // make sure to clear all GL error flags
2311 while ( glGetError() != GL_NO_ERROR ) ;
2312
2313 // create a FBO
2314 GLuint name, tname;
2315 glGenRenderbuffersOES(1, &tname);
2316 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2317 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2318 glGenFramebuffersOES(1, &name);
2319 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2320 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2321 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2322
2323 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002324
Mathias Agopian74c40c02010-09-29 13:02:36 -07002325 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2326
2327 // invert everything, b/c glReadPixel() below will invert the FB
2328 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002329 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002330 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 glMatrixMode(GL_PROJECTION);
2332 glPushMatrix();
2333 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002334 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 glMatrixMode(GL_MODELVIEW);
2336
2337 // redraw the screen entirely...
2338 glClearColor(0,0,0,1);
2339 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002340
Jamie Gennis9575f602011-10-07 14:51:16 -07002341 const LayerVector& layers(mDrawingState.layersSortedByZ);
2342 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 for (size_t i=0 ; i<count ; ++i) {
2344 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002345 const uint32_t flags = layer->drawingState().flags;
2346 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2347 const uint32_t z = layer->drawingState().z;
2348 if (z >= minLayerZ && z <= maxLayerZ) {
2349 layer->drawForSreenShot();
2350 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002351 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 }
2353
2354 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002355 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356 glScissor(0, 0, sw, sh);
2357
2358 // check for errors and return screen capture
2359 if (glGetError() != GL_NO_ERROR) {
2360 // error while rendering
2361 result = INVALID_OPERATION;
2362 } else {
2363 // allocate shared memory large enough to hold the
2364 // screen capture
2365 sp<MemoryHeapBase> base(
2366 new MemoryHeapBase(size, 0, "screen-capture") );
2367 void* const ptr = base->getBase();
2368 if (ptr) {
2369 // capture the screen with glReadPixels()
2370 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2371 if (glGetError() == GL_NO_ERROR) {
2372 *heap = base;
2373 *w = sw;
2374 *h = sh;
2375 *f = PIXEL_FORMAT_RGBA_8888;
2376 result = NO_ERROR;
2377 }
2378 } else {
2379 result = NO_MEMORY;
2380 }
2381 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002382 glEnable(GL_SCISSOR_TEST);
2383 glViewport(0, 0, hw_w, hw_h);
2384 glMatrixMode(GL_PROJECTION);
2385 glPopMatrix();
2386 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002387 } else {
2388 result = BAD_VALUE;
2389 }
2390
2391 // release FBO resources
2392 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2393 glDeleteRenderbuffersOES(1, &tname);
2394 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002395
2396 hw.compositionComplete();
2397
Steve Block9d453682011-12-20 16:23:08 +00002398 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002399
Mathias Agopian74c40c02010-09-29 13:02:36 -07002400 return result;
2401}
2402
2403
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002404status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2405 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002406 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 uint32_t sw, uint32_t sh,
2408 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002409{
2410 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002411 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002412 return BAD_VALUE;
2413
2414 if (!GLExtensions::getInstance().haveFramebufferObject())
2415 return INVALID_OPERATION;
2416
2417 class MessageCaptureScreen : public MessageBase {
2418 SurfaceFlinger* flinger;
2419 DisplayID dpy;
2420 sp<IMemoryHeap>* heap;
2421 uint32_t* w;
2422 uint32_t* h;
2423 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002424 uint32_t sw;
2425 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002426 uint32_t minLayerZ;
2427 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002428 status_t result;
2429 public:
2430 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002432 uint32_t sw, uint32_t sh,
2433 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002434 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002435 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2436 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2437 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002438 {
2439 }
2440 status_t getResult() const {
2441 return result;
2442 }
2443 virtual bool handler() {
2444 Mutex::Autolock _l(flinger->mStateLock);
2445
2446 // if we have secure windows, never allow the screen capture
2447 if (flinger->mSecureFrameBuffer)
2448 return true;
2449
Mathias Agopian74c40c02010-09-29 13:02:36 -07002450 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002451 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002452
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002453 return true;
2454 }
2455 };
2456
2457 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002458 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002459 status_t res = postMessageSync(msg);
2460 if (res == NO_ERROR) {
2461 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2462 }
2463 return res;
2464}
2465
2466// ---------------------------------------------------------------------------
2467
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002468sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2469{
2470 sp<Layer> result;
2471 Mutex::Autolock _l(mStateLock);
2472 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2473 return result;
2474}
2475
2476// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477
Mathias Agopian96f08192010-06-02 23:28:45 -07002478Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002479 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002481}
2482
Mathias Agopian96f08192010-06-02 23:28:45 -07002483Client::~Client()
2484{
Mathias Agopian96f08192010-06-02 23:28:45 -07002485 const size_t count = mLayers.size();
2486 for (size_t i=0 ; i<count ; i++) {
2487 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2488 if (layer != 0) {
2489 mFlinger->removeLayer(layer);
2490 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 }
2492}
2493
Mathias Agopian96f08192010-06-02 23:28:45 -07002494status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002495 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002497
Mathias Agopian4f113742011-05-03 16:21:41 -07002498size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002499{
Mathias Agopian4f113742011-05-03 16:21:41 -07002500 Mutex::Autolock _l(mLock);
2501 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002502 mLayers.add(name, layer);
2503 return name;
2504}
2505
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002506void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002507{
Mathias Agopian4f113742011-05-03 16:21:41 -07002508 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002509 // we do a linear search here, because this doesn't happen often
2510 const size_t count = mLayers.size();
2511 for (size_t i=0 ; i<count ; i++) {
2512 if (mLayers.valueAt(i) == layer) {
2513 mLayers.removeItemsAt(i, 1);
2514 break;
2515 }
2516 }
2517}
Mathias Agopian4f113742011-05-03 16:21:41 -07002518sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2519{
2520 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002521 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002522 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002523 if (layer != 0) {
2524 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002525 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002526 }
2527 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002528}
2529
Mathias Agopiana67932f2011-04-20 14:20:59 -07002530
2531status_t Client::onTransact(
2532 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2533{
2534 // these must be checked
2535 IPCThreadState* ipc = IPCThreadState::self();
2536 const int pid = ipc->getCallingPid();
2537 const int uid = ipc->getCallingUid();
2538 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002539 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002540 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002541 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002542 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002543 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002544 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2545 return PERMISSION_DENIED;
2546 }
2547 }
2548 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002549}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002550
2551
Mathias Agopian96f08192010-06-02 23:28:45 -07002552sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002553 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002554 const String8& name,
2555 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 uint32_t flags)
2557{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002558 /*
2559 * createSurface must be called from the GL thread so that it can
2560 * have access to the GL context.
2561 */
2562
2563 class MessageCreateSurface : public MessageBase {
2564 sp<ISurface> result;
2565 SurfaceFlinger* flinger;
2566 ISurfaceComposerClient::surface_data_t* params;
2567 Client* client;
2568 const String8& name;
2569 DisplayID display;
2570 uint32_t w, h;
2571 PixelFormat format;
2572 uint32_t flags;
2573 public:
2574 MessageCreateSurface(SurfaceFlinger* flinger,
2575 ISurfaceComposerClient::surface_data_t* params,
2576 const String8& name, Client* client,
2577 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2578 uint32_t flags)
2579 : flinger(flinger), params(params), client(client), name(name),
2580 display(display), w(w), h(h), format(format), flags(flags)
2581 {
2582 }
2583 sp<ISurface> getResult() const { return result; }
2584 virtual bool handler() {
2585 result = flinger->createSurface(params, name, client,
2586 display, w, h, format, flags);
2587 return true;
2588 }
2589 };
2590
2591 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2592 params, name, this, display, w, h, format, flags);
2593 mFlinger->postMessageSync(msg);
2594 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002595}
Mathias Agopian96f08192010-06-02 23:28:45 -07002596status_t Client::destroySurface(SurfaceID sid) {
2597 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002599
2600// ---------------------------------------------------------------------------
2601
Jamie Gennis9a78c902011-01-12 18:30:40 -08002602GraphicBufferAlloc::GraphicBufferAlloc() {}
2603
2604GraphicBufferAlloc::~GraphicBufferAlloc() {}
2605
2606sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002607 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002608 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2609 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002610 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002611 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002612 if (err == NO_MEMORY) {
2613 GraphicBuffer::dumpAllocationsToSystemLog();
2614 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002615 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002616 "failed (%s), handle=%p",
2617 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002618 return 0;
2619 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002620 return graphicBuffer;
2621}
2622
Jamie Gennis9a78c902011-01-12 18:30:40 -08002623// ---------------------------------------------------------------------------
2624
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002625GraphicPlane::GraphicPlane()
2626 : mHw(0)
2627{
2628}
2629
2630GraphicPlane::~GraphicPlane() {
2631 delete mHw;
2632}
2633
2634bool GraphicPlane::initialized() const {
2635 return mHw ? true : false;
2636}
2637
Mathias Agopian2b92d892010-02-08 15:49:35 -08002638int GraphicPlane::getWidth() const {
2639 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002640}
2641
Mathias Agopian2b92d892010-02-08 15:49:35 -08002642int GraphicPlane::getHeight() const {
2643 return mHeight;
2644}
2645
2646void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2647{
2648 mHw = hw;
2649
2650 // initialize the display orientation transform.
2651 // it's a constant that should come from the display driver.
2652 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2653 char property[PROPERTY_VALUE_MAX];
2654 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2655 //displayOrientation
2656 switch (atoi(property)) {
2657 case 90:
2658 displayOrientation = ISurfaceComposer::eOrientation90;
2659 break;
2660 case 270:
2661 displayOrientation = ISurfaceComposer::eOrientation270;
2662 break;
2663 }
2664 }
2665
2666 const float w = hw->getWidth();
2667 const float h = hw->getHeight();
2668 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2669 &mDisplayTransform);
2670 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2671 mDisplayWidth = h;
2672 mDisplayHeight = w;
2673 } else {
2674 mDisplayWidth = w;
2675 mDisplayHeight = h;
2676 }
2677
2678 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002679}
2680
2681status_t GraphicPlane::orientationToTransfrom(
2682 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002683{
2684 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002685 switch (orientation) {
2686 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002687 flags = Transform::ROT_0;
2688 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002690 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002691 break;
2692 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002693 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002694 break;
2695 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002696 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002697 break;
2698 default:
2699 return BAD_VALUE;
2700 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002701 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002702 return NO_ERROR;
2703}
2704
2705status_t GraphicPlane::setOrientation(int orientation)
2706{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002707 // If the rotation can be handled in hardware, this is where
2708 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002709
2710 const DisplayHardware& hw(displayHardware());
2711 const float w = mDisplayWidth;
2712 const float h = mDisplayHeight;
2713 mWidth = int(w);
2714 mHeight = int(h);
2715
2716 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002717 GraphicPlane::orientationToTransfrom(orientation, w, h,
2718 &orientationTransform);
2719 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2720 mWidth = int(h);
2721 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002722 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002723
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002724 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002725 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726 return NO_ERROR;
2727}
2728
2729const DisplayHardware& GraphicPlane::displayHardware() const {
2730 return *mHw;
2731}
2732
Mathias Agopian59119e62010-10-11 12:37:43 -07002733DisplayHardware& GraphicPlane::editDisplayHardware() {
2734 return *mHw;
2735}
2736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002737const Transform& GraphicPlane::transform() const {
2738 return mGlobalTransform;
2739}
2740
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002741EGLDisplay GraphicPlane::getEGLDisplay() const {
2742 return mHw->getEGLDisplay();
2743}
2744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002745// ---------------------------------------------------------------------------
2746
2747}; // namespace android