blob: c68f5c1637e621a7c3371a055875ffb54085308e [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 "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070057#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070061#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Glenn Kasten1db13d72011-12-19 13:55:34 -080063#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include <private/gui/SharedBufferStack.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mSecureFrameBuffer(0)
100{
101 init();
102}
103
104void SurfaceFlinger::init()
105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showbackground", value, "0");
115 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Colin Cross3854ed52012-03-23 14:17:18 -0700117#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
Colin Cross3854ed52012-03-23 14:17:18 -0700123#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124
Steve Blocka19954a2012-01-04 20:05:49 +0000125 ALOGI_IF(mDebugRegion, "showupdates enabled");
126 ALOGI_IF(mDebugBackground, "showbackground enabled");
127 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128}
129
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800130void SurfaceFlinger::onFirstRef()
131{
132 mEventQueue.init(this);
133
134 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
135
136 // Wait for the main thread to be done with its initialization
137 mReadyToRunBarrier.wait();
138}
139
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141SurfaceFlinger::~SurfaceFlinger()
142{
143 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}
145
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146void SurfaceFlinger::binderDied(const wp<IBinder>& who)
147{
148 // the window manager died on us. prepare its eulogy.
149
150 // reset screen orientation
151 Vector<ComposerState> state;
152 setTransactionState(state, eOrientationDefault, 0);
153
154 // restart the boot-animation
155 property_set("ctl.start", "bootanim");
156}
157
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700160 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Mathias Agopian7e27f052010-05-28 14:22:23 -0700163sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopian96f08192010-06-02 23:28:45 -0700165 sp<ISurfaceComposerClient> bclient;
166 sp<Client> client(new Client(this));
167 status_t err = client->initCheck();
168 if (err == NO_ERROR) {
169 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 return bclient;
172}
173
Jamie Gennis9a78c902011-01-12 18:30:40 -0800174sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
175{
176 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
177 return gba;
178}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
181{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000182 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 const GraphicPlane& plane(mGraphicPlanes[dpy]);
184 return plane;
185}
186
187GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
188{
189 return const_cast<GraphicPlane&>(
190 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
191}
192
193void SurfaceFlinger::bootFinished()
194{
195 const nsecs_t now = systemTime();
196 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000197 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700198 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700199
200 // wait patiently for the window manager death
201 const String16 name("window");
202 sp<IBinder> window(defaultServiceManager()->getService(name));
203 if (window != 0) {
204 window->linkToDeath(this);
205 }
206
207 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700208 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209}
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211static inline uint16_t pack565(int r, int g, int b) {
212 return (r<<11)|(g<<5)|b;
213}
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215status_t SurfaceFlinger::readyToRun()
216{
Steve Blocka19954a2012-01-04 20:05:49 +0000217 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 "Initializing graphics H/W...");
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 // we only support one display currently
221 int dpy = 0;
222
223 {
224 // initialize the main display
225 GraphicPlane& plane(graphicPlane(dpy));
226 DisplayHardware* const hw = new DisplayHardware(this, dpy);
227 plane.setDisplayHardware(hw);
228 }
229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 // create the shared control-block
231 mServerHeap = new MemoryHeapBase(4096,
232 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700234
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700235 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000236 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700238 new(mServerCblk) surface_flinger_cblk_t;
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 // initialize primary screen
241 // (other display should be initialized in the same manner, but
242 // asynchronously, as they could come and go. None of this is supported
243 // yet).
244 const GraphicPlane& plane(graphicPlane(dpy));
245 const DisplayHardware& hw = plane.displayHardware();
246 const uint32_t w = hw.getWidth();
247 const uint32_t h = hw.getHeight();
248 const uint32_t f = hw.getFormat();
249 hw.makeCurrent();
250
251 // initialize the shared control block
252 mServerCblk->connected |= 1<<dpy;
253 display_cblk_t* dcblk = mServerCblk->displays + dpy;
254 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800255 dcblk->w = plane.getWidth();
256 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 dcblk->format = f;
258 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
259 dcblk->xdpi = hw.getDpiX();
260 dcblk->ydpi = hw.getDpiY();
261 dcblk->fps = hw.getRefreshRate();
262 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263
264 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700266 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glEnableClientState(GL_VERTEX_ARRAY);
268 glEnable(GL_SCISSOR_TEST);
269 glShadeModel(GL_FLAT);
270 glDisable(GL_DITHER);
271 glDisable(GL_CULL_FACE);
272
273 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
274 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700275 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 glGenTextures(1, &mWormholeTexName);
277 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
282 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700283 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
284
285 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
286 glGenTextures(1, &mProtectedTexName);
287 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
293 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
295 glViewport(0, 0, w, h);
296 glMatrixMode(GL_PROJECTION);
297 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700298 // put the origin in the left-bottom corner
299 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 -0800300
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800301
302 // start the EventThread
303 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800304 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800305 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
307 /*
308 * We're now ready to accept clients...
309 */
310
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800311 mReadyToRunBarrier.open();
312
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700313 // start boot animation
314 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
319// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800320
Jamie Gennis582270d2011-08-17 18:19:00 -0700321bool SurfaceFlinger::authenticateSurfaceTexture(
322 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800323 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700324 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800325
326 // Check the visible layer list for the ISurface
327 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
328 size_t count = currentLayers.size();
329 for (size_t i=0 ; i<count ; i++) {
330 const sp<LayerBase>& layer(currentLayers[i]);
331 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700332 if (lbc != NULL) {
333 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
334 if (lbcBinder == surfaceTextureBinder) {
335 return true;
336 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800337 }
338 }
339
340 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700341 // SurfaceTexture gets destroyed before all the clients are done using it,
342 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800343 // will instead fail later on when the client tries to use the surface,
344 // which should be reported as "surface XYZ returned an -ENODEV". The
345 // purgatorized layers are no less authentic than the visible ones, so this
346 // should not cause any harm.
347 size_t purgatorySize = mLayerPurgatory.size();
348 for (size_t i=0 ; i<purgatorySize ; i++) {
349 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
350 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700351 if (lbc != NULL) {
352 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
353 if (lbcBinder == surfaceTextureBinder) {
354 return true;
355 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800356 }
357 }
358
359 return false;
360}
361
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800362// ----------------------------------------------------------------------------
363
364sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800365 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700366}
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800369
370void SurfaceFlinger::waitForEvent() {
371 mEventQueue.waitMessage();
372}
373
374void SurfaceFlinger::signalTransaction() {
375 mEventQueue.invalidate();
376}
377
378void SurfaceFlinger::signalLayerUpdate() {
379 mEventQueue.invalidate();
380}
381
382void SurfaceFlinger::signalRefresh() {
383 mEventQueue.refresh();
384}
385
386status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
387 nsecs_t reltime, uint32_t flags) {
388 return mEventQueue.postMessage(msg, reltime);
389}
390
391status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
392 nsecs_t reltime, uint32_t flags) {
393 status_t res = mEventQueue.postMessage(msg, reltime);
394 if (res == NO_ERROR) {
395 msg->wait();
396 }
397 return res;
398}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
400bool SurfaceFlinger::threadLoop()
401{
402 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 return true;
404}
405
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800406void SurfaceFlinger::onMessageReceived(int32_t what)
407{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800408 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800409 switch (what) {
Mathias Agopian562f4b22012-03-24 17:47:24 -0700410 case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800411 // if we're in a global transaction, don't do anything.
412 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
413 uint32_t transactionFlags = peekTransactionFlags(mask);
414 if (CC_UNLIKELY(transactionFlags)) {
415 handleTransaction(transactionFlags);
416 }
417
418 // post surfaces (if needed)
419 handlePageFlip();
420
Mathias Agopian562f4b22012-03-24 17:47:24 -0700421 signalRefresh();
422
423 } break;
424
425 case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800426
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800427 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800428
Mathias Agopian303d5382012-02-05 01:49:16 -0800429 const DisplayHardware& hw(graphicPlane(0).displayHardware());
430
Mathias Agopianb048cef2012-02-04 15:44:04 -0800431 if (CC_UNLIKELY(mHwWorkListDirty)) {
432 // build the h/w work list
433 handleWorkList();
434 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800435
Mathias Agopianb048cef2012-02-04 15:44:04 -0800436 if (CC_LIKELY(hw.canDraw())) {
437 // repaint the framebuffer (if needed)
438 handleRepaint();
439 // inform the h/w that we're done compositing
440 hw.compositionComplete();
441 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800442 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800443 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800444 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800445 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800446
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800447 } break;
448 }
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451void SurfaceFlinger::postFramebuffer()
452{
Mathias Agopian841cde52012-03-01 15:44:37 -0800453 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800454 // mSwapRegion can be empty here is some cases, for instance if a hidden
455 // or fully transparent window is updating.
456 // in that case, we need to flip anyways to not risk a deadlock with
457 // h/w composer.
458
Mathias Agopiana44b0412011-10-16 18:46:35 -0700459 const DisplayHardware& hw(graphicPlane(0).displayHardware());
460 const nsecs_t now = systemTime();
461 mDebugInSwapBuffers = now;
462 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800463
464 size_t numLayers = mVisibleLayersSortedByZ.size();
465 for (size_t i = 0; i < numLayers; i++) {
466 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
467 }
468
Mathias Agopiana44b0412011-10-16 18:46:35 -0700469 mLastSwapBufferTime = systemTime() - now;
470 mDebugInSwapBuffers = 0;
471 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
475{
Mathias Agopian841cde52012-03-01 15:44:37 -0800476 ATRACE_CALL();
477
Mathias Agopianca4d3602011-05-19 15:38:14 -0700478 Mutex::Autolock _l(mStateLock);
479 const nsecs_t now = systemTime();
480 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481
Mathias Agopianca4d3602011-05-19 15:38:14 -0700482 // Here we're guaranteed that some transaction flags are set
483 // so we can call handleTransactionLocked() unconditionally.
484 // We call getTransactionFlags(), which will also clear the flags,
485 // with mStateLock held to guarantee that mCurrentState won't change
486 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700487
Mathias Agopianca4d3602011-05-19 15:38:14 -0700488 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
489 transactionFlags = getTransactionFlags(mask);
490 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700491
Mathias Agopianca4d3602011-05-19 15:38:14 -0700492 mLastTransactionTime = systemTime() - now;
493 mDebugInTransaction = 0;
494 invalidateHwcGeometry();
495 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700496}
497
Mathias Agopianca4d3602011-05-19 15:38:14 -0700498void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700499{
500 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 const size_t count = currentLayers.size();
502
503 /*
504 * Traversal of the children
505 * (perform the transaction for each of them if needed)
506 */
507
508 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
509 if (layersNeedTransaction) {
510 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
513 if (!trFlags) continue;
514
515 const uint32_t flags = layer->doTransaction(0);
516 if (flags & Layer::eVisibleRegion)
517 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 }
519 }
520
521 /*
522 * Perform our own transaction if needed
523 */
524
525 if (transactionFlags & eTransactionNeeded) {
526 if (mCurrentState.orientation != mDrawingState.orientation) {
527 // the orientation has changed, recompute all visible regions
528 // and invalidate everything.
529
530 const int dpy = 0;
531 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700532 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 GraphicPlane& plane(graphicPlane(dpy));
534 plane.setOrientation(orientation);
535
536 // update the shared control block
537 const DisplayHardware& hw(plane.displayHardware());
538 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
539 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800540 dcblk->w = plane.getWidth();
541 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
543 mVisibleRegionsDirty = true;
544 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 }
546
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700547 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
548 // layers have been added
549 mVisibleRegionsDirty = true;
550 }
551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 // some layers might have been removed, so
553 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700555 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700558 const size_t count = previousLayers.size();
559 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700560 const sp<LayerBase>& layer(previousLayers[i]);
561 if (currentLayers.indexOf( layer ) < 0) {
562 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700563 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700564 }
565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 }
568
569 commitTransaction();
570}
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800573 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574{
Mathias Agopian841cde52012-03-01 15:44:37 -0800575 ATRACE_CALL();
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 const GraphicPlane& plane(graphicPlane(0));
578 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700579 const DisplayHardware& hw(plane.displayHardware());
580 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581
582 Region aboveOpaqueLayers;
583 Region aboveCoveredLayers;
584 Region dirty;
585
586 bool secureFrameBuffer = false;
587
588 size_t i = currentLayers.size();
589 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 layer->validateVisibility(planeTransform);
592
593 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700594 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595
Mathias Agopianab028732010-03-16 16:41:46 -0700596 /*
597 * opaqueRegion: area of a surface that is fully opaque.
598 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700600
601 /*
602 * visibleRegion: area of a surface that is visible on screen
603 * and not fully transparent. This is essentially the layer's
604 * footprint minus the opaque regions above it.
605 * Areas covered by a translucent surface are considered visible.
606 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700608
609 /*
610 * coveredRegion: area of a surface that is covered by all
611 * visible regions above it (which includes the translucent areas).
612 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700614
615
616 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800617 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700618 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700619 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700621 visibleRegion.andSelf(screenRegion);
622 if (!visibleRegion.isEmpty()) {
623 // Remove the transparent area from the visible region
624 if (translucent) {
625 visibleRegion.subtractSelf(layer->transparentRegionScreen);
626 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopianab028732010-03-16 16:41:46 -0700628 // compute the opaque region
629 const int32_t layerOrientation = layer->getOrientation();
630 if (s.alpha==255 && !translucent &&
631 ((layerOrientation & Transform::ROT_INVALID) == false)) {
632 // the opaque region is the layer's footprint
633 opaqueRegion = visibleRegion;
634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636 }
637
Mathias Agopianab028732010-03-16 16:41:46 -0700638 // Clip the covered region to the visible region
639 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
640
641 // Update aboveCoveredLayers for next (lower) layer
642 aboveCoveredLayers.orSelf(visibleRegion);
643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 // subtract the opaque region covered by the layers above us
645 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
647 // compute this layer's dirty region
648 if (layer->contentDirty) {
649 // we need to invalidate the whole region
650 dirty = visibleRegion;
651 // as well, as the old visible region
652 dirty.orSelf(layer->visibleRegionScreen);
653 layer->contentDirty = false;
654 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700655 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700656 * the exposed region consists of two components:
657 * 1) what's VISIBLE now and was COVERED before
658 * 2) what's EXPOSED now less what was EXPOSED before
659 *
660 * note that (1) is conservative, we start with the whole
661 * visible region but only keep what used to be covered by
662 * something -- which mean it may have been exposed.
663 *
664 * (2) handles areas that were not covered by anything but got
665 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700666 */
Mathias Agopianab028732010-03-16 16:41:46 -0700667 const Region newExposed = visibleRegion - coveredRegion;
668 const Region oldVisibleRegion = layer->visibleRegionScreen;
669 const Region oldCoveredRegion = layer->coveredRegionScreen;
670 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
671 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 }
673 dirty.subtractSelf(aboveOpaqueLayers);
674
675 // accumulate to the screen dirty region
676 dirtyRegion.orSelf(dirty);
677
Mathias Agopianab028732010-03-16 16:41:46 -0700678 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 // Store the visible region is screen space
682 layer->setVisibleRegion(visibleRegion);
683 layer->setCoveredRegion(coveredRegion);
684
Mathias Agopian97011222009-07-28 10:57:27 -0700685 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 if (layer->isSecure() && !visibleRegion.isEmpty()) {
687 secureFrameBuffer = true;
688 }
689 }
690
Mathias Agopian97011222009-07-28 10:57:27 -0700691 // invalidate the areas where a layer was removed
692 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
693 mDirtyRegionRemovedLayer.clear();
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 mSecureFrameBuffer = secureFrameBuffer;
696 opaqueRegion = aboveOpaqueLayers;
697}
698
699
700void SurfaceFlinger::commitTransaction()
701{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800702 if (!mLayersPendingRemoval.isEmpty()) {
703 // Notify removed layers now that they can't be drawn from
704 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
705 mLayersPendingRemoval[i]->onRemoved();
706 }
707 mLayersPendingRemoval.clear();
708 }
709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700711 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700712 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713}
714
715void SurfaceFlinger::handlePageFlip()
716{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800717 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800718 const DisplayHardware& hw = graphicPlane(0).displayHardware();
719 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
722 const bool visibleRegions = lockPageFlip(currentLayers);
723
724 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 Region opaqueRegion;
726 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700727
728 /*
729 * rebuild the visible layer list
730 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800731 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700732 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700733 mVisibleLayersSortedByZ.setCapacity(count);
734 for (size_t i=0 ; i<count ; i++) {
735 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
736 mVisibleLayersSortedByZ.add(currentLayers[i]);
737 }
738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 mWormholeRegion = screenRegion.subtract(opaqueRegion);
740 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800741 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
743
744 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700745
746 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 mDirtyRegion.andSelf(screenRegion);
748}
749
Mathias Agopianad456f92011-01-13 17:53:01 -0800750void SurfaceFlinger::invalidateHwcGeometry()
751{
752 mHwWorkListDirty = true;
753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
756{
757 bool recomputeVisibleRegions = false;
758 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700761 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 layer->lockPageFlip(recomputeVisibleRegions);
763 }
764 return recomputeVisibleRegions;
765}
766
767void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
768{
769 const GraphicPlane& plane(graphicPlane(0));
770 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800771 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700772 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700774 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 layer->unlockPageFlip(planeTransform, mDirtyRegion);
776 }
777}
778
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800779void SurfaceFlinger::handleRefresh()
780{
781 bool needInvalidate = false;
782 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
783 const size_t count = currentLayers.size();
784 for (size_t i=0 ; i<count ; i++) {
785 const sp<LayerBase>& layer(currentLayers[i]);
786 if (layer->onPreComposition()) {
787 needInvalidate = true;
788 }
789 }
790 if (needInvalidate) {
791 signalLayerUpdate();
792 }
793}
794
795
Mathias Agopiana350ff92010-08-10 17:14:02 -0700796void SurfaceFlinger::handleWorkList()
797{
798 mHwWorkListDirty = false;
799 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
800 if (hwc.initCheck() == NO_ERROR) {
801 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
802 const size_t count = currentLayers.size();
803 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700804 hwc_layer_t* const cur(hwc.getLayers());
805 for (size_t i=0 ; cur && i<count ; i++) {
806 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700807 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700808 cur[i].compositionType = HWC_FRAMEBUFFER;
809 cur[i].flags |= HWC_SKIP_LAYER;
810 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700811 }
812 }
813}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815void SurfaceFlinger::handleRepaint()
816{
Mathias Agopian841cde52012-03-01 15:44:37 -0800817 ATRACE_CALL();
818
Mathias Agopianb8a55602009-06-26 19:06:36 -0700819 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700820 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821
Glenn Kasten99ed2242011-12-15 09:51:17 -0800822 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 debugFlashRegions();
824 }
825
Mathias Agopianb8a55602009-06-26 19:06:36 -0700826 // set the frame buffer
827 const DisplayHardware& hw(graphicPlane(0).displayHardware());
828 glMatrixMode(GL_MODELVIEW);
829 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
831 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700832 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
833 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700834 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700835 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
836 // takes a rectangle, we must make sure to update that whole
837 // rectangle in that case
838 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700839 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700840 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700841 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700843 // in the BUFFER_PRESERVED case, obviously, we can update only
844 // what's needed and nothing more.
845 // NOTE: this is NOT a common case, as preserving the backbuffer
846 // is costly and usually involves copying the whole update back.
847 }
848 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700849 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700850 // We need to redraw the rectangle that will be updated
851 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700852 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700854 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700855 } else {
856 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700858 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 }
860 }
861
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700862 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 composeSurfaces(mDirtyRegion);
864
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700865 // update the swap region and clear the dirty region
866 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 mDirtyRegion.clear();
868}
869
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700870void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700872 const DisplayHardware& hw(graphicPlane(0).displayHardware());
873 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700874 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700875 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700876 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700877 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700878
Mathias Agopianf384cc32011-09-08 18:31:55 -0700879 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
880 size_t count = layers.size();
881
Steve Blocke6f43dd2012-01-06 19:20:56 +0000882 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700883 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
884 hwc.getNumLayers(), count);
885
886 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700887 if (hwc.initCheck() == NO_ERROR) {
888 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
889 }
Mathias Agopian45721772010-08-12 15:03:26 -0700890
891 /*
892 * update the per-frame h/w composer data for each layer
893 * and build the transparent region of the FB
894 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895 for (size_t i=0 ; i<count ; i++) {
896 const sp<LayerBase>& layer(layers[i]);
897 layer->setPerFrameData(&cur[i]);
898 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700899 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000901 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700902
Mathias Agopianf384cc32011-09-08 18:31:55 -0700903 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700904 // what's happening here is tricky.
905 // we want to clear all the layers with the CLEAR_FB flags
906 // that are opaque.
907 // however, since some GPU are efficient at preserving
908 // the backbuffer, we want to take advantage of that so we do the
909 // clear only in the dirty region (other areas will be preserved
910 // on those GPUs).
911 // NOTE: on non backbuffer preserving GPU, the dirty region
912 // has already been expanded as needed, so the code is correct
913 // there too.
914 //
915 // However, the content of the framebuffer cannot be trusted when
916 // we switch to/from FB/OVERLAY, in which case we need to
917 // expand the dirty region to those areas too.
918 //
919 // Note also that there is a special case when switching from
920 // "no layers in FB" to "some layers in FB", where we need to redraw
921 // the entire FB, since some areas might contain uninitialized
922 // data.
923 //
924 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700925 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700926 // that is, we can't erase anything outside the dirty region.
927
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700928 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700929
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700930 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
931 transparent.set(hw.getBounds());
932 dirtyInOut = transparent;
933 } else {
934 for (size_t i=0 ; i<count ; i++) {
935 const sp<LayerBase>& layer(layers[i]);
936 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
937 transparent.orSelf(layer->visibleRegionScreen);
938 }
939 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
940 if (isOverlay != layer->isOverlay()) {
941 // we transitioned to/from overlay, so add this layer
942 // to the dirty region so the framebuffer can be either
943 // cleared or redrawn.
944 dirtyInOut.orSelf(layer->visibleRegionScreen);
945 }
946 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800947 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700948 // don't erase stuff outside the dirty region
949 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700950 }
951
952 /*
953 * clear the area of the FB that need to be transparent
954 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700955 if (!transparent.isEmpty()) {
956 glClearColor(0,0,0,0);
957 Region::const_iterator it = transparent.begin();
958 Region::const_iterator const end = transparent.end();
959 const int32_t height = hw.getHeight();
960 while (it != end) {
961 const Rect& r(*it++);
962 const GLint sy = height - (r.top + r.height());
963 glScissor(r.left, sy, r.width(), r.height());
964 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800965 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700966 }
967 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700968}
Mathias Agopian45721772010-08-12 15:03:26 -0700969
Mathias Agopianf384cc32011-09-08 18:31:55 -0700970void SurfaceFlinger::composeSurfaces(const Region& dirty)
971{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700972 const DisplayHardware& hw(graphicPlane(0).displayHardware());
973 HWComposer& hwc(hw.getHwComposer());
974
975 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800976 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700977 // should never happen unless the window manager has a bug
978 // draw something...
979 drawWormhole();
980 }
981
Mathias Agopian45721772010-08-12 15:03:26 -0700982 /*
983 * and then, render the layers targeted at the framebuffer
984 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700985
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700986 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700987 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
988 size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700989
990
991 // FIXME: workaround for b/6020860
992 if (hw.getFlags() & DisplayHardware::BUFFER_PRESERVED) {
993 for (size_t i=0 ; i<count ; i++) {
994 if (cur && (cur[i].compositionType == HWC_FRAMEBUFFER)) {
995 glEnable(GL_SCISSOR_TEST);
996 glScissor(0,0,0,0);
997 glClear(GL_COLOR_BUFFER_BIT);
998 break;
999 }
1000 }
1001 }
1002 // FIXME: bug6020860 for b/6020860
1003
1004
Mathias Agopian45721772010-08-12 15:03:26 -07001005 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001006 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001007 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001008 }
1009 const sp<LayerBase>& layer(layers[i]);
1010 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1011 if (!clip.isEmpty()) {
1012 layer->draw(clip);
1013 }
1014 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015}
1016
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017void SurfaceFlinger::debugFlashRegions()
1018{
Mathias Agopian0a917752010-06-14 21:20:00 -07001019 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1020 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001021 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001022 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001023 return;
1024 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001025
Mathias Agopian0a917752010-06-14 21:20:00 -07001026 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1027 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1028 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1029 mDirtyRegion.bounds() : hw.bounds());
1030 composeSurfaces(repaint);
1031 }
1032
Mathias Agopianc492e672011-10-18 14:49:27 -07001033 glDisable(GL_TEXTURE_EXTERNAL_OES);
1034 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 glDisable(GL_SCISSOR_TEST);
1037
Mathias Agopian0926f502009-05-04 14:17:04 -07001038 static int toggle = 0;
1039 toggle = 1 - toggle;
1040 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001041 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001042 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001043 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001044 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045
Mathias Agopian20f68782009-05-11 00:03:41 -07001046 Region::const_iterator it = mDirtyRegion.begin();
1047 Region::const_iterator const end = mDirtyRegion.end();
1048 while (it != end) {
1049 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001051 { r.left, height - r.top },
1052 { r.left, height - r.bottom },
1053 { r.right, height - r.bottom },
1054 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 };
1056 glVertexPointer(2, GL_FLOAT, 0, vertices);
1057 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1058 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001059
Mathias Agopian0656a682011-09-20 17:22:44 -07001060 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061
1062 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001063 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
1065 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066}
1067
1068void SurfaceFlinger::drawWormhole() const
1069{
1070 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1071 if (region.isEmpty())
1072 return;
1073
1074 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1075 const int32_t width = hw.getWidth();
1076 const int32_t height = hw.getHeight();
1077
Glenn Kasten99ed2242011-12-15 09:51:17 -08001078 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001079 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001080 Region::const_iterator it = region.begin();
1081 Region::const_iterator const end = region.end();
1082 while (it != end) {
1083 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 const GLint sy = height - (r.top + r.height());
1085 glScissor(r.left, sy, r.width(), r.height());
1086 glClear(GL_COLOR_BUFFER_BIT);
1087 }
1088 } else {
1089 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1090 { width, height }, { 0, height } };
1091 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 glVertexPointer(2, GL_SHORT, 0, vertices);
1094 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1095 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001096
1097 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 glEnable(GL_TEXTURE_2D);
1099 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1100 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1101 glMatrixMode(GL_TEXTURE);
1102 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001103
1104 glDisable(GL_BLEND);
1105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001107 Region::const_iterator it = region.begin();
1108 Region::const_iterator const end = region.end();
1109 while (it != end) {
1110 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 const GLint sy = height - (r.top + r.height());
1112 glScissor(r.left, sy, r.width(), r.height());
1113 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1114 }
1115 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001116 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001117 glLoadIdentity();
1118 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 }
1120}
1121
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001122status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001123{
1124 Mutex::Autolock _l(mStateLock);
1125 addLayer_l(layer);
1126 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1127 return NO_ERROR;
1128}
1129
Mathias Agopian96f08192010-06-02 23:28:45 -07001130status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1131{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001132 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001133 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1134}
1135
1136ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1137 const sp<LayerBaseClient>& lbc)
1138{
Mathias Agopian96f08192010-06-02 23:28:45 -07001139 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001140 size_t name = client->attachLayer(lbc);
1141
1142 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001143
1144 // add this layer to the current state list
1145 addLayer_l(lbc);
1146
Mathias Agopian4f113742011-05-03 16:21:41 -07001147 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001148}
1149
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001150status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151{
1152 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001153 status_t err = purgatorizeLayer_l(layer);
1154 if (err == NO_ERROR)
1155 setTransactionFlags(eTransactionNeeded);
1156 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157}
1158
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001159status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001161 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1162 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001163 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1166 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001167 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 return NO_ERROR;
1169 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001170 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171}
1172
Mathias Agopian9a112062009-04-17 19:36:26 -07001173status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1174{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001175 // First add the layer to the purgatory list, which makes sure it won't
1176 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001177 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001178 if (err >= 0) {
1179 mLayerPurgatory.add(layerBase);
1180 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001181
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001182 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001183
Mathias Agopian3d579642009-06-04 18:46:21 -07001184 // it's possible that we don't find a layer, because it might
1185 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001186 // from the user because there is a race between Client::destroySurface(),
1187 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001188 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1189}
1190
Mathias Agopian96f08192010-06-02 23:28:45 -07001191status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192{
Mathias Agopian96f08192010-06-02 23:28:45 -07001193 layer->forceVisibilityTransaction();
1194 setTransactionFlags(eTraversalNeeded);
1195 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196}
1197
Mathias Agopiandea20b12011-05-03 17:04:02 -07001198uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1199{
1200 return android_atomic_release_load(&mTransactionFlags);
1201}
1202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1204{
1205 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1206}
1207
Mathias Agopianbb641242010-05-18 17:06:55 -07001208uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209{
1210 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1211 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001212 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 }
1214 return old;
1215}
1216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001218void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001219 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001220 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001221
Jamie Gennis28378392011-10-12 17:39:00 -07001222 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001223 if (mCurrentState.orientation != orientation) {
1224 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1225 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001226 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001227 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001228 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001229 orientation);
1230 }
1231 }
1232
Mathias Agopian698c0872011-06-28 19:09:31 -07001233 const size_t count = state.size();
1234 for (size_t i=0 ; i<count ; i++) {
1235 const ComposerState& s(state[i]);
1236 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001237 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001238 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001239
Mathias Agopian386aa982011-11-07 21:58:03 -08001240 if (transactionFlags) {
1241 // this triggers the transaction
1242 setTransactionFlags(transactionFlags);
1243
1244 // if this is a synchronous transaction, wait for it to take effect
1245 // before returning.
1246 if (flags & eSynchronous) {
1247 mTransationPending = true;
1248 }
1249 while (mTransationPending) {
1250 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1251 if (CC_UNLIKELY(err != NO_ERROR)) {
1252 // just in case something goes wrong in SF, return to the
1253 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001254 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001255 mTransationPending = false;
1256 break;
1257 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001258 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 }
1260}
1261
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001262sp<ISurface> SurfaceFlinger::createSurface(
1263 ISurfaceComposerClient::surface_data_t* params,
1264 const String8& name,
1265 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1267 uint32_t flags)
1268{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001269 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001270 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001271
1272 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001273 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001274 int(w), int(h));
1275 return surfaceHandle;
1276 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001277
Mathias Agopianff615cc2012-02-24 14:58:36 -08001278 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001279 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 switch (flags & eFXSurfaceMask) {
1281 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001282 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1283 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 break;
1285 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001286 // for now we treat Blur as Dim, until we can implement it
1287 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001291 case eFXSurfaceScreenshot:
1292 layer = createScreenshotSurface(client, d, w, h, flags);
1293 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294 }
1295
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001296 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001297 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001298 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001299 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001302 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001303 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001304 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001305 if (normalLayer != 0) {
1306 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001307 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001308 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001309 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001310
Mathias Agopian96f08192010-06-02 23:28:45 -07001311 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 }
1313
1314 return surfaceHandle;
1315}
1316
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001317sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001318 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001319 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001320 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321{
1322 // initialize the surfaces
1323 switch (format) { // TODO: take h/w into account
1324 case PIXEL_FORMAT_TRANSPARENT:
1325 case PIXEL_FORMAT_TRANSLUCENT:
1326 format = PIXEL_FORMAT_RGBA_8888;
1327 break;
1328 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001329#ifdef NO_RGBX_8888
1330 format = PIXEL_FORMAT_RGB_565;
1331#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001332 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001333#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 break;
1335 }
1336
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001337#ifdef NO_RGBX_8888
1338 if (format == PIXEL_FORMAT_RGBX_8888)
1339 format = PIXEL_FORMAT_RGBA_8888;
1340#endif
1341
Mathias Agopian96f08192010-06-02 23:28:45 -07001342 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001343 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001344 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001345 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001346 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 }
1348 return layer;
1349}
1350
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001351sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001352 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001353 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354{
Mathias Agopian96f08192010-06-02 23:28:45 -07001355 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001356 return layer;
1357}
1358
1359sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1360 const sp<Client>& client, DisplayID display,
1361 uint32_t w, uint32_t h, uint32_t flags)
1362{
1363 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 return layer;
1365}
1366
Mathias Agopian96f08192010-06-02 23:28:45 -07001367status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368{
Mathias Agopian9a112062009-04-17 19:36:26 -07001369 /*
1370 * called by the window manager, when a surface should be marked for
1371 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001372 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001373 * The surface is removed from the current and drawing lists, but placed
1374 * in the purgatory queue, so it's not destroyed right-away (we need
1375 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001376 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001377
Mathias Agopian48d819a2009-09-10 19:41:18 -07001378 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001379 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001380 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001381
Mathias Agopian48d819a2009-09-10 19:41:18 -07001382 if (layer != 0) {
1383 err = purgatorizeLayer_l(layer);
1384 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001385 setTransactionFlags(eTransactionNeeded);
1386 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001387 }
1388 return err;
1389}
1390
Mathias Agopianca4d3602011-05-19 15:38:14 -07001391status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001392{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001393 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001394 status_t err = NO_ERROR;
1395 sp<LayerBaseClient> l(layer.promote());
1396 if (l != NULL) {
1397 Mutex::Autolock _l(mStateLock);
1398 err = removeLayer_l(l);
1399 if (err == NAME_NOT_FOUND) {
1400 // The surface wasn't in the current list, which means it was
1401 // removed already, which means it is in the purgatory,
1402 // and need to be removed from there.
1403 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001404 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001405 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001406 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001407 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001408 "error removing layer=%p (%s)", l.get(), strerror(-err));
1409 }
1410 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411}
1412
Mathias Agopian698c0872011-06-28 19:09:31 -07001413uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001414 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001415 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001418 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1419 if (layer != 0) {
1420 const uint32_t what = s.what;
1421 if (what & ePositionChanged) {
1422 if (layer->setPosition(s.x, s.y))
1423 flags |= eTraversalNeeded;
1424 }
1425 if (what & eLayerChanged) {
1426 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1427 if (layer->setLayer(s.z)) {
1428 mCurrentState.layersSortedByZ.removeAt(idx);
1429 mCurrentState.layersSortedByZ.add(layer);
1430 // we need traversal (state changed)
1431 // AND transaction (list changed)
1432 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 }
1434 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001435 if (what & eSizeChanged) {
1436 if (layer->setSize(s.w, s.h)) {
1437 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001438 }
1439 }
1440 if (what & eAlphaChanged) {
1441 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1442 flags |= eTraversalNeeded;
1443 }
1444 if (what & eMatrixChanged) {
1445 if (layer->setMatrix(s.matrix))
1446 flags |= eTraversalNeeded;
1447 }
1448 if (what & eTransparentRegionChanged) {
1449 if (layer->setTransparentRegionHint(s.transparentRegion))
1450 flags |= eTraversalNeeded;
1451 }
1452 if (what & eVisibilityChanged) {
1453 if (layer->setFlags(s.flags, s.mask))
1454 flags |= eTraversalNeeded;
1455 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001457 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458}
1459
Mathias Agopianb60314a2012-04-10 22:09:54 -07001460// ---------------------------------------------------------------------------
1461
1462void SurfaceFlinger::onScreenAcquired() {
1463 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1464 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001465 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001466 // this is a temporary work-around, eventually this should be called
1467 // by the power-manager
1468 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
1469 mDirtyRegion.set(hw.bounds());
Mathias Agopian22ffb112012-04-10 21:04:02 -07001470 // from this point on, SF will process updates again
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471}
1472
Mathias Agopianb60314a2012-04-10 22:09:54 -07001473void SurfaceFlinger::onScreenReleased() {
1474 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1475 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001476 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001477 mDirtyRegion.set(hw.bounds());
1478 hw.releaseScreen();
1479 // from this point on, SF will stop drawing
1480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481}
1482
Mathias Agopianb60314a2012-04-10 22:09:54 -07001483void SurfaceFlinger::screenAcquired() {
1484 class MessageScreenAcquired : public MessageBase {
1485 SurfaceFlinger* flinger;
1486 public:
1487 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1488 virtual bool handler() {
1489 flinger->onScreenAcquired();
1490 return true;
1491 }
1492 };
1493 sp<MessageBase> msg = new MessageScreenAcquired(this);
1494 postMessageSync(msg);
1495}
1496
1497void SurfaceFlinger::screenReleased() {
1498 class MessageScreenReleased : public MessageBase {
1499 SurfaceFlinger* flinger;
1500 public:
1501 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1502 virtual bool handler() {
1503 flinger->onScreenReleased();
1504 return true;
1505 }
1506 };
1507 sp<MessageBase> msg = new MessageScreenReleased(this);
1508 postMessageSync(msg);
1509}
1510
1511// ---------------------------------------------------------------------------
1512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1514{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001515 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 char buffer[SIZE];
1517 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001518
1519 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 snprintf(buffer, SIZE, "Permission Denial: "
1521 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1522 IPCThreadState::self()->getCallingPid(),
1523 IPCThreadState::self()->getCallingUid());
1524 result.append(buffer);
1525 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001526 // Try to get the main lock, but don't insist if we can't
1527 // (this would indicate SF is stuck, but we want to be able to
1528 // print something in dumpsys).
1529 int retry = 3;
1530 while (mStateLock.tryLock()<0 && --retry>=0) {
1531 usleep(1000000);
1532 }
1533 const bool locked(retry >= 0);
1534 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001535 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001536 "SurfaceFlinger appears to be unresponsive, "
1537 "dumping anyways (no locks held)\n");
1538 result.append(buffer);
1539 }
1540
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001541 bool dumpAll = true;
1542 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001543 size_t numArgs = args.size();
1544 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001545 if ((index < numArgs) &&
1546 (args[index] == String16("--list"))) {
1547 index++;
1548 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001549 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001550 }
1551
1552 if ((index < numArgs) &&
1553 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001554 index++;
1555 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001556 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001557 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001558
1559 if ((index < numArgs) &&
1560 (args[index] == String16("--latency-clear"))) {
1561 index++;
1562 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001563 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001566
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001567 if (dumpAll) {
1568 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001569 }
1570
Mathias Agopian9795c422009-08-26 16:36:26 -07001571 if (locked) {
1572 mStateLock.unlock();
1573 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 }
1575 write(fd, result.string(), result.size());
1576 return NO_ERROR;
1577}
1578
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001579void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1580 String8& result, char* buffer, size_t SIZE) const
1581{
1582 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1583 const size_t count = currentLayers.size();
1584 for (size_t i=0 ; i<count ; i++) {
1585 const sp<LayerBase>& layer(currentLayers[i]);
1586 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1587 result.append(buffer);
1588 }
1589}
1590
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001591void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1592 String8& result, char* buffer, size_t SIZE) const
1593{
1594 String8 name;
1595 if (index < args.size()) {
1596 name = String8(args[index]);
1597 index++;
1598 }
1599
1600 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1601 const size_t count = currentLayers.size();
1602 for (size_t i=0 ; i<count ; i++) {
1603 const sp<LayerBase>& layer(currentLayers[i]);
1604 if (name.isEmpty()) {
1605 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1606 result.append(buffer);
1607 }
1608 if (name.isEmpty() || (name == layer->getName())) {
1609 layer->dumpStats(result, buffer, SIZE);
1610 }
1611 }
1612}
1613
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001614void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1615 String8& result, char* buffer, size_t SIZE) const
1616{
1617 String8 name;
1618 if (index < args.size()) {
1619 name = String8(args[index]);
1620 index++;
1621 }
1622
1623 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1624 const size_t count = currentLayers.size();
1625 for (size_t i=0 ; i<count ; i++) {
1626 const sp<LayerBase>& layer(currentLayers[i]);
1627 if (name.isEmpty() || (name == layer->getName())) {
1628 layer->clearStats();
1629 }
1630 }
1631}
1632
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001633void SurfaceFlinger::dumpAllLocked(
1634 String8& result, char* buffer, size_t SIZE) const
1635{
1636 // figure out if we're stuck somewhere
1637 const nsecs_t now = systemTime();
1638 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1639 const nsecs_t inTransaction(mDebugInTransaction);
1640 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1641 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1642
1643 /*
1644 * Dump the visible layer list
1645 */
1646 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1647 const size_t count = currentLayers.size();
1648 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1649 result.append(buffer);
1650 for (size_t i=0 ; i<count ; i++) {
1651 const sp<LayerBase>& layer(currentLayers[i]);
1652 layer->dump(result, buffer, SIZE);
1653 }
1654
1655 /*
1656 * Dump the layers in the purgatory
1657 */
1658
1659 const size_t purgatorySize = mLayerPurgatory.size();
1660 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1661 result.append(buffer);
1662 for (size_t i=0 ; i<purgatorySize ; i++) {
1663 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1664 layer->shortDump(result, buffer, SIZE);
1665 }
1666
1667 /*
1668 * Dump SurfaceFlinger global state
1669 */
1670
1671 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1672 result.append(buffer);
1673
1674 const GLExtensions& extensions(GLExtensions::getInstance());
1675 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1676 extensions.getVendor(),
1677 extensions.getRenderer(),
1678 extensions.getVersion());
1679 result.append(buffer);
1680
1681 snprintf(buffer, SIZE, "EGL : %s\n",
1682 eglQueryString(graphicPlane(0).getEGLDisplay(),
1683 EGL_VERSION_HW_ANDROID));
1684 result.append(buffer);
1685
1686 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1687 result.append(buffer);
1688
1689 mWormholeRegion.dump(result, "WormholeRegion");
1690 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1691 snprintf(buffer, SIZE,
1692 " orientation=%d, canDraw=%d\n",
1693 mCurrentState.orientation, hw.canDraw());
1694 result.append(buffer);
1695 snprintf(buffer, SIZE,
1696 " last eglSwapBuffers() time: %f us\n"
1697 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001698 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001699 " refresh-rate : %f fps\n"
1700 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001701 " y-dpi : %f\n"
1702 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001703 mLastSwapBufferTime/1000.0,
1704 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001705 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001706 hw.getRefreshRate(),
1707 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001708 hw.getDpiY(),
1709 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001710 result.append(buffer);
1711
1712 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1713 inSwapBuffersDuration/1000.0);
1714 result.append(buffer);
1715
1716 snprintf(buffer, SIZE, " transaction time: %f us\n",
1717 inTransactionDuration/1000.0);
1718 result.append(buffer);
1719
1720 /*
1721 * VSYNC state
1722 */
1723 mEventThread->dump(result, buffer, SIZE);
1724
1725 /*
1726 * Dump HWComposer state
1727 */
1728 HWComposer& hwc(hw.getHwComposer());
1729 snprintf(buffer, SIZE, "h/w composer state:\n");
1730 result.append(buffer);
1731 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1732 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1733 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1734 result.append(buffer);
1735 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1736
1737 /*
1738 * Dump gralloc state
1739 */
1740 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1741 alloc.dump(result);
1742 hw.dump(result);
1743}
1744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745status_t SurfaceFlinger::onTransact(
1746 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1747{
1748 switch (code) {
1749 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001750 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001753 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001754 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 {
1756 // codes that require permission check
1757 IPCThreadState* ipc = IPCThreadState::self();
1758 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001759 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001760 if ((uid != AID_GRAPHICS) &&
1761 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001762 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001763 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1764 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001766 break;
1767 }
1768 case CAPTURE_SCREEN:
1769 {
1770 // codes that require permission check
1771 IPCThreadState* ipc = IPCThreadState::self();
1772 const int pid = ipc->getCallingPid();
1773 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001774 if ((uid != AID_GRAPHICS) &&
1775 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001776 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001777 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1778 return PERMISSION_DENIED;
1779 }
1780 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 }
1782 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001783
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1785 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001786 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001787 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001788 IPCThreadState* ipc = IPCThreadState::self();
1789 const int pid = ipc->getCallingPid();
1790 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001791 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001792 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 return PERMISSION_DENIED;
1794 }
1795 int n;
1796 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001797 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001798 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 return NO_ERROR;
1800 case 1002: // SHOW_UPDATES
1801 n = data.readInt32();
1802 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001803 invalidateHwcGeometry();
1804 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805 return NO_ERROR;
1806 case 1003: // SHOW_BACKGROUND
1807 n = data.readInt32();
1808 mDebugBackground = n ? 1 : 0;
1809 return NO_ERROR;
1810 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001811 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001812 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001813 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001814 case 1005:{ // force transaction
1815 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1816 return NO_ERROR;
1817 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001818 case 1006:{ // send empty update
1819 signalRefresh();
1820 return NO_ERROR;
1821 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001822 case 1008: // toggle use of hw composer
1823 n = data.readInt32();
1824 mDebugDisableHWC = n ? 1 : 0;
1825 invalidateHwcGeometry();
1826 repaintEverything();
1827 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001828 case 1009: // toggle use of transform hint
1829 n = data.readInt32();
1830 mDebugDisableTransformHint = n ? 1 : 0;
1831 invalidateHwcGeometry();
1832 repaintEverything();
1833 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001835 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836 reply->writeInt32(0);
1837 reply->writeInt32(mDebugRegion);
1838 reply->writeInt32(mDebugBackground);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001839 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840 return NO_ERROR;
1841 case 1013: {
1842 Mutex::Autolock _l(mStateLock);
1843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1844 reply->writeInt32(hw.getPageFlipCount());
1845 }
1846 return NO_ERROR;
1847 }
1848 }
1849 return err;
1850}
1851
Mathias Agopian53331da2011-08-22 21:44:41 -07001852void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001853 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001854 const Rect bounds(hw.getBounds());
1855 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001856 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001857}
1858
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001859void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1860 Mutex::Autolock _l(mInvalidateLock);
1861 mInvalidateRegion = reg;
1862}
1863
1864Region SurfaceFlinger::getAndClearInvalidateRegion() {
1865 Mutex::Autolock _l(mInvalidateLock);
1866 Region reg(mInvalidateRegion);
1867 mInvalidateRegion.clear();
1868 return reg;
1869}
1870
Mathias Agopian59119e62010-10-11 12:37:43 -07001871// ---------------------------------------------------------------------------
1872
Mathias Agopian118d0242011-10-13 16:02:48 -07001873status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1874 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1875{
1876 Mutex::Autolock _l(mStateLock);
1877 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1878}
1879
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001880status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1881 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001882{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001883 ATRACE_CALL();
1884
Mathias Agopian59119e62010-10-11 12:37:43 -07001885 if (!GLExtensions::getInstance().haveFramebufferObject())
1886 return INVALID_OPERATION;
1887
1888 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001889 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001890 const uint32_t hw_w = hw.getWidth();
1891 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001892 GLfloat u = 1;
1893 GLfloat v = 1;
1894
1895 // make sure to clear all GL error flags
1896 while ( glGetError() != GL_NO_ERROR ) ;
1897
1898 // create a FBO
1899 GLuint name, tname;
1900 glGenTextures(1, &tname);
1901 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001902 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1903 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001904 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001905 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001906 GLint tw = (2 << (31 - clz(hw_w)));
1907 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001908 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1909 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001910 u = GLfloat(hw_w) / tw;
1911 v = GLfloat(hw_h) / th;
1912 }
1913 glGenFramebuffersOES(1, &name);
1914 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001915 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1916 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001917
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001918 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001919 glDisable(GL_TEXTURE_EXTERNAL_OES);
1920 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001921 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001922 glClearColor(0,0,0,1);
1923 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001924 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001925 glMatrixMode(GL_MODELVIEW);
1926 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001927 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1928 const size_t count = layers.size();
1929 for (size_t i=0 ; i<count ; ++i) {
1930 const sp<LayerBase>& layer(layers[i]);
1931 layer->drawForSreenShot();
1932 }
1933
Mathias Agopian118d0242011-10-13 16:02:48 -07001934 hw.compositionComplete();
1935
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001936 // back to main framebuffer
1937 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1938 glDisable(GL_SCISSOR_TEST);
1939 glDeleteFramebuffersOES(1, &name);
1940
1941 *textureName = tname;
1942 *uOut = u;
1943 *vOut = v;
1944 return NO_ERROR;
1945}
1946
1947// ---------------------------------------------------------------------------
1948
1949status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1950{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001951 // get screen geometry
1952 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1953 const uint32_t hw_w = hw.getWidth();
1954 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001955 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001956
1957 GLfloat u, v;
1958 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001959 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001960 if (result != NO_ERROR) {
1961 return result;
1962 }
1963
1964 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001965 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001966 glBindTexture(GL_TEXTURE_2D, tname);
1967 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1968 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1969 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001970 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1971 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1973 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1974 glVertexPointer(2, GL_FLOAT, 0, vtx);
1975
Mathias Agopianffcf4652011-07-07 17:30:31 -07001976 /*
1977 * Texture coordinate mapping
1978 *
1979 * u
1980 * 1 +----------+---+
1981 * | | | | image is inverted
1982 * | V | | w.r.t. the texture
1983 * 1-v +----------+ | coordinates
1984 * | |
1985 * | |
1986 * | |
1987 * 0 +--------------+
1988 * 0 1
1989 *
1990 */
1991
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001992 class s_curve_interpolator {
1993 const float nbFrames, s, v;
1994 public:
1995 s_curve_interpolator(int nbFrames, float s)
1996 : nbFrames(1.0f / (nbFrames-1)), s(s),
1997 v(1.0f + expf(-s + 0.5f*s)) {
1998 }
1999 float operator()(int f) {
2000 const float x = f * nbFrames;
2001 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2002 }
2003 };
2004
2005 class v_stretch {
2006 const GLfloat hw_w, hw_h;
2007 public:
2008 v_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 = hw_h - (hw_h * v);
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 class h_stretch {
2024 const GLfloat hw_w, hw_h;
2025 public:
2026 h_stretch(uint32_t hw_w, uint32_t hw_h)
2027 : hw_w(hw_w), hw_h(hw_h) {
2028 }
2029 void operator()(GLfloat* vtx, float v) {
2030 const GLfloat w = hw_w - (hw_w * v);
2031 const GLfloat h = 1.0f;
2032 const GLfloat x = (hw_w - w) * 0.5f;
2033 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002034 vtx[0] = x; vtx[1] = y;
2035 vtx[2] = x; vtx[3] = y + h;
2036 vtx[4] = x + w; vtx[5] = y + h;
2037 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 }
2039 };
2040
2041 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002042 char value[PROPERTY_VALUE_MAX];
2043 property_get("debug.sf.electron_frames", value, "24");
2044 int nbFrames = (atoi(value) + 1) >> 1;
2045 if (nbFrames <= 0) // just in case
2046 nbFrames = 24;
2047
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002048 s_curve_interpolator itr(nbFrames, 7.5f);
2049 s_curve_interpolator itg(nbFrames, 8.0f);
2050 s_curve_interpolator itb(nbFrames, 8.5f);
2051
2052 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002053
2054 glMatrixMode(GL_TEXTURE);
2055 glLoadIdentity();
2056 glMatrixMode(GL_MODELVIEW);
2057 glLoadIdentity();
2058
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002059 glEnable(GL_BLEND);
2060 glBlendFunc(GL_ONE, GL_ONE);
2061 for (int i=0 ; i<nbFrames ; i++) {
2062 float x, y, w, h;
2063 const float vr = itr(i);
2064 const float vg = itg(i);
2065 const float vb = itb(i);
2066
2067 // clear screen
2068 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002069 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002070 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002071
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 // draw the red plane
2073 vverts(vtx, vr);
2074 glColorMask(1,0,0,1);
2075 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002076
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002077 // draw the green plane
2078 vverts(vtx, vg);
2079 glColorMask(0,1,0,1);
2080 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002081
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082 // draw the blue plane
2083 vverts(vtx, vb);
2084 glColorMask(0,0,1,1);
2085 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002086
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002087 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002088 glDisable(GL_TEXTURE_2D);
2089 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002090 glColor4f(vg, vg, vg, 1);
2091 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2092 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002093 }
2094
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002095 h_stretch hverts(hw_w, hw_h);
2096 glDisable(GL_BLEND);
2097 glDisable(GL_TEXTURE_2D);
2098 glColorMask(1,1,1,1);
2099 for (int i=0 ; i<nbFrames ; i++) {
2100 const float v = itg(i);
2101 hverts(vtx, v);
2102 glClear(GL_COLOR_BUFFER_BIT);
2103 glColor4f(1-v, 1-v, 1-v, 1);
2104 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2105 hw.flip(screenBounds);
2106 }
2107
2108 glColorMask(1,1,1,1);
2109 glEnable(GL_SCISSOR_TEST);
2110 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2111 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002112 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002113 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002114 return NO_ERROR;
2115}
2116
2117status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2118{
2119 status_t result = PERMISSION_DENIED;
2120
2121 if (!GLExtensions::getInstance().haveFramebufferObject())
2122 return INVALID_OPERATION;
2123
2124
2125 // get screen geometry
2126 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2127 const uint32_t hw_w = hw.getWidth();
2128 const uint32_t hw_h = hw.getHeight();
2129 const Region screenBounds(hw.bounds());
2130
2131 GLfloat u, v;
2132 GLuint tname;
2133 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2134 if (result != NO_ERROR) {
2135 return result;
2136 }
2137
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002138 GLfloat vtx[8];
2139 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002140 glBindTexture(GL_TEXTURE_2D, tname);
2141 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2142 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2143 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002144 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2145 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002146 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2147 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2148 glVertexPointer(2, GL_FLOAT, 0, vtx);
2149
2150 class s_curve_interpolator {
2151 const float nbFrames, s, v;
2152 public:
2153 s_curve_interpolator(int nbFrames, float s)
2154 : nbFrames(1.0f / (nbFrames-1)), s(s),
2155 v(1.0f + expf(-s + 0.5f*s)) {
2156 }
2157 float operator()(int f) {
2158 const float x = f * nbFrames;
2159 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2160 }
2161 };
2162
2163 class v_stretch {
2164 const GLfloat hw_w, hw_h;
2165 public:
2166 v_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 = hw_h - (hw_h * v);
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
2181 class h_stretch {
2182 const GLfloat hw_w, hw_h;
2183 public:
2184 h_stretch(uint32_t hw_w, uint32_t hw_h)
2185 : hw_w(hw_w), hw_h(hw_h) {
2186 }
2187 void operator()(GLfloat* vtx, float v) {
2188 const GLfloat w = hw_w - (hw_w * v);
2189 const GLfloat h = 1.0f;
2190 const GLfloat x = (hw_w - w) * 0.5f;
2191 const GLfloat y = (hw_h - h) * 0.5f;
2192 vtx[0] = x; vtx[1] = y;
2193 vtx[2] = x; vtx[3] = y + h;
2194 vtx[4] = x + w; vtx[5] = y + h;
2195 vtx[6] = x + w; vtx[7] = y;
2196 }
2197 };
2198
Mathias Agopiana6546e52010-10-14 12:33:07 -07002199 // the full animation is 12 frames
2200 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002201 s_curve_interpolator itr(nbFrames, 7.5f);
2202 s_curve_interpolator itg(nbFrames, 8.0f);
2203 s_curve_interpolator itb(nbFrames, 8.5f);
2204
2205 h_stretch hverts(hw_w, hw_h);
2206 glDisable(GL_BLEND);
2207 glDisable(GL_TEXTURE_2D);
2208 glColorMask(1,1,1,1);
2209 for (int i=nbFrames-1 ; i>=0 ; i--) {
2210 const float v = itg(i);
2211 hverts(vtx, v);
2212 glClear(GL_COLOR_BUFFER_BIT);
2213 glColor4f(1-v, 1-v, 1-v, 1);
2214 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2215 hw.flip(screenBounds);
2216 }
2217
Mathias Agopiana6546e52010-10-14 12:33:07 -07002218 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002219 v_stretch vverts(hw_w, hw_h);
2220 glEnable(GL_BLEND);
2221 glBlendFunc(GL_ONE, GL_ONE);
2222 for (int i=nbFrames-1 ; i>=0 ; i--) {
2223 float x, y, w, h;
2224 const float vr = itr(i);
2225 const float vg = itg(i);
2226 const float vb = itb(i);
2227
2228 // clear screen
2229 glColorMask(1,1,1,1);
2230 glClear(GL_COLOR_BUFFER_BIT);
2231 glEnable(GL_TEXTURE_2D);
2232
2233 // draw the red plane
2234 vverts(vtx, vr);
2235 glColorMask(1,0,0,1);
2236 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2237
2238 // draw the green plane
2239 vverts(vtx, vg);
2240 glColorMask(0,1,0,1);
2241 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2242
2243 // draw the blue plane
2244 vverts(vtx, vb);
2245 glColorMask(0,0,1,1);
2246 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2247
2248 hw.flip(screenBounds);
2249 }
2250
2251 glColorMask(1,1,1,1);
2252 glEnable(GL_SCISSOR_TEST);
2253 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002254 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002255 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002256 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002257
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002258 return NO_ERROR;
2259}
2260
2261// ---------------------------------------------------------------------------
2262
Mathias Agopianabd671a2010-10-14 14:54:06 -07002263status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002265 ATRACE_CALL();
2266
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002267 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2268 if (!hw.canDraw()) {
2269 // we're already off
2270 return NO_ERROR;
2271 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002272
2273 // turn off hwc while we're doing the animation
2274 hw.getHwComposer().disable();
2275 // and make sure to turn it back on (if needed) next time we compose
2276 invalidateHwcGeometry();
2277
Mathias Agopianabd671a2010-10-14 14:54:06 -07002278 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2279 electronBeamOffAnimationImplLocked();
2280 }
2281
2282 // always clear the whole screen at the end of the animation
2283 glClearColor(0,0,0,1);
2284 glDisable(GL_SCISSOR_TEST);
2285 glClear(GL_COLOR_BUFFER_BIT);
2286 glEnable(GL_SCISSOR_TEST);
2287 hw.flip( Region(hw.bounds()) );
2288
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002289 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002290}
2291
2292status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2293{
Mathias Agopian59119e62010-10-11 12:37:43 -07002294 class MessageTurnElectronBeamOff : public MessageBase {
2295 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002296 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002297 status_t result;
2298 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002299 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2300 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002301 }
2302 status_t getResult() const {
2303 return result;
2304 }
2305 virtual bool handler() {
2306 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002307 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002308 return true;
2309 }
2310 };
2311
Mathias Agopianabd671a2010-10-14 14:54:06 -07002312 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002313 status_t res = postMessageSync(msg);
2314 if (res == NO_ERROR) {
2315 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002316
2317 // work-around: when the power-manager calls us we activate the
2318 // animation. eventually, the "on" animation will be called
2319 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002320 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002321 }
2322 return res;
2323}
2324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002325// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002326
Mathias Agopianabd671a2010-10-14 14:54:06 -07002327status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002328{
2329 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2330 if (hw.canDraw()) {
2331 // we're already on
2332 return NO_ERROR;
2333 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002334 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2335 electronBeamOnAnimationImplLocked();
2336 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002337
2338 // make sure to redraw the whole screen when the animation is done
2339 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002340 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002341
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002342 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002343}
2344
2345status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2346{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002347 class MessageTurnElectronBeamOn : public MessageBase {
2348 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002349 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002350 status_t result;
2351 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002352 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2353 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002354 }
2355 status_t getResult() const {
2356 return result;
2357 }
2358 virtual bool handler() {
2359 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002360 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002361 return true;
2362 }
2363 };
2364
Mathias Agopianabd671a2010-10-14 14:54:06 -07002365 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002366 return NO_ERROR;
2367}
2368
2369// ---------------------------------------------------------------------------
2370
Mathias Agopian74c40c02010-09-29 13:02:36 -07002371status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2372 sp<IMemoryHeap>* heap,
2373 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002374 uint32_t sw, uint32_t sh,
2375 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002377 ATRACE_CALL();
2378
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 status_t result = PERMISSION_DENIED;
2380
2381 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002382 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 return BAD_VALUE;
2384
2385 if (!GLExtensions::getInstance().haveFramebufferObject())
2386 return INVALID_OPERATION;
2387
2388 // get screen geometry
2389 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2390 const uint32_t hw_w = hw.getWidth();
2391 const uint32_t hw_h = hw.getHeight();
2392
2393 if ((sw > hw_w) || (sh > hw_h))
2394 return BAD_VALUE;
2395
2396 sw = (!sw) ? hw_w : sw;
2397 sh = (!sh) ? hw_h : sh;
2398 const size_t size = sw * sh * 4;
2399
Steve Block9d453682011-12-20 16:23:08 +00002400 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002401 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002402
Mathias Agopian74c40c02010-09-29 13:02:36 -07002403 // make sure to clear all GL error flags
2404 while ( glGetError() != GL_NO_ERROR ) ;
2405
2406 // create a FBO
2407 GLuint name, tname;
2408 glGenRenderbuffersOES(1, &tname);
2409 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2410 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002411
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 glGenFramebuffersOES(1, &name);
2413 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2414 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2415 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2416
2417 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002418
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2420
2421 // invert everything, b/c glReadPixel() below will invert the FB
2422 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002423 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002424 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002425 glMatrixMode(GL_PROJECTION);
2426 glPushMatrix();
2427 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002428 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002429 glMatrixMode(GL_MODELVIEW);
2430
2431 // redraw the screen entirely...
2432 glClearColor(0,0,0,1);
2433 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002434
Jamie Gennis9575f602011-10-07 14:51:16 -07002435 const LayerVector& layers(mDrawingState.layersSortedByZ);
2436 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002437 for (size_t i=0 ; i<count ; ++i) {
2438 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002439 const uint32_t flags = layer->drawingState().flags;
2440 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2441 const uint32_t z = layer->drawingState().z;
2442 if (z >= minLayerZ && z <= maxLayerZ) {
2443 layer->drawForSreenShot();
2444 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002445 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002446 }
2447
2448 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002449 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002450 glScissor(0, 0, sw, sh);
2451
2452 // check for errors and return screen capture
2453 if (glGetError() != GL_NO_ERROR) {
2454 // error while rendering
2455 result = INVALID_OPERATION;
2456 } else {
2457 // allocate shared memory large enough to hold the
2458 // screen capture
2459 sp<MemoryHeapBase> base(
2460 new MemoryHeapBase(size, 0, "screen-capture") );
2461 void* const ptr = base->getBase();
2462 if (ptr) {
2463 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002464 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002465 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2466 if (glGetError() == GL_NO_ERROR) {
2467 *heap = base;
2468 *w = sw;
2469 *h = sh;
2470 *f = PIXEL_FORMAT_RGBA_8888;
2471 result = NO_ERROR;
2472 }
2473 } else {
2474 result = NO_MEMORY;
2475 }
2476 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002477 glEnable(GL_SCISSOR_TEST);
2478 glViewport(0, 0, hw_w, hw_h);
2479 glMatrixMode(GL_PROJECTION);
2480 glPopMatrix();
2481 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002482 } else {
2483 result = BAD_VALUE;
2484 }
2485
2486 // release FBO resources
2487 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2488 glDeleteRenderbuffersOES(1, &tname);
2489 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002490
2491 hw.compositionComplete();
2492
Steve Block9d453682011-12-20 16:23:08 +00002493 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002494
Mathias Agopian74c40c02010-09-29 13:02:36 -07002495 return result;
2496}
2497
2498
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002499status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2500 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002501 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002502 uint32_t sw, uint32_t sh,
2503 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504{
2505 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002506 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002507 return BAD_VALUE;
2508
2509 if (!GLExtensions::getInstance().haveFramebufferObject())
2510 return INVALID_OPERATION;
2511
2512 class MessageCaptureScreen : public MessageBase {
2513 SurfaceFlinger* flinger;
2514 DisplayID dpy;
2515 sp<IMemoryHeap>* heap;
2516 uint32_t* w;
2517 uint32_t* h;
2518 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002519 uint32_t sw;
2520 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002521 uint32_t minLayerZ;
2522 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002523 status_t result;
2524 public:
2525 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002526 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002527 uint32_t sw, uint32_t sh,
2528 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002529 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002530 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2531 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2532 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002533 {
2534 }
2535 status_t getResult() const {
2536 return result;
2537 }
2538 virtual bool handler() {
2539 Mutex::Autolock _l(flinger->mStateLock);
2540
2541 // if we have secure windows, never allow the screen capture
2542 if (flinger->mSecureFrameBuffer)
2543 return true;
2544
Mathias Agopian74c40c02010-09-29 13:02:36 -07002545 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002546 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002547
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002548 return true;
2549 }
2550 };
2551
2552 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002553 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002554 status_t res = postMessageSync(msg);
2555 if (res == NO_ERROR) {
2556 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2557 }
2558 return res;
2559}
2560
2561// ---------------------------------------------------------------------------
2562
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002563sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2564{
2565 sp<Layer> result;
2566 Mutex::Autolock _l(mStateLock);
2567 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2568 return result;
2569}
2570
2571// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002572
Mathias Agopian96f08192010-06-02 23:28:45 -07002573Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002574 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002575{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002576}
2577
Mathias Agopian96f08192010-06-02 23:28:45 -07002578Client::~Client()
2579{
Mathias Agopian96f08192010-06-02 23:28:45 -07002580 const size_t count = mLayers.size();
2581 for (size_t i=0 ; i<count ; i++) {
2582 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2583 if (layer != 0) {
2584 mFlinger->removeLayer(layer);
2585 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002586 }
2587}
2588
Mathias Agopian96f08192010-06-02 23:28:45 -07002589status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002590 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002592
Mathias Agopian4f113742011-05-03 16:21:41 -07002593size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002594{
Mathias Agopian4f113742011-05-03 16:21:41 -07002595 Mutex::Autolock _l(mLock);
2596 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002597 mLayers.add(name, layer);
2598 return name;
2599}
2600
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002601void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002602{
Mathias Agopian4f113742011-05-03 16:21:41 -07002603 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002604 // we do a linear search here, because this doesn't happen often
2605 const size_t count = mLayers.size();
2606 for (size_t i=0 ; i<count ; i++) {
2607 if (mLayers.valueAt(i) == layer) {
2608 mLayers.removeItemsAt(i, 1);
2609 break;
2610 }
2611 }
2612}
Mathias Agopian4f113742011-05-03 16:21:41 -07002613sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2614{
2615 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002616 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002617 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002618 if (layer != 0) {
2619 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002620 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002621 }
2622 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623}
2624
Mathias Agopiana67932f2011-04-20 14:20:59 -07002625
2626status_t Client::onTransact(
2627 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2628{
2629 // these must be checked
2630 IPCThreadState* ipc = IPCThreadState::self();
2631 const int pid = ipc->getCallingPid();
2632 const int uid = ipc->getCallingUid();
2633 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002634 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002635 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002636 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002637 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002638 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002639 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2640 return PERMISSION_DENIED;
2641 }
2642 }
2643 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002644}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002645
2646
Mathias Agopian96f08192010-06-02 23:28:45 -07002647sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002648 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002649 const String8& name,
2650 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 uint32_t flags)
2652{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002653 /*
2654 * createSurface must be called from the GL thread so that it can
2655 * have access to the GL context.
2656 */
2657
2658 class MessageCreateSurface : public MessageBase {
2659 sp<ISurface> result;
2660 SurfaceFlinger* flinger;
2661 ISurfaceComposerClient::surface_data_t* params;
2662 Client* client;
2663 const String8& name;
2664 DisplayID display;
2665 uint32_t w, h;
2666 PixelFormat format;
2667 uint32_t flags;
2668 public:
2669 MessageCreateSurface(SurfaceFlinger* flinger,
2670 ISurfaceComposerClient::surface_data_t* params,
2671 const String8& name, Client* client,
2672 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2673 uint32_t flags)
2674 : flinger(flinger), params(params), client(client), name(name),
2675 display(display), w(w), h(h), format(format), flags(flags)
2676 {
2677 }
2678 sp<ISurface> getResult() const { return result; }
2679 virtual bool handler() {
2680 result = flinger->createSurface(params, name, client,
2681 display, w, h, format, flags);
2682 return true;
2683 }
2684 };
2685
2686 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2687 params, name, this, display, w, h, format, flags);
2688 mFlinger->postMessageSync(msg);
2689 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690}
Mathias Agopian96f08192010-06-02 23:28:45 -07002691status_t Client::destroySurface(SurfaceID sid) {
2692 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002693}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002694
2695// ---------------------------------------------------------------------------
2696
Jamie Gennis9a78c902011-01-12 18:30:40 -08002697GraphicBufferAlloc::GraphicBufferAlloc() {}
2698
2699GraphicBufferAlloc::~GraphicBufferAlloc() {}
2700
2701sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002702 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002703 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2704 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002705 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002706 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002707 if (err == NO_MEMORY) {
2708 GraphicBuffer::dumpAllocationsToSystemLog();
2709 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002710 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002711 "failed (%s), handle=%p",
2712 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002713 return 0;
2714 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002715 return graphicBuffer;
2716}
2717
Jamie Gennis9a78c902011-01-12 18:30:40 -08002718// ---------------------------------------------------------------------------
2719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002720GraphicPlane::GraphicPlane()
2721 : mHw(0)
2722{
2723}
2724
2725GraphicPlane::~GraphicPlane() {
2726 delete mHw;
2727}
2728
2729bool GraphicPlane::initialized() const {
2730 return mHw ? true : false;
2731}
2732
Mathias Agopian2b92d892010-02-08 15:49:35 -08002733int GraphicPlane::getWidth() const {
2734 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002735}
2736
Mathias Agopian2b92d892010-02-08 15:49:35 -08002737int GraphicPlane::getHeight() const {
2738 return mHeight;
2739}
2740
2741void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2742{
2743 mHw = hw;
2744
2745 // initialize the display orientation transform.
2746 // it's a constant that should come from the display driver.
2747 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2748 char property[PROPERTY_VALUE_MAX];
2749 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2750 //displayOrientation
2751 switch (atoi(property)) {
2752 case 90:
2753 displayOrientation = ISurfaceComposer::eOrientation90;
2754 break;
2755 case 270:
2756 displayOrientation = ISurfaceComposer::eOrientation270;
2757 break;
2758 }
2759 }
2760
2761 const float w = hw->getWidth();
2762 const float h = hw->getHeight();
2763 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2764 &mDisplayTransform);
2765 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2766 mDisplayWidth = h;
2767 mDisplayHeight = w;
2768 } else {
2769 mDisplayWidth = w;
2770 mDisplayHeight = h;
2771 }
2772
2773 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002774}
2775
2776status_t GraphicPlane::orientationToTransfrom(
2777 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002778{
2779 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002780 switch (orientation) {
2781 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002782 flags = Transform::ROT_0;
2783 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002784 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002785 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002786 break;
2787 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002788 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002789 break;
2790 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002791 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002792 break;
2793 default:
2794 return BAD_VALUE;
2795 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002796 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002797 return NO_ERROR;
2798}
2799
2800status_t GraphicPlane::setOrientation(int orientation)
2801{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002802 // If the rotation can be handled in hardware, this is where
2803 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002804
2805 const DisplayHardware& hw(displayHardware());
2806 const float w = mDisplayWidth;
2807 const float h = mDisplayHeight;
2808 mWidth = int(w);
2809 mHeight = int(h);
2810
2811 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002812 GraphicPlane::orientationToTransfrom(orientation, w, h,
2813 &orientationTransform);
2814 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2815 mWidth = int(h);
2816 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002817 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002818
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002819 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002820 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002821 return NO_ERROR;
2822}
2823
2824const DisplayHardware& GraphicPlane::displayHardware() const {
2825 return *mHw;
2826}
2827
Mathias Agopian59119e62010-10-11 12:37:43 -07002828DisplayHardware& GraphicPlane::editDisplayHardware() {
2829 return *mHw;
2830}
2831
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002832const Transform& GraphicPlane::transform() const {
2833 return mGlobalTransform;
2834}
2835
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002836EGLDisplay GraphicPlane::getEGLDisplay() const {
2837 return mHw->getEGLDisplay();
2838}
2839
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002840// ---------------------------------------------------------------------------
2841
2842}; // namespace android