blob: 51fcce43e68224b483f4cd8c21b9f83635f17520 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080053#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070057#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070061#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Glenn Kasten1db13d72011-12-19 13:55:34 -080063#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070065#include <gui/BitTube.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mSecureFrameBuffer(0)
100{
101 init();
102}
103
104void SurfaceFlinger::init()
105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
Colin Cross3854ed52012-03-23 14:17:18 -0700114#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115 property_get("debug.sf.ddms", value, "0");
116 mDebugDDMS = atoi(value);
117 if (mDebugDDMS) {
118 DdmConnection::start(getServiceName());
119 }
Colin Cross3854ed52012-03-23 14:17:18 -0700120#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Steve Blocka19954a2012-01-04 20:05:49 +0000122 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126void SurfaceFlinger::onFirstRef()
127{
128 mEventQueue.init(this);
129
130 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
131
132 // Wait for the main thread to be done with its initialization
133 mReadyToRunBarrier.wait();
134}
135
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137SurfaceFlinger::~SurfaceFlinger()
138{
139 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142void SurfaceFlinger::binderDied(const wp<IBinder>& who)
143{
144 // the window manager died on us. prepare its eulogy.
145
146 // reset screen orientation
147 Vector<ComposerState> state;
148 setTransactionState(state, eOrientationDefault, 0);
149
150 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700151 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800152}
153
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700154sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700156 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157}
158
Mathias Agopian7e27f052010-05-28 14:22:23 -0700159sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Mathias Agopian96f08192010-06-02 23:28:45 -0700161 sp<ISurfaceComposerClient> bclient;
162 sp<Client> client(new Client(this));
163 status_t err = client->initCheck();
164 if (err == NO_ERROR) {
165 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 return bclient;
168}
169
Jamie Gennis9a78c902011-01-12 18:30:40 -0800170sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
171{
172 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
173 return gba;
174}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
177{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000178 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 const GraphicPlane& plane(mGraphicPlanes[dpy]);
180 return plane;
181}
182
183GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
184{
185 return const_cast<GraphicPlane&>(
186 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
187}
188
189void SurfaceFlinger::bootFinished()
190{
191 const nsecs_t now = systemTime();
192 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000193 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700194 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700195
196 // wait patiently for the window manager death
197 const String16 name("window");
198 sp<IBinder> window(defaultServiceManager()->getService(name));
199 if (window != 0) {
200 window->linkToDeath(this);
201 }
202
203 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700204 // formerly we would just kill the process, but we now ask it to exit so it
205 // can choose where to stop the animation.
206 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209static inline uint16_t pack565(int r, int g, int b) {
210 return (r<<11)|(g<<5)|b;
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213status_t SurfaceFlinger::readyToRun()
214{
Steve Blocka19954a2012-01-04 20:05:49 +0000215 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 "Initializing graphics H/W...");
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 // we only support one display currently
219 int dpy = 0;
220
221 {
222 // initialize the main display
223 GraphicPlane& plane(graphicPlane(dpy));
224 DisplayHardware* const hw = new DisplayHardware(this, dpy);
225 plane.setDisplayHardware(hw);
226 }
227
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700228 // create the shared control-block
229 mServerHeap = new MemoryHeapBase(4096,
230 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000234 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700235
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700236 new(mServerCblk) surface_flinger_cblk_t;
237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 // initialize primary screen
239 // (other display should be initialized in the same manner, but
240 // asynchronously, as they could come and go. None of this is supported
241 // yet).
242 const GraphicPlane& plane(graphicPlane(dpy));
243 const DisplayHardware& hw = plane.displayHardware();
244 const uint32_t w = hw.getWidth();
245 const uint32_t h = hw.getHeight();
246 const uint32_t f = hw.getFormat();
247 hw.makeCurrent();
248
249 // initialize the shared control block
250 mServerCblk->connected |= 1<<dpy;
251 display_cblk_t* dcblk = mServerCblk->displays + dpy;
252 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800253 dcblk->w = plane.getWidth();
254 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 dcblk->format = f;
256 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
257 dcblk->xdpi = hw.getDpiX();
258 dcblk->ydpi = hw.getDpiY();
259 dcblk->fps = hw.getRefreshRate();
260 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
262 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700264 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 glShadeModel(GL_FLAT);
267 glDisable(GL_DITHER);
268 glDisable(GL_CULL_FACE);
269
270 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
271 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700272 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 glGenTextures(1, &mWormholeTexName);
274 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700280 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
281
282 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
283 glGenTextures(1, &mProtectedTexName);
284 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
290 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291
292 glViewport(0, 0, w, h);
293 glMatrixMode(GL_PROJECTION);
294 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700295 // put the origin in the left-bottom corner
296 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 -0800297
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800298
299 // start the EventThread
300 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800301 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800302 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
304 /*
305 * We're now ready to accept clients...
306 */
307
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800308 mReadyToRunBarrier.open();
309
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700310 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700311 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
Mathias Agopiana67e4182012-06-19 17:26:12 -0700316void SurfaceFlinger::startBootAnim() {
317 // start boot animation
318 property_set("service.bootanim.exit", "0");
319 property_set("ctl.start", "bootanim");
320}
321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800323
Jamie Gennis582270d2011-08-17 18:19:00 -0700324bool SurfaceFlinger::authenticateSurfaceTexture(
325 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800326 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700327 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800328
329 // Check the visible layer list for the ISurface
330 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
331 size_t count = currentLayers.size();
332 for (size_t i=0 ; i<count ; i++) {
333 const sp<LayerBase>& layer(currentLayers[i]);
334 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700335 if (lbc != NULL) {
336 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
337 if (lbcBinder == surfaceTextureBinder) {
338 return true;
339 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800340 }
341 }
342
343 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700344 // SurfaceTexture gets destroyed before all the clients are done using it,
345 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800346 // will instead fail later on when the client tries to use the surface,
347 // which should be reported as "surface XYZ returned an -ENODEV". The
348 // purgatorized layers are no less authentic than the visible ones, so this
349 // should not cause any harm.
350 size_t purgatorySize = mLayerPurgatory.size();
351 for (size_t i=0 ; i<purgatorySize ; i++) {
352 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
353 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700354 if (lbc != NULL) {
355 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
356 if (lbcBinder == surfaceTextureBinder) {
357 return true;
358 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800359 }
360 }
361
362 return false;
363}
364
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800365// ----------------------------------------------------------------------------
366
367sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800368 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700369}
370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800372
373void SurfaceFlinger::waitForEvent() {
374 mEventQueue.waitMessage();
375}
376
377void SurfaceFlinger::signalTransaction() {
378 mEventQueue.invalidate();
379}
380
381void SurfaceFlinger::signalLayerUpdate() {
382 mEventQueue.invalidate();
383}
384
385void SurfaceFlinger::signalRefresh() {
386 mEventQueue.refresh();
387}
388
389status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
390 nsecs_t reltime, uint32_t flags) {
391 return mEventQueue.postMessage(msg, reltime);
392}
393
394status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
395 nsecs_t reltime, uint32_t flags) {
396 status_t res = mEventQueue.postMessage(msg, reltime);
397 if (res == NO_ERROR) {
398 msg->wait();
399 }
400 return res;
401}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402
403bool SurfaceFlinger::threadLoop()
404{
405 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 return true;
407}
408
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800409void SurfaceFlinger::onMessageReceived(int32_t what)
410{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800411 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800412 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700413 case MessageQueue::REFRESH: {
414// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800415 // if we're in a global transaction, don't do anything.
416 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
417 uint32_t transactionFlags = peekTransactionFlags(mask);
418 if (CC_UNLIKELY(transactionFlags)) {
419 handleTransaction(transactionFlags);
420 }
421
422 // post surfaces (if needed)
423 handlePageFlip();
424
Mathias Agopian69a655c2012-04-11 20:43:19 -0700425// signalRefresh();
426//
427// } break;
428//
429// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800430
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800431 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800432
Mathias Agopian303d5382012-02-05 01:49:16 -0800433 const DisplayHardware& hw(graphicPlane(0).displayHardware());
434
Mathias Agopian69a655c2012-04-11 20:43:19 -0700435// if (mDirtyRegion.isEmpty()) {
436// return;
437// }
438
Mathias Agopianb048cef2012-02-04 15:44:04 -0800439 if (CC_UNLIKELY(mHwWorkListDirty)) {
440 // build the h/w work list
441 handleWorkList();
442 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800443
Mathias Agopianb048cef2012-02-04 15:44:04 -0800444 if (CC_LIKELY(hw.canDraw())) {
445 // repaint the framebuffer (if needed)
446 handleRepaint();
447 // inform the h/w that we're done compositing
448 hw.compositionComplete();
449 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800450 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800451 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800452 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800453 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800454
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800455 } break;
456 }
457}
458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459void SurfaceFlinger::postFramebuffer()
460{
Mathias Agopian841cde52012-03-01 15:44:37 -0800461 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800462 // mSwapRegion can be empty here is some cases, for instance if a hidden
463 // or fully transparent window is updating.
464 // in that case, we need to flip anyways to not risk a deadlock with
465 // h/w composer.
466
Mathias Agopiana44b0412011-10-16 18:46:35 -0700467 const DisplayHardware& hw(graphicPlane(0).displayHardware());
468 const nsecs_t now = systemTime();
469 mDebugInSwapBuffers = now;
470 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800471
472 size_t numLayers = mVisibleLayersSortedByZ.size();
473 for (size_t i = 0; i < numLayers; i++) {
474 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
475 }
476
Mathias Agopiana44b0412011-10-16 18:46:35 -0700477 mLastSwapBufferTime = systemTime() - now;
478 mDebugInSwapBuffers = 0;
479 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480}
481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
483{
Mathias Agopian841cde52012-03-01 15:44:37 -0800484 ATRACE_CALL();
485
Mathias Agopianca4d3602011-05-19 15:38:14 -0700486 Mutex::Autolock _l(mStateLock);
487 const nsecs_t now = systemTime();
488 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489
Mathias Agopianca4d3602011-05-19 15:38:14 -0700490 // Here we're guaranteed that some transaction flags are set
491 // so we can call handleTransactionLocked() unconditionally.
492 // We call getTransactionFlags(), which will also clear the flags,
493 // with mStateLock held to guarantee that mCurrentState won't change
494 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700495
Mathias Agopianca4d3602011-05-19 15:38:14 -0700496 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
497 transactionFlags = getTransactionFlags(mask);
498 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700499
Mathias Agopianca4d3602011-05-19 15:38:14 -0700500 mLastTransactionTime = systemTime() - now;
501 mDebugInTransaction = 0;
502 invalidateHwcGeometry();
503 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700504}
505
Mathias Agopianca4d3602011-05-19 15:38:14 -0700506void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700507{
508 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 const size_t count = currentLayers.size();
510
511 /*
512 * Traversal of the children
513 * (perform the transaction for each of them if needed)
514 */
515
516 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
517 if (layersNeedTransaction) {
518 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700519 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
521 if (!trFlags) continue;
522
523 const uint32_t flags = layer->doTransaction(0);
524 if (flags & Layer::eVisibleRegion)
525 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 }
527 }
528
529 /*
530 * Perform our own transaction if needed
531 */
532
533 if (transactionFlags & eTransactionNeeded) {
534 if (mCurrentState.orientation != mDrawingState.orientation) {
535 // the orientation has changed, recompute all visible regions
536 // and invalidate everything.
537
538 const int dpy = 0;
539 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700540 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 GraphicPlane& plane(graphicPlane(dpy));
542 plane.setOrientation(orientation);
543
544 // update the shared control block
545 const DisplayHardware& hw(plane.displayHardware());
546 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
547 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800548 dcblk->w = plane.getWidth();
549 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550
551 mVisibleRegionsDirty = true;
552 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553 }
554
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700555 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
556 // layers have been added
557 mVisibleRegionsDirty = true;
558 }
559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 // some layers might have been removed, so
561 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700562 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700563 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700565 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700566 const size_t count = previousLayers.size();
567 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700568 const sp<LayerBase>& layer(previousLayers[i]);
569 if (currentLayers.indexOf( layer ) < 0) {
570 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700571 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700572 }
573 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 }
576
577 commitTransaction();
578}
579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800581 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582{
Mathias Agopian841cde52012-03-01 15:44:37 -0800583 ATRACE_CALL();
584
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 const GraphicPlane& plane(graphicPlane(0));
586 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700587 const DisplayHardware& hw(plane.displayHardware());
588 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589
590 Region aboveOpaqueLayers;
591 Region aboveCoveredLayers;
592 Region dirty;
593
594 bool secureFrameBuffer = false;
595
596 size_t i = currentLayers.size();
597 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700598 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 layer->validateVisibility(planeTransform);
600
601 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700602 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603
Mathias Agopianab028732010-03-16 16:41:46 -0700604 /*
605 * opaqueRegion: area of a surface that is fully opaque.
606 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700608
609 /*
610 * visibleRegion: area of a surface that is visible on screen
611 * and not fully transparent. This is essentially the layer's
612 * footprint minus the opaque regions above it.
613 * Areas covered by a translucent surface are considered visible.
614 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700616
617 /*
618 * coveredRegion: area of a surface that is covered by all
619 * visible regions above it (which includes the translucent areas).
620 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700622
623
624 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800625 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700626 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700627 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700629 visibleRegion.andSelf(screenRegion);
630 if (!visibleRegion.isEmpty()) {
631 // Remove the transparent area from the visible region
632 if (translucent) {
633 visibleRegion.subtractSelf(layer->transparentRegionScreen);
634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635
Mathias Agopianab028732010-03-16 16:41:46 -0700636 // compute the opaque region
637 const int32_t layerOrientation = layer->getOrientation();
638 if (s.alpha==255 && !translucent &&
639 ((layerOrientation & Transform::ROT_INVALID) == false)) {
640 // the opaque region is the layer's footprint
641 opaqueRegion = visibleRegion;
642 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 }
644 }
645
Mathias Agopianab028732010-03-16 16:41:46 -0700646 // Clip the covered region to the visible region
647 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
648
649 // Update aboveCoveredLayers for next (lower) layer
650 aboveCoveredLayers.orSelf(visibleRegion);
651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 // subtract the opaque region covered by the layers above us
653 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654
655 // compute this layer's dirty region
656 if (layer->contentDirty) {
657 // we need to invalidate the whole region
658 dirty = visibleRegion;
659 // as well, as the old visible region
660 dirty.orSelf(layer->visibleRegionScreen);
661 layer->contentDirty = false;
662 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700663 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700664 * the exposed region consists of two components:
665 * 1) what's VISIBLE now and was COVERED before
666 * 2) what's EXPOSED now less what was EXPOSED before
667 *
668 * note that (1) is conservative, we start with the whole
669 * visible region but only keep what used to be covered by
670 * something -- which mean it may have been exposed.
671 *
672 * (2) handles areas that were not covered by anything but got
673 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700674 */
Mathias Agopianab028732010-03-16 16:41:46 -0700675 const Region newExposed = visibleRegion - coveredRegion;
676 const Region oldVisibleRegion = layer->visibleRegionScreen;
677 const Region oldCoveredRegion = layer->coveredRegionScreen;
678 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
679 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 }
681 dirty.subtractSelf(aboveOpaqueLayers);
682
683 // accumulate to the screen dirty region
684 dirtyRegion.orSelf(dirty);
685
Mathias Agopianab028732010-03-16 16:41:46 -0700686 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700688
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 // Store the visible region is screen space
690 layer->setVisibleRegion(visibleRegion);
691 layer->setCoveredRegion(coveredRegion);
692
Mathias Agopian97011222009-07-28 10:57:27 -0700693 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 if (layer->isSecure() && !visibleRegion.isEmpty()) {
695 secureFrameBuffer = true;
696 }
697 }
698
Mathias Agopian97011222009-07-28 10:57:27 -0700699 // invalidate the areas where a layer was removed
700 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
701 mDirtyRegionRemovedLayer.clear();
702
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 mSecureFrameBuffer = secureFrameBuffer;
704 opaqueRegion = aboveOpaqueLayers;
705}
706
707
708void SurfaceFlinger::commitTransaction()
709{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800710 if (!mLayersPendingRemoval.isEmpty()) {
711 // Notify removed layers now that they can't be drawn from
712 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
713 mLayersPendingRemoval[i]->onRemoved();
714 }
715 mLayersPendingRemoval.clear();
716 }
717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700719 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700720 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721}
722
723void SurfaceFlinger::handlePageFlip()
724{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800725 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800726 const DisplayHardware& hw = graphicPlane(0).displayHardware();
727 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800729 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
730 const bool visibleRegions = lockPageFlip(currentLayers);
731
732 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 Region opaqueRegion;
734 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700735
736 /*
737 * rebuild the visible layer list
738 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800739 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700740 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700741 mVisibleLayersSortedByZ.setCapacity(count);
742 for (size_t i=0 ; i<count ; i++) {
743 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
744 mVisibleLayersSortedByZ.add(currentLayers[i]);
745 }
746
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 mWormholeRegion = screenRegion.subtract(opaqueRegion);
748 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800749 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 }
751
752 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700753
754 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755 mDirtyRegion.andSelf(screenRegion);
756}
757
Mathias Agopianad456f92011-01-13 17:53:01 -0800758void SurfaceFlinger::invalidateHwcGeometry()
759{
760 mHwWorkListDirty = true;
761}
762
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
764{
765 bool recomputeVisibleRegions = false;
766 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700767 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700769 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 layer->lockPageFlip(recomputeVisibleRegions);
771 }
772 return recomputeVisibleRegions;
773}
774
775void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
776{
777 const GraphicPlane& plane(graphicPlane(0));
778 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800779 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700780 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700782 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 layer->unlockPageFlip(planeTransform, mDirtyRegion);
784 }
785}
786
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800787void SurfaceFlinger::handleRefresh()
788{
789 bool needInvalidate = false;
790 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
791 const size_t count = currentLayers.size();
792 for (size_t i=0 ; i<count ; i++) {
793 const sp<LayerBase>& layer(currentLayers[i]);
794 if (layer->onPreComposition()) {
795 needInvalidate = true;
796 }
797 }
798 if (needInvalidate) {
799 signalLayerUpdate();
800 }
801}
802
803
Mathias Agopiana350ff92010-08-10 17:14:02 -0700804void SurfaceFlinger::handleWorkList()
805{
806 mHwWorkListDirty = false;
807 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
808 if (hwc.initCheck() == NO_ERROR) {
809 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
810 const size_t count = currentLayers.size();
811 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700812 hwc_layer_t* const cur(hwc.getLayers());
813 for (size_t i=0 ; cur && i<count ; i++) {
814 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700815 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700816 cur[i].compositionType = HWC_FRAMEBUFFER;
817 cur[i].flags |= HWC_SKIP_LAYER;
818 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700819 }
820 }
821}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700822
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823void SurfaceFlinger::handleRepaint()
824{
Mathias Agopian841cde52012-03-01 15:44:37 -0800825 ATRACE_CALL();
826
Mathias Agopianb8a55602009-06-26 19:06:36 -0700827 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700828 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829
Glenn Kasten99ed2242011-12-15 09:51:17 -0800830 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 debugFlashRegions();
832 }
833
Mathias Agopianb8a55602009-06-26 19:06:36 -0700834 // set the frame buffer
835 const DisplayHardware& hw(graphicPlane(0).displayHardware());
836 glMatrixMode(GL_MODELVIEW);
837 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838
839 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700840 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
842 // takes a rectangle, we must make sure to update that whole
843 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700844 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700846 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700847 // We need to redraw the rectangle that will be updated
848 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700849 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700850 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700851 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700852 } else {
853 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700855 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 }
857 }
858
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700859 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 composeSurfaces(mDirtyRegion);
861
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700862 // update the swap region and clear the dirty region
863 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 mDirtyRegion.clear();
865}
866
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700867void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700869 const DisplayHardware& hw(graphicPlane(0).displayHardware());
870 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700871 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700872 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700873 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700874 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700875
Mathias Agopianf384cc32011-09-08 18:31:55 -0700876 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
877 size_t count = layers.size();
878
Steve Blocke6f43dd2012-01-06 19:20:56 +0000879 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700880 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
881 hwc.getNumLayers(), count);
882
883 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700884 if (hwc.initCheck() == NO_ERROR) {
885 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
886 }
Mathias Agopian45721772010-08-12 15:03:26 -0700887
888 /*
889 * update the per-frame h/w composer data for each layer
890 * and build the transparent region of the FB
891 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700892 for (size_t i=0 ; i<count ; i++) {
893 const sp<LayerBase>& layer(layers[i]);
894 layer->setPerFrameData(&cur[i]);
895 }
896 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000897 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700898}
Mathias Agopian45721772010-08-12 15:03:26 -0700899
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900void SurfaceFlinger::composeSurfaces(const Region& dirty)
901{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700902 const DisplayHardware& hw(graphicPlane(0).displayHardware());
903 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700904 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700905
906 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700907 if (!cur || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700908 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700909
Mathias Agopianb9494d52012-04-18 02:28:45 -0700910 if (hwc.getLayerCount(HWC_OVERLAY)) {
911 // when using overlays, we assume a fully transparent framebuffer
912 // NOTE: we could reduce how much we need to clear, for instance
913 // remove where there are opaque FB layers. however, on some
914 // GPUs doing a "clean slate" glClear might be more efficient.
915 // We'll revisit later if needed.
916 glClearColor(0, 0, 0, 0);
917 glClear(GL_COLOR_BUFFER_BIT);
918 } else {
919 // screen is already cleared here
920 if (!mWormholeRegion.isEmpty()) {
921 // can happen with SurfaceView
922 drawWormhole();
923 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700924 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700925
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700926 /*
927 * and then, render the layers targeted at the framebuffer
928 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700929
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700930 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
931 const size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700932
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700933 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700934 const sp<LayerBase>& layer(layers[i]);
935 const Region clip(dirty.intersect(layer->visibleRegionScreen));
936 if (!clip.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700937 if (cur && (cur[i].compositionType == HWC_OVERLAY)) {
938 if (i && (cur[i].hints & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700939 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700940 // never clear the very first layer since we're
941 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700942 layer->clearWithOpenGL(clip);
943 }
944 continue;
945 }
946 // render the layer
947 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700948 }
949 }
950 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951}
952
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953void SurfaceFlinger::debugFlashRegions()
954{
Mathias Agopian0a917752010-06-14 21:20:00 -0700955 const DisplayHardware& hw(graphicPlane(0).displayHardware());
956 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700957 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700958 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700959 return;
960 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700961
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700962 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700963 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
964 mDirtyRegion.bounds() : hw.bounds());
965 composeSurfaces(repaint);
966 }
967
Mathias Agopianc492e672011-10-18 14:49:27 -0700968 glDisable(GL_TEXTURE_EXTERNAL_OES);
969 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
Mathias Agopian0926f502009-05-04 14:17:04 -0700972 static int toggle = 0;
973 toggle = 1 - toggle;
974 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700975 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700976 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700977 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700978 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979
Mathias Agopian20f68782009-05-11 00:03:41 -0700980 Region::const_iterator it = mDirtyRegion.begin();
981 Region::const_iterator const end = mDirtyRegion.end();
982 while (it != end) {
983 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700985 { r.left, height - r.top },
986 { r.left, height - r.bottom },
987 { r.right, height - r.bottom },
988 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 };
990 glVertexPointer(2, GL_FLOAT, 0, vertices);
991 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
992 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700993
Mathias Agopian0656a682011-09-20 17:22:44 -0700994 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995
996 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700997 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998}
999
1000void SurfaceFlinger::drawWormhole() const
1001{
1002 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1003 if (region.isEmpty())
1004 return;
1005
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001006 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001007 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001008 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001009 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001010
1011 GLfloat vertices[4][2];
1012 glVertexPointer(2, GL_FLOAT, 0, vertices);
1013 Region::const_iterator it = region.begin();
1014 Region::const_iterator const end = region.end();
1015 while (it != end) {
1016 const Rect& r = *it++;
1017 vertices[0][0] = r.left;
1018 vertices[0][1] = r.top;
1019 vertices[1][0] = r.right;
1020 vertices[1][1] = r.top;
1021 vertices[2][0] = r.right;
1022 vertices[2][1] = r.bottom;
1023 vertices[3][0] = r.left;
1024 vertices[3][1] = r.bottom;
1025 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1026 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027}
1028
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001029status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030{
1031 Mutex::Autolock _l(mStateLock);
1032 addLayer_l(layer);
1033 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1034 return NO_ERROR;
1035}
1036
Mathias Agopian96f08192010-06-02 23:28:45 -07001037status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1038{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001039 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001040 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1041}
1042
1043ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1044 const sp<LayerBaseClient>& lbc)
1045{
Mathias Agopian96f08192010-06-02 23:28:45 -07001046 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001047 size_t name = client->attachLayer(lbc);
1048
1049 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001050
1051 // add this layer to the current state list
1052 addLayer_l(lbc);
1053
Mathias Agopian4f113742011-05-03 16:21:41 -07001054 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001055}
1056
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001057status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
1059 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001060 status_t err = purgatorizeLayer_l(layer);
1061 if (err == NO_ERROR)
1062 setTransactionFlags(eTransactionNeeded);
1063 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064}
1065
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001068 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1069 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001070 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001071 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1073 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001074 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 return NO_ERROR;
1076 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001077 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078}
1079
Mathias Agopian9a112062009-04-17 19:36:26 -07001080status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1081{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001082 // First add the layer to the purgatory list, which makes sure it won't
1083 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001084 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001085 if (err >= 0) {
1086 mLayerPurgatory.add(layerBase);
1087 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001088
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001089 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001090
Mathias Agopian3d579642009-06-04 18:46:21 -07001091 // it's possible that we don't find a layer, because it might
1092 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001093 // from the user because there is a race between Client::destroySurface(),
1094 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001095 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1096}
1097
Mathias Agopian96f08192010-06-02 23:28:45 -07001098status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099{
Mathias Agopian96f08192010-06-02 23:28:45 -07001100 layer->forceVisibilityTransaction();
1101 setTransactionFlags(eTraversalNeeded);
1102 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103}
1104
Mathias Agopiandea20b12011-05-03 17:04:02 -07001105uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1106{
1107 return android_atomic_release_load(&mTransactionFlags);
1108}
1109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1111{
1112 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1113}
1114
Mathias Agopianbb641242010-05-18 17:06:55 -07001115uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116{
1117 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1118 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001119 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 }
1121 return old;
1122}
1123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001125void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001126 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001127 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001128
Jamie Gennis28378392011-10-12 17:39:00 -07001129 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001130 if (mCurrentState.orientation != orientation) {
1131 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1132 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001133 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001134 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001135 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001136 orientation);
1137 }
1138 }
1139
Mathias Agopian698c0872011-06-28 19:09:31 -07001140 const size_t count = state.size();
1141 for (size_t i=0 ; i<count ; i++) {
1142 const ComposerState& s(state[i]);
1143 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001144 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001145 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001146
Mathias Agopian386aa982011-11-07 21:58:03 -08001147 if (transactionFlags) {
1148 // this triggers the transaction
1149 setTransactionFlags(transactionFlags);
1150
1151 // if this is a synchronous transaction, wait for it to take effect
1152 // before returning.
1153 if (flags & eSynchronous) {
1154 mTransationPending = true;
1155 }
1156 while (mTransationPending) {
1157 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1158 if (CC_UNLIKELY(err != NO_ERROR)) {
1159 // just in case something goes wrong in SF, return to the
1160 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001161 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001162 mTransationPending = false;
1163 break;
1164 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001165 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 }
1167}
1168
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001169sp<ISurface> SurfaceFlinger::createSurface(
1170 ISurfaceComposerClient::surface_data_t* params,
1171 const String8& name,
1172 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1174 uint32_t flags)
1175{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001176 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001177 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001178
1179 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001180 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001181 int(w), int(h));
1182 return surfaceHandle;
1183 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001184
Mathias Agopianff615cc2012-02-24 14:58:36 -08001185 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001186 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 switch (flags & eFXSurfaceMask) {
1188 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001189 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1190 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191 break;
1192 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001193 // for now we treat Blur as Dim, until we can implement it
1194 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001196 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001198 case eFXSurfaceScreenshot:
1199 layer = createScreenshotSurface(client, d, w, h, flags);
1200 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 }
1202
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001203 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001204 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001205 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001206 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001209 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001211 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212 if (normalLayer != 0) {
1213 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001214 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001215 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001216 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001217
Mathias Agopian96f08192010-06-02 23:28:45 -07001218 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 }
1220
1221 return surfaceHandle;
1222}
1223
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001224sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001225 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001226 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001227 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228{
1229 // initialize the surfaces
1230 switch (format) { // TODO: take h/w into account
1231 case PIXEL_FORMAT_TRANSPARENT:
1232 case PIXEL_FORMAT_TRANSLUCENT:
1233 format = PIXEL_FORMAT_RGBA_8888;
1234 break;
1235 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001236#ifdef NO_RGBX_8888
1237 format = PIXEL_FORMAT_RGB_565;
1238#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001239 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001240#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 break;
1242 }
1243
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001244#ifdef NO_RGBX_8888
1245 if (format == PIXEL_FORMAT_RGBX_8888)
1246 format = PIXEL_FORMAT_RGBA_8888;
1247#endif
1248
Mathias Agopian96f08192010-06-02 23:28:45 -07001249 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001250 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001251 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001252 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001253 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 }
1255 return layer;
1256}
1257
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001258sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001259 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001260 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261{
Mathias Agopian96f08192010-06-02 23:28:45 -07001262 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001263 return layer;
1264}
1265
1266sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1267 const sp<Client>& client, DisplayID display,
1268 uint32_t w, uint32_t h, uint32_t flags)
1269{
1270 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 return layer;
1272}
1273
Mathias Agopian96f08192010-06-02 23:28:45 -07001274status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275{
Mathias Agopian9a112062009-04-17 19:36:26 -07001276 /*
1277 * called by the window manager, when a surface should be marked for
1278 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001279 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001280 * The surface is removed from the current and drawing lists, but placed
1281 * in the purgatory queue, so it's not destroyed right-away (we need
1282 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001283 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001284
Mathias Agopian48d819a2009-09-10 19:41:18 -07001285 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001286 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001288
Mathias Agopian48d819a2009-09-10 19:41:18 -07001289 if (layer != 0) {
1290 err = purgatorizeLayer_l(layer);
1291 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001292 setTransactionFlags(eTransactionNeeded);
1293 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001294 }
1295 return err;
1296}
1297
Mathias Agopianca4d3602011-05-19 15:38:14 -07001298status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001299{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001300 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001301 status_t err = NO_ERROR;
1302 sp<LayerBaseClient> l(layer.promote());
1303 if (l != NULL) {
1304 Mutex::Autolock _l(mStateLock);
1305 err = removeLayer_l(l);
1306 if (err == NAME_NOT_FOUND) {
1307 // The surface wasn't in the current list, which means it was
1308 // removed already, which means it is in the purgatory,
1309 // and need to be removed from there.
1310 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001311 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001312 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001313 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001314 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001315 "error removing layer=%p (%s)", l.get(), strerror(-err));
1316 }
1317 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318}
1319
Mathias Agopian698c0872011-06-28 19:09:31 -07001320uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001321 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001322 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001325 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1326 if (layer != 0) {
1327 const uint32_t what = s.what;
1328 if (what & ePositionChanged) {
1329 if (layer->setPosition(s.x, s.y))
1330 flags |= eTraversalNeeded;
1331 }
1332 if (what & eLayerChanged) {
1333 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1334 if (layer->setLayer(s.z)) {
1335 mCurrentState.layersSortedByZ.removeAt(idx);
1336 mCurrentState.layersSortedByZ.add(layer);
1337 // we need traversal (state changed)
1338 // AND transaction (list changed)
1339 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 }
1341 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001342 if (what & eSizeChanged) {
1343 if (layer->setSize(s.w, s.h)) {
1344 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001345 }
1346 }
1347 if (what & eAlphaChanged) {
1348 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1349 flags |= eTraversalNeeded;
1350 }
1351 if (what & eMatrixChanged) {
1352 if (layer->setMatrix(s.matrix))
1353 flags |= eTraversalNeeded;
1354 }
1355 if (what & eTransparentRegionChanged) {
1356 if (layer->setTransparentRegionHint(s.transparentRegion))
1357 flags |= eTraversalNeeded;
1358 }
1359 if (what & eVisibilityChanged) {
1360 if (layer->setFlags(s.flags, s.mask))
1361 flags |= eTraversalNeeded;
1362 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001363 if (what & eCropChanged) {
1364 if (layer->setCrop(s.crop))
1365 flags |= eTraversalNeeded;
1366 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001368 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369}
1370
Mathias Agopianb60314a2012-04-10 22:09:54 -07001371// ---------------------------------------------------------------------------
1372
1373void SurfaceFlinger::onScreenAcquired() {
1374 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1375 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001376 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001377 // this is a temporary work-around, eventually this should be called
1378 // by the power-manager
1379 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001380 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001381 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382}
1383
Mathias Agopianb60314a2012-04-10 22:09:54 -07001384void SurfaceFlinger::onScreenReleased() {
1385 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1386 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001387 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001388 hw.releaseScreen();
1389 // from this point on, SF will stop drawing
1390 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391}
1392
Mathias Agopianb60314a2012-04-10 22:09:54 -07001393void SurfaceFlinger::screenAcquired() {
1394 class MessageScreenAcquired : public MessageBase {
1395 SurfaceFlinger* flinger;
1396 public:
1397 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1398 virtual bool handler() {
1399 flinger->onScreenAcquired();
1400 return true;
1401 }
1402 };
1403 sp<MessageBase> msg = new MessageScreenAcquired(this);
1404 postMessageSync(msg);
1405}
1406
1407void SurfaceFlinger::screenReleased() {
1408 class MessageScreenReleased : public MessageBase {
1409 SurfaceFlinger* flinger;
1410 public:
1411 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1412 virtual bool handler() {
1413 flinger->onScreenReleased();
1414 return true;
1415 }
1416 };
1417 sp<MessageBase> msg = new MessageScreenReleased(this);
1418 postMessageSync(msg);
1419}
1420
1421// ---------------------------------------------------------------------------
1422
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1424{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001425 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 char buffer[SIZE];
1427 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001428
1429 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 snprintf(buffer, SIZE, "Permission Denial: "
1431 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1432 IPCThreadState::self()->getCallingPid(),
1433 IPCThreadState::self()->getCallingUid());
1434 result.append(buffer);
1435 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001436 // Try to get the main lock, but don't insist if we can't
1437 // (this would indicate SF is stuck, but we want to be able to
1438 // print something in dumpsys).
1439 int retry = 3;
1440 while (mStateLock.tryLock()<0 && --retry>=0) {
1441 usleep(1000000);
1442 }
1443 const bool locked(retry >= 0);
1444 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001445 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001446 "SurfaceFlinger appears to be unresponsive, "
1447 "dumping anyways (no locks held)\n");
1448 result.append(buffer);
1449 }
1450
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001451 bool dumpAll = true;
1452 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001453 size_t numArgs = args.size();
1454 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001455 if ((index < numArgs) &&
1456 (args[index] == String16("--list"))) {
1457 index++;
1458 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001459 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001460 }
1461
1462 if ((index < numArgs) &&
1463 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001464 index++;
1465 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001466 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001467 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001468
1469 if ((index < numArgs) &&
1470 (args[index] == String16("--latency-clear"))) {
1471 index++;
1472 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001473 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001474 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001476
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001477 if (dumpAll) {
1478 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001479 }
1480
Mathias Agopian9795c422009-08-26 16:36:26 -07001481 if (locked) {
1482 mStateLock.unlock();
1483 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 }
1485 write(fd, result.string(), result.size());
1486 return NO_ERROR;
1487}
1488
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001489void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1490 String8& result, char* buffer, size_t SIZE) const
1491{
1492 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1493 const size_t count = currentLayers.size();
1494 for (size_t i=0 ; i<count ; i++) {
1495 const sp<LayerBase>& layer(currentLayers[i]);
1496 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1497 result.append(buffer);
1498 }
1499}
1500
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001501void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1502 String8& result, char* buffer, size_t SIZE) const
1503{
1504 String8 name;
1505 if (index < args.size()) {
1506 name = String8(args[index]);
1507 index++;
1508 }
1509
1510 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1511 const size_t count = currentLayers.size();
1512 for (size_t i=0 ; i<count ; i++) {
1513 const sp<LayerBase>& layer(currentLayers[i]);
1514 if (name.isEmpty()) {
1515 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1516 result.append(buffer);
1517 }
1518 if (name.isEmpty() || (name == layer->getName())) {
1519 layer->dumpStats(result, buffer, SIZE);
1520 }
1521 }
1522}
1523
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001524void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1525 String8& result, char* buffer, size_t SIZE) const
1526{
1527 String8 name;
1528 if (index < args.size()) {
1529 name = String8(args[index]);
1530 index++;
1531 }
1532
1533 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1534 const size_t count = currentLayers.size();
1535 for (size_t i=0 ; i<count ; i++) {
1536 const sp<LayerBase>& layer(currentLayers[i]);
1537 if (name.isEmpty() || (name == layer->getName())) {
1538 layer->clearStats();
1539 }
1540 }
1541}
1542
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001543void SurfaceFlinger::dumpAllLocked(
1544 String8& result, char* buffer, size_t SIZE) const
1545{
1546 // figure out if we're stuck somewhere
1547 const nsecs_t now = systemTime();
1548 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1549 const nsecs_t inTransaction(mDebugInTransaction);
1550 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1551 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1552
1553 /*
1554 * Dump the visible layer list
1555 */
1556 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1557 const size_t count = currentLayers.size();
1558 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1559 result.append(buffer);
1560 for (size_t i=0 ; i<count ; i++) {
1561 const sp<LayerBase>& layer(currentLayers[i]);
1562 layer->dump(result, buffer, SIZE);
1563 }
1564
1565 /*
1566 * Dump the layers in the purgatory
1567 */
1568
1569 const size_t purgatorySize = mLayerPurgatory.size();
1570 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1571 result.append(buffer);
1572 for (size_t i=0 ; i<purgatorySize ; i++) {
1573 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1574 layer->shortDump(result, buffer, SIZE);
1575 }
1576
1577 /*
1578 * Dump SurfaceFlinger global state
1579 */
1580
1581 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1582 result.append(buffer);
1583
1584 const GLExtensions& extensions(GLExtensions::getInstance());
1585 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1586 extensions.getVendor(),
1587 extensions.getRenderer(),
1588 extensions.getVersion());
1589 result.append(buffer);
1590
1591 snprintf(buffer, SIZE, "EGL : %s\n",
1592 eglQueryString(graphicPlane(0).getEGLDisplay(),
1593 EGL_VERSION_HW_ANDROID));
1594 result.append(buffer);
1595
1596 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1597 result.append(buffer);
1598
1599 mWormholeRegion.dump(result, "WormholeRegion");
1600 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1601 snprintf(buffer, SIZE,
1602 " orientation=%d, canDraw=%d\n",
1603 mCurrentState.orientation, hw.canDraw());
1604 result.append(buffer);
1605 snprintf(buffer, SIZE,
1606 " last eglSwapBuffers() time: %f us\n"
1607 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001608 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001609 " refresh-rate : %f fps\n"
1610 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001611 " y-dpi : %f\n"
1612 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001613 mLastSwapBufferTime/1000.0,
1614 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001615 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001616 hw.getRefreshRate(),
1617 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001618 hw.getDpiY(),
1619 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001620 result.append(buffer);
1621
1622 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1623 inSwapBuffersDuration/1000.0);
1624 result.append(buffer);
1625
1626 snprintf(buffer, SIZE, " transaction time: %f us\n",
1627 inTransactionDuration/1000.0);
1628 result.append(buffer);
1629
1630 /*
1631 * VSYNC state
1632 */
1633 mEventThread->dump(result, buffer, SIZE);
1634
1635 /*
1636 * Dump HWComposer state
1637 */
1638 HWComposer& hwc(hw.getHwComposer());
1639 snprintf(buffer, SIZE, "h/w composer state:\n");
1640 result.append(buffer);
1641 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1642 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1643 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1644 result.append(buffer);
1645 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1646
1647 /*
1648 * Dump gralloc state
1649 */
1650 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1651 alloc.dump(result);
1652 hw.dump(result);
1653}
1654
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655status_t SurfaceFlinger::onTransact(
1656 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1657{
1658 switch (code) {
1659 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001660 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001663 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001664 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665 {
1666 // codes that require permission check
1667 IPCThreadState* ipc = IPCThreadState::self();
1668 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001669 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001670 if ((uid != AID_GRAPHICS) &&
1671 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001672 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001673 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1674 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001676 break;
1677 }
1678 case CAPTURE_SCREEN:
1679 {
1680 // codes that require permission check
1681 IPCThreadState* ipc = IPCThreadState::self();
1682 const int pid = ipc->getCallingPid();
1683 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001684 if ((uid != AID_GRAPHICS) &&
1685 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001686 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001687 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1688 return PERMISSION_DENIED;
1689 }
1690 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 }
1692 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001693
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1695 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001696 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001697 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001698 IPCThreadState* ipc = IPCThreadState::self();
1699 const int pid = ipc->getCallingPid();
1700 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001701 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001702 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 return PERMISSION_DENIED;
1704 }
1705 int n;
1706 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001707 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001708 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 return NO_ERROR;
1710 case 1002: // SHOW_UPDATES
1711 n = data.readInt32();
1712 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001713 invalidateHwcGeometry();
1714 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001717 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001718 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001720 case 1005:{ // force transaction
1721 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1722 return NO_ERROR;
1723 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001724 case 1006:{ // send empty update
1725 signalRefresh();
1726 return NO_ERROR;
1727 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001728 case 1008: // toggle use of hw composer
1729 n = data.readInt32();
1730 mDebugDisableHWC = n ? 1 : 0;
1731 invalidateHwcGeometry();
1732 repaintEverything();
1733 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001734 case 1009: // toggle use of transform hint
1735 n = data.readInt32();
1736 mDebugDisableTransformHint = n ? 1 : 0;
1737 invalidateHwcGeometry();
1738 repaintEverything();
1739 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001741 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 reply->writeInt32(0);
1743 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001744 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001745 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 return NO_ERROR;
1747 case 1013: {
1748 Mutex::Autolock _l(mStateLock);
1749 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1750 reply->writeInt32(hw.getPageFlipCount());
1751 }
1752 return NO_ERROR;
1753 }
1754 }
1755 return err;
1756}
1757
Mathias Agopian53331da2011-08-22 21:44:41 -07001758void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001759 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001760 const Rect bounds(hw.getBounds());
1761 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001762 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001763}
1764
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001765void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1766 Mutex::Autolock _l(mInvalidateLock);
1767 mInvalidateRegion = reg;
1768}
1769
1770Region SurfaceFlinger::getAndClearInvalidateRegion() {
1771 Mutex::Autolock _l(mInvalidateLock);
1772 Region reg(mInvalidateRegion);
1773 mInvalidateRegion.clear();
1774 return reg;
1775}
1776
Mathias Agopian59119e62010-10-11 12:37:43 -07001777// ---------------------------------------------------------------------------
1778
Mathias Agopian118d0242011-10-13 16:02:48 -07001779status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1780 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1781{
1782 Mutex::Autolock _l(mStateLock);
1783 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1784}
1785
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001786status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1787 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001788{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001789 ATRACE_CALL();
1790
Mathias Agopian59119e62010-10-11 12:37:43 -07001791 if (!GLExtensions::getInstance().haveFramebufferObject())
1792 return INVALID_OPERATION;
1793
1794 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001795 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 const uint32_t hw_w = hw.getWidth();
1797 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001798 GLfloat u = 1;
1799 GLfloat v = 1;
1800
1801 // make sure to clear all GL error flags
1802 while ( glGetError() != GL_NO_ERROR ) ;
1803
1804 // create a FBO
1805 GLuint name, tname;
1806 glGenTextures(1, &tname);
1807 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001808 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1809 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001810 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1811 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001812 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001813 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001814 GLint tw = (2 << (31 - clz(hw_w)));
1815 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001816 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1817 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001818 u = GLfloat(hw_w) / tw;
1819 v = GLfloat(hw_h) / th;
1820 }
1821 glGenFramebuffersOES(1, &name);
1822 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001823 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1824 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001825
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001826 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001827 glDisable(GL_TEXTURE_EXTERNAL_OES);
1828 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001829 glClearColor(0,0,0,1);
1830 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001831 glMatrixMode(GL_MODELVIEW);
1832 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001833 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1834 const size_t count = layers.size();
1835 for (size_t i=0 ; i<count ; ++i) {
1836 const sp<LayerBase>& layer(layers[i]);
1837 layer->drawForSreenShot();
1838 }
1839
Mathias Agopian118d0242011-10-13 16:02:48 -07001840 hw.compositionComplete();
1841
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 // back to main framebuffer
1843 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001844 glDeleteFramebuffersOES(1, &name);
1845
1846 *textureName = tname;
1847 *uOut = u;
1848 *vOut = v;
1849 return NO_ERROR;
1850}
1851
1852// ---------------------------------------------------------------------------
1853
Mathias Agopianed9807b2012-05-18 14:18:08 -07001854class VSyncWaiter {
1855 DisplayEventReceiver::Event buffer[4];
1856 sp<Looper> looper;
1857 sp<IDisplayEventConnection> events;
1858 sp<BitTube> eventTube;
1859public:
1860 VSyncWaiter(const sp<EventThread>& eventThread) {
1861 looper = new Looper(true);
1862 events = eventThread->createEventConnection();
1863 eventTube = events->getDataChannel();
1864 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1865 events->requestNextVsync();
1866 }
1867
1868 void wait() {
1869 ssize_t n;
1870
1871 looper->pollOnce(-1);
1872 // we don't handle any errors here, it doesn't matter
1873 // and we don't want to take the risk to get stuck.
1874
1875 // drain the events...
1876 while ((n = DisplayEventReceiver::getEvents(
1877 eventTube, buffer, 4)) > 0) ;
1878
1879 events->requestNextVsync();
1880 }
1881};
1882
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001883status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1884{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001885 // get screen geometry
1886 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1887 const uint32_t hw_w = hw.getWidth();
1888 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001889 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001890
1891 GLfloat u, v;
1892 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001893 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001894 if (result != NO_ERROR) {
1895 return result;
1896 }
1897
1898 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001899 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001900 glBindTexture(GL_TEXTURE_2D, tname);
1901 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1902 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1903 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001904 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1905 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001906 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1907 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1908 glVertexPointer(2, GL_FLOAT, 0, vtx);
1909
Mathias Agopianffcf4652011-07-07 17:30:31 -07001910 /*
1911 * Texture coordinate mapping
1912 *
1913 * u
1914 * 1 +----------+---+
1915 * | | | | image is inverted
1916 * | V | | w.r.t. the texture
1917 * 1-v +----------+ | coordinates
1918 * | |
1919 * | |
1920 * | |
1921 * 0 +--------------+
1922 * 0 1
1923 *
1924 */
1925
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001926 class s_curve_interpolator {
1927 const float nbFrames, s, v;
1928 public:
1929 s_curve_interpolator(int nbFrames, float s)
1930 : nbFrames(1.0f / (nbFrames-1)), s(s),
1931 v(1.0f + expf(-s + 0.5f*s)) {
1932 }
1933 float operator()(int f) {
1934 const float x = f * nbFrames;
1935 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1936 }
1937 };
1938
1939 class v_stretch {
1940 const GLfloat hw_w, hw_h;
1941 public:
1942 v_stretch(uint32_t hw_w, uint32_t hw_h)
1943 : hw_w(hw_w), hw_h(hw_h) {
1944 }
1945 void operator()(GLfloat* vtx, float v) {
1946 const GLfloat w = hw_w + (hw_w * v);
1947 const GLfloat h = hw_h - (hw_h * v);
1948 const GLfloat x = (hw_w - w) * 0.5f;
1949 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001950 vtx[0] = x; vtx[1] = y;
1951 vtx[2] = x; vtx[3] = y + h;
1952 vtx[4] = x + w; vtx[5] = y + h;
1953 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001954 }
1955 };
1956
1957 class h_stretch {
1958 const GLfloat hw_w, hw_h;
1959 public:
1960 h_stretch(uint32_t hw_w, uint32_t hw_h)
1961 : hw_w(hw_w), hw_h(hw_h) {
1962 }
1963 void operator()(GLfloat* vtx, float v) {
1964 const GLfloat w = hw_w - (hw_w * v);
1965 const GLfloat h = 1.0f;
1966 const GLfloat x = (hw_w - w) * 0.5f;
1967 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001968 vtx[0] = x; vtx[1] = y;
1969 vtx[2] = x; vtx[3] = y + h;
1970 vtx[4] = x + w; vtx[5] = y + h;
1971 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 }
1973 };
1974
Mathias Agopianed9807b2012-05-18 14:18:08 -07001975 VSyncWaiter vsync(mEventThread);
1976
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001978 char value[PROPERTY_VALUE_MAX];
1979 property_get("debug.sf.electron_frames", value, "24");
1980 int nbFrames = (atoi(value) + 1) >> 1;
1981 if (nbFrames <= 0) // just in case
1982 nbFrames = 24;
1983
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001984 s_curve_interpolator itr(nbFrames, 7.5f);
1985 s_curve_interpolator itg(nbFrames, 8.0f);
1986 s_curve_interpolator itb(nbFrames, 8.5f);
1987
1988 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001989
1990 glMatrixMode(GL_TEXTURE);
1991 glLoadIdentity();
1992 glMatrixMode(GL_MODELVIEW);
1993 glLoadIdentity();
1994
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001995 glEnable(GL_BLEND);
1996 glBlendFunc(GL_ONE, GL_ONE);
1997 for (int i=0 ; i<nbFrames ; i++) {
1998 float x, y, w, h;
1999 const float vr = itr(i);
2000 const float vg = itg(i);
2001 const float vb = itb(i);
2002
Mathias Agopianed9807b2012-05-18 14:18:08 -07002003 // wait for vsync
2004 vsync.wait();
2005
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002006 // clear screen
2007 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002008 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002009 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002010
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002011 // draw the red plane
2012 vverts(vtx, vr);
2013 glColorMask(1,0,0,1);
2014 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002015
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002016 // draw the green plane
2017 vverts(vtx, vg);
2018 glColorMask(0,1,0,1);
2019 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002020
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021 // draw the blue plane
2022 vverts(vtx, vb);
2023 glColorMask(0,0,1,1);
2024 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002025
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002027 glDisable(GL_TEXTURE_2D);
2028 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002029 glColor4f(vg, vg, vg, 1);
2030 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2031 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002032 }
2033
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034 h_stretch hverts(hw_w, hw_h);
2035 glDisable(GL_BLEND);
2036 glDisable(GL_TEXTURE_2D);
2037 glColorMask(1,1,1,1);
2038 for (int i=0 ; i<nbFrames ; i++) {
2039 const float v = itg(i);
2040 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002041
2042 // wait for vsync
2043 vsync.wait();
2044
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 glClear(GL_COLOR_BUFFER_BIT);
2046 glColor4f(1-v, 1-v, 1-v, 1);
2047 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2048 hw.flip(screenBounds);
2049 }
2050
2051 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002052 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2053 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002054 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002055 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002056 return NO_ERROR;
2057}
2058
2059status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2060{
2061 status_t result = PERMISSION_DENIED;
2062
2063 if (!GLExtensions::getInstance().haveFramebufferObject())
2064 return INVALID_OPERATION;
2065
2066
2067 // get screen geometry
2068 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2069 const uint32_t hw_w = hw.getWidth();
2070 const uint32_t hw_h = hw.getHeight();
2071 const Region screenBounds(hw.bounds());
2072
2073 GLfloat u, v;
2074 GLuint tname;
2075 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2076 if (result != NO_ERROR) {
2077 return result;
2078 }
2079
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002080 GLfloat vtx[8];
2081 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002082 glBindTexture(GL_TEXTURE_2D, tname);
2083 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2084 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2085 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002086 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2087 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002088 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2089 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2090 glVertexPointer(2, GL_FLOAT, 0, vtx);
2091
2092 class s_curve_interpolator {
2093 const float nbFrames, s, v;
2094 public:
2095 s_curve_interpolator(int nbFrames, float s)
2096 : nbFrames(1.0f / (nbFrames-1)), s(s),
2097 v(1.0f + expf(-s + 0.5f*s)) {
2098 }
2099 float operator()(int f) {
2100 const float x = f * nbFrames;
2101 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2102 }
2103 };
2104
2105 class v_stretch {
2106 const GLfloat hw_w, hw_h;
2107 public:
2108 v_stretch(uint32_t hw_w, uint32_t hw_h)
2109 : hw_w(hw_w), hw_h(hw_h) {
2110 }
2111 void operator()(GLfloat* vtx, float v) {
2112 const GLfloat w = hw_w + (hw_w * v);
2113 const GLfloat h = hw_h - (hw_h * v);
2114 const GLfloat x = (hw_w - w) * 0.5f;
2115 const GLfloat y = (hw_h - h) * 0.5f;
2116 vtx[0] = x; vtx[1] = y;
2117 vtx[2] = x; vtx[3] = y + h;
2118 vtx[4] = x + w; vtx[5] = y + h;
2119 vtx[6] = x + w; vtx[7] = y;
2120 }
2121 };
2122
2123 class h_stretch {
2124 const GLfloat hw_w, hw_h;
2125 public:
2126 h_stretch(uint32_t hw_w, uint32_t hw_h)
2127 : hw_w(hw_w), hw_h(hw_h) {
2128 }
2129 void operator()(GLfloat* vtx, float v) {
2130 const GLfloat w = hw_w - (hw_w * v);
2131 const GLfloat h = 1.0f;
2132 const GLfloat x = (hw_w - w) * 0.5f;
2133 const GLfloat y = (hw_h - h) * 0.5f;
2134 vtx[0] = x; vtx[1] = y;
2135 vtx[2] = x; vtx[3] = y + h;
2136 vtx[4] = x + w; vtx[5] = y + h;
2137 vtx[6] = x + w; vtx[7] = y;
2138 }
2139 };
2140
Mathias Agopianed9807b2012-05-18 14:18:08 -07002141 VSyncWaiter vsync(mEventThread);
2142
Mathias Agopiana6546e52010-10-14 12:33:07 -07002143 // the full animation is 12 frames
2144 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002145 s_curve_interpolator itr(nbFrames, 7.5f);
2146 s_curve_interpolator itg(nbFrames, 8.0f);
2147 s_curve_interpolator itb(nbFrames, 8.5f);
2148
2149 h_stretch hverts(hw_w, hw_h);
2150 glDisable(GL_BLEND);
2151 glDisable(GL_TEXTURE_2D);
2152 glColorMask(1,1,1,1);
2153 for (int i=nbFrames-1 ; i>=0 ; i--) {
2154 const float v = itg(i);
2155 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002156
2157 // wait for vsync
2158 vsync.wait();
2159
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002160 glClear(GL_COLOR_BUFFER_BIT);
2161 glColor4f(1-v, 1-v, 1-v, 1);
2162 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2163 hw.flip(screenBounds);
2164 }
2165
Mathias Agopiana6546e52010-10-14 12:33:07 -07002166 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002167 v_stretch vverts(hw_w, hw_h);
2168 glEnable(GL_BLEND);
2169 glBlendFunc(GL_ONE, GL_ONE);
2170 for (int i=nbFrames-1 ; i>=0 ; i--) {
2171 float x, y, w, h;
2172 const float vr = itr(i);
2173 const float vg = itg(i);
2174 const float vb = itb(i);
2175
Mathias Agopianed9807b2012-05-18 14:18:08 -07002176 // wait for vsync
2177 vsync.wait();
2178
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179 // clear screen
2180 glColorMask(1,1,1,1);
2181 glClear(GL_COLOR_BUFFER_BIT);
2182 glEnable(GL_TEXTURE_2D);
2183
2184 // draw the red plane
2185 vverts(vtx, vr);
2186 glColorMask(1,0,0,1);
2187 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2188
2189 // draw the green plane
2190 vverts(vtx, vg);
2191 glColorMask(0,1,0,1);
2192 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2193
2194 // draw the blue plane
2195 vverts(vtx, vb);
2196 glColorMask(0,0,1,1);
2197 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2198
2199 hw.flip(screenBounds);
2200 }
2201
2202 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002204 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002205 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002206 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002207
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002208 return NO_ERROR;
2209}
2210
2211// ---------------------------------------------------------------------------
2212
Mathias Agopianabd671a2010-10-14 14:54:06 -07002213status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002214{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002215 ATRACE_CALL();
2216
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002217 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2218 if (!hw.canDraw()) {
2219 // we're already off
2220 return NO_ERROR;
2221 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002222
2223 // turn off hwc while we're doing the animation
2224 hw.getHwComposer().disable();
2225 // and make sure to turn it back on (if needed) next time we compose
2226 invalidateHwcGeometry();
2227
Mathias Agopianabd671a2010-10-14 14:54:06 -07002228 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2229 electronBeamOffAnimationImplLocked();
2230 }
2231
2232 // always clear the whole screen at the end of the animation
2233 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002234 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002235 hw.flip( Region(hw.bounds()) );
2236
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002237 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002238}
2239
2240status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2241{
Mathias Agopian59119e62010-10-11 12:37:43 -07002242 class MessageTurnElectronBeamOff : public MessageBase {
2243 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002244 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002245 status_t result;
2246 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002247 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2248 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002249 }
2250 status_t getResult() const {
2251 return result;
2252 }
2253 virtual bool handler() {
2254 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002256 return true;
2257 }
2258 };
2259
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002261 status_t res = postMessageSync(msg);
2262 if (res == NO_ERROR) {
2263 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264
2265 // work-around: when the power-manager calls us we activate the
2266 // animation. eventually, the "on" animation will be called
2267 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002268 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002269 }
2270 return res;
2271}
2272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002273// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002274
Mathias Agopianabd671a2010-10-14 14:54:06 -07002275status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002276{
2277 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2278 if (hw.canDraw()) {
2279 // we're already on
2280 return NO_ERROR;
2281 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002282 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2283 electronBeamOnAnimationImplLocked();
2284 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002285
2286 // make sure to redraw the whole screen when the animation is done
2287 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002288 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002289
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002290 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002291}
2292
2293status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2294{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002295 class MessageTurnElectronBeamOn : public MessageBase {
2296 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002297 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002298 status_t result;
2299 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002300 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2301 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002302 }
2303 status_t getResult() const {
2304 return result;
2305 }
2306 virtual bool handler() {
2307 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002308 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002309 return true;
2310 }
2311 };
2312
Mathias Agopianabd671a2010-10-14 14:54:06 -07002313 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002314 return NO_ERROR;
2315}
2316
2317// ---------------------------------------------------------------------------
2318
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2320 sp<IMemoryHeap>* heap,
2321 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002322 uint32_t sw, uint32_t sh,
2323 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002325 ATRACE_CALL();
2326
Mathias Agopian74c40c02010-09-29 13:02:36 -07002327 status_t result = PERMISSION_DENIED;
2328
2329 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002330 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 return BAD_VALUE;
2332
2333 if (!GLExtensions::getInstance().haveFramebufferObject())
2334 return INVALID_OPERATION;
2335
2336 // get screen geometry
2337 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2338 const uint32_t hw_w = hw.getWidth();
2339 const uint32_t hw_h = hw.getHeight();
2340
2341 if ((sw > hw_w) || (sh > hw_h))
2342 return BAD_VALUE;
2343
2344 sw = (!sw) ? hw_w : sw;
2345 sh = (!sh) ? hw_h : sh;
2346 const size_t size = sw * sh * 4;
2347
Steve Block9d453682011-12-20 16:23:08 +00002348 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002349 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002350
Mathias Agopian74c40c02010-09-29 13:02:36 -07002351 // make sure to clear all GL error flags
2352 while ( glGetError() != GL_NO_ERROR ) ;
2353
2354 // create a FBO
2355 GLuint name, tname;
2356 glGenRenderbuffersOES(1, &tname);
2357 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2358 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002359
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360 glGenFramebuffersOES(1, &name);
2361 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2362 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2363 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2364
2365 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002366
Mathias Agopian74c40c02010-09-29 13:02:36 -07002367 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2368
2369 // invert everything, b/c glReadPixel() below will invert the FB
2370 glViewport(0, 0, sw, sh);
2371 glMatrixMode(GL_PROJECTION);
2372 glPushMatrix();
2373 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002374 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 glMatrixMode(GL_MODELVIEW);
2376
2377 // redraw the screen entirely...
2378 glClearColor(0,0,0,1);
2379 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002380
Jamie Gennis9575f602011-10-07 14:51:16 -07002381 const LayerVector& layers(mDrawingState.layersSortedByZ);
2382 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002383 for (size_t i=0 ; i<count ; ++i) {
2384 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002385 const uint32_t flags = layer->drawingState().flags;
2386 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2387 const uint32_t z = layer->drawingState().z;
2388 if (z >= minLayerZ && z <= maxLayerZ) {
2389 layer->drawForSreenShot();
2390 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002391 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 }
2393
Mathias Agopian74c40c02010-09-29 13:02:36 -07002394 // check for errors and return screen capture
2395 if (glGetError() != GL_NO_ERROR) {
2396 // error while rendering
2397 result = INVALID_OPERATION;
2398 } else {
2399 // allocate shared memory large enough to hold the
2400 // screen capture
2401 sp<MemoryHeapBase> base(
2402 new MemoryHeapBase(size, 0, "screen-capture") );
2403 void* const ptr = base->getBase();
2404 if (ptr) {
2405 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002406 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002407 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2408 if (glGetError() == GL_NO_ERROR) {
2409 *heap = base;
2410 *w = sw;
2411 *h = sh;
2412 *f = PIXEL_FORMAT_RGBA_8888;
2413 result = NO_ERROR;
2414 }
2415 } else {
2416 result = NO_MEMORY;
2417 }
2418 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 glViewport(0, 0, hw_w, hw_h);
2420 glMatrixMode(GL_PROJECTION);
2421 glPopMatrix();
2422 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 } else {
2424 result = BAD_VALUE;
2425 }
2426
2427 // release FBO resources
2428 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2429 glDeleteRenderbuffersOES(1, &tname);
2430 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002431
2432 hw.compositionComplete();
2433
Steve Block9d453682011-12-20 16:23:08 +00002434 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002435
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 return result;
2437}
2438
2439
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2441 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002442 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002443 uint32_t sw, uint32_t sh,
2444 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002445{
2446 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002447 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002448 return BAD_VALUE;
2449
2450 if (!GLExtensions::getInstance().haveFramebufferObject())
2451 return INVALID_OPERATION;
2452
2453 class MessageCaptureScreen : public MessageBase {
2454 SurfaceFlinger* flinger;
2455 DisplayID dpy;
2456 sp<IMemoryHeap>* heap;
2457 uint32_t* w;
2458 uint32_t* h;
2459 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002460 uint32_t sw;
2461 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002462 uint32_t minLayerZ;
2463 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002464 status_t result;
2465 public:
2466 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002467 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002468 uint32_t sw, uint32_t sh,
2469 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002470 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002471 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2472 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2473 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002474 {
2475 }
2476 status_t getResult() const {
2477 return result;
2478 }
2479 virtual bool handler() {
2480 Mutex::Autolock _l(flinger->mStateLock);
2481
2482 // if we have secure windows, never allow the screen capture
2483 if (flinger->mSecureFrameBuffer)
2484 return true;
2485
Mathias Agopian74c40c02010-09-29 13:02:36 -07002486 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002487 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002488
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489 return true;
2490 }
2491 };
2492
2493 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002494 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002495 status_t res = postMessageSync(msg);
2496 if (res == NO_ERROR) {
2497 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2498 }
2499 return res;
2500}
2501
2502// ---------------------------------------------------------------------------
2503
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002504sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2505{
2506 sp<Layer> result;
2507 Mutex::Autolock _l(mStateLock);
2508 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2509 return result;
2510}
2511
2512// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002513
Mathias Agopian96f08192010-06-02 23:28:45 -07002514Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002515 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517}
2518
Mathias Agopian96f08192010-06-02 23:28:45 -07002519Client::~Client()
2520{
Mathias Agopian96f08192010-06-02 23:28:45 -07002521 const size_t count = mLayers.size();
2522 for (size_t i=0 ; i<count ; i++) {
2523 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2524 if (layer != 0) {
2525 mFlinger->removeLayer(layer);
2526 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 }
2528}
2529
Mathias Agopian96f08192010-06-02 23:28:45 -07002530status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002531 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002533
Mathias Agopian4f113742011-05-03 16:21:41 -07002534size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002535{
Mathias Agopian4f113742011-05-03 16:21:41 -07002536 Mutex::Autolock _l(mLock);
2537 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002538 mLayers.add(name, layer);
2539 return name;
2540}
2541
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002542void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002543{
Mathias Agopian4f113742011-05-03 16:21:41 -07002544 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002545 // we do a linear search here, because this doesn't happen often
2546 const size_t count = mLayers.size();
2547 for (size_t i=0 ; i<count ; i++) {
2548 if (mLayers.valueAt(i) == layer) {
2549 mLayers.removeItemsAt(i, 1);
2550 break;
2551 }
2552 }
2553}
Mathias Agopian4f113742011-05-03 16:21:41 -07002554sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2555{
2556 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002557 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002558 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002559 if (layer != 0) {
2560 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002561 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002562 }
2563 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002564}
2565
Mathias Agopiana67932f2011-04-20 14:20:59 -07002566
2567status_t Client::onTransact(
2568 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2569{
2570 // these must be checked
2571 IPCThreadState* ipc = IPCThreadState::self();
2572 const int pid = ipc->getCallingPid();
2573 const int uid = ipc->getCallingUid();
2574 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002575 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002576 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002577 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002578 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002579 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002580 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2581 return PERMISSION_DENIED;
2582 }
2583 }
2584 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002585}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002586
2587
Mathias Agopian96f08192010-06-02 23:28:45 -07002588sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002589 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002590 const String8& name,
2591 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002592 uint32_t flags)
2593{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002594 /*
2595 * createSurface must be called from the GL thread so that it can
2596 * have access to the GL context.
2597 */
2598
2599 class MessageCreateSurface : public MessageBase {
2600 sp<ISurface> result;
2601 SurfaceFlinger* flinger;
2602 ISurfaceComposerClient::surface_data_t* params;
2603 Client* client;
2604 const String8& name;
2605 DisplayID display;
2606 uint32_t w, h;
2607 PixelFormat format;
2608 uint32_t flags;
2609 public:
2610 MessageCreateSurface(SurfaceFlinger* flinger,
2611 ISurfaceComposerClient::surface_data_t* params,
2612 const String8& name, Client* client,
2613 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2614 uint32_t flags)
2615 : flinger(flinger), params(params), client(client), name(name),
2616 display(display), w(w), h(h), format(format), flags(flags)
2617 {
2618 }
2619 sp<ISurface> getResult() const { return result; }
2620 virtual bool handler() {
2621 result = flinger->createSurface(params, name, client,
2622 display, w, h, format, flags);
2623 return true;
2624 }
2625 };
2626
2627 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2628 params, name, this, display, w, h, format, flags);
2629 mFlinger->postMessageSync(msg);
2630 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631}
Mathias Agopian96f08192010-06-02 23:28:45 -07002632status_t Client::destroySurface(SurfaceID sid) {
2633 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635
2636// ---------------------------------------------------------------------------
2637
Jamie Gennis9a78c902011-01-12 18:30:40 -08002638GraphicBufferAlloc::GraphicBufferAlloc() {}
2639
2640GraphicBufferAlloc::~GraphicBufferAlloc() {}
2641
2642sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002643 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002644 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2645 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002646 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002647 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002648 if (err == NO_MEMORY) {
2649 GraphicBuffer::dumpAllocationsToSystemLog();
2650 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002651 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002652 "failed (%s), handle=%p",
2653 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002654 return 0;
2655 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002656 return graphicBuffer;
2657}
2658
Jamie Gennis9a78c902011-01-12 18:30:40 -08002659// ---------------------------------------------------------------------------
2660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002661GraphicPlane::GraphicPlane()
2662 : mHw(0)
2663{
2664}
2665
2666GraphicPlane::~GraphicPlane() {
2667 delete mHw;
2668}
2669
2670bool GraphicPlane::initialized() const {
2671 return mHw ? true : false;
2672}
2673
Mathias Agopian2b92d892010-02-08 15:49:35 -08002674int GraphicPlane::getWidth() const {
2675 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002676}
2677
Mathias Agopian2b92d892010-02-08 15:49:35 -08002678int GraphicPlane::getHeight() const {
2679 return mHeight;
2680}
2681
2682void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2683{
2684 mHw = hw;
2685
2686 // initialize the display orientation transform.
2687 // it's a constant that should come from the display driver.
2688 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2689 char property[PROPERTY_VALUE_MAX];
2690 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2691 //displayOrientation
2692 switch (atoi(property)) {
2693 case 90:
2694 displayOrientation = ISurfaceComposer::eOrientation90;
2695 break;
2696 case 270:
2697 displayOrientation = ISurfaceComposer::eOrientation270;
2698 break;
2699 }
2700 }
2701
2702 const float w = hw->getWidth();
2703 const float h = hw->getHeight();
2704 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2705 &mDisplayTransform);
2706 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2707 mDisplayWidth = h;
2708 mDisplayHeight = w;
2709 } else {
2710 mDisplayWidth = w;
2711 mDisplayHeight = h;
2712 }
2713
2714 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002715}
2716
2717status_t GraphicPlane::orientationToTransfrom(
2718 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002719{
2720 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002721 switch (orientation) {
2722 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002723 flags = Transform::ROT_0;
2724 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002726 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002727 break;
2728 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002729 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002730 break;
2731 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002732 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733 break;
2734 default:
2735 return BAD_VALUE;
2736 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002737 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002738 return NO_ERROR;
2739}
2740
2741status_t GraphicPlane::setOrientation(int orientation)
2742{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002743 // If the rotation can be handled in hardware, this is where
2744 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002745
2746 const DisplayHardware& hw(displayHardware());
2747 const float w = mDisplayWidth;
2748 const float h = mDisplayHeight;
2749 mWidth = int(w);
2750 mHeight = int(h);
2751
2752 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002753 GraphicPlane::orientationToTransfrom(orientation, w, h,
2754 &orientationTransform);
2755 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2756 mWidth = int(h);
2757 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002758 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002759
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002760 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002761 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002762 return NO_ERROR;
2763}
2764
2765const DisplayHardware& GraphicPlane::displayHardware() const {
2766 return *mHw;
2767}
2768
Mathias Agopian59119e62010-10-11 12:37:43 -07002769DisplayHardware& GraphicPlane::editDisplayHardware() {
2770 return *mHw;
2771}
2772
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002773const Transform& GraphicPlane::transform() const {
2774 return mGlobalTransform;
2775}
2776
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002777EGLDisplay GraphicPlane::getEGLDisplay() const {
2778 return mHw->getEGLDisplay();
2779}
2780
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002781// ---------------------------------------------------------------------------
2782
2783}; // namespace android