blob: 981d694241c4eb410ae0d002bf94ffd812c7340b [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 Agopiandb403e82012-06-18 16:47:56 -070053#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Glenn Kasten1db13d72011-12-19 13:55:34 -080064#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070066#include <gui/BitTube.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070067
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070090 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070099 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 mSecureFrameBuffer(0)
101{
102 init();
103}
104
105void SurfaceFlinger::init()
106{
Steve Blocka19954a2012-01-04 20:05:49 +0000107 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 property_get("debug.sf.showupdates", value, "0");
113 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
Colin Cross3854ed52012-03-23 14:17:18 -0700115#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116 property_get("debug.sf.ddms", value, "0");
117 mDebugDDMS = atoi(value);
118 if (mDebugDDMS) {
119 DdmConnection::start(getServiceName());
120 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700121#else
122#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700123#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124
Steve Blocka19954a2012-01-04 20:05:49 +0000125 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000126 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800129void SurfaceFlinger::onFirstRef()
130{
131 mEventQueue.init(this);
132
133 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
134
135 // Wait for the main thread to be done with its initialization
136 mReadyToRunBarrier.wait();
137}
138
139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140SurfaceFlinger::~SurfaceFlinger()
141{
142 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145void SurfaceFlinger::binderDied(const wp<IBinder>& who)
146{
147 // the window manager died on us. prepare its eulogy.
148
149 // reset screen orientation
150 Vector<ComposerState> state;
151 setTransactionState(state, eOrientationDefault, 0);
152
153 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700154 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800155}
156
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700157sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700159 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160}
161
Mathias Agopian7e27f052010-05-28 14:22:23 -0700162sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163{
Mathias Agopian96f08192010-06-02 23:28:45 -0700164 sp<ISurfaceComposerClient> bclient;
165 sp<Client> client(new Client(this));
166 status_t err = client->initCheck();
167 if (err == NO_ERROR) {
168 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 return bclient;
171}
172
Jamie Gennis9a78c902011-01-12 18:30:40 -0800173sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
174{
175 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
176 return gba;
177}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
180{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000181 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 const GraphicPlane& plane(mGraphicPlanes[dpy]);
183 return plane;
184}
185
186GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
187{
188 return const_cast<GraphicPlane&>(
189 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
190}
191
192void SurfaceFlinger::bootFinished()
193{
194 const nsecs_t now = systemTime();
195 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000196 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700197 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700198
199 // wait patiently for the window manager death
200 const String16 name("window");
201 sp<IBinder> window(defaultServiceManager()->getService(name));
202 if (window != 0) {
203 window->linkToDeath(this);
204 }
205
206 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700207 // formerly we would just kill the process, but we now ask it to exit so it
208 // can choose where to stop the animation.
209 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212static inline uint16_t pack565(int r, int g, int b) {
213 return (r<<11)|(g<<5)|b;
214}
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216status_t SurfaceFlinger::readyToRun()
217{
Steve Blocka19954a2012-01-04 20:05:49 +0000218 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 "Initializing graphics H/W...");
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 // we only support one display currently
222 int dpy = 0;
223
224 {
225 // initialize the main display
226 GraphicPlane& plane(graphicPlane(dpy));
227 DisplayHardware* const hw = new DisplayHardware(this, dpy);
228 plane.setDisplayHardware(hw);
229 }
230
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700231 // create the shared control-block
232 mServerHeap = new MemoryHeapBase(4096,
233 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000234 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700235
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700236 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000237 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700238
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700239 new(mServerCblk) surface_flinger_cblk_t;
240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 // initialize primary screen
242 // (other display should be initialized in the same manner, but
243 // asynchronously, as they could come and go. None of this is supported
244 // yet).
245 const GraphicPlane& plane(graphicPlane(dpy));
246 const DisplayHardware& hw = plane.displayHardware();
247 const uint32_t w = hw.getWidth();
248 const uint32_t h = hw.getHeight();
249 const uint32_t f = hw.getFormat();
250 hw.makeCurrent();
251
252 // initialize the shared control block
253 mServerCblk->connected |= 1<<dpy;
254 display_cblk_t* dcblk = mServerCblk->displays + dpy;
255 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800256 dcblk->w = plane.getWidth();
257 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 dcblk->format = f;
259 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
260 dcblk->xdpi = hw.getDpiX();
261 dcblk->ydpi = hw.getDpiY();
262 dcblk->fps = hw.getRefreshRate();
263 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264
265 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700267 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 glShadeModel(GL_FLAT);
270 glDisable(GL_DITHER);
271 glDisable(GL_CULL_FACE);
272
273 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
274 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700275 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 glGenTextures(1, &mWormholeTexName);
277 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
282 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700283 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
284
285 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
286 glGenTextures(1, &mProtectedTexName);
287 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
293 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
295 glViewport(0, 0, w, h);
296 glMatrixMode(GL_PROJECTION);
297 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700298 // put the origin in the left-bottom corner
299 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800301
302 // start the EventThread
303 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800304 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800310 mReadyToRunBarrier.open();
311
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700312 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700313 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 return NO_ERROR;
316}
317
Mathias Agopiana67e4182012-06-19 17:26:12 -0700318void SurfaceFlinger::startBootAnim() {
319 // start boot animation
320 property_set("service.bootanim.exit", "0");
321 property_set("ctl.start", "bootanim");
322}
323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800325
Jamie Gennis582270d2011-08-17 18:19:00 -0700326bool SurfaceFlinger::authenticateSurfaceTexture(
327 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800328 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700329 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800330
331 // Check the visible layer list for the ISurface
332 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
333 size_t count = currentLayers.size();
334 for (size_t i=0 ; i<count ; i++) {
335 const sp<LayerBase>& layer(currentLayers[i]);
336 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700337 if (lbc != NULL) {
338 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
339 if (lbcBinder == surfaceTextureBinder) {
340 return true;
341 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800342 }
343 }
344
345 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700346 // SurfaceTexture gets destroyed before all the clients are done using it,
347 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800348 // will instead fail later on when the client tries to use the surface,
349 // which should be reported as "surface XYZ returned an -ENODEV". The
350 // purgatorized layers are no less authentic than the visible ones, so this
351 // should not cause any harm.
352 size_t purgatorySize = mLayerPurgatory.size();
353 for (size_t i=0 ; i<purgatorySize ; i++) {
354 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
355 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700356 if (lbc != NULL) {
357 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
358 if (lbcBinder == surfaceTextureBinder) {
359 return true;
360 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800361 }
362 }
363
364 return false;
365}
366
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800367// ----------------------------------------------------------------------------
368
369sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800370 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700371}
372
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800374
375void SurfaceFlinger::waitForEvent() {
376 mEventQueue.waitMessage();
377}
378
379void SurfaceFlinger::signalTransaction() {
380 mEventQueue.invalidate();
381}
382
383void SurfaceFlinger::signalLayerUpdate() {
384 mEventQueue.invalidate();
385}
386
387void SurfaceFlinger::signalRefresh() {
388 mEventQueue.refresh();
389}
390
391status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
392 nsecs_t reltime, uint32_t flags) {
393 return mEventQueue.postMessage(msg, reltime);
394}
395
396status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
397 nsecs_t reltime, uint32_t flags) {
398 status_t res = mEventQueue.postMessage(msg, reltime);
399 if (res == NO_ERROR) {
400 msg->wait();
401 }
402 return res;
403}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404
405bool SurfaceFlinger::threadLoop()
406{
407 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 return true;
409}
410
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800411void SurfaceFlinger::onMessageReceived(int32_t what)
412{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800413 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800414 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700415 case MessageQueue::REFRESH: {
416// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800417 // if we're in a global transaction, don't do anything.
418 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
419 uint32_t transactionFlags = peekTransactionFlags(mask);
420 if (CC_UNLIKELY(transactionFlags)) {
421 handleTransaction(transactionFlags);
422 }
423
424 // post surfaces (if needed)
425 handlePageFlip();
426
Mathias Agopian69a655c2012-04-11 20:43:19 -0700427// signalRefresh();
428//
429// } break;
430//
431// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800432
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800433 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800434
Mathias Agopian303d5382012-02-05 01:49:16 -0800435 const DisplayHardware& hw(graphicPlane(0).displayHardware());
436
Mathias Agopian69a655c2012-04-11 20:43:19 -0700437// if (mDirtyRegion.isEmpty()) {
438// return;
439// }
440
Mathias Agopianb048cef2012-02-04 15:44:04 -0800441 if (CC_UNLIKELY(mHwWorkListDirty)) {
442 // build the h/w work list
443 handleWorkList();
444 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800445
Mathias Agopianb048cef2012-02-04 15:44:04 -0800446 if (CC_LIKELY(hw.canDraw())) {
447 // repaint the framebuffer (if needed)
448 handleRepaint();
449 // inform the h/w that we're done compositing
450 hw.compositionComplete();
451 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800452 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800453 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800454 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800455 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800456
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800457 } break;
458 }
459}
460
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461void SurfaceFlinger::postFramebuffer()
462{
Mathias Agopian841cde52012-03-01 15:44:37 -0800463 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800464 // mSwapRegion can be empty here is some cases, for instance if a hidden
465 // or fully transparent window is updating.
466 // in that case, we need to flip anyways to not risk a deadlock with
467 // h/w composer.
468
Mathias Agopiana44b0412011-10-16 18:46:35 -0700469 const DisplayHardware& hw(graphicPlane(0).displayHardware());
470 const nsecs_t now = systemTime();
471 mDebugInSwapBuffers = now;
472 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800473
474 size_t numLayers = mVisibleLayersSortedByZ.size();
Jesse Hallef194142012-06-14 14:45:17 -0700475 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
476 if (hwc.initCheck() == NO_ERROR) {
477 HWComposer::LayerListIterator cur = hwc.begin();
478 const HWComposer::LayerListIterator end = hwc.end();
479 for (size_t i = 0; cur != end && i < numLayers; ++i, ++cur) {
480 mVisibleLayersSortedByZ[i]->onLayerDisplayed(&*cur);
481 }
482 } else {
483 for (size_t i = 0; i < numLayers; i++) {
484 mVisibleLayersSortedByZ[i]->onLayerDisplayed(NULL);
485 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800486 }
487
Mathias Agopiana44b0412011-10-16 18:46:35 -0700488 mLastSwapBufferTime = systemTime() - now;
489 mDebugInSwapBuffers = 0;
490 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491}
492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
494{
Mathias Agopian841cde52012-03-01 15:44:37 -0800495 ATRACE_CALL();
496
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497 Mutex::Autolock _l(mStateLock);
498 const nsecs_t now = systemTime();
499 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500
Mathias Agopianca4d3602011-05-19 15:38:14 -0700501 // Here we're guaranteed that some transaction flags are set
502 // so we can call handleTransactionLocked() unconditionally.
503 // We call getTransactionFlags(), which will also clear the flags,
504 // with mStateLock held to guarantee that mCurrentState won't change
505 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700506
Mathias Agopianca4d3602011-05-19 15:38:14 -0700507 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
508 transactionFlags = getTransactionFlags(mask);
509 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700510
Mathias Agopianca4d3602011-05-19 15:38:14 -0700511 mLastTransactionTime = systemTime() - now;
512 mDebugInTransaction = 0;
513 invalidateHwcGeometry();
514 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700515}
516
Mathias Agopianca4d3602011-05-19 15:38:14 -0700517void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700518{
519 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 const size_t count = currentLayers.size();
521
522 /*
523 * Traversal of the children
524 * (perform the transaction for each of them if needed)
525 */
526
527 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
528 if (layersNeedTransaction) {
529 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700530 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
532 if (!trFlags) continue;
533
534 const uint32_t flags = layer->doTransaction(0);
535 if (flags & Layer::eVisibleRegion)
536 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 }
538 }
539
540 /*
541 * Perform our own transaction if needed
542 */
543
544 if (transactionFlags & eTransactionNeeded) {
545 if (mCurrentState.orientation != mDrawingState.orientation) {
546 // the orientation has changed, recompute all visible regions
547 // and invalidate everything.
548
549 const int dpy = 0;
550 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700551 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 GraphicPlane& plane(graphicPlane(dpy));
553 plane.setOrientation(orientation);
554
555 // update the shared control block
556 const DisplayHardware& hw(plane.displayHardware());
557 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
558 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800559 dcblk->w = plane.getWidth();
560 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561
562 mVisibleRegionsDirty = true;
563 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 }
565
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700566 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
567 // layers have been added
568 mVisibleRegionsDirty = true;
569 }
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 // some layers might have been removed, so
572 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700573 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700574 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700576 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700577 const size_t count = previousLayers.size();
578 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700579 const sp<LayerBase>& layer(previousLayers[i]);
580 if (currentLayers.indexOf( layer ) < 0) {
581 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700582 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700583 }
584 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 }
587
588 commitTransaction();
589}
590
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800592 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593{
Mathias Agopian841cde52012-03-01 15:44:37 -0800594 ATRACE_CALL();
595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 const GraphicPlane& plane(graphicPlane(0));
597 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700598 const DisplayHardware& hw(plane.displayHardware());
599 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600
601 Region aboveOpaqueLayers;
602 Region aboveCoveredLayers;
603 Region dirty;
604
605 bool secureFrameBuffer = false;
606
607 size_t i = currentLayers.size();
608 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700609 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 layer->validateVisibility(planeTransform);
611
612 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700613 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614
Mathias Agopianab028732010-03-16 16:41:46 -0700615 /*
616 * opaqueRegion: area of a surface that is fully opaque.
617 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700619
620 /*
621 * visibleRegion: area of a surface that is visible on screen
622 * and not fully transparent. This is essentially the layer's
623 * footprint minus the opaque regions above it.
624 * Areas covered by a translucent surface are considered visible.
625 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700627
628 /*
629 * coveredRegion: area of a surface that is covered by all
630 * visible regions above it (which includes the translucent areas).
631 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700633
634
635 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800636 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700637 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700638 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700640 visibleRegion.andSelf(screenRegion);
641 if (!visibleRegion.isEmpty()) {
642 // Remove the transparent area from the visible region
643 if (translucent) {
644 visibleRegion.subtractSelf(layer->transparentRegionScreen);
645 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
Mathias Agopianab028732010-03-16 16:41:46 -0700647 // compute the opaque region
648 const int32_t layerOrientation = layer->getOrientation();
649 if (s.alpha==255 && !translucent &&
650 ((layerOrientation & Transform::ROT_INVALID) == false)) {
651 // the opaque region is the layer's footprint
652 opaqueRegion = visibleRegion;
653 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 }
655 }
656
Mathias Agopianab028732010-03-16 16:41:46 -0700657 // Clip the covered region to the visible region
658 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
659
660 // Update aboveCoveredLayers for next (lower) layer
661 aboveCoveredLayers.orSelf(visibleRegion);
662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 // subtract the opaque region covered by the layers above us
664 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665
666 // compute this layer's dirty region
667 if (layer->contentDirty) {
668 // we need to invalidate the whole region
669 dirty = visibleRegion;
670 // as well, as the old visible region
671 dirty.orSelf(layer->visibleRegionScreen);
672 layer->contentDirty = false;
673 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700674 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700675 * the exposed region consists of two components:
676 * 1) what's VISIBLE now and was COVERED before
677 * 2) what's EXPOSED now less what was EXPOSED before
678 *
679 * note that (1) is conservative, we start with the whole
680 * visible region but only keep what used to be covered by
681 * something -- which mean it may have been exposed.
682 *
683 * (2) handles areas that were not covered by anything but got
684 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700685 */
Mathias Agopianab028732010-03-16 16:41:46 -0700686 const Region newExposed = visibleRegion - coveredRegion;
687 const Region oldVisibleRegion = layer->visibleRegionScreen;
688 const Region oldCoveredRegion = layer->coveredRegionScreen;
689 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
690 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 }
692 dirty.subtractSelf(aboveOpaqueLayers);
693
694 // accumulate to the screen dirty region
695 dirtyRegion.orSelf(dirty);
696
Mathias Agopianab028732010-03-16 16:41:46 -0700697 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 // Store the visible region is screen space
701 layer->setVisibleRegion(visibleRegion);
702 layer->setCoveredRegion(coveredRegion);
703
Mathias Agopian97011222009-07-28 10:57:27 -0700704 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 if (layer->isSecure() && !visibleRegion.isEmpty()) {
706 secureFrameBuffer = true;
707 }
708 }
709
Mathias Agopian97011222009-07-28 10:57:27 -0700710 // invalidate the areas where a layer was removed
711 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
712 mDirtyRegionRemovedLayer.clear();
713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 mSecureFrameBuffer = secureFrameBuffer;
715 opaqueRegion = aboveOpaqueLayers;
716}
717
718
719void SurfaceFlinger::commitTransaction()
720{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800721 if (!mLayersPendingRemoval.isEmpty()) {
722 // Notify removed layers now that they can't be drawn from
723 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
724 mLayersPendingRemoval[i]->onRemoved();
725 }
726 mLayersPendingRemoval.clear();
727 }
728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700730 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700731 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732}
733
734void SurfaceFlinger::handlePageFlip()
735{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800736 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800737 const DisplayHardware& hw = graphicPlane(0).displayHardware();
738 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800740 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
741 const bool visibleRegions = lockPageFlip(currentLayers);
742
743 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 Region opaqueRegion;
745 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700746
747 /*
748 * rebuild the visible layer list
749 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800750 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700751 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700752 mVisibleLayersSortedByZ.setCapacity(count);
753 for (size_t i=0 ; i<count ; i++) {
754 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
755 mVisibleLayersSortedByZ.add(currentLayers[i]);
756 }
757
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 mWormholeRegion = screenRegion.subtract(opaqueRegion);
759 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800760 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 }
762
763 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700764
765 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 mDirtyRegion.andSelf(screenRegion);
767}
768
Mathias Agopianad456f92011-01-13 17:53:01 -0800769void SurfaceFlinger::invalidateHwcGeometry()
770{
771 mHwWorkListDirty = true;
772}
773
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
775{
776 bool recomputeVisibleRegions = false;
777 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700778 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700780 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 layer->lockPageFlip(recomputeVisibleRegions);
782 }
783 return recomputeVisibleRegions;
784}
785
786void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
787{
788 const GraphicPlane& plane(graphicPlane(0));
789 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800790 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700791 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700793 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 layer->unlockPageFlip(planeTransform, mDirtyRegion);
795 }
796}
797
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800798void SurfaceFlinger::handleRefresh()
799{
800 bool needInvalidate = false;
801 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
802 const size_t count = currentLayers.size();
803 for (size_t i=0 ; i<count ; i++) {
804 const sp<LayerBase>& layer(currentLayers[i]);
805 if (layer->onPreComposition()) {
806 needInvalidate = true;
807 }
808 }
809 if (needInvalidate) {
810 signalLayerUpdate();
811 }
812}
813
814
Mathias Agopiana350ff92010-08-10 17:14:02 -0700815void SurfaceFlinger::handleWorkList()
816{
817 mHwWorkListDirty = false;
818 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
819 if (hwc.initCheck() == NO_ERROR) {
820 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
821 const size_t count = currentLayers.size();
822 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700823
824 HWComposer::LayerListIterator cur = hwc.begin();
825 const HWComposer::LayerListIterator end = hwc.end();
826 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
827 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700828 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700829 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700830 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700831 }
832 }
833}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835void SurfaceFlinger::handleRepaint()
836{
Mathias Agopian841cde52012-03-01 15:44:37 -0800837 ATRACE_CALL();
838
Mathias Agopianb8a55602009-06-26 19:06:36 -0700839 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700840 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841
Glenn Kasten99ed2242011-12-15 09:51:17 -0800842 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 debugFlashRegions();
844 }
845
Mathias Agopianb8a55602009-06-26 19:06:36 -0700846 // set the frame buffer
847 const DisplayHardware& hw(graphicPlane(0).displayHardware());
848 glMatrixMode(GL_MODELVIEW);
849 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850
851 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700852 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
854 // takes a rectangle, we must make sure to update that whole
855 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700856 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700857 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700858 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // We need to redraw the rectangle that will be updated
860 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700861 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700862 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700863 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700864 } else {
865 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700867 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868 }
869 }
870
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700871 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 composeSurfaces(mDirtyRegion);
873
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700874 // update the swap region and clear the dirty region
875 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 mDirtyRegion.clear();
877}
878
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700879void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700881 const DisplayHardware& hw(graphicPlane(0).displayHardware());
882 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700883
884 HWComposer::LayerListIterator cur = hwc.begin();
885 const HWComposer::LayerListIterator end = hwc.end();
886 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700887 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700888 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700889
Mathias Agopianf384cc32011-09-08 18:31:55 -0700890 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
891 size_t count = layers.size();
892
Steve Blocke6f43dd2012-01-06 19:20:56 +0000893 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700894 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
895 hwc.getNumLayers(), count);
896
897 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700898 if (hwc.initCheck() == NO_ERROR) {
899 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
900 }
Mathias Agopian45721772010-08-12 15:03:26 -0700901
902 /*
903 * update the per-frame h/w composer data for each layer
904 * and build the transparent region of the FB
905 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700906 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700907 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700908 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700909 }
910 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000911 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700912}
Mathias Agopian45721772010-08-12 15:03:26 -0700913
Mathias Agopianf384cc32011-09-08 18:31:55 -0700914void SurfaceFlinger::composeSurfaces(const Region& dirty)
915{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700916 const DisplayHardware& hw(graphicPlane(0).displayHardware());
917 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700918 HWComposer::LayerListIterator cur = hwc.begin();
919 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700920
921 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700922 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700923 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700924
Mathias Agopianb9494d52012-04-18 02:28:45 -0700925 if (hwc.getLayerCount(HWC_OVERLAY)) {
926 // when using overlays, we assume a fully transparent framebuffer
927 // NOTE: we could reduce how much we need to clear, for instance
928 // remove where there are opaque FB layers. however, on some
929 // GPUs doing a "clean slate" glClear might be more efficient.
930 // We'll revisit later if needed.
931 glClearColor(0, 0, 0, 0);
932 glClear(GL_COLOR_BUFFER_BIT);
933 } else {
934 // screen is already cleared here
935 if (!mWormholeRegion.isEmpty()) {
936 // can happen with SurfaceView
937 drawWormhole();
938 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700939 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700940
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700941 /*
942 * and then, render the layers targeted at the framebuffer
943 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700944
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700945 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
946 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700947 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700948 const sp<LayerBase>& layer(layers[i]);
949 const Region clip(dirty.intersect(layer->visibleRegionScreen));
950 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700951 if (cur->getCompositionType() == HWC_OVERLAY) {
952 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700953 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700954 // never clear the very first layer since we're
955 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700956 layer->clearWithOpenGL(clip);
957 }
958 continue;
959 }
960 // render the layer
961 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700962 }
963 }
964 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965}
966
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967void SurfaceFlinger::debugFlashRegions()
968{
Mathias Agopian0a917752010-06-14 21:20:00 -0700969 const DisplayHardware& hw(graphicPlane(0).displayHardware());
970 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700971 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700972 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700973 return;
974 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700975
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700976 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700977 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
978 mDirtyRegion.bounds() : hw.bounds());
979 composeSurfaces(repaint);
980 }
981
Mathias Agopianc492e672011-10-18 14:49:27 -0700982 glDisable(GL_TEXTURE_EXTERNAL_OES);
983 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985
Mathias Agopian0926f502009-05-04 14:17:04 -0700986 static int toggle = 0;
987 toggle = 1 - toggle;
988 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700989 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700990 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700991 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700992 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993
Mathias Agopian20f68782009-05-11 00:03:41 -0700994 Region::const_iterator it = mDirtyRegion.begin();
995 Region::const_iterator const end = mDirtyRegion.end();
996 while (it != end) {
997 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700999 { r.left, height - r.top },
1000 { r.left, height - r.bottom },
1001 { r.right, height - r.bottom },
1002 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 };
1004 glVertexPointer(2, GL_FLOAT, 0, vertices);
1005 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1006 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001007
Mathias Agopian0656a682011-09-20 17:22:44 -07001008 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009
1010 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001011 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012}
1013
1014void SurfaceFlinger::drawWormhole() const
1015{
1016 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1017 if (region.isEmpty())
1018 return;
1019
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001020 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001021 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001022 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001023 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001024
1025 GLfloat vertices[4][2];
1026 glVertexPointer(2, GL_FLOAT, 0, vertices);
1027 Region::const_iterator it = region.begin();
1028 Region::const_iterator const end = region.end();
1029 while (it != end) {
1030 const Rect& r = *it++;
1031 vertices[0][0] = r.left;
1032 vertices[0][1] = r.top;
1033 vertices[1][0] = r.right;
1034 vertices[1][1] = r.top;
1035 vertices[2][0] = r.right;
1036 vertices[2][1] = r.bottom;
1037 vertices[3][0] = r.left;
1038 vertices[3][1] = r.bottom;
1039 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1040 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041}
1042
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001043status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044{
1045 Mutex::Autolock _l(mStateLock);
1046 addLayer_l(layer);
1047 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1048 return NO_ERROR;
1049}
1050
Mathias Agopian96f08192010-06-02 23:28:45 -07001051status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1052{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001053 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001054 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1055}
1056
1057ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1058 const sp<LayerBaseClient>& lbc)
1059{
Mathias Agopian96f08192010-06-02 23:28:45 -07001060 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001061 size_t name = client->attachLayer(lbc);
1062
1063 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001064
1065 // add this layer to the current state list
1066 addLayer_l(lbc);
1067
Mathias Agopian4f113742011-05-03 16:21:41 -07001068 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001069}
1070
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
1073 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001074 status_t err = purgatorizeLayer_l(layer);
1075 if (err == NO_ERROR)
1076 setTransactionFlags(eTransactionNeeded);
1077 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078}
1079
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001080status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001082 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1083 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001084 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001085 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1087 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001088 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 return NO_ERROR;
1090 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001091 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092}
1093
Mathias Agopian9a112062009-04-17 19:36:26 -07001094status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1095{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001096 // First add the layer to the purgatory list, which makes sure it won't
1097 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001098 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001099 if (err >= 0) {
1100 mLayerPurgatory.add(layerBase);
1101 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001102
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001103 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001104
Mathias Agopian3d579642009-06-04 18:46:21 -07001105 // it's possible that we don't find a layer, because it might
1106 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001107 // from the user because there is a race between Client::destroySurface(),
1108 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001109 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1110}
1111
Mathias Agopian96f08192010-06-02 23:28:45 -07001112status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113{
Mathias Agopian96f08192010-06-02 23:28:45 -07001114 layer->forceVisibilityTransaction();
1115 setTransactionFlags(eTraversalNeeded);
1116 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117}
1118
Mathias Agopiandea20b12011-05-03 17:04:02 -07001119uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1120{
1121 return android_atomic_release_load(&mTransactionFlags);
1122}
1123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1125{
1126 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1127}
1128
Mathias Agopianbb641242010-05-18 17:06:55 -07001129uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130{
1131 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1132 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001133 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134 }
1135 return old;
1136}
1137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001139void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001140 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001141 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001142
Jamie Gennis28378392011-10-12 17:39:00 -07001143 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001144 if (mCurrentState.orientation != orientation) {
1145 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1146 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001147 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001148 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001149 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001150 orientation);
1151 }
1152 }
1153
Mathias Agopian698c0872011-06-28 19:09:31 -07001154 const size_t count = state.size();
1155 for (size_t i=0 ; i<count ; i++) {
1156 const ComposerState& s(state[i]);
1157 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001158 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001159 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001160
Mathias Agopian386aa982011-11-07 21:58:03 -08001161 if (transactionFlags) {
1162 // this triggers the transaction
1163 setTransactionFlags(transactionFlags);
1164
1165 // if this is a synchronous transaction, wait for it to take effect
1166 // before returning.
1167 if (flags & eSynchronous) {
1168 mTransationPending = true;
1169 }
1170 while (mTransationPending) {
1171 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1172 if (CC_UNLIKELY(err != NO_ERROR)) {
1173 // just in case something goes wrong in SF, return to the
1174 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001175 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001176 mTransationPending = false;
1177 break;
1178 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001179 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 }
1181}
1182
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001183sp<ISurface> SurfaceFlinger::createSurface(
1184 ISurfaceComposerClient::surface_data_t* params,
1185 const String8& name,
1186 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1188 uint32_t flags)
1189{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001190 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001191 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001192
1193 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001194 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001195 int(w), int(h));
1196 return surfaceHandle;
1197 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001198
Mathias Agopianff615cc2012-02-24 14:58:36 -08001199 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001200 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 switch (flags & eFXSurfaceMask) {
1202 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001203 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1204 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 break;
1206 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001207 // for now we treat Blur as Dim, until we can implement it
1208 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001212 case eFXSurfaceScreenshot:
1213 layer = createScreenshotSurface(client, d, w, h, flags);
1214 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 }
1216
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001217 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001218 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001219 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001220 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001223 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001224 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001225 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001226 if (normalLayer != 0) {
1227 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001229 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001230 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001231
Mathias Agopian96f08192010-06-02 23:28:45 -07001232 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 }
1234
1235 return surfaceHandle;
1236}
1237
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001238sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001239 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001240 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001241 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242{
1243 // initialize the surfaces
1244 switch (format) { // TODO: take h/w into account
1245 case PIXEL_FORMAT_TRANSPARENT:
1246 case PIXEL_FORMAT_TRANSLUCENT:
1247 format = PIXEL_FORMAT_RGBA_8888;
1248 break;
1249 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001250#ifdef NO_RGBX_8888
1251 format = PIXEL_FORMAT_RGB_565;
1252#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001253 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001254#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 break;
1256 }
1257
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001258#ifdef NO_RGBX_8888
1259 if (format == PIXEL_FORMAT_RGBX_8888)
1260 format = PIXEL_FORMAT_RGBA_8888;
1261#endif
1262
Mathias Agopian96f08192010-06-02 23:28:45 -07001263 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001264 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001265 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001266 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001267 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 }
1269 return layer;
1270}
1271
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001272sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001273 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001274 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275{
Mathias Agopian96f08192010-06-02 23:28:45 -07001276 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001277 return layer;
1278}
1279
1280sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1281 const sp<Client>& client, DisplayID display,
1282 uint32_t w, uint32_t h, uint32_t flags)
1283{
1284 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 return layer;
1286}
1287
Mathias Agopian96f08192010-06-02 23:28:45 -07001288status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289{
Mathias Agopian9a112062009-04-17 19:36:26 -07001290 /*
1291 * called by the window manager, when a surface should be marked for
1292 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001293 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001294 * The surface is removed from the current and drawing lists, but placed
1295 * in the purgatory queue, so it's not destroyed right-away (we need
1296 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001297 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001298
Mathias Agopian48d819a2009-09-10 19:41:18 -07001299 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001300 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001301 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001302
Mathias Agopian48d819a2009-09-10 19:41:18 -07001303 if (layer != 0) {
1304 err = purgatorizeLayer_l(layer);
1305 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001306 setTransactionFlags(eTransactionNeeded);
1307 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001308 }
1309 return err;
1310}
1311
Mathias Agopianca4d3602011-05-19 15:38:14 -07001312status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001313{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001314 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001315 status_t err = NO_ERROR;
1316 sp<LayerBaseClient> l(layer.promote());
1317 if (l != NULL) {
1318 Mutex::Autolock _l(mStateLock);
1319 err = removeLayer_l(l);
1320 if (err == NAME_NOT_FOUND) {
1321 // The surface wasn't in the current list, which means it was
1322 // removed already, which means it is in the purgatory,
1323 // and need to be removed from there.
1324 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001325 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001326 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001327 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001328 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001329 "error removing layer=%p (%s)", l.get(), strerror(-err));
1330 }
1331 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332}
1333
Mathias Agopian698c0872011-06-28 19:09:31 -07001334uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001335 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001336 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001339 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1340 if (layer != 0) {
1341 const uint32_t what = s.what;
1342 if (what & ePositionChanged) {
1343 if (layer->setPosition(s.x, s.y))
1344 flags |= eTraversalNeeded;
1345 }
1346 if (what & eLayerChanged) {
1347 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1348 if (layer->setLayer(s.z)) {
1349 mCurrentState.layersSortedByZ.removeAt(idx);
1350 mCurrentState.layersSortedByZ.add(layer);
1351 // we need traversal (state changed)
1352 // AND transaction (list changed)
1353 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 }
1355 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001356 if (what & eSizeChanged) {
1357 if (layer->setSize(s.w, s.h)) {
1358 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001359 }
1360 }
1361 if (what & eAlphaChanged) {
1362 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1363 flags |= eTraversalNeeded;
1364 }
1365 if (what & eMatrixChanged) {
1366 if (layer->setMatrix(s.matrix))
1367 flags |= eTraversalNeeded;
1368 }
1369 if (what & eTransparentRegionChanged) {
1370 if (layer->setTransparentRegionHint(s.transparentRegion))
1371 flags |= eTraversalNeeded;
1372 }
1373 if (what & eVisibilityChanged) {
1374 if (layer->setFlags(s.flags, s.mask))
1375 flags |= eTraversalNeeded;
1376 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001377 if (what & eCropChanged) {
1378 if (layer->setCrop(s.crop))
1379 flags |= eTraversalNeeded;
1380 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001382 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383}
1384
Mathias Agopianb60314a2012-04-10 22:09:54 -07001385// ---------------------------------------------------------------------------
1386
1387void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001388 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001389 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1390 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001391 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001392 // this is a temporary work-around, eventually this should be called
1393 // by the power-manager
1394 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001395 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001396 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397}
1398
Mathias Agopianb60314a2012-04-10 22:09:54 -07001399void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001400 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001401 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1402 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001403 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001404 hw.releaseScreen();
1405 // from this point on, SF will stop drawing
1406 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407}
1408
Colin Cross8e533062012-06-07 13:17:52 -07001409void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001410 class MessageScreenAcquired : public MessageBase {
1411 SurfaceFlinger* flinger;
1412 public:
1413 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1414 virtual bool handler() {
1415 flinger->onScreenAcquired();
1416 return true;
1417 }
1418 };
1419 sp<MessageBase> msg = new MessageScreenAcquired(this);
1420 postMessageSync(msg);
1421}
1422
Colin Cross8e533062012-06-07 13:17:52 -07001423void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001424 class MessageScreenReleased : public MessageBase {
1425 SurfaceFlinger* flinger;
1426 public:
1427 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1428 virtual bool handler() {
1429 flinger->onScreenReleased();
1430 return true;
1431 }
1432 };
1433 sp<MessageBase> msg = new MessageScreenReleased(this);
1434 postMessageSync(msg);
1435}
1436
1437// ---------------------------------------------------------------------------
1438
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1440{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001441 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 char buffer[SIZE];
1443 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001444
1445 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446 snprintf(buffer, SIZE, "Permission Denial: "
1447 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1448 IPCThreadState::self()->getCallingPid(),
1449 IPCThreadState::self()->getCallingUid());
1450 result.append(buffer);
1451 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001452 // Try to get the main lock, but don't insist if we can't
1453 // (this would indicate SF is stuck, but we want to be able to
1454 // print something in dumpsys).
1455 int retry = 3;
1456 while (mStateLock.tryLock()<0 && --retry>=0) {
1457 usleep(1000000);
1458 }
1459 const bool locked(retry >= 0);
1460 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001461 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001462 "SurfaceFlinger appears to be unresponsive, "
1463 "dumping anyways (no locks held)\n");
1464 result.append(buffer);
1465 }
1466
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001467 bool dumpAll = true;
1468 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001469 size_t numArgs = args.size();
1470 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001471 if ((index < numArgs) &&
1472 (args[index] == String16("--list"))) {
1473 index++;
1474 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001475 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001476 }
1477
1478 if ((index < numArgs) &&
1479 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001480 index++;
1481 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001482 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001483 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001484
1485 if ((index < numArgs) &&
1486 (args[index] == String16("--latency-clear"))) {
1487 index++;
1488 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001489 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001490 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001492
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001493 if (dumpAll) {
1494 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001495 }
1496
Mathias Agopian9795c422009-08-26 16:36:26 -07001497 if (locked) {
1498 mStateLock.unlock();
1499 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 }
1501 write(fd, result.string(), result.size());
1502 return NO_ERROR;
1503}
1504
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001505void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1506 String8& result, char* buffer, size_t SIZE) const
1507{
1508 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1509 const size_t count = currentLayers.size();
1510 for (size_t i=0 ; i<count ; i++) {
1511 const sp<LayerBase>& layer(currentLayers[i]);
1512 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1513 result.append(buffer);
1514 }
1515}
1516
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001517void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1518 String8& result, char* buffer, size_t SIZE) const
1519{
1520 String8 name;
1521 if (index < args.size()) {
1522 name = String8(args[index]);
1523 index++;
1524 }
1525
1526 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1527 const size_t count = currentLayers.size();
1528 for (size_t i=0 ; i<count ; i++) {
1529 const sp<LayerBase>& layer(currentLayers[i]);
1530 if (name.isEmpty()) {
1531 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1532 result.append(buffer);
1533 }
1534 if (name.isEmpty() || (name == layer->getName())) {
1535 layer->dumpStats(result, buffer, SIZE);
1536 }
1537 }
1538}
1539
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001540void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1541 String8& result, char* buffer, size_t SIZE) const
1542{
1543 String8 name;
1544 if (index < args.size()) {
1545 name = String8(args[index]);
1546 index++;
1547 }
1548
1549 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1550 const size_t count = currentLayers.size();
1551 for (size_t i=0 ; i<count ; i++) {
1552 const sp<LayerBase>& layer(currentLayers[i]);
1553 if (name.isEmpty() || (name == layer->getName())) {
1554 layer->clearStats();
1555 }
1556 }
1557}
1558
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001559void SurfaceFlinger::dumpAllLocked(
1560 String8& result, char* buffer, size_t SIZE) const
1561{
1562 // figure out if we're stuck somewhere
1563 const nsecs_t now = systemTime();
1564 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1565 const nsecs_t inTransaction(mDebugInTransaction);
1566 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1567 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1568
1569 /*
1570 * Dump the visible layer list
1571 */
1572 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1573 const size_t count = currentLayers.size();
1574 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1575 result.append(buffer);
1576 for (size_t i=0 ; i<count ; i++) {
1577 const sp<LayerBase>& layer(currentLayers[i]);
1578 layer->dump(result, buffer, SIZE);
1579 }
1580
1581 /*
1582 * Dump the layers in the purgatory
1583 */
1584
1585 const size_t purgatorySize = mLayerPurgatory.size();
1586 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1587 result.append(buffer);
1588 for (size_t i=0 ; i<purgatorySize ; i++) {
1589 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1590 layer->shortDump(result, buffer, SIZE);
1591 }
1592
1593 /*
1594 * Dump SurfaceFlinger global state
1595 */
1596
1597 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1598 result.append(buffer);
1599
1600 const GLExtensions& extensions(GLExtensions::getInstance());
1601 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1602 extensions.getVendor(),
1603 extensions.getRenderer(),
1604 extensions.getVersion());
1605 result.append(buffer);
1606
1607 snprintf(buffer, SIZE, "EGL : %s\n",
1608 eglQueryString(graphicPlane(0).getEGLDisplay(),
1609 EGL_VERSION_HW_ANDROID));
1610 result.append(buffer);
1611
1612 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1613 result.append(buffer);
1614
1615 mWormholeRegion.dump(result, "WormholeRegion");
1616 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1617 snprintf(buffer, SIZE,
1618 " orientation=%d, canDraw=%d\n",
1619 mCurrentState.orientation, hw.canDraw());
1620 result.append(buffer);
1621 snprintf(buffer, SIZE,
1622 " last eglSwapBuffers() time: %f us\n"
1623 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001624 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001625 " refresh-rate : %f fps\n"
1626 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001627 " y-dpi : %f\n"
1628 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001629 mLastSwapBufferTime/1000.0,
1630 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001631 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001632 hw.getRefreshRate(),
1633 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001634 hw.getDpiY(),
1635 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001636 result.append(buffer);
1637
1638 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1639 inSwapBuffersDuration/1000.0);
1640 result.append(buffer);
1641
1642 snprintf(buffer, SIZE, " transaction time: %f us\n",
1643 inTransactionDuration/1000.0);
1644 result.append(buffer);
1645
1646 /*
1647 * VSYNC state
1648 */
1649 mEventThread->dump(result, buffer, SIZE);
1650
1651 /*
1652 * Dump HWComposer state
1653 */
1654 HWComposer& hwc(hw.getHwComposer());
1655 snprintf(buffer, SIZE, "h/w composer state:\n");
1656 result.append(buffer);
1657 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1658 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1659 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1660 result.append(buffer);
1661 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1662
1663 /*
1664 * Dump gralloc state
1665 */
1666 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1667 alloc.dump(result);
1668 hw.dump(result);
1669}
1670
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671status_t SurfaceFlinger::onTransact(
1672 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1673{
1674 switch (code) {
1675 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001676 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001679 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001680 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001681 case BLANK:
1682 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 {
1684 // codes that require permission check
1685 IPCThreadState* ipc = IPCThreadState::self();
1686 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001687 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001688 if ((uid != AID_GRAPHICS) &&
1689 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001690 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001691 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1692 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001694 break;
1695 }
1696 case CAPTURE_SCREEN:
1697 {
1698 // codes that require permission check
1699 IPCThreadState* ipc = IPCThreadState::self();
1700 const int pid = ipc->getCallingPid();
1701 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001702 if ((uid != AID_GRAPHICS) &&
1703 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001704 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001705 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1706 return PERMISSION_DENIED;
1707 }
1708 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 }
1710 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1713 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001714 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001715 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001716 IPCThreadState* ipc = IPCThreadState::self();
1717 const int pid = ipc->getCallingPid();
1718 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001719 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001720 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 return PERMISSION_DENIED;
1722 }
1723 int n;
1724 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001725 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001726 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 return NO_ERROR;
1728 case 1002: // SHOW_UPDATES
1729 n = data.readInt32();
1730 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001731 invalidateHwcGeometry();
1732 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001735 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001736 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001738 case 1005:{ // force transaction
1739 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1740 return NO_ERROR;
1741 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001742 case 1006:{ // send empty update
1743 signalRefresh();
1744 return NO_ERROR;
1745 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001746 case 1008: // toggle use of hw composer
1747 n = data.readInt32();
1748 mDebugDisableHWC = n ? 1 : 0;
1749 invalidateHwcGeometry();
1750 repaintEverything();
1751 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001752 case 1009: // toggle use of transform hint
1753 n = data.readInt32();
1754 mDebugDisableTransformHint = n ? 1 : 0;
1755 invalidateHwcGeometry();
1756 repaintEverything();
1757 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001759 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 reply->writeInt32(0);
1761 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001762 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001763 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764 return NO_ERROR;
1765 case 1013: {
1766 Mutex::Autolock _l(mStateLock);
1767 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1768 reply->writeInt32(hw.getPageFlipCount());
1769 }
1770 return NO_ERROR;
1771 }
1772 }
1773 return err;
1774}
1775
Mathias Agopian53331da2011-08-22 21:44:41 -07001776void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001777 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001778 const Rect bounds(hw.getBounds());
1779 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001780 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001781}
1782
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001783void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1784 Mutex::Autolock _l(mInvalidateLock);
1785 mInvalidateRegion = reg;
1786}
1787
1788Region SurfaceFlinger::getAndClearInvalidateRegion() {
1789 Mutex::Autolock _l(mInvalidateLock);
1790 Region reg(mInvalidateRegion);
1791 mInvalidateRegion.clear();
1792 return reg;
1793}
1794
Mathias Agopian59119e62010-10-11 12:37:43 -07001795// ---------------------------------------------------------------------------
1796
Mathias Agopian118d0242011-10-13 16:02:48 -07001797status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1798 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1799{
1800 Mutex::Autolock _l(mStateLock);
1801 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1802}
1803
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001804status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1805 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001806{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001807 ATRACE_CALL();
1808
Mathias Agopian59119e62010-10-11 12:37:43 -07001809 if (!GLExtensions::getInstance().haveFramebufferObject())
1810 return INVALID_OPERATION;
1811
1812 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001813 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001814 const uint32_t hw_w = hw.getWidth();
1815 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001816 GLfloat u = 1;
1817 GLfloat v = 1;
1818
1819 // make sure to clear all GL error flags
1820 while ( glGetError() != GL_NO_ERROR ) ;
1821
1822 // create a FBO
1823 GLuint name, tname;
1824 glGenTextures(1, &tname);
1825 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001826 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1827 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001828 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1829 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001830 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001831 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001832 GLint tw = (2 << (31 - clz(hw_w)));
1833 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001834 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1835 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001836 u = GLfloat(hw_w) / tw;
1837 v = GLfloat(hw_h) / th;
1838 }
1839 glGenFramebuffersOES(1, &name);
1840 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001841 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1842 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001843
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001844 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001845 glDisable(GL_TEXTURE_EXTERNAL_OES);
1846 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001847 glClearColor(0,0,0,1);
1848 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001849 glMatrixMode(GL_MODELVIEW);
1850 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001851 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1852 const size_t count = layers.size();
1853 for (size_t i=0 ; i<count ; ++i) {
1854 const sp<LayerBase>& layer(layers[i]);
1855 layer->drawForSreenShot();
1856 }
1857
Mathias Agopian118d0242011-10-13 16:02:48 -07001858 hw.compositionComplete();
1859
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001860 // back to main framebuffer
1861 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001862 glDeleteFramebuffersOES(1, &name);
1863
1864 *textureName = tname;
1865 *uOut = u;
1866 *vOut = v;
1867 return NO_ERROR;
1868}
1869
1870// ---------------------------------------------------------------------------
1871
Mathias Agopianed9807b2012-05-18 14:18:08 -07001872class VSyncWaiter {
1873 DisplayEventReceiver::Event buffer[4];
1874 sp<Looper> looper;
1875 sp<IDisplayEventConnection> events;
1876 sp<BitTube> eventTube;
1877public:
1878 VSyncWaiter(const sp<EventThread>& eventThread) {
1879 looper = new Looper(true);
1880 events = eventThread->createEventConnection();
1881 eventTube = events->getDataChannel();
1882 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1883 events->requestNextVsync();
1884 }
1885
1886 void wait() {
1887 ssize_t n;
1888
1889 looper->pollOnce(-1);
1890 // we don't handle any errors here, it doesn't matter
1891 // and we don't want to take the risk to get stuck.
1892
1893 // drain the events...
1894 while ((n = DisplayEventReceiver::getEvents(
1895 eventTube, buffer, 4)) > 0) ;
1896
1897 events->requestNextVsync();
1898 }
1899};
1900
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001901status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1902{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001903 // get screen geometry
1904 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1905 const uint32_t hw_w = hw.getWidth();
1906 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001907 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001908
1909 GLfloat u, v;
1910 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001911 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001912 if (result != NO_ERROR) {
1913 return result;
1914 }
1915
1916 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001917 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001918 glBindTexture(GL_TEXTURE_2D, tname);
1919 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1920 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1921 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001922 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1923 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001924 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1925 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1926 glVertexPointer(2, GL_FLOAT, 0, vtx);
1927
Mathias Agopianffcf4652011-07-07 17:30:31 -07001928 /*
1929 * Texture coordinate mapping
1930 *
1931 * u
1932 * 1 +----------+---+
1933 * | | | | image is inverted
1934 * | V | | w.r.t. the texture
1935 * 1-v +----------+ | coordinates
1936 * | |
1937 * | |
1938 * | |
1939 * 0 +--------------+
1940 * 0 1
1941 *
1942 */
1943
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001944 class s_curve_interpolator {
1945 const float nbFrames, s, v;
1946 public:
1947 s_curve_interpolator(int nbFrames, float s)
1948 : nbFrames(1.0f / (nbFrames-1)), s(s),
1949 v(1.0f + expf(-s + 0.5f*s)) {
1950 }
1951 float operator()(int f) {
1952 const float x = f * nbFrames;
1953 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1954 }
1955 };
1956
1957 class v_stretch {
1958 const GLfloat hw_w, hw_h;
1959 public:
1960 v_stretch(uint32_t hw_w, uint32_t hw_h)
1961 : hw_w(hw_w), hw_h(hw_h) {
1962 }
1963 void operator()(GLfloat* vtx, float v) {
1964 const GLfloat w = hw_w + (hw_w * v);
1965 const GLfloat h = hw_h - (hw_h * v);
1966 const GLfloat x = (hw_w - w) * 0.5f;
1967 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001968 vtx[0] = x; vtx[1] = y;
1969 vtx[2] = x; vtx[3] = y + h;
1970 vtx[4] = x + w; vtx[5] = y + h;
1971 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 }
1973 };
1974
1975 class h_stretch {
1976 const GLfloat hw_w, hw_h;
1977 public:
1978 h_stretch(uint32_t hw_w, uint32_t hw_h)
1979 : hw_w(hw_w), hw_h(hw_h) {
1980 }
1981 void operator()(GLfloat* vtx, float v) {
1982 const GLfloat w = hw_w - (hw_w * v);
1983 const GLfloat h = 1.0f;
1984 const GLfloat x = (hw_w - w) * 0.5f;
1985 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001986 vtx[0] = x; vtx[1] = y;
1987 vtx[2] = x; vtx[3] = y + h;
1988 vtx[4] = x + w; vtx[5] = y + h;
1989 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 }
1991 };
1992
Mathias Agopianed9807b2012-05-18 14:18:08 -07001993 VSyncWaiter vsync(mEventThread);
1994
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001995 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001996 char value[PROPERTY_VALUE_MAX];
1997 property_get("debug.sf.electron_frames", value, "24");
1998 int nbFrames = (atoi(value) + 1) >> 1;
1999 if (nbFrames <= 0) // just in case
2000 nbFrames = 24;
2001
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 s_curve_interpolator itr(nbFrames, 7.5f);
2003 s_curve_interpolator itg(nbFrames, 8.0f);
2004 s_curve_interpolator itb(nbFrames, 8.5f);
2005
2006 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002007
2008 glMatrixMode(GL_TEXTURE);
2009 glLoadIdentity();
2010 glMatrixMode(GL_MODELVIEW);
2011 glLoadIdentity();
2012
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002013 glEnable(GL_BLEND);
2014 glBlendFunc(GL_ONE, GL_ONE);
2015 for (int i=0 ; i<nbFrames ; i++) {
2016 float x, y, w, h;
2017 const float vr = itr(i);
2018 const float vg = itg(i);
2019 const float vb = itb(i);
2020
Mathias Agopianed9807b2012-05-18 14:18:08 -07002021 // wait for vsync
2022 vsync.wait();
2023
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002024 // clear screen
2025 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002026 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002027 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002028
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002029 // draw the red plane
2030 vverts(vtx, vr);
2031 glColorMask(1,0,0,1);
2032 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002033
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034 // draw the green plane
2035 vverts(vtx, vg);
2036 glColorMask(0,1,0,1);
2037 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002038
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 // draw the blue plane
2040 vverts(vtx, vb);
2041 glColorMask(0,0,1,1);
2042 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002043
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002045 glDisable(GL_TEXTURE_2D);
2046 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 glColor4f(vg, vg, vg, 1);
2048 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2049 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002050 }
2051
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002052 h_stretch hverts(hw_w, hw_h);
2053 glDisable(GL_BLEND);
2054 glDisable(GL_TEXTURE_2D);
2055 glColorMask(1,1,1,1);
2056 for (int i=0 ; i<nbFrames ; i++) {
2057 const float v = itg(i);
2058 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002059
2060 // wait for vsync
2061 vsync.wait();
2062
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002063 glClear(GL_COLOR_BUFFER_BIT);
2064 glColor4f(1-v, 1-v, 1-v, 1);
2065 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2066 hw.flip(screenBounds);
2067 }
2068
2069 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2071 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002072 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002073 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002074 return NO_ERROR;
2075}
2076
2077status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2078{
2079 status_t result = PERMISSION_DENIED;
2080
2081 if (!GLExtensions::getInstance().haveFramebufferObject())
2082 return INVALID_OPERATION;
2083
2084
2085 // get screen geometry
2086 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2087 const uint32_t hw_w = hw.getWidth();
2088 const uint32_t hw_h = hw.getHeight();
2089 const Region screenBounds(hw.bounds());
2090
2091 GLfloat u, v;
2092 GLuint tname;
2093 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2094 if (result != NO_ERROR) {
2095 return result;
2096 }
2097
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098 GLfloat vtx[8];
2099 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002100 glBindTexture(GL_TEXTURE_2D, tname);
2101 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2102 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2103 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002104 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2105 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2107 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2108 glVertexPointer(2, GL_FLOAT, 0, vtx);
2109
2110 class s_curve_interpolator {
2111 const float nbFrames, s, v;
2112 public:
2113 s_curve_interpolator(int nbFrames, float s)
2114 : nbFrames(1.0f / (nbFrames-1)), s(s),
2115 v(1.0f + expf(-s + 0.5f*s)) {
2116 }
2117 float operator()(int f) {
2118 const float x = f * nbFrames;
2119 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2120 }
2121 };
2122
2123 class v_stretch {
2124 const GLfloat hw_w, hw_h;
2125 public:
2126 v_stretch(uint32_t hw_w, uint32_t hw_h)
2127 : hw_w(hw_w), hw_h(hw_h) {
2128 }
2129 void operator()(GLfloat* vtx, float v) {
2130 const GLfloat w = hw_w + (hw_w * v);
2131 const GLfloat h = hw_h - (hw_h * v);
2132 const GLfloat x = (hw_w - w) * 0.5f;
2133 const GLfloat y = (hw_h - h) * 0.5f;
2134 vtx[0] = x; vtx[1] = y;
2135 vtx[2] = x; vtx[3] = y + h;
2136 vtx[4] = x + w; vtx[5] = y + h;
2137 vtx[6] = x + w; vtx[7] = y;
2138 }
2139 };
2140
2141 class h_stretch {
2142 const GLfloat hw_w, hw_h;
2143 public:
2144 h_stretch(uint32_t hw_w, uint32_t hw_h)
2145 : hw_w(hw_w), hw_h(hw_h) {
2146 }
2147 void operator()(GLfloat* vtx, float v) {
2148 const GLfloat w = hw_w - (hw_w * v);
2149 const GLfloat h = 1.0f;
2150 const GLfloat x = (hw_w - w) * 0.5f;
2151 const GLfloat y = (hw_h - h) * 0.5f;
2152 vtx[0] = x; vtx[1] = y;
2153 vtx[2] = x; vtx[3] = y + h;
2154 vtx[4] = x + w; vtx[5] = y + h;
2155 vtx[6] = x + w; vtx[7] = y;
2156 }
2157 };
2158
Mathias Agopianed9807b2012-05-18 14:18:08 -07002159 VSyncWaiter vsync(mEventThread);
2160
Mathias Agopiana6546e52010-10-14 12:33:07 -07002161 // the full animation is 12 frames
2162 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002163 s_curve_interpolator itr(nbFrames, 7.5f);
2164 s_curve_interpolator itg(nbFrames, 8.0f);
2165 s_curve_interpolator itb(nbFrames, 8.5f);
2166
2167 h_stretch hverts(hw_w, hw_h);
2168 glDisable(GL_BLEND);
2169 glDisable(GL_TEXTURE_2D);
2170 glColorMask(1,1,1,1);
2171 for (int i=nbFrames-1 ; i>=0 ; i--) {
2172 const float v = itg(i);
2173 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002174
2175 // wait for vsync
2176 vsync.wait();
2177
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002178 glClear(GL_COLOR_BUFFER_BIT);
2179 glColor4f(1-v, 1-v, 1-v, 1);
2180 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2181 hw.flip(screenBounds);
2182 }
2183
Mathias Agopiana6546e52010-10-14 12:33:07 -07002184 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002185 v_stretch vverts(hw_w, hw_h);
2186 glEnable(GL_BLEND);
2187 glBlendFunc(GL_ONE, GL_ONE);
2188 for (int i=nbFrames-1 ; i>=0 ; i--) {
2189 float x, y, w, h;
2190 const float vr = itr(i);
2191 const float vg = itg(i);
2192 const float vb = itb(i);
2193
Mathias Agopianed9807b2012-05-18 14:18:08 -07002194 // wait for vsync
2195 vsync.wait();
2196
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002197 // clear screen
2198 glColorMask(1,1,1,1);
2199 glClear(GL_COLOR_BUFFER_BIT);
2200 glEnable(GL_TEXTURE_2D);
2201
2202 // draw the red plane
2203 vverts(vtx, vr);
2204 glColorMask(1,0,0,1);
2205 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2206
2207 // draw the green plane
2208 vverts(vtx, vg);
2209 glColorMask(0,1,0,1);
2210 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2211
2212 // draw the blue plane
2213 vverts(vtx, vb);
2214 glColorMask(0,0,1,1);
2215 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2216
2217 hw.flip(screenBounds);
2218 }
2219
2220 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002221 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002222 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002223 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002224 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002225
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226 return NO_ERROR;
2227}
2228
2229// ---------------------------------------------------------------------------
2230
Mathias Agopianabd671a2010-10-14 14:54:06 -07002231status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002232{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002233 ATRACE_CALL();
2234
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002235 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2236 if (!hw.canDraw()) {
2237 // we're already off
2238 return NO_ERROR;
2239 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002240
2241 // turn off hwc while we're doing the animation
2242 hw.getHwComposer().disable();
2243 // and make sure to turn it back on (if needed) next time we compose
2244 invalidateHwcGeometry();
2245
Mathias Agopianabd671a2010-10-14 14:54:06 -07002246 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2247 electronBeamOffAnimationImplLocked();
2248 }
2249
2250 // always clear the whole screen at the end of the animation
2251 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002252 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002253 hw.flip( Region(hw.bounds()) );
2254
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002255 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002256}
2257
2258status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2259{
Mathias Agopian59119e62010-10-11 12:37:43 -07002260 class MessageTurnElectronBeamOff : public MessageBase {
2261 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002262 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002263 status_t result;
2264 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002265 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2266 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002267 }
2268 status_t getResult() const {
2269 return result;
2270 }
2271 virtual bool handler() {
2272 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002273 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002274 return true;
2275 }
2276 };
2277
Mathias Agopianabd671a2010-10-14 14:54:06 -07002278 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002279 status_t res = postMessageSync(msg);
2280 if (res == NO_ERROR) {
2281 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002282
2283 // work-around: when the power-manager calls us we activate the
2284 // animation. eventually, the "on" animation will be called
2285 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002286 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002287 }
2288 return res;
2289}
2290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002291// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002292
Mathias Agopianabd671a2010-10-14 14:54:06 -07002293status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002294{
2295 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2296 if (hw.canDraw()) {
2297 // we're already on
2298 return NO_ERROR;
2299 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2301 electronBeamOnAnimationImplLocked();
2302 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002303
2304 // make sure to redraw the whole screen when the animation is done
2305 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002306 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002307
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002308 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002309}
2310
2311status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2312{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002313 class MessageTurnElectronBeamOn : public MessageBase {
2314 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002315 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002316 status_t result;
2317 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002318 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2319 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002320 }
2321 status_t getResult() const {
2322 return result;
2323 }
2324 virtual bool handler() {
2325 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002326 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002327 return true;
2328 }
2329 };
2330
Mathias Agopianabd671a2010-10-14 14:54:06 -07002331 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002332 return NO_ERROR;
2333}
2334
2335// ---------------------------------------------------------------------------
2336
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2338 sp<IMemoryHeap>* heap,
2339 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002340 uint32_t sw, uint32_t sh,
2341 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002342{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002343 ATRACE_CALL();
2344
Mathias Agopian74c40c02010-09-29 13:02:36 -07002345 status_t result = PERMISSION_DENIED;
2346
2347 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002348 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 return BAD_VALUE;
2350
2351 if (!GLExtensions::getInstance().haveFramebufferObject())
2352 return INVALID_OPERATION;
2353
2354 // get screen geometry
2355 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2356 const uint32_t hw_w = hw.getWidth();
2357 const uint32_t hw_h = hw.getHeight();
2358
2359 if ((sw > hw_w) || (sh > hw_h))
2360 return BAD_VALUE;
2361
2362 sw = (!sw) ? hw_w : sw;
2363 sh = (!sh) ? hw_h : sh;
2364 const size_t size = sw * sh * 4;
2365
Steve Block9d453682011-12-20 16:23:08 +00002366 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002367 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002368
Mathias Agopian74c40c02010-09-29 13:02:36 -07002369 // make sure to clear all GL error flags
2370 while ( glGetError() != GL_NO_ERROR ) ;
2371
2372 // create a FBO
2373 GLuint name, tname;
2374 glGenRenderbuffersOES(1, &tname);
2375 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2376 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002377
Mathias Agopian74c40c02010-09-29 13:02:36 -07002378 glGenFramebuffersOES(1, &name);
2379 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2380 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2381 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2382
2383 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002384
Mathias Agopian74c40c02010-09-29 13:02:36 -07002385 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2386
2387 // invert everything, b/c glReadPixel() below will invert the FB
2388 glViewport(0, 0, sw, sh);
2389 glMatrixMode(GL_PROJECTION);
2390 glPushMatrix();
2391 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002392 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002393 glMatrixMode(GL_MODELVIEW);
2394
2395 // redraw the screen entirely...
2396 glClearColor(0,0,0,1);
2397 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002398
Jamie Gennis9575f602011-10-07 14:51:16 -07002399 const LayerVector& layers(mDrawingState.layersSortedByZ);
2400 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 for (size_t i=0 ; i<count ; ++i) {
2402 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002403 const uint32_t flags = layer->drawingState().flags;
2404 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2405 const uint32_t z = layer->drawingState().z;
2406 if (z >= minLayerZ && z <= maxLayerZ) {
2407 layer->drawForSreenShot();
2408 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002409 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002410 }
2411
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 // check for errors and return screen capture
2413 if (glGetError() != GL_NO_ERROR) {
2414 // error while rendering
2415 result = INVALID_OPERATION;
2416 } else {
2417 // allocate shared memory large enough to hold the
2418 // screen capture
2419 sp<MemoryHeapBase> base(
2420 new MemoryHeapBase(size, 0, "screen-capture") );
2421 void* const ptr = base->getBase();
2422 if (ptr) {
2423 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002424 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002425 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2426 if (glGetError() == GL_NO_ERROR) {
2427 *heap = base;
2428 *w = sw;
2429 *h = sh;
2430 *f = PIXEL_FORMAT_RGBA_8888;
2431 result = NO_ERROR;
2432 }
2433 } else {
2434 result = NO_MEMORY;
2435 }
2436 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002437 glViewport(0, 0, hw_w, hw_h);
2438 glMatrixMode(GL_PROJECTION);
2439 glPopMatrix();
2440 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002441 } else {
2442 result = BAD_VALUE;
2443 }
2444
2445 // release FBO resources
2446 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2447 glDeleteRenderbuffersOES(1, &tname);
2448 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002449
2450 hw.compositionComplete();
2451
Steve Block9d453682011-12-20 16:23:08 +00002452 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002453
Mathias Agopian74c40c02010-09-29 13:02:36 -07002454 return result;
2455}
2456
2457
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2459 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002460 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002461 uint32_t sw, uint32_t sh,
2462 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002463{
2464 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002465 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002466 return BAD_VALUE;
2467
2468 if (!GLExtensions::getInstance().haveFramebufferObject())
2469 return INVALID_OPERATION;
2470
2471 class MessageCaptureScreen : public MessageBase {
2472 SurfaceFlinger* flinger;
2473 DisplayID dpy;
2474 sp<IMemoryHeap>* heap;
2475 uint32_t* w;
2476 uint32_t* h;
2477 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002478 uint32_t sw;
2479 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002480 uint32_t minLayerZ;
2481 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002482 status_t result;
2483 public:
2484 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002485 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002486 uint32_t sw, uint32_t sh,
2487 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002488 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002489 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2490 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2491 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002492 {
2493 }
2494 status_t getResult() const {
2495 return result;
2496 }
2497 virtual bool handler() {
2498 Mutex::Autolock _l(flinger->mStateLock);
2499
2500 // if we have secure windows, never allow the screen capture
2501 if (flinger->mSecureFrameBuffer)
2502 return true;
2503
Mathias Agopian74c40c02010-09-29 13:02:36 -07002504 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002505 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002506
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002507 return true;
2508 }
2509 };
2510
2511 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002512 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002513 status_t res = postMessageSync(msg);
2514 if (res == NO_ERROR) {
2515 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2516 }
2517 return res;
2518}
2519
2520// ---------------------------------------------------------------------------
2521
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002522sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2523{
2524 sp<Layer> result;
2525 Mutex::Autolock _l(mStateLock);
2526 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2527 return result;
2528}
2529
2530// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002531
Jamie Gennis9a78c902011-01-12 18:30:40 -08002532GraphicBufferAlloc::GraphicBufferAlloc() {}
2533
2534GraphicBufferAlloc::~GraphicBufferAlloc() {}
2535
2536sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002537 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002538 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2539 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002540 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002541 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002542 if (err == NO_MEMORY) {
2543 GraphicBuffer::dumpAllocationsToSystemLog();
2544 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002545 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002546 "failed (%s), handle=%p",
2547 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002548 return 0;
2549 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002550 return graphicBuffer;
2551}
2552
Jamie Gennis9a78c902011-01-12 18:30:40 -08002553// ---------------------------------------------------------------------------
2554
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002555GraphicPlane::GraphicPlane()
2556 : mHw(0)
2557{
2558}
2559
2560GraphicPlane::~GraphicPlane() {
2561 delete mHw;
2562}
2563
2564bool GraphicPlane::initialized() const {
2565 return mHw ? true : false;
2566}
2567
Mathias Agopian2b92d892010-02-08 15:49:35 -08002568int GraphicPlane::getWidth() const {
2569 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002570}
2571
Mathias Agopian2b92d892010-02-08 15:49:35 -08002572int GraphicPlane::getHeight() const {
2573 return mHeight;
2574}
2575
2576void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2577{
2578 mHw = hw;
2579
2580 // initialize the display orientation transform.
2581 // it's a constant that should come from the display driver.
2582 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2583 char property[PROPERTY_VALUE_MAX];
2584 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2585 //displayOrientation
2586 switch (atoi(property)) {
2587 case 90:
2588 displayOrientation = ISurfaceComposer::eOrientation90;
2589 break;
2590 case 270:
2591 displayOrientation = ISurfaceComposer::eOrientation270;
2592 break;
2593 }
2594 }
2595
2596 const float w = hw->getWidth();
2597 const float h = hw->getHeight();
2598 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2599 &mDisplayTransform);
2600 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2601 mDisplayWidth = h;
2602 mDisplayHeight = w;
2603 } else {
2604 mDisplayWidth = w;
2605 mDisplayHeight = h;
2606 }
2607
2608 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609}
2610
2611status_t GraphicPlane::orientationToTransfrom(
2612 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002613{
2614 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615 switch (orientation) {
2616 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002617 flags = Transform::ROT_0;
2618 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002620 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002621 break;
2622 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002623 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002624 break;
2625 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002626 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627 break;
2628 default:
2629 return BAD_VALUE;
2630 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002631 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632 return NO_ERROR;
2633}
2634
2635status_t GraphicPlane::setOrientation(int orientation)
2636{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 // If the rotation can be handled in hardware, this is where
2638 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002639
2640 const DisplayHardware& hw(displayHardware());
2641 const float w = mDisplayWidth;
2642 const float h = mDisplayHeight;
2643 mWidth = int(w);
2644 mHeight = int(h);
2645
2646 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002647 GraphicPlane::orientationToTransfrom(orientation, w, h,
2648 &orientationTransform);
2649 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2650 mWidth = int(h);
2651 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002652 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002653
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002654 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002655 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 return NO_ERROR;
2657}
2658
2659const DisplayHardware& GraphicPlane::displayHardware() const {
2660 return *mHw;
2661}
2662
Mathias Agopian59119e62010-10-11 12:37:43 -07002663DisplayHardware& GraphicPlane::editDisplayHardware() {
2664 return *mHw;
2665}
2666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002667const Transform& GraphicPlane::transform() const {
2668 return mGlobalTransform;
2669}
2670
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002671EGLDisplay GraphicPlane::getEGLDisplay() const {
2672 return mHw->getEGLDisplay();
2673}
2674
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675// ---------------------------------------------------------------------------
2676
2677}; // namespace android