blob: 7323a39aa2f09b4e9841060e261acc563b6a39c3 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiandb403e82012-06-18 16:47:56 -070053#include "Client.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080054#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070055#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070058#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070062#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Glenn Kasten1db13d72011-12-19 13:55:34 -080064#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include <private/gui/SharedBufferStack.h>
Mathias Agopianed9807b2012-05-18 14:18:08 -070066#include <gui/BitTube.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070067
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070090 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070092 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070093 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070094 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070095 mDebugInSwapBuffers(0),
96 mLastSwapBufferTime(0),
97 mDebugInTransaction(0),
98 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070099 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 mSecureFrameBuffer(0)
101{
102 init();
103}
104
105void SurfaceFlinger::init()
106{
Steve Blocka19954a2012-01-04 20:05:49 +0000107 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 property_get("debug.sf.showupdates", value, "0");
113 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
Colin Cross3854ed52012-03-23 14:17:18 -0700115#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116 property_get("debug.sf.ddms", value, "0");
117 mDebugDDMS = atoi(value);
118 if (mDebugDDMS) {
119 DdmConnection::start(getServiceName());
120 }
Mathias Agopian5df99622012-06-18 17:27:56 -0700121#else
122#warning "DDMS_DEBUGGING disabled"
Colin Cross3854ed52012-03-23 14:17:18 -0700123#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124
Steve Blocka19954a2012-01-04 20:05:49 +0000125 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000126 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800129void SurfaceFlinger::onFirstRef()
130{
131 mEventQueue.init(this);
132
133 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
134
135 // Wait for the main thread to be done with its initialization
136 mReadyToRunBarrier.wait();
137}
138
139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140SurfaceFlinger::~SurfaceFlinger()
141{
142 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145void SurfaceFlinger::binderDied(const wp<IBinder>& who)
146{
147 // the window manager died on us. prepare its eulogy.
148
149 // reset screen orientation
150 Vector<ComposerState> state;
151 setTransactionState(state, eOrientationDefault, 0);
152
153 // restart the boot-animation
154 property_set("ctl.start", "bootanim");
155}
156
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700157sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700159 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160}
161
Mathias Agopian7e27f052010-05-28 14:22:23 -0700162sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163{
Mathias Agopian96f08192010-06-02 23:28:45 -0700164 sp<ISurfaceComposerClient> bclient;
165 sp<Client> client(new Client(this));
166 status_t err = client->initCheck();
167 if (err == NO_ERROR) {
168 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 return bclient;
171}
172
Jamie Gennis9a78c902011-01-12 18:30:40 -0800173sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
174{
175 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
176 return gba;
177}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
180{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000181 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 const GraphicPlane& plane(mGraphicPlanes[dpy]);
183 return plane;
184}
185
186GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
187{
188 return const_cast<GraphicPlane&>(
189 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
190}
191
192void SurfaceFlinger::bootFinished()
193{
194 const nsecs_t now = systemTime();
195 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000196 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700197 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700198
199 // wait patiently for the window manager death
200 const String16 name("window");
201 sp<IBinder> window(defaultServiceManager()->getService(name));
202 if (window != 0) {
203 window->linkToDeath(this);
204 }
205
206 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700207 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210static inline uint16_t pack565(int r, int g, int b) {
211 return (r<<11)|(g<<5)|b;
212}
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214status_t SurfaceFlinger::readyToRun()
215{
Steve Blocka19954a2012-01-04 20:05:49 +0000216 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 "Initializing graphics H/W...");
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 // we only support one display currently
220 int dpy = 0;
221
222 {
223 // initialize the main display
224 GraphicPlane& plane(graphicPlane(dpy));
225 DisplayHardware* const hw = new DisplayHardware(this, dpy);
226 plane.setDisplayHardware(hw);
227 }
228
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700229 // create the shared control-block
230 mServerHeap = new MemoryHeapBase(4096,
231 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000232 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700233
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700234 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000235 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700236
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700237 new(mServerCblk) surface_flinger_cblk_t;
238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 // initialize primary screen
240 // (other display should be initialized in the same manner, but
241 // asynchronously, as they could come and go. None of this is supported
242 // yet).
243 const GraphicPlane& plane(graphicPlane(dpy));
244 const DisplayHardware& hw = plane.displayHardware();
245 const uint32_t w = hw.getWidth();
246 const uint32_t h = hw.getHeight();
247 const uint32_t f = hw.getFormat();
248 hw.makeCurrent();
249
250 // initialize the shared control block
251 mServerCblk->connected |= 1<<dpy;
252 display_cblk_t* dcblk = mServerCblk->displays + dpy;
253 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800254 dcblk->w = plane.getWidth();
255 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 dcblk->format = f;
257 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
258 dcblk->xdpi = hw.getDpiX();
259 dcblk->ydpi = hw.getDpiY();
260 dcblk->fps = hw.getRefreshRate();
261 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262
263 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700265 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glShadeModel(GL_FLAT);
268 glDisable(GL_DITHER);
269 glDisable(GL_CULL_FACE);
270
271 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
272 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700273 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 glGenTextures(1, &mWormholeTexName);
275 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700281 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
282
283 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
284 glGenTextures(1, &mProtectedTexName);
285 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
290 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
291 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
293 glViewport(0, 0, w, h);
294 glMatrixMode(GL_PROJECTION);
295 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700296 // put the origin in the left-bottom corner
297 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 -0800298
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800299
300 // start the EventThread
301 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800302 mEventQueue.setEventThread(mEventThread);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
304 /*
305 * We're now ready to accept clients...
306 */
307
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800308 mReadyToRunBarrier.open();
309
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800317
Jamie Gennis582270d2011-08-17 18:19:00 -0700318bool SurfaceFlinger::authenticateSurfaceTexture(
319 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800320 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700321 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800322
323 // Check the visible layer list for the ISurface
324 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
325 size_t count = currentLayers.size();
326 for (size_t i=0 ; i<count ; i++) {
327 const sp<LayerBase>& layer(currentLayers[i]);
328 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700329 if (lbc != NULL) {
330 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
331 if (lbcBinder == surfaceTextureBinder) {
332 return true;
333 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800334 }
335 }
336
337 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700338 // SurfaceTexture gets destroyed before all the clients are done using it,
339 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800340 // will instead fail later on when the client tries to use the surface,
341 // which should be reported as "surface XYZ returned an -ENODEV". The
342 // purgatorized layers are no less authentic than the visible ones, so this
343 // should not cause any harm.
344 size_t purgatorySize = mLayerPurgatory.size();
345 for (size_t i=0 ; i<purgatorySize ; i++) {
346 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
347 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700348 if (lbc != NULL) {
349 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
350 if (lbcBinder == surfaceTextureBinder) {
351 return true;
352 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800353 }
354 }
355
356 return false;
357}
358
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800359// ----------------------------------------------------------------------------
360
361sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800362 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700363}
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800366
367void SurfaceFlinger::waitForEvent() {
368 mEventQueue.waitMessage();
369}
370
371void SurfaceFlinger::signalTransaction() {
372 mEventQueue.invalidate();
373}
374
375void SurfaceFlinger::signalLayerUpdate() {
376 mEventQueue.invalidate();
377}
378
379void SurfaceFlinger::signalRefresh() {
380 mEventQueue.refresh();
381}
382
383status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
384 nsecs_t reltime, uint32_t flags) {
385 return mEventQueue.postMessage(msg, reltime);
386}
387
388status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
389 nsecs_t reltime, uint32_t flags) {
390 status_t res = mEventQueue.postMessage(msg, reltime);
391 if (res == NO_ERROR) {
392 msg->wait();
393 }
394 return res;
395}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396
397bool SurfaceFlinger::threadLoop()
398{
399 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400 return true;
401}
402
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800403void SurfaceFlinger::onMessageReceived(int32_t what)
404{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800405 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800406 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700407 case MessageQueue::REFRESH: {
408// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800409 // if we're in a global transaction, don't do anything.
410 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
411 uint32_t transactionFlags = peekTransactionFlags(mask);
412 if (CC_UNLIKELY(transactionFlags)) {
413 handleTransaction(transactionFlags);
414 }
415
416 // post surfaces (if needed)
417 handlePageFlip();
418
Mathias Agopian69a655c2012-04-11 20:43:19 -0700419// signalRefresh();
420//
421// } break;
422//
423// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800424
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800425 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800426
Mathias Agopian303d5382012-02-05 01:49:16 -0800427 const DisplayHardware& hw(graphicPlane(0).displayHardware());
428
Mathias Agopian69a655c2012-04-11 20:43:19 -0700429// if (mDirtyRegion.isEmpty()) {
430// return;
431// }
432
Mathias Agopianb048cef2012-02-04 15:44:04 -0800433 if (CC_UNLIKELY(mHwWorkListDirty)) {
434 // build the h/w work list
435 handleWorkList();
436 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800437
Mathias Agopianb048cef2012-02-04 15:44:04 -0800438 if (CC_LIKELY(hw.canDraw())) {
439 // repaint the framebuffer (if needed)
440 handleRepaint();
441 // inform the h/w that we're done compositing
442 hw.compositionComplete();
443 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800444 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800445 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800446 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800447 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800448
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800449 } break;
450 }
451}
452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453void SurfaceFlinger::postFramebuffer()
454{
Mathias Agopian841cde52012-03-01 15:44:37 -0800455 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800456 // mSwapRegion can be empty here is some cases, for instance if a hidden
457 // or fully transparent window is updating.
458 // in that case, we need to flip anyways to not risk a deadlock with
459 // h/w composer.
460
Mathias Agopiana44b0412011-10-16 18:46:35 -0700461 const DisplayHardware& hw(graphicPlane(0).displayHardware());
462 const nsecs_t now = systemTime();
463 mDebugInSwapBuffers = now;
464 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800465
466 size_t numLayers = mVisibleLayersSortedByZ.size();
467 for (size_t i = 0; i < numLayers; i++) {
468 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
469 }
470
Mathias Agopiana44b0412011-10-16 18:46:35 -0700471 mLastSwapBufferTime = systemTime() - now;
472 mDebugInSwapBuffers = 0;
473 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474}
475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
477{
Mathias Agopian841cde52012-03-01 15:44:37 -0800478 ATRACE_CALL();
479
Mathias Agopianca4d3602011-05-19 15:38:14 -0700480 Mutex::Autolock _l(mStateLock);
481 const nsecs_t now = systemTime();
482 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483
Mathias Agopianca4d3602011-05-19 15:38:14 -0700484 // Here we're guaranteed that some transaction flags are set
485 // so we can call handleTransactionLocked() unconditionally.
486 // We call getTransactionFlags(), which will also clear the flags,
487 // with mStateLock held to guarantee that mCurrentState won't change
488 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700489
Mathias Agopianca4d3602011-05-19 15:38:14 -0700490 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
491 transactionFlags = getTransactionFlags(mask);
492 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700493
Mathias Agopianca4d3602011-05-19 15:38:14 -0700494 mLastTransactionTime = systemTime() - now;
495 mDebugInTransaction = 0;
496 invalidateHwcGeometry();
497 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700498}
499
Mathias Agopianca4d3602011-05-19 15:38:14 -0700500void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700501{
502 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 const size_t count = currentLayers.size();
504
505 /*
506 * Traversal of the children
507 * (perform the transaction for each of them if needed)
508 */
509
510 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
511 if (layersNeedTransaction) {
512 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700513 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
515 if (!trFlags) continue;
516
517 const uint32_t flags = layer->doTransaction(0);
518 if (flags & Layer::eVisibleRegion)
519 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 }
521 }
522
523 /*
524 * Perform our own transaction if needed
525 */
526
527 if (transactionFlags & eTransactionNeeded) {
528 if (mCurrentState.orientation != mDrawingState.orientation) {
529 // the orientation has changed, recompute all visible regions
530 // and invalidate everything.
531
532 const int dpy = 0;
533 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700534 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 GraphicPlane& plane(graphicPlane(dpy));
536 plane.setOrientation(orientation);
537
538 // update the shared control block
539 const DisplayHardware& hw(plane.displayHardware());
540 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
541 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800542 dcblk->w = plane.getWidth();
543 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544
545 mVisibleRegionsDirty = true;
546 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 }
548
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700549 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
550 // layers have been added
551 mVisibleRegionsDirty = true;
552 }
553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 // some layers might have been removed, so
555 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700556 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700557 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700559 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700560 const size_t count = previousLayers.size();
561 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700562 const sp<LayerBase>& layer(previousLayers[i]);
563 if (currentLayers.indexOf( layer ) < 0) {
564 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700565 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700566 }
567 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 }
570
571 commitTransaction();
572}
573
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800575 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576{
Mathias Agopian841cde52012-03-01 15:44:37 -0800577 ATRACE_CALL();
578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 const GraphicPlane& plane(graphicPlane(0));
580 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700581 const DisplayHardware& hw(plane.displayHardware());
582 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583
584 Region aboveOpaqueLayers;
585 Region aboveCoveredLayers;
586 Region dirty;
587
588 bool secureFrameBuffer = false;
589
590 size_t i = currentLayers.size();
591 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700592 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593 layer->validateVisibility(planeTransform);
594
595 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700596 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597
Mathias Agopianab028732010-03-16 16:41:46 -0700598 /*
599 * opaqueRegion: area of a surface that is fully opaque.
600 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700602
603 /*
604 * visibleRegion: area of a surface that is visible on screen
605 * and not fully transparent. This is essentially the layer's
606 * footprint minus the opaque regions above it.
607 * Areas covered by a translucent surface are considered visible.
608 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700610
611 /*
612 * coveredRegion: area of a surface that is covered by all
613 * visible regions above it (which includes the translucent areas).
614 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700616
617
618 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800619 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700620 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700621 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700623 visibleRegion.andSelf(screenRegion);
624 if (!visibleRegion.isEmpty()) {
625 // Remove the transparent area from the visible region
626 if (translucent) {
627 visibleRegion.subtractSelf(layer->transparentRegionScreen);
628 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629
Mathias Agopianab028732010-03-16 16:41:46 -0700630 // compute the opaque region
631 const int32_t layerOrientation = layer->getOrientation();
632 if (s.alpha==255 && !translucent &&
633 ((layerOrientation & Transform::ROT_INVALID) == false)) {
634 // the opaque region is the layer's footprint
635 opaqueRegion = visibleRegion;
636 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638 }
639
Mathias Agopianab028732010-03-16 16:41:46 -0700640 // Clip the covered region to the visible region
641 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
642
643 // Update aboveCoveredLayers for next (lower) layer
644 aboveCoveredLayers.orSelf(visibleRegion);
645
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 // subtract the opaque region covered by the layers above us
647 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648
649 // compute this layer's dirty region
650 if (layer->contentDirty) {
651 // we need to invalidate the whole region
652 dirty = visibleRegion;
653 // as well, as the old visible region
654 dirty.orSelf(layer->visibleRegionScreen);
655 layer->contentDirty = false;
656 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700657 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700658 * the exposed region consists of two components:
659 * 1) what's VISIBLE now and was COVERED before
660 * 2) what's EXPOSED now less what was EXPOSED before
661 *
662 * note that (1) is conservative, we start with the whole
663 * visible region but only keep what used to be covered by
664 * something -- which mean it may have been exposed.
665 *
666 * (2) handles areas that were not covered by anything but got
667 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700668 */
Mathias Agopianab028732010-03-16 16:41:46 -0700669 const Region newExposed = visibleRegion - coveredRegion;
670 const Region oldVisibleRegion = layer->visibleRegionScreen;
671 const Region oldCoveredRegion = layer->coveredRegionScreen;
672 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
673 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 }
675 dirty.subtractSelf(aboveOpaqueLayers);
676
677 // accumulate to the screen dirty region
678 dirtyRegion.orSelf(dirty);
679
Mathias Agopianab028732010-03-16 16:41:46 -0700680 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700682
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 // Store the visible region is screen space
684 layer->setVisibleRegion(visibleRegion);
685 layer->setCoveredRegion(coveredRegion);
686
Mathias Agopian97011222009-07-28 10:57:27 -0700687 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 if (layer->isSecure() && !visibleRegion.isEmpty()) {
689 secureFrameBuffer = true;
690 }
691 }
692
Mathias Agopian97011222009-07-28 10:57:27 -0700693 // invalidate the areas where a layer was removed
694 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
695 mDirtyRegionRemovedLayer.clear();
696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 mSecureFrameBuffer = secureFrameBuffer;
698 opaqueRegion = aboveOpaqueLayers;
699}
700
701
702void SurfaceFlinger::commitTransaction()
703{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800704 if (!mLayersPendingRemoval.isEmpty()) {
705 // Notify removed layers now that they can't be drawn from
706 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
707 mLayersPendingRemoval[i]->onRemoved();
708 }
709 mLayersPendingRemoval.clear();
710 }
711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700713 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700714 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715}
716
717void SurfaceFlinger::handlePageFlip()
718{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800719 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800720 const DisplayHardware& hw = graphicPlane(0).displayHardware();
721 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800723 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
724 const bool visibleRegions = lockPageFlip(currentLayers);
725
726 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 Region opaqueRegion;
728 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700729
730 /*
731 * rebuild the visible layer list
732 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800733 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700734 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700735 mVisibleLayersSortedByZ.setCapacity(count);
736 for (size_t i=0 ; i<count ; i++) {
737 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
738 mVisibleLayersSortedByZ.add(currentLayers[i]);
739 }
740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 mWormholeRegion = screenRegion.subtract(opaqueRegion);
742 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800743 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 }
745
746 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700747
748 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 mDirtyRegion.andSelf(screenRegion);
750}
751
Mathias Agopianad456f92011-01-13 17:53:01 -0800752void SurfaceFlinger::invalidateHwcGeometry()
753{
754 mHwWorkListDirty = true;
755}
756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
758{
759 bool recomputeVisibleRegions = false;
760 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700761 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700763 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 layer->lockPageFlip(recomputeVisibleRegions);
765 }
766 return recomputeVisibleRegions;
767}
768
769void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
770{
771 const GraphicPlane& plane(graphicPlane(0));
772 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800773 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 layer->unlockPageFlip(planeTransform, mDirtyRegion);
778 }
779}
780
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800781void SurfaceFlinger::handleRefresh()
782{
783 bool needInvalidate = false;
784 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
785 const size_t count = currentLayers.size();
786 for (size_t i=0 ; i<count ; i++) {
787 const sp<LayerBase>& layer(currentLayers[i]);
788 if (layer->onPreComposition()) {
789 needInvalidate = true;
790 }
791 }
792 if (needInvalidate) {
793 signalLayerUpdate();
794 }
795}
796
797
Mathias Agopiana350ff92010-08-10 17:14:02 -0700798void SurfaceFlinger::handleWorkList()
799{
800 mHwWorkListDirty = false;
801 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
802 if (hwc.initCheck() == NO_ERROR) {
803 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
804 const size_t count = currentLayers.size();
805 hwc.createWorkList(count);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700806
807 HWComposer::LayerListIterator cur = hwc.begin();
808 const HWComposer::LayerListIterator end = hwc.end();
809 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
810 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700811 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700812 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700813 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700814 }
815 }
816}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818void SurfaceFlinger::handleRepaint()
819{
Mathias Agopian841cde52012-03-01 15:44:37 -0800820 ATRACE_CALL();
821
Mathias Agopianb8a55602009-06-26 19:06:36 -0700822 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700823 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824
Glenn Kasten99ed2242011-12-15 09:51:17 -0800825 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826 debugFlashRegions();
827 }
828
Mathias Agopianb8a55602009-06-26 19:06:36 -0700829 // set the frame buffer
830 const DisplayHardware& hw(graphicPlane(0).displayHardware());
831 glMatrixMode(GL_MODELVIEW);
832 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833
834 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700835 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
837 // takes a rectangle, we must make sure to update that whole
838 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700839 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700840 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700841 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700842 // We need to redraw the rectangle that will be updated
843 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700844 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700846 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700847 } else {
848 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700850 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 }
852 }
853
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700854 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 composeSurfaces(mDirtyRegion);
856
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700857 // update the swap region and clear the dirty region
858 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 mDirtyRegion.clear();
860}
861
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700862void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700864 const DisplayHardware& hw(graphicPlane(0).displayHardware());
865 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700866
867 HWComposer::LayerListIterator cur = hwc.begin();
868 const HWComposer::LayerListIterator end = hwc.end();
869 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700870 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700871 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700872
Mathias Agopianf384cc32011-09-08 18:31:55 -0700873 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
874 size_t count = layers.size();
875
Steve Blocke6f43dd2012-01-06 19:20:56 +0000876 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700877 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
878 hwc.getNumLayers(), count);
879
880 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700881 if (hwc.initCheck() == NO_ERROR) {
882 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
883 }
Mathias Agopian45721772010-08-12 15:03:26 -0700884
885 /*
886 * update the per-frame h/w composer data for each layer
887 * and build the transparent region of the FB
888 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700889 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700890 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700891 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700892 }
893 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000894 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895}
Mathias Agopian45721772010-08-12 15:03:26 -0700896
Mathias Agopianf384cc32011-09-08 18:31:55 -0700897void SurfaceFlinger::composeSurfaces(const Region& dirty)
898{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700899 const DisplayHardware& hw(graphicPlane(0).displayHardware());
900 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700901 HWComposer::LayerListIterator cur = hwc.begin();
902 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700903
904 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700905 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700906 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700907
Mathias Agopianb9494d52012-04-18 02:28:45 -0700908 if (hwc.getLayerCount(HWC_OVERLAY)) {
909 // when using overlays, we assume a fully transparent framebuffer
910 // NOTE: we could reduce how much we need to clear, for instance
911 // remove where there are opaque FB layers. however, on some
912 // GPUs doing a "clean slate" glClear might be more efficient.
913 // We'll revisit later if needed.
914 glClearColor(0, 0, 0, 0);
915 glClear(GL_COLOR_BUFFER_BIT);
916 } else {
917 // screen is already cleared here
918 if (!mWormholeRegion.isEmpty()) {
919 // can happen with SurfaceView
920 drawWormhole();
921 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700922 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700923
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700924 /*
925 * and then, render the layers targeted at the framebuffer
926 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700927
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700928 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
929 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700930 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700931 const sp<LayerBase>& layer(layers[i]);
932 const Region clip(dirty.intersect(layer->visibleRegionScreen));
933 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700934 if (cur->getCompositionType() == HWC_OVERLAY) {
935 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700936 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700937 // never clear the very first layer since we're
938 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700939 layer->clearWithOpenGL(clip);
940 }
941 continue;
942 }
943 // render the layer
944 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700945 }
946 }
947 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948}
949
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950void SurfaceFlinger::debugFlashRegions()
951{
Mathias Agopian0a917752010-06-14 21:20:00 -0700952 const DisplayHardware& hw(graphicPlane(0).displayHardware());
953 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700954 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700955 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700956 return;
957 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700958
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700959 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700960 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
961 mDirtyRegion.bounds() : hw.bounds());
962 composeSurfaces(repaint);
963 }
964
Mathias Agopianc492e672011-10-18 14:49:27 -0700965 glDisable(GL_TEXTURE_EXTERNAL_OES);
966 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968
Mathias Agopian0926f502009-05-04 14:17:04 -0700969 static int toggle = 0;
970 toggle = 1 - toggle;
971 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700972 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700973 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700974 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700975 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976
Mathias Agopian20f68782009-05-11 00:03:41 -0700977 Region::const_iterator it = mDirtyRegion.begin();
978 Region::const_iterator const end = mDirtyRegion.end();
979 while (it != end) {
980 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700982 { r.left, height - r.top },
983 { r.left, height - r.bottom },
984 { r.right, height - r.bottom },
985 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 };
987 glVertexPointer(2, GL_FLOAT, 0, vertices);
988 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
989 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700990
Mathias Agopian0656a682011-09-20 17:22:44 -0700991 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992
993 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700994 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995}
996
997void SurfaceFlinger::drawWormhole() const
998{
999 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1000 if (region.isEmpty())
1001 return;
1002
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001003 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001004 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001005 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001006 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001007
1008 GLfloat vertices[4][2];
1009 glVertexPointer(2, GL_FLOAT, 0, vertices);
1010 Region::const_iterator it = region.begin();
1011 Region::const_iterator const end = region.end();
1012 while (it != end) {
1013 const Rect& r = *it++;
1014 vertices[0][0] = r.left;
1015 vertices[0][1] = r.top;
1016 vertices[1][0] = r.right;
1017 vertices[1][1] = r.top;
1018 vertices[2][0] = r.right;
1019 vertices[2][1] = r.bottom;
1020 vertices[3][0] = r.left;
1021 vertices[3][1] = r.bottom;
1022 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1023 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024}
1025
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001026status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027{
1028 Mutex::Autolock _l(mStateLock);
1029 addLayer_l(layer);
1030 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1031 return NO_ERROR;
1032}
1033
Mathias Agopian96f08192010-06-02 23:28:45 -07001034status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1035{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001036 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001037 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1038}
1039
1040ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1041 const sp<LayerBaseClient>& lbc)
1042{
Mathias Agopian96f08192010-06-02 23:28:45 -07001043 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001044 size_t name = client->attachLayer(lbc);
1045
1046 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001047
1048 // add this layer to the current state list
1049 addLayer_l(lbc);
1050
Mathias Agopian4f113742011-05-03 16:21:41 -07001051 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001052}
1053
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001054status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055{
1056 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001057 status_t err = purgatorizeLayer_l(layer);
1058 if (err == NO_ERROR)
1059 setTransactionFlags(eTransactionNeeded);
1060 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061}
1062
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001063status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001065 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1066 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001067 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001068 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1070 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 return NO_ERROR;
1073 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001074 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075}
1076
Mathias Agopian9a112062009-04-17 19:36:26 -07001077status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1078{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001079 // First add the layer to the purgatory list, which makes sure it won't
1080 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001081 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001082 if (err >= 0) {
1083 mLayerPurgatory.add(layerBase);
1084 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001085
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001086 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001087
Mathias Agopian3d579642009-06-04 18:46:21 -07001088 // it's possible that we don't find a layer, because it might
1089 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001090 // from the user because there is a race between Client::destroySurface(),
1091 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001092 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1093}
1094
Mathias Agopian96f08192010-06-02 23:28:45 -07001095status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096{
Mathias Agopian96f08192010-06-02 23:28:45 -07001097 layer->forceVisibilityTransaction();
1098 setTransactionFlags(eTraversalNeeded);
1099 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100}
1101
Mathias Agopiandea20b12011-05-03 17:04:02 -07001102uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1103{
1104 return android_atomic_release_load(&mTransactionFlags);
1105}
1106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1108{
1109 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1110}
1111
Mathias Agopianbb641242010-05-18 17:06:55 -07001112uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113{
1114 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1115 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001116 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 }
1118 return old;
1119}
1120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001122void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001123 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001124 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001125
Jamie Gennis28378392011-10-12 17:39:00 -07001126 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001127 if (mCurrentState.orientation != orientation) {
1128 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1129 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001130 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001131 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001132 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001133 orientation);
1134 }
1135 }
1136
Mathias Agopian698c0872011-06-28 19:09:31 -07001137 const size_t count = state.size();
1138 for (size_t i=0 ; i<count ; i++) {
1139 const ComposerState& s(state[i]);
1140 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001141 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001142 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001143
Mathias Agopian386aa982011-11-07 21:58:03 -08001144 if (transactionFlags) {
1145 // this triggers the transaction
1146 setTransactionFlags(transactionFlags);
1147
1148 // if this is a synchronous transaction, wait for it to take effect
1149 // before returning.
1150 if (flags & eSynchronous) {
1151 mTransationPending = true;
1152 }
1153 while (mTransationPending) {
1154 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1155 if (CC_UNLIKELY(err != NO_ERROR)) {
1156 // just in case something goes wrong in SF, return to the
1157 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001158 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001159 mTransationPending = false;
1160 break;
1161 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001162 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 }
1164}
1165
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001166sp<ISurface> SurfaceFlinger::createSurface(
1167 ISurfaceComposerClient::surface_data_t* params,
1168 const String8& name,
1169 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1171 uint32_t flags)
1172{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001173 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001174 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001175
1176 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001177 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001178 int(w), int(h));
1179 return surfaceHandle;
1180 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001181
Mathias Agopianff615cc2012-02-24 14:58:36 -08001182 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001183 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 switch (flags & eFXSurfaceMask) {
1185 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001186 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1187 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 break;
1189 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001190 // for now we treat Blur as Dim, until we can implement it
1191 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001193 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001195 case eFXSurfaceScreenshot:
1196 layer = createScreenshotSurface(client, d, w, h, flags);
1197 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 }
1199
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001200 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001201 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001202 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001203 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001206 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001207 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001208 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209 if (normalLayer != 0) {
1210 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001211 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001213 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001214
Mathias Agopian96f08192010-06-02 23:28:45 -07001215 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216 }
1217
1218 return surfaceHandle;
1219}
1220
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001221sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001222 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001224 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225{
1226 // initialize the surfaces
1227 switch (format) { // TODO: take h/w into account
1228 case PIXEL_FORMAT_TRANSPARENT:
1229 case PIXEL_FORMAT_TRANSLUCENT:
1230 format = PIXEL_FORMAT_RGBA_8888;
1231 break;
1232 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001233#ifdef NO_RGBX_8888
1234 format = PIXEL_FORMAT_RGB_565;
1235#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001236 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001237#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 break;
1239 }
1240
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001241#ifdef NO_RGBX_8888
1242 if (format == PIXEL_FORMAT_RGBX_8888)
1243 format = PIXEL_FORMAT_RGBA_8888;
1244#endif
1245
Mathias Agopian96f08192010-06-02 23:28:45 -07001246 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001247 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001248 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001249 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001250 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 }
1252 return layer;
1253}
1254
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001255sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001256 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001257 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258{
Mathias Agopian96f08192010-06-02 23:28:45 -07001259 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001260 return layer;
1261}
1262
1263sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1264 const sp<Client>& client, DisplayID display,
1265 uint32_t w, uint32_t h, uint32_t flags)
1266{
1267 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 return layer;
1269}
1270
Mathias Agopian96f08192010-06-02 23:28:45 -07001271status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272{
Mathias Agopian9a112062009-04-17 19:36:26 -07001273 /*
1274 * called by the window manager, when a surface should be marked for
1275 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001276 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001277 * The surface is removed from the current and drawing lists, but placed
1278 * in the purgatory queue, so it's not destroyed right-away (we need
1279 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001280 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001281
Mathias Agopian48d819a2009-09-10 19:41:18 -07001282 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001283 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001284 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001285
Mathias Agopian48d819a2009-09-10 19:41:18 -07001286 if (layer != 0) {
1287 err = purgatorizeLayer_l(layer);
1288 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001289 setTransactionFlags(eTransactionNeeded);
1290 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001291 }
1292 return err;
1293}
1294
Mathias Agopianca4d3602011-05-19 15:38:14 -07001295status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001296{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001297 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001298 status_t err = NO_ERROR;
1299 sp<LayerBaseClient> l(layer.promote());
1300 if (l != NULL) {
1301 Mutex::Autolock _l(mStateLock);
1302 err = removeLayer_l(l);
1303 if (err == NAME_NOT_FOUND) {
1304 // The surface wasn't in the current list, which means it was
1305 // removed already, which means it is in the purgatory,
1306 // and need to be removed from there.
1307 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001308 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001309 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001310 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001311 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001312 "error removing layer=%p (%s)", l.get(), strerror(-err));
1313 }
1314 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315}
1316
Mathias Agopian698c0872011-06-28 19:09:31 -07001317uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001319 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001322 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1323 if (layer != 0) {
1324 const uint32_t what = s.what;
1325 if (what & ePositionChanged) {
1326 if (layer->setPosition(s.x, s.y))
1327 flags |= eTraversalNeeded;
1328 }
1329 if (what & eLayerChanged) {
1330 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1331 if (layer->setLayer(s.z)) {
1332 mCurrentState.layersSortedByZ.removeAt(idx);
1333 mCurrentState.layersSortedByZ.add(layer);
1334 // we need traversal (state changed)
1335 // AND transaction (list changed)
1336 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 }
1338 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001339 if (what & eSizeChanged) {
1340 if (layer->setSize(s.w, s.h)) {
1341 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001342 }
1343 }
1344 if (what & eAlphaChanged) {
1345 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1346 flags |= eTraversalNeeded;
1347 }
1348 if (what & eMatrixChanged) {
1349 if (layer->setMatrix(s.matrix))
1350 flags |= eTraversalNeeded;
1351 }
1352 if (what & eTransparentRegionChanged) {
1353 if (layer->setTransparentRegionHint(s.transparentRegion))
1354 flags |= eTraversalNeeded;
1355 }
1356 if (what & eVisibilityChanged) {
1357 if (layer->setFlags(s.flags, s.mask))
1358 flags |= eTraversalNeeded;
1359 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001360 if (what & eCropChanged) {
1361 if (layer->setCrop(s.crop))
1362 flags |= eTraversalNeeded;
1363 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001365 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366}
1367
Mathias Agopianb60314a2012-04-10 22:09:54 -07001368// ---------------------------------------------------------------------------
1369
1370void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001371 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001372 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1373 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001374 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001375 // this is a temporary work-around, eventually this should be called
1376 // by the power-manager
1377 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001378 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001379 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380}
1381
Mathias Agopianb60314a2012-04-10 22:09:54 -07001382void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001383 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001384 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1385 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001386 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001387 hw.releaseScreen();
1388 // from this point on, SF will stop drawing
1389 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390}
1391
Colin Cross8e533062012-06-07 13:17:52 -07001392void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001393 class MessageScreenAcquired : public MessageBase {
1394 SurfaceFlinger* flinger;
1395 public:
1396 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1397 virtual bool handler() {
1398 flinger->onScreenAcquired();
1399 return true;
1400 }
1401 };
1402 sp<MessageBase> msg = new MessageScreenAcquired(this);
1403 postMessageSync(msg);
1404}
1405
Colin Cross8e533062012-06-07 13:17:52 -07001406void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001407 class MessageScreenReleased : public MessageBase {
1408 SurfaceFlinger* flinger;
1409 public:
1410 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1411 virtual bool handler() {
1412 flinger->onScreenReleased();
1413 return true;
1414 }
1415 };
1416 sp<MessageBase> msg = new MessageScreenReleased(this);
1417 postMessageSync(msg);
1418}
1419
1420// ---------------------------------------------------------------------------
1421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1423{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001424 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 char buffer[SIZE];
1426 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001427
1428 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 snprintf(buffer, SIZE, "Permission Denial: "
1430 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1431 IPCThreadState::self()->getCallingPid(),
1432 IPCThreadState::self()->getCallingUid());
1433 result.append(buffer);
1434 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001435 // Try to get the main lock, but don't insist if we can't
1436 // (this would indicate SF is stuck, but we want to be able to
1437 // print something in dumpsys).
1438 int retry = 3;
1439 while (mStateLock.tryLock()<0 && --retry>=0) {
1440 usleep(1000000);
1441 }
1442 const bool locked(retry >= 0);
1443 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001444 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001445 "SurfaceFlinger appears to be unresponsive, "
1446 "dumping anyways (no locks held)\n");
1447 result.append(buffer);
1448 }
1449
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001450 bool dumpAll = true;
1451 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001452 size_t numArgs = args.size();
1453 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001454 if ((index < numArgs) &&
1455 (args[index] == String16("--list"))) {
1456 index++;
1457 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001458 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001459 }
1460
1461 if ((index < numArgs) &&
1462 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001463 index++;
1464 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001465 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001466 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001467
1468 if ((index < numArgs) &&
1469 (args[index] == String16("--latency-clear"))) {
1470 index++;
1471 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001472 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001473 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001475
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001476 if (dumpAll) {
1477 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001478 }
1479
Mathias Agopian9795c422009-08-26 16:36:26 -07001480 if (locked) {
1481 mStateLock.unlock();
1482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 }
1484 write(fd, result.string(), result.size());
1485 return NO_ERROR;
1486}
1487
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001488void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1489 String8& result, char* buffer, size_t SIZE) const
1490{
1491 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1492 const size_t count = currentLayers.size();
1493 for (size_t i=0 ; i<count ; i++) {
1494 const sp<LayerBase>& layer(currentLayers[i]);
1495 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1496 result.append(buffer);
1497 }
1498}
1499
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001500void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1501 String8& result, char* buffer, size_t SIZE) const
1502{
1503 String8 name;
1504 if (index < args.size()) {
1505 name = String8(args[index]);
1506 index++;
1507 }
1508
1509 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1510 const size_t count = currentLayers.size();
1511 for (size_t i=0 ; i<count ; i++) {
1512 const sp<LayerBase>& layer(currentLayers[i]);
1513 if (name.isEmpty()) {
1514 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1515 result.append(buffer);
1516 }
1517 if (name.isEmpty() || (name == layer->getName())) {
1518 layer->dumpStats(result, buffer, SIZE);
1519 }
1520 }
1521}
1522
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001523void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1524 String8& result, char* buffer, size_t SIZE) const
1525{
1526 String8 name;
1527 if (index < args.size()) {
1528 name = String8(args[index]);
1529 index++;
1530 }
1531
1532 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1533 const size_t count = currentLayers.size();
1534 for (size_t i=0 ; i<count ; i++) {
1535 const sp<LayerBase>& layer(currentLayers[i]);
1536 if (name.isEmpty() || (name == layer->getName())) {
1537 layer->clearStats();
1538 }
1539 }
1540}
1541
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001542void SurfaceFlinger::dumpAllLocked(
1543 String8& result, char* buffer, size_t SIZE) const
1544{
1545 // figure out if we're stuck somewhere
1546 const nsecs_t now = systemTime();
1547 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1548 const nsecs_t inTransaction(mDebugInTransaction);
1549 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1550 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1551
1552 /*
1553 * Dump the visible layer list
1554 */
1555 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1556 const size_t count = currentLayers.size();
1557 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1558 result.append(buffer);
1559 for (size_t i=0 ; i<count ; i++) {
1560 const sp<LayerBase>& layer(currentLayers[i]);
1561 layer->dump(result, buffer, SIZE);
1562 }
1563
1564 /*
1565 * Dump the layers in the purgatory
1566 */
1567
1568 const size_t purgatorySize = mLayerPurgatory.size();
1569 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1570 result.append(buffer);
1571 for (size_t i=0 ; i<purgatorySize ; i++) {
1572 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1573 layer->shortDump(result, buffer, SIZE);
1574 }
1575
1576 /*
1577 * Dump SurfaceFlinger global state
1578 */
1579
1580 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1581 result.append(buffer);
1582
1583 const GLExtensions& extensions(GLExtensions::getInstance());
1584 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1585 extensions.getVendor(),
1586 extensions.getRenderer(),
1587 extensions.getVersion());
1588 result.append(buffer);
1589
1590 snprintf(buffer, SIZE, "EGL : %s\n",
1591 eglQueryString(graphicPlane(0).getEGLDisplay(),
1592 EGL_VERSION_HW_ANDROID));
1593 result.append(buffer);
1594
1595 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1596 result.append(buffer);
1597
1598 mWormholeRegion.dump(result, "WormholeRegion");
1599 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1600 snprintf(buffer, SIZE,
1601 " orientation=%d, canDraw=%d\n",
1602 mCurrentState.orientation, hw.canDraw());
1603 result.append(buffer);
1604 snprintf(buffer, SIZE,
1605 " last eglSwapBuffers() time: %f us\n"
1606 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001607 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001608 " refresh-rate : %f fps\n"
1609 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001610 " y-dpi : %f\n"
1611 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001612 mLastSwapBufferTime/1000.0,
1613 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001614 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001615 hw.getRefreshRate(),
1616 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001617 hw.getDpiY(),
1618 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001619 result.append(buffer);
1620
1621 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1622 inSwapBuffersDuration/1000.0);
1623 result.append(buffer);
1624
1625 snprintf(buffer, SIZE, " transaction time: %f us\n",
1626 inTransactionDuration/1000.0);
1627 result.append(buffer);
1628
1629 /*
1630 * VSYNC state
1631 */
1632 mEventThread->dump(result, buffer, SIZE);
1633
1634 /*
1635 * Dump HWComposer state
1636 */
1637 HWComposer& hwc(hw.getHwComposer());
1638 snprintf(buffer, SIZE, "h/w composer state:\n");
1639 result.append(buffer);
1640 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1641 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1642 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1643 result.append(buffer);
1644 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1645
1646 /*
1647 * Dump gralloc state
1648 */
1649 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1650 alloc.dump(result);
1651 hw.dump(result);
1652}
1653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654status_t SurfaceFlinger::onTransact(
1655 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1656{
1657 switch (code) {
1658 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001659 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001662 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001663 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001664 case BLANK:
1665 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 {
1667 // codes that require permission check
1668 IPCThreadState* ipc = IPCThreadState::self();
1669 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001670 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001671 if ((uid != AID_GRAPHICS) &&
1672 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001673 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001674 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1675 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001677 break;
1678 }
1679 case CAPTURE_SCREEN:
1680 {
1681 // codes that require permission check
1682 IPCThreadState* ipc = IPCThreadState::self();
1683 const int pid = ipc->getCallingPid();
1684 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001685 if ((uid != AID_GRAPHICS) &&
1686 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001687 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001688 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1689 return PERMISSION_DENIED;
1690 }
1691 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 }
1693 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1696 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001697 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001698 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001699 IPCThreadState* ipc = IPCThreadState::self();
1700 const int pid = ipc->getCallingPid();
1701 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001702 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001703 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 return PERMISSION_DENIED;
1705 }
1706 int n;
1707 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001708 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001709 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 return NO_ERROR;
1711 case 1002: // SHOW_UPDATES
1712 n = data.readInt32();
1713 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001714 invalidateHwcGeometry();
1715 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001718 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001719 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001721 case 1005:{ // force transaction
1722 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1723 return NO_ERROR;
1724 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001725 case 1006:{ // send empty update
1726 signalRefresh();
1727 return NO_ERROR;
1728 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001729 case 1008: // toggle use of hw composer
1730 n = data.readInt32();
1731 mDebugDisableHWC = n ? 1 : 0;
1732 invalidateHwcGeometry();
1733 repaintEverything();
1734 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001735 case 1009: // toggle use of transform hint
1736 n = data.readInt32();
1737 mDebugDisableTransformHint = n ? 1 : 0;
1738 invalidateHwcGeometry();
1739 repaintEverything();
1740 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001742 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 reply->writeInt32(0);
1744 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001745 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001746 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 return NO_ERROR;
1748 case 1013: {
1749 Mutex::Autolock _l(mStateLock);
1750 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1751 reply->writeInt32(hw.getPageFlipCount());
1752 }
1753 return NO_ERROR;
1754 }
1755 }
1756 return err;
1757}
1758
Mathias Agopian53331da2011-08-22 21:44:41 -07001759void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001760 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001761 const Rect bounds(hw.getBounds());
1762 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001763 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001764}
1765
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001766void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1767 Mutex::Autolock _l(mInvalidateLock);
1768 mInvalidateRegion = reg;
1769}
1770
1771Region SurfaceFlinger::getAndClearInvalidateRegion() {
1772 Mutex::Autolock _l(mInvalidateLock);
1773 Region reg(mInvalidateRegion);
1774 mInvalidateRegion.clear();
1775 return reg;
1776}
1777
Mathias Agopian59119e62010-10-11 12:37:43 -07001778// ---------------------------------------------------------------------------
1779
Mathias Agopian118d0242011-10-13 16:02:48 -07001780status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1781 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1782{
1783 Mutex::Autolock _l(mStateLock);
1784 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1785}
1786
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001787status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1788 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001789{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001790 ATRACE_CALL();
1791
Mathias Agopian59119e62010-10-11 12:37:43 -07001792 if (!GLExtensions::getInstance().haveFramebufferObject())
1793 return INVALID_OPERATION;
1794
1795 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001797 const uint32_t hw_w = hw.getWidth();
1798 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001799 GLfloat u = 1;
1800 GLfloat v = 1;
1801
1802 // make sure to clear all GL error flags
1803 while ( glGetError() != GL_NO_ERROR ) ;
1804
1805 // create a FBO
1806 GLuint name, tname;
1807 glGenTextures(1, &tname);
1808 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001809 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1810 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001811 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1812 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001813 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001814 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001815 GLint tw = (2 << (31 - clz(hw_w)));
1816 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001817 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1818 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001819 u = GLfloat(hw_w) / tw;
1820 v = GLfloat(hw_h) / th;
1821 }
1822 glGenFramebuffersOES(1, &name);
1823 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001824 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1825 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001826
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001827 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001828 glDisable(GL_TEXTURE_EXTERNAL_OES);
1829 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001830 glClearColor(0,0,0,1);
1831 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001832 glMatrixMode(GL_MODELVIEW);
1833 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001834 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1835 const size_t count = layers.size();
1836 for (size_t i=0 ; i<count ; ++i) {
1837 const sp<LayerBase>& layer(layers[i]);
1838 layer->drawForSreenShot();
1839 }
1840
Mathias Agopian118d0242011-10-13 16:02:48 -07001841 hw.compositionComplete();
1842
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001843 // back to main framebuffer
1844 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001845 glDeleteFramebuffersOES(1, &name);
1846
1847 *textureName = tname;
1848 *uOut = u;
1849 *vOut = v;
1850 return NO_ERROR;
1851}
1852
1853// ---------------------------------------------------------------------------
1854
Mathias Agopianed9807b2012-05-18 14:18:08 -07001855class VSyncWaiter {
1856 DisplayEventReceiver::Event buffer[4];
1857 sp<Looper> looper;
1858 sp<IDisplayEventConnection> events;
1859 sp<BitTube> eventTube;
1860public:
1861 VSyncWaiter(const sp<EventThread>& eventThread) {
1862 looper = new Looper(true);
1863 events = eventThread->createEventConnection();
1864 eventTube = events->getDataChannel();
1865 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1866 events->requestNextVsync();
1867 }
1868
1869 void wait() {
1870 ssize_t n;
1871
1872 looper->pollOnce(-1);
1873 // we don't handle any errors here, it doesn't matter
1874 // and we don't want to take the risk to get stuck.
1875
1876 // drain the events...
1877 while ((n = DisplayEventReceiver::getEvents(
1878 eventTube, buffer, 4)) > 0) ;
1879
1880 events->requestNextVsync();
1881 }
1882};
1883
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001884status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1885{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001886 // get screen geometry
1887 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1888 const uint32_t hw_w = hw.getWidth();
1889 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001890 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001891
1892 GLfloat u, v;
1893 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001894 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001895 if (result != NO_ERROR) {
1896 return result;
1897 }
1898
1899 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001900 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001901 glBindTexture(GL_TEXTURE_2D, tname);
1902 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1903 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1904 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001905 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1906 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001907 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1908 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1909 glVertexPointer(2, GL_FLOAT, 0, vtx);
1910
Mathias Agopianffcf4652011-07-07 17:30:31 -07001911 /*
1912 * Texture coordinate mapping
1913 *
1914 * u
1915 * 1 +----------+---+
1916 * | | | | image is inverted
1917 * | V | | w.r.t. the texture
1918 * 1-v +----------+ | coordinates
1919 * | |
1920 * | |
1921 * | |
1922 * 0 +--------------+
1923 * 0 1
1924 *
1925 */
1926
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001927 class s_curve_interpolator {
1928 const float nbFrames, s, v;
1929 public:
1930 s_curve_interpolator(int nbFrames, float s)
1931 : nbFrames(1.0f / (nbFrames-1)), s(s),
1932 v(1.0f + expf(-s + 0.5f*s)) {
1933 }
1934 float operator()(int f) {
1935 const float x = f * nbFrames;
1936 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1937 }
1938 };
1939
1940 class v_stretch {
1941 const GLfloat hw_w, hw_h;
1942 public:
1943 v_stretch(uint32_t hw_w, uint32_t hw_h)
1944 : hw_w(hw_w), hw_h(hw_h) {
1945 }
1946 void operator()(GLfloat* vtx, float v) {
1947 const GLfloat w = hw_w + (hw_w * v);
1948 const GLfloat h = hw_h - (hw_h * v);
1949 const GLfloat x = (hw_w - w) * 0.5f;
1950 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001951 vtx[0] = x; vtx[1] = y;
1952 vtx[2] = x; vtx[3] = y + h;
1953 vtx[4] = x + w; vtx[5] = y + h;
1954 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001955 }
1956 };
1957
1958 class h_stretch {
1959 const GLfloat hw_w, hw_h;
1960 public:
1961 h_stretch(uint32_t hw_w, uint32_t hw_h)
1962 : hw_w(hw_w), hw_h(hw_h) {
1963 }
1964 void operator()(GLfloat* vtx, float v) {
1965 const GLfloat w = hw_w - (hw_w * v);
1966 const GLfloat h = 1.0f;
1967 const GLfloat x = (hw_w - w) * 0.5f;
1968 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001969 vtx[0] = x; vtx[1] = y;
1970 vtx[2] = x; vtx[3] = y + h;
1971 vtx[4] = x + w; vtx[5] = y + h;
1972 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001973 }
1974 };
1975
Mathias Agopianed9807b2012-05-18 14:18:08 -07001976 VSyncWaiter vsync(mEventThread);
1977
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001978 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001979 char value[PROPERTY_VALUE_MAX];
1980 property_get("debug.sf.electron_frames", value, "24");
1981 int nbFrames = (atoi(value) + 1) >> 1;
1982 if (nbFrames <= 0) // just in case
1983 nbFrames = 24;
1984
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 s_curve_interpolator itr(nbFrames, 7.5f);
1986 s_curve_interpolator itg(nbFrames, 8.0f);
1987 s_curve_interpolator itb(nbFrames, 8.5f);
1988
1989 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001990
1991 glMatrixMode(GL_TEXTURE);
1992 glLoadIdentity();
1993 glMatrixMode(GL_MODELVIEW);
1994 glLoadIdentity();
1995
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001996 glEnable(GL_BLEND);
1997 glBlendFunc(GL_ONE, GL_ONE);
1998 for (int i=0 ; i<nbFrames ; i++) {
1999 float x, y, w, h;
2000 const float vr = itr(i);
2001 const float vg = itg(i);
2002 const float vb = itb(i);
2003
Mathias Agopianed9807b2012-05-18 14:18:08 -07002004 // wait for vsync
2005 vsync.wait();
2006
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002007 // clear screen
2008 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002009 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002010 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002011
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002012 // draw the red plane
2013 vverts(vtx, vr);
2014 glColorMask(1,0,0,1);
2015 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002016
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002017 // draw the green plane
2018 vverts(vtx, vg);
2019 glColorMask(0,1,0,1);
2020 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002021
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002022 // draw the blue plane
2023 vverts(vtx, vb);
2024 glColorMask(0,0,1,1);
2025 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002026
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002027 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002028 glDisable(GL_TEXTURE_2D);
2029 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002030 glColor4f(vg, vg, vg, 1);
2031 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2032 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002033 }
2034
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002035 h_stretch hverts(hw_w, hw_h);
2036 glDisable(GL_BLEND);
2037 glDisable(GL_TEXTURE_2D);
2038 glColorMask(1,1,1,1);
2039 for (int i=0 ; i<nbFrames ; i++) {
2040 const float v = itg(i);
2041 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002042
2043 // wait for vsync
2044 vsync.wait();
2045
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002046 glClear(GL_COLOR_BUFFER_BIT);
2047 glColor4f(1-v, 1-v, 1-v, 1);
2048 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2049 hw.flip(screenBounds);
2050 }
2051
2052 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2054 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002055 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002056 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002057 return NO_ERROR;
2058}
2059
2060status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2061{
2062 status_t result = PERMISSION_DENIED;
2063
2064 if (!GLExtensions::getInstance().haveFramebufferObject())
2065 return INVALID_OPERATION;
2066
2067
2068 // get screen geometry
2069 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2070 const uint32_t hw_w = hw.getWidth();
2071 const uint32_t hw_h = hw.getHeight();
2072 const Region screenBounds(hw.bounds());
2073
2074 GLfloat u, v;
2075 GLuint tname;
2076 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2077 if (result != NO_ERROR) {
2078 return result;
2079 }
2080
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002081 GLfloat vtx[8];
2082 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002083 glBindTexture(GL_TEXTURE_2D, tname);
2084 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2085 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2086 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002087 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2088 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002089 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2090 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2091 glVertexPointer(2, GL_FLOAT, 0, vtx);
2092
2093 class s_curve_interpolator {
2094 const float nbFrames, s, v;
2095 public:
2096 s_curve_interpolator(int nbFrames, float s)
2097 : nbFrames(1.0f / (nbFrames-1)), s(s),
2098 v(1.0f + expf(-s + 0.5f*s)) {
2099 }
2100 float operator()(int f) {
2101 const float x = f * nbFrames;
2102 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2103 }
2104 };
2105
2106 class v_stretch {
2107 const GLfloat hw_w, hw_h;
2108 public:
2109 v_stretch(uint32_t hw_w, uint32_t hw_h)
2110 : hw_w(hw_w), hw_h(hw_h) {
2111 }
2112 void operator()(GLfloat* vtx, float v) {
2113 const GLfloat w = hw_w + (hw_w * v);
2114 const GLfloat h = hw_h - (hw_h * v);
2115 const GLfloat x = (hw_w - w) * 0.5f;
2116 const GLfloat y = (hw_h - h) * 0.5f;
2117 vtx[0] = x; vtx[1] = y;
2118 vtx[2] = x; vtx[3] = y + h;
2119 vtx[4] = x + w; vtx[5] = y + h;
2120 vtx[6] = x + w; vtx[7] = y;
2121 }
2122 };
2123
2124 class h_stretch {
2125 const GLfloat hw_w, hw_h;
2126 public:
2127 h_stretch(uint32_t hw_w, uint32_t hw_h)
2128 : hw_w(hw_w), hw_h(hw_h) {
2129 }
2130 void operator()(GLfloat* vtx, float v) {
2131 const GLfloat w = hw_w - (hw_w * v);
2132 const GLfloat h = 1.0f;
2133 const GLfloat x = (hw_w - w) * 0.5f;
2134 const GLfloat y = (hw_h - h) * 0.5f;
2135 vtx[0] = x; vtx[1] = y;
2136 vtx[2] = x; vtx[3] = y + h;
2137 vtx[4] = x + w; vtx[5] = y + h;
2138 vtx[6] = x + w; vtx[7] = y;
2139 }
2140 };
2141
Mathias Agopianed9807b2012-05-18 14:18:08 -07002142 VSyncWaiter vsync(mEventThread);
2143
Mathias Agopiana6546e52010-10-14 12:33:07 -07002144 // the full animation is 12 frames
2145 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002146 s_curve_interpolator itr(nbFrames, 7.5f);
2147 s_curve_interpolator itg(nbFrames, 8.0f);
2148 s_curve_interpolator itb(nbFrames, 8.5f);
2149
2150 h_stretch hverts(hw_w, hw_h);
2151 glDisable(GL_BLEND);
2152 glDisable(GL_TEXTURE_2D);
2153 glColorMask(1,1,1,1);
2154 for (int i=nbFrames-1 ; i>=0 ; i--) {
2155 const float v = itg(i);
2156 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002157
2158 // wait for vsync
2159 vsync.wait();
2160
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002161 glClear(GL_COLOR_BUFFER_BIT);
2162 glColor4f(1-v, 1-v, 1-v, 1);
2163 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2164 hw.flip(screenBounds);
2165 }
2166
Mathias Agopiana6546e52010-10-14 12:33:07 -07002167 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002168 v_stretch vverts(hw_w, hw_h);
2169 glEnable(GL_BLEND);
2170 glBlendFunc(GL_ONE, GL_ONE);
2171 for (int i=nbFrames-1 ; i>=0 ; i--) {
2172 float x, y, w, h;
2173 const float vr = itr(i);
2174 const float vg = itg(i);
2175 const float vb = itb(i);
2176
Mathias Agopianed9807b2012-05-18 14:18:08 -07002177 // wait for vsync
2178 vsync.wait();
2179
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002180 // clear screen
2181 glColorMask(1,1,1,1);
2182 glClear(GL_COLOR_BUFFER_BIT);
2183 glEnable(GL_TEXTURE_2D);
2184
2185 // draw the red plane
2186 vverts(vtx, vr);
2187 glColorMask(1,0,0,1);
2188 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2189
2190 // draw the green plane
2191 vverts(vtx, vg);
2192 glColorMask(0,1,0,1);
2193 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2194
2195 // draw the blue plane
2196 vverts(vtx, vb);
2197 glColorMask(0,0,1,1);
2198 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2199
2200 hw.flip(screenBounds);
2201 }
2202
2203 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002204 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002205 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002206 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002207 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002208
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002209 return NO_ERROR;
2210}
2211
2212// ---------------------------------------------------------------------------
2213
Mathias Agopianabd671a2010-10-14 14:54:06 -07002214status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002215{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002216 ATRACE_CALL();
2217
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002218 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2219 if (!hw.canDraw()) {
2220 // we're already off
2221 return NO_ERROR;
2222 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002223
2224 // turn off hwc while we're doing the animation
2225 hw.getHwComposer().disable();
2226 // and make sure to turn it back on (if needed) next time we compose
2227 invalidateHwcGeometry();
2228
Mathias Agopianabd671a2010-10-14 14:54:06 -07002229 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2230 electronBeamOffAnimationImplLocked();
2231 }
2232
2233 // always clear the whole screen at the end of the animation
2234 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002235 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002236 hw.flip( Region(hw.bounds()) );
2237
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002238 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002239}
2240
2241status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2242{
Mathias Agopian59119e62010-10-11 12:37:43 -07002243 class MessageTurnElectronBeamOff : public MessageBase {
2244 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002245 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002246 status_t result;
2247 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002248 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2249 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002250 }
2251 status_t getResult() const {
2252 return result;
2253 }
2254 virtual bool handler() {
2255 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002256 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002257 return true;
2258 }
2259 };
2260
Mathias Agopianabd671a2010-10-14 14:54:06 -07002261 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002262 status_t res = postMessageSync(msg);
2263 if (res == NO_ERROR) {
2264 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002265
2266 // work-around: when the power-manager calls us we activate the
2267 // animation. eventually, the "on" animation will be called
2268 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002269 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002270 }
2271 return res;
2272}
2273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002274// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002275
Mathias Agopianabd671a2010-10-14 14:54:06 -07002276status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002277{
2278 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2279 if (hw.canDraw()) {
2280 // we're already on
2281 return NO_ERROR;
2282 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002283 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2284 electronBeamOnAnimationImplLocked();
2285 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002286
2287 // make sure to redraw the whole screen when the animation is done
2288 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002289 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002290
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002291 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002292}
2293
2294status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2295{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002296 class MessageTurnElectronBeamOn : public MessageBase {
2297 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002298 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002299 status_t result;
2300 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002301 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2302 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002303 }
2304 status_t getResult() const {
2305 return result;
2306 }
2307 virtual bool handler() {
2308 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002309 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002310 return true;
2311 }
2312 };
2313
Mathias Agopianabd671a2010-10-14 14:54:06 -07002314 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002315 return NO_ERROR;
2316}
2317
2318// ---------------------------------------------------------------------------
2319
Mathias Agopian74c40c02010-09-29 13:02:36 -07002320status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2321 sp<IMemoryHeap>* heap,
2322 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002323 uint32_t sw, uint32_t sh,
2324 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002325{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002326 ATRACE_CALL();
2327
Mathias Agopian74c40c02010-09-29 13:02:36 -07002328 status_t result = PERMISSION_DENIED;
2329
2330 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002331 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002332 return BAD_VALUE;
2333
2334 if (!GLExtensions::getInstance().haveFramebufferObject())
2335 return INVALID_OPERATION;
2336
2337 // get screen geometry
2338 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2339 const uint32_t hw_w = hw.getWidth();
2340 const uint32_t hw_h = hw.getHeight();
2341
2342 if ((sw > hw_w) || (sh > hw_h))
2343 return BAD_VALUE;
2344
2345 sw = (!sw) ? hw_w : sw;
2346 sh = (!sh) ? hw_h : sh;
2347 const size_t size = sw * sh * 4;
2348
Steve Block9d453682011-12-20 16:23:08 +00002349 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002350 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002351
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 // make sure to clear all GL error flags
2353 while ( glGetError() != GL_NO_ERROR ) ;
2354
2355 // create a FBO
2356 GLuint name, tname;
2357 glGenRenderbuffersOES(1, &tname);
2358 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2359 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002360
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361 glGenFramebuffersOES(1, &name);
2362 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2363 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2364 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2365
2366 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002367
Mathias Agopian74c40c02010-09-29 13:02:36 -07002368 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2369
2370 // invert everything, b/c glReadPixel() below will invert the FB
2371 glViewport(0, 0, sw, sh);
2372 glMatrixMode(GL_PROJECTION);
2373 glPushMatrix();
2374 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002375 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376 glMatrixMode(GL_MODELVIEW);
2377
2378 // redraw the screen entirely...
2379 glClearColor(0,0,0,1);
2380 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002381
Jamie Gennis9575f602011-10-07 14:51:16 -07002382 const LayerVector& layers(mDrawingState.layersSortedByZ);
2383 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 for (size_t i=0 ; i<count ; ++i) {
2385 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002386 const uint32_t flags = layer->drawingState().flags;
2387 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2388 const uint32_t z = layer->drawingState().z;
2389 if (z >= minLayerZ && z <= maxLayerZ) {
2390 layer->drawForSreenShot();
2391 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002392 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002393 }
2394
Mathias Agopian74c40c02010-09-29 13:02:36 -07002395 // check for errors and return screen capture
2396 if (glGetError() != GL_NO_ERROR) {
2397 // error while rendering
2398 result = INVALID_OPERATION;
2399 } else {
2400 // allocate shared memory large enough to hold the
2401 // screen capture
2402 sp<MemoryHeapBase> base(
2403 new MemoryHeapBase(size, 0, "screen-capture") );
2404 void* const ptr = base->getBase();
2405 if (ptr) {
2406 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002407 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002408 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2409 if (glGetError() == GL_NO_ERROR) {
2410 *heap = base;
2411 *w = sw;
2412 *h = sh;
2413 *f = PIXEL_FORMAT_RGBA_8888;
2414 result = NO_ERROR;
2415 }
2416 } else {
2417 result = NO_MEMORY;
2418 }
2419 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002420 glViewport(0, 0, hw_w, hw_h);
2421 glMatrixMode(GL_PROJECTION);
2422 glPopMatrix();
2423 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002424 } else {
2425 result = BAD_VALUE;
2426 }
2427
2428 // release FBO resources
2429 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2430 glDeleteRenderbuffersOES(1, &tname);
2431 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002432
2433 hw.compositionComplete();
2434
Steve Block9d453682011-12-20 16:23:08 +00002435 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002436
Mathias Agopian74c40c02010-09-29 13:02:36 -07002437 return result;
2438}
2439
2440
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002441status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2442 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002443 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002444 uint32_t sw, uint32_t sh,
2445 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002446{
2447 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002448 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002449 return BAD_VALUE;
2450
2451 if (!GLExtensions::getInstance().haveFramebufferObject())
2452 return INVALID_OPERATION;
2453
2454 class MessageCaptureScreen : public MessageBase {
2455 SurfaceFlinger* flinger;
2456 DisplayID dpy;
2457 sp<IMemoryHeap>* heap;
2458 uint32_t* w;
2459 uint32_t* h;
2460 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002461 uint32_t sw;
2462 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002463 uint32_t minLayerZ;
2464 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002465 status_t result;
2466 public:
2467 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002468 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002469 uint32_t sw, uint32_t sh,
2470 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002471 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002472 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2473 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2474 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002475 {
2476 }
2477 status_t getResult() const {
2478 return result;
2479 }
2480 virtual bool handler() {
2481 Mutex::Autolock _l(flinger->mStateLock);
2482
2483 // if we have secure windows, never allow the screen capture
2484 if (flinger->mSecureFrameBuffer)
2485 return true;
2486
Mathias Agopian74c40c02010-09-29 13:02:36 -07002487 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002488 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002490 return true;
2491 }
2492 };
2493
2494 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002495 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002496 status_t res = postMessageSync(msg);
2497 if (res == NO_ERROR) {
2498 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2499 }
2500 return res;
2501}
2502
2503// ---------------------------------------------------------------------------
2504
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002505sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2506{
2507 sp<Layer> result;
2508 Mutex::Autolock _l(mStateLock);
2509 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2510 return result;
2511}
2512
2513// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514
Jamie Gennis9a78c902011-01-12 18:30:40 -08002515GraphicBufferAlloc::GraphicBufferAlloc() {}
2516
2517GraphicBufferAlloc::~GraphicBufferAlloc() {}
2518
2519sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002520 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002521 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2522 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002523 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002524 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002525 if (err == NO_MEMORY) {
2526 GraphicBuffer::dumpAllocationsToSystemLog();
2527 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002528 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002529 "failed (%s), handle=%p",
2530 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002531 return 0;
2532 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002533 return graphicBuffer;
2534}
2535
Jamie Gennis9a78c902011-01-12 18:30:40 -08002536// ---------------------------------------------------------------------------
2537
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002538GraphicPlane::GraphicPlane()
2539 : mHw(0)
2540{
2541}
2542
2543GraphicPlane::~GraphicPlane() {
2544 delete mHw;
2545}
2546
2547bool GraphicPlane::initialized() const {
2548 return mHw ? true : false;
2549}
2550
Mathias Agopian2b92d892010-02-08 15:49:35 -08002551int GraphicPlane::getWidth() const {
2552 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002553}
2554
Mathias Agopian2b92d892010-02-08 15:49:35 -08002555int GraphicPlane::getHeight() const {
2556 return mHeight;
2557}
2558
2559void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2560{
2561 mHw = hw;
2562
2563 // initialize the display orientation transform.
2564 // it's a constant that should come from the display driver.
2565 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2566 char property[PROPERTY_VALUE_MAX];
2567 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2568 //displayOrientation
2569 switch (atoi(property)) {
2570 case 90:
2571 displayOrientation = ISurfaceComposer::eOrientation90;
2572 break;
2573 case 270:
2574 displayOrientation = ISurfaceComposer::eOrientation270;
2575 break;
2576 }
2577 }
2578
2579 const float w = hw->getWidth();
2580 const float h = hw->getHeight();
2581 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2582 &mDisplayTransform);
2583 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2584 mDisplayWidth = h;
2585 mDisplayHeight = w;
2586 } else {
2587 mDisplayWidth = w;
2588 mDisplayHeight = h;
2589 }
2590
2591 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002592}
2593
2594status_t GraphicPlane::orientationToTransfrom(
2595 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002596{
2597 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598 switch (orientation) {
2599 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002600 flags = Transform::ROT_0;
2601 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002602 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002603 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002604 break;
2605 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002606 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002607 break;
2608 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002609 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002610 break;
2611 default:
2612 return BAD_VALUE;
2613 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002614 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615 return NO_ERROR;
2616}
2617
2618status_t GraphicPlane::setOrientation(int orientation)
2619{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002620 // If the rotation can be handled in hardware, this is where
2621 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002622
2623 const DisplayHardware& hw(displayHardware());
2624 const float w = mDisplayWidth;
2625 const float h = mDisplayHeight;
2626 mWidth = int(w);
2627 mHeight = int(h);
2628
2629 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002630 GraphicPlane::orientationToTransfrom(orientation, w, h,
2631 &orientationTransform);
2632 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2633 mWidth = int(h);
2634 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002636
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002637 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002638 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002639 return NO_ERROR;
2640}
2641
2642const DisplayHardware& GraphicPlane::displayHardware() const {
2643 return *mHw;
2644}
2645
Mathias Agopian59119e62010-10-11 12:37:43 -07002646DisplayHardware& GraphicPlane::editDisplayHardware() {
2647 return *mHw;
2648}
2649
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002650const Transform& GraphicPlane::transform() const {
2651 return mGlobalTransform;
2652}
2653
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002654EGLDisplay GraphicPlane::getEGLDisplay() const {
2655 return mHw->getEGLDisplay();
2656}
2657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658// ---------------------------------------------------------------------------
2659
2660}; // namespace android