blob: bc850ea1d46266693bd969af3f271b000cf2ec20 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070053#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Glenn Kasten1db13d72011-12-19 13:55:34 -080064#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070066#include <gui/BitTube.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070067
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070090 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070099 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 mSecureFrameBuffer(0)
101{
102 init();
103}
104
105void SurfaceFlinger::init()
106{
Steve Blocka19954a2012-01-04 20:05:49 +0000107 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 property_get("debug.sf.showupdates", value, "0");
113 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
Colin Cross3854ed52012-03-23 14:17:18 -0700115#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116 property_get("debug.sf.ddms", value, "0");
117 mDebugDDMS = atoi(value);
118 if (mDebugDDMS) {
119 DdmConnection::start(getServiceName());
120 }
Colin Cross3854ed52012-03-23 14:17:18 -0700121#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700122
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000124 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125}
126
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800127void SurfaceFlinger::onFirstRef()
128{
129 mEventQueue.init(this);
130
131 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
132
133 // Wait for the main thread to be done with its initialization
134 mReadyToRunBarrier.wait();
135}
136
137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138SurfaceFlinger::~SurfaceFlinger()
139{
140 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141}
142
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800143void SurfaceFlinger::binderDied(const wp<IBinder>& who)
144{
145 // the window manager died on us. prepare its eulogy.
146
147 // reset screen orientation
148 Vector<ComposerState> state;
149 setTransactionState(state, eOrientationDefault, 0);
150
151 // restart the boot-animation
152 property_set("ctl.start", "bootanim");
153}
154
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700155sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700157 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158}
159
Mathias Agopian7e27f052010-05-28 14:22:23 -0700160sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161{
Mathias Agopian96f08192010-06-02 23:28:45 -0700162 sp<ISurfaceComposerClient> bclient;
163 sp<Client> client(new Client(this));
164 status_t err = client->initCheck();
165 if (err == NO_ERROR) {
166 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 return bclient;
169}
170
Jamie Gennis9a78c902011-01-12 18:30:40 -0800171sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
172{
173 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
174 return gba;
175}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700176
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
178{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000179 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 const GraphicPlane& plane(mGraphicPlanes[dpy]);
181 return plane;
182}
183
184GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
185{
186 return const_cast<GraphicPlane&>(
187 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
188}
189
190void SurfaceFlinger::bootFinished()
191{
192 const nsecs_t now = systemTime();
193 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000194 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700195 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700196
197 // wait patiently for the window manager death
198 const String16 name("window");
199 sp<IBinder> window(defaultServiceManager()->getService(name));
200 if (window != 0) {
201 window->linkToDeath(this);
202 }
203
204 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700205 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206}
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208static inline uint16_t pack565(int r, int g, int b) {
209 return (r<<11)|(g<<5)|b;
210}
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212status_t SurfaceFlinger::readyToRun()
213{
Steve Blocka19954a2012-01-04 20:05:49 +0000214 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 "Initializing graphics H/W...");
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 // we only support one display currently
218 int dpy = 0;
219
220 {
221 // initialize the main display
222 GraphicPlane& plane(graphicPlane(dpy));
223 DisplayHardware* const hw = new DisplayHardware(this, dpy);
224 plane.setDisplayHardware(hw);
225 }
226
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700227 // create the shared control-block
228 mServerHeap = new MemoryHeapBase(4096,
229 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000230 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700231
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700232 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700234
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700235 new(mServerCblk) surface_flinger_cblk_t;
236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 // initialize primary screen
238 // (other display should be initialized in the same manner, but
239 // asynchronously, as they could come and go. None of this is supported
240 // yet).
241 const GraphicPlane& plane(graphicPlane(dpy));
242 const DisplayHardware& hw = plane.displayHardware();
243 const uint32_t w = hw.getWidth();
244 const uint32_t h = hw.getHeight();
245 const uint32_t f = hw.getFormat();
246 hw.makeCurrent();
247
248 // initialize the shared control block
249 mServerCblk->connected |= 1<<dpy;
250 display_cblk_t* dcblk = mServerCblk->displays + dpy;
251 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800252 dcblk->w = plane.getWidth();
253 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 dcblk->format = f;
255 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
256 dcblk->xdpi = hw.getDpiX();
257 dcblk->ydpi = hw.getDpiY();
258 dcblk->fps = hw.getRefreshRate();
259 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260
261 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700263 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glShadeModel(GL_FLAT);
266 glDisable(GL_DITHER);
267 glDisable(GL_CULL_FACE);
268
269 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
270 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700271 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 glGenTextures(1, &mWormholeTexName);
273 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
278 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700279 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
280
281 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
282 glGenTextures(1, &mProtectedTexName);
283 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
288 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
289 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290
291 glViewport(0, 0, w, h);
292 glMatrixMode(GL_PROJECTION);
293 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700294 // put the origin in the left-bottom corner
295 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 -0800296
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800297
298 // start the EventThread
299 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800300 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301
302 /*
303 * We're now ready to accept clients...
304 */
305
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800306 mReadyToRunBarrier.open();
307
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700308 // start boot animation
309 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 return NO_ERROR;
312}
313
314// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800315
Jamie Gennis582270d2011-08-17 18:19:00 -0700316bool SurfaceFlinger::authenticateSurfaceTexture(
317 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800318 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700319 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800320
321 // Check the visible layer list for the ISurface
322 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
323 size_t count = currentLayers.size();
324 for (size_t i=0 ; i<count ; i++) {
325 const sp<LayerBase>& layer(currentLayers[i]);
326 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700327 if (lbc != NULL) {
328 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
329 if (lbcBinder == surfaceTextureBinder) {
330 return true;
331 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800332 }
333 }
334
335 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700336 // SurfaceTexture gets destroyed before all the clients are done using it,
337 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800338 // will instead fail later on when the client tries to use the surface,
339 // which should be reported as "surface XYZ returned an -ENODEV". The
340 // purgatorized layers are no less authentic than the visible ones, so this
341 // should not cause any harm.
342 size_t purgatorySize = mLayerPurgatory.size();
343 for (size_t i=0 ; i<purgatorySize ; i++) {
344 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
345 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700346 if (lbc != NULL) {
347 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
348 if (lbcBinder == surfaceTextureBinder) {
349 return true;
350 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800351 }
352 }
353
354 return false;
355}
356
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800357// ----------------------------------------------------------------------------
358
359sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800360 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700361}
362
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800364
365void SurfaceFlinger::waitForEvent() {
366 mEventQueue.waitMessage();
367}
368
369void SurfaceFlinger::signalTransaction() {
370 mEventQueue.invalidate();
371}
372
373void SurfaceFlinger::signalLayerUpdate() {
374 mEventQueue.invalidate();
375}
376
377void SurfaceFlinger::signalRefresh() {
378 mEventQueue.refresh();
379}
380
381status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
382 nsecs_t reltime, uint32_t flags) {
383 return mEventQueue.postMessage(msg, reltime);
384}
385
386status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
387 nsecs_t reltime, uint32_t flags) {
388 status_t res = mEventQueue.postMessage(msg, reltime);
389 if (res == NO_ERROR) {
390 msg->wait();
391 }
392 return res;
393}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394
395bool SurfaceFlinger::threadLoop()
396{
397 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398 return true;
399}
400
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800401void SurfaceFlinger::onMessageReceived(int32_t what)
402{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800403 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800404 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700405 case MessageQueue::REFRESH: {
406// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800407 // if we're in a global transaction, don't do anything.
408 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
409 uint32_t transactionFlags = peekTransactionFlags(mask);
410 if (CC_UNLIKELY(transactionFlags)) {
411 handleTransaction(transactionFlags);
412 }
413
414 // post surfaces (if needed)
415 handlePageFlip();
416
Mathias Agopian69a655c2012-04-11 20:43:19 -0700417// signalRefresh();
418//
419// } break;
420//
421// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800422
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800423 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800424
Mathias Agopian303d5382012-02-05 01:49:16 -0800425 const DisplayHardware& hw(graphicPlane(0).displayHardware());
426
Mathias Agopian69a655c2012-04-11 20:43:19 -0700427// if (mDirtyRegion.isEmpty()) {
428// return;
429// }
430
Mathias Agopianb048cef2012-02-04 15:44:04 -0800431 if (CC_UNLIKELY(mHwWorkListDirty)) {
432 // build the h/w work list
433 handleWorkList();
434 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800435
Mathias Agopianb048cef2012-02-04 15:44:04 -0800436 if (CC_LIKELY(hw.canDraw())) {
437 // repaint the framebuffer (if needed)
438 handleRepaint();
439 // inform the h/w that we're done compositing
440 hw.compositionComplete();
441 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800442 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800443 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800444 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800445 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800446
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800447 } break;
448 }
449}
450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451void SurfaceFlinger::postFramebuffer()
452{
Mathias Agopian841cde52012-03-01 15:44:37 -0800453 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800454 // mSwapRegion can be empty here is some cases, for instance if a hidden
455 // or fully transparent window is updating.
456 // in that case, we need to flip anyways to not risk a deadlock with
457 // h/w composer.
458
Mathias Agopiana44b0412011-10-16 18:46:35 -0700459 const DisplayHardware& hw(graphicPlane(0).displayHardware());
460 const nsecs_t now = systemTime();
461 mDebugInSwapBuffers = now;
462 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800463
464 size_t numLayers = mVisibleLayersSortedByZ.size();
465 for (size_t i = 0; i < numLayers; i++) {
466 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
467 }
468
Mathias Agopiana44b0412011-10-16 18:46:35 -0700469 mLastSwapBufferTime = systemTime() - now;
470 mDebugInSwapBuffers = 0;
471 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
475{
Mathias Agopian841cde52012-03-01 15:44:37 -0800476 ATRACE_CALL();
477
Mathias Agopianca4d3602011-05-19 15:38:14 -0700478 Mutex::Autolock _l(mStateLock);
479 const nsecs_t now = systemTime();
480 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481
Mathias Agopianca4d3602011-05-19 15:38:14 -0700482 // Here we're guaranteed that some transaction flags are set
483 // so we can call handleTransactionLocked() unconditionally.
484 // We call getTransactionFlags(), which will also clear the flags,
485 // with mStateLock held to guarantee that mCurrentState won't change
486 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700487
Mathias Agopianca4d3602011-05-19 15:38:14 -0700488 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
489 transactionFlags = getTransactionFlags(mask);
490 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700491
Mathias Agopianca4d3602011-05-19 15:38:14 -0700492 mLastTransactionTime = systemTime() - now;
493 mDebugInTransaction = 0;
494 invalidateHwcGeometry();
495 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700496}
497
Mathias Agopianca4d3602011-05-19 15:38:14 -0700498void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700499{
500 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 const size_t count = currentLayers.size();
502
503 /*
504 * Traversal of the children
505 * (perform the transaction for each of them if needed)
506 */
507
508 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
509 if (layersNeedTransaction) {
510 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
513 if (!trFlags) continue;
514
515 const uint32_t flags = layer->doTransaction(0);
516 if (flags & Layer::eVisibleRegion)
517 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 }
519 }
520
521 /*
522 * Perform our own transaction if needed
523 */
524
525 if (transactionFlags & eTransactionNeeded) {
526 if (mCurrentState.orientation != mDrawingState.orientation) {
527 // the orientation has changed, recompute all visible regions
528 // and invalidate everything.
529
530 const int dpy = 0;
531 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700532 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 GraphicPlane& plane(graphicPlane(dpy));
534 plane.setOrientation(orientation);
535
536 // update the shared control block
537 const DisplayHardware& hw(plane.displayHardware());
538 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
539 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800540 dcblk->w = plane.getWidth();
541 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
543 mVisibleRegionsDirty = true;
544 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 }
546
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700547 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
548 // layers have been added
549 mVisibleRegionsDirty = true;
550 }
551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 // some layers might have been removed, so
553 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700555 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700558 const size_t count = previousLayers.size();
559 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700560 const sp<LayerBase>& layer(previousLayers[i]);
561 if (currentLayers.indexOf( layer ) < 0) {
562 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700563 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700564 }
565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 }
568
569 commitTransaction();
570}
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800573 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574{
Mathias Agopian841cde52012-03-01 15:44:37 -0800575 ATRACE_CALL();
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 const GraphicPlane& plane(graphicPlane(0));
578 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700579 const DisplayHardware& hw(plane.displayHardware());
580 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581
582 Region aboveOpaqueLayers;
583 Region aboveCoveredLayers;
584 Region dirty;
585
586 bool secureFrameBuffer = false;
587
588 size_t i = currentLayers.size();
589 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700590 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 layer->validateVisibility(planeTransform);
592
593 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700594 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595
Mathias Agopianab028732010-03-16 16:41:46 -0700596 /*
597 * opaqueRegion: area of a surface that is fully opaque.
598 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700600
601 /*
602 * visibleRegion: area of a surface that is visible on screen
603 * and not fully transparent. This is essentially the layer's
604 * footprint minus the opaque regions above it.
605 * Areas covered by a translucent surface are considered visible.
606 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700608
609 /*
610 * coveredRegion: area of a surface that is covered by all
611 * visible regions above it (which includes the translucent areas).
612 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700614
615
616 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800617 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700618 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700619 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700621 visibleRegion.andSelf(screenRegion);
622 if (!visibleRegion.isEmpty()) {
623 // Remove the transparent area from the visible region
624 if (translucent) {
625 visibleRegion.subtractSelf(layer->transparentRegionScreen);
626 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopianab028732010-03-16 16:41:46 -0700628 // compute the opaque region
629 const int32_t layerOrientation = layer->getOrientation();
630 if (s.alpha==255 && !translucent &&
631 ((layerOrientation & Transform::ROT_INVALID) == false)) {
632 // the opaque region is the layer's footprint
633 opaqueRegion = visibleRegion;
634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 }
636 }
637
Mathias Agopianab028732010-03-16 16:41:46 -0700638 // Clip the covered region to the visible region
639 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
640
641 // Update aboveCoveredLayers for next (lower) layer
642 aboveCoveredLayers.orSelf(visibleRegion);
643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 // subtract the opaque region covered by the layers above us
645 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
647 // compute this layer's dirty region
648 if (layer->contentDirty) {
649 // we need to invalidate the whole region
650 dirty = visibleRegion;
651 // as well, as the old visible region
652 dirty.orSelf(layer->visibleRegionScreen);
653 layer->contentDirty = false;
654 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700655 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700656 * the exposed region consists of two components:
657 * 1) what's VISIBLE now and was COVERED before
658 * 2) what's EXPOSED now less what was EXPOSED before
659 *
660 * note that (1) is conservative, we start with the whole
661 * visible region but only keep what used to be covered by
662 * something -- which mean it may have been exposed.
663 *
664 * (2) handles areas that were not covered by anything but got
665 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700666 */
Mathias Agopianab028732010-03-16 16:41:46 -0700667 const Region newExposed = visibleRegion - coveredRegion;
668 const Region oldVisibleRegion = layer->visibleRegionScreen;
669 const Region oldCoveredRegion = layer->coveredRegionScreen;
670 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
671 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 }
673 dirty.subtractSelf(aboveOpaqueLayers);
674
675 // accumulate to the screen dirty region
676 dirtyRegion.orSelf(dirty);
677
Mathias Agopianab028732010-03-16 16:41:46 -0700678 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 // Store the visible region is screen space
682 layer->setVisibleRegion(visibleRegion);
683 layer->setCoveredRegion(coveredRegion);
684
Mathias Agopian97011222009-07-28 10:57:27 -0700685 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 if (layer->isSecure() && !visibleRegion.isEmpty()) {
687 secureFrameBuffer = true;
688 }
689 }
690
Mathias Agopian97011222009-07-28 10:57:27 -0700691 // invalidate the areas where a layer was removed
692 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
693 mDirtyRegionRemovedLayer.clear();
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 mSecureFrameBuffer = secureFrameBuffer;
696 opaqueRegion = aboveOpaqueLayers;
697}
698
699
700void SurfaceFlinger::commitTransaction()
701{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800702 if (!mLayersPendingRemoval.isEmpty()) {
703 // Notify removed layers now that they can't be drawn from
704 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
705 mLayersPendingRemoval[i]->onRemoved();
706 }
707 mLayersPendingRemoval.clear();
708 }
709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700711 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700712 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713}
714
715void SurfaceFlinger::handlePageFlip()
716{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800717 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800718 const DisplayHardware& hw = graphicPlane(0).displayHardware();
719 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
722 const bool visibleRegions = lockPageFlip(currentLayers);
723
724 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 Region opaqueRegion;
726 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700727
728 /*
729 * rebuild the visible layer list
730 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800731 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700732 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700733 mVisibleLayersSortedByZ.setCapacity(count);
734 for (size_t i=0 ; i<count ; i++) {
735 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
736 mVisibleLayersSortedByZ.add(currentLayers[i]);
737 }
738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 mWormholeRegion = screenRegion.subtract(opaqueRegion);
740 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800741 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
743
744 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700745
746 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 mDirtyRegion.andSelf(screenRegion);
748}
749
Mathias Agopianad456f92011-01-13 17:53:01 -0800750void SurfaceFlinger::invalidateHwcGeometry()
751{
752 mHwWorkListDirty = true;
753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
756{
757 bool recomputeVisibleRegions = false;
758 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700761 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 layer->lockPageFlip(recomputeVisibleRegions);
763 }
764 return recomputeVisibleRegions;
765}
766
767void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
768{
769 const GraphicPlane& plane(graphicPlane(0));
770 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800771 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700772 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700774 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 layer->unlockPageFlip(planeTransform, mDirtyRegion);
776 }
777}
778
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800779void SurfaceFlinger::handleRefresh()
780{
781 bool needInvalidate = false;
782 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
783 const size_t count = currentLayers.size();
784 for (size_t i=0 ; i<count ; i++) {
785 const sp<LayerBase>& layer(currentLayers[i]);
786 if (layer->onPreComposition()) {
787 needInvalidate = true;
788 }
789 }
790 if (needInvalidate) {
791 signalLayerUpdate();
792 }
793}
794
795
Mathias Agopiana350ff92010-08-10 17:14:02 -0700796void SurfaceFlinger::handleWorkList()
797{
798 mHwWorkListDirty = false;
799 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
800 if (hwc.initCheck() == NO_ERROR) {
801 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
802 const size_t count = currentLayers.size();
803 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700804
805 HWComposer::LayerListIterator cur = hwc.begin();
806 const HWComposer::LayerListIterator end = hwc.end();
807 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
808 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700809 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700810 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700811 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700812 }
813 }
814}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816void SurfaceFlinger::handleRepaint()
817{
Mathias Agopian841cde52012-03-01 15:44:37 -0800818 ATRACE_CALL();
819
Mathias Agopianb8a55602009-06-26 19:06:36 -0700820 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700821 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822
Glenn Kasten99ed2242011-12-15 09:51:17 -0800823 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 debugFlashRegions();
825 }
826
Mathias Agopianb8a55602009-06-26 19:06:36 -0700827 // set the frame buffer
828 const DisplayHardware& hw(graphicPlane(0).displayHardware());
829 glMatrixMode(GL_MODELVIEW);
830 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831
832 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700833 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700834 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
835 // takes a rectangle, we must make sure to update that whole
836 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700837 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700839 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700840 // We need to redraw the rectangle that will be updated
841 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700842 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700843 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700844 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 } else {
846 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700848 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 }
850 }
851
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700852 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853 composeSurfaces(mDirtyRegion);
854
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700855 // update the swap region and clear the dirty region
856 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857 mDirtyRegion.clear();
858}
859
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700860void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700862 const DisplayHardware& hw(graphicPlane(0).displayHardware());
863 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700864
865 HWComposer::LayerListIterator cur = hwc.begin();
866 const HWComposer::LayerListIterator end = hwc.end();
867 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700868 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700869 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700870
Mathias Agopianf384cc32011-09-08 18:31:55 -0700871 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
872 size_t count = layers.size();
873
Steve Blocke6f43dd2012-01-06 19:20:56 +0000874 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700875 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
876 hwc.getNumLayers(), count);
877
878 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700879 if (hwc.initCheck() == NO_ERROR) {
880 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
881 }
Mathias Agopian45721772010-08-12 15:03:26 -0700882
883 /*
884 * update the per-frame h/w composer data for each layer
885 * and build the transparent region of the FB
886 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700887 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700888 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700889 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700890 }
891 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000892 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700893}
Mathias Agopian45721772010-08-12 15:03:26 -0700894
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895void SurfaceFlinger::composeSurfaces(const Region& dirty)
896{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700897 const DisplayHardware& hw(graphicPlane(0).displayHardware());
898 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700899 HWComposer::LayerListIterator cur = hwc.begin();
900 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700901
902 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700903 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700904 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700905
Mathias Agopianb9494d52012-04-18 02:28:45 -0700906 if (hwc.getLayerCount(HWC_OVERLAY)) {
907 // when using overlays, we assume a fully transparent framebuffer
908 // NOTE: we could reduce how much we need to clear, for instance
909 // remove where there are opaque FB layers. however, on some
910 // GPUs doing a "clean slate" glClear might be more efficient.
911 // We'll revisit later if needed.
912 glClearColor(0, 0, 0, 0);
913 glClear(GL_COLOR_BUFFER_BIT);
914 } else {
915 // screen is already cleared here
916 if (!mWormholeRegion.isEmpty()) {
917 // can happen with SurfaceView
918 drawWormhole();
919 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700920 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700921
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700922 /*
923 * and then, render the layers targeted at the framebuffer
924 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700925
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700926 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
927 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700928 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700929 const sp<LayerBase>& layer(layers[i]);
930 const Region clip(dirty.intersect(layer->visibleRegionScreen));
931 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700932 if (cur->getCompositionType() == HWC_OVERLAY) {
933 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700934 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700935 // never clear the very first layer since we're
936 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700937 layer->clearWithOpenGL(clip);
938 }
939 continue;
940 }
941 // render the layer
942 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700943 }
944 }
945 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946}
947
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948void SurfaceFlinger::debugFlashRegions()
949{
Mathias Agopian0a917752010-06-14 21:20:00 -0700950 const DisplayHardware& hw(graphicPlane(0).displayHardware());
951 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700952 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700953 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700954 return;
955 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700956
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700957 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700958 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
959 mDirtyRegion.bounds() : hw.bounds());
960 composeSurfaces(repaint);
961 }
962
Mathias Agopianc492e672011-10-18 14:49:27 -0700963 glDisable(GL_TEXTURE_EXTERNAL_OES);
964 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966
Mathias Agopian0926f502009-05-04 14:17:04 -0700967 static int toggle = 0;
968 toggle = 1 - toggle;
969 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700970 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700971 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700972 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700973 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974
Mathias Agopian20f68782009-05-11 00:03:41 -0700975 Region::const_iterator it = mDirtyRegion.begin();
976 Region::const_iterator const end = mDirtyRegion.end();
977 while (it != end) {
978 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700980 { r.left, height - r.top },
981 { r.left, height - r.bottom },
982 { r.right, height - r.bottom },
983 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 };
985 glVertexPointer(2, GL_FLOAT, 0, vertices);
986 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
987 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700988
Mathias Agopian0656a682011-09-20 17:22:44 -0700989 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990
991 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700992 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993}
994
995void SurfaceFlinger::drawWormhole() const
996{
997 const Region region(mWormholeRegion.intersect(mDirtyRegion));
998 if (region.isEmpty())
999 return;
1000
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001001 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001002 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001003 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001004 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001005
1006 GLfloat vertices[4][2];
1007 glVertexPointer(2, GL_FLOAT, 0, vertices);
1008 Region::const_iterator it = region.begin();
1009 Region::const_iterator const end = region.end();
1010 while (it != end) {
1011 const Rect& r = *it++;
1012 vertices[0][0] = r.left;
1013 vertices[0][1] = r.top;
1014 vertices[1][0] = r.right;
1015 vertices[1][1] = r.top;
1016 vertices[2][0] = r.right;
1017 vertices[2][1] = r.bottom;
1018 vertices[3][0] = r.left;
1019 vertices[3][1] = r.bottom;
1020 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1021 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022}
1023
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001024status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025{
1026 Mutex::Autolock _l(mStateLock);
1027 addLayer_l(layer);
1028 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1029 return NO_ERROR;
1030}
1031
Mathias Agopian96f08192010-06-02 23:28:45 -07001032status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1033{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001034 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001035 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1036}
1037
1038ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1039 const sp<LayerBaseClient>& lbc)
1040{
Mathias Agopian96f08192010-06-02 23:28:45 -07001041 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001042 size_t name = client->attachLayer(lbc);
1043
1044 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001045
1046 // add this layer to the current state list
1047 addLayer_l(lbc);
1048
Mathias Agopian4f113742011-05-03 16:21:41 -07001049 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001050}
1051
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001052status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053{
1054 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001055 status_t err = purgatorizeLayer_l(layer);
1056 if (err == NO_ERROR)
1057 setTransactionFlags(eTransactionNeeded);
1058 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059}
1060
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001061status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001063 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1064 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001065 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001066 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1068 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001069 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 return NO_ERROR;
1071 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001072 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073}
1074
Mathias Agopian9a112062009-04-17 19:36:26 -07001075status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1076{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001077 // First add the layer to the purgatory list, which makes sure it won't
1078 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001079 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001080 if (err >= 0) {
1081 mLayerPurgatory.add(layerBase);
1082 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001083
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001084 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001085
Mathias Agopian3d579642009-06-04 18:46:21 -07001086 // it's possible that we don't find a layer, because it might
1087 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001088 // from the user because there is a race between Client::destroySurface(),
1089 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001090 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1091}
1092
Mathias Agopian96f08192010-06-02 23:28:45 -07001093status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094{
Mathias Agopian96f08192010-06-02 23:28:45 -07001095 layer->forceVisibilityTransaction();
1096 setTransactionFlags(eTraversalNeeded);
1097 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098}
1099
Mathias Agopiandea20b12011-05-03 17:04:02 -07001100uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1101{
1102 return android_atomic_release_load(&mTransactionFlags);
1103}
1104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1106{
1107 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1108}
1109
Mathias Agopianbb641242010-05-18 17:06:55 -07001110uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111{
1112 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1113 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001114 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 }
1116 return old;
1117}
1118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001120void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001121 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001122 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001123
Jamie Gennis28378392011-10-12 17:39:00 -07001124 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001125 if (mCurrentState.orientation != orientation) {
1126 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1127 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001128 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001129 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001130 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001131 orientation);
1132 }
1133 }
1134
Mathias Agopian698c0872011-06-28 19:09:31 -07001135 const size_t count = state.size();
1136 for (size_t i=0 ; i<count ; i++) {
1137 const ComposerState& s(state[i]);
1138 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001139 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001140 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001141
Mathias Agopian386aa982011-11-07 21:58:03 -08001142 if (transactionFlags) {
1143 // this triggers the transaction
1144 setTransactionFlags(transactionFlags);
1145
1146 // if this is a synchronous transaction, wait for it to take effect
1147 // before returning.
1148 if (flags & eSynchronous) {
1149 mTransationPending = true;
1150 }
1151 while (mTransationPending) {
1152 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1153 if (CC_UNLIKELY(err != NO_ERROR)) {
1154 // just in case something goes wrong in SF, return to the
1155 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001156 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001157 mTransationPending = false;
1158 break;
1159 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161 }
1162}
1163
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001164sp<ISurface> SurfaceFlinger::createSurface(
1165 ISurfaceComposerClient::surface_data_t* params,
1166 const String8& name,
1167 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1169 uint32_t flags)
1170{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001171 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001172 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001173
1174 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001175 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001176 int(w), int(h));
1177 return surfaceHandle;
1178 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001179
Mathias Agopianff615cc2012-02-24 14:58:36 -08001180 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001181 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 switch (flags & eFXSurfaceMask) {
1183 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001184 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1185 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 break;
1187 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001188 // for now we treat Blur as Dim, until we can implement it
1189 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001191 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001193 case eFXSurfaceScreenshot:
1194 layer = createScreenshotSurface(client, d, w, h, flags);
1195 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 }
1197
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001198 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001199 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001200 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001201 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001204 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001205 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001206 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001207 if (normalLayer != 0) {
1208 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001209 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001210 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001211 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212
Mathias Agopian96f08192010-06-02 23:28:45 -07001213 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 }
1215
1216 return surfaceHandle;
1217}
1218
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001219sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001220 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001221 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001222 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223{
1224 // initialize the surfaces
1225 switch (format) { // TODO: take h/w into account
1226 case PIXEL_FORMAT_TRANSPARENT:
1227 case PIXEL_FORMAT_TRANSLUCENT:
1228 format = PIXEL_FORMAT_RGBA_8888;
1229 break;
1230 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001231#ifdef NO_RGBX_8888
1232 format = PIXEL_FORMAT_RGB_565;
1233#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001234 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001235#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 break;
1237 }
1238
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001239#ifdef NO_RGBX_8888
1240 if (format == PIXEL_FORMAT_RGBX_8888)
1241 format = PIXEL_FORMAT_RGBA_8888;
1242#endif
1243
Mathias Agopian96f08192010-06-02 23:28:45 -07001244 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001245 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001246 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001247 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001248 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 }
1250 return layer;
1251}
1252
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001253sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001254 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001255 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256{
Mathias Agopian96f08192010-06-02 23:28:45 -07001257 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001258 return layer;
1259}
1260
1261sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1262 const sp<Client>& client, DisplayID display,
1263 uint32_t w, uint32_t h, uint32_t flags)
1264{
1265 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 return layer;
1267}
1268
Mathias Agopian96f08192010-06-02 23:28:45 -07001269status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270{
Mathias Agopian9a112062009-04-17 19:36:26 -07001271 /*
1272 * called by the window manager, when a surface should be marked for
1273 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001274 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001275 * The surface is removed from the current and drawing lists, but placed
1276 * in the purgatory queue, so it's not destroyed right-away (we need
1277 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001278 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001279
Mathias Agopian48d819a2009-09-10 19:41:18 -07001280 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001281 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001282 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001283
Mathias Agopian48d819a2009-09-10 19:41:18 -07001284 if (layer != 0) {
1285 err = purgatorizeLayer_l(layer);
1286 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001287 setTransactionFlags(eTransactionNeeded);
1288 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001289 }
1290 return err;
1291}
1292
Mathias Agopianca4d3602011-05-19 15:38:14 -07001293status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001294{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001295 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001296 status_t err = NO_ERROR;
1297 sp<LayerBaseClient> l(layer.promote());
1298 if (l != NULL) {
1299 Mutex::Autolock _l(mStateLock);
1300 err = removeLayer_l(l);
1301 if (err == NAME_NOT_FOUND) {
1302 // The surface wasn't in the current list, which means it was
1303 // removed already, which means it is in the purgatory,
1304 // and need to be removed from there.
1305 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001306 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001307 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001308 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001309 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001310 "error removing layer=%p (%s)", l.get(), strerror(-err));
1311 }
1312 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313}
1314
Mathias Agopian698c0872011-06-28 19:09:31 -07001315uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001316 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001317 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001320 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1321 if (layer != 0) {
1322 const uint32_t what = s.what;
1323 if (what & ePositionChanged) {
1324 if (layer->setPosition(s.x, s.y))
1325 flags |= eTraversalNeeded;
1326 }
1327 if (what & eLayerChanged) {
1328 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1329 if (layer->setLayer(s.z)) {
1330 mCurrentState.layersSortedByZ.removeAt(idx);
1331 mCurrentState.layersSortedByZ.add(layer);
1332 // we need traversal (state changed)
1333 // AND transaction (list changed)
1334 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 }
1336 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001337 if (what & eSizeChanged) {
1338 if (layer->setSize(s.w, s.h)) {
1339 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001340 }
1341 }
1342 if (what & eAlphaChanged) {
1343 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1344 flags |= eTraversalNeeded;
1345 }
1346 if (what & eMatrixChanged) {
1347 if (layer->setMatrix(s.matrix))
1348 flags |= eTraversalNeeded;
1349 }
1350 if (what & eTransparentRegionChanged) {
1351 if (layer->setTransparentRegionHint(s.transparentRegion))
1352 flags |= eTraversalNeeded;
1353 }
1354 if (what & eVisibilityChanged) {
1355 if (layer->setFlags(s.flags, s.mask))
1356 flags |= eTraversalNeeded;
1357 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001358 if (what & eCropChanged) {
1359 if (layer->setCrop(s.crop))
1360 flags |= eTraversalNeeded;
1361 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001363 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364}
1365
Mathias Agopianb60314a2012-04-10 22:09:54 -07001366// ---------------------------------------------------------------------------
1367
1368void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001369 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001370 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1371 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001372 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001373 // this is a temporary work-around, eventually this should be called
1374 // by the power-manager
1375 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001376 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001377 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378}
1379
Mathias Agopianb60314a2012-04-10 22:09:54 -07001380void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001381 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001382 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1383 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001384 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001385 hw.releaseScreen();
1386 // from this point on, SF will stop drawing
1387 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388}
1389
Colin Cross8e533062012-06-07 13:17:52 -07001390void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001391 class MessageScreenAcquired : public MessageBase {
1392 SurfaceFlinger* flinger;
1393 public:
1394 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1395 virtual bool handler() {
1396 flinger->onScreenAcquired();
1397 return true;
1398 }
1399 };
1400 sp<MessageBase> msg = new MessageScreenAcquired(this);
1401 postMessageSync(msg);
1402}
1403
Colin Cross8e533062012-06-07 13:17:52 -07001404void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001405 class MessageScreenReleased : public MessageBase {
1406 SurfaceFlinger* flinger;
1407 public:
1408 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1409 virtual bool handler() {
1410 flinger->onScreenReleased();
1411 return true;
1412 }
1413 };
1414 sp<MessageBase> msg = new MessageScreenReleased(this);
1415 postMessageSync(msg);
1416}
1417
1418// ---------------------------------------------------------------------------
1419
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1421{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001422 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 char buffer[SIZE];
1424 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001425
1426 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 snprintf(buffer, SIZE, "Permission Denial: "
1428 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1429 IPCThreadState::self()->getCallingPid(),
1430 IPCThreadState::self()->getCallingUid());
1431 result.append(buffer);
1432 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001433 // Try to get the main lock, but don't insist if we can't
1434 // (this would indicate SF is stuck, but we want to be able to
1435 // print something in dumpsys).
1436 int retry = 3;
1437 while (mStateLock.tryLock()<0 && --retry>=0) {
1438 usleep(1000000);
1439 }
1440 const bool locked(retry >= 0);
1441 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001442 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001443 "SurfaceFlinger appears to be unresponsive, "
1444 "dumping anyways (no locks held)\n");
1445 result.append(buffer);
1446 }
1447
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001448 bool dumpAll = true;
1449 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001450 size_t numArgs = args.size();
1451 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001452 if ((index < numArgs) &&
1453 (args[index] == String16("--list"))) {
1454 index++;
1455 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001456 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001457 }
1458
1459 if ((index < numArgs) &&
1460 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001461 index++;
1462 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001463 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001464 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001465
1466 if ((index < numArgs) &&
1467 (args[index] == String16("--latency-clear"))) {
1468 index++;
1469 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001470 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001471 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001473
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001474 if (dumpAll) {
1475 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001476 }
1477
Mathias Agopian9795c422009-08-26 16:36:26 -07001478 if (locked) {
1479 mStateLock.unlock();
1480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 }
1482 write(fd, result.string(), result.size());
1483 return NO_ERROR;
1484}
1485
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001486void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1487 String8& result, char* buffer, size_t SIZE) const
1488{
1489 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1490 const size_t count = currentLayers.size();
1491 for (size_t i=0 ; i<count ; i++) {
1492 const sp<LayerBase>& layer(currentLayers[i]);
1493 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1494 result.append(buffer);
1495 }
1496}
1497
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001498void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1499 String8& result, char* buffer, size_t SIZE) const
1500{
1501 String8 name;
1502 if (index < args.size()) {
1503 name = String8(args[index]);
1504 index++;
1505 }
1506
1507 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1508 const size_t count = currentLayers.size();
1509 for (size_t i=0 ; i<count ; i++) {
1510 const sp<LayerBase>& layer(currentLayers[i]);
1511 if (name.isEmpty()) {
1512 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1513 result.append(buffer);
1514 }
1515 if (name.isEmpty() || (name == layer->getName())) {
1516 layer->dumpStats(result, buffer, SIZE);
1517 }
1518 }
1519}
1520
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001521void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1522 String8& result, char* buffer, size_t SIZE) const
1523{
1524 String8 name;
1525 if (index < args.size()) {
1526 name = String8(args[index]);
1527 index++;
1528 }
1529
1530 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1531 const size_t count = currentLayers.size();
1532 for (size_t i=0 ; i<count ; i++) {
1533 const sp<LayerBase>& layer(currentLayers[i]);
1534 if (name.isEmpty() || (name == layer->getName())) {
1535 layer->clearStats();
1536 }
1537 }
1538}
1539
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001540void SurfaceFlinger::dumpAllLocked(
1541 String8& result, char* buffer, size_t SIZE) const
1542{
1543 // figure out if we're stuck somewhere
1544 const nsecs_t now = systemTime();
1545 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1546 const nsecs_t inTransaction(mDebugInTransaction);
1547 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1548 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1549
1550 /*
1551 * Dump the visible layer list
1552 */
1553 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1554 const size_t count = currentLayers.size();
1555 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1556 result.append(buffer);
1557 for (size_t i=0 ; i<count ; i++) {
1558 const sp<LayerBase>& layer(currentLayers[i]);
1559 layer->dump(result, buffer, SIZE);
1560 }
1561
1562 /*
1563 * Dump the layers in the purgatory
1564 */
1565
1566 const size_t purgatorySize = mLayerPurgatory.size();
1567 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1568 result.append(buffer);
1569 for (size_t i=0 ; i<purgatorySize ; i++) {
1570 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1571 layer->shortDump(result, buffer, SIZE);
1572 }
1573
1574 /*
1575 * Dump SurfaceFlinger global state
1576 */
1577
1578 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1579 result.append(buffer);
1580
1581 const GLExtensions& extensions(GLExtensions::getInstance());
1582 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1583 extensions.getVendor(),
1584 extensions.getRenderer(),
1585 extensions.getVersion());
1586 result.append(buffer);
1587
1588 snprintf(buffer, SIZE, "EGL : %s\n",
1589 eglQueryString(graphicPlane(0).getEGLDisplay(),
1590 EGL_VERSION_HW_ANDROID));
1591 result.append(buffer);
1592
1593 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1594 result.append(buffer);
1595
1596 mWormholeRegion.dump(result, "WormholeRegion");
1597 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1598 snprintf(buffer, SIZE,
1599 " orientation=%d, canDraw=%d\n",
1600 mCurrentState.orientation, hw.canDraw());
1601 result.append(buffer);
1602 snprintf(buffer, SIZE,
1603 " last eglSwapBuffers() time: %f us\n"
1604 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001605 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001606 " refresh-rate : %f fps\n"
1607 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001608 " y-dpi : %f\n"
1609 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001610 mLastSwapBufferTime/1000.0,
1611 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001612 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001613 hw.getRefreshRate(),
1614 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001615 hw.getDpiY(),
1616 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001617 result.append(buffer);
1618
1619 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1620 inSwapBuffersDuration/1000.0);
1621 result.append(buffer);
1622
1623 snprintf(buffer, SIZE, " transaction time: %f us\n",
1624 inTransactionDuration/1000.0);
1625 result.append(buffer);
1626
1627 /*
1628 * VSYNC state
1629 */
1630 mEventThread->dump(result, buffer, SIZE);
1631
1632 /*
1633 * Dump HWComposer state
1634 */
1635 HWComposer& hwc(hw.getHwComposer());
1636 snprintf(buffer, SIZE, "h/w composer state:\n");
1637 result.append(buffer);
1638 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1639 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1640 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1641 result.append(buffer);
1642 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1643
1644 /*
1645 * Dump gralloc state
1646 */
1647 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1648 alloc.dump(result);
1649 hw.dump(result);
1650}
1651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652status_t SurfaceFlinger::onTransact(
1653 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1654{
1655 switch (code) {
1656 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001657 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001660 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001661 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001662 case BLANK:
1663 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 {
1665 // codes that require permission check
1666 IPCThreadState* ipc = IPCThreadState::self();
1667 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001668 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001669 if ((uid != AID_GRAPHICS) &&
1670 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001671 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001672 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1673 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001675 break;
1676 }
1677 case CAPTURE_SCREEN:
1678 {
1679 // codes that require permission check
1680 IPCThreadState* ipc = IPCThreadState::self();
1681 const int pid = ipc->getCallingPid();
1682 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001683 if ((uid != AID_GRAPHICS) &&
1684 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001685 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001686 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1687 return PERMISSION_DENIED;
1688 }
1689 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 }
1691 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001692
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1694 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001695 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001696 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001697 IPCThreadState* ipc = IPCThreadState::self();
1698 const int pid = ipc->getCallingPid();
1699 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001700 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001701 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702 return PERMISSION_DENIED;
1703 }
1704 int n;
1705 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001706 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001707 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 return NO_ERROR;
1709 case 1002: // SHOW_UPDATES
1710 n = data.readInt32();
1711 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001712 invalidateHwcGeometry();
1713 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001716 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001717 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001719 case 1005:{ // force transaction
1720 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1721 return NO_ERROR;
1722 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001723 case 1006:{ // send empty update
1724 signalRefresh();
1725 return NO_ERROR;
1726 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001727 case 1008: // toggle use of hw composer
1728 n = data.readInt32();
1729 mDebugDisableHWC = n ? 1 : 0;
1730 invalidateHwcGeometry();
1731 repaintEverything();
1732 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001733 case 1009: // toggle use of transform hint
1734 n = data.readInt32();
1735 mDebugDisableTransformHint = n ? 1 : 0;
1736 invalidateHwcGeometry();
1737 repaintEverything();
1738 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001740 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 reply->writeInt32(0);
1742 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001743 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001744 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 return NO_ERROR;
1746 case 1013: {
1747 Mutex::Autolock _l(mStateLock);
1748 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1749 reply->writeInt32(hw.getPageFlipCount());
1750 }
1751 return NO_ERROR;
1752 }
1753 }
1754 return err;
1755}
1756
Mathias Agopian53331da2011-08-22 21:44:41 -07001757void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001758 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001759 const Rect bounds(hw.getBounds());
1760 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001761 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001762}
1763
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001764void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1765 Mutex::Autolock _l(mInvalidateLock);
1766 mInvalidateRegion = reg;
1767}
1768
1769Region SurfaceFlinger::getAndClearInvalidateRegion() {
1770 Mutex::Autolock _l(mInvalidateLock);
1771 Region reg(mInvalidateRegion);
1772 mInvalidateRegion.clear();
1773 return reg;
1774}
1775
Mathias Agopian59119e62010-10-11 12:37:43 -07001776// ---------------------------------------------------------------------------
1777
Mathias Agopian118d0242011-10-13 16:02:48 -07001778status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1779 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1780{
1781 Mutex::Autolock _l(mStateLock);
1782 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1783}
1784
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001785status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1786 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001787{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001788 ATRACE_CALL();
1789
Mathias Agopian59119e62010-10-11 12:37:43 -07001790 if (!GLExtensions::getInstance().haveFramebufferObject())
1791 return INVALID_OPERATION;
1792
1793 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001794 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001795 const uint32_t hw_w = hw.getWidth();
1796 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001797 GLfloat u = 1;
1798 GLfloat v = 1;
1799
1800 // make sure to clear all GL error flags
1801 while ( glGetError() != GL_NO_ERROR ) ;
1802
1803 // create a FBO
1804 GLuint name, tname;
1805 glGenTextures(1, &tname);
1806 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001807 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1808 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001809 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1810 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001811 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001812 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001813 GLint tw = (2 << (31 - clz(hw_w)));
1814 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001815 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1816 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001817 u = GLfloat(hw_w) / tw;
1818 v = GLfloat(hw_h) / th;
1819 }
1820 glGenFramebuffersOES(1, &name);
1821 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001822 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1823 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001824
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001825 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001826 glDisable(GL_TEXTURE_EXTERNAL_OES);
1827 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001828 glClearColor(0,0,0,1);
1829 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001830 glMatrixMode(GL_MODELVIEW);
1831 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001832 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1833 const size_t count = layers.size();
1834 for (size_t i=0 ; i<count ; ++i) {
1835 const sp<LayerBase>& layer(layers[i]);
1836 layer->drawForSreenShot();
1837 }
1838
Mathias Agopian118d0242011-10-13 16:02:48 -07001839 hw.compositionComplete();
1840
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001841 // back to main framebuffer
1842 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001843 glDeleteFramebuffersOES(1, &name);
1844
1845 *textureName = tname;
1846 *uOut = u;
1847 *vOut = v;
1848 return NO_ERROR;
1849}
1850
1851// ---------------------------------------------------------------------------
1852
Mathias Agopianed9807b2012-05-18 14:18:08 -07001853class VSyncWaiter {
1854 DisplayEventReceiver::Event buffer[4];
1855 sp<Looper> looper;
1856 sp<IDisplayEventConnection> events;
1857 sp<BitTube> eventTube;
1858public:
1859 VSyncWaiter(const sp<EventThread>& eventThread) {
1860 looper = new Looper(true);
1861 events = eventThread->createEventConnection();
1862 eventTube = events->getDataChannel();
1863 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1864 events->requestNextVsync();
1865 }
1866
1867 void wait() {
1868 ssize_t n;
1869
1870 looper->pollOnce(-1);
1871 // we don't handle any errors here, it doesn't matter
1872 // and we don't want to take the risk to get stuck.
1873
1874 // drain the events...
1875 while ((n = DisplayEventReceiver::getEvents(
1876 eventTube, buffer, 4)) > 0) ;
1877
1878 events->requestNextVsync();
1879 }
1880};
1881
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001882status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1883{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001884 // get screen geometry
1885 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1886 const uint32_t hw_w = hw.getWidth();
1887 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001888 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001889
1890 GLfloat u, v;
1891 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001892 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001893 if (result != NO_ERROR) {
1894 return result;
1895 }
1896
1897 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001898 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001899 glBindTexture(GL_TEXTURE_2D, tname);
1900 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1901 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1902 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001903 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1904 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001905 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1906 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1907 glVertexPointer(2, GL_FLOAT, 0, vtx);
1908
Mathias Agopianffcf4652011-07-07 17:30:31 -07001909 /*
1910 * Texture coordinate mapping
1911 *
1912 * u
1913 * 1 +----------+---+
1914 * | | | | image is inverted
1915 * | V | | w.r.t. the texture
1916 * 1-v +----------+ | coordinates
1917 * | |
1918 * | |
1919 * | |
1920 * 0 +--------------+
1921 * 0 1
1922 *
1923 */
1924
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001925 class s_curve_interpolator {
1926 const float nbFrames, s, v;
1927 public:
1928 s_curve_interpolator(int nbFrames, float s)
1929 : nbFrames(1.0f / (nbFrames-1)), s(s),
1930 v(1.0f + expf(-s + 0.5f*s)) {
1931 }
1932 float operator()(int f) {
1933 const float x = f * nbFrames;
1934 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1935 }
1936 };
1937
1938 class v_stretch {
1939 const GLfloat hw_w, hw_h;
1940 public:
1941 v_stretch(uint32_t hw_w, uint32_t hw_h)
1942 : hw_w(hw_w), hw_h(hw_h) {
1943 }
1944 void operator()(GLfloat* vtx, float v) {
1945 const GLfloat w = hw_w + (hw_w * v);
1946 const GLfloat h = hw_h - (hw_h * v);
1947 const GLfloat x = (hw_w - w) * 0.5f;
1948 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001949 vtx[0] = x; vtx[1] = y;
1950 vtx[2] = x; vtx[3] = y + h;
1951 vtx[4] = x + w; vtx[5] = y + h;
1952 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001953 }
1954 };
1955
1956 class h_stretch {
1957 const GLfloat hw_w, hw_h;
1958 public:
1959 h_stretch(uint32_t hw_w, uint32_t hw_h)
1960 : hw_w(hw_w), hw_h(hw_h) {
1961 }
1962 void operator()(GLfloat* vtx, float v) {
1963 const GLfloat w = hw_w - (hw_w * v);
1964 const GLfloat h = 1.0f;
1965 const GLfloat x = (hw_w - w) * 0.5f;
1966 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001967 vtx[0] = x; vtx[1] = y;
1968 vtx[2] = x; vtx[3] = y + h;
1969 vtx[4] = x + w; vtx[5] = y + h;
1970 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 }
1972 };
1973
Mathias Agopianed9807b2012-05-18 14:18:08 -07001974 VSyncWaiter vsync(mEventThread);
1975
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001976 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001977 char value[PROPERTY_VALUE_MAX];
1978 property_get("debug.sf.electron_frames", value, "24");
1979 int nbFrames = (atoi(value) + 1) >> 1;
1980 if (nbFrames <= 0) // just in case
1981 nbFrames = 24;
1982
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001983 s_curve_interpolator itr(nbFrames, 7.5f);
1984 s_curve_interpolator itg(nbFrames, 8.0f);
1985 s_curve_interpolator itb(nbFrames, 8.5f);
1986
1987 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001988
1989 glMatrixMode(GL_TEXTURE);
1990 glLoadIdentity();
1991 glMatrixMode(GL_MODELVIEW);
1992 glLoadIdentity();
1993
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001994 glEnable(GL_BLEND);
1995 glBlendFunc(GL_ONE, GL_ONE);
1996 for (int i=0 ; i<nbFrames ; i++) {
1997 float x, y, w, h;
1998 const float vr = itr(i);
1999 const float vg = itg(i);
2000 const float vb = itb(i);
2001
Mathias Agopianed9807b2012-05-18 14:18:08 -07002002 // wait for vsync
2003 vsync.wait();
2004
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002005 // clear screen
2006 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002007 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002008 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002009
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002010 // draw the red plane
2011 vverts(vtx, vr);
2012 glColorMask(1,0,0,1);
2013 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002014
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002015 // draw the green plane
2016 vverts(vtx, vg);
2017 glColorMask(0,1,0,1);
2018 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002019
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002020 // draw the blue plane
2021 vverts(vtx, vb);
2022 glColorMask(0,0,1,1);
2023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002024
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002026 glDisable(GL_TEXTURE_2D);
2027 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002028 glColor4f(vg, vg, vg, 1);
2029 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2030 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002031 }
2032
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 h_stretch hverts(hw_w, hw_h);
2034 glDisable(GL_BLEND);
2035 glDisable(GL_TEXTURE_2D);
2036 glColorMask(1,1,1,1);
2037 for (int i=0 ; i<nbFrames ; i++) {
2038 const float v = itg(i);
2039 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002040
2041 // wait for vsync
2042 vsync.wait();
2043
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 glClear(GL_COLOR_BUFFER_BIT);
2045 glColor4f(1-v, 1-v, 1-v, 1);
2046 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2047 hw.flip(screenBounds);
2048 }
2049
2050 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2052 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002053 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002054 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002055 return NO_ERROR;
2056}
2057
2058status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2059{
2060 status_t result = PERMISSION_DENIED;
2061
2062 if (!GLExtensions::getInstance().haveFramebufferObject())
2063 return INVALID_OPERATION;
2064
2065
2066 // get screen geometry
2067 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2068 const uint32_t hw_w = hw.getWidth();
2069 const uint32_t hw_h = hw.getHeight();
2070 const Region screenBounds(hw.bounds());
2071
2072 GLfloat u, v;
2073 GLuint tname;
2074 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2075 if (result != NO_ERROR) {
2076 return result;
2077 }
2078
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002079 GLfloat vtx[8];
2080 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002081 glBindTexture(GL_TEXTURE_2D, tname);
2082 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2083 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2084 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002085 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2086 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002087 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2088 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2089 glVertexPointer(2, GL_FLOAT, 0, vtx);
2090
2091 class s_curve_interpolator {
2092 const float nbFrames, s, v;
2093 public:
2094 s_curve_interpolator(int nbFrames, float s)
2095 : nbFrames(1.0f / (nbFrames-1)), s(s),
2096 v(1.0f + expf(-s + 0.5f*s)) {
2097 }
2098 float operator()(int f) {
2099 const float x = f * nbFrames;
2100 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2101 }
2102 };
2103
2104 class v_stretch {
2105 const GLfloat hw_w, hw_h;
2106 public:
2107 v_stretch(uint32_t hw_w, uint32_t hw_h)
2108 : hw_w(hw_w), hw_h(hw_h) {
2109 }
2110 void operator()(GLfloat* vtx, float v) {
2111 const GLfloat w = hw_w + (hw_w * v);
2112 const GLfloat h = hw_h - (hw_h * v);
2113 const GLfloat x = (hw_w - w) * 0.5f;
2114 const GLfloat y = (hw_h - h) * 0.5f;
2115 vtx[0] = x; vtx[1] = y;
2116 vtx[2] = x; vtx[3] = y + h;
2117 vtx[4] = x + w; vtx[5] = y + h;
2118 vtx[6] = x + w; vtx[7] = y;
2119 }
2120 };
2121
2122 class h_stretch {
2123 const GLfloat hw_w, hw_h;
2124 public:
2125 h_stretch(uint32_t hw_w, uint32_t hw_h)
2126 : hw_w(hw_w), hw_h(hw_h) {
2127 }
2128 void operator()(GLfloat* vtx, float v) {
2129 const GLfloat w = hw_w - (hw_w * v);
2130 const GLfloat h = 1.0f;
2131 const GLfloat x = (hw_w - w) * 0.5f;
2132 const GLfloat y = (hw_h - h) * 0.5f;
2133 vtx[0] = x; vtx[1] = y;
2134 vtx[2] = x; vtx[3] = y + h;
2135 vtx[4] = x + w; vtx[5] = y + h;
2136 vtx[6] = x + w; vtx[7] = y;
2137 }
2138 };
2139
Mathias Agopianed9807b2012-05-18 14:18:08 -07002140 VSyncWaiter vsync(mEventThread);
2141
Mathias Agopiana6546e52010-10-14 12:33:07 -07002142 // the full animation is 12 frames
2143 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002144 s_curve_interpolator itr(nbFrames, 7.5f);
2145 s_curve_interpolator itg(nbFrames, 8.0f);
2146 s_curve_interpolator itb(nbFrames, 8.5f);
2147
2148 h_stretch hverts(hw_w, hw_h);
2149 glDisable(GL_BLEND);
2150 glDisable(GL_TEXTURE_2D);
2151 glColorMask(1,1,1,1);
2152 for (int i=nbFrames-1 ; i>=0 ; i--) {
2153 const float v = itg(i);
2154 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002155
2156 // wait for vsync
2157 vsync.wait();
2158
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002159 glClear(GL_COLOR_BUFFER_BIT);
2160 glColor4f(1-v, 1-v, 1-v, 1);
2161 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2162 hw.flip(screenBounds);
2163 }
2164
Mathias Agopiana6546e52010-10-14 12:33:07 -07002165 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002166 v_stretch vverts(hw_w, hw_h);
2167 glEnable(GL_BLEND);
2168 glBlendFunc(GL_ONE, GL_ONE);
2169 for (int i=nbFrames-1 ; i>=0 ; i--) {
2170 float x, y, w, h;
2171 const float vr = itr(i);
2172 const float vg = itg(i);
2173 const float vb = itb(i);
2174
Mathias Agopianed9807b2012-05-18 14:18:08 -07002175 // wait for vsync
2176 vsync.wait();
2177
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002178 // clear screen
2179 glColorMask(1,1,1,1);
2180 glClear(GL_COLOR_BUFFER_BIT);
2181 glEnable(GL_TEXTURE_2D);
2182
2183 // draw the red plane
2184 vverts(vtx, vr);
2185 glColorMask(1,0,0,1);
2186 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2187
2188 // draw the green plane
2189 vverts(vtx, vg);
2190 glColorMask(0,1,0,1);
2191 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2192
2193 // draw the blue plane
2194 vverts(vtx, vb);
2195 glColorMask(0,0,1,1);
2196 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2197
2198 hw.flip(screenBounds);
2199 }
2200
2201 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002202 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002203 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002204 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002205 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002206
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 return NO_ERROR;
2208}
2209
2210// ---------------------------------------------------------------------------
2211
Mathias Agopianabd671a2010-10-14 14:54:06 -07002212status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002213{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002214 ATRACE_CALL();
2215
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002216 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2217 if (!hw.canDraw()) {
2218 // we're already off
2219 return NO_ERROR;
2220 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002221
2222 // turn off hwc while we're doing the animation
2223 hw.getHwComposer().disable();
2224 // and make sure to turn it back on (if needed) next time we compose
2225 invalidateHwcGeometry();
2226
Mathias Agopianabd671a2010-10-14 14:54:06 -07002227 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2228 electronBeamOffAnimationImplLocked();
2229 }
2230
2231 // always clear the whole screen at the end of the animation
2232 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002233 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002234 hw.flip( Region(hw.bounds()) );
2235
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002236 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002237}
2238
2239status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2240{
Mathias Agopian59119e62010-10-11 12:37:43 -07002241 class MessageTurnElectronBeamOff : public MessageBase {
2242 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002243 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002244 status_t result;
2245 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002246 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2247 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002248 }
2249 status_t getResult() const {
2250 return result;
2251 }
2252 virtual bool handler() {
2253 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002254 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002255 return true;
2256 }
2257 };
2258
Mathias Agopianabd671a2010-10-14 14:54:06 -07002259 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002260 status_t res = postMessageSync(msg);
2261 if (res == NO_ERROR) {
2262 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002263
2264 // work-around: when the power-manager calls us we activate the
2265 // animation. eventually, the "on" animation will be called
2266 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002267 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002268 }
2269 return res;
2270}
2271
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002272// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002273
Mathias Agopianabd671a2010-10-14 14:54:06 -07002274status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002275{
2276 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2277 if (hw.canDraw()) {
2278 // we're already on
2279 return NO_ERROR;
2280 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002281 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2282 electronBeamOnAnimationImplLocked();
2283 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002284
2285 // make sure to redraw the whole screen when the animation is done
2286 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002287 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002288
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002289 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290}
2291
2292status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2293{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002294 class MessageTurnElectronBeamOn : public MessageBase {
2295 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002296 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002297 status_t result;
2298 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002299 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2300 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002301 }
2302 status_t getResult() const {
2303 return result;
2304 }
2305 virtual bool handler() {
2306 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002307 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002308 return true;
2309 }
2310 };
2311
Mathias Agopianabd671a2010-10-14 14:54:06 -07002312 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002313 return NO_ERROR;
2314}
2315
2316// ---------------------------------------------------------------------------
2317
Mathias Agopian74c40c02010-09-29 13:02:36 -07002318status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2319 sp<IMemoryHeap>* heap,
2320 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002321 uint32_t sw, uint32_t sh,
2322 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002323{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002324 ATRACE_CALL();
2325
Mathias Agopian74c40c02010-09-29 13:02:36 -07002326 status_t result = PERMISSION_DENIED;
2327
2328 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002329 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002330 return BAD_VALUE;
2331
2332 if (!GLExtensions::getInstance().haveFramebufferObject())
2333 return INVALID_OPERATION;
2334
2335 // get screen geometry
2336 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2337 const uint32_t hw_w = hw.getWidth();
2338 const uint32_t hw_h = hw.getHeight();
2339
2340 if ((sw > hw_w) || (sh > hw_h))
2341 return BAD_VALUE;
2342
2343 sw = (!sw) ? hw_w : sw;
2344 sh = (!sh) ? hw_h : sh;
2345 const size_t size = sw * sh * 4;
2346
Steve Block9d453682011-12-20 16:23:08 +00002347 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002348 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002349
Mathias Agopian74c40c02010-09-29 13:02:36 -07002350 // make sure to clear all GL error flags
2351 while ( glGetError() != GL_NO_ERROR ) ;
2352
2353 // create a FBO
2354 GLuint name, tname;
2355 glGenRenderbuffersOES(1, &tname);
2356 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2357 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002358
Mathias Agopian74c40c02010-09-29 13:02:36 -07002359 glGenFramebuffersOES(1, &name);
2360 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2361 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2362 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2363
2364 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002365
Mathias Agopian74c40c02010-09-29 13:02:36 -07002366 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2367
2368 // invert everything, b/c glReadPixel() below will invert the FB
2369 glViewport(0, 0, sw, sh);
2370 glMatrixMode(GL_PROJECTION);
2371 glPushMatrix();
2372 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002373 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002374 glMatrixMode(GL_MODELVIEW);
2375
2376 // redraw the screen entirely...
2377 glClearColor(0,0,0,1);
2378 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002379
Jamie Gennis9575f602011-10-07 14:51:16 -07002380 const LayerVector& layers(mDrawingState.layersSortedByZ);
2381 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002382 for (size_t i=0 ; i<count ; ++i) {
2383 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002384 const uint32_t flags = layer->drawingState().flags;
2385 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2386 const uint32_t z = layer->drawingState().z;
2387 if (z >= minLayerZ && z <= maxLayerZ) {
2388 layer->drawForSreenShot();
2389 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002390 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002391 }
2392
Mathias Agopian74c40c02010-09-29 13:02:36 -07002393 // check for errors and return screen capture
2394 if (glGetError() != GL_NO_ERROR) {
2395 // error while rendering
2396 result = INVALID_OPERATION;
2397 } else {
2398 // allocate shared memory large enough to hold the
2399 // screen capture
2400 sp<MemoryHeapBase> base(
2401 new MemoryHeapBase(size, 0, "screen-capture") );
2402 void* const ptr = base->getBase();
2403 if (ptr) {
2404 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002405 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002406 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2407 if (glGetError() == GL_NO_ERROR) {
2408 *heap = base;
2409 *w = sw;
2410 *h = sh;
2411 *f = PIXEL_FORMAT_RGBA_8888;
2412 result = NO_ERROR;
2413 }
2414 } else {
2415 result = NO_MEMORY;
2416 }
2417 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 glViewport(0, 0, hw_w, hw_h);
2419 glMatrixMode(GL_PROJECTION);
2420 glPopMatrix();
2421 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002422 } else {
2423 result = BAD_VALUE;
2424 }
2425
2426 // release FBO resources
2427 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2428 glDeleteRenderbuffersOES(1, &tname);
2429 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002430
2431 hw.compositionComplete();
2432
Steve Block9d453682011-12-20 16:23:08 +00002433 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002434
Mathias Agopian74c40c02010-09-29 13:02:36 -07002435 return result;
2436}
2437
2438
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002439status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2440 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002441 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002442 uint32_t sw, uint32_t sh,
2443 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002444{
2445 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002446 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447 return BAD_VALUE;
2448
2449 if (!GLExtensions::getInstance().haveFramebufferObject())
2450 return INVALID_OPERATION;
2451
2452 class MessageCaptureScreen : public MessageBase {
2453 SurfaceFlinger* flinger;
2454 DisplayID dpy;
2455 sp<IMemoryHeap>* heap;
2456 uint32_t* w;
2457 uint32_t* h;
2458 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002459 uint32_t sw;
2460 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002461 uint32_t minLayerZ;
2462 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002463 status_t result;
2464 public:
2465 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002466 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002467 uint32_t sw, uint32_t sh,
2468 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002469 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002470 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2471 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2472 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002473 {
2474 }
2475 status_t getResult() const {
2476 return result;
2477 }
2478 virtual bool handler() {
2479 Mutex::Autolock _l(flinger->mStateLock);
2480
2481 // if we have secure windows, never allow the screen capture
2482 if (flinger->mSecureFrameBuffer)
2483 return true;
2484
Mathias Agopian74c40c02010-09-29 13:02:36 -07002485 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002486 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002487
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002488 return true;
2489 }
2490 };
2491
2492 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002493 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002494 status_t res = postMessageSync(msg);
2495 if (res == NO_ERROR) {
2496 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2497 }
2498 return res;
2499}
2500
2501// ---------------------------------------------------------------------------
2502
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002503sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2504{
2505 sp<Layer> result;
2506 Mutex::Autolock _l(mStateLock);
2507 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2508 return result;
2509}
2510
2511// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512
Jamie Gennis9a78c902011-01-12 18:30:40 -08002513GraphicBufferAlloc::GraphicBufferAlloc() {}
2514
2515GraphicBufferAlloc::~GraphicBufferAlloc() {}
2516
2517sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002518 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002519 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2520 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002521 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002522 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002523 if (err == NO_MEMORY) {
2524 GraphicBuffer::dumpAllocationsToSystemLog();
2525 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002526 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002527 "failed (%s), handle=%p",
2528 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002529 return 0;
2530 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002531 return graphicBuffer;
2532}
2533
Jamie Gennis9a78c902011-01-12 18:30:40 -08002534// ---------------------------------------------------------------------------
2535
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536GraphicPlane::GraphicPlane()
2537 : mHw(0)
2538{
2539}
2540
2541GraphicPlane::~GraphicPlane() {
2542 delete mHw;
2543}
2544
2545bool GraphicPlane::initialized() const {
2546 return mHw ? true : false;
2547}
2548
Mathias Agopian2b92d892010-02-08 15:49:35 -08002549int GraphicPlane::getWidth() const {
2550 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551}
2552
Mathias Agopian2b92d892010-02-08 15:49:35 -08002553int GraphicPlane::getHeight() const {
2554 return mHeight;
2555}
2556
2557void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2558{
2559 mHw = hw;
2560
2561 // initialize the display orientation transform.
2562 // it's a constant that should come from the display driver.
2563 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2564 char property[PROPERTY_VALUE_MAX];
2565 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2566 //displayOrientation
2567 switch (atoi(property)) {
2568 case 90:
2569 displayOrientation = ISurfaceComposer::eOrientation90;
2570 break;
2571 case 270:
2572 displayOrientation = ISurfaceComposer::eOrientation270;
2573 break;
2574 }
2575 }
2576
2577 const float w = hw->getWidth();
2578 const float h = hw->getHeight();
2579 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2580 &mDisplayTransform);
2581 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2582 mDisplayWidth = h;
2583 mDisplayHeight = w;
2584 } else {
2585 mDisplayWidth = w;
2586 mDisplayHeight = h;
2587 }
2588
2589 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590}
2591
2592status_t GraphicPlane::orientationToTransfrom(
2593 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002594{
2595 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002596 switch (orientation) {
2597 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002598 flags = Transform::ROT_0;
2599 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002600 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002601 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002602 break;
2603 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002604 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002605 break;
2606 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002607 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002608 break;
2609 default:
2610 return BAD_VALUE;
2611 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002612 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002613 return NO_ERROR;
2614}
2615
2616status_t GraphicPlane::setOrientation(int orientation)
2617{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618 // If the rotation can be handled in hardware, this is where
2619 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002620
2621 const DisplayHardware& hw(displayHardware());
2622 const float w = mDisplayWidth;
2623 const float h = mDisplayHeight;
2624 mWidth = int(w);
2625 mHeight = int(h);
2626
2627 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002628 GraphicPlane::orientationToTransfrom(orientation, w, h,
2629 &orientationTransform);
2630 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2631 mWidth = int(h);
2632 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002634
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002635 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002636 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 return NO_ERROR;
2638}
2639
2640const DisplayHardware& GraphicPlane::displayHardware() const {
2641 return *mHw;
2642}
2643
Mathias Agopian59119e62010-10-11 12:37:43 -07002644DisplayHardware& GraphicPlane::editDisplayHardware() {
2645 return *mHw;
2646}
2647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002648const Transform& GraphicPlane::transform() const {
2649 return mGlobalTransform;
2650}
2651
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002652EGLDisplay GraphicPlane::getEGLDisplay() const {
2653 return mHw->getEGLDisplay();
2654}
2655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656// ---------------------------------------------------------------------------
2657
2658}; // namespace android