blob: 62461bb26471396feaf6e5943161f8101b00abdf [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian0dd593f2011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070036
Mathias Agopian79f39eb2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include <utils/String8.h>
40#include <utils/String16.h>
41#include <utils/StopWatch.h>
42
Mathias Agopian6950e422009-10-05 17:07:12 -070043#include <ui/GraphicBufferAllocator.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian8335f1c2012-02-25 18:48:35 -080049#include "DdmConnection.h"
Mathias Agopian79f39eb2011-11-17 17:49:17 -080050#include "DisplayEventConnection.h"
51#include "EventThread.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070052#include "GLExtensions.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054#include "LayerDim.h"
Mathias Agopian0ab84ef2011-10-13 16:02:48 -070055#include "LayerScreenshot.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070059#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Glenn Kasten1e808692011-12-19 13:55:34 -080061#include <private/android_filesystem_config.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080062#include <private/gui/SharedBufferStack.h>
Mathias Agopian7bb843c2011-04-20 14:20:59 -070063
Mathias Agopiancadd3252011-11-29 17:55:46 -080064#define EGL_VERSION_HW_ANDROID 0x3143
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066#define DISPLAY_COUNT 1
67
68namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069// ---------------------------------------------------------------------------
70
Mathias Agopian0dd593f2011-06-27 16:05:52 -070071const String16 sHardwareTest("android.permission.HARDWARE_TEST");
72const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
73const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
74const String16 sDump("android.permission.DUMP");
75
76// ---------------------------------------------------------------------------
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078SurfaceFlinger::SurfaceFlinger()
79 : BnSurfaceComposer(), Thread(false),
80 mTransactionFlags(0),
Jamie Gennis122aa6b2011-10-12 17:39:00 -070081 mTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070082 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 mBootTime(systemTime()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070085 mHwWorkListDirty(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070086 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 mDebugBackground(0),
Mathias Agopian93d75ec2011-08-15 20:44:40 -070089 mDebugDDMS(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070090 mDebugDisableHWC(0),
Mathias Agopian2143fe02011-08-23 18:03:18 -070091 mDebugDisableTransformHint(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070092 mDebugInSwapBuffers(0),
93 mLastSwapBufferTime(0),
94 mDebugInTransaction(0),
95 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -070096 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 mConsoleSignals(0),
98 mSecureFrameBuffer(0)
99{
100 init();
101}
102
103void SurfaceFlinger::init()
104{
Steve Block6215d3f2012-01-04 20:05:49 +0000105 ALOGI("SurfaceFlinger is starting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 // debugging stuff...
108 char value[PROPERTY_VALUE_MAX];
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 property_get("debug.sf.showupdates", value, "0");
111 mDebugRegion = atoi(value);
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 property_get("debug.sf.showbackground", value, "0");
114 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700116 property_get("debug.sf.ddms", value, "0");
117 mDebugDDMS = atoi(value);
118 if (mDebugDDMS) {
119 DdmConnection::start(getServiceName());
120 }
121
Steve Block6215d3f2012-01-04 20:05:49 +0000122 ALOGI_IF(mDebugRegion, "showupdates enabled");
123 ALOGI_IF(mDebugBackground, "showbackground enabled");
124 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125}
126
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800127void SurfaceFlinger::onFirstRef()
128{
129 mEventQueue.init(this);
130
131 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
132
133 // Wait for the main thread to be done with its initialization
134 mReadyToRunBarrier.wait();
135}
136
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138SurfaceFlinger::~SurfaceFlinger()
139{
140 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141}
142
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800143void SurfaceFlinger::binderDied(const wp<IBinder>& who)
144{
145 // the window manager died on us. prepare its eulogy.
146
147 // reset screen orientation
148 Vector<ComposerState> state;
149 setTransactionState(state, eOrientationDefault, 0);
150
151 // restart the boot-animation
152 property_set("ctl.start", "bootanim");
153}
154
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700155sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700157 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158}
159
Mathias Agopian770492c2010-05-28 14:22:23 -0700160sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700162 sp<ISurfaceComposerClient> bclient;
163 sp<Client> client(new Client(this));
164 status_t err = client->initCheck();
165 if (err == NO_ERROR) {
166 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 return bclient;
169}
170
Jamie Gennisf7acf162011-01-12 18:30:40 -0800171sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
172{
173 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
174 return gba;
175}
Mathias Agopian7623da42010-06-01 15:12:58 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
178{
Steve Block3762c312012-01-06 19:20:56 +0000179 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 const GraphicPlane& plane(mGraphicPlanes[dpy]);
181 return plane;
182}
183
184GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
185{
186 return const_cast<GraphicPlane&>(
187 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
188}
189
190void SurfaceFlinger::bootFinished()
191{
192 const nsecs_t now = systemTime();
193 const nsecs_t duration = now - mBootTime;
Steve Block6215d3f2012-01-04 20:05:49 +0000194 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700195 mBootFinished = true;
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700196
197 // wait patiently for the window manager death
198 const String16 name("window");
199 sp<IBinder> window(defaultServiceManager()->getService(name));
200 if (window != 0) {
201 window->linkToDeath(this);
202 }
203
204 // stop boot animation
Mathias Agopian627e7b52009-05-21 19:21:59 -0700205 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206}
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208static inline uint16_t pack565(int r, int g, int b) {
209 return (r<<11)|(g<<5)|b;
210}
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212status_t SurfaceFlinger::readyToRun()
213{
Steve Block6215d3f2012-01-04 20:05:49 +0000214 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 "Initializing graphics H/W...");
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 // we only support one display currently
218 int dpy = 0;
219
220 {
221 // initialize the main display
222 GraphicPlane& plane(graphicPlane(dpy));
223 DisplayHardware* const hw = new DisplayHardware(this, dpy);
224 plane.setDisplayHardware(hw);
225 }
226
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700227 // create the shared control-block
228 mServerHeap = new MemoryHeapBase(4096,
229 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Block3762c312012-01-06 19:20:56 +0000230 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700231
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700232 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Block3762c312012-01-06 19:20:56 +0000233 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700234
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700235 new(mServerCblk) surface_flinger_cblk_t;
236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 // initialize primary screen
238 // (other display should be initialized in the same manner, but
239 // asynchronously, as they could come and go. None of this is supported
240 // yet).
241 const GraphicPlane& plane(graphicPlane(dpy));
242 const DisplayHardware& hw = plane.displayHardware();
243 const uint32_t w = hw.getWidth();
244 const uint32_t h = hw.getHeight();
245 const uint32_t f = hw.getFormat();
246 hw.makeCurrent();
247
248 // initialize the shared control block
249 mServerCblk->connected |= 1<<dpy;
250 display_cblk_t* dcblk = mServerCblk->displays + dpy;
251 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800252 dcblk->w = plane.getWidth();
253 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 dcblk->format = f;
255 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
256 dcblk->xdpi = hw.getDpiX();
257 dcblk->ydpi = hw.getDpiY();
258 dcblk->fps = hw.getRefreshRate();
259 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700263 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 glEnableClientState(GL_VERTEX_ARRAY);
265 glEnable(GL_SCISSOR_TEST);
266 glShadeModel(GL_FLAT);
267 glDisable(GL_DITHER);
268 glDisable(GL_CULL_FACE);
269
270 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
271 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis830d0832011-10-07 14:51:16 -0700272 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 glGenTextures(1, &mWormholeTexName);
274 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis830d0832011-10-07 14:51:16 -0700280 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
281
282 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
283 glGenTextures(1, &mProtectedTexName);
284 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
290 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
292 glViewport(0, 0, w, h);
293 glMatrixMode(GL_PROJECTION);
294 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -0700295 // put the origin in the left-bottom corner
296 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800298
299 // start the EventThread
300 mEventThread = new EventThread(this);
Mathias Agopiane02f7b32012-01-24 16:39:14 -0800301 mEventQueue.setEventThread(mEventThread);
Mathias Agopian587ae012012-02-05 02:15:28 -0800302 hw.startSleepManagement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
304 /*
305 * We're now ready to accept clients...
306 */
307
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800308 mReadyToRunBarrier.open();
309
Mathias Agopian627e7b52009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800317
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700318bool SurfaceFlinger::authenticateSurfaceTexture(
319 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800320 Mutex::Autolock _l(mStateLock);
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700321 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800322
323 // Check the visible layer list for the ISurface
324 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
325 size_t count = currentLayers.size();
326 for (size_t i=0 ; i<count ; i++) {
327 const sp<LayerBase>& layer(currentLayers[i]);
328 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700329 if (lbc != NULL) {
330 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
331 if (lbcBinder == surfaceTextureBinder) {
332 return true;
333 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800334 }
335 }
336
337 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700338 // SurfaceTexture gets destroyed before all the clients are done using it,
339 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800340 // will instead fail later on when the client tries to use the surface,
341 // which should be reported as "surface XYZ returned an -ENODEV". The
342 // purgatorized layers are no less authentic than the visible ones, so this
343 // should not cause any harm.
344 size_t purgatorySize = mLayerPurgatory.size();
345 for (size_t i=0 ; i<purgatorySize ; i++) {
346 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
347 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700348 if (lbc != NULL) {
349 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
350 if (lbcBinder == surfaceTextureBinder) {
351 return true;
352 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800353 }
354 }
355
356 return false;
357}
358
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800359// ----------------------------------------------------------------------------
360
361sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopiane02f7b32012-01-24 16:39:14 -0800362 return mEventThread->createEventConnection();
Mathias Agopian898c4c92010-05-18 17:06:55 -0700363}
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365// ----------------------------------------------------------------------------
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800366
367void SurfaceFlinger::waitForEvent() {
368 mEventQueue.waitMessage();
369}
370
371void SurfaceFlinger::signalTransaction() {
372 mEventQueue.invalidate();
373}
374
375void SurfaceFlinger::signalLayerUpdate() {
376 mEventQueue.invalidate();
377}
378
379void SurfaceFlinger::signalRefresh() {
380 mEventQueue.refresh();
381}
382
383status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
384 nsecs_t reltime, uint32_t flags) {
385 return mEventQueue.postMessage(msg, reltime);
386}
387
388status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
389 nsecs_t reltime, uint32_t flags) {
390 status_t res = mEventQueue.postMessage(msg, reltime);
391 if (res == NO_ERROR) {
392 msg->wait();
393 }
394 return res;
395}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396
397bool SurfaceFlinger::threadLoop()
398{
399 waitForEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 return true;
401}
402
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800403void SurfaceFlinger::onMessageReceived(int32_t what)
404{
405 switch (what) {
Mathias Agopiancf7f0c72012-02-05 01:49:16 -0800406 case MessageQueue::REFRESH: {
407// case MessageQueue::INVALIDATE: {
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800408 // check for transactions
409 if (CC_UNLIKELY(mConsoleSignals)) {
410 handleConsoleEvents();
411 }
412
413 // if we're in a global transaction, don't do anything.
414 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
415 uint32_t transactionFlags = peekTransactionFlags(mask);
416 if (CC_UNLIKELY(transactionFlags)) {
417 handleTransaction(transactionFlags);
418 }
419
420 // post surfaces (if needed)
421 handlePageFlip();
422
Mathias Agopiancf7f0c72012-02-05 01:49:16 -0800423// signalRefresh();
424//
425// } break;
426//
427// case MessageQueue::REFRESH: {
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800428
Mathias Agopianaf872162012-02-03 17:22:09 -0800429 handleRefresh();
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800430
Mathias Agopiancf7f0c72012-02-05 01:49:16 -0800431 const DisplayHardware& hw(graphicPlane(0).displayHardware());
432
433// if (mDirtyRegion.isEmpty()) {
434// return;
435// }
436
Mathias Agopianfe005282012-02-04 15:44:04 -0800437 if (CC_UNLIKELY(mHwWorkListDirty)) {
438 // build the h/w work list
439 handleWorkList();
440 }
Mathias Agopiancf7f0c72012-02-05 01:49:16 -0800441
Mathias Agopianfe005282012-02-04 15:44:04 -0800442 if (CC_LIKELY(hw.canDraw())) {
443 // repaint the framebuffer (if needed)
444 handleRepaint();
445 // inform the h/w that we're done compositing
446 hw.compositionComplete();
447 postFramebuffer();
Mathias Agopianaf872162012-02-03 17:22:09 -0800448 } else {
Mathias Agopianfe005282012-02-04 15:44:04 -0800449 // pretend we did the post
Mathias Agopianaf872162012-02-03 17:22:09 -0800450 hw.compositionComplete();
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800451 }
Mathias Agopianfe005282012-02-04 15:44:04 -0800452
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800453 } break;
454 }
455}
456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457void SurfaceFlinger::postFramebuffer()
458{
Mathias Agopianfe005282012-02-04 15:44:04 -0800459 // mSwapRegion can be empty here is some cases, for instance if a hidden
460 // or fully transparent window is updating.
461 // in that case, we need to flip anyways to not risk a deadlock with
462 // h/w composer.
463
Mathias Agopiane757a872011-10-16 18:46:35 -0700464 const DisplayHardware& hw(graphicPlane(0).displayHardware());
465 const nsecs_t now = systemTime();
466 mDebugInSwapBuffers = now;
467 hw.flip(mSwapRegion);
Jamie Gennisb335fad2012-01-15 18:54:57 -0800468
469 size_t numLayers = mVisibleLayersSortedByZ.size();
470 for (size_t i = 0; i < numLayers; i++) {
471 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
472 }
473
Mathias Agopiane757a872011-10-16 18:46:35 -0700474 mLastSwapBufferTime = systemTime() - now;
475 mDebugInSwapBuffers = 0;
476 mSwapRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477}
478
479void SurfaceFlinger::handleConsoleEvents()
480{
481 // something to do with the console
482 const DisplayHardware& hw = graphicPlane(0).displayHardware();
483
484 int what = android_atomic_and(0, &mConsoleSignals);
485 if (what & eConsoleAcquired) {
486 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700487 // this is a temporary work-around, eventually this should be called
488 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700489 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700493 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 hw.releaseScreen();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496 }
497
498 mDirtyRegion.set(hw.bounds());
499}
500
501void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
502{
Mathias Agopian69608112011-05-19 15:38:14 -0700503 Mutex::Autolock _l(mStateLock);
504 const nsecs_t now = systemTime();
505 mDebugInTransaction = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
Mathias Agopian69608112011-05-19 15:38:14 -0700507 // Here we're guaranteed that some transaction flags are set
508 // so we can call handleTransactionLocked() unconditionally.
509 // We call getTransactionFlags(), which will also clear the flags,
510 // with mStateLock held to guarantee that mCurrentState won't change
511 // until the transaction is committed.
Mathias Agopianff1d4102010-08-10 17:19:56 -0700512
Mathias Agopian69608112011-05-19 15:38:14 -0700513 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
514 transactionFlags = getTransactionFlags(mask);
515 handleTransactionLocked(transactionFlags);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700516
Mathias Agopian69608112011-05-19 15:38:14 -0700517 mLastTransactionTime = systemTime() - now;
518 mDebugInTransaction = 0;
519 invalidateHwcGeometry();
520 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700521}
522
Mathias Agopian69608112011-05-19 15:38:14 -0700523void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700524{
525 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 const size_t count = currentLayers.size();
527
528 /*
529 * Traversal of the children
530 * (perform the transaction for each of them if needed)
531 */
532
533 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
534 if (layersNeedTransaction) {
535 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700536 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
538 if (!trFlags) continue;
539
540 const uint32_t flags = layer->doTransaction(0);
541 if (flags & Layer::eVisibleRegion)
542 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 }
545
546 /*
547 * Perform our own transaction if needed
548 */
549
550 if (transactionFlags & eTransactionNeeded) {
551 if (mCurrentState.orientation != mDrawingState.orientation) {
552 // the orientation has changed, recompute all visible regions
553 // and invalidate everything.
554
555 const int dpy = 0;
556 const int orientation = mCurrentState.orientation;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700557 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 GraphicPlane& plane(graphicPlane(dpy));
559 plane.setOrientation(orientation);
560
561 // update the shared control block
562 const DisplayHardware& hw(plane.displayHardware());
563 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
564 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800565 dcblk->w = plane.getWidth();
566 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567
568 mVisibleRegionsDirty = true;
569 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700572 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
573 // layers have been added
574 mVisibleRegionsDirty = true;
575 }
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 // some layers might have been removed, so
578 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700579 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700580 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700582 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700583 const size_t count = previousLayers.size();
584 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700585 const sp<LayerBase>& layer(previousLayers[i]);
586 if (currentLayers.indexOf( layer ) < 0) {
587 // this layer is not visible anymore
Mathias Agopian33863dd2009-07-28 14:20:21 -0700588 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700589 }
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593
594 commitTransaction();
595}
596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800598 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599{
600 const GraphicPlane& plane(graphicPlane(0));
601 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700602 const DisplayHardware& hw(plane.displayHardware());
603 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604
605 Region aboveOpaqueLayers;
606 Region aboveCoveredLayers;
607 Region dirty;
608
609 bool secureFrameBuffer = false;
610
611 size_t i = currentLayers.size();
612 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700613 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 layer->validateVisibility(planeTransform);
615
616 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700617 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700619 /*
620 * opaqueRegion: area of a surface that is fully opaque.
621 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700623
624 /*
625 * visibleRegion: area of a surface that is visible on screen
626 * and not fully transparent. This is essentially the layer's
627 * footprint minus the opaque regions above it.
628 * Areas covered by a translucent surface are considered visible.
629 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700631
632 /*
633 * coveredRegion: area of a surface that is covered by all
634 * visible regions above it (which includes the translucent areas).
635 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700637
638
639 // handle hidden surfaces by setting the visible region to empty
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800640 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700641 const bool translucent = !layer->isOpaque();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700642 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700644 visibleRegion.andSelf(screenRegion);
645 if (!visibleRegion.isEmpty()) {
646 // Remove the transparent area from the visible region
647 if (translucent) {
648 visibleRegion.subtractSelf(layer->transparentRegionScreen);
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700651 // compute the opaque region
652 const int32_t layerOrientation = layer->getOrientation();
653 if (s.alpha==255 && !translucent &&
654 ((layerOrientation & Transform::ROT_INVALID) == false)) {
655 // the opaque region is the layer's footprint
656 opaqueRegion = visibleRegion;
657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659 }
660
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700661 // Clip the covered region to the visible region
662 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
663
664 // Update aboveCoveredLayers for next (lower) layer
665 aboveCoveredLayers.orSelf(visibleRegion);
666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 // subtract the opaque region covered by the layers above us
668 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
670 // compute this layer's dirty region
671 if (layer->contentDirty) {
672 // we need to invalidate the whole region
673 dirty = visibleRegion;
674 // as well, as the old visible region
675 dirty.orSelf(layer->visibleRegionScreen);
676 layer->contentDirty = false;
677 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700678 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700679 * the exposed region consists of two components:
680 * 1) what's VISIBLE now and was COVERED before
681 * 2) what's EXPOSED now less what was EXPOSED before
682 *
683 * note that (1) is conservative, we start with the whole
684 * visible region but only keep what used to be covered by
685 * something -- which mean it may have been exposed.
686 *
687 * (2) handles areas that were not covered by anything but got
688 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700689 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700690 const Region newExposed = visibleRegion - coveredRegion;
691 const Region oldVisibleRegion = layer->visibleRegionScreen;
692 const Region oldCoveredRegion = layer->coveredRegionScreen;
693 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
694 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696 dirty.subtractSelf(aboveOpaqueLayers);
697
698 // accumulate to the screen dirty region
699 dirtyRegion.orSelf(dirty);
700
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700701 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 // Store the visible region is screen space
705 layer->setVisibleRegion(visibleRegion);
706 layer->setCoveredRegion(coveredRegion);
707
Mathias Agopian12cedff2009-07-28 10:57:27 -0700708 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 if (layer->isSecure() && !visibleRegion.isEmpty()) {
710 secureFrameBuffer = true;
711 }
712 }
713
Mathias Agopian12cedff2009-07-28 10:57:27 -0700714 // invalidate the areas where a layer was removed
715 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
716 mDirtyRegionRemovedLayer.clear();
717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 mSecureFrameBuffer = secureFrameBuffer;
719 opaqueRegion = aboveOpaqueLayers;
720}
721
722
723void SurfaceFlinger::commitTransaction()
724{
Jesse Hall8a7c9402011-12-02 10:00:00 -0800725 if (!mLayersPendingRemoval.isEmpty()) {
726 // Notify removed layers now that they can't be drawn from
727 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
728 mLayersPendingRemoval[i]->onRemoved();
729 }
730 mLayersPendingRemoval.clear();
731 }
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 mDrawingState = mCurrentState;
Jamie Gennis122aa6b2011-10-12 17:39:00 -0700734 mTransationPending = false;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700735 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736}
737
738void SurfaceFlinger::handlePageFlip()
739{
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800740 const DisplayHardware& hw = graphicPlane(0).displayHardware();
741 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800743 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
744 const bool visibleRegions = lockPageFlip(currentLayers);
745
746 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 Region opaqueRegion;
748 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700749
750 /*
751 * rebuild the visible layer list
752 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800753 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700754 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700755 mVisibleLayersSortedByZ.setCapacity(count);
756 for (size_t i=0 ; i<count ; i++) {
757 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
758 mVisibleLayersSortedByZ.add(currentLayers[i]);
759 }
760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 mWormholeRegion = screenRegion.subtract(opaqueRegion);
762 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800763 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
765
766 unlockPageFlip(currentLayers);
Mathias Agopian6497eab2011-10-21 15:18:28 -0700767
768 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 mDirtyRegion.andSelf(screenRegion);
770}
771
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800772void SurfaceFlinger::invalidateHwcGeometry()
773{
774 mHwWorkListDirty = true;
775}
776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
778{
779 bool recomputeVisibleRegions = false;
780 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700781 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700783 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 layer->lockPageFlip(recomputeVisibleRegions);
785 }
786 return recomputeVisibleRegions;
787}
788
789void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
790{
791 const GraphicPlane& plane(graphicPlane(0));
792 const Transform& planeTransform(plane.transform());
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800793 const size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700794 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700796 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 layer->unlockPageFlip(planeTransform, mDirtyRegion);
798 }
799}
800
Mathias Agopian9a9dbd52012-01-31 18:24:27 -0800801void SurfaceFlinger::handleRefresh()
802{
803 bool needInvalidate = false;
804 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
805 const size_t count = currentLayers.size();
806 for (size_t i=0 ; i<count ; i++) {
807 const sp<LayerBase>& layer(currentLayers[i]);
808 if (layer->onPreComposition()) {
809 needInvalidate = true;
810 }
811 }
812 if (needInvalidate) {
813 signalLayerUpdate();
814 }
815}
816
817
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700818void SurfaceFlinger::handleWorkList()
819{
820 mHwWorkListDirty = false;
821 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
822 if (hwc.initCheck() == NO_ERROR) {
823 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
824 const size_t count = currentLayers.size();
825 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700826 hwc_layer_t* const cur(hwc.getLayers());
827 for (size_t i=0 ; cur && i<count ; i++) {
828 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700829 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian6a969242010-09-22 18:58:01 -0700830 cur[i].compositionType = HWC_FRAMEBUFFER;
831 cur[i].flags |= HWC_SKIP_LAYER;
832 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700833 }
834 }
835}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837void SurfaceFlinger::handleRepaint()
838{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700839 // compute the invalid region
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700840 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800842 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 debugFlashRegions();
844 }
845
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700846 // set the frame buffer
847 const DisplayHardware& hw(graphicPlane(0).displayHardware());
848 glMatrixMode(GL_MODELVIEW);
849 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850
851 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700852 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
853 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700854 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700855 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
856 // takes a rectangle, we must make sure to update that whole
857 // rectangle in that case
858 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700859 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700860 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700861 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700863 // in the BUFFER_PRESERVED case, obviously, we can update only
864 // what's needed and nothing more.
865 // NOTE: this is NOT a common case, as preserving the backbuffer
866 // is costly and usually involves copying the whole update back.
867 }
868 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700869 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700870 // We need to redraw the rectangle that will be updated
871 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700872 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700873 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700874 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700875 } else {
876 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 mDirtyRegion.set(hw.bounds());
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700878 mSwapRegion = mDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880 }
881
Mathias Agopian88cde072011-09-20 17:21:56 -0700882 setupHardwareComposer(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 composeSurfaces(mDirtyRegion);
884
Mathias Agopian88cde072011-09-20 17:21:56 -0700885 // update the swap region and clear the dirty region
886 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 mDirtyRegion.clear();
888}
889
Mathias Agopian88cde072011-09-20 17:21:56 -0700890void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891{
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700892 const DisplayHardware& hw(graphicPlane(0).displayHardware());
893 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700894 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700895 if (!cur) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700896 return;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700897 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700898
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700899 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
900 size_t count = layers.size();
901
Steve Block3762c312012-01-06 19:20:56 +0000902 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700903 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
904 hwc.getNumLayers(), count);
905
906 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700907 if (hwc.initCheck() == NO_ERROR) {
908 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
909 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700910
911 /*
912 * update the per-frame h/w composer data for each layer
913 * and build the transparent region of the FB
914 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700915 for (size_t i=0 ; i<count ; i++) {
916 const sp<LayerBase>& layer(layers[i]);
917 layer->setPerFrameData(&cur[i]);
918 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700919 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700920 status_t err = hwc.prepare();
Steve Block3762c312012-01-06 19:20:56 +0000921 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700922
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700923 if (err == NO_ERROR) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700924 // what's happening here is tricky.
925 // we want to clear all the layers with the CLEAR_FB flags
926 // that are opaque.
927 // however, since some GPU are efficient at preserving
928 // the backbuffer, we want to take advantage of that so we do the
929 // clear only in the dirty region (other areas will be preserved
930 // on those GPUs).
931 // NOTE: on non backbuffer preserving GPU, the dirty region
932 // has already been expanded as needed, so the code is correct
933 // there too.
934 //
935 // However, the content of the framebuffer cannot be trusted when
936 // we switch to/from FB/OVERLAY, in which case we need to
937 // expand the dirty region to those areas too.
938 //
939 // Note also that there is a special case when switching from
940 // "no layers in FB" to "some layers in FB", where we need to redraw
941 // the entire FB, since some areas might contain uninitialized
942 // data.
943 //
944 // Also we want to make sure to not clear areas that belong to
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700945 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian88cde072011-09-20 17:21:56 -0700946 // that is, we can't erase anything outside the dirty region.
947
Mathias Agopian0a5abdb2011-09-09 01:47:48 -0700948 Region transparent;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700949
Mathias Agopian88cde072011-09-20 17:21:56 -0700950 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
951 transparent.set(hw.getBounds());
952 dirtyInOut = transparent;
953 } else {
954 for (size_t i=0 ; i<count ; i++) {
955 const sp<LayerBase>& layer(layers[i]);
956 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
957 transparent.orSelf(layer->visibleRegionScreen);
958 }
959 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
960 if (isOverlay != layer->isOverlay()) {
961 // we transitioned to/from overlay, so add this layer
962 // to the dirty region so the framebuffer can be either
963 // cleared or redrawn.
964 dirtyInOut.orSelf(layer->visibleRegionScreen);
965 }
966 layer->setOverlay(isOverlay);
Mathias Agopian45491692011-01-19 15:24:23 -0800967 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700968 // don't erase stuff outside the dirty region
969 transparent.andSelf(dirtyInOut);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700970 }
971
972 /*
973 * clear the area of the FB that need to be transparent
974 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700975 if (!transparent.isEmpty()) {
976 glClearColor(0,0,0,0);
977 Region::const_iterator it = transparent.begin();
978 Region::const_iterator const end = transparent.end();
979 const int32_t height = hw.getHeight();
980 while (it != end) {
981 const Rect& r(*it++);
982 const GLint sy = height - (r.top + r.height());
983 glScissor(r.left, sy, r.width(), r.height());
984 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian45491692011-01-19 15:24:23 -0800985 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700986 }
987 }
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700988}
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700989
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700990void SurfaceFlinger::composeSurfaces(const Region& dirty)
991{
Mathias Agopian9fe96542011-09-22 20:57:04 -0700992 const DisplayHardware& hw(graphicPlane(0).displayHardware());
993 HWComposer& hwc(hw.getHwComposer());
994
995 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800996 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700997 // should never happen unless the window manager has a bug
998 // draw something...
999 drawWormhole();
1000 }
1001
Mathias Agopian6ea851f2012-02-23 21:20:01 -08001002 // FIXME: workaroud for b/6020860
1003 glEnable(GL_SCISSOR_TEST);
1004 glScissor(0,0,0,0);
1005 glClear(GL_COLOR_BUFFER_BIT);
1006 // end-workaround
1007
Mathias Agopianf8e705d2010-08-12 15:03:26 -07001008 /*
1009 * and then, render the layers targeted at the framebuffer
1010 */
Mathias Agopian9fe96542011-09-22 20:57:04 -07001011 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -07001012 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1013 size_t count = layers.size();
Mathias Agopianf8e705d2010-08-12 15:03:26 -07001014 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian88cde072011-09-20 17:21:56 -07001015 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -07001016 continue;
Mathias Agopianf8e705d2010-08-12 15:03:26 -07001017 }
1018 const sp<LayerBase>& layer(layers[i]);
1019 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1020 if (!clip.isEmpty()) {
1021 layer->draw(clip);
1022 }
1023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024}
1025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026void SurfaceFlinger::debugFlashRegions()
1027{
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001028 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1029 const uint32_t flags = hw.getFlags();
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001030 const int32_t height = hw.getHeight();
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001031 if (mSwapRegion.isEmpty()) {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001032 return;
1033 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -07001034
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001035 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1036 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1037 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1038 mDirtyRegion.bounds() : hw.bounds());
1039 composeSurfaces(repaint);
1040 }
1041
Mathias Agopian9044ef02011-10-18 14:49:27 -07001042 glDisable(GL_TEXTURE_EXTERNAL_OES);
1043 glDisable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 glDisable(GL_SCISSOR_TEST);
1046
Mathias Agopiandff8e582009-05-04 14:17:04 -07001047 static int toggle = 0;
1048 toggle = 1 - toggle;
1049 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001050 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001051 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001052 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001055 Region::const_iterator it = mDirtyRegion.begin();
1056 Region::const_iterator const end = mDirtyRegion.end();
1057 while (it != end) {
1058 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 GLfloat vertices[][2] = {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001060 { r.left, height - r.top },
1061 { r.left, height - r.bottom },
1062 { r.right, height - r.bottom },
1063 { r.right, height - r.top }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 };
1065 glVertexPointer(2, GL_FLOAT, 0, vertices);
1066 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1067 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001068
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001069 hw.flip(mSwapRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070
1071 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001072 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073
1074 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075}
1076
1077void SurfaceFlinger::drawWormhole() const
1078{
1079 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1080 if (region.isEmpty())
1081 return;
1082
1083 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1084 const int32_t width = hw.getWidth();
1085 const int32_t height = hw.getHeight();
1086
Glenn Kastene80a4cc2011-12-15 09:51:17 -08001087 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001088 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001089 Region::const_iterator it = region.begin();
1090 Region::const_iterator const end = region.end();
1091 while (it != end) {
1092 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 const GLint sy = height - (r.top + r.height());
1094 glScissor(r.left, sy, r.width(), r.height());
1095 glClear(GL_COLOR_BUFFER_BIT);
1096 }
1097 } else {
1098 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1099 { width, height }, { 0, height } };
1100 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopian9044ef02011-10-18 14:49:27 -07001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 glVertexPointer(2, GL_SHORT, 0, vertices);
1103 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1104 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian9044ef02011-10-18 14:49:27 -07001105
1106 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 glEnable(GL_TEXTURE_2D);
1108 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1109 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1110 glMatrixMode(GL_TEXTURE);
1111 glLoadIdentity();
Mathias Agopian9044ef02011-10-18 14:49:27 -07001112
1113 glDisable(GL_BLEND);
1114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001116 Region::const_iterator it = region.begin();
1117 Region::const_iterator const end = region.end();
1118 while (it != end) {
1119 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 const GLint sy = height - (r.top + r.height());
1121 glScissor(r.left, sy, r.width(), r.height());
1122 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1123 }
1124 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001125 glDisable(GL_TEXTURE_2D);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001126 glLoadIdentity();
1127 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129}
1130
Mathias Agopian1473f462009-04-10 14:24:30 -07001131status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132{
1133 Mutex::Autolock _l(mStateLock);
1134 addLayer_l(layer);
1135 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1136 return NO_ERROR;
1137}
1138
Mathias Agopian593c05c2010-06-02 23:28:45 -07001139status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1140{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001141 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001142 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1143}
1144
1145ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1146 const sp<LayerBaseClient>& lbc)
1147{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001148 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001149 size_t name = client->attachLayer(lbc);
1150
1151 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001152
1153 // add this layer to the current state list
1154 addLayer_l(lbc);
1155
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001156 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001157}
1158
Mathias Agopian1473f462009-04-10 14:24:30 -07001159status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160{
1161 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001162 status_t err = purgatorizeLayer_l(layer);
1163 if (err == NO_ERROR)
1164 setTransactionFlags(eTransactionNeeded);
1165 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166}
1167
Mathias Agopian1473f462009-04-10 14:24:30 -07001168status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169{
Mathias Agopian7623da42010-06-01 15:12:58 -07001170 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1171 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001172 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1175 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001176 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 return NO_ERROR;
1178 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001179 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180}
1181
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001182status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1183{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001184 // First add the layer to the purgatory list, which makes sure it won't
1185 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001186 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001187 if (err >= 0) {
1188 mLayerPurgatory.add(layerBase);
1189 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001190
Jesse Hall8a7c9402011-12-02 10:00:00 -08001191 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001192
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001193 // it's possible that we don't find a layer, because it might
1194 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001195 // from the user because there is a race between Client::destroySurface(),
1196 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001197 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1198}
1199
Mathias Agopian593c05c2010-06-02 23:28:45 -07001200status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001202 layer->forceVisibilityTransaction();
1203 setTransactionFlags(eTraversalNeeded);
1204 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205}
1206
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001207uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1208{
1209 return android_atomic_release_load(&mTransactionFlags);
1210}
1211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1213{
1214 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1215}
1216
Mathias Agopian898c4c92010-05-18 17:06:55 -07001217uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218{
1219 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1220 if ((old & flags)==0) { // wake the server up
Mathias Agopian9a9dbd52012-01-31 18:24:27 -08001221 signalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 return old;
1224}
1225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226
Jamie Gennise2909e12011-10-10 15:48:06 -07001227void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001228 int orientation, uint32_t flags) {
Mathias Agopian439863f2011-06-28 19:09:31 -07001229 Mutex::Autolock _l(mStateLock);
Mathias Agopian9779b2212009-09-07 16:32:45 -07001230
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001231 uint32_t transactionFlags = 0;
Jamie Gennise2909e12011-10-10 15:48:06 -07001232 if (mCurrentState.orientation != orientation) {
1233 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1234 mCurrentState.orientation = orientation;
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001235 transactionFlags |= eTransactionNeeded;
Jamie Gennise2909e12011-10-10 15:48:06 -07001236 } else if (orientation != eOrientationUnchanged) {
Steve Block8564c8d2012-01-05 23:22:43 +00001237 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennise2909e12011-10-10 15:48:06 -07001238 orientation);
1239 }
1240 }
1241
Mathias Agopian439863f2011-06-28 19:09:31 -07001242 const size_t count = state.size();
1243 for (size_t i=0 ; i<count ; i++) {
1244 const ComposerState& s(state[i]);
1245 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001246 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian439863f2011-06-28 19:09:31 -07001247 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001248
Mathias Agopiana06c06b2011-11-07 21:58:03 -08001249 if (transactionFlags) {
1250 // this triggers the transaction
1251 setTransactionFlags(transactionFlags);
1252
1253 // if this is a synchronous transaction, wait for it to take effect
1254 // before returning.
1255 if (flags & eSynchronous) {
1256 mTransationPending = true;
1257 }
1258 while (mTransationPending) {
1259 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1260 if (CC_UNLIKELY(err != NO_ERROR)) {
1261 // just in case something goes wrong in SF, return to the
1262 // called after a few seconds.
Steve Block8564c8d2012-01-05 23:22:43 +00001263 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopiana06c06b2011-11-07 21:58:03 -08001264 mTransationPending = false;
1265 break;
1266 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269}
1270
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001271sp<ISurface> SurfaceFlinger::createSurface(
1272 ISurfaceComposerClient::surface_data_t* params,
1273 const String8& name,
1274 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1276 uint32_t flags)
1277{
Mathias Agopian1473f462009-04-10 14:24:30 -07001278 sp<LayerBaseClient> layer;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001279 sp<ISurface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001280
1281 if (int32_t(w|h) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001282 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001283 int(w), int(h));
1284 return surfaceHandle;
1285 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001286
Mathias Agopiane9d4c712012-02-24 14:58:36 -08001287 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian7623da42010-06-01 15:12:58 -07001288 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 switch (flags & eFXSurfaceMask) {
1290 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001291 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1292 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 break;
1294 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001295 // for now we treat Blur as Dim, until we can implement it
1296 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001298 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001300 case eFXSurfaceScreenshot:
1301 layer = createScreenshotSurface(client, d, w, h, flags);
1302 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304
Mathias Agopian1473f462009-04-10 14:24:30 -07001305 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001306 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001307 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001308 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001311 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001312 params->token = token;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001313 params->identity = layer->getIdentity();
Mathias Agopian7623da42010-06-01 15:12:58 -07001314 if (normalLayer != 0) {
1315 Mutex::Autolock _l(mStateLock);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001316 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001317 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001318 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001319
Mathias Agopian593c05c2010-06-02 23:28:45 -07001320 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322
1323 return surfaceHandle;
1324}
1325
Mathias Agopian7623da42010-06-01 15:12:58 -07001326sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001327 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001328 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001329 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330{
1331 // initialize the surfaces
1332 switch (format) { // TODO: take h/w into account
1333 case PIXEL_FORMAT_TRANSPARENT:
1334 case PIXEL_FORMAT_TRANSLUCENT:
1335 format = PIXEL_FORMAT_RGBA_8888;
1336 break;
1337 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001338#ifdef NO_RGBX_8888
1339 format = PIXEL_FORMAT_RGB_565;
1340#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001341 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001342#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 break;
1344 }
1345
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001346#ifdef NO_RGBX_8888
1347 if (format == PIXEL_FORMAT_RGBX_8888)
1348 format = PIXEL_FORMAT_RGBA_8888;
1349#endif
1350
Mathias Agopian593c05c2010-06-02 23:28:45 -07001351 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001352 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kastene80a4cc2011-12-15 09:51:17 -08001353 if (CC_LIKELY(err != NO_ERROR)) {
Steve Block3762c312012-01-06 19:20:56 +00001354 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001355 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
1357 return layer;
1358}
1359
Mathias Agopian7623da42010-06-01 15:12:58 -07001360sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001361 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001362 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001364 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001365 return layer;
1366}
1367
1368sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1369 const sp<Client>& client, DisplayID display,
1370 uint32_t w, uint32_t h, uint32_t flags)
1371{
1372 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 return layer;
1374}
1375
Mathias Agopian593c05c2010-06-02 23:28:45 -07001376status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001378 /*
1379 * called by the window manager, when a surface should be marked for
1380 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001381 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001382 * The surface is removed from the current and drawing lists, but placed
1383 * in the purgatory queue, so it's not destroyed right-away (we need
1384 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001385 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001386
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001387 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001388 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001389 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001390 if (layer != 0) {
1391 err = purgatorizeLayer_l(layer);
1392 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001393 setTransactionFlags(eTransactionNeeded);
1394 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001395 }
1396 return err;
1397}
1398
Mathias Agopian69608112011-05-19 15:38:14 -07001399status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001400{
Mathias Agopian359140c2009-07-02 17:33:40 -07001401 // called by ~ISurface() when all references are gone
Mathias Agopian69608112011-05-19 15:38:14 -07001402 status_t err = NO_ERROR;
1403 sp<LayerBaseClient> l(layer.promote());
1404 if (l != NULL) {
1405 Mutex::Autolock _l(mStateLock);
1406 err = removeLayer_l(l);
1407 if (err == NAME_NOT_FOUND) {
1408 // The surface wasn't in the current list, which means it was
1409 // removed already, which means it is in the purgatory,
1410 // and need to be removed from there.
1411 ssize_t idx = mLayerPurgatory.remove(l);
Steve Block3762c312012-01-06 19:20:56 +00001412 ALOGE_IF(idx < 0,
Mathias Agopian69608112011-05-19 15:38:14 -07001413 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001414 }
Steve Block3762c312012-01-06 19:20:56 +00001415 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopian69608112011-05-19 15:38:14 -07001416 "error removing layer=%p (%s)", l.get(), strerror(-err));
1417 }
1418 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419}
1420
Mathias Agopian439863f2011-06-28 19:09:31 -07001421uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001422 const sp<Client>& client,
Mathias Agopian439863f2011-06-28 19:09:31 -07001423 const layer_state_t& s)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 uint32_t flags = 0;
Mathias Agopian439863f2011-06-28 19:09:31 -07001426 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1427 if (layer != 0) {
1428 const uint32_t what = s.what;
1429 if (what & ePositionChanged) {
1430 if (layer->setPosition(s.x, s.y))
1431 flags |= eTraversalNeeded;
1432 }
1433 if (what & eLayerChanged) {
1434 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1435 if (layer->setLayer(s.z)) {
1436 mCurrentState.layersSortedByZ.removeAt(idx);
1437 mCurrentState.layersSortedByZ.add(layer);
1438 // we need traversal (state changed)
1439 // AND transaction (list changed)
1440 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001443 if (what & eSizeChanged) {
1444 if (layer->setSize(s.w, s.h)) {
1445 flags |= eTraversalNeeded;
Mathias Agopian439863f2011-06-28 19:09:31 -07001446 }
1447 }
1448 if (what & eAlphaChanged) {
1449 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1450 flags |= eTraversalNeeded;
1451 }
1452 if (what & eMatrixChanged) {
1453 if (layer->setMatrix(s.matrix))
1454 flags |= eTraversalNeeded;
1455 }
1456 if (what & eTransparentRegionChanged) {
1457 if (layer->setTransparentRegionHint(s.transparentRegion))
1458 flags |= eTraversalNeeded;
1459 }
1460 if (what & eVisibilityChanged) {
1461 if (layer->setFlags(s.flags, s.mask))
1462 flags |= eTraversalNeeded;
1463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001465 return flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466}
1467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468void SurfaceFlinger::screenReleased(int dpy)
1469{
1470 // this may be called by a signal handler, we can't do too much in here
1471 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian9a9dbd52012-01-31 18:24:27 -08001472 signalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473}
1474
1475void SurfaceFlinger::screenAcquired(int dpy)
1476{
1477 // this may be called by a signal handler, we can't do too much in here
1478 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian9a9dbd52012-01-31 18:24:27 -08001479 signalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480}
1481
1482status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1483{
Erik Gilling94720d72010-12-01 16:38:01 -08001484 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 char buffer[SIZE];
1486 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001487
1488 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 snprintf(buffer, SIZE, "Permission Denial: "
1490 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1491 IPCThreadState::self()->getCallingPid(),
1492 IPCThreadState::self()->getCallingUid());
1493 result.append(buffer);
1494 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001495 // Try to get the main lock, but don't insist if we can't
1496 // (this would indicate SF is stuck, but we want to be able to
1497 // print something in dumpsys).
1498 int retry = 3;
1499 while (mStateLock.tryLock()<0 && --retry>=0) {
1500 usleep(1000000);
1501 }
1502 const bool locked(retry >= 0);
1503 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001504 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001505 "SurfaceFlinger appears to be unresponsive, "
1506 "dumping anyways (no locks held)\n");
1507 result.append(buffer);
1508 }
1509
Mathias Agopian1d997952012-01-19 18:34:40 -08001510 bool dumpAll = true;
1511 size_t index = 0;
Mathias Agopian8d1b5e32012-01-28 22:31:55 -08001512 size_t numArgs = args.size();
1513 if (numArgs) {
Mathias Agopian1d997952012-01-19 18:34:40 -08001514 dumpAll = false;
Mathias Agopian8d1b5e32012-01-28 22:31:55 -08001515
1516 if ((index < numArgs) &&
1517 (args[index] == String16("--list"))) {
1518 index++;
1519 listLayersLocked(args, index, result, buffer, SIZE);
1520 }
1521
1522 if ((index < numArgs) &&
1523 (args[index] == String16("--latency"))) {
Mathias Agopian1d997952012-01-19 18:34:40 -08001524 index++;
1525 dumpStatsLocked(args, index, result, buffer, SIZE);
1526 }
Mathias Agopian8d1b5e32012-01-28 22:31:55 -08001527
1528 if ((index < numArgs) &&
1529 (args[index] == String16("--latency-clear"))) {
1530 index++;
1531 clearStatsLocked(args, index, result, buffer, SIZE);
1532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001534
Mathias Agopian1d997952012-01-19 18:34:40 -08001535 if (dumpAll) {
1536 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian06a61e22011-01-19 16:15:53 -08001537 }
1538
Mathias Agopiana8d49172009-08-26 16:36:26 -07001539 if (locked) {
1540 mStateLock.unlock();
1541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543 write(fd, result.string(), result.size());
1544 return NO_ERROR;
1545}
1546
Mathias Agopian8d1b5e32012-01-28 22:31:55 -08001547void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1548 String8& result, char* buffer, size_t SIZE) const
1549{
1550 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1551 const size_t count = currentLayers.size();
1552 for (size_t i=0 ; i<count ; i++) {
1553 const sp<LayerBase>& layer(currentLayers[i]);
1554 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1555 result.append(buffer);
1556 }
1557}
1558
Mathias Agopian1d997952012-01-19 18:34:40 -08001559void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1560 String8& result, char* buffer, size_t SIZE) const
1561{
1562 String8 name;
1563 if (index < args.size()) {
1564 name = String8(args[index]);
1565 index++;
1566 }
1567
1568 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1569 const size_t count = currentLayers.size();
1570 for (size_t i=0 ; i<count ; i++) {
1571 const sp<LayerBase>& layer(currentLayers[i]);
1572 if (name.isEmpty()) {
1573 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1574 result.append(buffer);
1575 }
1576 if (name.isEmpty() || (name == layer->getName())) {
1577 layer->dumpStats(result, buffer, SIZE);
1578 }
1579 }
1580}
1581
Mathias Agopian8d1b5e32012-01-28 22:31:55 -08001582void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1583 String8& result, char* buffer, size_t SIZE) const
1584{
1585 String8 name;
1586 if (index < args.size()) {
1587 name = String8(args[index]);
1588 index++;
1589 }
1590
1591 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1592 const size_t count = currentLayers.size();
1593 for (size_t i=0 ; i<count ; i++) {
1594 const sp<LayerBase>& layer(currentLayers[i]);
1595 if (name.isEmpty() || (name == layer->getName())) {
1596 layer->clearStats();
1597 }
1598 }
1599}
1600
Mathias Agopian1d997952012-01-19 18:34:40 -08001601void SurfaceFlinger::dumpAllLocked(
1602 String8& result, char* buffer, size_t SIZE) const
1603{
1604 // figure out if we're stuck somewhere
1605 const nsecs_t now = systemTime();
1606 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1607 const nsecs_t inTransaction(mDebugInTransaction);
1608 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1609 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1610
1611 /*
1612 * Dump the visible layer list
1613 */
1614 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1615 const size_t count = currentLayers.size();
1616 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1617 result.append(buffer);
1618 for (size_t i=0 ; i<count ; i++) {
1619 const sp<LayerBase>& layer(currentLayers[i]);
1620 layer->dump(result, buffer, SIZE);
1621 }
1622
1623 /*
1624 * Dump the layers in the purgatory
1625 */
1626
1627 const size_t purgatorySize = mLayerPurgatory.size();
1628 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1629 result.append(buffer);
1630 for (size_t i=0 ; i<purgatorySize ; i++) {
1631 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1632 layer->shortDump(result, buffer, SIZE);
1633 }
1634
1635 /*
1636 * Dump SurfaceFlinger global state
1637 */
1638
1639 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1640 result.append(buffer);
1641
1642 const GLExtensions& extensions(GLExtensions::getInstance());
1643 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1644 extensions.getVendor(),
1645 extensions.getRenderer(),
1646 extensions.getVersion());
1647 result.append(buffer);
1648
1649 snprintf(buffer, SIZE, "EGL : %s\n",
1650 eglQueryString(graphicPlane(0).getEGLDisplay(),
1651 EGL_VERSION_HW_ANDROID));
1652 result.append(buffer);
1653
1654 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1655 result.append(buffer);
1656
1657 mWormholeRegion.dump(result, "WormholeRegion");
1658 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1659 snprintf(buffer, SIZE,
1660 " orientation=%d, canDraw=%d\n",
1661 mCurrentState.orientation, hw.canDraw());
1662 result.append(buffer);
1663 snprintf(buffer, SIZE,
1664 " last eglSwapBuffers() time: %f us\n"
1665 " last transaction time : %f us\n"
Mathias Agopian077f29f2012-02-05 00:19:27 -08001666 " transaction-flags : %08x\n"
Mathias Agopian1d997952012-01-19 18:34:40 -08001667 " refresh-rate : %f fps\n"
1668 " x-dpi : %f\n"
1669 " y-dpi : %f\n",
1670 mLastSwapBufferTime/1000.0,
1671 mLastTransactionTime/1000.0,
Mathias Agopian077f29f2012-02-05 00:19:27 -08001672 mTransactionFlags,
Mathias Agopian1d997952012-01-19 18:34:40 -08001673 hw.getRefreshRate(),
1674 hw.getDpiX(),
1675 hw.getDpiY());
1676 result.append(buffer);
1677
1678 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1679 inSwapBuffersDuration/1000.0);
1680 result.append(buffer);
1681
1682 snprintf(buffer, SIZE, " transaction time: %f us\n",
1683 inTransactionDuration/1000.0);
1684 result.append(buffer);
1685
1686 /*
1687 * VSYNC state
1688 */
1689 mEventThread->dump(result, buffer, SIZE);
1690
1691 /*
1692 * Dump HWComposer state
1693 */
1694 HWComposer& hwc(hw.getHwComposer());
1695 snprintf(buffer, SIZE, "h/w composer state:\n");
1696 result.append(buffer);
1697 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1698 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1699 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1700 result.append(buffer);
1701 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1702
1703 /*
1704 * Dump gralloc state
1705 */
1706 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1707 alloc.dump(result);
1708 hw.dump(result);
1709}
1710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711status_t SurfaceFlinger::onTransact(
1712 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1713{
1714 switch (code) {
1715 case CREATE_CONNECTION:
Mathias Agopian439863f2011-06-28 19:09:31 -07001716 case SET_TRANSACTION_STATE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 case SET_ORIENTATION:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001719 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001720 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 {
1722 // codes that require permission check
1723 IPCThreadState* ipc = IPCThreadState::self();
1724 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001725 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001726 if ((uid != AID_GRAPHICS) &&
1727 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Block3762c312012-01-06 19:20:56 +00001728 ALOGE("Permission Denial: "
Mathias Agopian151e8592009-06-15 18:24:59 -07001729 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1730 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001732 break;
1733 }
1734 case CAPTURE_SCREEN:
1735 {
1736 // codes that require permission check
1737 IPCThreadState* ipc = IPCThreadState::self();
1738 const int pid = ipc->getCallingPid();
1739 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001740 if ((uid != AID_GRAPHICS) &&
1741 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Block3762c312012-01-06 19:20:56 +00001742 ALOGE("Permission Denial: "
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001743 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1744 return PERMISSION_DENIED;
1745 }
1746 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1751 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001752 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kastene80a4cc2011-12-15 09:51:17 -08001753 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001754 IPCThreadState* ipc = IPCThreadState::self();
1755 const int pid = ipc->getCallingPid();
1756 const int uid = ipc->getCallingUid();
Steve Block3762c312012-01-06 19:20:56 +00001757 ALOGE("Permission Denial: "
Mathias Agopian151e8592009-06-15 18:24:59 -07001758 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 return PERMISSION_DENIED;
1760 }
1761 int n;
1762 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001763 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001764 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 return NO_ERROR;
1766 case 1002: // SHOW_UPDATES
1767 n = data.readInt32();
1768 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001769 invalidateHwcGeometry();
1770 repaintEverything();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 return NO_ERROR;
1772 case 1003: // SHOW_BACKGROUND
1773 n = data.readInt32();
1774 mDebugBackground = n ? 1 : 0;
1775 return NO_ERROR;
1776 case 1004:{ // repaint everything
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001777 repaintEverything();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001778 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001780 case 1005:{ // force transaction
1781 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1782 return NO_ERROR;
1783 }
Mathias Agopianf37d8fc2012-02-23 20:05:39 -08001784 case 1006:{ // send empty update
1785 signalRefresh();
1786 return NO_ERROR;
1787 }
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001788 case 1008: // toggle use of hw composer
1789 n = data.readInt32();
1790 mDebugDisableHWC = n ? 1 : 0;
1791 invalidateHwcGeometry();
1792 repaintEverything();
1793 return NO_ERROR;
Mathias Agopian2143fe02011-08-23 18:03:18 -07001794 case 1009: // toggle use of transform hint
1795 n = data.readInt32();
1796 mDebugDisableTransformHint = n ? 1 : 0;
1797 invalidateHwcGeometry();
1798 repaintEverything();
1799 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001801 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 reply->writeInt32(0);
1803 reply->writeInt32(mDebugRegion);
1804 reply->writeInt32(mDebugBackground);
Dianne Hackborn5989e8e72012-02-06 21:21:05 -08001805 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 return NO_ERROR;
1807 case 1013: {
1808 Mutex::Autolock _l(mStateLock);
1809 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1810 reply->writeInt32(hw.getPageFlipCount());
1811 }
1812 return NO_ERROR;
1813 }
1814 }
1815 return err;
1816}
1817
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001818void SurfaceFlinger::repaintEverything() {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001819 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian6497eab2011-10-21 15:18:28 -07001820 const Rect bounds(hw.getBounds());
1821 setInvalidateRegion(Region(bounds));
Mathias Agopian9a9dbd52012-01-31 18:24:27 -08001822 signalTransaction();
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001823}
1824
Mathias Agopian6497eab2011-10-21 15:18:28 -07001825void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1826 Mutex::Autolock _l(mInvalidateLock);
1827 mInvalidateRegion = reg;
1828}
1829
1830Region SurfaceFlinger::getAndClearInvalidateRegion() {
1831 Mutex::Autolock _l(mInvalidateLock);
1832 Region reg(mInvalidateRegion);
1833 mInvalidateRegion.clear();
1834 return reg;
1835}
1836
Mathias Agopianaab758e2010-10-11 12:37:43 -07001837// ---------------------------------------------------------------------------
1838
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001839status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1840 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1841{
1842 Mutex::Autolock _l(mStateLock);
1843 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1844}
1845
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001846status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1847 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001848{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001849 if (!GLExtensions::getInstance().haveFramebufferObject())
1850 return INVALID_OPERATION;
1851
1852 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001853 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001854 const uint32_t hw_w = hw.getWidth();
1855 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001856 GLfloat u = 1;
1857 GLfloat v = 1;
1858
1859 // make sure to clear all GL error flags
1860 while ( glGetError() != GL_NO_ERROR ) ;
1861
1862 // create a FBO
1863 GLuint name, tname;
1864 glGenTextures(1, &tname);
1865 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001866 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1867 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001868 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001869 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001870 GLint tw = (2 << (31 - clz(hw_w)));
1871 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001872 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1873 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001874 u = GLfloat(hw_w) / tw;
1875 v = GLfloat(hw_h) / th;
1876 }
1877 glGenFramebuffersOES(1, &name);
1878 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001879 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1880 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001881
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001882 // redraw the screen entirely...
Mathias Agopian9044ef02011-10-18 14:49:27 -07001883 glDisable(GL_TEXTURE_EXTERNAL_OES);
1884 glDisable(GL_TEXTURE_2D);
Mathias Agopian01d650e2011-10-26 15:11:59 -07001885 glDisable(GL_SCISSOR_TEST);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001886 glClearColor(0,0,0,1);
1887 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian01d650e2011-10-26 15:11:59 -07001888 glEnable(GL_SCISSOR_TEST);
Mathias Agopianb946a562011-10-10 19:02:07 -07001889 glMatrixMode(GL_MODELVIEW);
1890 glLoadIdentity();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001891 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1892 const size_t count = layers.size();
1893 for (size_t i=0 ; i<count ; ++i) {
1894 const sp<LayerBase>& layer(layers[i]);
1895 layer->drawForSreenShot();
1896 }
1897
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001898 hw.compositionComplete();
1899
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001900 // back to main framebuffer
1901 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1902 glDisable(GL_SCISSOR_TEST);
1903 glDeleteFramebuffersOES(1, &name);
1904
1905 *textureName = tname;
1906 *uOut = u;
1907 *vOut = v;
1908 return NO_ERROR;
1909}
1910
1911// ---------------------------------------------------------------------------
1912
1913status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1914{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001915 // get screen geometry
1916 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1917 const uint32_t hw_w = hw.getWidth();
1918 const uint32_t hw_h = hw.getHeight();
Mathias Agopianb946a562011-10-10 19:02:07 -07001919 const Region screenBounds(hw.getBounds());
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001920
1921 GLfloat u, v;
1922 GLuint tname;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001923 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001924 if (result != NO_ERROR) {
1925 return result;
1926 }
1927
1928 GLfloat vtx[8];
Mathias Agopianb946a562011-10-10 19:02:07 -07001929 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001930 glBindTexture(GL_TEXTURE_2D, tname);
1931 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1932 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1933 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Golde8400872012-01-13 00:36:45 -08001934 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1935 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001936 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1937 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1938 glVertexPointer(2, GL_FLOAT, 0, vtx);
1939
Mathias Agopian3a831d22011-07-07 17:30:31 -07001940 /*
1941 * Texture coordinate mapping
1942 *
1943 * u
1944 * 1 +----------+---+
1945 * | | | | image is inverted
1946 * | V | | w.r.t. the texture
1947 * 1-v +----------+ | coordinates
1948 * | |
1949 * | |
1950 * | |
1951 * 0 +--------------+
1952 * 0 1
1953 *
1954 */
1955
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001956 class s_curve_interpolator {
1957 const float nbFrames, s, v;
1958 public:
1959 s_curve_interpolator(int nbFrames, float s)
1960 : nbFrames(1.0f / (nbFrames-1)), s(s),
1961 v(1.0f + expf(-s + 0.5f*s)) {
1962 }
1963 float operator()(int f) {
1964 const float x = f * nbFrames;
1965 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1966 }
1967 };
1968
1969 class v_stretch {
1970 const GLfloat hw_w, hw_h;
1971 public:
1972 v_stretch(uint32_t hw_w, uint32_t hw_h)
1973 : hw_w(hw_w), hw_h(hw_h) {
1974 }
1975 void operator()(GLfloat* vtx, float v) {
1976 const GLfloat w = hw_w + (hw_w * v);
1977 const GLfloat h = hw_h - (hw_h * v);
1978 const GLfloat x = (hw_w - w) * 0.5f;
1979 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001980 vtx[0] = x; vtx[1] = y;
1981 vtx[2] = x; vtx[3] = y + h;
1982 vtx[4] = x + w; vtx[5] = y + h;
1983 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001984 }
1985 };
1986
1987 class h_stretch {
1988 const GLfloat hw_w, hw_h;
1989 public:
1990 h_stretch(uint32_t hw_w, uint32_t hw_h)
1991 : hw_w(hw_w), hw_h(hw_h) {
1992 }
1993 void operator()(GLfloat* vtx, float v) {
1994 const GLfloat w = hw_w - (hw_w * v);
1995 const GLfloat h = 1.0f;
1996 const GLfloat x = (hw_w - w) * 0.5f;
1997 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001998 vtx[0] = x; vtx[1] = y;
1999 vtx[2] = x; vtx[3] = y + h;
2000 vtx[4] = x + w; vtx[5] = y + h;
2001 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002002 }
2003 };
2004
2005 // the full animation is 24 frames
Mathias Agopian3a831d22011-07-07 17:30:31 -07002006 char value[PROPERTY_VALUE_MAX];
2007 property_get("debug.sf.electron_frames", value, "24");
2008 int nbFrames = (atoi(value) + 1) >> 1;
2009 if (nbFrames <= 0) // just in case
2010 nbFrames = 24;
2011
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002012 s_curve_interpolator itr(nbFrames, 7.5f);
2013 s_curve_interpolator itg(nbFrames, 8.0f);
2014 s_curve_interpolator itb(nbFrames, 8.5f);
2015
2016 v_stretch vverts(hw_w, hw_h);
Mathias Agopianb946a562011-10-10 19:02:07 -07002017
2018 glMatrixMode(GL_TEXTURE);
2019 glLoadIdentity();
2020 glMatrixMode(GL_MODELVIEW);
2021 glLoadIdentity();
2022
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002023 glEnable(GL_BLEND);
2024 glBlendFunc(GL_ONE, GL_ONE);
2025 for (int i=0 ; i<nbFrames ; i++) {
2026 float x, y, w, h;
2027 const float vr = itr(i);
2028 const float vg = itg(i);
2029 const float vb = itb(i);
2030
2031 // clear screen
2032 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002033 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002034 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002035
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002036 // draw the red plane
2037 vverts(vtx, vr);
2038 glColorMask(1,0,0,1);
2039 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002040
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002041 // draw the green plane
2042 vverts(vtx, vg);
2043 glColorMask(0,1,0,1);
2044 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002045
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002046 // draw the blue plane
2047 vverts(vtx, vb);
2048 glColorMask(0,0,1,1);
2049 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002050
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002051 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07002052 glDisable(GL_TEXTURE_2D);
2053 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002054 glColor4f(vg, vg, vg, 1);
2055 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2056 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002057 }
2058
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002059 h_stretch hverts(hw_w, hw_h);
2060 glDisable(GL_BLEND);
2061 glDisable(GL_TEXTURE_2D);
2062 glColorMask(1,1,1,1);
2063 for (int i=0 ; i<nbFrames ; i++) {
2064 const float v = itg(i);
2065 hverts(vtx, v);
2066 glClear(GL_COLOR_BUFFER_BIT);
2067 glColor4f(1-v, 1-v, 1-v, 1);
2068 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2069 hw.flip(screenBounds);
2070 }
2071
2072 glColorMask(1,1,1,1);
2073 glEnable(GL_SCISSOR_TEST);
2074 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2075 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002076 glDisable(GL_TEXTURE_2D);
Mathias Agopian9044ef02011-10-18 14:49:27 -07002077 glDisable(GL_BLEND);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002078 return NO_ERROR;
2079}
2080
2081status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2082{
2083 status_t result = PERMISSION_DENIED;
2084
2085 if (!GLExtensions::getInstance().haveFramebufferObject())
2086 return INVALID_OPERATION;
2087
2088
2089 // get screen geometry
2090 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2091 const uint32_t hw_w = hw.getWidth();
2092 const uint32_t hw_h = hw.getHeight();
2093 const Region screenBounds(hw.bounds());
2094
2095 GLfloat u, v;
2096 GLuint tname;
2097 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2098 if (result != NO_ERROR) {
2099 return result;
2100 }
2101
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002102 GLfloat vtx[8];
2103 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002104 glBindTexture(GL_TEXTURE_2D, tname);
2105 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2106 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2107 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Golde8400872012-01-13 00:36:45 -08002108 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2109 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002110 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2111 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2112 glVertexPointer(2, GL_FLOAT, 0, vtx);
2113
2114 class s_curve_interpolator {
2115 const float nbFrames, s, v;
2116 public:
2117 s_curve_interpolator(int nbFrames, float s)
2118 : nbFrames(1.0f / (nbFrames-1)), s(s),
2119 v(1.0f + expf(-s + 0.5f*s)) {
2120 }
2121 float operator()(int f) {
2122 const float x = f * nbFrames;
2123 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2124 }
2125 };
2126
2127 class v_stretch {
2128 const GLfloat hw_w, hw_h;
2129 public:
2130 v_stretch(uint32_t hw_w, uint32_t hw_h)
2131 : hw_w(hw_w), hw_h(hw_h) {
2132 }
2133 void operator()(GLfloat* vtx, float v) {
2134 const GLfloat w = hw_w + (hw_w * v);
2135 const GLfloat h = hw_h - (hw_h * v);
2136 const GLfloat x = (hw_w - w) * 0.5f;
2137 const GLfloat y = (hw_h - h) * 0.5f;
2138 vtx[0] = x; vtx[1] = y;
2139 vtx[2] = x; vtx[3] = y + h;
2140 vtx[4] = x + w; vtx[5] = y + h;
2141 vtx[6] = x + w; vtx[7] = y;
2142 }
2143 };
2144
2145 class h_stretch {
2146 const GLfloat hw_w, hw_h;
2147 public:
2148 h_stretch(uint32_t hw_w, uint32_t hw_h)
2149 : hw_w(hw_w), hw_h(hw_h) {
2150 }
2151 void operator()(GLfloat* vtx, float v) {
2152 const GLfloat w = hw_w - (hw_w * v);
2153 const GLfloat h = 1.0f;
2154 const GLfloat x = (hw_w - w) * 0.5f;
2155 const GLfloat y = (hw_h - h) * 0.5f;
2156 vtx[0] = x; vtx[1] = y;
2157 vtx[2] = x; vtx[3] = y + h;
2158 vtx[4] = x + w; vtx[5] = y + h;
2159 vtx[6] = x + w; vtx[7] = y;
2160 }
2161 };
2162
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002163 // the full animation is 12 frames
2164 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002165 s_curve_interpolator itr(nbFrames, 7.5f);
2166 s_curve_interpolator itg(nbFrames, 8.0f);
2167 s_curve_interpolator itb(nbFrames, 8.5f);
2168
2169 h_stretch hverts(hw_w, hw_h);
2170 glDisable(GL_BLEND);
2171 glDisable(GL_TEXTURE_2D);
2172 glColorMask(1,1,1,1);
2173 for (int i=nbFrames-1 ; i>=0 ; i--) {
2174 const float v = itg(i);
2175 hverts(vtx, v);
2176 glClear(GL_COLOR_BUFFER_BIT);
2177 glColor4f(1-v, 1-v, 1-v, 1);
2178 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2179 hw.flip(screenBounds);
2180 }
2181
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002182 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002183 v_stretch vverts(hw_w, hw_h);
2184 glEnable(GL_BLEND);
2185 glBlendFunc(GL_ONE, GL_ONE);
2186 for (int i=nbFrames-1 ; i>=0 ; i--) {
2187 float x, y, w, h;
2188 const float vr = itr(i);
2189 const float vg = itg(i);
2190 const float vb = itb(i);
2191
2192 // clear screen
2193 glColorMask(1,1,1,1);
2194 glClear(GL_COLOR_BUFFER_BIT);
2195 glEnable(GL_TEXTURE_2D);
2196
2197 // draw the red plane
2198 vverts(vtx, vr);
2199 glColorMask(1,0,0,1);
2200 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2201
2202 // draw the green plane
2203 vverts(vtx, vg);
2204 glColorMask(0,1,0,1);
2205 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2206
2207 // draw the blue plane
2208 vverts(vtx, vb);
2209 glColorMask(0,0,1,1);
2210 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2211
2212 hw.flip(screenBounds);
2213 }
2214
2215 glColorMask(1,1,1,1);
2216 glEnable(GL_SCISSOR_TEST);
2217 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002218 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002219 glDisable(GL_TEXTURE_2D);
Mathias Agopian9044ef02011-10-18 14:49:27 -07002220 glDisable(GL_BLEND);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002221
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002222 return NO_ERROR;
2223}
2224
2225// ---------------------------------------------------------------------------
2226
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002227status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002228{
2229 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2230 if (!hw.canDraw()) {
2231 // we're already off
2232 return NO_ERROR;
2233 }
Mathias Agopian7f972582011-09-02 12:22:39 -07002234
2235 // turn off hwc while we're doing the animation
2236 hw.getHwComposer().disable();
2237 // and make sure to turn it back on (if needed) next time we compose
2238 invalidateHwcGeometry();
2239
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002240 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2241 electronBeamOffAnimationImplLocked();
2242 }
2243
2244 // always clear the whole screen at the end of the animation
2245 glClearColor(0,0,0,1);
2246 glDisable(GL_SCISSOR_TEST);
2247 glClear(GL_COLOR_BUFFER_BIT);
2248 glEnable(GL_SCISSOR_TEST);
2249 hw.flip( Region(hw.bounds()) );
2250
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002251 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002252}
2253
2254status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2255{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002256 class MessageTurnElectronBeamOff : public MessageBase {
2257 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002258 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002259 status_t result;
2260 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002261 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2262 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002263 }
2264 status_t getResult() const {
2265 return result;
2266 }
2267 virtual bool handler() {
2268 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002269 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002270 return true;
2271 }
2272 };
2273
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002274 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002275 status_t res = postMessageSync(msg);
2276 if (res == NO_ERROR) {
2277 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002278
2279 // work-around: when the power-manager calls us we activate the
2280 // animation. eventually, the "on" animation will be called
2281 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002282 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002283 }
2284 return res;
2285}
2286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002288
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002289status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002290{
2291 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2292 if (hw.canDraw()) {
2293 // we're already on
2294 return NO_ERROR;
2295 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002296 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2297 electronBeamOnAnimationImplLocked();
2298 }
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002299
2300 // make sure to redraw the whole screen when the animation is done
2301 mDirtyRegion.set(hw.bounds());
Mathias Agopian9a9dbd52012-01-31 18:24:27 -08002302 signalTransaction();
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002303
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002304 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002305}
2306
2307status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2308{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002309 class MessageTurnElectronBeamOn : public MessageBase {
2310 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002311 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002312 status_t result;
2313 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002314 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2315 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002316 }
2317 status_t getResult() const {
2318 return result;
2319 }
2320 virtual bool handler() {
2321 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002322 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002323 return true;
2324 }
2325 };
2326
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002327 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002328 return NO_ERROR;
2329}
2330
2331// ---------------------------------------------------------------------------
2332
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002333status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2334 sp<IMemoryHeap>* heap,
2335 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002336 uint32_t sw, uint32_t sh,
2337 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002338{
2339 status_t result = PERMISSION_DENIED;
2340
2341 // only one display supported for now
Glenn Kastene80a4cc2011-12-15 09:51:17 -08002342 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002343 return BAD_VALUE;
2344
2345 if (!GLExtensions::getInstance().haveFramebufferObject())
2346 return INVALID_OPERATION;
2347
2348 // get screen geometry
2349 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2350 const uint32_t hw_w = hw.getWidth();
2351 const uint32_t hw_h = hw.getHeight();
2352
2353 if ((sw > hw_w) || (sh > hw_h))
2354 return BAD_VALUE;
2355
2356 sw = (!sw) ? hw_w : sw;
2357 sh = (!sh) ? hw_h : sh;
2358 const size_t size = sw * sh * 4;
2359
Steve Block5baa3a62011-12-20 16:23:08 +00002360 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian32ae0942011-03-02 18:45:50 -08002361 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002362
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002363 // make sure to clear all GL error flags
2364 while ( glGetError() != GL_NO_ERROR ) ;
2365
2366 // create a FBO
2367 GLuint name, tname;
2368 glGenRenderbuffersOES(1, &tname);
2369 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2370 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2371 glGenFramebuffersOES(1, &name);
2372 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2373 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2374 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2375
2376 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002377
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002378 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2379
2380 // invert everything, b/c glReadPixel() below will invert the FB
2381 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002382 glScissor(0, 0, sw, sh);
Mathias Agopian3a831d22011-07-07 17:30:31 -07002383 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002384 glMatrixMode(GL_PROJECTION);
2385 glPushMatrix();
2386 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -07002387 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002388 glMatrixMode(GL_MODELVIEW);
2389
2390 // redraw the screen entirely...
2391 glClearColor(0,0,0,1);
2392 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002393
Jamie Gennis830d0832011-10-07 14:51:16 -07002394 const LayerVector& layers(mDrawingState.layersSortedByZ);
2395 const size_t count = layers.size();
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002396 for (size_t i=0 ; i<count ; ++i) {
2397 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianec49d892011-08-25 14:36:43 -07002398 const uint32_t flags = layer->drawingState().flags;
2399 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2400 const uint32_t z = layer->drawingState().z;
2401 if (z >= minLayerZ && z <= maxLayerZ) {
2402 layer->drawForSreenShot();
2403 }
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002404 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002405 }
2406
2407 // XXX: this is needed on tegra
Mathias Agopian3a831d22011-07-07 17:30:31 -07002408 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002409 glScissor(0, 0, sw, sh);
2410
2411 // check for errors and return screen capture
2412 if (glGetError() != GL_NO_ERROR) {
2413 // error while rendering
2414 result = INVALID_OPERATION;
2415 } else {
2416 // allocate shared memory large enough to hold the
2417 // screen capture
2418 sp<MemoryHeapBase> base(
2419 new MemoryHeapBase(size, 0, "screen-capture") );
2420 void* const ptr = base->getBase();
2421 if (ptr) {
2422 // capture the screen with glReadPixels()
2423 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2424 if (glGetError() == GL_NO_ERROR) {
2425 *heap = base;
2426 *w = sw;
2427 *h = sh;
2428 *f = PIXEL_FORMAT_RGBA_8888;
2429 result = NO_ERROR;
2430 }
2431 } else {
2432 result = NO_MEMORY;
2433 }
2434 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002435 glEnable(GL_SCISSOR_TEST);
2436 glViewport(0, 0, hw_w, hw_h);
2437 glMatrixMode(GL_PROJECTION);
2438 glPopMatrix();
2439 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002440 } else {
2441 result = BAD_VALUE;
2442 }
2443
2444 // release FBO resources
2445 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2446 glDeleteRenderbuffersOES(1, &tname);
2447 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002448
2449 hw.compositionComplete();
2450
Steve Block5baa3a62011-12-20 16:23:08 +00002451 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002452
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002453 return result;
2454}
2455
2456
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002457status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2458 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002459 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002460 uint32_t sw, uint32_t sh,
2461 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002462{
2463 // only one display supported for now
Glenn Kastene80a4cc2011-12-15 09:51:17 -08002464 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002465 return BAD_VALUE;
2466
2467 if (!GLExtensions::getInstance().haveFramebufferObject())
2468 return INVALID_OPERATION;
2469
2470 class MessageCaptureScreen : public MessageBase {
2471 SurfaceFlinger* flinger;
2472 DisplayID dpy;
2473 sp<IMemoryHeap>* heap;
2474 uint32_t* w;
2475 uint32_t* h;
2476 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002477 uint32_t sw;
2478 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002479 uint32_t minLayerZ;
2480 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002481 status_t result;
2482 public:
2483 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002484 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002485 uint32_t sw, uint32_t sh,
2486 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002487 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002488 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2489 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2490 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002491 {
2492 }
2493 status_t getResult() const {
2494 return result;
2495 }
2496 virtual bool handler() {
2497 Mutex::Autolock _l(flinger->mStateLock);
2498
2499 // if we have secure windows, never allow the screen capture
2500 if (flinger->mSecureFrameBuffer)
2501 return true;
2502
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002503 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002504 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002505
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002506 return true;
2507 }
2508 };
2509
2510 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002511 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002512 status_t res = postMessageSync(msg);
2513 if (res == NO_ERROR) {
2514 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2515 }
2516 return res;
2517}
2518
2519// ---------------------------------------------------------------------------
2520
Mathias Agopian7623da42010-06-01 15:12:58 -07002521sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2522{
2523 sp<Layer> result;
2524 Mutex::Autolock _l(mStateLock);
2525 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2526 return result;
2527}
2528
2529// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530
Mathias Agopian593c05c2010-06-02 23:28:45 -07002531Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002532 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534}
2535
Mathias Agopian593c05c2010-06-02 23:28:45 -07002536Client::~Client()
2537{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002538 const size_t count = mLayers.size();
2539 for (size_t i=0 ; i<count ; i++) {
2540 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2541 if (layer != 0) {
2542 mFlinger->removeLayer(layer);
2543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 }
2545}
2546
Mathias Agopian593c05c2010-06-02 23:28:45 -07002547status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002548 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549}
Mathias Agopian1473f462009-04-10 14:24:30 -07002550
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002551size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002552{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002553 Mutex::Autolock _l(mLock);
2554 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002555 mLayers.add(name, layer);
2556 return name;
2557}
2558
Mathias Agopian7623da42010-06-01 15:12:58 -07002559void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002560{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002561 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002562 // we do a linear search here, because this doesn't happen often
2563 const size_t count = mLayers.size();
2564 for (size_t i=0 ; i<count ; i++) {
2565 if (mLayers.valueAt(i) == layer) {
2566 mLayers.removeItemsAt(i, 1);
2567 break;
2568 }
2569 }
2570}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002571sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2572{
2573 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002574 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002575 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002576 if (layer != 0) {
2577 lbc = layer.promote();
Steve Block3762c312012-01-06 19:20:56 +00002578 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002579 }
2580 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581}
2582
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002583
2584status_t Client::onTransact(
2585 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2586{
2587 // these must be checked
2588 IPCThreadState* ipc = IPCThreadState::self();
2589 const int pid = ipc->getCallingPid();
2590 const int uid = ipc->getCallingUid();
2591 const int self_pid = getpid();
Glenn Kastene80a4cc2011-12-15 09:51:17 -08002592 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002593 // we're called from a different process, do the real check
Mathias Agopian0dd593f2011-06-27 16:05:52 -07002594 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002595 {
Steve Block3762c312012-01-06 19:20:56 +00002596 ALOGE("Permission Denial: "
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002597 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2598 return PERMISSION_DENIED;
2599 }
2600 }
2601 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopian7623da42010-06-01 15:12:58 -07002602}
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002603
2604
Mathias Agopian593c05c2010-06-02 23:28:45 -07002605sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002606 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002607 const String8& name,
2608 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 uint32_t flags)
2610{
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002611 /*
2612 * createSurface must be called from the GL thread so that it can
2613 * have access to the GL context.
2614 */
2615
2616 class MessageCreateSurface : public MessageBase {
2617 sp<ISurface> result;
2618 SurfaceFlinger* flinger;
2619 ISurfaceComposerClient::surface_data_t* params;
2620 Client* client;
2621 const String8& name;
2622 DisplayID display;
2623 uint32_t w, h;
2624 PixelFormat format;
2625 uint32_t flags;
2626 public:
2627 MessageCreateSurface(SurfaceFlinger* flinger,
2628 ISurfaceComposerClient::surface_data_t* params,
2629 const String8& name, Client* client,
2630 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2631 uint32_t flags)
2632 : flinger(flinger), params(params), client(client), name(name),
2633 display(display), w(w), h(h), format(format), flags(flags)
2634 {
2635 }
2636 sp<ISurface> getResult() const { return result; }
2637 virtual bool handler() {
2638 result = flinger->createSurface(params, name, client,
2639 display, w, h, format, flags);
2640 return true;
2641 }
2642 };
2643
2644 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2645 params, name, this, display, w, h, format, flags);
2646 mFlinger->postMessageSync(msg);
2647 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002649status_t Client::destroySurface(SurfaceID sid) {
2650 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652
2653// ---------------------------------------------------------------------------
2654
Jamie Gennisf7acf162011-01-12 18:30:40 -08002655GraphicBufferAlloc::GraphicBufferAlloc() {}
2656
2657GraphicBufferAlloc::~GraphicBufferAlloc() {}
2658
2659sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002660 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennisf7acf162011-01-12 18:30:40 -08002661 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2662 status_t err = graphicBuffer->initCheck();
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002663 *error = err;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002664 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002665 if (err == NO_MEMORY) {
2666 GraphicBuffer::dumpAllocationsToSystemLog();
2667 }
Steve Block3762c312012-01-06 19:20:56 +00002668 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002669 "failed (%s), handle=%p",
2670 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennisf7acf162011-01-12 18:30:40 -08002671 return 0;
2672 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002673 return graphicBuffer;
2674}
2675
Jamie Gennisf7acf162011-01-12 18:30:40 -08002676// ---------------------------------------------------------------------------
2677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678GraphicPlane::GraphicPlane()
2679 : mHw(0)
2680{
2681}
2682
2683GraphicPlane::~GraphicPlane() {
2684 delete mHw;
2685}
2686
2687bool GraphicPlane::initialized() const {
2688 return mHw ? true : false;
2689}
2690
Mathias Agopian66c77a52010-02-08 15:49:35 -08002691int GraphicPlane::getWidth() const {
2692 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693}
2694
Mathias Agopian66c77a52010-02-08 15:49:35 -08002695int GraphicPlane::getHeight() const {
2696 return mHeight;
2697}
2698
2699void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2700{
2701 mHw = hw;
2702
2703 // initialize the display orientation transform.
2704 // it's a constant that should come from the display driver.
2705 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2706 char property[PROPERTY_VALUE_MAX];
2707 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2708 //displayOrientation
2709 switch (atoi(property)) {
2710 case 90:
2711 displayOrientation = ISurfaceComposer::eOrientation90;
2712 break;
2713 case 270:
2714 displayOrientation = ISurfaceComposer::eOrientation270;
2715 break;
2716 }
2717 }
2718
2719 const float w = hw->getWidth();
2720 const float h = hw->getHeight();
2721 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2722 &mDisplayTransform);
2723 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2724 mDisplayWidth = h;
2725 mDisplayHeight = w;
2726 } else {
2727 mDisplayWidth = w;
2728 mDisplayHeight = h;
2729 }
2730
2731 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732}
2733
2734status_t GraphicPlane::orientationToTransfrom(
2735 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002736{
2737 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 switch (orientation) {
2739 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002740 flags = Transform::ROT_0;
2741 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002743 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 break;
2745 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002746 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 break;
2748 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002749 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 break;
2751 default:
2752 return BAD_VALUE;
2753 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002754 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 return NO_ERROR;
2756}
2757
2758status_t GraphicPlane::setOrientation(int orientation)
2759{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 // If the rotation can be handled in hardware, this is where
2761 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002762
2763 const DisplayHardware& hw(displayHardware());
2764 const float w = mDisplayWidth;
2765 const float h = mDisplayHeight;
2766 mWidth = int(w);
2767 mHeight = int(h);
2768
2769 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002770 GraphicPlane::orientationToTransfrom(orientation, w, h,
2771 &orientationTransform);
2772 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2773 mWidth = int(h);
2774 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002776
Mathias Agopian3552f532009-03-27 17:58:20 -07002777 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002778 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 return NO_ERROR;
2780}
2781
2782const DisplayHardware& GraphicPlane::displayHardware() const {
2783 return *mHw;
2784}
2785
Mathias Agopianaab758e2010-10-11 12:37:43 -07002786DisplayHardware& GraphicPlane::editDisplayHardware() {
2787 return *mHw;
2788}
2789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790const Transform& GraphicPlane::transform() const {
2791 return mGlobalTransform;
2792}
2793
Mathias Agopian1473f462009-04-10 14:24:30 -07002794EGLDisplay GraphicPlane::getEGLDisplay() const {
2795 return mHw->getEGLDisplay();
2796}
2797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798// ---------------------------------------------------------------------------
2799
2800}; // namespace android