blob: fe39584298df8be72e62159c103662d0f49c39e8 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080053#include "DisplayEventConnection.h"
54#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Glenn Kasten1db13d72011-12-19 13:55:34 -080064#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include <private/gui/SharedBufferStack.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070099 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 mConsoleSignals(0),
101 mSecureFrameBuffer(0)
102{
103 init();
104}
105
106void SurfaceFlinger::init()
107{
Steve Blocka19954a2012-01-04 20:05:49 +0000108 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 // debugging stuff...
111 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 property_get("debug.sf.showupdates", value, "0");
114 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 property_get("debug.sf.showbackground", value, "0");
117 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
Colin Cross3854ed52012-03-23 14:17:18 -0700119#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120 property_get("debug.sf.ddms", value, "0");
121 mDebugDDMS = atoi(value);
122 if (mDebugDDMS) {
123 DdmConnection::start(getServiceName());
124 }
Colin Cross3854ed52012-03-23 14:17:18 -0700125#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126
Steve Blocka19954a2012-01-04 20:05:49 +0000127 ALOGI_IF(mDebugRegion, "showupdates enabled");
128 ALOGI_IF(mDebugBackground, "showbackground enabled");
129 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800132void SurfaceFlinger::onFirstRef()
133{
134 mEventQueue.init(this);
135
136 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
137
138 // Wait for the main thread to be done with its initialization
139 mReadyToRunBarrier.wait();
140}
141
142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143SurfaceFlinger::~SurfaceFlinger()
144{
145 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146}
147
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800148void SurfaceFlinger::binderDied(const wp<IBinder>& who)
149{
150 // the window manager died on us. prepare its eulogy.
151
152 // reset screen orientation
153 Vector<ComposerState> state;
154 setTransactionState(state, eOrientationDefault, 0);
155
156 // restart the boot-animation
157 property_set("ctl.start", "bootanim");
158}
159
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700160sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700162 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163}
164
Mathias Agopian7e27f052010-05-28 14:22:23 -0700165sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166{
Mathias Agopian96f08192010-06-02 23:28:45 -0700167 sp<ISurfaceComposerClient> bclient;
168 sp<Client> client(new Client(this));
169 status_t err = client->initCheck();
170 if (err == NO_ERROR) {
171 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 return bclient;
174}
175
Jamie Gennis9a78c902011-01-12 18:30:40 -0800176sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
177{
178 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
179 return gba;
180}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
183{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000184 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 const GraphicPlane& plane(mGraphicPlanes[dpy]);
186 return plane;
187}
188
189GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
190{
191 return const_cast<GraphicPlane&>(
192 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
193}
194
195void SurfaceFlinger::bootFinished()
196{
197 const nsecs_t now = systemTime();
198 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000199 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700200 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700201
202 // wait patiently for the window manager death
203 const String16 name("window");
204 sp<IBinder> window(defaultServiceManager()->getService(name));
205 if (window != 0) {
206 window->linkToDeath(this);
207 }
208
209 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700210 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213static inline uint16_t pack565(int r, int g, int b) {
214 return (r<<11)|(g<<5)|b;
215}
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217status_t SurfaceFlinger::readyToRun()
218{
Steve Blocka19954a2012-01-04 20:05:49 +0000219 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 "Initializing graphics H/W...");
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 // we only support one display currently
223 int dpy = 0;
224
225 {
226 // initialize the main display
227 GraphicPlane& plane(graphicPlane(dpy));
228 DisplayHardware* const hw = new DisplayHardware(this, dpy);
229 plane.setDisplayHardware(hw);
230 }
231
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700232 // create the shared control-block
233 mServerHeap = new MemoryHeapBase(4096,
234 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000235 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700236
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700237 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000238 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700239
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700240 new(mServerCblk) surface_flinger_cblk_t;
241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 // initialize primary screen
243 // (other display should be initialized in the same manner, but
244 // asynchronously, as they could come and go. None of this is supported
245 // yet).
246 const GraphicPlane& plane(graphicPlane(dpy));
247 const DisplayHardware& hw = plane.displayHardware();
248 const uint32_t w = hw.getWidth();
249 const uint32_t h = hw.getHeight();
250 const uint32_t f = hw.getFormat();
251 hw.makeCurrent();
252
253 // initialize the shared control block
254 mServerCblk->connected |= 1<<dpy;
255 display_cblk_t* dcblk = mServerCblk->displays + dpy;
256 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800257 dcblk->w = plane.getWidth();
258 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 dcblk->format = f;
260 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
261 dcblk->xdpi = hw.getDpiX();
262 dcblk->ydpi = hw.getDpiY();
263 dcblk->fps = hw.getRefreshRate();
264 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265
266 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700268 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 glEnableClientState(GL_VERTEX_ARRAY);
270 glEnable(GL_SCISSOR_TEST);
271 glShadeModel(GL_FLAT);
272 glDisable(GL_DITHER);
273 glDisable(GL_CULL_FACE);
274
275 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
276 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 glGenTextures(1, &mWormholeTexName);
279 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700285 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
286
287 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
288 glGenTextures(1, &mProtectedTexName);
289 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
293 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
295 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
297 glViewport(0, 0, w, h);
298 glMatrixMode(GL_PROJECTION);
299 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700300 // put the origin in the left-bottom corner
301 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800303
304 // start the EventThread
305 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800306 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800307 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308
309 /*
310 * We're now ready to accept clients...
311 */
312
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800313 mReadyToRunBarrier.open();
314
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700315 // start boot animation
316 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 return NO_ERROR;
319}
320
321// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800322
Jamie Gennis582270d2011-08-17 18:19:00 -0700323bool SurfaceFlinger::authenticateSurfaceTexture(
324 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800325 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700326 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800327
328 // Check the visible layer list for the ISurface
329 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
330 size_t count = currentLayers.size();
331 for (size_t i=0 ; i<count ; i++) {
332 const sp<LayerBase>& layer(currentLayers[i]);
333 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700334 if (lbc != NULL) {
335 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
336 if (lbcBinder == surfaceTextureBinder) {
337 return true;
338 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800339 }
340 }
341
342 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700343 // SurfaceTexture gets destroyed before all the clients are done using it,
344 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800345 // will instead fail later on when the client tries to use the surface,
346 // which should be reported as "surface XYZ returned an -ENODEV". The
347 // purgatorized layers are no less authentic than the visible ones, so this
348 // should not cause any harm.
349 size_t purgatorySize = mLayerPurgatory.size();
350 for (size_t i=0 ; i<purgatorySize ; i++) {
351 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
352 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700353 if (lbc != NULL) {
354 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
355 if (lbcBinder == surfaceTextureBinder) {
356 return true;
357 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800358 }
359 }
360
361 return false;
362}
363
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800364// ----------------------------------------------------------------------------
365
366sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800367 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700368}
369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800371
372void SurfaceFlinger::waitForEvent() {
373 mEventQueue.waitMessage();
374}
375
376void SurfaceFlinger::signalTransaction() {
377 mEventQueue.invalidate();
378}
379
380void SurfaceFlinger::signalLayerUpdate() {
381 mEventQueue.invalidate();
382}
383
384void SurfaceFlinger::signalRefresh() {
385 mEventQueue.refresh();
386}
387
388status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
389 nsecs_t reltime, uint32_t flags) {
390 return mEventQueue.postMessage(msg, reltime);
391}
392
393status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
394 nsecs_t reltime, uint32_t flags) {
395 status_t res = mEventQueue.postMessage(msg, reltime);
396 if (res == NO_ERROR) {
397 msg->wait();
398 }
399 return res;
400}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401
402bool SurfaceFlinger::threadLoop()
403{
404 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 return true;
406}
407
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800408void SurfaceFlinger::onMessageReceived(int32_t what)
409{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800410 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800411 switch (what) {
Mathias Agopian303d5382012-02-05 01:49:16 -0800412 case MessageQueue::REFRESH: {
413// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800414 // check for transactions
415 if (CC_UNLIKELY(mConsoleSignals)) {
416 handleConsoleEvents();
417 }
418
419 // if we're in a global transaction, don't do anything.
420 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
421 uint32_t transactionFlags = peekTransactionFlags(mask);
422 if (CC_UNLIKELY(transactionFlags)) {
423 handleTransaction(transactionFlags);
424 }
425
426 // post surfaces (if needed)
427 handlePageFlip();
428
Mathias Agopian303d5382012-02-05 01:49:16 -0800429// signalRefresh();
430//
431// } break;
432//
433// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800434
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800435 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800436
Mathias Agopian303d5382012-02-05 01:49:16 -0800437 const DisplayHardware& hw(graphicPlane(0).displayHardware());
438
439// if (mDirtyRegion.isEmpty()) {
440// return;
441// }
442
Mathias Agopianb048cef2012-02-04 15:44:04 -0800443 if (CC_UNLIKELY(mHwWorkListDirty)) {
444 // build the h/w work list
445 handleWorkList();
446 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800447
Mathias Agopianb048cef2012-02-04 15:44:04 -0800448 if (CC_LIKELY(hw.canDraw())) {
449 // repaint the framebuffer (if needed)
450 handleRepaint();
451 // inform the h/w that we're done compositing
452 hw.compositionComplete();
453 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800454 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800455 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800456 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800457 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800458
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800459 } break;
460 }
461}
462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463void SurfaceFlinger::postFramebuffer()
464{
Mathias Agopian841cde52012-03-01 15:44:37 -0800465 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800466 // mSwapRegion can be empty here is some cases, for instance if a hidden
467 // or fully transparent window is updating.
468 // in that case, we need to flip anyways to not risk a deadlock with
469 // h/w composer.
470
Mathias Agopiana44b0412011-10-16 18:46:35 -0700471 const DisplayHardware& hw(graphicPlane(0).displayHardware());
472 const nsecs_t now = systemTime();
473 mDebugInSwapBuffers = now;
474 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800475
476 size_t numLayers = mVisibleLayersSortedByZ.size();
477 for (size_t i = 0; i < numLayers; i++) {
478 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
479 }
480
Mathias Agopiana44b0412011-10-16 18:46:35 -0700481 mLastSwapBufferTime = systemTime() - now;
482 mDebugInSwapBuffers = 0;
483 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484}
485
486void SurfaceFlinger::handleConsoleEvents()
487{
488 // something to do with the console
489 const DisplayHardware& hw = graphicPlane(0).displayHardware();
490
491 int what = android_atomic_and(0, &mConsoleSignals);
492 if (what & eConsoleAcquired) {
493 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700494 // this is a temporary work-around, eventually this should be called
495 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700496 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 }
498
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700500 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 }
503 }
504
505 mDirtyRegion.set(hw.bounds());
506}
507
508void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
509{
Mathias Agopian841cde52012-03-01 15:44:37 -0800510 ATRACE_CALL();
511
Mathias Agopianca4d3602011-05-19 15:38:14 -0700512 Mutex::Autolock _l(mStateLock);
513 const nsecs_t now = systemTime();
514 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515
Mathias Agopianca4d3602011-05-19 15:38:14 -0700516 // Here we're guaranteed that some transaction flags are set
517 // so we can call handleTransactionLocked() unconditionally.
518 // We call getTransactionFlags(), which will also clear the flags,
519 // with mStateLock held to guarantee that mCurrentState won't change
520 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700521
Mathias Agopianca4d3602011-05-19 15:38:14 -0700522 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
523 transactionFlags = getTransactionFlags(mask);
524 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700525
Mathias Agopianca4d3602011-05-19 15:38:14 -0700526 mLastTransactionTime = systemTime() - now;
527 mDebugInTransaction = 0;
528 invalidateHwcGeometry();
529 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700530}
531
Mathias Agopianca4d3602011-05-19 15:38:14 -0700532void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700533{
534 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 const size_t count = currentLayers.size();
536
537 /*
538 * Traversal of the children
539 * (perform the transaction for each of them if needed)
540 */
541
542 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
543 if (layersNeedTransaction) {
544 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
547 if (!trFlags) continue;
548
549 const uint32_t flags = layer->doTransaction(0);
550 if (flags & Layer::eVisibleRegion)
551 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 }
553 }
554
555 /*
556 * Perform our own transaction if needed
557 */
558
559 if (transactionFlags & eTransactionNeeded) {
560 if (mCurrentState.orientation != mDrawingState.orientation) {
561 // the orientation has changed, recompute all visible regions
562 // and invalidate everything.
563
564 const int dpy = 0;
565 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700566 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 GraphicPlane& plane(graphicPlane(dpy));
568 plane.setOrientation(orientation);
569
570 // update the shared control block
571 const DisplayHardware& hw(plane.displayHardware());
572 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
573 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800574 dcblk->w = plane.getWidth();
575 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576
577 mVisibleRegionsDirty = true;
578 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 }
580
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700581 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
582 // layers have been added
583 mVisibleRegionsDirty = true;
584 }
585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 // some layers might have been removed, so
587 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700589 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700591 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700592 const size_t count = previousLayers.size();
593 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700594 const sp<LayerBase>& layer(previousLayers[i]);
595 if (currentLayers.indexOf( layer ) < 0) {
596 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700597 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700598 }
599 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 }
602
603 commitTransaction();
604}
605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800607 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608{
Mathias Agopian841cde52012-03-01 15:44:37 -0800609 ATRACE_CALL();
610
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 const GraphicPlane& plane(graphicPlane(0));
612 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700613 const DisplayHardware& hw(plane.displayHardware());
614 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615
616 Region aboveOpaqueLayers;
617 Region aboveCoveredLayers;
618 Region dirty;
619
620 bool secureFrameBuffer = false;
621
622 size_t i = currentLayers.size();
623 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700624 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 layer->validateVisibility(planeTransform);
626
627 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700628 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629
Mathias Agopianab028732010-03-16 16:41:46 -0700630 /*
631 * opaqueRegion: area of a surface that is fully opaque.
632 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700634
635 /*
636 * visibleRegion: area of a surface that is visible on screen
637 * and not fully transparent. This is essentially the layer's
638 * footprint minus the opaque regions above it.
639 * Areas covered by a translucent surface are considered visible.
640 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700642
643 /*
644 * coveredRegion: area of a surface that is covered by all
645 * visible regions above it (which includes the translucent areas).
646 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700648
649
650 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800651 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700652 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700653 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700655 visibleRegion.andSelf(screenRegion);
656 if (!visibleRegion.isEmpty()) {
657 // Remove the transparent area from the visible region
658 if (translucent) {
659 visibleRegion.subtractSelf(layer->transparentRegionScreen);
660 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
Mathias Agopianab028732010-03-16 16:41:46 -0700662 // compute the opaque region
663 const int32_t layerOrientation = layer->getOrientation();
664 if (s.alpha==255 && !translucent &&
665 ((layerOrientation & Transform::ROT_INVALID) == false)) {
666 // the opaque region is the layer's footprint
667 opaqueRegion = visibleRegion;
668 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 }
670 }
671
Mathias Agopianab028732010-03-16 16:41:46 -0700672 // Clip the covered region to the visible region
673 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
674
675 // Update aboveCoveredLayers for next (lower) layer
676 aboveCoveredLayers.orSelf(visibleRegion);
677
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 // subtract the opaque region covered by the layers above us
679 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680
681 // compute this layer's dirty region
682 if (layer->contentDirty) {
683 // we need to invalidate the whole region
684 dirty = visibleRegion;
685 // as well, as the old visible region
686 dirty.orSelf(layer->visibleRegionScreen);
687 layer->contentDirty = false;
688 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700689 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700690 * the exposed region consists of two components:
691 * 1) what's VISIBLE now and was COVERED before
692 * 2) what's EXPOSED now less what was EXPOSED before
693 *
694 * note that (1) is conservative, we start with the whole
695 * visible region but only keep what used to be covered by
696 * something -- which mean it may have been exposed.
697 *
698 * (2) handles areas that were not covered by anything but got
699 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700700 */
Mathias Agopianab028732010-03-16 16:41:46 -0700701 const Region newExposed = visibleRegion - coveredRegion;
702 const Region oldVisibleRegion = layer->visibleRegionScreen;
703 const Region oldCoveredRegion = layer->coveredRegionScreen;
704 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
705 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 }
707 dirty.subtractSelf(aboveOpaqueLayers);
708
709 // accumulate to the screen dirty region
710 dirtyRegion.orSelf(dirty);
711
Mathias Agopianab028732010-03-16 16:41:46 -0700712 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 // Store the visible region is screen space
716 layer->setVisibleRegion(visibleRegion);
717 layer->setCoveredRegion(coveredRegion);
718
Mathias Agopian97011222009-07-28 10:57:27 -0700719 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 if (layer->isSecure() && !visibleRegion.isEmpty()) {
721 secureFrameBuffer = true;
722 }
723 }
724
Mathias Agopian97011222009-07-28 10:57:27 -0700725 // invalidate the areas where a layer was removed
726 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
727 mDirtyRegionRemovedLayer.clear();
728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 mSecureFrameBuffer = secureFrameBuffer;
730 opaqueRegion = aboveOpaqueLayers;
731}
732
733
734void SurfaceFlinger::commitTransaction()
735{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800736 if (!mLayersPendingRemoval.isEmpty()) {
737 // Notify removed layers now that they can't be drawn from
738 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
739 mLayersPendingRemoval[i]->onRemoved();
740 }
741 mLayersPendingRemoval.clear();
742 }
743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700745 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700746 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
749void SurfaceFlinger::handlePageFlip()
750{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800751 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800752 const DisplayHardware& hw = graphicPlane(0).displayHardware();
753 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800755 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
756 const bool visibleRegions = lockPageFlip(currentLayers);
757
758 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 Region opaqueRegion;
760 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700761
762 /*
763 * rebuild the visible layer list
764 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800765 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700766 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700767 mVisibleLayersSortedByZ.setCapacity(count);
768 for (size_t i=0 ; i<count ; i++) {
769 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
770 mVisibleLayersSortedByZ.add(currentLayers[i]);
771 }
772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 mWormholeRegion = screenRegion.subtract(opaqueRegion);
774 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800775 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 }
777
778 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700779
780 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 mDirtyRegion.andSelf(screenRegion);
782}
783
Mathias Agopianad456f92011-01-13 17:53:01 -0800784void SurfaceFlinger::invalidateHwcGeometry()
785{
786 mHwWorkListDirty = true;
787}
788
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
790{
791 bool recomputeVisibleRegions = false;
792 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700793 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700795 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 layer->lockPageFlip(recomputeVisibleRegions);
797 }
798 return recomputeVisibleRegions;
799}
800
801void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
802{
803 const GraphicPlane& plane(graphicPlane(0));
804 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800805 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700806 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700808 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 layer->unlockPageFlip(planeTransform, mDirtyRegion);
810 }
811}
812
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800813void SurfaceFlinger::handleRefresh()
814{
815 bool needInvalidate = false;
816 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
817 const size_t count = currentLayers.size();
818 for (size_t i=0 ; i<count ; i++) {
819 const sp<LayerBase>& layer(currentLayers[i]);
820 if (layer->onPreComposition()) {
821 needInvalidate = true;
822 }
823 }
824 if (needInvalidate) {
825 signalLayerUpdate();
826 }
827}
828
829
Mathias Agopiana350ff92010-08-10 17:14:02 -0700830void SurfaceFlinger::handleWorkList()
831{
832 mHwWorkListDirty = false;
833 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
834 if (hwc.initCheck() == NO_ERROR) {
835 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
836 const size_t count = currentLayers.size();
837 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700838 hwc_layer_t* const cur(hwc.getLayers());
839 for (size_t i=0 ; cur && i<count ; i++) {
840 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700841 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700842 cur[i].compositionType = HWC_FRAMEBUFFER;
843 cur[i].flags |= HWC_SKIP_LAYER;
844 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700845 }
846 }
847}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700848
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849void SurfaceFlinger::handleRepaint()
850{
Mathias Agopian841cde52012-03-01 15:44:37 -0800851 ATRACE_CALL();
852
Mathias Agopianb8a55602009-06-26 19:06:36 -0700853 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700854 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855
Glenn Kasten99ed2242011-12-15 09:51:17 -0800856 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 debugFlashRegions();
858 }
859
Mathias Agopianb8a55602009-06-26 19:06:36 -0700860 // set the frame buffer
861 const DisplayHardware& hw(graphicPlane(0).displayHardware());
862 glMatrixMode(GL_MODELVIEW);
863 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864
865 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700866 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
867 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700868 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700869 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
870 // takes a rectangle, we must make sure to update that whole
871 // rectangle in that case
872 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700873 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700874 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700875 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700877 // in the BUFFER_PRESERVED case, obviously, we can update only
878 // what's needed and nothing more.
879 // NOTE: this is NOT a common case, as preserving the backbuffer
880 // is costly and usually involves copying the whole update back.
881 }
882 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700883 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700884 // We need to redraw the rectangle that will be updated
885 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700886 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700887 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700888 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700889 } else {
890 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700892 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 }
894 }
895
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700896 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 composeSurfaces(mDirtyRegion);
898
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700899 // update the swap region and clear the dirty region
900 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 mDirtyRegion.clear();
902}
903
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700904void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700906 const DisplayHardware& hw(graphicPlane(0).displayHardware());
907 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700908 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700909 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700910 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700911 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700912
Mathias Agopianf384cc32011-09-08 18:31:55 -0700913 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
914 size_t count = layers.size();
915
Steve Blocke6f43dd2012-01-06 19:20:56 +0000916 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700917 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
918 hwc.getNumLayers(), count);
919
920 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700921 if (hwc.initCheck() == NO_ERROR) {
922 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
923 }
Mathias Agopian45721772010-08-12 15:03:26 -0700924
925 /*
926 * update the per-frame h/w composer data for each layer
927 * and build the transparent region of the FB
928 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700929 for (size_t i=0 ; i<count ; i++) {
930 const sp<LayerBase>& layer(layers[i]);
931 layer->setPerFrameData(&cur[i]);
932 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700933 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700934 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000935 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700936
Mathias Agopianf384cc32011-09-08 18:31:55 -0700937 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700938 // what's happening here is tricky.
939 // we want to clear all the layers with the CLEAR_FB flags
940 // that are opaque.
941 // however, since some GPU are efficient at preserving
942 // the backbuffer, we want to take advantage of that so we do the
943 // clear only in the dirty region (other areas will be preserved
944 // on those GPUs).
945 // NOTE: on non backbuffer preserving GPU, the dirty region
946 // has already been expanded as needed, so the code is correct
947 // there too.
948 //
949 // However, the content of the framebuffer cannot be trusted when
950 // we switch to/from FB/OVERLAY, in which case we need to
951 // expand the dirty region to those areas too.
952 //
953 // Note also that there is a special case when switching from
954 // "no layers in FB" to "some layers in FB", where we need to redraw
955 // the entire FB, since some areas might contain uninitialized
956 // data.
957 //
958 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700959 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700960 // that is, we can't erase anything outside the dirty region.
961
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700962 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700963
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700964 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
965 transparent.set(hw.getBounds());
966 dirtyInOut = transparent;
967 } else {
968 for (size_t i=0 ; i<count ; i++) {
969 const sp<LayerBase>& layer(layers[i]);
970 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
971 transparent.orSelf(layer->visibleRegionScreen);
972 }
973 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
974 if (isOverlay != layer->isOverlay()) {
975 // we transitioned to/from overlay, so add this layer
976 // to the dirty region so the framebuffer can be either
977 // cleared or redrawn.
978 dirtyInOut.orSelf(layer->visibleRegionScreen);
979 }
980 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800981 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700982 // don't erase stuff outside the dirty region
983 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700984 }
985
986 /*
987 * clear the area of the FB that need to be transparent
988 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700989 if (!transparent.isEmpty()) {
990 glClearColor(0,0,0,0);
991 Region::const_iterator it = transparent.begin();
992 Region::const_iterator const end = transparent.end();
993 const int32_t height = hw.getHeight();
994 while (it != end) {
995 const Rect& r(*it++);
996 const GLint sy = height - (r.top + r.height());
997 glScissor(r.left, sy, r.width(), r.height());
998 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800999 }
Mathias Agopiana350ff92010-08-10 17:14:02 -07001000 }
1001 }
Mathias Agopianf384cc32011-09-08 18:31:55 -07001002}
Mathias Agopian45721772010-08-12 15:03:26 -07001003
Mathias Agopianf384cc32011-09-08 18:31:55 -07001004void SurfaceFlinger::composeSurfaces(const Region& dirty)
1005{
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001006 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1007 HWComposer& hwc(hw.getHwComposer());
1008
1009 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001010 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001011 // should never happen unless the window manager has a bug
1012 // draw something...
1013 drawWormhole();
1014 }
1015
Mathias Agopianfbc79222012-02-23 21:20:01 -08001016 // FIXME: workaroud for b/6020860
1017 glEnable(GL_SCISSOR_TEST);
1018 glScissor(0,0,0,0);
1019 glClear(GL_COLOR_BUFFER_BIT);
1020 // end-workaround
1021
Mathias Agopian45721772010-08-12 15:03:26 -07001022 /*
1023 * and then, render the layers targeted at the framebuffer
1024 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001025 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001026 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1027 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001028 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001029 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001030 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001031 }
1032 const sp<LayerBase>& layer(layers[i]);
1033 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1034 if (!clip.isEmpty()) {
1035 layer->draw(clip);
1036 }
1037 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040void SurfaceFlinger::debugFlashRegions()
1041{
Mathias Agopian0a917752010-06-14 21:20:00 -07001042 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1043 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001044 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001045 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001046 return;
1047 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001048
Mathias Agopian0a917752010-06-14 21:20:00 -07001049 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1050 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1051 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1052 mDirtyRegion.bounds() : hw.bounds());
1053 composeSurfaces(repaint);
1054 }
1055
Mathias Agopianc492e672011-10-18 14:49:27 -07001056 glDisable(GL_TEXTURE_EXTERNAL_OES);
1057 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 glDisable(GL_SCISSOR_TEST);
1060
Mathias Agopian0926f502009-05-04 14:17:04 -07001061 static int toggle = 0;
1062 toggle = 1 - toggle;
1063 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001064 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001065 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001066 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001067 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068
Mathias Agopian20f68782009-05-11 00:03:41 -07001069 Region::const_iterator it = mDirtyRegion.begin();
1070 Region::const_iterator const end = mDirtyRegion.end();
1071 while (it != end) {
1072 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001074 { r.left, height - r.top },
1075 { r.left, height - r.bottom },
1076 { r.right, height - r.bottom },
1077 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 };
1079 glVertexPointer(2, GL_FLOAT, 0, vertices);
1080 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1081 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001082
Mathias Agopian0656a682011-09-20 17:22:44 -07001083 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084
1085 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001086 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087
1088 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089}
1090
1091void SurfaceFlinger::drawWormhole() const
1092{
1093 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1094 if (region.isEmpty())
1095 return;
1096
1097 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1098 const int32_t width = hw.getWidth();
1099 const int32_t height = hw.getHeight();
1100
Glenn Kasten99ed2242011-12-15 09:51:17 -08001101 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001102 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001103 Region::const_iterator it = region.begin();
1104 Region::const_iterator const end = region.end();
1105 while (it != end) {
1106 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107 const GLint sy = height - (r.top + r.height());
1108 glScissor(r.left, sy, r.width(), r.height());
1109 glClear(GL_COLOR_BUFFER_BIT);
1110 }
1111 } else {
1112 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1113 { width, height }, { 0, height } };
1114 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 glVertexPointer(2, GL_SHORT, 0, vertices);
1117 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1118 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001119
1120 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 glEnable(GL_TEXTURE_2D);
1122 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1123 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1124 glMatrixMode(GL_TEXTURE);
1125 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001126
1127 glDisable(GL_BLEND);
1128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001130 Region::const_iterator it = region.begin();
1131 Region::const_iterator const end = region.end();
1132 while (it != end) {
1133 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 const GLint sy = height - (r.top + r.height());
1135 glScissor(r.left, sy, r.width(), r.height());
1136 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1137 }
1138 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001139 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001140 glLoadIdentity();
1141 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 }
1143}
1144
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001145status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146{
1147 Mutex::Autolock _l(mStateLock);
1148 addLayer_l(layer);
1149 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1150 return NO_ERROR;
1151}
1152
Mathias Agopian96f08192010-06-02 23:28:45 -07001153status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1154{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001155 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001156 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1157}
1158
1159ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1160 const sp<LayerBaseClient>& lbc)
1161{
Mathias Agopian96f08192010-06-02 23:28:45 -07001162 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001163 size_t name = client->attachLayer(lbc);
1164
1165 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001166
1167 // add this layer to the current state list
1168 addLayer_l(lbc);
1169
Mathias Agopian4f113742011-05-03 16:21:41 -07001170 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001171}
1172
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001173status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174{
1175 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001176 status_t err = purgatorizeLayer_l(layer);
1177 if (err == NO_ERROR)
1178 setTransactionFlags(eTransactionNeeded);
1179 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180}
1181
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001182status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001184 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1185 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001186 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001187 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1189 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001190 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191 return NO_ERROR;
1192 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001193 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194}
1195
Mathias Agopian9a112062009-04-17 19:36:26 -07001196status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1197{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001198 // First add the layer to the purgatory list, which makes sure it won't
1199 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001200 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001201 if (err >= 0) {
1202 mLayerPurgatory.add(layerBase);
1203 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001204
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001205 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001206
Mathias Agopian3d579642009-06-04 18:46:21 -07001207 // it's possible that we don't find a layer, because it might
1208 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001209 // from the user because there is a race between Client::destroySurface(),
1210 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001211 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1212}
1213
Mathias Agopian96f08192010-06-02 23:28:45 -07001214status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215{
Mathias Agopian96f08192010-06-02 23:28:45 -07001216 layer->forceVisibilityTransaction();
1217 setTransactionFlags(eTraversalNeeded);
1218 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219}
1220
Mathias Agopiandea20b12011-05-03 17:04:02 -07001221uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1222{
1223 return android_atomic_release_load(&mTransactionFlags);
1224}
1225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1227{
1228 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1229}
1230
Mathias Agopianbb641242010-05-18 17:06:55 -07001231uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232{
1233 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1234 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001235 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 }
1237 return old;
1238}
1239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001241void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001242 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001243 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001244
Jamie Gennis28378392011-10-12 17:39:00 -07001245 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001246 if (mCurrentState.orientation != orientation) {
1247 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1248 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001249 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001250 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001251 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001252 orientation);
1253 }
1254 }
1255
Mathias Agopian698c0872011-06-28 19:09:31 -07001256 const size_t count = state.size();
1257 for (size_t i=0 ; i<count ; i++) {
1258 const ComposerState& s(state[i]);
1259 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001260 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001261 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001262
Mathias Agopian386aa982011-11-07 21:58:03 -08001263 if (transactionFlags) {
1264 // this triggers the transaction
1265 setTransactionFlags(transactionFlags);
1266
1267 // if this is a synchronous transaction, wait for it to take effect
1268 // before returning.
1269 if (flags & eSynchronous) {
1270 mTransationPending = true;
1271 }
1272 while (mTransationPending) {
1273 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1274 if (CC_UNLIKELY(err != NO_ERROR)) {
1275 // just in case something goes wrong in SF, return to the
1276 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001277 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001278 mTransationPending = false;
1279 break;
1280 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001281 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 }
1283}
1284
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001285sp<ISurface> SurfaceFlinger::createSurface(
1286 ISurfaceComposerClient::surface_data_t* params,
1287 const String8& name,
1288 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1290 uint32_t flags)
1291{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001292 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001293 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001294
1295 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001296 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001297 int(w), int(h));
1298 return surfaceHandle;
1299 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001300
Mathias Agopianff615cc2012-02-24 14:58:36 -08001301 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001302 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 switch (flags & eFXSurfaceMask) {
1304 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001305 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1306 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 break;
1308 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001309 // for now we treat Blur as Dim, until we can implement it
1310 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001312 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001314 case eFXSurfaceScreenshot:
1315 layer = createScreenshotSurface(client, d, w, h, flags);
1316 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 }
1318
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001319 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001321 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001325 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001326 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001327 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001328 if (normalLayer != 0) {
1329 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001330 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001331 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001332 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001333
Mathias Agopian96f08192010-06-02 23:28:45 -07001334 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 }
1336
1337 return surfaceHandle;
1338}
1339
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001340sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001341 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001342 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001343 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344{
1345 // initialize the surfaces
1346 switch (format) { // TODO: take h/w into account
1347 case PIXEL_FORMAT_TRANSPARENT:
1348 case PIXEL_FORMAT_TRANSLUCENT:
1349 format = PIXEL_FORMAT_RGBA_8888;
1350 break;
1351 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001352#ifdef NO_RGBX_8888
1353 format = PIXEL_FORMAT_RGB_565;
1354#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001355 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001356#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 break;
1358 }
1359
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001360#ifdef NO_RGBX_8888
1361 if (format == PIXEL_FORMAT_RGBX_8888)
1362 format = PIXEL_FORMAT_RGBA_8888;
1363#endif
1364
Mathias Agopian96f08192010-06-02 23:28:45 -07001365 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001366 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001367 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001368 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001369 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 }
1371 return layer;
1372}
1373
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001374sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001375 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001376 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377{
Mathias Agopian96f08192010-06-02 23:28:45 -07001378 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001379 return layer;
1380}
1381
1382sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1383 const sp<Client>& client, DisplayID display,
1384 uint32_t w, uint32_t h, uint32_t flags)
1385{
1386 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 return layer;
1388}
1389
Mathias Agopian96f08192010-06-02 23:28:45 -07001390status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391{
Mathias Agopian9a112062009-04-17 19:36:26 -07001392 /*
1393 * called by the window manager, when a surface should be marked for
1394 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001395 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001396 * The surface is removed from the current and drawing lists, but placed
1397 * in the purgatory queue, so it's not destroyed right-away (we need
1398 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001399 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001400
Mathias Agopian48d819a2009-09-10 19:41:18 -07001401 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001402 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001403 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001404
Mathias Agopian48d819a2009-09-10 19:41:18 -07001405 if (layer != 0) {
1406 err = purgatorizeLayer_l(layer);
1407 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001408 setTransactionFlags(eTransactionNeeded);
1409 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001410 }
1411 return err;
1412}
1413
Mathias Agopianca4d3602011-05-19 15:38:14 -07001414status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001415{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001416 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001417 status_t err = NO_ERROR;
1418 sp<LayerBaseClient> l(layer.promote());
1419 if (l != NULL) {
1420 Mutex::Autolock _l(mStateLock);
1421 err = removeLayer_l(l);
1422 if (err == NAME_NOT_FOUND) {
1423 // The surface wasn't in the current list, which means it was
1424 // removed already, which means it is in the purgatory,
1425 // and need to be removed from there.
1426 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001427 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001428 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001429 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001430 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001431 "error removing layer=%p (%s)", l.get(), strerror(-err));
1432 }
1433 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434}
1435
Mathias Agopian698c0872011-06-28 19:09:31 -07001436uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001437 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001438 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001441 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1442 if (layer != 0) {
1443 const uint32_t what = s.what;
1444 if (what & ePositionChanged) {
1445 if (layer->setPosition(s.x, s.y))
1446 flags |= eTraversalNeeded;
1447 }
1448 if (what & eLayerChanged) {
1449 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1450 if (layer->setLayer(s.z)) {
1451 mCurrentState.layersSortedByZ.removeAt(idx);
1452 mCurrentState.layersSortedByZ.add(layer);
1453 // we need traversal (state changed)
1454 // AND transaction (list changed)
1455 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 }
1457 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001458 if (what & eSizeChanged) {
1459 if (layer->setSize(s.w, s.h)) {
1460 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001461 }
1462 }
1463 if (what & eAlphaChanged) {
1464 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1465 flags |= eTraversalNeeded;
1466 }
1467 if (what & eMatrixChanged) {
1468 if (layer->setMatrix(s.matrix))
1469 flags |= eTraversalNeeded;
1470 }
1471 if (what & eTransparentRegionChanged) {
1472 if (layer->setTransparentRegionHint(s.transparentRegion))
1473 flags |= eTraversalNeeded;
1474 }
1475 if (what & eVisibilityChanged) {
1476 if (layer->setFlags(s.flags, s.mask))
1477 flags |= eTraversalNeeded;
1478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001480 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481}
1482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483void SurfaceFlinger::screenReleased(int dpy)
1484{
1485 // this may be called by a signal handler, we can't do too much in here
1486 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001487 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488}
1489
1490void SurfaceFlinger::screenAcquired(int dpy)
1491{
1492 // this may be called by a signal handler, we can't do too much in here
1493 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001494 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495}
1496
1497status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1498{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001499 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 char buffer[SIZE];
1501 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001502
1503 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 snprintf(buffer, SIZE, "Permission Denial: "
1505 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1506 IPCThreadState::self()->getCallingPid(),
1507 IPCThreadState::self()->getCallingUid());
1508 result.append(buffer);
1509 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001510 // Try to get the main lock, but don't insist if we can't
1511 // (this would indicate SF is stuck, but we want to be able to
1512 // print something in dumpsys).
1513 int retry = 3;
1514 while (mStateLock.tryLock()<0 && --retry>=0) {
1515 usleep(1000000);
1516 }
1517 const bool locked(retry >= 0);
1518 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001519 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001520 "SurfaceFlinger appears to be unresponsive, "
1521 "dumping anyways (no locks held)\n");
1522 result.append(buffer);
1523 }
1524
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001525 bool dumpAll = true;
1526 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001527 size_t numArgs = args.size();
1528 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001529 if ((index < numArgs) &&
1530 (args[index] == String16("--list"))) {
1531 index++;
1532 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001533 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001534 }
1535
1536 if ((index < numArgs) &&
1537 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001538 index++;
1539 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001540 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001541 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001542
1543 if ((index < numArgs) &&
1544 (args[index] == String16("--latency-clear"))) {
1545 index++;
1546 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001547 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001548 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001550
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001551 if (dumpAll) {
1552 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001553 }
1554
Mathias Agopian9795c422009-08-26 16:36:26 -07001555 if (locked) {
1556 mStateLock.unlock();
1557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 }
1559 write(fd, result.string(), result.size());
1560 return NO_ERROR;
1561}
1562
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001563void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1564 String8& result, char* buffer, size_t SIZE) const
1565{
1566 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1567 const size_t count = currentLayers.size();
1568 for (size_t i=0 ; i<count ; i++) {
1569 const sp<LayerBase>& layer(currentLayers[i]);
1570 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1571 result.append(buffer);
1572 }
1573}
1574
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001575void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1576 String8& result, char* buffer, size_t SIZE) const
1577{
1578 String8 name;
1579 if (index < args.size()) {
1580 name = String8(args[index]);
1581 index++;
1582 }
1583
1584 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1585 const size_t count = currentLayers.size();
1586 for (size_t i=0 ; i<count ; i++) {
1587 const sp<LayerBase>& layer(currentLayers[i]);
1588 if (name.isEmpty()) {
1589 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1590 result.append(buffer);
1591 }
1592 if (name.isEmpty() || (name == layer->getName())) {
1593 layer->dumpStats(result, buffer, SIZE);
1594 }
1595 }
1596}
1597
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001598void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1599 String8& result, char* buffer, size_t SIZE) const
1600{
1601 String8 name;
1602 if (index < args.size()) {
1603 name = String8(args[index]);
1604 index++;
1605 }
1606
1607 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1608 const size_t count = currentLayers.size();
1609 for (size_t i=0 ; i<count ; i++) {
1610 const sp<LayerBase>& layer(currentLayers[i]);
1611 if (name.isEmpty() || (name == layer->getName())) {
1612 layer->clearStats();
1613 }
1614 }
1615}
1616
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001617void SurfaceFlinger::dumpAllLocked(
1618 String8& result, char* buffer, size_t SIZE) const
1619{
1620 // figure out if we're stuck somewhere
1621 const nsecs_t now = systemTime();
1622 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1623 const nsecs_t inTransaction(mDebugInTransaction);
1624 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1625 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1626
1627 /*
1628 * Dump the visible layer list
1629 */
1630 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1631 const size_t count = currentLayers.size();
1632 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1633 result.append(buffer);
1634 for (size_t i=0 ; i<count ; i++) {
1635 const sp<LayerBase>& layer(currentLayers[i]);
1636 layer->dump(result, buffer, SIZE);
1637 }
1638
1639 /*
1640 * Dump the layers in the purgatory
1641 */
1642
1643 const size_t purgatorySize = mLayerPurgatory.size();
1644 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1645 result.append(buffer);
1646 for (size_t i=0 ; i<purgatorySize ; i++) {
1647 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1648 layer->shortDump(result, buffer, SIZE);
1649 }
1650
1651 /*
1652 * Dump SurfaceFlinger global state
1653 */
1654
1655 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1656 result.append(buffer);
1657
1658 const GLExtensions& extensions(GLExtensions::getInstance());
1659 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1660 extensions.getVendor(),
1661 extensions.getRenderer(),
1662 extensions.getVersion());
1663 result.append(buffer);
1664
1665 snprintf(buffer, SIZE, "EGL : %s\n",
1666 eglQueryString(graphicPlane(0).getEGLDisplay(),
1667 EGL_VERSION_HW_ANDROID));
1668 result.append(buffer);
1669
1670 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1671 result.append(buffer);
1672
1673 mWormholeRegion.dump(result, "WormholeRegion");
1674 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1675 snprintf(buffer, SIZE,
1676 " orientation=%d, canDraw=%d\n",
1677 mCurrentState.orientation, hw.canDraw());
1678 result.append(buffer);
1679 snprintf(buffer, SIZE,
1680 " last eglSwapBuffers() time: %f us\n"
1681 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001682 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001683 " refresh-rate : %f fps\n"
1684 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001685 " y-dpi : %f\n"
1686 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001687 mLastSwapBufferTime/1000.0,
1688 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001689 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001690 hw.getRefreshRate(),
1691 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001692 hw.getDpiY(),
1693 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001694 result.append(buffer);
1695
1696 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1697 inSwapBuffersDuration/1000.0);
1698 result.append(buffer);
1699
1700 snprintf(buffer, SIZE, " transaction time: %f us\n",
1701 inTransactionDuration/1000.0);
1702 result.append(buffer);
1703
1704 /*
1705 * VSYNC state
1706 */
1707 mEventThread->dump(result, buffer, SIZE);
1708
1709 /*
1710 * Dump HWComposer state
1711 */
1712 HWComposer& hwc(hw.getHwComposer());
1713 snprintf(buffer, SIZE, "h/w composer state:\n");
1714 result.append(buffer);
1715 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1716 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1717 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1718 result.append(buffer);
1719 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1720
1721 /*
1722 * Dump gralloc state
1723 */
1724 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1725 alloc.dump(result);
1726 hw.dump(result);
1727}
1728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729status_t SurfaceFlinger::onTransact(
1730 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1731{
1732 switch (code) {
1733 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001734 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001737 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001738 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 {
1740 // codes that require permission check
1741 IPCThreadState* ipc = IPCThreadState::self();
1742 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001743 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001744 if ((uid != AID_GRAPHICS) &&
1745 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001746 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001747 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1748 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001750 break;
1751 }
1752 case CAPTURE_SCREEN:
1753 {
1754 // codes that require permission check
1755 IPCThreadState* ipc = IPCThreadState::self();
1756 const int pid = ipc->getCallingPid();
1757 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001758 if ((uid != AID_GRAPHICS) &&
1759 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001760 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001761 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1762 return PERMISSION_DENIED;
1763 }
1764 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 }
1766 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001767
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1769 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001770 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001771 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001772 IPCThreadState* ipc = IPCThreadState::self();
1773 const int pid = ipc->getCallingPid();
1774 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001775 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001776 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001777 return PERMISSION_DENIED;
1778 }
1779 int n;
1780 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001781 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001782 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 return NO_ERROR;
1784 case 1002: // SHOW_UPDATES
1785 n = data.readInt32();
1786 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001787 invalidateHwcGeometry();
1788 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001789 return NO_ERROR;
1790 case 1003: // SHOW_BACKGROUND
1791 n = data.readInt32();
1792 mDebugBackground = n ? 1 : 0;
1793 return NO_ERROR;
1794 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001795 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001796 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001798 case 1005:{ // force transaction
1799 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1800 return NO_ERROR;
1801 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001802 case 1006:{ // send empty update
1803 signalRefresh();
1804 return NO_ERROR;
1805 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001806 case 1008: // toggle use of hw composer
1807 n = data.readInt32();
1808 mDebugDisableHWC = n ? 1 : 0;
1809 invalidateHwcGeometry();
1810 repaintEverything();
1811 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001812 case 1009: // toggle use of transform hint
1813 n = data.readInt32();
1814 mDebugDisableTransformHint = n ? 1 : 0;
1815 invalidateHwcGeometry();
1816 repaintEverything();
1817 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001819 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001820 reply->writeInt32(0);
1821 reply->writeInt32(mDebugRegion);
1822 reply->writeInt32(mDebugBackground);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001823 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001824 return NO_ERROR;
1825 case 1013: {
1826 Mutex::Autolock _l(mStateLock);
1827 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1828 reply->writeInt32(hw.getPageFlipCount());
1829 }
1830 return NO_ERROR;
1831 }
1832 }
1833 return err;
1834}
1835
Mathias Agopian53331da2011-08-22 21:44:41 -07001836void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001837 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001838 const Rect bounds(hw.getBounds());
1839 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001840 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001841}
1842
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001843void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1844 Mutex::Autolock _l(mInvalidateLock);
1845 mInvalidateRegion = reg;
1846}
1847
1848Region SurfaceFlinger::getAndClearInvalidateRegion() {
1849 Mutex::Autolock _l(mInvalidateLock);
1850 Region reg(mInvalidateRegion);
1851 mInvalidateRegion.clear();
1852 return reg;
1853}
1854
Mathias Agopian59119e62010-10-11 12:37:43 -07001855// ---------------------------------------------------------------------------
1856
Mathias Agopian118d0242011-10-13 16:02:48 -07001857status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1858 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1859{
1860 Mutex::Autolock _l(mStateLock);
1861 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1862}
1863
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001864status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1865 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001866{
Mathias Agopian59119e62010-10-11 12:37:43 -07001867 if (!GLExtensions::getInstance().haveFramebufferObject())
1868 return INVALID_OPERATION;
1869
1870 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001871 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001872 const uint32_t hw_w = hw.getWidth();
1873 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001874 GLfloat u = 1;
1875 GLfloat v = 1;
1876
1877 // make sure to clear all GL error flags
1878 while ( glGetError() != GL_NO_ERROR ) ;
1879
1880 // create a FBO
1881 GLuint name, tname;
1882 glGenTextures(1, &tname);
1883 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001884 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1885 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001886 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001887 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001888 GLint tw = (2 << (31 - clz(hw_w)));
1889 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001890 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1891 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001892 u = GLfloat(hw_w) / tw;
1893 v = GLfloat(hw_h) / th;
1894 }
1895 glGenFramebuffersOES(1, &name);
1896 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001897 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1898 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001899
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001900 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001901 glDisable(GL_TEXTURE_EXTERNAL_OES);
1902 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001903 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001904 glClearColor(0,0,0,1);
1905 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001906 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001907 glMatrixMode(GL_MODELVIEW);
1908 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001909 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1910 const size_t count = layers.size();
1911 for (size_t i=0 ; i<count ; ++i) {
1912 const sp<LayerBase>& layer(layers[i]);
1913 layer->drawForSreenShot();
1914 }
1915
Mathias Agopian118d0242011-10-13 16:02:48 -07001916 hw.compositionComplete();
1917
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001918 // back to main framebuffer
1919 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1920 glDisable(GL_SCISSOR_TEST);
1921 glDeleteFramebuffersOES(1, &name);
1922
1923 *textureName = tname;
1924 *uOut = u;
1925 *vOut = v;
1926 return NO_ERROR;
1927}
1928
1929// ---------------------------------------------------------------------------
1930
1931status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1932{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001933 // get screen geometry
1934 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1935 const uint32_t hw_w = hw.getWidth();
1936 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001937 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001938
1939 GLfloat u, v;
1940 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001941 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001942 if (result != NO_ERROR) {
1943 return result;
1944 }
1945
1946 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001947 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001948 glBindTexture(GL_TEXTURE_2D, tname);
1949 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1950 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1951 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001952 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1953 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001954 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1955 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1956 glVertexPointer(2, GL_FLOAT, 0, vtx);
1957
Mathias Agopianffcf4652011-07-07 17:30:31 -07001958 /*
1959 * Texture coordinate mapping
1960 *
1961 * u
1962 * 1 +----------+---+
1963 * | | | | image is inverted
1964 * | V | | w.r.t. the texture
1965 * 1-v +----------+ | coordinates
1966 * | |
1967 * | |
1968 * | |
1969 * 0 +--------------+
1970 * 0 1
1971 *
1972 */
1973
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001974 class s_curve_interpolator {
1975 const float nbFrames, s, v;
1976 public:
1977 s_curve_interpolator(int nbFrames, float s)
1978 : nbFrames(1.0f / (nbFrames-1)), s(s),
1979 v(1.0f + expf(-s + 0.5f*s)) {
1980 }
1981 float operator()(int f) {
1982 const float x = f * nbFrames;
1983 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1984 }
1985 };
1986
1987 class v_stretch {
1988 const GLfloat hw_w, hw_h;
1989 public:
1990 v_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 = hw_h - (hw_h * v);
1996 const GLfloat x = (hw_w - w) * 0.5f;
1997 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-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 Agopian9daa5c92010-10-12 16:05:48 -07002002 }
2003 };
2004
2005 class h_stretch {
2006 const GLfloat hw_w, hw_h;
2007 public:
2008 h_stretch(uint32_t hw_w, uint32_t hw_h)
2009 : hw_w(hw_w), hw_h(hw_h) {
2010 }
2011 void operator()(GLfloat* vtx, float v) {
2012 const GLfloat w = hw_w - (hw_w * v);
2013 const GLfloat h = 1.0f;
2014 const GLfloat x = (hw_w - w) * 0.5f;
2015 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002016 vtx[0] = x; vtx[1] = y;
2017 vtx[2] = x; vtx[3] = y + h;
2018 vtx[4] = x + w; vtx[5] = y + h;
2019 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002020 }
2021 };
2022
2023 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002024 char value[PROPERTY_VALUE_MAX];
2025 property_get("debug.sf.electron_frames", value, "24");
2026 int nbFrames = (atoi(value) + 1) >> 1;
2027 if (nbFrames <= 0) // just in case
2028 nbFrames = 24;
2029
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002030 s_curve_interpolator itr(nbFrames, 7.5f);
2031 s_curve_interpolator itg(nbFrames, 8.0f);
2032 s_curve_interpolator itb(nbFrames, 8.5f);
2033
2034 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002035
2036 glMatrixMode(GL_TEXTURE);
2037 glLoadIdentity();
2038 glMatrixMode(GL_MODELVIEW);
2039 glLoadIdentity();
2040
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002041 glEnable(GL_BLEND);
2042 glBlendFunc(GL_ONE, GL_ONE);
2043 for (int i=0 ; i<nbFrames ; i++) {
2044 float x, y, w, h;
2045 const float vr = itr(i);
2046 const float vg = itg(i);
2047 const float vb = itb(i);
2048
2049 // clear screen
2050 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002051 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002052 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002053
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 // draw the red plane
2055 vverts(vtx, vr);
2056 glColorMask(1,0,0,1);
2057 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002058
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002059 // draw the green plane
2060 vverts(vtx, vg);
2061 glColorMask(0,1,0,1);
2062 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002063
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 // draw the blue plane
2065 vverts(vtx, vb);
2066 glColorMask(0,0,1,1);
2067 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002068
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002069 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002070 glDisable(GL_TEXTURE_2D);
2071 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 glColor4f(vg, vg, vg, 1);
2073 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2074 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002075 }
2076
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002077 h_stretch hverts(hw_w, hw_h);
2078 glDisable(GL_BLEND);
2079 glDisable(GL_TEXTURE_2D);
2080 glColorMask(1,1,1,1);
2081 for (int i=0 ; i<nbFrames ; i++) {
2082 const float v = itg(i);
2083 hverts(vtx, v);
2084 glClear(GL_COLOR_BUFFER_BIT);
2085 glColor4f(1-v, 1-v, 1-v, 1);
2086 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2087 hw.flip(screenBounds);
2088 }
2089
2090 glColorMask(1,1,1,1);
2091 glEnable(GL_SCISSOR_TEST);
2092 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2093 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002094 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002095 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002096 return NO_ERROR;
2097}
2098
2099status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2100{
2101 status_t result = PERMISSION_DENIED;
2102
2103 if (!GLExtensions::getInstance().haveFramebufferObject())
2104 return INVALID_OPERATION;
2105
2106
2107 // get screen geometry
2108 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2109 const uint32_t hw_w = hw.getWidth();
2110 const uint32_t hw_h = hw.getHeight();
2111 const Region screenBounds(hw.bounds());
2112
2113 GLfloat u, v;
2114 GLuint tname;
2115 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2116 if (result != NO_ERROR) {
2117 return result;
2118 }
2119
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002120 GLfloat vtx[8];
2121 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002122 glBindTexture(GL_TEXTURE_2D, tname);
2123 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2124 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2125 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002126 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2127 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002128 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2129 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2130 glVertexPointer(2, GL_FLOAT, 0, vtx);
2131
2132 class s_curve_interpolator {
2133 const float nbFrames, s, v;
2134 public:
2135 s_curve_interpolator(int nbFrames, float s)
2136 : nbFrames(1.0f / (nbFrames-1)), s(s),
2137 v(1.0f + expf(-s + 0.5f*s)) {
2138 }
2139 float operator()(int f) {
2140 const float x = f * nbFrames;
2141 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2142 }
2143 };
2144
2145 class v_stretch {
2146 const GLfloat hw_w, hw_h;
2147 public:
2148 v_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 = hw_h - (hw_h * v);
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
2163 class h_stretch {
2164 const GLfloat hw_w, hw_h;
2165 public:
2166 h_stretch(uint32_t hw_w, uint32_t hw_h)
2167 : hw_w(hw_w), hw_h(hw_h) {
2168 }
2169 void operator()(GLfloat* vtx, float v) {
2170 const GLfloat w = hw_w - (hw_w * v);
2171 const GLfloat h = 1.0f;
2172 const GLfloat x = (hw_w - w) * 0.5f;
2173 const GLfloat y = (hw_h - h) * 0.5f;
2174 vtx[0] = x; vtx[1] = y;
2175 vtx[2] = x; vtx[3] = y + h;
2176 vtx[4] = x + w; vtx[5] = y + h;
2177 vtx[6] = x + w; vtx[7] = y;
2178 }
2179 };
2180
Mathias Agopiana6546e52010-10-14 12:33:07 -07002181 // the full animation is 12 frames
2182 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002183 s_curve_interpolator itr(nbFrames, 7.5f);
2184 s_curve_interpolator itg(nbFrames, 8.0f);
2185 s_curve_interpolator itb(nbFrames, 8.5f);
2186
2187 h_stretch hverts(hw_w, hw_h);
2188 glDisable(GL_BLEND);
2189 glDisable(GL_TEXTURE_2D);
2190 glColorMask(1,1,1,1);
2191 for (int i=nbFrames-1 ; i>=0 ; i--) {
2192 const float v = itg(i);
2193 hverts(vtx, v);
2194 glClear(GL_COLOR_BUFFER_BIT);
2195 glColor4f(1-v, 1-v, 1-v, 1);
2196 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2197 hw.flip(screenBounds);
2198 }
2199
Mathias Agopiana6546e52010-10-14 12:33:07 -07002200 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002201 v_stretch vverts(hw_w, hw_h);
2202 glEnable(GL_BLEND);
2203 glBlendFunc(GL_ONE, GL_ONE);
2204 for (int i=nbFrames-1 ; i>=0 ; i--) {
2205 float x, y, w, h;
2206 const float vr = itr(i);
2207 const float vg = itg(i);
2208 const float vb = itb(i);
2209
2210 // clear screen
2211 glColorMask(1,1,1,1);
2212 glClear(GL_COLOR_BUFFER_BIT);
2213 glEnable(GL_TEXTURE_2D);
2214
2215 // draw the red plane
2216 vverts(vtx, vr);
2217 glColorMask(1,0,0,1);
2218 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2219
2220 // draw the green plane
2221 vverts(vtx, vg);
2222 glColorMask(0,1,0,1);
2223 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2224
2225 // draw the blue plane
2226 vverts(vtx, vb);
2227 glColorMask(0,0,1,1);
2228 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2229
2230 hw.flip(screenBounds);
2231 }
2232
2233 glColorMask(1,1,1,1);
2234 glEnable(GL_SCISSOR_TEST);
2235 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002236 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002237 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002238 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002239
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002240 return NO_ERROR;
2241}
2242
2243// ---------------------------------------------------------------------------
2244
Mathias Agopianabd671a2010-10-14 14:54:06 -07002245status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002246{
2247 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2248 if (!hw.canDraw()) {
2249 // we're already off
2250 return NO_ERROR;
2251 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002252
2253 // turn off hwc while we're doing the animation
2254 hw.getHwComposer().disable();
2255 // and make sure to turn it back on (if needed) next time we compose
2256 invalidateHwcGeometry();
2257
Mathias Agopianabd671a2010-10-14 14:54:06 -07002258 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2259 electronBeamOffAnimationImplLocked();
2260 }
2261
2262 // always clear the whole screen at the end of the animation
2263 glClearColor(0,0,0,1);
2264 glDisable(GL_SCISSOR_TEST);
2265 glClear(GL_COLOR_BUFFER_BIT);
2266 glEnable(GL_SCISSOR_TEST);
2267 hw.flip( Region(hw.bounds()) );
2268
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002269 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002270}
2271
2272status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2273{
Mathias Agopian59119e62010-10-11 12:37:43 -07002274 class MessageTurnElectronBeamOff : public MessageBase {
2275 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002276 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002277 status_t result;
2278 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002279 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2280 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002281 }
2282 status_t getResult() const {
2283 return result;
2284 }
2285 virtual bool handler() {
2286 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002287 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002288 return true;
2289 }
2290 };
2291
Mathias Agopianabd671a2010-10-14 14:54:06 -07002292 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002293 status_t res = postMessageSync(msg);
2294 if (res == NO_ERROR) {
2295 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002296
2297 // work-around: when the power-manager calls us we activate the
2298 // animation. eventually, the "on" animation will be called
2299 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002301 }
2302 return res;
2303}
2304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002306
Mathias Agopianabd671a2010-10-14 14:54:06 -07002307status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002308{
2309 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2310 if (hw.canDraw()) {
2311 // we're already on
2312 return NO_ERROR;
2313 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002314 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2315 electronBeamOnAnimationImplLocked();
2316 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002317
2318 // make sure to redraw the whole screen when the animation is done
2319 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002320 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002321
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002322 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002323}
2324
2325status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2326{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002327 class MessageTurnElectronBeamOn : public MessageBase {
2328 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002329 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002330 status_t result;
2331 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002332 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2333 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002334 }
2335 status_t getResult() const {
2336 return result;
2337 }
2338 virtual bool handler() {
2339 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002340 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002341 return true;
2342 }
2343 };
2344
Mathias Agopianabd671a2010-10-14 14:54:06 -07002345 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002346 return NO_ERROR;
2347}
2348
2349// ---------------------------------------------------------------------------
2350
Mathias Agopian74c40c02010-09-29 13:02:36 -07002351status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2352 sp<IMemoryHeap>* heap,
2353 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002354 uint32_t sw, uint32_t sh,
2355 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002357 ATRACE_CALL();
2358
Mathias Agopian74c40c02010-09-29 13:02:36 -07002359 status_t result = PERMISSION_DENIED;
2360
2361 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002362 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002363 return BAD_VALUE;
2364
2365 if (!GLExtensions::getInstance().haveFramebufferObject())
2366 return INVALID_OPERATION;
2367
2368 // get screen geometry
2369 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2370 const uint32_t hw_w = hw.getWidth();
2371 const uint32_t hw_h = hw.getHeight();
2372
2373 if ((sw > hw_w) || (sh > hw_h))
2374 return BAD_VALUE;
2375
2376 sw = (!sw) ? hw_w : sw;
2377 sh = (!sh) ? hw_h : sh;
2378 const size_t size = sw * sh * 4;
2379
Steve Block9d453682011-12-20 16:23:08 +00002380 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002381 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002382
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 // make sure to clear all GL error flags
2384 while ( glGetError() != GL_NO_ERROR ) ;
2385
2386 // create a FBO
2387 GLuint name, tname;
2388 glGenRenderbuffersOES(1, &tname);
2389 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2390 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002391
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 glGenFramebuffersOES(1, &name);
2393 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2394 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2395 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2396
2397 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002398
Mathias Agopian74c40c02010-09-29 13:02:36 -07002399 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2400
2401 // invert everything, b/c glReadPixel() below will invert the FB
2402 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002403 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002404 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 glMatrixMode(GL_PROJECTION);
2406 glPushMatrix();
2407 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002408 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002409 glMatrixMode(GL_MODELVIEW);
2410
2411 // redraw the screen entirely...
2412 glClearColor(0,0,0,1);
2413 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002414
Jamie Gennis9575f602011-10-07 14:51:16 -07002415 const LayerVector& layers(mDrawingState.layersSortedByZ);
2416 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417 for (size_t i=0 ; i<count ; ++i) {
2418 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002419 const uint32_t flags = layer->drawingState().flags;
2420 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2421 const uint32_t z = layer->drawingState().z;
2422 if (z >= minLayerZ && z <= maxLayerZ) {
2423 layer->drawForSreenShot();
2424 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002425 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002426 }
2427
2428 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002429 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002430 glScissor(0, 0, sw, sh);
2431
2432 // check for errors and return screen capture
2433 if (glGetError() != GL_NO_ERROR) {
2434 // error while rendering
2435 result = INVALID_OPERATION;
2436 } else {
2437 // allocate shared memory large enough to hold the
2438 // screen capture
2439 sp<MemoryHeapBase> base(
2440 new MemoryHeapBase(size, 0, "screen-capture") );
2441 void* const ptr = base->getBase();
2442 if (ptr) {
2443 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002444 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002445 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2446 if (glGetError() == GL_NO_ERROR) {
2447 *heap = base;
2448 *w = sw;
2449 *h = sh;
2450 *f = PIXEL_FORMAT_RGBA_8888;
2451 result = NO_ERROR;
2452 }
2453 } else {
2454 result = NO_MEMORY;
2455 }
2456 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002457 glEnable(GL_SCISSOR_TEST);
2458 glViewport(0, 0, hw_w, hw_h);
2459 glMatrixMode(GL_PROJECTION);
2460 glPopMatrix();
2461 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002462 } else {
2463 result = BAD_VALUE;
2464 }
2465
2466 // release FBO resources
2467 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2468 glDeleteRenderbuffersOES(1, &tname);
2469 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002470
2471 hw.compositionComplete();
2472
Steve Block9d453682011-12-20 16:23:08 +00002473 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002474
Mathias Agopian74c40c02010-09-29 13:02:36 -07002475 return result;
2476}
2477
2478
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002479status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2480 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002481 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002482 uint32_t sw, uint32_t sh,
2483 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002484{
2485 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002486 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002487 return BAD_VALUE;
2488
2489 if (!GLExtensions::getInstance().haveFramebufferObject())
2490 return INVALID_OPERATION;
2491
2492 class MessageCaptureScreen : public MessageBase {
2493 SurfaceFlinger* flinger;
2494 DisplayID dpy;
2495 sp<IMemoryHeap>* heap;
2496 uint32_t* w;
2497 uint32_t* h;
2498 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002499 uint32_t sw;
2500 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002501 uint32_t minLayerZ;
2502 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002503 status_t result;
2504 public:
2505 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002506 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002507 uint32_t sw, uint32_t sh,
2508 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002509 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002510 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2511 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2512 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002513 {
2514 }
2515 status_t getResult() const {
2516 return result;
2517 }
2518 virtual bool handler() {
2519 Mutex::Autolock _l(flinger->mStateLock);
2520
2521 // if we have secure windows, never allow the screen capture
2522 if (flinger->mSecureFrameBuffer)
2523 return true;
2524
Mathias Agopian74c40c02010-09-29 13:02:36 -07002525 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002526 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002527
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002528 return true;
2529 }
2530 };
2531
2532 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002533 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002534 status_t res = postMessageSync(msg);
2535 if (res == NO_ERROR) {
2536 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2537 }
2538 return res;
2539}
2540
2541// ---------------------------------------------------------------------------
2542
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002543sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2544{
2545 sp<Layer> result;
2546 Mutex::Autolock _l(mStateLock);
2547 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2548 return result;
2549}
2550
2551// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002552
Mathias Agopian96f08192010-06-02 23:28:45 -07002553Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002554 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002555{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556}
2557
Mathias Agopian96f08192010-06-02 23:28:45 -07002558Client::~Client()
2559{
Mathias Agopian96f08192010-06-02 23:28:45 -07002560 const size_t count = mLayers.size();
2561 for (size_t i=0 ; i<count ; i++) {
2562 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2563 if (layer != 0) {
2564 mFlinger->removeLayer(layer);
2565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002566 }
2567}
2568
Mathias Agopian96f08192010-06-02 23:28:45 -07002569status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002570 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002571}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002572
Mathias Agopian4f113742011-05-03 16:21:41 -07002573size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002574{
Mathias Agopian4f113742011-05-03 16:21:41 -07002575 Mutex::Autolock _l(mLock);
2576 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002577 mLayers.add(name, layer);
2578 return name;
2579}
2580
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002581void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002582{
Mathias Agopian4f113742011-05-03 16:21:41 -07002583 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002584 // we do a linear search here, because this doesn't happen often
2585 const size_t count = mLayers.size();
2586 for (size_t i=0 ; i<count ; i++) {
2587 if (mLayers.valueAt(i) == layer) {
2588 mLayers.removeItemsAt(i, 1);
2589 break;
2590 }
2591 }
2592}
Mathias Agopian4f113742011-05-03 16:21:41 -07002593sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2594{
2595 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002596 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002597 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002598 if (layer != 0) {
2599 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002600 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002601 }
2602 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002603}
2604
Mathias Agopiana67932f2011-04-20 14:20:59 -07002605
2606status_t Client::onTransact(
2607 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2608{
2609 // these must be checked
2610 IPCThreadState* ipc = IPCThreadState::self();
2611 const int pid = ipc->getCallingPid();
2612 const int uid = ipc->getCallingUid();
2613 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002614 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002615 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002616 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002617 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002618 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002619 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2620 return PERMISSION_DENIED;
2621 }
2622 }
2623 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002624}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002625
2626
Mathias Agopian96f08192010-06-02 23:28:45 -07002627sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002628 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002629 const String8& name,
2630 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631 uint32_t flags)
2632{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002633 /*
2634 * createSurface must be called from the GL thread so that it can
2635 * have access to the GL context.
2636 */
2637
2638 class MessageCreateSurface : public MessageBase {
2639 sp<ISurface> result;
2640 SurfaceFlinger* flinger;
2641 ISurfaceComposerClient::surface_data_t* params;
2642 Client* client;
2643 const String8& name;
2644 DisplayID display;
2645 uint32_t w, h;
2646 PixelFormat format;
2647 uint32_t flags;
2648 public:
2649 MessageCreateSurface(SurfaceFlinger* flinger,
2650 ISurfaceComposerClient::surface_data_t* params,
2651 const String8& name, Client* client,
2652 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2653 uint32_t flags)
2654 : flinger(flinger), params(params), client(client), name(name),
2655 display(display), w(w), h(h), format(format), flags(flags)
2656 {
2657 }
2658 sp<ISurface> getResult() const { return result; }
2659 virtual bool handler() {
2660 result = flinger->createSurface(params, name, client,
2661 display, w, h, format, flags);
2662 return true;
2663 }
2664 };
2665
2666 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2667 params, name, this, display, w, h, format, flags);
2668 mFlinger->postMessageSync(msg);
2669 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002670}
Mathias Agopian96f08192010-06-02 23:28:45 -07002671status_t Client::destroySurface(SurfaceID sid) {
2672 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002674
2675// ---------------------------------------------------------------------------
2676
Jamie Gennis9a78c902011-01-12 18:30:40 -08002677GraphicBufferAlloc::GraphicBufferAlloc() {}
2678
2679GraphicBufferAlloc::~GraphicBufferAlloc() {}
2680
2681sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002682 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002683 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2684 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002685 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002686 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002687 if (err == NO_MEMORY) {
2688 GraphicBuffer::dumpAllocationsToSystemLog();
2689 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002690 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002691 "failed (%s), handle=%p",
2692 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002693 return 0;
2694 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002695 return graphicBuffer;
2696}
2697
Jamie Gennis9a78c902011-01-12 18:30:40 -08002698// ---------------------------------------------------------------------------
2699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002700GraphicPlane::GraphicPlane()
2701 : mHw(0)
2702{
2703}
2704
2705GraphicPlane::~GraphicPlane() {
2706 delete mHw;
2707}
2708
2709bool GraphicPlane::initialized() const {
2710 return mHw ? true : false;
2711}
2712
Mathias Agopian2b92d892010-02-08 15:49:35 -08002713int GraphicPlane::getWidth() const {
2714 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002715}
2716
Mathias Agopian2b92d892010-02-08 15:49:35 -08002717int GraphicPlane::getHeight() const {
2718 return mHeight;
2719}
2720
2721void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2722{
2723 mHw = hw;
2724
2725 // initialize the display orientation transform.
2726 // it's a constant that should come from the display driver.
2727 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2728 char property[PROPERTY_VALUE_MAX];
2729 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2730 //displayOrientation
2731 switch (atoi(property)) {
2732 case 90:
2733 displayOrientation = ISurfaceComposer::eOrientation90;
2734 break;
2735 case 270:
2736 displayOrientation = ISurfaceComposer::eOrientation270;
2737 break;
2738 }
2739 }
2740
2741 const float w = hw->getWidth();
2742 const float h = hw->getHeight();
2743 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2744 &mDisplayTransform);
2745 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2746 mDisplayWidth = h;
2747 mDisplayHeight = w;
2748 } else {
2749 mDisplayWidth = w;
2750 mDisplayHeight = h;
2751 }
2752
2753 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002754}
2755
2756status_t GraphicPlane::orientationToTransfrom(
2757 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002758{
2759 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002760 switch (orientation) {
2761 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002762 flags = Transform::ROT_0;
2763 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002764 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002765 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002766 break;
2767 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002768 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002769 break;
2770 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002771 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002772 break;
2773 default:
2774 return BAD_VALUE;
2775 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002776 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002777 return NO_ERROR;
2778}
2779
2780status_t GraphicPlane::setOrientation(int orientation)
2781{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002782 // If the rotation can be handled in hardware, this is where
2783 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002784
2785 const DisplayHardware& hw(displayHardware());
2786 const float w = mDisplayWidth;
2787 const float h = mDisplayHeight;
2788 mWidth = int(w);
2789 mHeight = int(h);
2790
2791 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002792 GraphicPlane::orientationToTransfrom(orientation, w, h,
2793 &orientationTransform);
2794 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2795 mWidth = int(h);
2796 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002797 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002798
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002799 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002800 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002801 return NO_ERROR;
2802}
2803
2804const DisplayHardware& GraphicPlane::displayHardware() const {
2805 return *mHw;
2806}
2807
Mathias Agopian59119e62010-10-11 12:37:43 -07002808DisplayHardware& GraphicPlane::editDisplayHardware() {
2809 return *mHw;
2810}
2811
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002812const Transform& GraphicPlane::transform() const {
2813 return mGlobalTransform;
2814}
2815
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002816EGLDisplay GraphicPlane::getEGLDisplay() const {
2817 return mHw->getEGLDisplay();
2818}
2819
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002820// ---------------------------------------------------------------------------
2821
2822}; // namespace android