blob: ce6c4a060a40aab22689a2bf3af4adef1266b9fd [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 Agopian303d5382012-02-05 01:49:16 -0800410 case MessageQueue::REFRESH: {
411// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800412 // if we're in a global transaction, don't do anything.
413 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
414 uint32_t transactionFlags = peekTransactionFlags(mask);
415 if (CC_UNLIKELY(transactionFlags)) {
416 handleTransaction(transactionFlags);
417 }
418
419 // post surfaces (if needed)
420 handlePageFlip();
421
Mathias Agopian303d5382012-02-05 01:49:16 -0800422// signalRefresh();
423//
424// } break;
425//
426// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800427
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800428 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800429
Mathias Agopian303d5382012-02-05 01:49:16 -0800430 const DisplayHardware& hw(graphicPlane(0).displayHardware());
431
432// if (mDirtyRegion.isEmpty()) {
433// return;
434// }
435
Mathias Agopianb048cef2012-02-04 15:44:04 -0800436 if (CC_UNLIKELY(mHwWorkListDirty)) {
437 // build the h/w work list
438 handleWorkList();
439 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800440
Mathias Agopianb048cef2012-02-04 15:44:04 -0800441 if (CC_LIKELY(hw.canDraw())) {
442 // repaint the framebuffer (if needed)
443 handleRepaint();
444 // inform the h/w that we're done compositing
445 hw.compositionComplete();
446 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800447 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800448 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800449 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800450 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800451
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800452 } break;
453 }
454}
455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456void SurfaceFlinger::postFramebuffer()
457{
Mathias Agopian841cde52012-03-01 15:44:37 -0800458 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800459 // mSwapRegion can be empty here is some cases, for instance if a hidden
460 // or fully transparent window is updating.
461 // in that case, we need to flip anyways to not risk a deadlock with
462 // h/w composer.
463
Mathias Agopiana44b0412011-10-16 18:46:35 -0700464 const DisplayHardware& hw(graphicPlane(0).displayHardware());
465 const nsecs_t now = systemTime();
466 mDebugInSwapBuffers = now;
467 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800468
469 size_t numLayers = mVisibleLayersSortedByZ.size();
470 for (size_t i = 0; i < numLayers; i++) {
471 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
472 }
473
Mathias Agopiana44b0412011-10-16 18:46:35 -0700474 mLastSwapBufferTime = systemTime() - now;
475 mDebugInSwapBuffers = 0;
476 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477}
478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
480{
Mathias Agopian841cde52012-03-01 15:44:37 -0800481 ATRACE_CALL();
482
Mathias Agopianca4d3602011-05-19 15:38:14 -0700483 Mutex::Autolock _l(mStateLock);
484 const nsecs_t now = systemTime();
485 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486
Mathias Agopianca4d3602011-05-19 15:38:14 -0700487 // Here we're guaranteed that some transaction flags are set
488 // so we can call handleTransactionLocked() unconditionally.
489 // We call getTransactionFlags(), which will also clear the flags,
490 // with mStateLock held to guarantee that mCurrentState won't change
491 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700492
Mathias Agopianca4d3602011-05-19 15:38:14 -0700493 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
494 transactionFlags = getTransactionFlags(mask);
495 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700496
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497 mLastTransactionTime = systemTime() - now;
498 mDebugInTransaction = 0;
499 invalidateHwcGeometry();
500 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700501}
502
Mathias Agopianca4d3602011-05-19 15:38:14 -0700503void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700504{
505 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 const size_t count = currentLayers.size();
507
508 /*
509 * Traversal of the children
510 * (perform the transaction for each of them if needed)
511 */
512
513 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
514 if (layersNeedTransaction) {
515 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700516 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
518 if (!trFlags) continue;
519
520 const uint32_t flags = layer->doTransaction(0);
521 if (flags & Layer::eVisibleRegion)
522 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 }
524 }
525
526 /*
527 * Perform our own transaction if needed
528 */
529
530 if (transactionFlags & eTransactionNeeded) {
531 if (mCurrentState.orientation != mDrawingState.orientation) {
532 // the orientation has changed, recompute all visible regions
533 // and invalidate everything.
534
535 const int dpy = 0;
536 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700537 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 GraphicPlane& plane(graphicPlane(dpy));
539 plane.setOrientation(orientation);
540
541 // update the shared control block
542 const DisplayHardware& hw(plane.displayHardware());
543 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
544 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800545 dcblk->w = plane.getWidth();
546 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547
548 mVisibleRegionsDirty = true;
549 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 }
551
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700552 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
553 // layers have been added
554 mVisibleRegionsDirty = true;
555 }
556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 // some layers might have been removed, so
558 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700560 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700562 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700563 const size_t count = previousLayers.size();
564 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700565 const sp<LayerBase>& layer(previousLayers[i]);
566 if (currentLayers.indexOf( layer ) < 0) {
567 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700568 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700569 }
570 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 }
573
574 commitTransaction();
575}
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800578 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579{
Mathias Agopian841cde52012-03-01 15:44:37 -0800580 ATRACE_CALL();
581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 const GraphicPlane& plane(graphicPlane(0));
583 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700584 const DisplayHardware& hw(plane.displayHardware());
585 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586
587 Region aboveOpaqueLayers;
588 Region aboveCoveredLayers;
589 Region dirty;
590
591 bool secureFrameBuffer = false;
592
593 size_t i = currentLayers.size();
594 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 layer->validateVisibility(planeTransform);
597
598 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700599 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600
Mathias Agopianab028732010-03-16 16:41:46 -0700601 /*
602 * opaqueRegion: area of a surface that is fully opaque.
603 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700605
606 /*
607 * visibleRegion: area of a surface that is visible on screen
608 * and not fully transparent. This is essentially the layer's
609 * footprint minus the opaque regions above it.
610 * Areas covered by a translucent surface are considered visible.
611 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700613
614 /*
615 * coveredRegion: area of a surface that is covered by all
616 * visible regions above it (which includes the translucent areas).
617 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700619
620
621 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800622 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700623 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700624 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700626 visibleRegion.andSelf(screenRegion);
627 if (!visibleRegion.isEmpty()) {
628 // Remove the transparent area from the visible region
629 if (translucent) {
630 visibleRegion.subtractSelf(layer->transparentRegionScreen);
631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632
Mathias Agopianab028732010-03-16 16:41:46 -0700633 // compute the opaque region
634 const int32_t layerOrientation = layer->getOrientation();
635 if (s.alpha==255 && !translucent &&
636 ((layerOrientation & Transform::ROT_INVALID) == false)) {
637 // the opaque region is the layer's footprint
638 opaqueRegion = visibleRegion;
639 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 }
641 }
642
Mathias Agopianab028732010-03-16 16:41:46 -0700643 // Clip the covered region to the visible region
644 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
645
646 // Update aboveCoveredLayers for next (lower) layer
647 aboveCoveredLayers.orSelf(visibleRegion);
648
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649 // subtract the opaque region covered by the layers above us
650 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651
652 // compute this layer's dirty region
653 if (layer->contentDirty) {
654 // we need to invalidate the whole region
655 dirty = visibleRegion;
656 // as well, as the old visible region
657 dirty.orSelf(layer->visibleRegionScreen);
658 layer->contentDirty = false;
659 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700660 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700661 * the exposed region consists of two components:
662 * 1) what's VISIBLE now and was COVERED before
663 * 2) what's EXPOSED now less what was EXPOSED before
664 *
665 * note that (1) is conservative, we start with the whole
666 * visible region but only keep what used to be covered by
667 * something -- which mean it may have been exposed.
668 *
669 * (2) handles areas that were not covered by anything but got
670 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700671 */
Mathias Agopianab028732010-03-16 16:41:46 -0700672 const Region newExposed = visibleRegion - coveredRegion;
673 const Region oldVisibleRegion = layer->visibleRegionScreen;
674 const Region oldCoveredRegion = layer->coveredRegionScreen;
675 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
676 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 }
678 dirty.subtractSelf(aboveOpaqueLayers);
679
680 // accumulate to the screen dirty region
681 dirtyRegion.orSelf(dirty);
682
Mathias Agopianab028732010-03-16 16:41:46 -0700683 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 // Store the visible region is screen space
687 layer->setVisibleRegion(visibleRegion);
688 layer->setCoveredRegion(coveredRegion);
689
Mathias Agopian97011222009-07-28 10:57:27 -0700690 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 if (layer->isSecure() && !visibleRegion.isEmpty()) {
692 secureFrameBuffer = true;
693 }
694 }
695
Mathias Agopian97011222009-07-28 10:57:27 -0700696 // invalidate the areas where a layer was removed
697 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
698 mDirtyRegionRemovedLayer.clear();
699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 mSecureFrameBuffer = secureFrameBuffer;
701 opaqueRegion = aboveOpaqueLayers;
702}
703
704
705void SurfaceFlinger::commitTransaction()
706{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800707 if (!mLayersPendingRemoval.isEmpty()) {
708 // Notify removed layers now that they can't be drawn from
709 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
710 mLayersPendingRemoval[i]->onRemoved();
711 }
712 mLayersPendingRemoval.clear();
713 }
714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700716 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700717 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718}
719
720void SurfaceFlinger::handlePageFlip()
721{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800722 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800723 const DisplayHardware& hw = graphicPlane(0).displayHardware();
724 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800726 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
727 const bool visibleRegions = lockPageFlip(currentLayers);
728
729 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 Region opaqueRegion;
731 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700732
733 /*
734 * rebuild the visible layer list
735 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800736 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700737 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700738 mVisibleLayersSortedByZ.setCapacity(count);
739 for (size_t i=0 ; i<count ; i++) {
740 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
741 mVisibleLayersSortedByZ.add(currentLayers[i]);
742 }
743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 mWormholeRegion = screenRegion.subtract(opaqueRegion);
745 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800746 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 }
748
749 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700750
751 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 mDirtyRegion.andSelf(screenRegion);
753}
754
Mathias Agopianad456f92011-01-13 17:53:01 -0800755void SurfaceFlinger::invalidateHwcGeometry()
756{
757 mHwWorkListDirty = true;
758}
759
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
761{
762 bool recomputeVisibleRegions = false;
763 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700764 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700766 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767 layer->lockPageFlip(recomputeVisibleRegions);
768 }
769 return recomputeVisibleRegions;
770}
771
772void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
773{
774 const GraphicPlane& plane(graphicPlane(0));
775 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800776 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700777 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700779 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 layer->unlockPageFlip(planeTransform, mDirtyRegion);
781 }
782}
783
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800784void SurfaceFlinger::handleRefresh()
785{
786 bool needInvalidate = false;
787 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
788 const size_t count = currentLayers.size();
789 for (size_t i=0 ; i<count ; i++) {
790 const sp<LayerBase>& layer(currentLayers[i]);
791 if (layer->onPreComposition()) {
792 needInvalidate = true;
793 }
794 }
795 if (needInvalidate) {
796 signalLayerUpdate();
797 }
798}
799
800
Mathias Agopiana350ff92010-08-10 17:14:02 -0700801void SurfaceFlinger::handleWorkList()
802{
803 mHwWorkListDirty = false;
804 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
805 if (hwc.initCheck() == NO_ERROR) {
806 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
807 const size_t count = currentLayers.size();
808 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700809 hwc_layer_t* const cur(hwc.getLayers());
810 for (size_t i=0 ; cur && i<count ; i++) {
811 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700812 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700813 cur[i].compositionType = HWC_FRAMEBUFFER;
814 cur[i].flags |= HWC_SKIP_LAYER;
815 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700816 }
817 }
818}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700819
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820void SurfaceFlinger::handleRepaint()
821{
Mathias Agopian841cde52012-03-01 15:44:37 -0800822 ATRACE_CALL();
823
Mathias Agopianb8a55602009-06-26 19:06:36 -0700824 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700825 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826
Glenn Kasten99ed2242011-12-15 09:51:17 -0800827 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 debugFlashRegions();
829 }
830
Mathias Agopianb8a55602009-06-26 19:06:36 -0700831 // set the frame buffer
832 const DisplayHardware& hw(graphicPlane(0).displayHardware());
833 glMatrixMode(GL_MODELVIEW);
834 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835
836 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700837 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
838 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700839 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700840 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
841 // takes a rectangle, we must make sure to update that whole
842 // rectangle in that case
843 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700844 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700846 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 // in the BUFFER_PRESERVED case, obviously, we can update only
849 // what's needed and nothing more.
850 // NOTE: this is NOT a common case, as preserving the backbuffer
851 // is costly and usually involves copying the whole update back.
852 }
853 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700854 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700855 // We need to redraw the rectangle that will be updated
856 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700857 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700858 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700859 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700860 } else {
861 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700863 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 }
865 }
866
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700867 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868 composeSurfaces(mDirtyRegion);
869
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700870 // update the swap region and clear the dirty region
871 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 mDirtyRegion.clear();
873}
874
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700875void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700877 const DisplayHardware& hw(graphicPlane(0).displayHardware());
878 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700879 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700880 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700881 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700882 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700883
Mathias Agopianf384cc32011-09-08 18:31:55 -0700884 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
885 size_t count = layers.size();
886
Steve Blocke6f43dd2012-01-06 19:20:56 +0000887 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700888 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
889 hwc.getNumLayers(), count);
890
891 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700892 if (hwc.initCheck() == NO_ERROR) {
893 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
894 }
Mathias Agopian45721772010-08-12 15:03:26 -0700895
896 /*
897 * update the per-frame h/w composer data for each layer
898 * and build the transparent region of the FB
899 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900 for (size_t i=0 ; i<count ; i++) {
901 const sp<LayerBase>& layer(layers[i]);
902 layer->setPerFrameData(&cur[i]);
903 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700904 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700905 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000906 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700907
Mathias Agopianf384cc32011-09-08 18:31:55 -0700908 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700909 // what's happening here is tricky.
910 // we want to clear all the layers with the CLEAR_FB flags
911 // that are opaque.
912 // however, since some GPU are efficient at preserving
913 // the backbuffer, we want to take advantage of that so we do the
914 // clear only in the dirty region (other areas will be preserved
915 // on those GPUs).
916 // NOTE: on non backbuffer preserving GPU, the dirty region
917 // has already been expanded as needed, so the code is correct
918 // there too.
919 //
920 // However, the content of the framebuffer cannot be trusted when
921 // we switch to/from FB/OVERLAY, in which case we need to
922 // expand the dirty region to those areas too.
923 //
924 // Note also that there is a special case when switching from
925 // "no layers in FB" to "some layers in FB", where we need to redraw
926 // the entire FB, since some areas might contain uninitialized
927 // data.
928 //
929 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700930 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700931 // that is, we can't erase anything outside the dirty region.
932
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700933 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700934
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700935 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
936 transparent.set(hw.getBounds());
937 dirtyInOut = transparent;
938 } else {
939 for (size_t i=0 ; i<count ; i++) {
940 const sp<LayerBase>& layer(layers[i]);
941 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
942 transparent.orSelf(layer->visibleRegionScreen);
943 }
944 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
945 if (isOverlay != layer->isOverlay()) {
946 // we transitioned to/from overlay, so add this layer
947 // to the dirty region so the framebuffer can be either
948 // cleared or redrawn.
949 dirtyInOut.orSelf(layer->visibleRegionScreen);
950 }
951 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800952 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700953 // don't erase stuff outside the dirty region
954 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700955 }
956
957 /*
958 * clear the area of the FB that need to be transparent
959 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700960 if (!transparent.isEmpty()) {
961 glClearColor(0,0,0,0);
962 Region::const_iterator it = transparent.begin();
963 Region::const_iterator const end = transparent.end();
964 const int32_t height = hw.getHeight();
965 while (it != end) {
966 const Rect& r(*it++);
967 const GLint sy = height - (r.top + r.height());
968 glScissor(r.left, sy, r.width(), r.height());
969 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800970 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700971 }
972 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700973}
Mathias Agopian45721772010-08-12 15:03:26 -0700974
Mathias Agopianf384cc32011-09-08 18:31:55 -0700975void SurfaceFlinger::composeSurfaces(const Region& dirty)
976{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700977 const DisplayHardware& hw(graphicPlane(0).displayHardware());
978 HWComposer& hwc(hw.getHwComposer());
979
980 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800981 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700982 // should never happen unless the window manager has a bug
983 // draw something...
984 drawWormhole();
985 }
986
Mathias Agopian45721772010-08-12 15:03:26 -0700987 /*
988 * and then, render the layers targeted at the framebuffer
989 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700990
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700991 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700992 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
993 size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700994
995
996 // FIXME: workaround for b/6020860
997 if (hw.getFlags() & DisplayHardware::BUFFER_PRESERVED) {
998 for (size_t i=0 ; i<count ; i++) {
999 if (cur && (cur[i].compositionType == HWC_FRAMEBUFFER)) {
1000 glEnable(GL_SCISSOR_TEST);
1001 glScissor(0,0,0,0);
1002 glClear(GL_COLOR_BUFFER_BIT);
1003 break;
1004 }
1005 }
1006 }
1007 // FIXME: bug6020860 for b/6020860
1008
1009
Mathias Agopian45721772010-08-12 15:03:26 -07001010 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001011 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001012 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001013 }
1014 const sp<LayerBase>& layer(layers[i]);
1015 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1016 if (!clip.isEmpty()) {
1017 layer->draw(clip);
1018 }
1019 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020}
1021
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022void SurfaceFlinger::debugFlashRegions()
1023{
Mathias Agopian0a917752010-06-14 21:20:00 -07001024 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1025 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001026 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001027 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001028 return;
1029 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001030
Mathias Agopian0a917752010-06-14 21:20:00 -07001031 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1032 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1033 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1034 mDirtyRegion.bounds() : hw.bounds());
1035 composeSurfaces(repaint);
1036 }
1037
Mathias Agopianc492e672011-10-18 14:49:27 -07001038 glDisable(GL_TEXTURE_EXTERNAL_OES);
1039 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 glDisable(GL_SCISSOR_TEST);
1042
Mathias Agopian0926f502009-05-04 14:17:04 -07001043 static int toggle = 0;
1044 toggle = 1 - toggle;
1045 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001046 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001047 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001048 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001049 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050
Mathias Agopian20f68782009-05-11 00:03:41 -07001051 Region::const_iterator it = mDirtyRegion.begin();
1052 Region::const_iterator const end = mDirtyRegion.end();
1053 while (it != end) {
1054 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001056 { r.left, height - r.top },
1057 { r.left, height - r.bottom },
1058 { r.right, height - r.bottom },
1059 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 };
1061 glVertexPointer(2, GL_FLOAT, 0, vertices);
1062 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1063 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001064
Mathias Agopian0656a682011-09-20 17:22:44 -07001065 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066
1067 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001068 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069
1070 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071}
1072
1073void SurfaceFlinger::drawWormhole() const
1074{
1075 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1076 if (region.isEmpty())
1077 return;
1078
1079 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1080 const int32_t width = hw.getWidth();
1081 const int32_t height = hw.getHeight();
1082
Glenn Kasten99ed2242011-12-15 09:51:17 -08001083 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001084 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001085 Region::const_iterator it = region.begin();
1086 Region::const_iterator const end = region.end();
1087 while (it != end) {
1088 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 const GLint sy = height - (r.top + r.height());
1090 glScissor(r.left, sy, r.width(), r.height());
1091 glClear(GL_COLOR_BUFFER_BIT);
1092 }
1093 } else {
1094 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1095 { width, height }, { 0, height } };
1096 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001097
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 glVertexPointer(2, GL_SHORT, 0, vertices);
1099 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1100 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001101
1102 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 glEnable(GL_TEXTURE_2D);
1104 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1105 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1106 glMatrixMode(GL_TEXTURE);
1107 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001108
1109 glDisable(GL_BLEND);
1110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001112 Region::const_iterator it = region.begin();
1113 Region::const_iterator const end = region.end();
1114 while (it != end) {
1115 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 const GLint sy = height - (r.top + r.height());
1117 glScissor(r.left, sy, r.width(), r.height());
1118 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1119 }
1120 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001121 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001122 glLoadIdentity();
1123 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 }
1125}
1126
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001127status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128{
1129 Mutex::Autolock _l(mStateLock);
1130 addLayer_l(layer);
1131 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1132 return NO_ERROR;
1133}
1134
Mathias Agopian96f08192010-06-02 23:28:45 -07001135status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1136{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001137 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001138 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1139}
1140
1141ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1142 const sp<LayerBaseClient>& lbc)
1143{
Mathias Agopian96f08192010-06-02 23:28:45 -07001144 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001145 size_t name = client->attachLayer(lbc);
1146
1147 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001148
1149 // add this layer to the current state list
1150 addLayer_l(lbc);
1151
Mathias Agopian4f113742011-05-03 16:21:41 -07001152 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001153}
1154
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001155status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156{
1157 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001158 status_t err = purgatorizeLayer_l(layer);
1159 if (err == NO_ERROR)
1160 setTransactionFlags(eTransactionNeeded);
1161 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162}
1163
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001164status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001166 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1167 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001168 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001169 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1171 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001172 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 return NO_ERROR;
1174 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001175 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176}
1177
Mathias Agopian9a112062009-04-17 19:36:26 -07001178status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1179{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001180 // First add the layer to the purgatory list, which makes sure it won't
1181 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001182 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001183 if (err >= 0) {
1184 mLayerPurgatory.add(layerBase);
1185 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001186
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001187 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001188
Mathias Agopian3d579642009-06-04 18:46:21 -07001189 // it's possible that we don't find a layer, because it might
1190 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001191 // from the user because there is a race between Client::destroySurface(),
1192 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001193 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1194}
1195
Mathias Agopian96f08192010-06-02 23:28:45 -07001196status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197{
Mathias Agopian96f08192010-06-02 23:28:45 -07001198 layer->forceVisibilityTransaction();
1199 setTransactionFlags(eTraversalNeeded);
1200 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201}
1202
Mathias Agopiandea20b12011-05-03 17:04:02 -07001203uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1204{
1205 return android_atomic_release_load(&mTransactionFlags);
1206}
1207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1209{
1210 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1211}
1212
Mathias Agopianbb641242010-05-18 17:06:55 -07001213uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214{
1215 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1216 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001217 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 }
1219 return old;
1220}
1221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001223void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001224 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001225 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001226
Jamie Gennis28378392011-10-12 17:39:00 -07001227 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001228 if (mCurrentState.orientation != orientation) {
1229 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1230 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001231 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001232 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001233 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001234 orientation);
1235 }
1236 }
1237
Mathias Agopian698c0872011-06-28 19:09:31 -07001238 const size_t count = state.size();
1239 for (size_t i=0 ; i<count ; i++) {
1240 const ComposerState& s(state[i]);
1241 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001242 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001243 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001244
Mathias Agopian386aa982011-11-07 21:58:03 -08001245 if (transactionFlags) {
1246 // this triggers the transaction
1247 setTransactionFlags(transactionFlags);
1248
1249 // if this is a synchronous transaction, wait for it to take effect
1250 // before returning.
1251 if (flags & eSynchronous) {
1252 mTransationPending = true;
1253 }
1254 while (mTransationPending) {
1255 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1256 if (CC_UNLIKELY(err != NO_ERROR)) {
1257 // just in case something goes wrong in SF, return to the
1258 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001259 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001260 mTransationPending = false;
1261 break;
1262 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001263 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 }
1265}
1266
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001267sp<ISurface> SurfaceFlinger::createSurface(
1268 ISurfaceComposerClient::surface_data_t* params,
1269 const String8& name,
1270 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1272 uint32_t flags)
1273{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001274 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001275 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001276
1277 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001278 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001279 int(w), int(h));
1280 return surfaceHandle;
1281 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001282
Mathias Agopianff615cc2012-02-24 14:58:36 -08001283 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001284 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 switch (flags & eFXSurfaceMask) {
1286 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001287 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1288 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 break;
1290 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001291 // for now we treat Blur as Dim, until we can implement it
1292 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001294 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001296 case eFXSurfaceScreenshot:
1297 layer = createScreenshotSurface(client, d, w, h, flags);
1298 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 }
1300
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001301 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001303 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001304 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001307 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001308 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001309 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001310 if (normalLayer != 0) {
1311 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001312 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001313 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001314 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001315
Mathias Agopian96f08192010-06-02 23:28:45 -07001316 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 }
1318
1319 return surfaceHandle;
1320}
1321
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001322sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001323 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001324 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001325 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326{
1327 // initialize the surfaces
1328 switch (format) { // TODO: take h/w into account
1329 case PIXEL_FORMAT_TRANSPARENT:
1330 case PIXEL_FORMAT_TRANSLUCENT:
1331 format = PIXEL_FORMAT_RGBA_8888;
1332 break;
1333 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001334#ifdef NO_RGBX_8888
1335 format = PIXEL_FORMAT_RGB_565;
1336#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001337 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001338#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 break;
1340 }
1341
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001342#ifdef NO_RGBX_8888
1343 if (format == PIXEL_FORMAT_RGBX_8888)
1344 format = PIXEL_FORMAT_RGBA_8888;
1345#endif
1346
Mathias Agopian96f08192010-06-02 23:28:45 -07001347 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001348 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001349 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001350 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001351 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 }
1353 return layer;
1354}
1355
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001356sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001357 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359{
Mathias Agopian96f08192010-06-02 23:28:45 -07001360 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001361 return layer;
1362}
1363
1364sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1365 const sp<Client>& client, DisplayID display,
1366 uint32_t w, uint32_t h, uint32_t flags)
1367{
1368 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 return layer;
1370}
1371
Mathias Agopian96f08192010-06-02 23:28:45 -07001372status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373{
Mathias Agopian9a112062009-04-17 19:36:26 -07001374 /*
1375 * called by the window manager, when a surface should be marked for
1376 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001377 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001378 * The surface is removed from the current and drawing lists, but placed
1379 * in the purgatory queue, so it's not destroyed right-away (we need
1380 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001381 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001382
Mathias Agopian48d819a2009-09-10 19:41:18 -07001383 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001384 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001385 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001386
Mathias Agopian48d819a2009-09-10 19:41:18 -07001387 if (layer != 0) {
1388 err = purgatorizeLayer_l(layer);
1389 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001390 setTransactionFlags(eTransactionNeeded);
1391 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001392 }
1393 return err;
1394}
1395
Mathias Agopianca4d3602011-05-19 15:38:14 -07001396status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001397{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001398 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001399 status_t err = NO_ERROR;
1400 sp<LayerBaseClient> l(layer.promote());
1401 if (l != NULL) {
1402 Mutex::Autolock _l(mStateLock);
1403 err = removeLayer_l(l);
1404 if (err == NAME_NOT_FOUND) {
1405 // The surface wasn't in the current list, which means it was
1406 // removed already, which means it is in the purgatory,
1407 // and need to be removed from there.
1408 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001409 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001410 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001411 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001412 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001413 "error removing layer=%p (%s)", l.get(), strerror(-err));
1414 }
1415 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416}
1417
Mathias Agopian698c0872011-06-28 19:09:31 -07001418uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001419 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001420 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001423 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1424 if (layer != 0) {
1425 const uint32_t what = s.what;
1426 if (what & ePositionChanged) {
1427 if (layer->setPosition(s.x, s.y))
1428 flags |= eTraversalNeeded;
1429 }
1430 if (what & eLayerChanged) {
1431 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1432 if (layer->setLayer(s.z)) {
1433 mCurrentState.layersSortedByZ.removeAt(idx);
1434 mCurrentState.layersSortedByZ.add(layer);
1435 // we need traversal (state changed)
1436 // AND transaction (list changed)
1437 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 }
1439 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001440 if (what & eSizeChanged) {
1441 if (layer->setSize(s.w, s.h)) {
1442 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001443 }
1444 }
1445 if (what & eAlphaChanged) {
1446 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1447 flags |= eTraversalNeeded;
1448 }
1449 if (what & eMatrixChanged) {
1450 if (layer->setMatrix(s.matrix))
1451 flags |= eTraversalNeeded;
1452 }
1453 if (what & eTransparentRegionChanged) {
1454 if (layer->setTransparentRegionHint(s.transparentRegion))
1455 flags |= eTraversalNeeded;
1456 }
1457 if (what & eVisibilityChanged) {
1458 if (layer->setFlags(s.flags, s.mask))
1459 flags |= eTraversalNeeded;
1460 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001462 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463}
1464
Mathias Agopianb60314a2012-04-10 22:09:54 -07001465// ---------------------------------------------------------------------------
1466
1467void SurfaceFlinger::onScreenAcquired() {
1468 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1469 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001470 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001471 // this is a temporary work-around, eventually this should be called
1472 // by the power-manager
1473 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
1474 mDirtyRegion.set(hw.bounds());
Mathias Agopian22ffb112012-04-10 21:04:02 -07001475 // from this point on, SF will process updates again
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476}
1477
Mathias Agopianb60314a2012-04-10 22:09:54 -07001478void SurfaceFlinger::onScreenReleased() {
1479 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1480 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001481 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001482 mDirtyRegion.set(hw.bounds());
1483 hw.releaseScreen();
1484 // from this point on, SF will stop drawing
1485 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486}
1487
Mathias Agopianb60314a2012-04-10 22:09:54 -07001488void SurfaceFlinger::screenAcquired() {
1489 class MessageScreenAcquired : public MessageBase {
1490 SurfaceFlinger* flinger;
1491 public:
1492 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1493 virtual bool handler() {
1494 flinger->onScreenAcquired();
1495 return true;
1496 }
1497 };
1498 sp<MessageBase> msg = new MessageScreenAcquired(this);
1499 postMessageSync(msg);
1500}
1501
1502void SurfaceFlinger::screenReleased() {
1503 class MessageScreenReleased : public MessageBase {
1504 SurfaceFlinger* flinger;
1505 public:
1506 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1507 virtual bool handler() {
1508 flinger->onScreenReleased();
1509 return true;
1510 }
1511 };
1512 sp<MessageBase> msg = new MessageScreenReleased(this);
1513 postMessageSync(msg);
1514}
1515
1516// ---------------------------------------------------------------------------
1517
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1519{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001520 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 char buffer[SIZE];
1522 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001523
1524 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 snprintf(buffer, SIZE, "Permission Denial: "
1526 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1527 IPCThreadState::self()->getCallingPid(),
1528 IPCThreadState::self()->getCallingUid());
1529 result.append(buffer);
1530 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001531 // Try to get the main lock, but don't insist if we can't
1532 // (this would indicate SF is stuck, but we want to be able to
1533 // print something in dumpsys).
1534 int retry = 3;
1535 while (mStateLock.tryLock()<0 && --retry>=0) {
1536 usleep(1000000);
1537 }
1538 const bool locked(retry >= 0);
1539 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001540 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001541 "SurfaceFlinger appears to be unresponsive, "
1542 "dumping anyways (no locks held)\n");
1543 result.append(buffer);
1544 }
1545
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001546 bool dumpAll = true;
1547 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001548 size_t numArgs = args.size();
1549 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001550 if ((index < numArgs) &&
1551 (args[index] == String16("--list"))) {
1552 index++;
1553 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001554 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001555 }
1556
1557 if ((index < numArgs) &&
1558 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001559 index++;
1560 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001561 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001562 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001563
1564 if ((index < numArgs) &&
1565 (args[index] == String16("--latency-clear"))) {
1566 index++;
1567 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001568 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001569 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001571
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001572 if (dumpAll) {
1573 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001574 }
1575
Mathias Agopian9795c422009-08-26 16:36:26 -07001576 if (locked) {
1577 mStateLock.unlock();
1578 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 }
1580 write(fd, result.string(), result.size());
1581 return NO_ERROR;
1582}
1583
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001584void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1585 String8& result, char* buffer, size_t SIZE) const
1586{
1587 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1588 const size_t count = currentLayers.size();
1589 for (size_t i=0 ; i<count ; i++) {
1590 const sp<LayerBase>& layer(currentLayers[i]);
1591 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1592 result.append(buffer);
1593 }
1594}
1595
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001596void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1597 String8& result, char* buffer, size_t SIZE) const
1598{
1599 String8 name;
1600 if (index < args.size()) {
1601 name = String8(args[index]);
1602 index++;
1603 }
1604
1605 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1606 const size_t count = currentLayers.size();
1607 for (size_t i=0 ; i<count ; i++) {
1608 const sp<LayerBase>& layer(currentLayers[i]);
1609 if (name.isEmpty()) {
1610 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1611 result.append(buffer);
1612 }
1613 if (name.isEmpty() || (name == layer->getName())) {
1614 layer->dumpStats(result, buffer, SIZE);
1615 }
1616 }
1617}
1618
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001619void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1620 String8& result, char* buffer, size_t SIZE) const
1621{
1622 String8 name;
1623 if (index < args.size()) {
1624 name = String8(args[index]);
1625 index++;
1626 }
1627
1628 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1629 const size_t count = currentLayers.size();
1630 for (size_t i=0 ; i<count ; i++) {
1631 const sp<LayerBase>& layer(currentLayers[i]);
1632 if (name.isEmpty() || (name == layer->getName())) {
1633 layer->clearStats();
1634 }
1635 }
1636}
1637
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001638void SurfaceFlinger::dumpAllLocked(
1639 String8& result, char* buffer, size_t SIZE) const
1640{
1641 // figure out if we're stuck somewhere
1642 const nsecs_t now = systemTime();
1643 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1644 const nsecs_t inTransaction(mDebugInTransaction);
1645 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1646 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1647
1648 /*
1649 * Dump the visible layer list
1650 */
1651 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1652 const size_t count = currentLayers.size();
1653 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1654 result.append(buffer);
1655 for (size_t i=0 ; i<count ; i++) {
1656 const sp<LayerBase>& layer(currentLayers[i]);
1657 layer->dump(result, buffer, SIZE);
1658 }
1659
1660 /*
1661 * Dump the layers in the purgatory
1662 */
1663
1664 const size_t purgatorySize = mLayerPurgatory.size();
1665 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1666 result.append(buffer);
1667 for (size_t i=0 ; i<purgatorySize ; i++) {
1668 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1669 layer->shortDump(result, buffer, SIZE);
1670 }
1671
1672 /*
1673 * Dump SurfaceFlinger global state
1674 */
1675
1676 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1677 result.append(buffer);
1678
1679 const GLExtensions& extensions(GLExtensions::getInstance());
1680 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1681 extensions.getVendor(),
1682 extensions.getRenderer(),
1683 extensions.getVersion());
1684 result.append(buffer);
1685
1686 snprintf(buffer, SIZE, "EGL : %s\n",
1687 eglQueryString(graphicPlane(0).getEGLDisplay(),
1688 EGL_VERSION_HW_ANDROID));
1689 result.append(buffer);
1690
1691 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1692 result.append(buffer);
1693
1694 mWormholeRegion.dump(result, "WormholeRegion");
1695 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1696 snprintf(buffer, SIZE,
1697 " orientation=%d, canDraw=%d\n",
1698 mCurrentState.orientation, hw.canDraw());
1699 result.append(buffer);
1700 snprintf(buffer, SIZE,
1701 " last eglSwapBuffers() time: %f us\n"
1702 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001703 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001704 " refresh-rate : %f fps\n"
1705 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001706 " y-dpi : %f\n"
1707 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001708 mLastSwapBufferTime/1000.0,
1709 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001710 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001711 hw.getRefreshRate(),
1712 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001713 hw.getDpiY(),
1714 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001715 result.append(buffer);
1716
1717 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1718 inSwapBuffersDuration/1000.0);
1719 result.append(buffer);
1720
1721 snprintf(buffer, SIZE, " transaction time: %f us\n",
1722 inTransactionDuration/1000.0);
1723 result.append(buffer);
1724
1725 /*
1726 * VSYNC state
1727 */
1728 mEventThread->dump(result, buffer, SIZE);
1729
1730 /*
1731 * Dump HWComposer state
1732 */
1733 HWComposer& hwc(hw.getHwComposer());
1734 snprintf(buffer, SIZE, "h/w composer state:\n");
1735 result.append(buffer);
1736 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1737 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1738 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1739 result.append(buffer);
1740 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1741
1742 /*
1743 * Dump gralloc state
1744 */
1745 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1746 alloc.dump(result);
1747 hw.dump(result);
1748}
1749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750status_t SurfaceFlinger::onTransact(
1751 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1752{
1753 switch (code) {
1754 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001755 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001758 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001759 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 {
1761 // codes that require permission check
1762 IPCThreadState* ipc = IPCThreadState::self();
1763 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001764 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001765 if ((uid != AID_GRAPHICS) &&
1766 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001767 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001768 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1769 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001770 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001771 break;
1772 }
1773 case CAPTURE_SCREEN:
1774 {
1775 // codes that require permission check
1776 IPCThreadState* ipc = IPCThreadState::self();
1777 const int pid = ipc->getCallingPid();
1778 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001779 if ((uid != AID_GRAPHICS) &&
1780 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001781 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001782 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1783 return PERMISSION_DENIED;
1784 }
1785 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 }
1787 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001788
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001789 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1790 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001791 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001792 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001793 IPCThreadState* ipc = IPCThreadState::self();
1794 const int pid = ipc->getCallingPid();
1795 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001796 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001797 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798 return PERMISSION_DENIED;
1799 }
1800 int n;
1801 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001802 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001803 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001804 return NO_ERROR;
1805 case 1002: // SHOW_UPDATES
1806 n = data.readInt32();
1807 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001808 invalidateHwcGeometry();
1809 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 return NO_ERROR;
1811 case 1003: // SHOW_BACKGROUND
1812 n = data.readInt32();
1813 mDebugBackground = n ? 1 : 0;
1814 return NO_ERROR;
1815 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001816 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001817 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001819 case 1005:{ // force transaction
1820 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1821 return NO_ERROR;
1822 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001823 case 1006:{ // send empty update
1824 signalRefresh();
1825 return NO_ERROR;
1826 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001827 case 1008: // toggle use of hw composer
1828 n = data.readInt32();
1829 mDebugDisableHWC = n ? 1 : 0;
1830 invalidateHwcGeometry();
1831 repaintEverything();
1832 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001833 case 1009: // toggle use of transform hint
1834 n = data.readInt32();
1835 mDebugDisableTransformHint = n ? 1 : 0;
1836 invalidateHwcGeometry();
1837 repaintEverything();
1838 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001840 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001841 reply->writeInt32(0);
1842 reply->writeInt32(mDebugRegion);
1843 reply->writeInt32(mDebugBackground);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001844 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845 return NO_ERROR;
1846 case 1013: {
1847 Mutex::Autolock _l(mStateLock);
1848 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1849 reply->writeInt32(hw.getPageFlipCount());
1850 }
1851 return NO_ERROR;
1852 }
1853 }
1854 return err;
1855}
1856
Mathias Agopian53331da2011-08-22 21:44:41 -07001857void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001858 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001859 const Rect bounds(hw.getBounds());
1860 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001861 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001862}
1863
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001864void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1865 Mutex::Autolock _l(mInvalidateLock);
1866 mInvalidateRegion = reg;
1867}
1868
1869Region SurfaceFlinger::getAndClearInvalidateRegion() {
1870 Mutex::Autolock _l(mInvalidateLock);
1871 Region reg(mInvalidateRegion);
1872 mInvalidateRegion.clear();
1873 return reg;
1874}
1875
Mathias Agopian59119e62010-10-11 12:37:43 -07001876// ---------------------------------------------------------------------------
1877
Mathias Agopian118d0242011-10-13 16:02:48 -07001878status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1879 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1880{
1881 Mutex::Autolock _l(mStateLock);
1882 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1883}
1884
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001885status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1886 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001887{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001888 ATRACE_CALL();
1889
Mathias Agopian59119e62010-10-11 12:37:43 -07001890 if (!GLExtensions::getInstance().haveFramebufferObject())
1891 return INVALID_OPERATION;
1892
1893 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001894 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001895 const uint32_t hw_w = hw.getWidth();
1896 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001897 GLfloat u = 1;
1898 GLfloat v = 1;
1899
1900 // make sure to clear all GL error flags
1901 while ( glGetError() != GL_NO_ERROR ) ;
1902
1903 // create a FBO
1904 GLuint name, tname;
1905 glGenTextures(1, &tname);
1906 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001907 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1908 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001909 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001910 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001911 GLint tw = (2 << (31 - clz(hw_w)));
1912 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001913 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1914 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001915 u = GLfloat(hw_w) / tw;
1916 v = GLfloat(hw_h) / th;
1917 }
1918 glGenFramebuffersOES(1, &name);
1919 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001920 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1921 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001922
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001923 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001924 glDisable(GL_TEXTURE_EXTERNAL_OES);
1925 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001926 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001927 glClearColor(0,0,0,1);
1928 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001929 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001930 glMatrixMode(GL_MODELVIEW);
1931 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001932 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1933 const size_t count = layers.size();
1934 for (size_t i=0 ; i<count ; ++i) {
1935 const sp<LayerBase>& layer(layers[i]);
1936 layer->drawForSreenShot();
1937 }
1938
Mathias Agopian118d0242011-10-13 16:02:48 -07001939 hw.compositionComplete();
1940
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001941 // back to main framebuffer
1942 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1943 glDisable(GL_SCISSOR_TEST);
1944 glDeleteFramebuffersOES(1, &name);
1945
1946 *textureName = tname;
1947 *uOut = u;
1948 *vOut = v;
1949 return NO_ERROR;
1950}
1951
1952// ---------------------------------------------------------------------------
1953
1954status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1955{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001956 // get screen geometry
1957 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1958 const uint32_t hw_w = hw.getWidth();
1959 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001960 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001961
1962 GLfloat u, v;
1963 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001964 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001965 if (result != NO_ERROR) {
1966 return result;
1967 }
1968
1969 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001970 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 glBindTexture(GL_TEXTURE_2D, tname);
1972 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1973 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1974 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001975 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1976 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1978 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1979 glVertexPointer(2, GL_FLOAT, 0, vtx);
1980
Mathias Agopianffcf4652011-07-07 17:30:31 -07001981 /*
1982 * Texture coordinate mapping
1983 *
1984 * u
1985 * 1 +----------+---+
1986 * | | | | image is inverted
1987 * | V | | w.r.t. the texture
1988 * 1-v +----------+ | coordinates
1989 * | |
1990 * | |
1991 * | |
1992 * 0 +--------------+
1993 * 0 1
1994 *
1995 */
1996
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001997 class s_curve_interpolator {
1998 const float nbFrames, s, v;
1999 public:
2000 s_curve_interpolator(int nbFrames, float s)
2001 : nbFrames(1.0f / (nbFrames-1)), s(s),
2002 v(1.0f + expf(-s + 0.5f*s)) {
2003 }
2004 float operator()(int f) {
2005 const float x = f * nbFrames;
2006 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2007 }
2008 };
2009
2010 class v_stretch {
2011 const GLfloat hw_w, hw_h;
2012 public:
2013 v_stretch(uint32_t hw_w, uint32_t hw_h)
2014 : hw_w(hw_w), hw_h(hw_h) {
2015 }
2016 void operator()(GLfloat* vtx, float v) {
2017 const GLfloat w = hw_w + (hw_w * v);
2018 const GLfloat h = hw_h - (hw_h * v);
2019 const GLfloat x = (hw_w - w) * 0.5f;
2020 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002021 vtx[0] = x; vtx[1] = y;
2022 vtx[2] = x; vtx[3] = y + h;
2023 vtx[4] = x + w; vtx[5] = y + h;
2024 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025 }
2026 };
2027
2028 class h_stretch {
2029 const GLfloat hw_w, hw_h;
2030 public:
2031 h_stretch(uint32_t hw_w, uint32_t hw_h)
2032 : hw_w(hw_w), hw_h(hw_h) {
2033 }
2034 void operator()(GLfloat* vtx, float v) {
2035 const GLfloat w = hw_w - (hw_w * v);
2036 const GLfloat h = 1.0f;
2037 const GLfloat x = (hw_w - w) * 0.5f;
2038 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002039 vtx[0] = x; vtx[1] = y;
2040 vtx[2] = x; vtx[3] = y + h;
2041 vtx[4] = x + w; vtx[5] = y + h;
2042 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002043 }
2044 };
2045
2046 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002047 char value[PROPERTY_VALUE_MAX];
2048 property_get("debug.sf.electron_frames", value, "24");
2049 int nbFrames = (atoi(value) + 1) >> 1;
2050 if (nbFrames <= 0) // just in case
2051 nbFrames = 24;
2052
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053 s_curve_interpolator itr(nbFrames, 7.5f);
2054 s_curve_interpolator itg(nbFrames, 8.0f);
2055 s_curve_interpolator itb(nbFrames, 8.5f);
2056
2057 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002058
2059 glMatrixMode(GL_TEXTURE);
2060 glLoadIdentity();
2061 glMatrixMode(GL_MODELVIEW);
2062 glLoadIdentity();
2063
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002064 glEnable(GL_BLEND);
2065 glBlendFunc(GL_ONE, GL_ONE);
2066 for (int i=0 ; i<nbFrames ; i++) {
2067 float x, y, w, h;
2068 const float vr = itr(i);
2069 const float vg = itg(i);
2070 const float vb = itb(i);
2071
2072 // clear screen
2073 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002074 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002075 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002076
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002077 // draw the red plane
2078 vverts(vtx, vr);
2079 glColorMask(1,0,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 green plane
2083 vverts(vtx, vg);
2084 glColorMask(0,1,0,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 blue plane
2088 vverts(vtx, vb);
2089 glColorMask(0,0,1,1);
2090 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002091
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002092 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002093 glDisable(GL_TEXTURE_2D);
2094 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002095 glColor4f(vg, vg, vg, 1);
2096 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2097 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002098 }
2099
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002100 h_stretch hverts(hw_w, hw_h);
2101 glDisable(GL_BLEND);
2102 glDisable(GL_TEXTURE_2D);
2103 glColorMask(1,1,1,1);
2104 for (int i=0 ; i<nbFrames ; i++) {
2105 const float v = itg(i);
2106 hverts(vtx, v);
2107 glClear(GL_COLOR_BUFFER_BIT);
2108 glColor4f(1-v, 1-v, 1-v, 1);
2109 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2110 hw.flip(screenBounds);
2111 }
2112
2113 glColorMask(1,1,1,1);
2114 glEnable(GL_SCISSOR_TEST);
2115 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2116 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002117 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002118 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002119 return NO_ERROR;
2120}
2121
2122status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2123{
2124 status_t result = PERMISSION_DENIED;
2125
2126 if (!GLExtensions::getInstance().haveFramebufferObject())
2127 return INVALID_OPERATION;
2128
2129
2130 // get screen geometry
2131 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2132 const uint32_t hw_w = hw.getWidth();
2133 const uint32_t hw_h = hw.getHeight();
2134 const Region screenBounds(hw.bounds());
2135
2136 GLfloat u, v;
2137 GLuint tname;
2138 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2139 if (result != NO_ERROR) {
2140 return result;
2141 }
2142
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002143 GLfloat vtx[8];
2144 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002145 glBindTexture(GL_TEXTURE_2D, tname);
2146 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2147 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2148 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002149 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2150 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002151 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2152 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2153 glVertexPointer(2, GL_FLOAT, 0, vtx);
2154
2155 class s_curve_interpolator {
2156 const float nbFrames, s, v;
2157 public:
2158 s_curve_interpolator(int nbFrames, float s)
2159 : nbFrames(1.0f / (nbFrames-1)), s(s),
2160 v(1.0f + expf(-s + 0.5f*s)) {
2161 }
2162 float operator()(int f) {
2163 const float x = f * nbFrames;
2164 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2165 }
2166 };
2167
2168 class v_stretch {
2169 const GLfloat hw_w, hw_h;
2170 public:
2171 v_stretch(uint32_t hw_w, uint32_t hw_h)
2172 : hw_w(hw_w), hw_h(hw_h) {
2173 }
2174 void operator()(GLfloat* vtx, float v) {
2175 const GLfloat w = hw_w + (hw_w * v);
2176 const GLfloat h = hw_h - (hw_h * v);
2177 const GLfloat x = (hw_w - w) * 0.5f;
2178 const GLfloat y = (hw_h - h) * 0.5f;
2179 vtx[0] = x; vtx[1] = y;
2180 vtx[2] = x; vtx[3] = y + h;
2181 vtx[4] = x + w; vtx[5] = y + h;
2182 vtx[6] = x + w; vtx[7] = y;
2183 }
2184 };
2185
2186 class h_stretch {
2187 const GLfloat hw_w, hw_h;
2188 public:
2189 h_stretch(uint32_t hw_w, uint32_t hw_h)
2190 : hw_w(hw_w), hw_h(hw_h) {
2191 }
2192 void operator()(GLfloat* vtx, float v) {
2193 const GLfloat w = hw_w - (hw_w * v);
2194 const GLfloat h = 1.0f;
2195 const GLfloat x = (hw_w - w) * 0.5f;
2196 const GLfloat y = (hw_h - h) * 0.5f;
2197 vtx[0] = x; vtx[1] = y;
2198 vtx[2] = x; vtx[3] = y + h;
2199 vtx[4] = x + w; vtx[5] = y + h;
2200 vtx[6] = x + w; vtx[7] = y;
2201 }
2202 };
2203
Mathias Agopiana6546e52010-10-14 12:33:07 -07002204 // the full animation is 12 frames
2205 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206 s_curve_interpolator itr(nbFrames, 7.5f);
2207 s_curve_interpolator itg(nbFrames, 8.0f);
2208 s_curve_interpolator itb(nbFrames, 8.5f);
2209
2210 h_stretch hverts(hw_w, hw_h);
2211 glDisable(GL_BLEND);
2212 glDisable(GL_TEXTURE_2D);
2213 glColorMask(1,1,1,1);
2214 for (int i=nbFrames-1 ; i>=0 ; i--) {
2215 const float v = itg(i);
2216 hverts(vtx, v);
2217 glClear(GL_COLOR_BUFFER_BIT);
2218 glColor4f(1-v, 1-v, 1-v, 1);
2219 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2220 hw.flip(screenBounds);
2221 }
2222
Mathias Agopiana6546e52010-10-14 12:33:07 -07002223 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002224 v_stretch vverts(hw_w, hw_h);
2225 glEnable(GL_BLEND);
2226 glBlendFunc(GL_ONE, GL_ONE);
2227 for (int i=nbFrames-1 ; i>=0 ; i--) {
2228 float x, y, w, h;
2229 const float vr = itr(i);
2230 const float vg = itg(i);
2231 const float vb = itb(i);
2232
2233 // clear screen
2234 glColorMask(1,1,1,1);
2235 glClear(GL_COLOR_BUFFER_BIT);
2236 glEnable(GL_TEXTURE_2D);
2237
2238 // draw the red plane
2239 vverts(vtx, vr);
2240 glColorMask(1,0,0,1);
2241 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2242
2243 // draw the green plane
2244 vverts(vtx, vg);
2245 glColorMask(0,1,0,1);
2246 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2247
2248 // draw the blue plane
2249 vverts(vtx, vb);
2250 glColorMask(0,0,1,1);
2251 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2252
2253 hw.flip(screenBounds);
2254 }
2255
2256 glColorMask(1,1,1,1);
2257 glEnable(GL_SCISSOR_TEST);
2258 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002259 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002260 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002261 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002262
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002263 return NO_ERROR;
2264}
2265
2266// ---------------------------------------------------------------------------
2267
Mathias Agopianabd671a2010-10-14 14:54:06 -07002268status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002269{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002270 ATRACE_CALL();
2271
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002272 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2273 if (!hw.canDraw()) {
2274 // we're already off
2275 return NO_ERROR;
2276 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002277
2278 // turn off hwc while we're doing the animation
2279 hw.getHwComposer().disable();
2280 // and make sure to turn it back on (if needed) next time we compose
2281 invalidateHwcGeometry();
2282
Mathias Agopianabd671a2010-10-14 14:54:06 -07002283 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2284 electronBeamOffAnimationImplLocked();
2285 }
2286
2287 // always clear the whole screen at the end of the animation
2288 glClearColor(0,0,0,1);
2289 glDisable(GL_SCISSOR_TEST);
2290 glClear(GL_COLOR_BUFFER_BIT);
2291 glEnable(GL_SCISSOR_TEST);
2292 hw.flip( Region(hw.bounds()) );
2293
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002294 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002295}
2296
2297status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2298{
Mathias Agopian59119e62010-10-11 12:37:43 -07002299 class MessageTurnElectronBeamOff : public MessageBase {
2300 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002301 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002302 status_t result;
2303 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002304 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2305 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002306 }
2307 status_t getResult() const {
2308 return result;
2309 }
2310 virtual bool handler() {
2311 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002312 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002313 return true;
2314 }
2315 };
2316
Mathias Agopianabd671a2010-10-14 14:54:06 -07002317 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002318 status_t res = postMessageSync(msg);
2319 if (res == NO_ERROR) {
2320 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002321
2322 // work-around: when the power-manager calls us we activate the
2323 // animation. eventually, the "on" animation will be called
2324 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002325 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002326 }
2327 return res;
2328}
2329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002330// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002331
Mathias Agopianabd671a2010-10-14 14:54:06 -07002332status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002333{
2334 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2335 if (hw.canDraw()) {
2336 // we're already on
2337 return NO_ERROR;
2338 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002339 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2340 electronBeamOnAnimationImplLocked();
2341 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002342
2343 // make sure to redraw the whole screen when the animation is done
2344 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002345 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002346
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002347 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002348}
2349
2350status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2351{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002352 class MessageTurnElectronBeamOn : public MessageBase {
2353 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002354 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002355 status_t result;
2356 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002357 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2358 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002359 }
2360 status_t getResult() const {
2361 return result;
2362 }
2363 virtual bool handler() {
2364 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002365 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002366 return true;
2367 }
2368 };
2369
Mathias Agopianabd671a2010-10-14 14:54:06 -07002370 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002371 return NO_ERROR;
2372}
2373
2374// ---------------------------------------------------------------------------
2375
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2377 sp<IMemoryHeap>* heap,
2378 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002379 uint32_t sw, uint32_t sh,
2380 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002382 ATRACE_CALL();
2383
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 status_t result = PERMISSION_DENIED;
2385
2386 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002387 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 return BAD_VALUE;
2389
2390 if (!GLExtensions::getInstance().haveFramebufferObject())
2391 return INVALID_OPERATION;
2392
2393 // get screen geometry
2394 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2395 const uint32_t hw_w = hw.getWidth();
2396 const uint32_t hw_h = hw.getHeight();
2397
2398 if ((sw > hw_w) || (sh > hw_h))
2399 return BAD_VALUE;
2400
2401 sw = (!sw) ? hw_w : sw;
2402 sh = (!sh) ? hw_h : sh;
2403 const size_t size = sw * sh * 4;
2404
Steve Block9d453682011-12-20 16:23:08 +00002405 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002406 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002407
Mathias Agopian74c40c02010-09-29 13:02:36 -07002408 // make sure to clear all GL error flags
2409 while ( glGetError() != GL_NO_ERROR ) ;
2410
2411 // create a FBO
2412 GLuint name, tname;
2413 glGenRenderbuffersOES(1, &tname);
2414 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2415 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002416
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417 glGenFramebuffersOES(1, &name);
2418 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2419 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2420 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2421
2422 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002423
Mathias Agopian74c40c02010-09-29 13:02:36 -07002424 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2425
2426 // invert everything, b/c glReadPixel() below will invert the FB
2427 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002428 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002429 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002430 glMatrixMode(GL_PROJECTION);
2431 glPushMatrix();
2432 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002433 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002434 glMatrixMode(GL_MODELVIEW);
2435
2436 // redraw the screen entirely...
2437 glClearColor(0,0,0,1);
2438 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002439
Jamie Gennis9575f602011-10-07 14:51:16 -07002440 const LayerVector& layers(mDrawingState.layersSortedByZ);
2441 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002442 for (size_t i=0 ; i<count ; ++i) {
2443 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002444 const uint32_t flags = layer->drawingState().flags;
2445 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2446 const uint32_t z = layer->drawingState().z;
2447 if (z >= minLayerZ && z <= maxLayerZ) {
2448 layer->drawForSreenShot();
2449 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002450 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002451 }
2452
2453 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002454 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002455 glScissor(0, 0, sw, sh);
2456
2457 // check for errors and return screen capture
2458 if (glGetError() != GL_NO_ERROR) {
2459 // error while rendering
2460 result = INVALID_OPERATION;
2461 } else {
2462 // allocate shared memory large enough to hold the
2463 // screen capture
2464 sp<MemoryHeapBase> base(
2465 new MemoryHeapBase(size, 0, "screen-capture") );
2466 void* const ptr = base->getBase();
2467 if (ptr) {
2468 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002469 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002470 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2471 if (glGetError() == GL_NO_ERROR) {
2472 *heap = base;
2473 *w = sw;
2474 *h = sh;
2475 *f = PIXEL_FORMAT_RGBA_8888;
2476 result = NO_ERROR;
2477 }
2478 } else {
2479 result = NO_MEMORY;
2480 }
2481 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002482 glEnable(GL_SCISSOR_TEST);
2483 glViewport(0, 0, hw_w, hw_h);
2484 glMatrixMode(GL_PROJECTION);
2485 glPopMatrix();
2486 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002487 } else {
2488 result = BAD_VALUE;
2489 }
2490
2491 // release FBO resources
2492 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2493 glDeleteRenderbuffersOES(1, &tname);
2494 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002495
2496 hw.compositionComplete();
2497
Steve Block9d453682011-12-20 16:23:08 +00002498 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002499
Mathias Agopian74c40c02010-09-29 13:02:36 -07002500 return result;
2501}
2502
2503
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2505 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002506 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002507 uint32_t sw, uint32_t sh,
2508 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002509{
2510 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002511 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002512 return BAD_VALUE;
2513
2514 if (!GLExtensions::getInstance().haveFramebufferObject())
2515 return INVALID_OPERATION;
2516
2517 class MessageCaptureScreen : public MessageBase {
2518 SurfaceFlinger* flinger;
2519 DisplayID dpy;
2520 sp<IMemoryHeap>* heap;
2521 uint32_t* w;
2522 uint32_t* h;
2523 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002524 uint32_t sw;
2525 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002526 uint32_t minLayerZ;
2527 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002528 status_t result;
2529 public:
2530 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002531 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002532 uint32_t sw, uint32_t sh,
2533 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002534 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002535 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2536 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2537 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002538 {
2539 }
2540 status_t getResult() const {
2541 return result;
2542 }
2543 virtual bool handler() {
2544 Mutex::Autolock _l(flinger->mStateLock);
2545
2546 // if we have secure windows, never allow the screen capture
2547 if (flinger->mSecureFrameBuffer)
2548 return true;
2549
Mathias Agopian74c40c02010-09-29 13:02:36 -07002550 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002551 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002552
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002553 return true;
2554 }
2555 };
2556
2557 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002558 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002559 status_t res = postMessageSync(msg);
2560 if (res == NO_ERROR) {
2561 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2562 }
2563 return res;
2564}
2565
2566// ---------------------------------------------------------------------------
2567
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002568sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2569{
2570 sp<Layer> result;
2571 Mutex::Autolock _l(mStateLock);
2572 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2573 return result;
2574}
2575
2576// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577
Mathias Agopian96f08192010-06-02 23:28:45 -07002578Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002579 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002580{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581}
2582
Mathias Agopian96f08192010-06-02 23:28:45 -07002583Client::~Client()
2584{
Mathias Agopian96f08192010-06-02 23:28:45 -07002585 const size_t count = mLayers.size();
2586 for (size_t i=0 ; i<count ; i++) {
2587 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2588 if (layer != 0) {
2589 mFlinger->removeLayer(layer);
2590 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591 }
2592}
2593
Mathias Agopian96f08192010-06-02 23:28:45 -07002594status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002595 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002596}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002597
Mathias Agopian4f113742011-05-03 16:21:41 -07002598size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002599{
Mathias Agopian4f113742011-05-03 16:21:41 -07002600 Mutex::Autolock _l(mLock);
2601 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002602 mLayers.add(name, layer);
2603 return name;
2604}
2605
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002606void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002607{
Mathias Agopian4f113742011-05-03 16:21:41 -07002608 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002609 // we do a linear search here, because this doesn't happen often
2610 const size_t count = mLayers.size();
2611 for (size_t i=0 ; i<count ; i++) {
2612 if (mLayers.valueAt(i) == layer) {
2613 mLayers.removeItemsAt(i, 1);
2614 break;
2615 }
2616 }
2617}
Mathias Agopian4f113742011-05-03 16:21:41 -07002618sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2619{
2620 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002621 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002622 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002623 if (layer != 0) {
2624 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002625 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002626 }
2627 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628}
2629
Mathias Agopiana67932f2011-04-20 14:20:59 -07002630
2631status_t Client::onTransact(
2632 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2633{
2634 // these must be checked
2635 IPCThreadState* ipc = IPCThreadState::self();
2636 const int pid = ipc->getCallingPid();
2637 const int uid = ipc->getCallingUid();
2638 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002639 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002640 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002641 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002642 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002643 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002644 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2645 return PERMISSION_DENIED;
2646 }
2647 }
2648 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002649}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002650
2651
Mathias Agopian96f08192010-06-02 23:28:45 -07002652sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002653 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002654 const String8& name,
2655 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 uint32_t flags)
2657{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002658 /*
2659 * createSurface must be called from the GL thread so that it can
2660 * have access to the GL context.
2661 */
2662
2663 class MessageCreateSurface : public MessageBase {
2664 sp<ISurface> result;
2665 SurfaceFlinger* flinger;
2666 ISurfaceComposerClient::surface_data_t* params;
2667 Client* client;
2668 const String8& name;
2669 DisplayID display;
2670 uint32_t w, h;
2671 PixelFormat format;
2672 uint32_t flags;
2673 public:
2674 MessageCreateSurface(SurfaceFlinger* flinger,
2675 ISurfaceComposerClient::surface_data_t* params,
2676 const String8& name, Client* client,
2677 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2678 uint32_t flags)
2679 : flinger(flinger), params(params), client(client), name(name),
2680 display(display), w(w), h(h), format(format), flags(flags)
2681 {
2682 }
2683 sp<ISurface> getResult() const { return result; }
2684 virtual bool handler() {
2685 result = flinger->createSurface(params, name, client,
2686 display, w, h, format, flags);
2687 return true;
2688 }
2689 };
2690
2691 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2692 params, name, this, display, w, h, format, flags);
2693 mFlinger->postMessageSync(msg);
2694 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002695}
Mathias Agopian96f08192010-06-02 23:28:45 -07002696status_t Client::destroySurface(SurfaceID sid) {
2697 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002698}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002699
2700// ---------------------------------------------------------------------------
2701
Jamie Gennis9a78c902011-01-12 18:30:40 -08002702GraphicBufferAlloc::GraphicBufferAlloc() {}
2703
2704GraphicBufferAlloc::~GraphicBufferAlloc() {}
2705
2706sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002707 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002708 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2709 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002710 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002711 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002712 if (err == NO_MEMORY) {
2713 GraphicBuffer::dumpAllocationsToSystemLog();
2714 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002715 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002716 "failed (%s), handle=%p",
2717 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002718 return 0;
2719 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002720 return graphicBuffer;
2721}
2722
Jamie Gennis9a78c902011-01-12 18:30:40 -08002723// ---------------------------------------------------------------------------
2724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725GraphicPlane::GraphicPlane()
2726 : mHw(0)
2727{
2728}
2729
2730GraphicPlane::~GraphicPlane() {
2731 delete mHw;
2732}
2733
2734bool GraphicPlane::initialized() const {
2735 return mHw ? true : false;
2736}
2737
Mathias Agopian2b92d892010-02-08 15:49:35 -08002738int GraphicPlane::getWidth() const {
2739 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002740}
2741
Mathias Agopian2b92d892010-02-08 15:49:35 -08002742int GraphicPlane::getHeight() const {
2743 return mHeight;
2744}
2745
2746void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2747{
2748 mHw = hw;
2749
2750 // initialize the display orientation transform.
2751 // it's a constant that should come from the display driver.
2752 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2753 char property[PROPERTY_VALUE_MAX];
2754 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2755 //displayOrientation
2756 switch (atoi(property)) {
2757 case 90:
2758 displayOrientation = ISurfaceComposer::eOrientation90;
2759 break;
2760 case 270:
2761 displayOrientation = ISurfaceComposer::eOrientation270;
2762 break;
2763 }
2764 }
2765
2766 const float w = hw->getWidth();
2767 const float h = hw->getHeight();
2768 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2769 &mDisplayTransform);
2770 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2771 mDisplayWidth = h;
2772 mDisplayHeight = w;
2773 } else {
2774 mDisplayWidth = w;
2775 mDisplayHeight = h;
2776 }
2777
2778 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002779}
2780
2781status_t GraphicPlane::orientationToTransfrom(
2782 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002783{
2784 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002785 switch (orientation) {
2786 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002787 flags = Transform::ROT_0;
2788 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002789 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002790 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002791 break;
2792 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002793 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002794 break;
2795 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002796 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002797 break;
2798 default:
2799 return BAD_VALUE;
2800 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002801 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002802 return NO_ERROR;
2803}
2804
2805status_t GraphicPlane::setOrientation(int orientation)
2806{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002807 // If the rotation can be handled in hardware, this is where
2808 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002809
2810 const DisplayHardware& hw(displayHardware());
2811 const float w = mDisplayWidth;
2812 const float h = mDisplayHeight;
2813 mWidth = int(w);
2814 mHeight = int(h);
2815
2816 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002817 GraphicPlane::orientationToTransfrom(orientation, w, h,
2818 &orientationTransform);
2819 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2820 mWidth = int(h);
2821 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002822 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002823
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002824 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002825 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002826 return NO_ERROR;
2827}
2828
2829const DisplayHardware& GraphicPlane::displayHardware() const {
2830 return *mHw;
2831}
2832
Mathias Agopian59119e62010-10-11 12:37:43 -07002833DisplayHardware& GraphicPlane::editDisplayHardware() {
2834 return *mHw;
2835}
2836
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002837const Transform& GraphicPlane::transform() const {
2838 return mGlobalTransform;
2839}
2840
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002841EGLDisplay GraphicPlane::getEGLDisplay() const {
2842 return mHw->getEGLDisplay();
2843}
2844
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002845// ---------------------------------------------------------------------------
2846
2847}; // namespace android