blob: 25e80d7f08bc5d12059e52e9880c4a76a0360778 [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 Agopianed9807b2012-05-18 14:18:08 -070065#include <gui/BitTube.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
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
Colin Cross3854ed52012-03-23 14:17:18 -0700114#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115 property_get("debug.sf.ddms", value, "0");
116 mDebugDDMS = atoi(value);
117 if (mDebugDDMS) {
118 DdmConnection::start(getServiceName());
119 }
Colin Cross3854ed52012-03-23 14:17:18 -0700120#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Steve Blocka19954a2012-01-04 20:05:49 +0000122 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126void SurfaceFlinger::onFirstRef()
127{
128 mEventQueue.init(this);
129
130 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
131
132 // Wait for the main thread to be done with its initialization
133 mReadyToRunBarrier.wait();
134}
135
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137SurfaceFlinger::~SurfaceFlinger()
138{
139 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142void SurfaceFlinger::binderDied(const wp<IBinder>& who)
143{
144 // the window manager died on us. prepare its eulogy.
145
146 // reset screen orientation
147 Vector<ComposerState> state;
148 setTransactionState(state, eOrientationDefault, 0);
149
150 // restart the boot-animation
151 property_set("ctl.start", "bootanim");
152}
153
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700154sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700156 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157}
158
Mathias Agopian7e27f052010-05-28 14:22:23 -0700159sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Mathias Agopian96f08192010-06-02 23:28:45 -0700161 sp<ISurfaceComposerClient> bclient;
162 sp<Client> client(new Client(this));
163 status_t err = client->initCheck();
164 if (err == NO_ERROR) {
165 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 return bclient;
168}
169
Jamie Gennis9a78c902011-01-12 18:30:40 -0800170sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
171{
172 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
173 return gba;
174}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
177{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000178 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 const GraphicPlane& plane(mGraphicPlanes[dpy]);
180 return plane;
181}
182
183GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
184{
185 return const_cast<GraphicPlane&>(
186 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
187}
188
189void SurfaceFlinger::bootFinished()
190{
191 const nsecs_t now = systemTime();
192 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000193 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700194 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700195
196 // wait patiently for the window manager death
197 const String16 name("window");
198 sp<IBinder> window(defaultServiceManager()->getService(name));
199 if (window != 0) {
200 window->linkToDeath(this);
201 }
202
203 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700204 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205}
206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207static inline uint16_t pack565(int r, int g, int b) {
208 return (r<<11)|(g<<5)|b;
209}
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211status_t SurfaceFlinger::readyToRun()
212{
Steve Blocka19954a2012-01-04 20:05:49 +0000213 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 "Initializing graphics H/W...");
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 // we only support one display currently
217 int dpy = 0;
218
219 {
220 // initialize the main display
221 GraphicPlane& plane(graphicPlane(dpy));
222 DisplayHardware* const hw = new DisplayHardware(this, dpy);
223 plane.setDisplayHardware(hw);
224 }
225
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700226 // create the shared control-block
227 mServerHeap = new MemoryHeapBase(4096,
228 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000229 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700230
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700231 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000232 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700233
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700234 new(mServerCblk) surface_flinger_cblk_t;
235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 // initialize primary screen
237 // (other display should be initialized in the same manner, but
238 // asynchronously, as they could come and go. None of this is supported
239 // yet).
240 const GraphicPlane& plane(graphicPlane(dpy));
241 const DisplayHardware& hw = plane.displayHardware();
242 const uint32_t w = hw.getWidth();
243 const uint32_t h = hw.getHeight();
244 const uint32_t f = hw.getFormat();
245 hw.makeCurrent();
246
247 // initialize the shared control block
248 mServerCblk->connected |= 1<<dpy;
249 display_cblk_t* dcblk = mServerCblk->displays + dpy;
250 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800251 dcblk->w = plane.getWidth();
252 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 dcblk->format = f;
254 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
255 dcblk->xdpi = hw.getDpiX();
256 dcblk->ydpi = hw.getDpiY();
257 dcblk->fps = hw.getRefreshRate();
258 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259
260 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700262 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 glShadeModel(GL_FLAT);
265 glDisable(GL_DITHER);
266 glDisable(GL_CULL_FACE);
267
268 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
269 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700270 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 glGenTextures(1, &mWormholeTexName);
272 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
279
280 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
281 glGenTextures(1, &mProtectedTexName);
282 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
287 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
288 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
290 glViewport(0, 0, w, h);
291 glMatrixMode(GL_PROJECTION);
292 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700293 // put the origin in the left-bottom corner
294 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800296
297 // start the EventThread
298 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800299 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800300 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301
302 /*
303 * We're now ready to accept clients...
304 */
305
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800306 mReadyToRunBarrier.open();
307
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700308 // start boot animation
309 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 return NO_ERROR;
312}
313
314// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800315
Jamie Gennis582270d2011-08-17 18:19:00 -0700316bool SurfaceFlinger::authenticateSurfaceTexture(
317 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800318 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700319 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800320
321 // Check the visible layer list for the ISurface
322 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
323 size_t count = currentLayers.size();
324 for (size_t i=0 ; i<count ; i++) {
325 const sp<LayerBase>& layer(currentLayers[i]);
326 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700327 if (lbc != NULL) {
328 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
329 if (lbcBinder == surfaceTextureBinder) {
330 return true;
331 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800332 }
333 }
334
335 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700336 // SurfaceTexture gets destroyed before all the clients are done using it,
337 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800338 // will instead fail later on when the client tries to use the surface,
339 // which should be reported as "surface XYZ returned an -ENODEV". The
340 // purgatorized layers are no less authentic than the visible ones, so this
341 // should not cause any harm.
342 size_t purgatorySize = mLayerPurgatory.size();
343 for (size_t i=0 ; i<purgatorySize ; i++) {
344 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
345 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700346 if (lbc != NULL) {
347 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
348 if (lbcBinder == surfaceTextureBinder) {
349 return true;
350 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800351 }
352 }
353
354 return false;
355}
356
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800357// ----------------------------------------------------------------------------
358
359sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800360 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700361}
362
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800364
365void SurfaceFlinger::waitForEvent() {
366 mEventQueue.waitMessage();
367}
368
369void SurfaceFlinger::signalTransaction() {
370 mEventQueue.invalidate();
371}
372
373void SurfaceFlinger::signalLayerUpdate() {
374 mEventQueue.invalidate();
375}
376
377void SurfaceFlinger::signalRefresh() {
378 mEventQueue.refresh();
379}
380
381status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
382 nsecs_t reltime, uint32_t flags) {
383 return mEventQueue.postMessage(msg, reltime);
384}
385
386status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
387 nsecs_t reltime, uint32_t flags) {
388 status_t res = mEventQueue.postMessage(msg, reltime);
389 if (res == NO_ERROR) {
390 msg->wait();
391 }
392 return res;
393}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394
395bool SurfaceFlinger::threadLoop()
396{
397 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 return true;
399}
400
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800401void SurfaceFlinger::onMessageReceived(int32_t what)
402{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800403 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800404 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700405 case MessageQueue::REFRESH: {
406// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800407 // if we're in a global transaction, don't do anything.
408 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
409 uint32_t transactionFlags = peekTransactionFlags(mask);
410 if (CC_UNLIKELY(transactionFlags)) {
411 handleTransaction(transactionFlags);
412 }
413
414 // post surfaces (if needed)
415 handlePageFlip();
416
Mathias Agopian69a655c2012-04-11 20:43:19 -0700417// signalRefresh();
418//
419// } break;
420//
421// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800422
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800423 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800424
Mathias Agopian303d5382012-02-05 01:49:16 -0800425 const DisplayHardware& hw(graphicPlane(0).displayHardware());
426
Mathias Agopian69a655c2012-04-11 20:43:19 -0700427// if (mDirtyRegion.isEmpty()) {
428// return;
429// }
430
Mathias Agopianb048cef2012-02-04 15:44:04 -0800431 if (CC_UNLIKELY(mHwWorkListDirty)) {
432 // build the h/w work list
433 handleWorkList();
434 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800435
Mathias Agopianb048cef2012-02-04 15:44:04 -0800436 if (CC_LIKELY(hw.canDraw())) {
437 // repaint the framebuffer (if needed)
438 handleRepaint();
439 // inform the h/w that we're done compositing
440 hw.compositionComplete();
441 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800442 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800443 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800444 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800445 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800446
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800447 } break;
448 }
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451void SurfaceFlinger::postFramebuffer()
452{
Mathias Agopian841cde52012-03-01 15:44:37 -0800453 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800454 // mSwapRegion can be empty here is some cases, for instance if a hidden
455 // or fully transparent window is updating.
456 // in that case, we need to flip anyways to not risk a deadlock with
457 // h/w composer.
458
Mathias Agopiana44b0412011-10-16 18:46:35 -0700459 const DisplayHardware& hw(graphicPlane(0).displayHardware());
460 const nsecs_t now = systemTime();
461 mDebugInSwapBuffers = now;
462 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800463
464 size_t numLayers = mVisibleLayersSortedByZ.size();
465 for (size_t i = 0; i < numLayers; i++) {
466 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
467 }
468
Mathias Agopiana44b0412011-10-16 18:46:35 -0700469 mLastSwapBufferTime = systemTime() - now;
470 mDebugInSwapBuffers = 0;
471 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
475{
Mathias Agopian841cde52012-03-01 15:44:37 -0800476 ATRACE_CALL();
477
Mathias Agopianca4d3602011-05-19 15:38:14 -0700478 Mutex::Autolock _l(mStateLock);
479 const nsecs_t now = systemTime();
480 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481
Mathias Agopianca4d3602011-05-19 15:38:14 -0700482 // Here we're guaranteed that some transaction flags are set
483 // so we can call handleTransactionLocked() unconditionally.
484 // We call getTransactionFlags(), which will also clear the flags,
485 // with mStateLock held to guarantee that mCurrentState won't change
486 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700487
Mathias Agopianca4d3602011-05-19 15:38:14 -0700488 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
489 transactionFlags = getTransactionFlags(mask);
490 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700491
Mathias Agopianca4d3602011-05-19 15:38:14 -0700492 mLastTransactionTime = systemTime() - now;
493 mDebugInTransaction = 0;
494 invalidateHwcGeometry();
495 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700496}
497
Mathias Agopianca4d3602011-05-19 15:38:14 -0700498void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700499{
500 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 const size_t count = currentLayers.size();
502
503 /*
504 * Traversal of the children
505 * (perform the transaction for each of them if needed)
506 */
507
508 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
509 if (layersNeedTransaction) {
510 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
513 if (!trFlags) continue;
514
515 const uint32_t flags = layer->doTransaction(0);
516 if (flags & Layer::eVisibleRegion)
517 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 }
519 }
520
521 /*
522 * Perform our own transaction if needed
523 */
524
525 if (transactionFlags & eTransactionNeeded) {
526 if (mCurrentState.orientation != mDrawingState.orientation) {
527 // the orientation has changed, recompute all visible regions
528 // and invalidate everything.
529
530 const int dpy = 0;
531 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700532 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 GraphicPlane& plane(graphicPlane(dpy));
534 plane.setOrientation(orientation);
535
536 // update the shared control block
537 const DisplayHardware& hw(plane.displayHardware());
538 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
539 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800540 dcblk->w = plane.getWidth();
541 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
543 mVisibleRegionsDirty = true;
544 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 }
546
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700547 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
548 // layers have been added
549 mVisibleRegionsDirty = true;
550 }
551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 // some layers might have been removed, so
553 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700555 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700558 const size_t count = previousLayers.size();
559 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700560 const sp<LayerBase>& layer(previousLayers[i]);
561 if (currentLayers.indexOf( layer ) < 0) {
562 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700563 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700564 }
565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 }
568
569 commitTransaction();
570}
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800573 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574{
Mathias Agopian841cde52012-03-01 15:44:37 -0800575 ATRACE_CALL();
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 const GraphicPlane& plane(graphicPlane(0));
578 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700579 const DisplayHardware& hw(plane.displayHardware());
580 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581
582 Region aboveOpaqueLayers;
583 Region aboveCoveredLayers;
584 Region dirty;
585
586 bool secureFrameBuffer = false;
587
588 size_t i = currentLayers.size();
589 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 layer->validateVisibility(planeTransform);
592
593 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700594 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595
Mathias Agopianab028732010-03-16 16:41:46 -0700596 /*
597 * opaqueRegion: area of a surface that is fully opaque.
598 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700600
601 /*
602 * visibleRegion: area of a surface that is visible on screen
603 * and not fully transparent. This is essentially the layer's
604 * footprint minus the opaque regions above it.
605 * Areas covered by a translucent surface are considered visible.
606 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700608
609 /*
610 * coveredRegion: area of a surface that is covered by all
611 * visible regions above it (which includes the translucent areas).
612 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700614
615
616 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800617 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700618 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700619 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700621 visibleRegion.andSelf(screenRegion);
622 if (!visibleRegion.isEmpty()) {
623 // Remove the transparent area from the visible region
624 if (translucent) {
625 visibleRegion.subtractSelf(layer->transparentRegionScreen);
626 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopianab028732010-03-16 16:41:46 -0700628 // compute the opaque region
629 const int32_t layerOrientation = layer->getOrientation();
630 if (s.alpha==255 && !translucent &&
631 ((layerOrientation & Transform::ROT_INVALID) == false)) {
632 // the opaque region is the layer's footprint
633 opaqueRegion = visibleRegion;
634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636 }
637
Mathias Agopianab028732010-03-16 16:41:46 -0700638 // Clip the covered region to the visible region
639 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
640
641 // Update aboveCoveredLayers for next (lower) layer
642 aboveCoveredLayers.orSelf(visibleRegion);
643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 // subtract the opaque region covered by the layers above us
645 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
647 // compute this layer's dirty region
648 if (layer->contentDirty) {
649 // we need to invalidate the whole region
650 dirty = visibleRegion;
651 // as well, as the old visible region
652 dirty.orSelf(layer->visibleRegionScreen);
653 layer->contentDirty = false;
654 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700655 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700656 * the exposed region consists of two components:
657 * 1) what's VISIBLE now and was COVERED before
658 * 2) what's EXPOSED now less what was EXPOSED before
659 *
660 * note that (1) is conservative, we start with the whole
661 * visible region but only keep what used to be covered by
662 * something -- which mean it may have been exposed.
663 *
664 * (2) handles areas that were not covered by anything but got
665 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700666 */
Mathias Agopianab028732010-03-16 16:41:46 -0700667 const Region newExposed = visibleRegion - coveredRegion;
668 const Region oldVisibleRegion = layer->visibleRegionScreen;
669 const Region oldCoveredRegion = layer->coveredRegionScreen;
670 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
671 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 }
673 dirty.subtractSelf(aboveOpaqueLayers);
674
675 // accumulate to the screen dirty region
676 dirtyRegion.orSelf(dirty);
677
Mathias Agopianab028732010-03-16 16:41:46 -0700678 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 // Store the visible region is screen space
682 layer->setVisibleRegion(visibleRegion);
683 layer->setCoveredRegion(coveredRegion);
684
Mathias Agopian97011222009-07-28 10:57:27 -0700685 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 if (layer->isSecure() && !visibleRegion.isEmpty()) {
687 secureFrameBuffer = true;
688 }
689 }
690
Mathias Agopian97011222009-07-28 10:57:27 -0700691 // invalidate the areas where a layer was removed
692 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
693 mDirtyRegionRemovedLayer.clear();
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 mSecureFrameBuffer = secureFrameBuffer;
696 opaqueRegion = aboveOpaqueLayers;
697}
698
699
700void SurfaceFlinger::commitTransaction()
701{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800702 if (!mLayersPendingRemoval.isEmpty()) {
703 // Notify removed layers now that they can't be drawn from
704 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
705 mLayersPendingRemoval[i]->onRemoved();
706 }
707 mLayersPendingRemoval.clear();
708 }
709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700711 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700712 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713}
714
715void SurfaceFlinger::handlePageFlip()
716{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800717 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800718 const DisplayHardware& hw = graphicPlane(0).displayHardware();
719 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
722 const bool visibleRegions = lockPageFlip(currentLayers);
723
724 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 Region opaqueRegion;
726 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700727
728 /*
729 * rebuild the visible layer list
730 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800731 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700732 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700733 mVisibleLayersSortedByZ.setCapacity(count);
734 for (size_t i=0 ; i<count ; i++) {
735 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
736 mVisibleLayersSortedByZ.add(currentLayers[i]);
737 }
738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 mWormholeRegion = screenRegion.subtract(opaqueRegion);
740 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800741 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
743
744 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700745
746 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 mDirtyRegion.andSelf(screenRegion);
748}
749
Mathias Agopianad456f92011-01-13 17:53:01 -0800750void SurfaceFlinger::invalidateHwcGeometry()
751{
752 mHwWorkListDirty = true;
753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
756{
757 bool recomputeVisibleRegions = false;
758 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700761 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 layer->lockPageFlip(recomputeVisibleRegions);
763 }
764 return recomputeVisibleRegions;
765}
766
767void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
768{
769 const GraphicPlane& plane(graphicPlane(0));
770 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800771 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700772 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700774 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 layer->unlockPageFlip(planeTransform, mDirtyRegion);
776 }
777}
778
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800779void SurfaceFlinger::handleRefresh()
780{
781 bool needInvalidate = false;
782 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
783 const size_t count = currentLayers.size();
784 for (size_t i=0 ; i<count ; i++) {
785 const sp<LayerBase>& layer(currentLayers[i]);
786 if (layer->onPreComposition()) {
787 needInvalidate = true;
788 }
789 }
790 if (needInvalidate) {
791 signalLayerUpdate();
792 }
793}
794
795
Mathias Agopiana350ff92010-08-10 17:14:02 -0700796void SurfaceFlinger::handleWorkList()
797{
798 mHwWorkListDirty = false;
799 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
800 if (hwc.initCheck() == NO_ERROR) {
801 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
802 const size_t count = currentLayers.size();
803 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700804 hwc_layer_t* const cur(hwc.getLayers());
805 for (size_t i=0 ; cur && i<count ; i++) {
806 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700807 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700808 cur[i].compositionType = HWC_FRAMEBUFFER;
809 cur[i].flags |= HWC_SKIP_LAYER;
810 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700811 }
812 }
813}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815void SurfaceFlinger::handleRepaint()
816{
Mathias Agopian841cde52012-03-01 15:44:37 -0800817 ATRACE_CALL();
818
Mathias Agopianb8a55602009-06-26 19:06:36 -0700819 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700820 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821
Glenn Kasten99ed2242011-12-15 09:51:17 -0800822 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 debugFlashRegions();
824 }
825
Mathias Agopianb8a55602009-06-26 19:06:36 -0700826 // set the frame buffer
827 const DisplayHardware& hw(graphicPlane(0).displayHardware());
828 glMatrixMode(GL_MODELVIEW);
829 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
831 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700832 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700833 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
834 // takes a rectangle, we must make sure to update that whole
835 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700836 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700837 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700838 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700839 // We need to redraw the rectangle that will be updated
840 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700841 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700842 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700843 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700844 } else {
845 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700847 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 }
849 }
850
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700851 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 composeSurfaces(mDirtyRegion);
853
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700854 // update the swap region and clear the dirty region
855 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 mDirtyRegion.clear();
857}
858
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700859void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700861 const DisplayHardware& hw(graphicPlane(0).displayHardware());
862 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700863 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700864 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700865 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700866 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700867
Mathias Agopianf384cc32011-09-08 18:31:55 -0700868 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
869 size_t count = layers.size();
870
Steve Blocke6f43dd2012-01-06 19:20:56 +0000871 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700872 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
873 hwc.getNumLayers(), count);
874
875 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700876 if (hwc.initCheck() == NO_ERROR) {
877 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
878 }
Mathias Agopian45721772010-08-12 15:03:26 -0700879
880 /*
881 * update the per-frame h/w composer data for each layer
882 * and build the transparent region of the FB
883 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700884 for (size_t i=0 ; i<count ; i++) {
885 const sp<LayerBase>& layer(layers[i]);
886 layer->setPerFrameData(&cur[i]);
887 }
888 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000889 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700890}
Mathias Agopian45721772010-08-12 15:03:26 -0700891
Mathias Agopianf384cc32011-09-08 18:31:55 -0700892void SurfaceFlinger::composeSurfaces(const Region& dirty)
893{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700894 const DisplayHardware& hw(graphicPlane(0).displayHardware());
895 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700896 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700897
898 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700899 if (!cur || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700900 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700901
Mathias Agopianb9494d52012-04-18 02:28:45 -0700902 if (hwc.getLayerCount(HWC_OVERLAY)) {
903 // when using overlays, we assume a fully transparent framebuffer
904 // NOTE: we could reduce how much we need to clear, for instance
905 // remove where there are opaque FB layers. however, on some
906 // GPUs doing a "clean slate" glClear might be more efficient.
907 // We'll revisit later if needed.
908 glClearColor(0, 0, 0, 0);
909 glClear(GL_COLOR_BUFFER_BIT);
910 } else {
911 // screen is already cleared here
912 if (!mWormholeRegion.isEmpty()) {
913 // can happen with SurfaceView
914 drawWormhole();
915 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700916 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700917
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700918 /*
919 * and then, render the layers targeted at the framebuffer
920 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700921
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700922 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
923 const size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700924
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700925 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700926 const sp<LayerBase>& layer(layers[i]);
927 const Region clip(dirty.intersect(layer->visibleRegionScreen));
928 if (!clip.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700929 if (cur && (cur[i].compositionType == HWC_OVERLAY)) {
930 if (i && (cur[i].hints & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700931 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700932 // never clear the very first layer since we're
933 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700934 layer->clearWithOpenGL(clip);
935 }
936 continue;
937 }
938 // render the layer
939 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700940 }
941 }
942 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943}
944
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945void SurfaceFlinger::debugFlashRegions()
946{
Mathias Agopian0a917752010-06-14 21:20:00 -0700947 const DisplayHardware& hw(graphicPlane(0).displayHardware());
948 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700949 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700950 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700951 return;
952 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700953
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700954 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700955 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
956 mDirtyRegion.bounds() : hw.bounds());
957 composeSurfaces(repaint);
958 }
959
Mathias Agopianc492e672011-10-18 14:49:27 -0700960 glDisable(GL_TEXTURE_EXTERNAL_OES);
961 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963
Mathias Agopian0926f502009-05-04 14:17:04 -0700964 static int toggle = 0;
965 toggle = 1 - toggle;
966 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700967 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700968 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700969 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700970 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
Mathias Agopian20f68782009-05-11 00:03:41 -0700972 Region::const_iterator it = mDirtyRegion.begin();
973 Region::const_iterator const end = mDirtyRegion.end();
974 while (it != end) {
975 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700977 { r.left, height - r.top },
978 { r.left, height - r.bottom },
979 { r.right, height - r.bottom },
980 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 };
982 glVertexPointer(2, GL_FLOAT, 0, vertices);
983 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
984 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700985
Mathias Agopian0656a682011-09-20 17:22:44 -0700986 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987
988 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700989 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990}
991
992void SurfaceFlinger::drawWormhole() const
993{
994 const Region region(mWormholeRegion.intersect(mDirtyRegion));
995 if (region.isEmpty())
996 return;
997
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700998 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -0700999 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001000 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001001 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001002
1003 GLfloat vertices[4][2];
1004 glVertexPointer(2, GL_FLOAT, 0, vertices);
1005 Region::const_iterator it = region.begin();
1006 Region::const_iterator const end = region.end();
1007 while (it != end) {
1008 const Rect& r = *it++;
1009 vertices[0][0] = r.left;
1010 vertices[0][1] = r.top;
1011 vertices[1][0] = r.right;
1012 vertices[1][1] = r.top;
1013 vertices[2][0] = r.right;
1014 vertices[2][1] = r.bottom;
1015 vertices[3][0] = r.left;
1016 vertices[3][1] = r.bottom;
1017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019}
1020
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001021status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022{
1023 Mutex::Autolock _l(mStateLock);
1024 addLayer_l(layer);
1025 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1026 return NO_ERROR;
1027}
1028
Mathias Agopian96f08192010-06-02 23:28:45 -07001029status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1030{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001031 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001032 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1033}
1034
1035ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1036 const sp<LayerBaseClient>& lbc)
1037{
Mathias Agopian96f08192010-06-02 23:28:45 -07001038 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001039 size_t name = client->attachLayer(lbc);
1040
1041 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001042
1043 // add this layer to the current state list
1044 addLayer_l(lbc);
1045
Mathias Agopian4f113742011-05-03 16:21:41 -07001046 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001047}
1048
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001049status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050{
1051 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001052 status_t err = purgatorizeLayer_l(layer);
1053 if (err == NO_ERROR)
1054 setTransactionFlags(eTransactionNeeded);
1055 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001058status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001060 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1061 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001062 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001063 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1065 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 return NO_ERROR;
1068 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001069 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070}
1071
Mathias Agopian9a112062009-04-17 19:36:26 -07001072status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1073{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001074 // First add the layer to the purgatory list, which makes sure it won't
1075 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001076 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001077 if (err >= 0) {
1078 mLayerPurgatory.add(layerBase);
1079 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001080
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001081 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001082
Mathias Agopian3d579642009-06-04 18:46:21 -07001083 // it's possible that we don't find a layer, because it might
1084 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001085 // from the user because there is a race between Client::destroySurface(),
1086 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001087 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1088}
1089
Mathias Agopian96f08192010-06-02 23:28:45 -07001090status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091{
Mathias Agopian96f08192010-06-02 23:28:45 -07001092 layer->forceVisibilityTransaction();
1093 setTransactionFlags(eTraversalNeeded);
1094 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopiandea20b12011-05-03 17:04:02 -07001097uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1098{
1099 return android_atomic_release_load(&mTransactionFlags);
1100}
1101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1103{
1104 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1105}
1106
Mathias Agopianbb641242010-05-18 17:06:55 -07001107uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108{
1109 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1110 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001111 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 }
1113 return old;
1114}
1115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001117void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001118 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001119 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001120
Jamie Gennis28378392011-10-12 17:39:00 -07001121 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001122 if (mCurrentState.orientation != orientation) {
1123 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1124 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001125 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001126 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001127 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001128 orientation);
1129 }
1130 }
1131
Mathias Agopian698c0872011-06-28 19:09:31 -07001132 const size_t count = state.size();
1133 for (size_t i=0 ; i<count ; i++) {
1134 const ComposerState& s(state[i]);
1135 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001136 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001137 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001138
Mathias Agopian386aa982011-11-07 21:58:03 -08001139 if (transactionFlags) {
1140 // this triggers the transaction
1141 setTransactionFlags(transactionFlags);
1142
1143 // if this is a synchronous transaction, wait for it to take effect
1144 // before returning.
1145 if (flags & eSynchronous) {
1146 mTransationPending = true;
1147 }
1148 while (mTransationPending) {
1149 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1150 if (CC_UNLIKELY(err != NO_ERROR)) {
1151 // just in case something goes wrong in SF, return to the
1152 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001153 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001154 mTransationPending = false;
1155 break;
1156 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 }
1159}
1160
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001161sp<ISurface> SurfaceFlinger::createSurface(
1162 ISurfaceComposerClient::surface_data_t* params,
1163 const String8& name,
1164 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1166 uint32_t flags)
1167{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001168 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001169 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001170
1171 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001172 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001173 int(w), int(h));
1174 return surfaceHandle;
1175 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001176
Mathias Agopianff615cc2012-02-24 14:58:36 -08001177 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001178 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 switch (flags & eFXSurfaceMask) {
1180 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001181 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1182 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 break;
1184 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001185 // for now we treat Blur as Dim, until we can implement it
1186 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001188 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001190 case eFXSurfaceScreenshot:
1191 layer = createScreenshotSurface(client, d, w, h, flags);
1192 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 }
1194
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001195 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001196 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001197 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001198 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001201 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001202 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001203 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001204 if (normalLayer != 0) {
1205 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001206 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001207 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001208 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 }
1212
1213 return surfaceHandle;
1214}
1215
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001216sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001217 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001218 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001219 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220{
1221 // initialize the surfaces
1222 switch (format) { // TODO: take h/w into account
1223 case PIXEL_FORMAT_TRANSPARENT:
1224 case PIXEL_FORMAT_TRANSLUCENT:
1225 format = PIXEL_FORMAT_RGBA_8888;
1226 break;
1227 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001228#ifdef NO_RGBX_8888
1229 format = PIXEL_FORMAT_RGB_565;
1230#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001231 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001232#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 break;
1234 }
1235
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001236#ifdef NO_RGBX_8888
1237 if (format == PIXEL_FORMAT_RGBX_8888)
1238 format = PIXEL_FORMAT_RGBA_8888;
1239#endif
1240
Mathias Agopian96f08192010-06-02 23:28:45 -07001241 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001242 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001243 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001244 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001245 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247 return layer;
1248}
1249
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001250sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001251 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001252 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001255 return layer;
1256}
1257
1258sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1259 const sp<Client>& client, DisplayID display,
1260 uint32_t w, uint32_t h, uint32_t flags)
1261{
1262 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 return layer;
1264}
1265
Mathias Agopian96f08192010-06-02 23:28:45 -07001266status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267{
Mathias Agopian9a112062009-04-17 19:36:26 -07001268 /*
1269 * called by the window manager, when a surface should be marked for
1270 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001271 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001272 * The surface is removed from the current and drawing lists, but placed
1273 * in the purgatory queue, so it's not destroyed right-away (we need
1274 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001275 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001276
Mathias Agopian48d819a2009-09-10 19:41:18 -07001277 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001278 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001279 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001280
Mathias Agopian48d819a2009-09-10 19:41:18 -07001281 if (layer != 0) {
1282 err = purgatorizeLayer_l(layer);
1283 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001284 setTransactionFlags(eTransactionNeeded);
1285 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001286 }
1287 return err;
1288}
1289
Mathias Agopianca4d3602011-05-19 15:38:14 -07001290status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001291{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001292 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001293 status_t err = NO_ERROR;
1294 sp<LayerBaseClient> l(layer.promote());
1295 if (l != NULL) {
1296 Mutex::Autolock _l(mStateLock);
1297 err = removeLayer_l(l);
1298 if (err == NAME_NOT_FOUND) {
1299 // The surface wasn't in the current list, which means it was
1300 // removed already, which means it is in the purgatory,
1301 // and need to be removed from there.
1302 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001303 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001304 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001305 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001306 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001307 "error removing layer=%p (%s)", l.get(), strerror(-err));
1308 }
1309 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310}
1311
Mathias Agopian698c0872011-06-28 19:09:31 -07001312uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001313 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001314 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001317 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1318 if (layer != 0) {
1319 const uint32_t what = s.what;
1320 if (what & ePositionChanged) {
1321 if (layer->setPosition(s.x, s.y))
1322 flags |= eTraversalNeeded;
1323 }
1324 if (what & eLayerChanged) {
1325 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1326 if (layer->setLayer(s.z)) {
1327 mCurrentState.layersSortedByZ.removeAt(idx);
1328 mCurrentState.layersSortedByZ.add(layer);
1329 // we need traversal (state changed)
1330 // AND transaction (list changed)
1331 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 }
1333 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001334 if (what & eSizeChanged) {
1335 if (layer->setSize(s.w, s.h)) {
1336 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001337 }
1338 }
1339 if (what & eAlphaChanged) {
1340 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1341 flags |= eTraversalNeeded;
1342 }
1343 if (what & eMatrixChanged) {
1344 if (layer->setMatrix(s.matrix))
1345 flags |= eTraversalNeeded;
1346 }
1347 if (what & eTransparentRegionChanged) {
1348 if (layer->setTransparentRegionHint(s.transparentRegion))
1349 flags |= eTraversalNeeded;
1350 }
1351 if (what & eVisibilityChanged) {
1352 if (layer->setFlags(s.flags, s.mask))
1353 flags |= eTraversalNeeded;
1354 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001355 if (what & eCropChanged) {
1356 if (layer->setCrop(s.crop))
1357 flags |= eTraversalNeeded;
1358 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001360 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361}
1362
Mathias Agopianb60314a2012-04-10 22:09:54 -07001363// ---------------------------------------------------------------------------
1364
1365void SurfaceFlinger::onScreenAcquired() {
1366 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1367 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001368 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001369 // this is a temporary work-around, eventually this should be called
1370 // by the power-manager
1371 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001372 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001373 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374}
1375
Mathias Agopianb60314a2012-04-10 22:09:54 -07001376void SurfaceFlinger::onScreenReleased() {
1377 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1378 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001379 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001380 hw.releaseScreen();
1381 // from this point on, SF will stop drawing
1382 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383}
1384
Mathias Agopianb60314a2012-04-10 22:09:54 -07001385void SurfaceFlinger::screenAcquired() {
1386 class MessageScreenAcquired : public MessageBase {
1387 SurfaceFlinger* flinger;
1388 public:
1389 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1390 virtual bool handler() {
1391 flinger->onScreenAcquired();
1392 return true;
1393 }
1394 };
1395 sp<MessageBase> msg = new MessageScreenAcquired(this);
1396 postMessageSync(msg);
1397}
1398
1399void SurfaceFlinger::screenReleased() {
1400 class MessageScreenReleased : public MessageBase {
1401 SurfaceFlinger* flinger;
1402 public:
1403 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1404 virtual bool handler() {
1405 flinger->onScreenReleased();
1406 return true;
1407 }
1408 };
1409 sp<MessageBase> msg = new MessageScreenReleased(this);
1410 postMessageSync(msg);
1411}
1412
1413// ---------------------------------------------------------------------------
1414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1416{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001417 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 char buffer[SIZE];
1419 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001420
1421 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 snprintf(buffer, SIZE, "Permission Denial: "
1423 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1424 IPCThreadState::self()->getCallingPid(),
1425 IPCThreadState::self()->getCallingUid());
1426 result.append(buffer);
1427 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001428 // Try to get the main lock, but don't insist if we can't
1429 // (this would indicate SF is stuck, but we want to be able to
1430 // print something in dumpsys).
1431 int retry = 3;
1432 while (mStateLock.tryLock()<0 && --retry>=0) {
1433 usleep(1000000);
1434 }
1435 const bool locked(retry >= 0);
1436 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001437 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001438 "SurfaceFlinger appears to be unresponsive, "
1439 "dumping anyways (no locks held)\n");
1440 result.append(buffer);
1441 }
1442
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001443 bool dumpAll = true;
1444 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001445 size_t numArgs = args.size();
1446 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001447 if ((index < numArgs) &&
1448 (args[index] == String16("--list"))) {
1449 index++;
1450 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001451 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001452 }
1453
1454 if ((index < numArgs) &&
1455 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001456 index++;
1457 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001458 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001459 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001460
1461 if ((index < numArgs) &&
1462 (args[index] == String16("--latency-clear"))) {
1463 index++;
1464 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001465 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001468
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001469 if (dumpAll) {
1470 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001471 }
1472
Mathias Agopian9795c422009-08-26 16:36:26 -07001473 if (locked) {
1474 mStateLock.unlock();
1475 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 }
1477 write(fd, result.string(), result.size());
1478 return NO_ERROR;
1479}
1480
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001481void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1482 String8& result, char* buffer, size_t SIZE) const
1483{
1484 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1485 const size_t count = currentLayers.size();
1486 for (size_t i=0 ; i<count ; i++) {
1487 const sp<LayerBase>& layer(currentLayers[i]);
1488 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1489 result.append(buffer);
1490 }
1491}
1492
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001493void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1494 String8& result, char* buffer, size_t SIZE) const
1495{
1496 String8 name;
1497 if (index < args.size()) {
1498 name = String8(args[index]);
1499 index++;
1500 }
1501
1502 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1503 const size_t count = currentLayers.size();
1504 for (size_t i=0 ; i<count ; i++) {
1505 const sp<LayerBase>& layer(currentLayers[i]);
1506 if (name.isEmpty()) {
1507 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1508 result.append(buffer);
1509 }
1510 if (name.isEmpty() || (name == layer->getName())) {
1511 layer->dumpStats(result, buffer, SIZE);
1512 }
1513 }
1514}
1515
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001516void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1517 String8& result, char* buffer, size_t SIZE) const
1518{
1519 String8 name;
1520 if (index < args.size()) {
1521 name = String8(args[index]);
1522 index++;
1523 }
1524
1525 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1526 const size_t count = currentLayers.size();
1527 for (size_t i=0 ; i<count ; i++) {
1528 const sp<LayerBase>& layer(currentLayers[i]);
1529 if (name.isEmpty() || (name == layer->getName())) {
1530 layer->clearStats();
1531 }
1532 }
1533}
1534
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001535void SurfaceFlinger::dumpAllLocked(
1536 String8& result, char* buffer, size_t SIZE) const
1537{
1538 // figure out if we're stuck somewhere
1539 const nsecs_t now = systemTime();
1540 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1541 const nsecs_t inTransaction(mDebugInTransaction);
1542 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1543 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1544
1545 /*
1546 * Dump the visible layer list
1547 */
1548 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1549 const size_t count = currentLayers.size();
1550 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1551 result.append(buffer);
1552 for (size_t i=0 ; i<count ; i++) {
1553 const sp<LayerBase>& layer(currentLayers[i]);
1554 layer->dump(result, buffer, SIZE);
1555 }
1556
1557 /*
1558 * Dump the layers in the purgatory
1559 */
1560
1561 const size_t purgatorySize = mLayerPurgatory.size();
1562 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1563 result.append(buffer);
1564 for (size_t i=0 ; i<purgatorySize ; i++) {
1565 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1566 layer->shortDump(result, buffer, SIZE);
1567 }
1568
1569 /*
1570 * Dump SurfaceFlinger global state
1571 */
1572
1573 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1574 result.append(buffer);
1575
1576 const GLExtensions& extensions(GLExtensions::getInstance());
1577 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1578 extensions.getVendor(),
1579 extensions.getRenderer(),
1580 extensions.getVersion());
1581 result.append(buffer);
1582
1583 snprintf(buffer, SIZE, "EGL : %s\n",
1584 eglQueryString(graphicPlane(0).getEGLDisplay(),
1585 EGL_VERSION_HW_ANDROID));
1586 result.append(buffer);
1587
1588 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1589 result.append(buffer);
1590
1591 mWormholeRegion.dump(result, "WormholeRegion");
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 snprintf(buffer, SIZE,
1594 " orientation=%d, canDraw=%d\n",
1595 mCurrentState.orientation, hw.canDraw());
1596 result.append(buffer);
1597 snprintf(buffer, SIZE,
1598 " last eglSwapBuffers() time: %f us\n"
1599 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001600 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001601 " refresh-rate : %f fps\n"
1602 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001603 " y-dpi : %f\n"
1604 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001605 mLastSwapBufferTime/1000.0,
1606 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001607 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001608 hw.getRefreshRate(),
1609 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001610 hw.getDpiY(),
1611 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001612 result.append(buffer);
1613
1614 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1615 inSwapBuffersDuration/1000.0);
1616 result.append(buffer);
1617
1618 snprintf(buffer, SIZE, " transaction time: %f us\n",
1619 inTransactionDuration/1000.0);
1620 result.append(buffer);
1621
1622 /*
1623 * VSYNC state
1624 */
1625 mEventThread->dump(result, buffer, SIZE);
1626
1627 /*
1628 * Dump HWComposer state
1629 */
1630 HWComposer& hwc(hw.getHwComposer());
1631 snprintf(buffer, SIZE, "h/w composer state:\n");
1632 result.append(buffer);
1633 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1634 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1635 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1636 result.append(buffer);
1637 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1638
1639 /*
1640 * Dump gralloc state
1641 */
1642 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1643 alloc.dump(result);
1644 hw.dump(result);
1645}
1646
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647status_t SurfaceFlinger::onTransact(
1648 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1649{
1650 switch (code) {
1651 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001652 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001655 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001656 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 {
1658 // codes that require permission check
1659 IPCThreadState* ipc = IPCThreadState::self();
1660 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001661 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001662 if ((uid != AID_GRAPHICS) &&
1663 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001664 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001665 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1666 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001668 break;
1669 }
1670 case CAPTURE_SCREEN:
1671 {
1672 // codes that require permission check
1673 IPCThreadState* ipc = IPCThreadState::self();
1674 const int pid = ipc->getCallingPid();
1675 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001676 if ((uid != AID_GRAPHICS) &&
1677 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001678 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001679 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1680 return PERMISSION_DENIED;
1681 }
1682 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 }
1684 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1687 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001688 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001689 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001690 IPCThreadState* ipc = IPCThreadState::self();
1691 const int pid = ipc->getCallingPid();
1692 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001693 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001694 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 return PERMISSION_DENIED;
1696 }
1697 int n;
1698 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001699 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001700 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 return NO_ERROR;
1702 case 1002: // SHOW_UPDATES
1703 n = data.readInt32();
1704 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001705 invalidateHwcGeometry();
1706 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001709 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001710 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001712 case 1005:{ // force transaction
1713 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1714 return NO_ERROR;
1715 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001716 case 1006:{ // send empty update
1717 signalRefresh();
1718 return NO_ERROR;
1719 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001720 case 1008: // toggle use of hw composer
1721 n = data.readInt32();
1722 mDebugDisableHWC = n ? 1 : 0;
1723 invalidateHwcGeometry();
1724 repaintEverything();
1725 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001726 case 1009: // toggle use of transform hint
1727 n = data.readInt32();
1728 mDebugDisableTransformHint = n ? 1 : 0;
1729 invalidateHwcGeometry();
1730 repaintEverything();
1731 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001733 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 reply->writeInt32(0);
1735 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001736 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001737 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 return NO_ERROR;
1739 case 1013: {
1740 Mutex::Autolock _l(mStateLock);
1741 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1742 reply->writeInt32(hw.getPageFlipCount());
1743 }
1744 return NO_ERROR;
1745 }
1746 }
1747 return err;
1748}
1749
Mathias Agopian53331da2011-08-22 21:44:41 -07001750void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001751 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001752 const Rect bounds(hw.getBounds());
1753 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001754 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001755}
1756
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001757void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1758 Mutex::Autolock _l(mInvalidateLock);
1759 mInvalidateRegion = reg;
1760}
1761
1762Region SurfaceFlinger::getAndClearInvalidateRegion() {
1763 Mutex::Autolock _l(mInvalidateLock);
1764 Region reg(mInvalidateRegion);
1765 mInvalidateRegion.clear();
1766 return reg;
1767}
1768
Mathias Agopian59119e62010-10-11 12:37:43 -07001769// ---------------------------------------------------------------------------
1770
Mathias Agopian118d0242011-10-13 16:02:48 -07001771status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1772 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1773{
1774 Mutex::Autolock _l(mStateLock);
1775 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1776}
1777
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001778status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1779 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001780{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001781 ATRACE_CALL();
1782
Mathias Agopian59119e62010-10-11 12:37:43 -07001783 if (!GLExtensions::getInstance().haveFramebufferObject())
1784 return INVALID_OPERATION;
1785
1786 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001787 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001788 const uint32_t hw_w = hw.getWidth();
1789 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001790 GLfloat u = 1;
1791 GLfloat v = 1;
1792
1793 // make sure to clear all GL error flags
1794 while ( glGetError() != GL_NO_ERROR ) ;
1795
1796 // create a FBO
1797 GLuint name, tname;
1798 glGenTextures(1, &tname);
1799 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001800 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1801 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001802 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1803 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001804 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001805 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001806 GLint tw = (2 << (31 - clz(hw_w)));
1807 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001808 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1809 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001810 u = GLfloat(hw_w) / tw;
1811 v = GLfloat(hw_h) / th;
1812 }
1813 glGenFramebuffersOES(1, &name);
1814 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001815 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1816 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001817
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001819 glDisable(GL_TEXTURE_EXTERNAL_OES);
1820 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001821 glClearColor(0,0,0,1);
1822 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001823 glMatrixMode(GL_MODELVIEW);
1824 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001825 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1826 const size_t count = layers.size();
1827 for (size_t i=0 ; i<count ; ++i) {
1828 const sp<LayerBase>& layer(layers[i]);
1829 layer->drawForSreenShot();
1830 }
1831
Mathias Agopian118d0242011-10-13 16:02:48 -07001832 hw.compositionComplete();
1833
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001834 // back to main framebuffer
1835 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001836 glDeleteFramebuffersOES(1, &name);
1837
1838 *textureName = tname;
1839 *uOut = u;
1840 *vOut = v;
1841 return NO_ERROR;
1842}
1843
1844// ---------------------------------------------------------------------------
1845
Mathias Agopianed9807b2012-05-18 14:18:08 -07001846class VSyncWaiter {
1847 DisplayEventReceiver::Event buffer[4];
1848 sp<Looper> looper;
1849 sp<IDisplayEventConnection> events;
1850 sp<BitTube> eventTube;
1851public:
1852 VSyncWaiter(const sp<EventThread>& eventThread) {
1853 looper = new Looper(true);
1854 events = eventThread->createEventConnection();
1855 eventTube = events->getDataChannel();
1856 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1857 events->requestNextVsync();
1858 }
1859
1860 void wait() {
1861 ssize_t n;
1862
1863 looper->pollOnce(-1);
1864 // we don't handle any errors here, it doesn't matter
1865 // and we don't want to take the risk to get stuck.
1866
1867 // drain the events...
1868 while ((n = DisplayEventReceiver::getEvents(
1869 eventTube, buffer, 4)) > 0) ;
1870
1871 events->requestNextVsync();
1872 }
1873};
1874
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001875status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1876{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001877 // get screen geometry
1878 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1879 const uint32_t hw_w = hw.getWidth();
1880 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001881 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001882
1883 GLfloat u, v;
1884 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001885 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001886 if (result != NO_ERROR) {
1887 return result;
1888 }
1889
1890 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001891 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001892 glBindTexture(GL_TEXTURE_2D, tname);
1893 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1894 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1895 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001896 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1897 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001898 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1899 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1900 glVertexPointer(2, GL_FLOAT, 0, vtx);
1901
Mathias Agopianffcf4652011-07-07 17:30:31 -07001902 /*
1903 * Texture coordinate mapping
1904 *
1905 * u
1906 * 1 +----------+---+
1907 * | | | | image is inverted
1908 * | V | | w.r.t. the texture
1909 * 1-v +----------+ | coordinates
1910 * | |
1911 * | |
1912 * | |
1913 * 0 +--------------+
1914 * 0 1
1915 *
1916 */
1917
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001918 class s_curve_interpolator {
1919 const float nbFrames, s, v;
1920 public:
1921 s_curve_interpolator(int nbFrames, float s)
1922 : nbFrames(1.0f / (nbFrames-1)), s(s),
1923 v(1.0f + expf(-s + 0.5f*s)) {
1924 }
1925 float operator()(int f) {
1926 const float x = f * nbFrames;
1927 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1928 }
1929 };
1930
1931 class v_stretch {
1932 const GLfloat hw_w, hw_h;
1933 public:
1934 v_stretch(uint32_t hw_w, uint32_t hw_h)
1935 : hw_w(hw_w), hw_h(hw_h) {
1936 }
1937 void operator()(GLfloat* vtx, float v) {
1938 const GLfloat w = hw_w + (hw_w * v);
1939 const GLfloat h = hw_h - (hw_h * v);
1940 const GLfloat x = (hw_w - w) * 0.5f;
1941 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001942 vtx[0] = x; vtx[1] = y;
1943 vtx[2] = x; vtx[3] = y + h;
1944 vtx[4] = x + w; vtx[5] = y + h;
1945 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001946 }
1947 };
1948
1949 class h_stretch {
1950 const GLfloat hw_w, hw_h;
1951 public:
1952 h_stretch(uint32_t hw_w, uint32_t hw_h)
1953 : hw_w(hw_w), hw_h(hw_h) {
1954 }
1955 void operator()(GLfloat* vtx, float v) {
1956 const GLfloat w = hw_w - (hw_w * v);
1957 const GLfloat h = 1.0f;
1958 const GLfloat x = (hw_w - w) * 0.5f;
1959 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001960 vtx[0] = x; vtx[1] = y;
1961 vtx[2] = x; vtx[3] = y + h;
1962 vtx[4] = x + w; vtx[5] = y + h;
1963 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001964 }
1965 };
1966
Mathias Agopianed9807b2012-05-18 14:18:08 -07001967 VSyncWaiter vsync(mEventThread);
1968
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001969 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001970 char value[PROPERTY_VALUE_MAX];
1971 property_get("debug.sf.electron_frames", value, "24");
1972 int nbFrames = (atoi(value) + 1) >> 1;
1973 if (nbFrames <= 0) // just in case
1974 nbFrames = 24;
1975
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001976 s_curve_interpolator itr(nbFrames, 7.5f);
1977 s_curve_interpolator itg(nbFrames, 8.0f);
1978 s_curve_interpolator itb(nbFrames, 8.5f);
1979
1980 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001981
1982 glMatrixMode(GL_TEXTURE);
1983 glLoadIdentity();
1984 glMatrixMode(GL_MODELVIEW);
1985 glLoadIdentity();
1986
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001987 glEnable(GL_BLEND);
1988 glBlendFunc(GL_ONE, GL_ONE);
1989 for (int i=0 ; i<nbFrames ; i++) {
1990 float x, y, w, h;
1991 const float vr = itr(i);
1992 const float vg = itg(i);
1993 const float vb = itb(i);
1994
Mathias Agopianed9807b2012-05-18 14:18:08 -07001995 // wait for vsync
1996 vsync.wait();
1997
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001998 // clear screen
1999 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002000 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002001 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002002
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002003 // draw the red plane
2004 vverts(vtx, vr);
2005 glColorMask(1,0,0,1);
2006 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002007
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002008 // draw the green plane
2009 vverts(vtx, vg);
2010 glColorMask(0,1,0,1);
2011 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002012
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002013 // draw the blue plane
2014 vverts(vtx, vb);
2015 glColorMask(0,0,1,1);
2016 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002017
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002018 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002019 glDisable(GL_TEXTURE_2D);
2020 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021 glColor4f(vg, vg, vg, 1);
2022 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2023 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002024 }
2025
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 h_stretch hverts(hw_w, hw_h);
2027 glDisable(GL_BLEND);
2028 glDisable(GL_TEXTURE_2D);
2029 glColorMask(1,1,1,1);
2030 for (int i=0 ; i<nbFrames ; i++) {
2031 const float v = itg(i);
2032 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002033
2034 // wait for vsync
2035 vsync.wait();
2036
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002037 glClear(GL_COLOR_BUFFER_BIT);
2038 glColor4f(1-v, 1-v, 1-v, 1);
2039 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2040 hw.flip(screenBounds);
2041 }
2042
2043 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2045 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002046 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002047 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002048 return NO_ERROR;
2049}
2050
2051status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2052{
2053 status_t result = PERMISSION_DENIED;
2054
2055 if (!GLExtensions::getInstance().haveFramebufferObject())
2056 return INVALID_OPERATION;
2057
2058
2059 // get screen geometry
2060 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2061 const uint32_t hw_w = hw.getWidth();
2062 const uint32_t hw_h = hw.getHeight();
2063 const Region screenBounds(hw.bounds());
2064
2065 GLfloat u, v;
2066 GLuint tname;
2067 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2068 if (result != NO_ERROR) {
2069 return result;
2070 }
2071
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002072 GLfloat vtx[8];
2073 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002074 glBindTexture(GL_TEXTURE_2D, tname);
2075 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2076 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2077 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002078 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2079 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002080 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2081 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2082 glVertexPointer(2, GL_FLOAT, 0, vtx);
2083
2084 class s_curve_interpolator {
2085 const float nbFrames, s, v;
2086 public:
2087 s_curve_interpolator(int nbFrames, float s)
2088 : nbFrames(1.0f / (nbFrames-1)), s(s),
2089 v(1.0f + expf(-s + 0.5f*s)) {
2090 }
2091 float operator()(int f) {
2092 const float x = f * nbFrames;
2093 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2094 }
2095 };
2096
2097 class v_stretch {
2098 const GLfloat hw_w, hw_h;
2099 public:
2100 v_stretch(uint32_t hw_w, uint32_t hw_h)
2101 : hw_w(hw_w), hw_h(hw_h) {
2102 }
2103 void operator()(GLfloat* vtx, float v) {
2104 const GLfloat w = hw_w + (hw_w * v);
2105 const GLfloat h = hw_h - (hw_h * v);
2106 const GLfloat x = (hw_w - w) * 0.5f;
2107 const GLfloat y = (hw_h - h) * 0.5f;
2108 vtx[0] = x; vtx[1] = y;
2109 vtx[2] = x; vtx[3] = y + h;
2110 vtx[4] = x + w; vtx[5] = y + h;
2111 vtx[6] = x + w; vtx[7] = y;
2112 }
2113 };
2114
2115 class h_stretch {
2116 const GLfloat hw_w, hw_h;
2117 public:
2118 h_stretch(uint32_t hw_w, uint32_t hw_h)
2119 : hw_w(hw_w), hw_h(hw_h) {
2120 }
2121 void operator()(GLfloat* vtx, float v) {
2122 const GLfloat w = hw_w - (hw_w * v);
2123 const GLfloat h = 1.0f;
2124 const GLfloat x = (hw_w - w) * 0.5f;
2125 const GLfloat y = (hw_h - h) * 0.5f;
2126 vtx[0] = x; vtx[1] = y;
2127 vtx[2] = x; vtx[3] = y + h;
2128 vtx[4] = x + w; vtx[5] = y + h;
2129 vtx[6] = x + w; vtx[7] = y;
2130 }
2131 };
2132
Mathias Agopianed9807b2012-05-18 14:18:08 -07002133 VSyncWaiter vsync(mEventThread);
2134
Mathias Agopiana6546e52010-10-14 12:33:07 -07002135 // the full animation is 12 frames
2136 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002137 s_curve_interpolator itr(nbFrames, 7.5f);
2138 s_curve_interpolator itg(nbFrames, 8.0f);
2139 s_curve_interpolator itb(nbFrames, 8.5f);
2140
2141 h_stretch hverts(hw_w, hw_h);
2142 glDisable(GL_BLEND);
2143 glDisable(GL_TEXTURE_2D);
2144 glColorMask(1,1,1,1);
2145 for (int i=nbFrames-1 ; i>=0 ; i--) {
2146 const float v = itg(i);
2147 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002148
2149 // wait for vsync
2150 vsync.wait();
2151
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002152 glClear(GL_COLOR_BUFFER_BIT);
2153 glColor4f(1-v, 1-v, 1-v, 1);
2154 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2155 hw.flip(screenBounds);
2156 }
2157
Mathias Agopiana6546e52010-10-14 12:33:07 -07002158 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002159 v_stretch vverts(hw_w, hw_h);
2160 glEnable(GL_BLEND);
2161 glBlendFunc(GL_ONE, GL_ONE);
2162 for (int i=nbFrames-1 ; i>=0 ; i--) {
2163 float x, y, w, h;
2164 const float vr = itr(i);
2165 const float vg = itg(i);
2166 const float vb = itb(i);
2167
Mathias Agopianed9807b2012-05-18 14:18:08 -07002168 // wait for vsync
2169 vsync.wait();
2170
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002171 // clear screen
2172 glColorMask(1,1,1,1);
2173 glClear(GL_COLOR_BUFFER_BIT);
2174 glEnable(GL_TEXTURE_2D);
2175
2176 // draw the red plane
2177 vverts(vtx, vr);
2178 glColorMask(1,0,0,1);
2179 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2180
2181 // draw the green plane
2182 vverts(vtx, vg);
2183 glColorMask(0,1,0,1);
2184 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2185
2186 // draw the blue plane
2187 vverts(vtx, vb);
2188 glColorMask(0,0,1,1);
2189 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2190
2191 hw.flip(screenBounds);
2192 }
2193
2194 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002195 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002196 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002197 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002198 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002199
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 return NO_ERROR;
2201}
2202
2203// ---------------------------------------------------------------------------
2204
Mathias Agopianabd671a2010-10-14 14:54:06 -07002205status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002207 ATRACE_CALL();
2208
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002209 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2210 if (!hw.canDraw()) {
2211 // we're already off
2212 return NO_ERROR;
2213 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002214
2215 // turn off hwc while we're doing the animation
2216 hw.getHwComposer().disable();
2217 // and make sure to turn it back on (if needed) next time we compose
2218 invalidateHwcGeometry();
2219
Mathias Agopianabd671a2010-10-14 14:54:06 -07002220 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2221 electronBeamOffAnimationImplLocked();
2222 }
2223
2224 // always clear the whole screen at the end of the animation
2225 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002226 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002227 hw.flip( Region(hw.bounds()) );
2228
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002229 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002230}
2231
2232status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2233{
Mathias Agopian59119e62010-10-11 12:37:43 -07002234 class MessageTurnElectronBeamOff : public MessageBase {
2235 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002236 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002237 status_t result;
2238 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002239 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2240 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002241 }
2242 status_t getResult() const {
2243 return result;
2244 }
2245 virtual bool handler() {
2246 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002247 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 return true;
2249 }
2250 };
2251
Mathias Agopianabd671a2010-10-14 14:54:06 -07002252 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002253 status_t res = postMessageSync(msg);
2254 if (res == NO_ERROR) {
2255 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002256
2257 // work-around: when the power-manager calls us we activate the
2258 // animation. eventually, the "on" animation will be called
2259 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002261 }
2262 return res;
2263}
2264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002265// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002266
Mathias Agopianabd671a2010-10-14 14:54:06 -07002267status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002268{
2269 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2270 if (hw.canDraw()) {
2271 // we're already on
2272 return NO_ERROR;
2273 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002274 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2275 electronBeamOnAnimationImplLocked();
2276 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002277
2278 // make sure to redraw the whole screen when the animation is done
2279 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002280 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002281
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002282 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002283}
2284
2285status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2286{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002287 class MessageTurnElectronBeamOn : public MessageBase {
2288 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002289 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290 status_t result;
2291 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002292 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2293 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002294 }
2295 status_t getResult() const {
2296 return result;
2297 }
2298 virtual bool handler() {
2299 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 return true;
2302 }
2303 };
2304
Mathias Agopianabd671a2010-10-14 14:54:06 -07002305 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002306 return NO_ERROR;
2307}
2308
2309// ---------------------------------------------------------------------------
2310
Mathias Agopian74c40c02010-09-29 13:02:36 -07002311status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2312 sp<IMemoryHeap>* heap,
2313 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002314 uint32_t sw, uint32_t sh,
2315 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002316{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002317 ATRACE_CALL();
2318
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319 status_t result = PERMISSION_DENIED;
2320
2321 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002322 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002323 return BAD_VALUE;
2324
2325 if (!GLExtensions::getInstance().haveFramebufferObject())
2326 return INVALID_OPERATION;
2327
2328 // get screen geometry
2329 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2330 const uint32_t hw_w = hw.getWidth();
2331 const uint32_t hw_h = hw.getHeight();
2332
2333 if ((sw > hw_w) || (sh > hw_h))
2334 return BAD_VALUE;
2335
2336 sw = (!sw) ? hw_w : sw;
2337 sh = (!sh) ? hw_h : sh;
2338 const size_t size = sw * sh * 4;
2339
Steve Block9d453682011-12-20 16:23:08 +00002340 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002341 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002342
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 // make sure to clear all GL error flags
2344 while ( glGetError() != GL_NO_ERROR ) ;
2345
2346 // create a FBO
2347 GLuint name, tname;
2348 glGenRenderbuffersOES(1, &tname);
2349 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2350 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002351
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 glGenFramebuffersOES(1, &name);
2353 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2354 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2355 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2356
2357 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002358
Mathias Agopian74c40c02010-09-29 13:02:36 -07002359 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2360
2361 // invert everything, b/c glReadPixel() below will invert the FB
2362 glViewport(0, 0, sw, sh);
2363 glMatrixMode(GL_PROJECTION);
2364 glPushMatrix();
2365 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002366 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002367 glMatrixMode(GL_MODELVIEW);
2368
2369 // redraw the screen entirely...
2370 glClearColor(0,0,0,1);
2371 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002372
Jamie Gennis9575f602011-10-07 14:51:16 -07002373 const LayerVector& layers(mDrawingState.layersSortedByZ);
2374 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 for (size_t i=0 ; i<count ; ++i) {
2376 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002377 const uint32_t flags = layer->drawingState().flags;
2378 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2379 const uint32_t z = layer->drawingState().z;
2380 if (z >= minLayerZ && z <= maxLayerZ) {
2381 layer->drawForSreenShot();
2382 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002383 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 }
2385
Mathias Agopian74c40c02010-09-29 13:02:36 -07002386 // check for errors and return screen capture
2387 if (glGetError() != GL_NO_ERROR) {
2388 // error while rendering
2389 result = INVALID_OPERATION;
2390 } else {
2391 // allocate shared memory large enough to hold the
2392 // screen capture
2393 sp<MemoryHeapBase> base(
2394 new MemoryHeapBase(size, 0, "screen-capture") );
2395 void* const ptr = base->getBase();
2396 if (ptr) {
2397 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002398 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002399 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2400 if (glGetError() == GL_NO_ERROR) {
2401 *heap = base;
2402 *w = sw;
2403 *h = sh;
2404 *f = PIXEL_FORMAT_RGBA_8888;
2405 result = NO_ERROR;
2406 }
2407 } else {
2408 result = NO_MEMORY;
2409 }
2410 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002411 glViewport(0, 0, hw_w, hw_h);
2412 glMatrixMode(GL_PROJECTION);
2413 glPopMatrix();
2414 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002415 } else {
2416 result = BAD_VALUE;
2417 }
2418
2419 // release FBO resources
2420 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2421 glDeleteRenderbuffersOES(1, &tname);
2422 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002423
2424 hw.compositionComplete();
2425
Steve Block9d453682011-12-20 16:23:08 +00002426 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002427
Mathias Agopian74c40c02010-09-29 13:02:36 -07002428 return result;
2429}
2430
2431
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002432status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2433 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002434 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002435 uint32_t sw, uint32_t sh,
2436 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002437{
2438 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002439 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440 return BAD_VALUE;
2441
2442 if (!GLExtensions::getInstance().haveFramebufferObject())
2443 return INVALID_OPERATION;
2444
2445 class MessageCaptureScreen : public MessageBase {
2446 SurfaceFlinger* flinger;
2447 DisplayID dpy;
2448 sp<IMemoryHeap>* heap;
2449 uint32_t* w;
2450 uint32_t* h;
2451 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002452 uint32_t sw;
2453 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002454 uint32_t minLayerZ;
2455 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002456 status_t result;
2457 public:
2458 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002459 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002460 uint32_t sw, uint32_t sh,
2461 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002462 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002463 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2464 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2465 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002466 {
2467 }
2468 status_t getResult() const {
2469 return result;
2470 }
2471 virtual bool handler() {
2472 Mutex::Autolock _l(flinger->mStateLock);
2473
2474 // if we have secure windows, never allow the screen capture
2475 if (flinger->mSecureFrameBuffer)
2476 return true;
2477
Mathias Agopian74c40c02010-09-29 13:02:36 -07002478 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002479 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002480
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002481 return true;
2482 }
2483 };
2484
2485 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002486 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002487 status_t res = postMessageSync(msg);
2488 if (res == NO_ERROR) {
2489 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2490 }
2491 return res;
2492}
2493
2494// ---------------------------------------------------------------------------
2495
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002496sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2497{
2498 sp<Layer> result;
2499 Mutex::Autolock _l(mStateLock);
2500 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2501 return result;
2502}
2503
2504// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505
Mathias Agopian96f08192010-06-02 23:28:45 -07002506Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002507 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002509}
2510
Mathias Agopian96f08192010-06-02 23:28:45 -07002511Client::~Client()
2512{
Mathias Agopian96f08192010-06-02 23:28:45 -07002513 const size_t count = mLayers.size();
2514 for (size_t i=0 ; i<count ; i++) {
2515 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2516 if (layer != 0) {
2517 mFlinger->removeLayer(layer);
2518 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519 }
2520}
2521
Mathias Agopian96f08192010-06-02 23:28:45 -07002522status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002523 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002525
Mathias Agopian4f113742011-05-03 16:21:41 -07002526size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002527{
Mathias Agopian4f113742011-05-03 16:21:41 -07002528 Mutex::Autolock _l(mLock);
2529 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002530 mLayers.add(name, layer);
2531 return name;
2532}
2533
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002534void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002535{
Mathias Agopian4f113742011-05-03 16:21:41 -07002536 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002537 // we do a linear search here, because this doesn't happen often
2538 const size_t count = mLayers.size();
2539 for (size_t i=0 ; i<count ; i++) {
2540 if (mLayers.valueAt(i) == layer) {
2541 mLayers.removeItemsAt(i, 1);
2542 break;
2543 }
2544 }
2545}
Mathias Agopian4f113742011-05-03 16:21:41 -07002546sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2547{
2548 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002549 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002550 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002551 if (layer != 0) {
2552 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002553 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002554 }
2555 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556}
2557
Mathias Agopiana67932f2011-04-20 14:20:59 -07002558
2559status_t Client::onTransact(
2560 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2561{
2562 // these must be checked
2563 IPCThreadState* ipc = IPCThreadState::self();
2564 const int pid = ipc->getCallingPid();
2565 const int uid = ipc->getCallingUid();
2566 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002567 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002568 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002569 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002570 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002571 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002572 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2573 return PERMISSION_DENIED;
2574 }
2575 }
2576 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002577}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002578
2579
Mathias Agopian96f08192010-06-02 23:28:45 -07002580sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002581 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002582 const String8& name,
2583 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002584 uint32_t flags)
2585{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002586 /*
2587 * createSurface must be called from the GL thread so that it can
2588 * have access to the GL context.
2589 */
2590
2591 class MessageCreateSurface : public MessageBase {
2592 sp<ISurface> result;
2593 SurfaceFlinger* flinger;
2594 ISurfaceComposerClient::surface_data_t* params;
2595 Client* client;
2596 const String8& name;
2597 DisplayID display;
2598 uint32_t w, h;
2599 PixelFormat format;
2600 uint32_t flags;
2601 public:
2602 MessageCreateSurface(SurfaceFlinger* flinger,
2603 ISurfaceComposerClient::surface_data_t* params,
2604 const String8& name, Client* client,
2605 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2606 uint32_t flags)
2607 : flinger(flinger), params(params), client(client), name(name),
2608 display(display), w(w), h(h), format(format), flags(flags)
2609 {
2610 }
2611 sp<ISurface> getResult() const { return result; }
2612 virtual bool handler() {
2613 result = flinger->createSurface(params, name, client,
2614 display, w, h, format, flags);
2615 return true;
2616 }
2617 };
2618
2619 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2620 params, name, this, display, w, h, format, flags);
2621 mFlinger->postMessageSync(msg);
2622 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623}
Mathias Agopian96f08192010-06-02 23:28:45 -07002624status_t Client::destroySurface(SurfaceID sid) {
2625 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627
2628// ---------------------------------------------------------------------------
2629
Jamie Gennis9a78c902011-01-12 18:30:40 -08002630GraphicBufferAlloc::GraphicBufferAlloc() {}
2631
2632GraphicBufferAlloc::~GraphicBufferAlloc() {}
2633
2634sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002635 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002636 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2637 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002638 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002639 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002640 if (err == NO_MEMORY) {
2641 GraphicBuffer::dumpAllocationsToSystemLog();
2642 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002643 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002644 "failed (%s), handle=%p",
2645 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002646 return 0;
2647 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002648 return graphicBuffer;
2649}
2650
Jamie Gennis9a78c902011-01-12 18:30:40 -08002651// ---------------------------------------------------------------------------
2652
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002653GraphicPlane::GraphicPlane()
2654 : mHw(0)
2655{
2656}
2657
2658GraphicPlane::~GraphicPlane() {
2659 delete mHw;
2660}
2661
2662bool GraphicPlane::initialized() const {
2663 return mHw ? true : false;
2664}
2665
Mathias Agopian2b92d892010-02-08 15:49:35 -08002666int GraphicPlane::getWidth() const {
2667 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002668}
2669
Mathias Agopian2b92d892010-02-08 15:49:35 -08002670int GraphicPlane::getHeight() const {
2671 return mHeight;
2672}
2673
2674void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2675{
2676 mHw = hw;
2677
2678 // initialize the display orientation transform.
2679 // it's a constant that should come from the display driver.
2680 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2681 char property[PROPERTY_VALUE_MAX];
2682 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2683 //displayOrientation
2684 switch (atoi(property)) {
2685 case 90:
2686 displayOrientation = ISurfaceComposer::eOrientation90;
2687 break;
2688 case 270:
2689 displayOrientation = ISurfaceComposer::eOrientation270;
2690 break;
2691 }
2692 }
2693
2694 const float w = hw->getWidth();
2695 const float h = hw->getHeight();
2696 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2697 &mDisplayTransform);
2698 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2699 mDisplayWidth = h;
2700 mDisplayHeight = w;
2701 } else {
2702 mDisplayWidth = w;
2703 mDisplayHeight = h;
2704 }
2705
2706 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002707}
2708
2709status_t GraphicPlane::orientationToTransfrom(
2710 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002711{
2712 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002713 switch (orientation) {
2714 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002715 flags = Transform::ROT_0;
2716 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002717 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002718 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002719 break;
2720 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002721 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002722 break;
2723 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002724 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 break;
2726 default:
2727 return BAD_VALUE;
2728 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002729 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002730 return NO_ERROR;
2731}
2732
2733status_t GraphicPlane::setOrientation(int orientation)
2734{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002735 // If the rotation can be handled in hardware, this is where
2736 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002737
2738 const DisplayHardware& hw(displayHardware());
2739 const float w = mDisplayWidth;
2740 const float h = mDisplayHeight;
2741 mWidth = int(w);
2742 mHeight = int(h);
2743
2744 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002745 GraphicPlane::orientationToTransfrom(orientation, w, h,
2746 &orientationTransform);
2747 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2748 mWidth = int(h);
2749 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002750 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002751
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002752 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002753 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002754 return NO_ERROR;
2755}
2756
2757const DisplayHardware& GraphicPlane::displayHardware() const {
2758 return *mHw;
2759}
2760
Mathias Agopian59119e62010-10-11 12:37:43 -07002761DisplayHardware& GraphicPlane::editDisplayHardware() {
2762 return *mHw;
2763}
2764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002765const Transform& GraphicPlane::transform() const {
2766 return mGlobalTransform;
2767}
2768
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002769EGLDisplay GraphicPlane::getEGLDisplay() const {
2770 return mHw->getEGLDisplay();
2771}
2772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002773// ---------------------------------------------------------------------------
2774
2775}; // namespace android