blob: e059f1ca551edb8528223aef0d9cb47bb849f716 [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();
475 for (size_t i = 0; i < numLayers; i++) {
476 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
477 }
478
Mathias Agopiana44b0412011-10-16 18:46:35 -0700479 mLastSwapBufferTime = systemTime() - now;
480 mDebugInSwapBuffers = 0;
481 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482}
483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
485{
Mathias Agopian841cde52012-03-01 15:44:37 -0800486 ATRACE_CALL();
487
Mathias Agopianca4d3602011-05-19 15:38:14 -0700488 Mutex::Autolock _l(mStateLock);
489 const nsecs_t now = systemTime();
490 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491
Mathias Agopianca4d3602011-05-19 15:38:14 -0700492 // Here we're guaranteed that some transaction flags are set
493 // so we can call handleTransactionLocked() unconditionally.
494 // We call getTransactionFlags(), which will also clear the flags,
495 // with mStateLock held to guarantee that mCurrentState won't change
496 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700497
Mathias Agopianca4d3602011-05-19 15:38:14 -0700498 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
499 transactionFlags = getTransactionFlags(mask);
500 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700501
Mathias Agopianca4d3602011-05-19 15:38:14 -0700502 mLastTransactionTime = systemTime() - now;
503 mDebugInTransaction = 0;
504 invalidateHwcGeometry();
505 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700506}
507
Mathias Agopianca4d3602011-05-19 15:38:14 -0700508void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700509{
510 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 const size_t count = currentLayers.size();
512
513 /*
514 * Traversal of the children
515 * (perform the transaction for each of them if needed)
516 */
517
518 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
519 if (layersNeedTransaction) {
520 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700521 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
523 if (!trFlags) continue;
524
525 const uint32_t flags = layer->doTransaction(0);
526 if (flags & Layer::eVisibleRegion)
527 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528 }
529 }
530
531 /*
532 * Perform our own transaction if needed
533 */
534
535 if (transactionFlags & eTransactionNeeded) {
536 if (mCurrentState.orientation != mDrawingState.orientation) {
537 // the orientation has changed, recompute all visible regions
538 // and invalidate everything.
539
540 const int dpy = 0;
541 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700542 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543 GraphicPlane& plane(graphicPlane(dpy));
544 plane.setOrientation(orientation);
545
546 // update the shared control block
547 const DisplayHardware& hw(plane.displayHardware());
548 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
549 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800550 dcblk->w = plane.getWidth();
551 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552
553 mVisibleRegionsDirty = true;
554 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 }
556
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
558 // layers have been added
559 mVisibleRegionsDirty = true;
560 }
561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 // some layers might have been removed, so
563 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700565 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700567 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700568 const size_t count = previousLayers.size();
569 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700570 const sp<LayerBase>& layer(previousLayers[i]);
571 if (currentLayers.indexOf( layer ) < 0) {
572 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700573 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700574 }
575 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
578
579 commitTransaction();
580}
581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800583 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584{
Mathias Agopian841cde52012-03-01 15:44:37 -0800585 ATRACE_CALL();
586
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587 const GraphicPlane& plane(graphicPlane(0));
588 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700589 const DisplayHardware& hw(plane.displayHardware());
590 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591
592 Region aboveOpaqueLayers;
593 Region aboveCoveredLayers;
594 Region dirty;
595
596 bool secureFrameBuffer = false;
597
598 size_t i = currentLayers.size();
599 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 layer->validateVisibility(planeTransform);
602
603 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700604 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605
Mathias Agopianab028732010-03-16 16:41:46 -0700606 /*
607 * opaqueRegion: area of a surface that is fully opaque.
608 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700610
611 /*
612 * visibleRegion: area of a surface that is visible on screen
613 * and not fully transparent. This is essentially the layer's
614 * footprint minus the opaque regions above it.
615 * Areas covered by a translucent surface are considered visible.
616 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700618
619 /*
620 * coveredRegion: area of a surface that is covered by all
621 * visible regions above it (which includes the translucent areas).
622 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700624
625
626 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800627 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700628 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700629 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700631 visibleRegion.andSelf(screenRegion);
632 if (!visibleRegion.isEmpty()) {
633 // Remove the transparent area from the visible region
634 if (translucent) {
635 visibleRegion.subtractSelf(layer->transparentRegionScreen);
636 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637
Mathias Agopianab028732010-03-16 16:41:46 -0700638 // compute the opaque region
639 const int32_t layerOrientation = layer->getOrientation();
640 if (s.alpha==255 && !translucent &&
641 ((layerOrientation & Transform::ROT_INVALID) == false)) {
642 // the opaque region is the layer's footprint
643 opaqueRegion = visibleRegion;
644 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 }
646 }
647
Mathias Agopianab028732010-03-16 16:41:46 -0700648 // Clip the covered region to the visible region
649 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
650
651 // Update aboveCoveredLayers for next (lower) layer
652 aboveCoveredLayers.orSelf(visibleRegion);
653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 // subtract the opaque region covered by the layers above us
655 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656
657 // compute this layer's dirty region
658 if (layer->contentDirty) {
659 // we need to invalidate the whole region
660 dirty = visibleRegion;
661 // as well, as the old visible region
662 dirty.orSelf(layer->visibleRegionScreen);
663 layer->contentDirty = false;
664 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700665 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700666 * the exposed region consists of two components:
667 * 1) what's VISIBLE now and was COVERED before
668 * 2) what's EXPOSED now less what was EXPOSED before
669 *
670 * note that (1) is conservative, we start with the whole
671 * visible region but only keep what used to be covered by
672 * something -- which mean it may have been exposed.
673 *
674 * (2) handles areas that were not covered by anything but got
675 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700676 */
Mathias Agopianab028732010-03-16 16:41:46 -0700677 const Region newExposed = visibleRegion - coveredRegion;
678 const Region oldVisibleRegion = layer->visibleRegionScreen;
679 const Region oldCoveredRegion = layer->coveredRegionScreen;
680 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
681 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 }
683 dirty.subtractSelf(aboveOpaqueLayers);
684
685 // accumulate to the screen dirty region
686 dirtyRegion.orSelf(dirty);
687
Mathias Agopianab028732010-03-16 16:41:46 -0700688 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 // Store the visible region is screen space
692 layer->setVisibleRegion(visibleRegion);
693 layer->setCoveredRegion(coveredRegion);
694
Mathias Agopian97011222009-07-28 10:57:27 -0700695 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 if (layer->isSecure() && !visibleRegion.isEmpty()) {
697 secureFrameBuffer = true;
698 }
699 }
700
Mathias Agopian97011222009-07-28 10:57:27 -0700701 // invalidate the areas where a layer was removed
702 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
703 mDirtyRegionRemovedLayer.clear();
704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 mSecureFrameBuffer = secureFrameBuffer;
706 opaqueRegion = aboveOpaqueLayers;
707}
708
709
710void SurfaceFlinger::commitTransaction()
711{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800712 if (!mLayersPendingRemoval.isEmpty()) {
713 // Notify removed layers now that they can't be drawn from
714 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
715 mLayersPendingRemoval[i]->onRemoved();
716 }
717 mLayersPendingRemoval.clear();
718 }
719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700721 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700722 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723}
724
725void SurfaceFlinger::handlePageFlip()
726{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800727 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800728 const DisplayHardware& hw = graphicPlane(0).displayHardware();
729 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800731 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
732 const bool visibleRegions = lockPageFlip(currentLayers);
733
734 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 Region opaqueRegion;
736 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700737
738 /*
739 * rebuild the visible layer list
740 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800741 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700742 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700743 mVisibleLayersSortedByZ.setCapacity(count);
744 for (size_t i=0 ; i<count ; i++) {
745 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
746 mVisibleLayersSortedByZ.add(currentLayers[i]);
747 }
748
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 mWormholeRegion = screenRegion.subtract(opaqueRegion);
750 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800751 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 }
753
754 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700755
756 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 mDirtyRegion.andSelf(screenRegion);
758}
759
Mathias Agopianad456f92011-01-13 17:53:01 -0800760void SurfaceFlinger::invalidateHwcGeometry()
761{
762 mHwWorkListDirty = true;
763}
764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
766{
767 bool recomputeVisibleRegions = false;
768 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700769 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700771 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 layer->lockPageFlip(recomputeVisibleRegions);
773 }
774 return recomputeVisibleRegions;
775}
776
777void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
778{
779 const GraphicPlane& plane(graphicPlane(0));
780 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800781 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700782 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700784 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 layer->unlockPageFlip(planeTransform, mDirtyRegion);
786 }
787}
788
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800789void SurfaceFlinger::handleRefresh()
790{
791 bool needInvalidate = false;
792 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
793 const size_t count = currentLayers.size();
794 for (size_t i=0 ; i<count ; i++) {
795 const sp<LayerBase>& layer(currentLayers[i]);
796 if (layer->onPreComposition()) {
797 needInvalidate = true;
798 }
799 }
800 if (needInvalidate) {
801 signalLayerUpdate();
802 }
803}
804
805
Mathias Agopiana350ff92010-08-10 17:14:02 -0700806void SurfaceFlinger::handleWorkList()
807{
808 mHwWorkListDirty = false;
809 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
810 if (hwc.initCheck() == NO_ERROR) {
811 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
812 const size_t count = currentLayers.size();
813 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700814
815 HWComposer::LayerListIterator cur = hwc.begin();
816 const HWComposer::LayerListIterator end = hwc.end();
817 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
818 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700819 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700820 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700821 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700822 }
823 }
824}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826void SurfaceFlinger::handleRepaint()
827{
Mathias Agopian841cde52012-03-01 15:44:37 -0800828 ATRACE_CALL();
829
Mathias Agopianb8a55602009-06-26 19:06:36 -0700830 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700831 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832
Glenn Kasten99ed2242011-12-15 09:51:17 -0800833 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 debugFlashRegions();
835 }
836
Mathias Agopianb8a55602009-06-26 19:06:36 -0700837 // set the frame buffer
838 const DisplayHardware& hw(graphicPlane(0).displayHardware());
839 glMatrixMode(GL_MODELVIEW);
840 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841
842 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700843 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700844 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
845 // takes a rectangle, we must make sure to update that whole
846 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700847 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700849 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700850 // We need to redraw the rectangle that will be updated
851 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700852 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700854 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700855 } else {
856 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700858 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 }
860 }
861
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700862 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 composeSurfaces(mDirtyRegion);
864
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700865 // update the swap region and clear the dirty region
866 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 mDirtyRegion.clear();
868}
869
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700870void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700872 const DisplayHardware& hw(graphicPlane(0).displayHardware());
873 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700874
875 HWComposer::LayerListIterator cur = hwc.begin();
876 const HWComposer::LayerListIterator end = hwc.end();
877 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700878 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700879 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700880
Mathias Agopianf384cc32011-09-08 18:31:55 -0700881 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
882 size_t count = layers.size();
883
Steve Blocke6f43dd2012-01-06 19:20:56 +0000884 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700885 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
886 hwc.getNumLayers(), count);
887
888 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700889 if (hwc.initCheck() == NO_ERROR) {
890 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
891 }
Mathias Agopian45721772010-08-12 15:03:26 -0700892
893 /*
894 * update the per-frame h/w composer data for each layer
895 * and build the transparent region of the FB
896 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700897 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700898 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700899 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900 }
901 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000902 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700903}
Mathias Agopian45721772010-08-12 15:03:26 -0700904
Mathias Agopianf384cc32011-09-08 18:31:55 -0700905void SurfaceFlinger::composeSurfaces(const Region& dirty)
906{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700907 const DisplayHardware& hw(graphicPlane(0).displayHardware());
908 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700909 HWComposer::LayerListIterator cur = hwc.begin();
910 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700911
912 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700913 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700914 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700915
Mathias Agopianb9494d52012-04-18 02:28:45 -0700916 if (hwc.getLayerCount(HWC_OVERLAY)) {
917 // when using overlays, we assume a fully transparent framebuffer
918 // NOTE: we could reduce how much we need to clear, for instance
919 // remove where there are opaque FB layers. however, on some
920 // GPUs doing a "clean slate" glClear might be more efficient.
921 // We'll revisit later if needed.
922 glClearColor(0, 0, 0, 0);
923 glClear(GL_COLOR_BUFFER_BIT);
924 } else {
925 // screen is already cleared here
926 if (!mWormholeRegion.isEmpty()) {
927 // can happen with SurfaceView
928 drawWormhole();
929 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700930 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700931
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700932 /*
933 * and then, render the layers targeted at the framebuffer
934 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700935
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700936 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
937 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700938 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700939 const sp<LayerBase>& layer(layers[i]);
940 const Region clip(dirty.intersect(layer->visibleRegionScreen));
941 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700942 if (cur->getCompositionType() == HWC_OVERLAY) {
943 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700944 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700945 // never clear the very first layer since we're
946 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700947 layer->clearWithOpenGL(clip);
948 }
949 continue;
950 }
951 // render the layer
952 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700953 }
954 }
955 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956}
957
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958void SurfaceFlinger::debugFlashRegions()
959{
Mathias Agopian0a917752010-06-14 21:20:00 -0700960 const DisplayHardware& hw(graphicPlane(0).displayHardware());
961 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700962 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700963 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700964 return;
965 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700966
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700967 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700968 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
969 mDirtyRegion.bounds() : hw.bounds());
970 composeSurfaces(repaint);
971 }
972
Mathias Agopianc492e672011-10-18 14:49:27 -0700973 glDisable(GL_TEXTURE_EXTERNAL_OES);
974 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976
Mathias Agopian0926f502009-05-04 14:17:04 -0700977 static int toggle = 0;
978 toggle = 1 - toggle;
979 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700980 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700981 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700982 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700983 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984
Mathias Agopian20f68782009-05-11 00:03:41 -0700985 Region::const_iterator it = mDirtyRegion.begin();
986 Region::const_iterator const end = mDirtyRegion.end();
987 while (it != end) {
988 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700990 { r.left, height - r.top },
991 { r.left, height - r.bottom },
992 { r.right, height - r.bottom },
993 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 };
995 glVertexPointer(2, GL_FLOAT, 0, vertices);
996 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
997 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700998
Mathias Agopian0656a682011-09-20 17:22:44 -0700999 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000
1001 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001002 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003}
1004
1005void SurfaceFlinger::drawWormhole() const
1006{
1007 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1008 if (region.isEmpty())
1009 return;
1010
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001011 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001012 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001013 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001014 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001015
1016 GLfloat vertices[4][2];
1017 glVertexPointer(2, GL_FLOAT, 0, vertices);
1018 Region::const_iterator it = region.begin();
1019 Region::const_iterator const end = region.end();
1020 while (it != end) {
1021 const Rect& r = *it++;
1022 vertices[0][0] = r.left;
1023 vertices[0][1] = r.top;
1024 vertices[1][0] = r.right;
1025 vertices[1][1] = r.top;
1026 vertices[2][0] = r.right;
1027 vertices[2][1] = r.bottom;
1028 vertices[3][0] = r.left;
1029 vertices[3][1] = r.bottom;
1030 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1031 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032}
1033
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001034status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035{
1036 Mutex::Autolock _l(mStateLock);
1037 addLayer_l(layer);
1038 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1039 return NO_ERROR;
1040}
1041
Mathias Agopian96f08192010-06-02 23:28:45 -07001042status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1043{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001044 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001045 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1046}
1047
1048ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1049 const sp<LayerBaseClient>& lbc)
1050{
Mathias Agopian96f08192010-06-02 23:28:45 -07001051 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001052 size_t name = client->attachLayer(lbc);
1053
1054 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001055
1056 // add this layer to the current state list
1057 addLayer_l(lbc);
1058
Mathias Agopian4f113742011-05-03 16:21:41 -07001059 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001060}
1061
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063{
1064 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001065 status_t err = purgatorizeLayer_l(layer);
1066 if (err == NO_ERROR)
1067 setTransactionFlags(eTransactionNeeded);
1068 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001073 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1074 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001075 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001076 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1078 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 return NO_ERROR;
1081 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001082 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083}
1084
Mathias Agopian9a112062009-04-17 19:36:26 -07001085status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1086{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001087 // First add the layer to the purgatory list, which makes sure it won't
1088 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001089 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001090 if (err >= 0) {
1091 mLayerPurgatory.add(layerBase);
1092 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001093
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001094 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001095
Mathias Agopian3d579642009-06-04 18:46:21 -07001096 // it's possible that we don't find a layer, because it might
1097 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001098 // from the user because there is a race between Client::destroySurface(),
1099 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001100 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1101}
1102
Mathias Agopian96f08192010-06-02 23:28:45 -07001103status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104{
Mathias Agopian96f08192010-06-02 23:28:45 -07001105 layer->forceVisibilityTransaction();
1106 setTransactionFlags(eTraversalNeeded);
1107 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108}
1109
Mathias Agopiandea20b12011-05-03 17:04:02 -07001110uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1111{
1112 return android_atomic_release_load(&mTransactionFlags);
1113}
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1116{
1117 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1118}
1119
Mathias Agopianbb641242010-05-18 17:06:55 -07001120uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121{
1122 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1123 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001124 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 }
1126 return old;
1127}
1128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001130void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001131 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001132 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001133
Jamie Gennis28378392011-10-12 17:39:00 -07001134 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001135 if (mCurrentState.orientation != orientation) {
1136 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1137 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001138 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001139 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001140 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001141 orientation);
1142 }
1143 }
1144
Mathias Agopian698c0872011-06-28 19:09:31 -07001145 const size_t count = state.size();
1146 for (size_t i=0 ; i<count ; i++) {
1147 const ComposerState& s(state[i]);
1148 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001149 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001150 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001151
Mathias Agopian386aa982011-11-07 21:58:03 -08001152 if (transactionFlags) {
1153 // this triggers the transaction
1154 setTransactionFlags(transactionFlags);
1155
1156 // if this is a synchronous transaction, wait for it to take effect
1157 // before returning.
1158 if (flags & eSynchronous) {
1159 mTransationPending = true;
1160 }
1161 while (mTransationPending) {
1162 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1163 if (CC_UNLIKELY(err != NO_ERROR)) {
1164 // just in case something goes wrong in SF, return to the
1165 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001166 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001167 mTransationPending = false;
1168 break;
1169 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 }
1172}
1173
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001174sp<ISurface> SurfaceFlinger::createSurface(
1175 ISurfaceComposerClient::surface_data_t* params,
1176 const String8& name,
1177 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1179 uint32_t flags)
1180{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001181 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001182 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001183
1184 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001185 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001186 int(w), int(h));
1187 return surfaceHandle;
1188 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001189
Mathias Agopianff615cc2012-02-24 14:58:36 -08001190 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001191 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 switch (flags & eFXSurfaceMask) {
1193 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001194 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1195 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 break;
1197 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001198 // for now we treat Blur as Dim, until we can implement it
1199 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001201 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001203 case eFXSurfaceScreenshot:
1204 layer = createScreenshotSurface(client, d, w, h, flags);
1205 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 }
1207
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001208 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001209 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001210 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001211 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001214 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001215 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001216 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001217 if (normalLayer != 0) {
1218 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001219 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001220 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001221 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001222
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
1225
1226 return surfaceHandle;
1227}
1228
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001229sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001230 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001232 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
1234 // initialize the surfaces
1235 switch (format) { // TODO: take h/w into account
1236 case PIXEL_FORMAT_TRANSPARENT:
1237 case PIXEL_FORMAT_TRANSLUCENT:
1238 format = PIXEL_FORMAT_RGBA_8888;
1239 break;
1240 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001241#ifdef NO_RGBX_8888
1242 format = PIXEL_FORMAT_RGB_565;
1243#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001244 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001245#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 break;
1247 }
1248
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001249#ifdef NO_RGBX_8888
1250 if (format == PIXEL_FORMAT_RGBX_8888)
1251 format = PIXEL_FORMAT_RGBA_8888;
1252#endif
1253
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001255 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001256 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001257 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001258 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 }
1260 return layer;
1261}
1262
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001263sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001264 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001265 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266{
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001268 return layer;
1269}
1270
1271sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1272 const sp<Client>& client, DisplayID display,
1273 uint32_t w, uint32_t h, uint32_t flags)
1274{
1275 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 return layer;
1277}
1278
Mathias Agopian96f08192010-06-02 23:28:45 -07001279status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280{
Mathias Agopian9a112062009-04-17 19:36:26 -07001281 /*
1282 * called by the window manager, when a surface should be marked for
1283 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001284 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001285 * The surface is removed from the current and drawing lists, but placed
1286 * in the purgatory queue, so it's not destroyed right-away (we need
1287 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001288 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001289
Mathias Agopian48d819a2009-09-10 19:41:18 -07001290 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001291 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001293
Mathias Agopian48d819a2009-09-10 19:41:18 -07001294 if (layer != 0) {
1295 err = purgatorizeLayer_l(layer);
1296 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001297 setTransactionFlags(eTransactionNeeded);
1298 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001299 }
1300 return err;
1301}
1302
Mathias Agopianca4d3602011-05-19 15:38:14 -07001303status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001304{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001305 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001306 status_t err = NO_ERROR;
1307 sp<LayerBaseClient> l(layer.promote());
1308 if (l != NULL) {
1309 Mutex::Autolock _l(mStateLock);
1310 err = removeLayer_l(l);
1311 if (err == NAME_NOT_FOUND) {
1312 // The surface wasn't in the current list, which means it was
1313 // removed already, which means it is in the purgatory,
1314 // and need to be removed from there.
1315 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001316 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001317 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001318 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001319 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001320 "error removing layer=%p (%s)", l.get(), strerror(-err));
1321 }
1322 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323}
1324
Mathias Agopian698c0872011-06-28 19:09:31 -07001325uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001326 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001327 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001330 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1331 if (layer != 0) {
1332 const uint32_t what = s.what;
1333 if (what & ePositionChanged) {
1334 if (layer->setPosition(s.x, s.y))
1335 flags |= eTraversalNeeded;
1336 }
1337 if (what & eLayerChanged) {
1338 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1339 if (layer->setLayer(s.z)) {
1340 mCurrentState.layersSortedByZ.removeAt(idx);
1341 mCurrentState.layersSortedByZ.add(layer);
1342 // we need traversal (state changed)
1343 // AND transaction (list changed)
1344 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345 }
1346 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001347 if (what & eSizeChanged) {
1348 if (layer->setSize(s.w, s.h)) {
1349 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001350 }
1351 }
1352 if (what & eAlphaChanged) {
1353 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1354 flags |= eTraversalNeeded;
1355 }
1356 if (what & eMatrixChanged) {
1357 if (layer->setMatrix(s.matrix))
1358 flags |= eTraversalNeeded;
1359 }
1360 if (what & eTransparentRegionChanged) {
1361 if (layer->setTransparentRegionHint(s.transparentRegion))
1362 flags |= eTraversalNeeded;
1363 }
1364 if (what & eVisibilityChanged) {
1365 if (layer->setFlags(s.flags, s.mask))
1366 flags |= eTraversalNeeded;
1367 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001368 if (what & eCropChanged) {
1369 if (layer->setCrop(s.crop))
1370 flags |= eTraversalNeeded;
1371 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001373 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374}
1375
Mathias Agopianb60314a2012-04-10 22:09:54 -07001376// ---------------------------------------------------------------------------
1377
1378void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001379 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001380 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1381 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001382 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001383 // this is a temporary work-around, eventually this should be called
1384 // by the power-manager
1385 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001386 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001387 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388}
1389
Mathias Agopianb60314a2012-04-10 22:09:54 -07001390void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001391 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001392 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1393 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001394 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001395 hw.releaseScreen();
1396 // from this point on, SF will stop drawing
1397 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398}
1399
Colin Cross8e533062012-06-07 13:17:52 -07001400void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001401 class MessageScreenAcquired : public MessageBase {
1402 SurfaceFlinger* flinger;
1403 public:
1404 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1405 virtual bool handler() {
1406 flinger->onScreenAcquired();
1407 return true;
1408 }
1409 };
1410 sp<MessageBase> msg = new MessageScreenAcquired(this);
1411 postMessageSync(msg);
1412}
1413
Colin Cross8e533062012-06-07 13:17:52 -07001414void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001415 class MessageScreenReleased : public MessageBase {
1416 SurfaceFlinger* flinger;
1417 public:
1418 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1419 virtual bool handler() {
1420 flinger->onScreenReleased();
1421 return true;
1422 }
1423 };
1424 sp<MessageBase> msg = new MessageScreenReleased(this);
1425 postMessageSync(msg);
1426}
1427
1428// ---------------------------------------------------------------------------
1429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1431{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001432 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 char buffer[SIZE];
1434 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001435
1436 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 snprintf(buffer, SIZE, "Permission Denial: "
1438 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1439 IPCThreadState::self()->getCallingPid(),
1440 IPCThreadState::self()->getCallingUid());
1441 result.append(buffer);
1442 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001443 // Try to get the main lock, but don't insist if we can't
1444 // (this would indicate SF is stuck, but we want to be able to
1445 // print something in dumpsys).
1446 int retry = 3;
1447 while (mStateLock.tryLock()<0 && --retry>=0) {
1448 usleep(1000000);
1449 }
1450 const bool locked(retry >= 0);
1451 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001452 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001453 "SurfaceFlinger appears to be unresponsive, "
1454 "dumping anyways (no locks held)\n");
1455 result.append(buffer);
1456 }
1457
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001458 bool dumpAll = true;
1459 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001460 size_t numArgs = args.size();
1461 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001462 if ((index < numArgs) &&
1463 (args[index] == String16("--list"))) {
1464 index++;
1465 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001466 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001467 }
1468
1469 if ((index < numArgs) &&
1470 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001471 index++;
1472 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001473 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001474 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001475
1476 if ((index < numArgs) &&
1477 (args[index] == String16("--latency-clear"))) {
1478 index++;
1479 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001480 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001481 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001483
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001484 if (dumpAll) {
1485 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001486 }
1487
Mathias Agopian9795c422009-08-26 16:36:26 -07001488 if (locked) {
1489 mStateLock.unlock();
1490 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 }
1492 write(fd, result.string(), result.size());
1493 return NO_ERROR;
1494}
1495
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001496void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1497 String8& result, char* buffer, size_t SIZE) const
1498{
1499 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1500 const size_t count = currentLayers.size();
1501 for (size_t i=0 ; i<count ; i++) {
1502 const sp<LayerBase>& layer(currentLayers[i]);
1503 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1504 result.append(buffer);
1505 }
1506}
1507
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001508void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1509 String8& result, char* buffer, size_t SIZE) const
1510{
1511 String8 name;
1512 if (index < args.size()) {
1513 name = String8(args[index]);
1514 index++;
1515 }
1516
1517 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1518 const size_t count = currentLayers.size();
1519 for (size_t i=0 ; i<count ; i++) {
1520 const sp<LayerBase>& layer(currentLayers[i]);
1521 if (name.isEmpty()) {
1522 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1523 result.append(buffer);
1524 }
1525 if (name.isEmpty() || (name == layer->getName())) {
1526 layer->dumpStats(result, buffer, SIZE);
1527 }
1528 }
1529}
1530
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001531void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1532 String8& result, char* buffer, size_t SIZE) const
1533{
1534 String8 name;
1535 if (index < args.size()) {
1536 name = String8(args[index]);
1537 index++;
1538 }
1539
1540 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1541 const size_t count = currentLayers.size();
1542 for (size_t i=0 ; i<count ; i++) {
1543 const sp<LayerBase>& layer(currentLayers[i]);
1544 if (name.isEmpty() || (name == layer->getName())) {
1545 layer->clearStats();
1546 }
1547 }
1548}
1549
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001550void SurfaceFlinger::dumpAllLocked(
1551 String8& result, char* buffer, size_t SIZE) const
1552{
1553 // figure out if we're stuck somewhere
1554 const nsecs_t now = systemTime();
1555 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1556 const nsecs_t inTransaction(mDebugInTransaction);
1557 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1558 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1559
1560 /*
1561 * Dump the visible layer list
1562 */
1563 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1564 const size_t count = currentLayers.size();
1565 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1566 result.append(buffer);
1567 for (size_t i=0 ; i<count ; i++) {
1568 const sp<LayerBase>& layer(currentLayers[i]);
1569 layer->dump(result, buffer, SIZE);
1570 }
1571
1572 /*
1573 * Dump the layers in the purgatory
1574 */
1575
1576 const size_t purgatorySize = mLayerPurgatory.size();
1577 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1578 result.append(buffer);
1579 for (size_t i=0 ; i<purgatorySize ; i++) {
1580 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1581 layer->shortDump(result, buffer, SIZE);
1582 }
1583
1584 /*
1585 * Dump SurfaceFlinger global state
1586 */
1587
1588 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1589 result.append(buffer);
1590
1591 const GLExtensions& extensions(GLExtensions::getInstance());
1592 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1593 extensions.getVendor(),
1594 extensions.getRenderer(),
1595 extensions.getVersion());
1596 result.append(buffer);
1597
1598 snprintf(buffer, SIZE, "EGL : %s\n",
1599 eglQueryString(graphicPlane(0).getEGLDisplay(),
1600 EGL_VERSION_HW_ANDROID));
1601 result.append(buffer);
1602
1603 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1604 result.append(buffer);
1605
1606 mWormholeRegion.dump(result, "WormholeRegion");
1607 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1608 snprintf(buffer, SIZE,
1609 " orientation=%d, canDraw=%d\n",
1610 mCurrentState.orientation, hw.canDraw());
1611 result.append(buffer);
1612 snprintf(buffer, SIZE,
1613 " last eglSwapBuffers() time: %f us\n"
1614 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001615 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001616 " refresh-rate : %f fps\n"
1617 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001618 " y-dpi : %f\n"
1619 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001620 mLastSwapBufferTime/1000.0,
1621 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001622 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001623 hw.getRefreshRate(),
1624 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001625 hw.getDpiY(),
1626 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001627 result.append(buffer);
1628
1629 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1630 inSwapBuffersDuration/1000.0);
1631 result.append(buffer);
1632
1633 snprintf(buffer, SIZE, " transaction time: %f us\n",
1634 inTransactionDuration/1000.0);
1635 result.append(buffer);
1636
1637 /*
1638 * VSYNC state
1639 */
1640 mEventThread->dump(result, buffer, SIZE);
1641
1642 /*
1643 * Dump HWComposer state
1644 */
1645 HWComposer& hwc(hw.getHwComposer());
1646 snprintf(buffer, SIZE, "h/w composer state:\n");
1647 result.append(buffer);
1648 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1649 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1650 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1651 result.append(buffer);
1652 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1653
1654 /*
1655 * Dump gralloc state
1656 */
1657 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1658 alloc.dump(result);
1659 hw.dump(result);
1660}
1661
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662status_t SurfaceFlinger::onTransact(
1663 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1664{
1665 switch (code) {
1666 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001667 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001670 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001671 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001672 case BLANK:
1673 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 {
1675 // codes that require permission check
1676 IPCThreadState* ipc = IPCThreadState::self();
1677 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001678 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001679 if ((uid != AID_GRAPHICS) &&
1680 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001681 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001682 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1683 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001685 break;
1686 }
1687 case CAPTURE_SCREEN:
1688 {
1689 // codes that require permission check
1690 IPCThreadState* ipc = IPCThreadState::self();
1691 const int pid = ipc->getCallingPid();
1692 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001693 if ((uid != AID_GRAPHICS) &&
1694 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001695 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001696 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1697 return PERMISSION_DENIED;
1698 }
1699 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700 }
1701 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001702
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1704 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001705 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001706 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001707 IPCThreadState* ipc = IPCThreadState::self();
1708 const int pid = ipc->getCallingPid();
1709 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001710 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001711 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 return PERMISSION_DENIED;
1713 }
1714 int n;
1715 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001716 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001717 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 return NO_ERROR;
1719 case 1002: // SHOW_UPDATES
1720 n = data.readInt32();
1721 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001722 invalidateHwcGeometry();
1723 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001726 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001727 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001729 case 1005:{ // force transaction
1730 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1731 return NO_ERROR;
1732 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001733 case 1006:{ // send empty update
1734 signalRefresh();
1735 return NO_ERROR;
1736 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001737 case 1008: // toggle use of hw composer
1738 n = data.readInt32();
1739 mDebugDisableHWC = n ? 1 : 0;
1740 invalidateHwcGeometry();
1741 repaintEverything();
1742 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001743 case 1009: // toggle use of transform hint
1744 n = data.readInt32();
1745 mDebugDisableTransformHint = n ? 1 : 0;
1746 invalidateHwcGeometry();
1747 repaintEverything();
1748 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001750 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751 reply->writeInt32(0);
1752 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001753 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001754 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 return NO_ERROR;
1756 case 1013: {
1757 Mutex::Autolock _l(mStateLock);
1758 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1759 reply->writeInt32(hw.getPageFlipCount());
1760 }
1761 return NO_ERROR;
1762 }
1763 }
1764 return err;
1765}
1766
Mathias Agopian53331da2011-08-22 21:44:41 -07001767void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001768 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001769 const Rect bounds(hw.getBounds());
1770 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001771 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001772}
1773
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001774void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1775 Mutex::Autolock _l(mInvalidateLock);
1776 mInvalidateRegion = reg;
1777}
1778
1779Region SurfaceFlinger::getAndClearInvalidateRegion() {
1780 Mutex::Autolock _l(mInvalidateLock);
1781 Region reg(mInvalidateRegion);
1782 mInvalidateRegion.clear();
1783 return reg;
1784}
1785
Mathias Agopian59119e62010-10-11 12:37:43 -07001786// ---------------------------------------------------------------------------
1787
Mathias Agopian118d0242011-10-13 16:02:48 -07001788status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1789 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1790{
1791 Mutex::Autolock _l(mStateLock);
1792 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1793}
1794
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001795status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1796 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001797{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001798 ATRACE_CALL();
1799
Mathias Agopian59119e62010-10-11 12:37:43 -07001800 if (!GLExtensions::getInstance().haveFramebufferObject())
1801 return INVALID_OPERATION;
1802
1803 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001804 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001805 const uint32_t hw_w = hw.getWidth();
1806 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001807 GLfloat u = 1;
1808 GLfloat v = 1;
1809
1810 // make sure to clear all GL error flags
1811 while ( glGetError() != GL_NO_ERROR ) ;
1812
1813 // create a FBO
1814 GLuint name, tname;
1815 glGenTextures(1, &tname);
1816 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001817 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1818 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001819 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1820 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001821 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001822 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001823 GLint tw = (2 << (31 - clz(hw_w)));
1824 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001825 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1826 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001827 u = GLfloat(hw_w) / tw;
1828 v = GLfloat(hw_h) / th;
1829 }
1830 glGenFramebuffersOES(1, &name);
1831 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001832 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1833 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001834
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001835 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001836 glDisable(GL_TEXTURE_EXTERNAL_OES);
1837 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001838 glClearColor(0,0,0,1);
1839 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001840 glMatrixMode(GL_MODELVIEW);
1841 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1843 const size_t count = layers.size();
1844 for (size_t i=0 ; i<count ; ++i) {
1845 const sp<LayerBase>& layer(layers[i]);
1846 layer->drawForSreenShot();
1847 }
1848
Mathias Agopian118d0242011-10-13 16:02:48 -07001849 hw.compositionComplete();
1850
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001851 // back to main framebuffer
1852 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001853 glDeleteFramebuffersOES(1, &name);
1854
1855 *textureName = tname;
1856 *uOut = u;
1857 *vOut = v;
1858 return NO_ERROR;
1859}
1860
1861// ---------------------------------------------------------------------------
1862
Mathias Agopianed9807b2012-05-18 14:18:08 -07001863class VSyncWaiter {
1864 DisplayEventReceiver::Event buffer[4];
1865 sp<Looper> looper;
1866 sp<IDisplayEventConnection> events;
1867 sp<BitTube> eventTube;
1868public:
1869 VSyncWaiter(const sp<EventThread>& eventThread) {
1870 looper = new Looper(true);
1871 events = eventThread->createEventConnection();
1872 eventTube = events->getDataChannel();
1873 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1874 events->requestNextVsync();
1875 }
1876
1877 void wait() {
1878 ssize_t n;
1879
1880 looper->pollOnce(-1);
1881 // we don't handle any errors here, it doesn't matter
1882 // and we don't want to take the risk to get stuck.
1883
1884 // drain the events...
1885 while ((n = DisplayEventReceiver::getEvents(
1886 eventTube, buffer, 4)) > 0) ;
1887
1888 events->requestNextVsync();
1889 }
1890};
1891
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001892status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1893{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001894 // get screen geometry
1895 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1896 const uint32_t hw_w = hw.getWidth();
1897 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001898 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001899
1900 GLfloat u, v;
1901 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001902 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001903 if (result != NO_ERROR) {
1904 return result;
1905 }
1906
1907 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001908 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001909 glBindTexture(GL_TEXTURE_2D, tname);
1910 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1911 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1912 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001913 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1914 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001915 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1916 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1917 glVertexPointer(2, GL_FLOAT, 0, vtx);
1918
Mathias Agopianffcf4652011-07-07 17:30:31 -07001919 /*
1920 * Texture coordinate mapping
1921 *
1922 * u
1923 * 1 +----------+---+
1924 * | | | | image is inverted
1925 * | V | | w.r.t. the texture
1926 * 1-v +----------+ | coordinates
1927 * | |
1928 * | |
1929 * | |
1930 * 0 +--------------+
1931 * 0 1
1932 *
1933 */
1934
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001935 class s_curve_interpolator {
1936 const float nbFrames, s, v;
1937 public:
1938 s_curve_interpolator(int nbFrames, float s)
1939 : nbFrames(1.0f / (nbFrames-1)), s(s),
1940 v(1.0f + expf(-s + 0.5f*s)) {
1941 }
1942 float operator()(int f) {
1943 const float x = f * nbFrames;
1944 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1945 }
1946 };
1947
1948 class v_stretch {
1949 const GLfloat hw_w, hw_h;
1950 public:
1951 v_stretch(uint32_t hw_w, uint32_t hw_h)
1952 : hw_w(hw_w), hw_h(hw_h) {
1953 }
1954 void operator()(GLfloat* vtx, float v) {
1955 const GLfloat w = hw_w + (hw_w * v);
1956 const GLfloat h = hw_h - (hw_h * v);
1957 const GLfloat x = (hw_w - w) * 0.5f;
1958 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001959 vtx[0] = x; vtx[1] = y;
1960 vtx[2] = x; vtx[3] = y + h;
1961 vtx[4] = x + w; vtx[5] = y + h;
1962 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001963 }
1964 };
1965
1966 class h_stretch {
1967 const GLfloat hw_w, hw_h;
1968 public:
1969 h_stretch(uint32_t hw_w, uint32_t hw_h)
1970 : hw_w(hw_w), hw_h(hw_h) {
1971 }
1972 void operator()(GLfloat* vtx, float v) {
1973 const GLfloat w = hw_w - (hw_w * v);
1974 const GLfloat h = 1.0f;
1975 const GLfloat x = (hw_w - w) * 0.5f;
1976 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001977 vtx[0] = x; vtx[1] = y;
1978 vtx[2] = x; vtx[3] = y + h;
1979 vtx[4] = x + w; vtx[5] = y + h;
1980 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001981 }
1982 };
1983
Mathias Agopianed9807b2012-05-18 14:18:08 -07001984 VSyncWaiter vsync(mEventThread);
1985
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001986 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001987 char value[PROPERTY_VALUE_MAX];
1988 property_get("debug.sf.electron_frames", value, "24");
1989 int nbFrames = (atoi(value) + 1) >> 1;
1990 if (nbFrames <= 0) // just in case
1991 nbFrames = 24;
1992
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001993 s_curve_interpolator itr(nbFrames, 7.5f);
1994 s_curve_interpolator itg(nbFrames, 8.0f);
1995 s_curve_interpolator itb(nbFrames, 8.5f);
1996
1997 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001998
1999 glMatrixMode(GL_TEXTURE);
2000 glLoadIdentity();
2001 glMatrixMode(GL_MODELVIEW);
2002 glLoadIdentity();
2003
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 glEnable(GL_BLEND);
2005 glBlendFunc(GL_ONE, GL_ONE);
2006 for (int i=0 ; i<nbFrames ; i++) {
2007 float x, y, w, h;
2008 const float vr = itr(i);
2009 const float vg = itg(i);
2010 const float vb = itb(i);
2011
Mathias Agopianed9807b2012-05-18 14:18:08 -07002012 // wait for vsync
2013 vsync.wait();
2014
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002015 // clear screen
2016 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002017 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002018 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002019
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002020 // draw the red plane
2021 vverts(vtx, vr);
2022 glColorMask(1,0,0,1);
2023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002024
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025 // draw the green plane
2026 vverts(vtx, vg);
2027 glColorMask(0,1,0,1);
2028 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002029
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002030 // draw the blue plane
2031 vverts(vtx, vb);
2032 glColorMask(0,0,1,1);
2033 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002034
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002035 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002036 glDisable(GL_TEXTURE_2D);
2037 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 glColor4f(vg, vg, vg, 1);
2039 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2040 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002041 }
2042
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002043 h_stretch hverts(hw_w, hw_h);
2044 glDisable(GL_BLEND);
2045 glDisable(GL_TEXTURE_2D);
2046 glColorMask(1,1,1,1);
2047 for (int i=0 ; i<nbFrames ; i++) {
2048 const float v = itg(i);
2049 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002050
2051 // wait for vsync
2052 vsync.wait();
2053
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 glClear(GL_COLOR_BUFFER_BIT);
2055 glColor4f(1-v, 1-v, 1-v, 1);
2056 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2057 hw.flip(screenBounds);
2058 }
2059
2060 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002061 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2062 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002063 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002064 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002065 return NO_ERROR;
2066}
2067
2068status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2069{
2070 status_t result = PERMISSION_DENIED;
2071
2072 if (!GLExtensions::getInstance().haveFramebufferObject())
2073 return INVALID_OPERATION;
2074
2075
2076 // get screen geometry
2077 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2078 const uint32_t hw_w = hw.getWidth();
2079 const uint32_t hw_h = hw.getHeight();
2080 const Region screenBounds(hw.bounds());
2081
2082 GLfloat u, v;
2083 GLuint tname;
2084 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2085 if (result != NO_ERROR) {
2086 return result;
2087 }
2088
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002089 GLfloat vtx[8];
2090 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002091 glBindTexture(GL_TEXTURE_2D, tname);
2092 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2093 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2094 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002095 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2096 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002097 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2098 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2099 glVertexPointer(2, GL_FLOAT, 0, vtx);
2100
2101 class s_curve_interpolator {
2102 const float nbFrames, s, v;
2103 public:
2104 s_curve_interpolator(int nbFrames, float s)
2105 : nbFrames(1.0f / (nbFrames-1)), s(s),
2106 v(1.0f + expf(-s + 0.5f*s)) {
2107 }
2108 float operator()(int f) {
2109 const float x = f * nbFrames;
2110 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2111 }
2112 };
2113
2114 class v_stretch {
2115 const GLfloat hw_w, hw_h;
2116 public:
2117 v_stretch(uint32_t hw_w, uint32_t hw_h)
2118 : hw_w(hw_w), hw_h(hw_h) {
2119 }
2120 void operator()(GLfloat* vtx, float v) {
2121 const GLfloat w = hw_w + (hw_w * v);
2122 const GLfloat h = hw_h - (hw_h * v);
2123 const GLfloat x = (hw_w - w) * 0.5f;
2124 const GLfloat y = (hw_h - h) * 0.5f;
2125 vtx[0] = x; vtx[1] = y;
2126 vtx[2] = x; vtx[3] = y + h;
2127 vtx[4] = x + w; vtx[5] = y + h;
2128 vtx[6] = x + w; vtx[7] = y;
2129 }
2130 };
2131
2132 class h_stretch {
2133 const GLfloat hw_w, hw_h;
2134 public:
2135 h_stretch(uint32_t hw_w, uint32_t hw_h)
2136 : hw_w(hw_w), hw_h(hw_h) {
2137 }
2138 void operator()(GLfloat* vtx, float v) {
2139 const GLfloat w = hw_w - (hw_w * v);
2140 const GLfloat h = 1.0f;
2141 const GLfloat x = (hw_w - w) * 0.5f;
2142 const GLfloat y = (hw_h - h) * 0.5f;
2143 vtx[0] = x; vtx[1] = y;
2144 vtx[2] = x; vtx[3] = y + h;
2145 vtx[4] = x + w; vtx[5] = y + h;
2146 vtx[6] = x + w; vtx[7] = y;
2147 }
2148 };
2149
Mathias Agopianed9807b2012-05-18 14:18:08 -07002150 VSyncWaiter vsync(mEventThread);
2151
Mathias Agopiana6546e52010-10-14 12:33:07 -07002152 // the full animation is 12 frames
2153 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002154 s_curve_interpolator itr(nbFrames, 7.5f);
2155 s_curve_interpolator itg(nbFrames, 8.0f);
2156 s_curve_interpolator itb(nbFrames, 8.5f);
2157
2158 h_stretch hverts(hw_w, hw_h);
2159 glDisable(GL_BLEND);
2160 glDisable(GL_TEXTURE_2D);
2161 glColorMask(1,1,1,1);
2162 for (int i=nbFrames-1 ; i>=0 ; i--) {
2163 const float v = itg(i);
2164 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002165
2166 // wait for vsync
2167 vsync.wait();
2168
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169 glClear(GL_COLOR_BUFFER_BIT);
2170 glColor4f(1-v, 1-v, 1-v, 1);
2171 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2172 hw.flip(screenBounds);
2173 }
2174
Mathias Agopiana6546e52010-10-14 12:33:07 -07002175 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002176 v_stretch vverts(hw_w, hw_h);
2177 glEnable(GL_BLEND);
2178 glBlendFunc(GL_ONE, GL_ONE);
2179 for (int i=nbFrames-1 ; i>=0 ; i--) {
2180 float x, y, w, h;
2181 const float vr = itr(i);
2182 const float vg = itg(i);
2183 const float vb = itb(i);
2184
Mathias Agopianed9807b2012-05-18 14:18:08 -07002185 // wait for vsync
2186 vsync.wait();
2187
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188 // clear screen
2189 glColorMask(1,1,1,1);
2190 glClear(GL_COLOR_BUFFER_BIT);
2191 glEnable(GL_TEXTURE_2D);
2192
2193 // draw the red plane
2194 vverts(vtx, vr);
2195 glColorMask(1,0,0,1);
2196 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2197
2198 // draw the green plane
2199 vverts(vtx, vg);
2200 glColorMask(0,1,0,1);
2201 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2202
2203 // draw the blue plane
2204 vverts(vtx, vb);
2205 glColorMask(0,0,1,1);
2206 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2207
2208 hw.flip(screenBounds);
2209 }
2210
2211 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002212 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002213 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002214 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002215 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002216
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002217 return NO_ERROR;
2218}
2219
2220// ---------------------------------------------------------------------------
2221
Mathias Agopianabd671a2010-10-14 14:54:06 -07002222status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002223{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002224 ATRACE_CALL();
2225
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2227 if (!hw.canDraw()) {
2228 // we're already off
2229 return NO_ERROR;
2230 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002231
2232 // turn off hwc while we're doing the animation
2233 hw.getHwComposer().disable();
2234 // and make sure to turn it back on (if needed) next time we compose
2235 invalidateHwcGeometry();
2236
Mathias Agopianabd671a2010-10-14 14:54:06 -07002237 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2238 electronBeamOffAnimationImplLocked();
2239 }
2240
2241 // always clear the whole screen at the end of the animation
2242 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002243 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002244 hw.flip( Region(hw.bounds()) );
2245
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002246 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002247}
2248
2249status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2250{
Mathias Agopian59119e62010-10-11 12:37:43 -07002251 class MessageTurnElectronBeamOff : public MessageBase {
2252 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002253 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002254 status_t result;
2255 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002256 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2257 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002258 }
2259 status_t getResult() const {
2260 return result;
2261 }
2262 virtual bool handler() {
2263 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002264 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002265 return true;
2266 }
2267 };
2268
Mathias Agopianabd671a2010-10-14 14:54:06 -07002269 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002270 status_t res = postMessageSync(msg);
2271 if (res == NO_ERROR) {
2272 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002273
2274 // work-around: when the power-manager calls us we activate the
2275 // animation. eventually, the "on" animation will be called
2276 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002277 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002278 }
2279 return res;
2280}
2281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002282// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002283
Mathias Agopianabd671a2010-10-14 14:54:06 -07002284status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002285{
2286 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2287 if (hw.canDraw()) {
2288 // we're already on
2289 return NO_ERROR;
2290 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002291 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2292 electronBeamOnAnimationImplLocked();
2293 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002294
2295 // make sure to redraw the whole screen when the animation is done
2296 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002297 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002298
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002299 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002300}
2301
2302status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2303{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002304 class MessageTurnElectronBeamOn : public MessageBase {
2305 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002306 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002307 status_t result;
2308 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002309 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2310 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002311 }
2312 status_t getResult() const {
2313 return result;
2314 }
2315 virtual bool handler() {
2316 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002317 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002318 return true;
2319 }
2320 };
2321
Mathias Agopianabd671a2010-10-14 14:54:06 -07002322 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002323 return NO_ERROR;
2324}
2325
2326// ---------------------------------------------------------------------------
2327
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2329 sp<IMemoryHeap>* heap,
2330 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002331 uint32_t sw, uint32_t sh,
2332 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002333{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002334 ATRACE_CALL();
2335
Mathias Agopian74c40c02010-09-29 13:02:36 -07002336 status_t result = PERMISSION_DENIED;
2337
2338 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002339 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002340 return BAD_VALUE;
2341
2342 if (!GLExtensions::getInstance().haveFramebufferObject())
2343 return INVALID_OPERATION;
2344
2345 // get screen geometry
2346 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2347 const uint32_t hw_w = hw.getWidth();
2348 const uint32_t hw_h = hw.getHeight();
2349
2350 if ((sw > hw_w) || (sh > hw_h))
2351 return BAD_VALUE;
2352
2353 sw = (!sw) ? hw_w : sw;
2354 sh = (!sh) ? hw_h : sh;
2355 const size_t size = sw * sh * 4;
2356
Steve Block9d453682011-12-20 16:23:08 +00002357 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002358 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002359
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360 // make sure to clear all GL error flags
2361 while ( glGetError() != GL_NO_ERROR ) ;
2362
2363 // create a FBO
2364 GLuint name, tname;
2365 glGenRenderbuffersOES(1, &tname);
2366 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2367 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002368
Mathias Agopian74c40c02010-09-29 13:02:36 -07002369 glGenFramebuffersOES(1, &name);
2370 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2371 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2372 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2373
2374 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002375
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2377
2378 // invert everything, b/c glReadPixel() below will invert the FB
2379 glViewport(0, 0, sw, sh);
2380 glMatrixMode(GL_PROJECTION);
2381 glPushMatrix();
2382 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002383 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 glMatrixMode(GL_MODELVIEW);
2385
2386 // redraw the screen entirely...
2387 glClearColor(0,0,0,1);
2388 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002389
Jamie Gennis9575f602011-10-07 14:51:16 -07002390 const LayerVector& layers(mDrawingState.layersSortedByZ);
2391 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 for (size_t i=0 ; i<count ; ++i) {
2393 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002394 const uint32_t flags = layer->drawingState().flags;
2395 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2396 const uint32_t z = layer->drawingState().z;
2397 if (z >= minLayerZ && z <= maxLayerZ) {
2398 layer->drawForSreenShot();
2399 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002400 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 }
2402
Mathias Agopian74c40c02010-09-29 13:02:36 -07002403 // check for errors and return screen capture
2404 if (glGetError() != GL_NO_ERROR) {
2405 // error while rendering
2406 result = INVALID_OPERATION;
2407 } else {
2408 // allocate shared memory large enough to hold the
2409 // screen capture
2410 sp<MemoryHeapBase> base(
2411 new MemoryHeapBase(size, 0, "screen-capture") );
2412 void* const ptr = base->getBase();
2413 if (ptr) {
2414 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002415 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002416 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2417 if (glGetError() == GL_NO_ERROR) {
2418 *heap = base;
2419 *w = sw;
2420 *h = sh;
2421 *f = PIXEL_FORMAT_RGBA_8888;
2422 result = NO_ERROR;
2423 }
2424 } else {
2425 result = NO_MEMORY;
2426 }
2427 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002428 glViewport(0, 0, hw_w, hw_h);
2429 glMatrixMode(GL_PROJECTION);
2430 glPopMatrix();
2431 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002432 } else {
2433 result = BAD_VALUE;
2434 }
2435
2436 // release FBO resources
2437 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2438 glDeleteRenderbuffersOES(1, &tname);
2439 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002440
2441 hw.compositionComplete();
2442
Steve Block9d453682011-12-20 16:23:08 +00002443 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002444
Mathias Agopian74c40c02010-09-29 13:02:36 -07002445 return result;
2446}
2447
2448
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002449status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2450 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002451 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002452 uint32_t sw, uint32_t sh,
2453 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002454{
2455 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002456 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002457 return BAD_VALUE;
2458
2459 if (!GLExtensions::getInstance().haveFramebufferObject())
2460 return INVALID_OPERATION;
2461
2462 class MessageCaptureScreen : public MessageBase {
2463 SurfaceFlinger* flinger;
2464 DisplayID dpy;
2465 sp<IMemoryHeap>* heap;
2466 uint32_t* w;
2467 uint32_t* h;
2468 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002469 uint32_t sw;
2470 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002471 uint32_t minLayerZ;
2472 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002473 status_t result;
2474 public:
2475 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002476 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002477 uint32_t sw, uint32_t sh,
2478 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002479 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002480 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2481 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2482 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483 {
2484 }
2485 status_t getResult() const {
2486 return result;
2487 }
2488 virtual bool handler() {
2489 Mutex::Autolock _l(flinger->mStateLock);
2490
2491 // if we have secure windows, never allow the screen capture
2492 if (flinger->mSecureFrameBuffer)
2493 return true;
2494
Mathias Agopian74c40c02010-09-29 13:02:36 -07002495 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002496 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002497
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002498 return true;
2499 }
2500 };
2501
2502 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002503 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504 status_t res = postMessageSync(msg);
2505 if (res == NO_ERROR) {
2506 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2507 }
2508 return res;
2509}
2510
2511// ---------------------------------------------------------------------------
2512
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002513sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2514{
2515 sp<Layer> result;
2516 Mutex::Autolock _l(mStateLock);
2517 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2518 return result;
2519}
2520
2521// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522
Jamie Gennis9a78c902011-01-12 18:30:40 -08002523GraphicBufferAlloc::GraphicBufferAlloc() {}
2524
2525GraphicBufferAlloc::~GraphicBufferAlloc() {}
2526
2527sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002528 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002529 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2530 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002531 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002532 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002533 if (err == NO_MEMORY) {
2534 GraphicBuffer::dumpAllocationsToSystemLog();
2535 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002536 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002537 "failed (%s), handle=%p",
2538 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002539 return 0;
2540 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002541 return graphicBuffer;
2542}
2543
Jamie Gennis9a78c902011-01-12 18:30:40 -08002544// ---------------------------------------------------------------------------
2545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546GraphicPlane::GraphicPlane()
2547 : mHw(0)
2548{
2549}
2550
2551GraphicPlane::~GraphicPlane() {
2552 delete mHw;
2553}
2554
2555bool GraphicPlane::initialized() const {
2556 return mHw ? true : false;
2557}
2558
Mathias Agopian2b92d892010-02-08 15:49:35 -08002559int GraphicPlane::getWidth() const {
2560 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561}
2562
Mathias Agopian2b92d892010-02-08 15:49:35 -08002563int GraphicPlane::getHeight() const {
2564 return mHeight;
2565}
2566
2567void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2568{
2569 mHw = hw;
2570
2571 // initialize the display orientation transform.
2572 // it's a constant that should come from the display driver.
2573 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2574 char property[PROPERTY_VALUE_MAX];
2575 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2576 //displayOrientation
2577 switch (atoi(property)) {
2578 case 90:
2579 displayOrientation = ISurfaceComposer::eOrientation90;
2580 break;
2581 case 270:
2582 displayOrientation = ISurfaceComposer::eOrientation270;
2583 break;
2584 }
2585 }
2586
2587 const float w = hw->getWidth();
2588 const float h = hw->getHeight();
2589 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2590 &mDisplayTransform);
2591 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2592 mDisplayWidth = h;
2593 mDisplayHeight = w;
2594 } else {
2595 mDisplayWidth = w;
2596 mDisplayHeight = h;
2597 }
2598
2599 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002600}
2601
2602status_t GraphicPlane::orientationToTransfrom(
2603 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002604{
2605 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002606 switch (orientation) {
2607 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002608 flags = Transform::ROT_0;
2609 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002610 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002611 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002612 break;
2613 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002614 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615 break;
2616 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002617 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618 break;
2619 default:
2620 return BAD_VALUE;
2621 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002622 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623 return NO_ERROR;
2624}
2625
2626status_t GraphicPlane::setOrientation(int orientation)
2627{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628 // If the rotation can be handled in hardware, this is where
2629 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002630
2631 const DisplayHardware& hw(displayHardware());
2632 const float w = mDisplayWidth;
2633 const float h = mDisplayHeight;
2634 mWidth = int(w);
2635 mHeight = int(h);
2636
2637 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002638 GraphicPlane::orientationToTransfrom(orientation, w, h,
2639 &orientationTransform);
2640 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2641 mWidth = int(h);
2642 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002644
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002645 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002646 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002647 return NO_ERROR;
2648}
2649
2650const DisplayHardware& GraphicPlane::displayHardware() const {
2651 return *mHw;
2652}
2653
Mathias Agopian59119e62010-10-11 12:37:43 -07002654DisplayHardware& GraphicPlane::editDisplayHardware() {
2655 return *mHw;
2656}
2657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658const Transform& GraphicPlane::transform() const {
2659 return mGlobalTransform;
2660}
2661
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002662EGLDisplay GraphicPlane::getEGLDisplay() const {
2663 return mHw->getEGLDisplay();
2664}
2665
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002666// ---------------------------------------------------------------------------
2667
2668}; // namespace android