blob: 00988893cae73c67ae18e4902dc72a903e143c84 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080053#include "DisplayEventConnection.h"
54#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 Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugBackground(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 mConsoleSignals(0),
101 mSecureFrameBuffer(0)
102{
103 init();
104}
105
106void SurfaceFlinger::init()
107{
Steve Blocka19954a2012-01-04 20:05:49 +0000108 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 // debugging stuff...
111 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 property_get("debug.sf.showupdates", value, "0");
114 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 property_get("debug.sf.showbackground", value, "0");
117 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700119 property_get("debug.sf.ddms", value, "0");
120 mDebugDDMS = atoi(value);
121 if (mDebugDDMS) {
122 DdmConnection::start(getServiceName());
123 }
124
Steve Blocka19954a2012-01-04 20:05:49 +0000125 ALOGI_IF(mDebugRegion, "showupdates enabled");
126 ALOGI_IF(mDebugBackground, "showbackground enabled");
127 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128}
129
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800130void SurfaceFlinger::onFirstRef()
131{
132 mEventQueue.init(this);
133
134 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
135
136 // Wait for the main thread to be done with its initialization
137 mReadyToRunBarrier.wait();
138}
139
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141SurfaceFlinger::~SurfaceFlinger()
142{
143 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}
145
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146void SurfaceFlinger::binderDied(const wp<IBinder>& who)
147{
148 // the window manager died on us. prepare its eulogy.
149
150 // reset screen orientation
151 Vector<ComposerState> state;
152 setTransactionState(state, eOrientationDefault, 0);
153
154 // restart the boot-animation
155 property_set("ctl.start", "bootanim");
156}
157
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700160 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Mathias Agopian7e27f052010-05-28 14:22:23 -0700163sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopian96f08192010-06-02 23:28:45 -0700165 sp<ISurfaceComposerClient> bclient;
166 sp<Client> client(new Client(this));
167 status_t err = client->initCheck();
168 if (err == NO_ERROR) {
169 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 return bclient;
172}
173
Jamie Gennis9a78c902011-01-12 18:30:40 -0800174sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
175{
176 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
177 return gba;
178}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
181{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000182 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 const GraphicPlane& plane(mGraphicPlanes[dpy]);
184 return plane;
185}
186
187GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
188{
189 return const_cast<GraphicPlane&>(
190 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
191}
192
193void SurfaceFlinger::bootFinished()
194{
195 const nsecs_t now = systemTime();
196 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000197 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700198 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700199
200 // wait patiently for the window manager death
201 const String16 name("window");
202 sp<IBinder> window(defaultServiceManager()->getService(name));
203 if (window != 0) {
204 window->linkToDeath(this);
205 }
206
207 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700208 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209}
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211static inline uint16_t pack565(int r, int g, int b) {
212 return (r<<11)|(g<<5)|b;
213}
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215status_t SurfaceFlinger::readyToRun()
216{
Steve Blocka19954a2012-01-04 20:05:49 +0000217 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 "Initializing graphics H/W...");
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 // we only support one display currently
221 int dpy = 0;
222
223 {
224 // initialize the main display
225 GraphicPlane& plane(graphicPlane(dpy));
226 DisplayHardware* const hw = new DisplayHardware(this, dpy);
227 plane.setDisplayHardware(hw);
228 }
229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 // create the shared control-block
231 mServerHeap = new MemoryHeapBase(4096,
232 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700234
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700235 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000236 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700238 new(mServerCblk) surface_flinger_cblk_t;
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 // initialize primary screen
241 // (other display should be initialized in the same manner, but
242 // asynchronously, as they could come and go. None of this is supported
243 // yet).
244 const GraphicPlane& plane(graphicPlane(dpy));
245 const DisplayHardware& hw = plane.displayHardware();
246 const uint32_t w = hw.getWidth();
247 const uint32_t h = hw.getHeight();
248 const uint32_t f = hw.getFormat();
249 hw.makeCurrent();
250
251 // initialize the shared control block
252 mServerCblk->connected |= 1<<dpy;
253 display_cblk_t* dcblk = mServerCblk->displays + dpy;
254 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800255 dcblk->w = plane.getWidth();
256 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 dcblk->format = f;
258 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
259 dcblk->xdpi = hw.getDpiX();
260 dcblk->ydpi = hw.getDpiY();
261 dcblk->fps = hw.getRefreshRate();
262 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263
264 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700266 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glEnableClientState(GL_VERTEX_ARRAY);
268 glEnable(GL_SCISSOR_TEST);
269 glShadeModel(GL_FLAT);
270 glDisable(GL_DITHER);
271 glDisable(GL_CULL_FACE);
272
273 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
274 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700275 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 glGenTextures(1, &mWormholeTexName);
277 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
282 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700283 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
284
285 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
286 glGenTextures(1, &mProtectedTexName);
287 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
293 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
295 glViewport(0, 0, w, h);
296 glMatrixMode(GL_PROJECTION);
297 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700298 // put the origin in the left-bottom corner
299 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800301
302 // start the EventThread
303 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800304 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800305 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
307 /*
308 * We're now ready to accept clients...
309 */
310
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800311 mReadyToRunBarrier.open();
312
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700313 // start boot animation
314 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
319// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800320
Jamie Gennis582270d2011-08-17 18:19:00 -0700321bool SurfaceFlinger::authenticateSurfaceTexture(
322 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800323 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700324 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800325
326 // Check the visible layer list for the ISurface
327 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
328 size_t count = currentLayers.size();
329 for (size_t i=0 ; i<count ; i++) {
330 const sp<LayerBase>& layer(currentLayers[i]);
331 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700332 if (lbc != NULL) {
333 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
334 if (lbcBinder == surfaceTextureBinder) {
335 return true;
336 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800337 }
338 }
339
340 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700341 // SurfaceTexture gets destroyed before all the clients are done using it,
342 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800343 // will instead fail later on when the client tries to use the surface,
344 // which should be reported as "surface XYZ returned an -ENODEV". The
345 // purgatorized layers are no less authentic than the visible ones, so this
346 // should not cause any harm.
347 size_t purgatorySize = mLayerPurgatory.size();
348 for (size_t i=0 ; i<purgatorySize ; i++) {
349 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
350 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700351 if (lbc != NULL) {
352 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
353 if (lbcBinder == surfaceTextureBinder) {
354 return true;
355 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800356 }
357 }
358
359 return false;
360}
361
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800362// ----------------------------------------------------------------------------
363
364sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800365 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700366}
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800369
370void SurfaceFlinger::waitForEvent() {
371 mEventQueue.waitMessage();
372}
373
374void SurfaceFlinger::signalTransaction() {
375 mEventQueue.invalidate();
376}
377
378void SurfaceFlinger::signalLayerUpdate() {
379 mEventQueue.invalidate();
380}
381
382void SurfaceFlinger::signalRefresh() {
383 mEventQueue.refresh();
384}
385
386status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
387 nsecs_t reltime, uint32_t flags) {
388 return mEventQueue.postMessage(msg, reltime);
389}
390
391status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
392 nsecs_t reltime, uint32_t flags) {
393 status_t res = mEventQueue.postMessage(msg, reltime);
394 if (res == NO_ERROR) {
395 msg->wait();
396 }
397 return res;
398}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399
400bool SurfaceFlinger::threadLoop()
401{
402 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 return true;
404}
405
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800406void SurfaceFlinger::onMessageReceived(int32_t what)
407{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800408 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800409 switch (what) {
Mathias Agopian303d5382012-02-05 01:49:16 -0800410 case MessageQueue::REFRESH: {
411// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800412 // check for transactions
413 if (CC_UNLIKELY(mConsoleSignals)) {
414 handleConsoleEvents();
415 }
416
417 // if we're in a global transaction, don't do anything.
418 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
419 uint32_t transactionFlags = peekTransactionFlags(mask);
420 if (CC_UNLIKELY(transactionFlags)) {
421 handleTransaction(transactionFlags);
422 }
423
424 // post surfaces (if needed)
425 handlePageFlip();
426
Mathias Agopian303d5382012-02-05 01:49:16 -0800427// signalRefresh();
428//
429// } break;
430//
431// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800432
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800433 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800434
Mathias Agopian303d5382012-02-05 01:49:16 -0800435 const DisplayHardware& hw(graphicPlane(0).displayHardware());
436
437// if (mDirtyRegion.isEmpty()) {
438// return;
439// }
440
Mathias Agopianb048cef2012-02-04 15:44:04 -0800441 if (CC_UNLIKELY(mHwWorkListDirty)) {
442 // build the h/w work list
443 handleWorkList();
444 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800445
Mathias Agopianb048cef2012-02-04 15:44:04 -0800446 if (CC_LIKELY(hw.canDraw())) {
447 // repaint the framebuffer (if needed)
448 handleRepaint();
449 // inform the h/w that we're done compositing
450 hw.compositionComplete();
451 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800452 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800453 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800454 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800455 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800456
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800457 } break;
458 }
459}
460
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461void SurfaceFlinger::postFramebuffer()
462{
Mathias Agopian841cde52012-03-01 15:44:37 -0800463 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800464 // mSwapRegion can be empty here is some cases, for instance if a hidden
465 // or fully transparent window is updating.
466 // in that case, we need to flip anyways to not risk a deadlock with
467 // h/w composer.
468
Mathias Agopiana44b0412011-10-16 18:46:35 -0700469 const DisplayHardware& hw(graphicPlane(0).displayHardware());
470 const nsecs_t now = systemTime();
471 mDebugInSwapBuffers = now;
472 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800473
474 size_t numLayers = mVisibleLayersSortedByZ.size();
475 for (size_t i = 0; i < numLayers; i++) {
476 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
477 }
478
Mathias Agopiana44b0412011-10-16 18:46:35 -0700479 mLastSwapBufferTime = systemTime() - now;
480 mDebugInSwapBuffers = 0;
481 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482}
483
484void SurfaceFlinger::handleConsoleEvents()
485{
486 // something to do with the console
487 const DisplayHardware& hw = graphicPlane(0).displayHardware();
488
489 int what = android_atomic_and(0, &mConsoleSignals);
490 if (what & eConsoleAcquired) {
491 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700492 // this is a temporary work-around, eventually this should be called
493 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700494 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 }
496
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700498 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 }
501 }
502
503 mDirtyRegion.set(hw.bounds());
504}
505
506void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
507{
Mathias Agopian841cde52012-03-01 15:44:37 -0800508 ATRACE_CALL();
509
Mathias Agopianca4d3602011-05-19 15:38:14 -0700510 Mutex::Autolock _l(mStateLock);
511 const nsecs_t now = systemTime();
512 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513
Mathias Agopianca4d3602011-05-19 15:38:14 -0700514 // Here we're guaranteed that some transaction flags are set
515 // so we can call handleTransactionLocked() unconditionally.
516 // We call getTransactionFlags(), which will also clear the flags,
517 // with mStateLock held to guarantee that mCurrentState won't change
518 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700519
Mathias Agopianca4d3602011-05-19 15:38:14 -0700520 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
521 transactionFlags = getTransactionFlags(mask);
522 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700523
Mathias Agopianca4d3602011-05-19 15:38:14 -0700524 mLastTransactionTime = systemTime() - now;
525 mDebugInTransaction = 0;
526 invalidateHwcGeometry();
527 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700528}
529
Mathias Agopianca4d3602011-05-19 15:38:14 -0700530void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700531{
532 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 const size_t count = currentLayers.size();
534
535 /*
536 * Traversal of the children
537 * (perform the transaction for each of them if needed)
538 */
539
540 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
541 if (layersNeedTransaction) {
542 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
545 if (!trFlags) continue;
546
547 const uint32_t flags = layer->doTransaction(0);
548 if (flags & Layer::eVisibleRegion)
549 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 }
551 }
552
553 /*
554 * Perform our own transaction if needed
555 */
556
557 if (transactionFlags & eTransactionNeeded) {
558 if (mCurrentState.orientation != mDrawingState.orientation) {
559 // the orientation has changed, recompute all visible regions
560 // and invalidate everything.
561
562 const int dpy = 0;
563 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700564 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 GraphicPlane& plane(graphicPlane(dpy));
566 plane.setOrientation(orientation);
567
568 // update the shared control block
569 const DisplayHardware& hw(plane.displayHardware());
570 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
571 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800572 dcblk->w = plane.getWidth();
573 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574
575 mVisibleRegionsDirty = true;
576 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
578
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700579 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
580 // layers have been added
581 mVisibleRegionsDirty = true;
582 }
583
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 // some layers might have been removed, so
585 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700587 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700589 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700590 const size_t count = previousLayers.size();
591 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700592 const sp<LayerBase>& layer(previousLayers[i]);
593 if (currentLayers.indexOf( layer ) < 0) {
594 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700595 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700596 }
597 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 }
600
601 commitTransaction();
602}
603
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800605 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606{
Mathias Agopian841cde52012-03-01 15:44:37 -0800607 ATRACE_CALL();
608
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 const GraphicPlane& plane(graphicPlane(0));
610 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700611 const DisplayHardware& hw(plane.displayHardware());
612 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613
614 Region aboveOpaqueLayers;
615 Region aboveCoveredLayers;
616 Region dirty;
617
618 bool secureFrameBuffer = false;
619
620 size_t i = currentLayers.size();
621 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700622 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 layer->validateVisibility(planeTransform);
624
625 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700626 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
Mathias Agopianab028732010-03-16 16:41:46 -0700628 /*
629 * opaqueRegion: area of a surface that is fully opaque.
630 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700632
633 /*
634 * visibleRegion: area of a surface that is visible on screen
635 * and not fully transparent. This is essentially the layer's
636 * footprint minus the opaque regions above it.
637 * Areas covered by a translucent surface are considered visible.
638 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700640
641 /*
642 * coveredRegion: area of a surface that is covered by all
643 * visible regions above it (which includes the translucent areas).
644 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700646
647
648 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800649 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700650 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700651 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700653 visibleRegion.andSelf(screenRegion);
654 if (!visibleRegion.isEmpty()) {
655 // Remove the transparent area from the visible region
656 if (translucent) {
657 visibleRegion.subtractSelf(layer->transparentRegionScreen);
658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659
Mathias Agopianab028732010-03-16 16:41:46 -0700660 // compute the opaque region
661 const int32_t layerOrientation = layer->getOrientation();
662 if (s.alpha==255 && !translucent &&
663 ((layerOrientation & Transform::ROT_INVALID) == false)) {
664 // the opaque region is the layer's footprint
665 opaqueRegion = visibleRegion;
666 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 }
668 }
669
Mathias Agopianab028732010-03-16 16:41:46 -0700670 // Clip the covered region to the visible region
671 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
672
673 // Update aboveCoveredLayers for next (lower) layer
674 aboveCoveredLayers.orSelf(visibleRegion);
675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 // subtract the opaque region covered by the layers above us
677 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678
679 // compute this layer's dirty region
680 if (layer->contentDirty) {
681 // we need to invalidate the whole region
682 dirty = visibleRegion;
683 // as well, as the old visible region
684 dirty.orSelf(layer->visibleRegionScreen);
685 layer->contentDirty = false;
686 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700687 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700688 * the exposed region consists of two components:
689 * 1) what's VISIBLE now and was COVERED before
690 * 2) what's EXPOSED now less what was EXPOSED before
691 *
692 * note that (1) is conservative, we start with the whole
693 * visible region but only keep what used to be covered by
694 * something -- which mean it may have been exposed.
695 *
696 * (2) handles areas that were not covered by anything but got
697 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700698 */
Mathias Agopianab028732010-03-16 16:41:46 -0700699 const Region newExposed = visibleRegion - coveredRegion;
700 const Region oldVisibleRegion = layer->visibleRegionScreen;
701 const Region oldCoveredRegion = layer->coveredRegionScreen;
702 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
703 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 }
705 dirty.subtractSelf(aboveOpaqueLayers);
706
707 // accumulate to the screen dirty region
708 dirtyRegion.orSelf(dirty);
709
Mathias Agopianab028732010-03-16 16:41:46 -0700710 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700712
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 // Store the visible region is screen space
714 layer->setVisibleRegion(visibleRegion);
715 layer->setCoveredRegion(coveredRegion);
716
Mathias Agopian97011222009-07-28 10:57:27 -0700717 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 if (layer->isSecure() && !visibleRegion.isEmpty()) {
719 secureFrameBuffer = true;
720 }
721 }
722
Mathias Agopian97011222009-07-28 10:57:27 -0700723 // invalidate the areas where a layer was removed
724 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
725 mDirtyRegionRemovedLayer.clear();
726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 mSecureFrameBuffer = secureFrameBuffer;
728 opaqueRegion = aboveOpaqueLayers;
729}
730
731
732void SurfaceFlinger::commitTransaction()
733{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800734 if (!mLayersPendingRemoval.isEmpty()) {
735 // Notify removed layers now that they can't be drawn from
736 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
737 mLayersPendingRemoval[i]->onRemoved();
738 }
739 mLayersPendingRemoval.clear();
740 }
741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700743 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700744 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745}
746
747void SurfaceFlinger::handlePageFlip()
748{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800749 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800750 const DisplayHardware& hw = graphicPlane(0).displayHardware();
751 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800753 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
754 const bool visibleRegions = lockPageFlip(currentLayers);
755
756 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 Region opaqueRegion;
758 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700759
760 /*
761 * rebuild the visible layer list
762 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800763 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700764 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700765 mVisibleLayersSortedByZ.setCapacity(count);
766 for (size_t i=0 ; i<count ; i++) {
767 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
768 mVisibleLayersSortedByZ.add(currentLayers[i]);
769 }
770
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 mWormholeRegion = screenRegion.subtract(opaqueRegion);
772 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800773 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 }
775
776 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700777
778 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 mDirtyRegion.andSelf(screenRegion);
780}
781
Mathias Agopianad456f92011-01-13 17:53:01 -0800782void SurfaceFlinger::invalidateHwcGeometry()
783{
784 mHwWorkListDirty = true;
785}
786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
788{
789 bool recomputeVisibleRegions = false;
790 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700791 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700793 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 layer->lockPageFlip(recomputeVisibleRegions);
795 }
796 return recomputeVisibleRegions;
797}
798
799void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
800{
801 const GraphicPlane& plane(graphicPlane(0));
802 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800803 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700804 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700806 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 layer->unlockPageFlip(planeTransform, mDirtyRegion);
808 }
809}
810
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800811void SurfaceFlinger::handleRefresh()
812{
813 bool needInvalidate = false;
814 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
815 const size_t count = currentLayers.size();
816 for (size_t i=0 ; i<count ; i++) {
817 const sp<LayerBase>& layer(currentLayers[i]);
818 if (layer->onPreComposition()) {
819 needInvalidate = true;
820 }
821 }
822 if (needInvalidate) {
823 signalLayerUpdate();
824 }
825}
826
827
Mathias Agopiana350ff92010-08-10 17:14:02 -0700828void SurfaceFlinger::handleWorkList()
829{
830 mHwWorkListDirty = false;
831 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
832 if (hwc.initCheck() == NO_ERROR) {
833 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
834 const size_t count = currentLayers.size();
835 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700836 hwc_layer_t* const cur(hwc.getLayers());
837 for (size_t i=0 ; cur && i<count ; i++) {
838 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700839 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700840 cur[i].compositionType = HWC_FRAMEBUFFER;
841 cur[i].flags |= HWC_SKIP_LAYER;
842 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700843 }
844 }
845}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700846
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847void SurfaceFlinger::handleRepaint()
848{
Mathias Agopian841cde52012-03-01 15:44:37 -0800849 ATRACE_CALL();
850
Mathias Agopianb8a55602009-06-26 19:06:36 -0700851 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700852 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853
Glenn Kasten99ed2242011-12-15 09:51:17 -0800854 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 debugFlashRegions();
856 }
857
Mathias Agopianb8a55602009-06-26 19:06:36 -0700858 // set the frame buffer
859 const DisplayHardware& hw(graphicPlane(0).displayHardware());
860 glMatrixMode(GL_MODELVIEW);
861 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862
863 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700864 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
865 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700866 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700867 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
868 // takes a rectangle, we must make sure to update that whole
869 // rectangle in that case
870 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700871 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700872 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700873 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700875 // in the BUFFER_PRESERVED case, obviously, we can update only
876 // what's needed and nothing more.
877 // NOTE: this is NOT a common case, as preserving the backbuffer
878 // is costly and usually involves copying the whole update back.
879 }
880 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700881 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700882 // We need to redraw the rectangle that will be updated
883 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700884 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700885 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700886 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700887 } else {
888 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700890 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 }
892 }
893
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700894 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 composeSurfaces(mDirtyRegion);
896
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700897 // update the swap region and clear the dirty region
898 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 mDirtyRegion.clear();
900}
901
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700902void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700904 const DisplayHardware& hw(graphicPlane(0).displayHardware());
905 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700906 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700907 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700908 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700909 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700910
Mathias Agopianf384cc32011-09-08 18:31:55 -0700911 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
912 size_t count = layers.size();
913
Steve Blocke6f43dd2012-01-06 19:20:56 +0000914 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700915 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
916 hwc.getNumLayers(), count);
917
918 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700919 if (hwc.initCheck() == NO_ERROR) {
920 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
921 }
Mathias Agopian45721772010-08-12 15:03:26 -0700922
923 /*
924 * update the per-frame h/w composer data for each layer
925 * and build the transparent region of the FB
926 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700927 for (size_t i=0 ; i<count ; i++) {
928 const sp<LayerBase>& layer(layers[i]);
929 layer->setPerFrameData(&cur[i]);
930 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700931 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700932 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000933 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700934
Mathias Agopianf384cc32011-09-08 18:31:55 -0700935 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700936 // what's happening here is tricky.
937 // we want to clear all the layers with the CLEAR_FB flags
938 // that are opaque.
939 // however, since some GPU are efficient at preserving
940 // the backbuffer, we want to take advantage of that so we do the
941 // clear only in the dirty region (other areas will be preserved
942 // on those GPUs).
943 // NOTE: on non backbuffer preserving GPU, the dirty region
944 // has already been expanded as needed, so the code is correct
945 // there too.
946 //
947 // However, the content of the framebuffer cannot be trusted when
948 // we switch to/from FB/OVERLAY, in which case we need to
949 // expand the dirty region to those areas too.
950 //
951 // Note also that there is a special case when switching from
952 // "no layers in FB" to "some layers in FB", where we need to redraw
953 // the entire FB, since some areas might contain uninitialized
954 // data.
955 //
956 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700957 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700958 // that is, we can't erase anything outside the dirty region.
959
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700960 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700961
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700962 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
963 transparent.set(hw.getBounds());
964 dirtyInOut = transparent;
965 } else {
966 for (size_t i=0 ; i<count ; i++) {
967 const sp<LayerBase>& layer(layers[i]);
968 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
969 transparent.orSelf(layer->visibleRegionScreen);
970 }
971 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
972 if (isOverlay != layer->isOverlay()) {
973 // we transitioned to/from overlay, so add this layer
974 // to the dirty region so the framebuffer can be either
975 // cleared or redrawn.
976 dirtyInOut.orSelf(layer->visibleRegionScreen);
977 }
978 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800979 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700980 // don't erase stuff outside the dirty region
981 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700982 }
983
984 /*
985 * clear the area of the FB that need to be transparent
986 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700987 if (!transparent.isEmpty()) {
988 glClearColor(0,0,0,0);
989 Region::const_iterator it = transparent.begin();
990 Region::const_iterator const end = transparent.end();
991 const int32_t height = hw.getHeight();
992 while (it != end) {
993 const Rect& r(*it++);
994 const GLint sy = height - (r.top + r.height());
995 glScissor(r.left, sy, r.width(), r.height());
996 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800997 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700998 }
999 }
Mathias Agopianf384cc32011-09-08 18:31:55 -07001000}
Mathias Agopian45721772010-08-12 15:03:26 -07001001
Mathias Agopianf384cc32011-09-08 18:31:55 -07001002void SurfaceFlinger::composeSurfaces(const Region& dirty)
1003{
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001004 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1005 HWComposer& hwc(hw.getHwComposer());
1006
1007 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001008 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001009 // should never happen unless the window manager has a bug
1010 // draw something...
1011 drawWormhole();
1012 }
1013
Mathias Agopianfbc79222012-02-23 21:20:01 -08001014 // FIXME: workaroud for b/6020860
1015 glEnable(GL_SCISSOR_TEST);
1016 glScissor(0,0,0,0);
1017 glClear(GL_COLOR_BUFFER_BIT);
1018 // end-workaround
1019
Mathias Agopian45721772010-08-12 15:03:26 -07001020 /*
1021 * and then, render the layers targeted at the framebuffer
1022 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001023 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001024 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1025 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001026 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001027 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001028 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001029 }
1030 const sp<LayerBase>& layer(layers[i]);
1031 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1032 if (!clip.isEmpty()) {
1033 layer->draw(clip);
1034 }
1035 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036}
1037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038void SurfaceFlinger::debugFlashRegions()
1039{
Mathias Agopian0a917752010-06-14 21:20:00 -07001040 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1041 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001042 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001043 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001044 return;
1045 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001046
Mathias Agopian0a917752010-06-14 21:20:00 -07001047 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1048 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1049 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1050 mDirtyRegion.bounds() : hw.bounds());
1051 composeSurfaces(repaint);
1052 }
1053
Mathias Agopianc492e672011-10-18 14:49:27 -07001054 glDisable(GL_TEXTURE_EXTERNAL_OES);
1055 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 glDisable(GL_SCISSOR_TEST);
1058
Mathias Agopian0926f502009-05-04 14:17:04 -07001059 static int toggle = 0;
1060 toggle = 1 - toggle;
1061 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001062 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001063 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001064 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001065 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066
Mathias Agopian20f68782009-05-11 00:03:41 -07001067 Region::const_iterator it = mDirtyRegion.begin();
1068 Region::const_iterator const end = mDirtyRegion.end();
1069 while (it != end) {
1070 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001072 { r.left, height - r.top },
1073 { r.left, height - r.bottom },
1074 { r.right, height - r.bottom },
1075 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 };
1077 glVertexPointer(2, GL_FLOAT, 0, vertices);
1078 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1079 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001080
Mathias Agopian0656a682011-09-20 17:22:44 -07001081 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082
1083 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001084 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085
1086 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087}
1088
1089void SurfaceFlinger::drawWormhole() const
1090{
1091 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1092 if (region.isEmpty())
1093 return;
1094
1095 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1096 const int32_t width = hw.getWidth();
1097 const int32_t height = hw.getHeight();
1098
Glenn Kasten99ed2242011-12-15 09:51:17 -08001099 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001100 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001101 Region::const_iterator it = region.begin();
1102 Region::const_iterator const end = region.end();
1103 while (it != end) {
1104 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 const GLint sy = height - (r.top + r.height());
1106 glScissor(r.left, sy, r.width(), r.height());
1107 glClear(GL_COLOR_BUFFER_BIT);
1108 }
1109 } else {
1110 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1111 { width, height }, { 0, height } };
1112 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 glVertexPointer(2, GL_SHORT, 0, vertices);
1115 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1116 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001117
1118 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 glEnable(GL_TEXTURE_2D);
1120 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1121 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1122 glMatrixMode(GL_TEXTURE);
1123 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001124
1125 glDisable(GL_BLEND);
1126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001128 Region::const_iterator it = region.begin();
1129 Region::const_iterator const end = region.end();
1130 while (it != end) {
1131 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 const GLint sy = height - (r.top + r.height());
1133 glScissor(r.left, sy, r.width(), r.height());
1134 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1135 }
1136 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001137 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001138 glLoadIdentity();
1139 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140 }
1141}
1142
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001143status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001144{
1145 Mutex::Autolock _l(mStateLock);
1146 addLayer_l(layer);
1147 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1148 return NO_ERROR;
1149}
1150
Mathias Agopian96f08192010-06-02 23:28:45 -07001151status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1152{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001153 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001154 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1155}
1156
1157ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1158 const sp<LayerBaseClient>& lbc)
1159{
Mathias Agopian96f08192010-06-02 23:28:45 -07001160 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001161 size_t name = client->attachLayer(lbc);
1162
1163 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001164
1165 // add this layer to the current state list
1166 addLayer_l(lbc);
1167
Mathias Agopian4f113742011-05-03 16:21:41 -07001168 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001169}
1170
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001171status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172{
1173 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001174 status_t err = purgatorizeLayer_l(layer);
1175 if (err == NO_ERROR)
1176 setTransactionFlags(eTransactionNeeded);
1177 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178}
1179
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001180status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001182 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1183 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001184 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1187 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001188 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 return NO_ERROR;
1190 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001191 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192}
1193
Mathias Agopian9a112062009-04-17 19:36:26 -07001194status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1195{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001196 // First add the layer to the purgatory list, which makes sure it won't
1197 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001198 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001199 if (err >= 0) {
1200 mLayerPurgatory.add(layerBase);
1201 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001202
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001203 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001204
Mathias Agopian3d579642009-06-04 18:46:21 -07001205 // it's possible that we don't find a layer, because it might
1206 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001207 // from the user because there is a race between Client::destroySurface(),
1208 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001209 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1210}
1211
Mathias Agopian96f08192010-06-02 23:28:45 -07001212status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213{
Mathias Agopian96f08192010-06-02 23:28:45 -07001214 layer->forceVisibilityTransaction();
1215 setTransactionFlags(eTraversalNeeded);
1216 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217}
1218
Mathias Agopiandea20b12011-05-03 17:04:02 -07001219uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1220{
1221 return android_atomic_release_load(&mTransactionFlags);
1222}
1223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1225{
1226 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1227}
1228
Mathias Agopianbb641242010-05-18 17:06:55 -07001229uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230{
1231 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1232 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001233 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 }
1235 return old;
1236}
1237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001239void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001240 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001241 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001242
Jamie Gennis28378392011-10-12 17:39:00 -07001243 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001244 if (mCurrentState.orientation != orientation) {
1245 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1246 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001247 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001248 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001249 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001250 orientation);
1251 }
1252 }
1253
Mathias Agopian698c0872011-06-28 19:09:31 -07001254 const size_t count = state.size();
1255 for (size_t i=0 ; i<count ; i++) {
1256 const ComposerState& s(state[i]);
1257 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001258 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001259 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001260
Mathias Agopian386aa982011-11-07 21:58:03 -08001261 if (transactionFlags) {
1262 // this triggers the transaction
1263 setTransactionFlags(transactionFlags);
1264
1265 // if this is a synchronous transaction, wait for it to take effect
1266 // before returning.
1267 if (flags & eSynchronous) {
1268 mTransationPending = true;
1269 }
1270 while (mTransationPending) {
1271 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1272 if (CC_UNLIKELY(err != NO_ERROR)) {
1273 // just in case something goes wrong in SF, return to the
1274 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001275 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001276 mTransationPending = false;
1277 break;
1278 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001279 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 }
1281}
1282
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001283sp<ISurface> SurfaceFlinger::createSurface(
1284 ISurfaceComposerClient::surface_data_t* params,
1285 const String8& name,
1286 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1288 uint32_t flags)
1289{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001290 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001291 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001292
1293 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001294 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001295 int(w), int(h));
1296 return surfaceHandle;
1297 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001298
Mathias Agopianff615cc2012-02-24 14:58:36 -08001299 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 switch (flags & eFXSurfaceMask) {
1302 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001303 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1304 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 break;
1306 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001307 // for now we treat Blur as Dim, until we can implement it
1308 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001310 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001312 case eFXSurfaceScreenshot:
1313 layer = createScreenshotSurface(client, d, w, h, flags);
1314 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 }
1316
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001317 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001319 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001323 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001324 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001325 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001326 if (normalLayer != 0) {
1327 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001328 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001329 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001330 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001331
Mathias Agopian96f08192010-06-02 23:28:45 -07001332 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 }
1334
1335 return surfaceHandle;
1336}
1337
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001338sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001339 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001340 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001341 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342{
1343 // initialize the surfaces
1344 switch (format) { // TODO: take h/w into account
1345 case PIXEL_FORMAT_TRANSPARENT:
1346 case PIXEL_FORMAT_TRANSLUCENT:
1347 format = PIXEL_FORMAT_RGBA_8888;
1348 break;
1349 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001350#ifdef NO_RGBX_8888
1351 format = PIXEL_FORMAT_RGB_565;
1352#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001353 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001354#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 break;
1356 }
1357
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001358#ifdef NO_RGBX_8888
1359 if (format == PIXEL_FORMAT_RGBX_8888)
1360 format = PIXEL_FORMAT_RGBA_8888;
1361#endif
1362
Mathias Agopian96f08192010-06-02 23:28:45 -07001363 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001364 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001365 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001366 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001367 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 }
1369 return layer;
1370}
1371
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001372sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001373 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001374 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375{
Mathias Agopian96f08192010-06-02 23:28:45 -07001376 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001377 return layer;
1378}
1379
1380sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1381 const sp<Client>& client, DisplayID display,
1382 uint32_t w, uint32_t h, uint32_t flags)
1383{
1384 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 return layer;
1386}
1387
Mathias Agopian96f08192010-06-02 23:28:45 -07001388status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389{
Mathias Agopian9a112062009-04-17 19:36:26 -07001390 /*
1391 * called by the window manager, when a surface should be marked for
1392 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001393 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001394 * The surface is removed from the current and drawing lists, but placed
1395 * in the purgatory queue, so it's not destroyed right-away (we need
1396 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001397 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001398
Mathias Agopian48d819a2009-09-10 19:41:18 -07001399 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001400 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001401 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001402
Mathias Agopian48d819a2009-09-10 19:41:18 -07001403 if (layer != 0) {
1404 err = purgatorizeLayer_l(layer);
1405 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001406 setTransactionFlags(eTransactionNeeded);
1407 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001408 }
1409 return err;
1410}
1411
Mathias Agopianca4d3602011-05-19 15:38:14 -07001412status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001413{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001414 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001415 status_t err = NO_ERROR;
1416 sp<LayerBaseClient> l(layer.promote());
1417 if (l != NULL) {
1418 Mutex::Autolock _l(mStateLock);
1419 err = removeLayer_l(l);
1420 if (err == NAME_NOT_FOUND) {
1421 // The surface wasn't in the current list, which means it was
1422 // removed already, which means it is in the purgatory,
1423 // and need to be removed from there.
1424 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001425 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001426 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001427 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001428 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001429 "error removing layer=%p (%s)", l.get(), strerror(-err));
1430 }
1431 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432}
1433
Mathias Agopian698c0872011-06-28 19:09:31 -07001434uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001435 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001436 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001439 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1440 if (layer != 0) {
1441 const uint32_t what = s.what;
1442 if (what & ePositionChanged) {
1443 if (layer->setPosition(s.x, s.y))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eLayerChanged) {
1447 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1448 if (layer->setLayer(s.z)) {
1449 mCurrentState.layersSortedByZ.removeAt(idx);
1450 mCurrentState.layersSortedByZ.add(layer);
1451 // we need traversal (state changed)
1452 // AND transaction (list changed)
1453 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 }
1455 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001456 if (what & eSizeChanged) {
1457 if (layer->setSize(s.w, s.h)) {
1458 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001459 }
1460 }
1461 if (what & eAlphaChanged) {
1462 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1463 flags |= eTraversalNeeded;
1464 }
1465 if (what & eMatrixChanged) {
1466 if (layer->setMatrix(s.matrix))
1467 flags |= eTraversalNeeded;
1468 }
1469 if (what & eTransparentRegionChanged) {
1470 if (layer->setTransparentRegionHint(s.transparentRegion))
1471 flags |= eTraversalNeeded;
1472 }
1473 if (what & eVisibilityChanged) {
1474 if (layer->setFlags(s.flags, s.mask))
1475 flags |= eTraversalNeeded;
1476 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001478 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479}
1480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481void SurfaceFlinger::screenReleased(int dpy)
1482{
1483 // this may be called by a signal handler, we can't do too much in here
1484 android_atomic_or(eConsoleReleased, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001485 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486}
1487
1488void SurfaceFlinger::screenAcquired(int dpy)
1489{
1490 // this may be called by a signal handler, we can't do too much in here
1491 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001492 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493}
1494
1495status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1496{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001497 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 char buffer[SIZE];
1499 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001500
1501 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 snprintf(buffer, SIZE, "Permission Denial: "
1503 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1504 IPCThreadState::self()->getCallingPid(),
1505 IPCThreadState::self()->getCallingUid());
1506 result.append(buffer);
1507 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001508 // Try to get the main lock, but don't insist if we can't
1509 // (this would indicate SF is stuck, but we want to be able to
1510 // print something in dumpsys).
1511 int retry = 3;
1512 while (mStateLock.tryLock()<0 && --retry>=0) {
1513 usleep(1000000);
1514 }
1515 const bool locked(retry >= 0);
1516 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001517 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001518 "SurfaceFlinger appears to be unresponsive, "
1519 "dumping anyways (no locks held)\n");
1520 result.append(buffer);
1521 }
1522
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001523 bool dumpAll = true;
1524 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001525 size_t numArgs = args.size();
1526 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001527 if ((index < numArgs) &&
1528 (args[index] == String16("--list"))) {
1529 index++;
1530 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001531 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001532 }
1533
1534 if ((index < numArgs) &&
1535 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001536 index++;
1537 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001538 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001539 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001540
1541 if ((index < numArgs) &&
1542 (args[index] == String16("--latency-clear"))) {
1543 index++;
1544 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001545 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001546 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001548
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001549 if (dumpAll) {
1550 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001551 }
1552
Mathias Agopian9795c422009-08-26 16:36:26 -07001553 if (locked) {
1554 mStateLock.unlock();
1555 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 }
1557 write(fd, result.string(), result.size());
1558 return NO_ERROR;
1559}
1560
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001561void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1562 String8& result, char* buffer, size_t SIZE) const
1563{
1564 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1565 const size_t count = currentLayers.size();
1566 for (size_t i=0 ; i<count ; i++) {
1567 const sp<LayerBase>& layer(currentLayers[i]);
1568 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1569 result.append(buffer);
1570 }
1571}
1572
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001573void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1574 String8& result, char* buffer, size_t SIZE) const
1575{
1576 String8 name;
1577 if (index < args.size()) {
1578 name = String8(args[index]);
1579 index++;
1580 }
1581
1582 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1583 const size_t count = currentLayers.size();
1584 for (size_t i=0 ; i<count ; i++) {
1585 const sp<LayerBase>& layer(currentLayers[i]);
1586 if (name.isEmpty()) {
1587 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1588 result.append(buffer);
1589 }
1590 if (name.isEmpty() || (name == layer->getName())) {
1591 layer->dumpStats(result, buffer, SIZE);
1592 }
1593 }
1594}
1595
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001596void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1597 String8& result, char* buffer, size_t SIZE) const
1598{
1599 String8 name;
1600 if (index < args.size()) {
1601 name = String8(args[index]);
1602 index++;
1603 }
1604
1605 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1606 const size_t count = currentLayers.size();
1607 for (size_t i=0 ; i<count ; i++) {
1608 const sp<LayerBase>& layer(currentLayers[i]);
1609 if (name.isEmpty() || (name == layer->getName())) {
1610 layer->clearStats();
1611 }
1612 }
1613}
1614
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001615void SurfaceFlinger::dumpAllLocked(
1616 String8& result, char* buffer, size_t SIZE) const
1617{
1618 // figure out if we're stuck somewhere
1619 const nsecs_t now = systemTime();
1620 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1621 const nsecs_t inTransaction(mDebugInTransaction);
1622 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1623 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1624
1625 /*
1626 * Dump the visible layer list
1627 */
1628 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1629 const size_t count = currentLayers.size();
1630 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1631 result.append(buffer);
1632 for (size_t i=0 ; i<count ; i++) {
1633 const sp<LayerBase>& layer(currentLayers[i]);
1634 layer->dump(result, buffer, SIZE);
1635 }
1636
1637 /*
1638 * Dump the layers in the purgatory
1639 */
1640
1641 const size_t purgatorySize = mLayerPurgatory.size();
1642 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1643 result.append(buffer);
1644 for (size_t i=0 ; i<purgatorySize ; i++) {
1645 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1646 layer->shortDump(result, buffer, SIZE);
1647 }
1648
1649 /*
1650 * Dump SurfaceFlinger global state
1651 */
1652
1653 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1654 result.append(buffer);
1655
1656 const GLExtensions& extensions(GLExtensions::getInstance());
1657 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1658 extensions.getVendor(),
1659 extensions.getRenderer(),
1660 extensions.getVersion());
1661 result.append(buffer);
1662
1663 snprintf(buffer, SIZE, "EGL : %s\n",
1664 eglQueryString(graphicPlane(0).getEGLDisplay(),
1665 EGL_VERSION_HW_ANDROID));
1666 result.append(buffer);
1667
1668 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1669 result.append(buffer);
1670
1671 mWormholeRegion.dump(result, "WormholeRegion");
1672 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1673 snprintf(buffer, SIZE,
1674 " orientation=%d, canDraw=%d\n",
1675 mCurrentState.orientation, hw.canDraw());
1676 result.append(buffer);
1677 snprintf(buffer, SIZE,
1678 " last eglSwapBuffers() time: %f us\n"
1679 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001680 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001681 " refresh-rate : %f fps\n"
1682 " x-dpi : %f\n"
1683 " y-dpi : %f\n",
1684 mLastSwapBufferTime/1000.0,
1685 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001686 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001687 hw.getRefreshRate(),
1688 hw.getDpiX(),
1689 hw.getDpiY());
1690 result.append(buffer);
1691
1692 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1693 inSwapBuffersDuration/1000.0);
1694 result.append(buffer);
1695
1696 snprintf(buffer, SIZE, " transaction time: %f us\n",
1697 inTransactionDuration/1000.0);
1698 result.append(buffer);
1699
1700 /*
1701 * VSYNC state
1702 */
1703 mEventThread->dump(result, buffer, SIZE);
1704
1705 /*
1706 * Dump HWComposer state
1707 */
1708 HWComposer& hwc(hw.getHwComposer());
1709 snprintf(buffer, SIZE, "h/w composer state:\n");
1710 result.append(buffer);
1711 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1712 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1713 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1714 result.append(buffer);
1715 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1716
1717 /*
1718 * Dump gralloc state
1719 */
1720 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1721 alloc.dump(result);
1722 hw.dump(result);
1723}
1724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725status_t SurfaceFlinger::onTransact(
1726 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1727{
1728 switch (code) {
1729 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001730 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001733 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001734 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 {
1736 // codes that require permission check
1737 IPCThreadState* ipc = IPCThreadState::self();
1738 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001739 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001740 if ((uid != AID_GRAPHICS) &&
1741 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001742 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001743 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1744 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001746 break;
1747 }
1748 case CAPTURE_SCREEN:
1749 {
1750 // codes that require permission check
1751 IPCThreadState* ipc = IPCThreadState::self();
1752 const int pid = ipc->getCallingPid();
1753 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001754 if ((uid != AID_GRAPHICS) &&
1755 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001756 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001757 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1758 return PERMISSION_DENIED;
1759 }
1760 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761 }
1762 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1765 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001766 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001767 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001768 IPCThreadState* ipc = IPCThreadState::self();
1769 const int pid = ipc->getCallingPid();
1770 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001771 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001772 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773 return PERMISSION_DENIED;
1774 }
1775 int n;
1776 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001777 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001778 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779 return NO_ERROR;
1780 case 1002: // SHOW_UPDATES
1781 n = data.readInt32();
1782 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001783 invalidateHwcGeometry();
1784 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 return NO_ERROR;
1786 case 1003: // SHOW_BACKGROUND
1787 n = data.readInt32();
1788 mDebugBackground = n ? 1 : 0;
1789 return NO_ERROR;
1790 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001791 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001792 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001794 case 1005:{ // force transaction
1795 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1796 return NO_ERROR;
1797 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001798 case 1006:{ // send empty update
1799 signalRefresh();
1800 return NO_ERROR;
1801 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001802 case 1008: // toggle use of hw composer
1803 n = data.readInt32();
1804 mDebugDisableHWC = n ? 1 : 0;
1805 invalidateHwcGeometry();
1806 repaintEverything();
1807 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001808 case 1009: // toggle use of transform hint
1809 n = data.readInt32();
1810 mDebugDisableTransformHint = n ? 1 : 0;
1811 invalidateHwcGeometry();
1812 repaintEverything();
1813 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001815 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 reply->writeInt32(0);
1817 reply->writeInt32(mDebugRegion);
1818 reply->writeInt32(mDebugBackground);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001819 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001820 return NO_ERROR;
1821 case 1013: {
1822 Mutex::Autolock _l(mStateLock);
1823 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1824 reply->writeInt32(hw.getPageFlipCount());
1825 }
1826 return NO_ERROR;
1827 }
1828 }
1829 return err;
1830}
1831
Mathias Agopian53331da2011-08-22 21:44:41 -07001832void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001833 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001834 const Rect bounds(hw.getBounds());
1835 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001836 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001837}
1838
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001839void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1840 Mutex::Autolock _l(mInvalidateLock);
1841 mInvalidateRegion = reg;
1842}
1843
1844Region SurfaceFlinger::getAndClearInvalidateRegion() {
1845 Mutex::Autolock _l(mInvalidateLock);
1846 Region reg(mInvalidateRegion);
1847 mInvalidateRegion.clear();
1848 return reg;
1849}
1850
Mathias Agopian59119e62010-10-11 12:37:43 -07001851// ---------------------------------------------------------------------------
1852
Mathias Agopian118d0242011-10-13 16:02:48 -07001853status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1854 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1855{
1856 Mutex::Autolock _l(mStateLock);
1857 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1858}
1859
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001860status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1861 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001862{
Mathias Agopian59119e62010-10-11 12:37:43 -07001863 if (!GLExtensions::getInstance().haveFramebufferObject())
1864 return INVALID_OPERATION;
1865
1866 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001867 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001868 const uint32_t hw_w = hw.getWidth();
1869 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001870 GLfloat u = 1;
1871 GLfloat v = 1;
1872
1873 // make sure to clear all GL error flags
1874 while ( glGetError() != GL_NO_ERROR ) ;
1875
1876 // create a FBO
1877 GLuint name, tname;
1878 glGenTextures(1, &tname);
1879 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001880 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1881 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001882 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001883 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001884 GLint tw = (2 << (31 - clz(hw_w)));
1885 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001886 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1887 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001888 u = GLfloat(hw_w) / tw;
1889 v = GLfloat(hw_h) / th;
1890 }
1891 glGenFramebuffersOES(1, &name);
1892 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001893 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1894 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001895
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001896 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001897 glDisable(GL_TEXTURE_EXTERNAL_OES);
1898 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001899 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001900 glClearColor(0,0,0,1);
1901 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001902 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001903 glMatrixMode(GL_MODELVIEW);
1904 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001905 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1906 const size_t count = layers.size();
1907 for (size_t i=0 ; i<count ; ++i) {
1908 const sp<LayerBase>& layer(layers[i]);
1909 layer->drawForSreenShot();
1910 }
1911
Mathias Agopian118d0242011-10-13 16:02:48 -07001912 hw.compositionComplete();
1913
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001914 // back to main framebuffer
1915 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1916 glDisable(GL_SCISSOR_TEST);
1917 glDeleteFramebuffersOES(1, &name);
1918
1919 *textureName = tname;
1920 *uOut = u;
1921 *vOut = v;
1922 return NO_ERROR;
1923}
1924
1925// ---------------------------------------------------------------------------
1926
1927status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1928{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001929 // get screen geometry
1930 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1931 const uint32_t hw_w = hw.getWidth();
1932 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001933 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001934
1935 GLfloat u, v;
1936 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001937 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001938 if (result != NO_ERROR) {
1939 return result;
1940 }
1941
1942 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001943 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001944 glBindTexture(GL_TEXTURE_2D, tname);
1945 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1946 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1947 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001948 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1949 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001950 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1951 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1952 glVertexPointer(2, GL_FLOAT, 0, vtx);
1953
Mathias Agopianffcf4652011-07-07 17:30:31 -07001954 /*
1955 * Texture coordinate mapping
1956 *
1957 * u
1958 * 1 +----------+---+
1959 * | | | | image is inverted
1960 * | V | | w.r.t. the texture
1961 * 1-v +----------+ | coordinates
1962 * | |
1963 * | |
1964 * | |
1965 * 0 +--------------+
1966 * 0 1
1967 *
1968 */
1969
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001970 class s_curve_interpolator {
1971 const float nbFrames, s, v;
1972 public:
1973 s_curve_interpolator(int nbFrames, float s)
1974 : nbFrames(1.0f / (nbFrames-1)), s(s),
1975 v(1.0f + expf(-s + 0.5f*s)) {
1976 }
1977 float operator()(int f) {
1978 const float x = f * nbFrames;
1979 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1980 }
1981 };
1982
1983 class v_stretch {
1984 const GLfloat hw_w, hw_h;
1985 public:
1986 v_stretch(uint32_t hw_w, uint32_t hw_h)
1987 : hw_w(hw_w), hw_h(hw_h) {
1988 }
1989 void operator()(GLfloat* vtx, float v) {
1990 const GLfloat w = hw_w + (hw_w * v);
1991 const GLfloat h = hw_h - (hw_h * v);
1992 const GLfloat x = (hw_w - w) * 0.5f;
1993 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001994 vtx[0] = x; vtx[1] = y;
1995 vtx[2] = x; vtx[3] = y + h;
1996 vtx[4] = x + w; vtx[5] = y + h;
1997 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001998 }
1999 };
2000
2001 class h_stretch {
2002 const GLfloat hw_w, hw_h;
2003 public:
2004 h_stretch(uint32_t hw_w, uint32_t hw_h)
2005 : hw_w(hw_w), hw_h(hw_h) {
2006 }
2007 void operator()(GLfloat* vtx, float v) {
2008 const GLfloat w = hw_w - (hw_w * v);
2009 const GLfloat h = 1.0f;
2010 const GLfloat x = (hw_w - w) * 0.5f;
2011 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07002012 vtx[0] = x; vtx[1] = y;
2013 vtx[2] = x; vtx[3] = y + h;
2014 vtx[4] = x + w; vtx[5] = y + h;
2015 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002016 }
2017 };
2018
2019 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07002020 char value[PROPERTY_VALUE_MAX];
2021 property_get("debug.sf.electron_frames", value, "24");
2022 int nbFrames = (atoi(value) + 1) >> 1;
2023 if (nbFrames <= 0) // just in case
2024 nbFrames = 24;
2025
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 s_curve_interpolator itr(nbFrames, 7.5f);
2027 s_curve_interpolator itg(nbFrames, 8.0f);
2028 s_curve_interpolator itb(nbFrames, 8.5f);
2029
2030 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07002031
2032 glMatrixMode(GL_TEXTURE);
2033 glLoadIdentity();
2034 glMatrixMode(GL_MODELVIEW);
2035 glLoadIdentity();
2036
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002037 glEnable(GL_BLEND);
2038 glBlendFunc(GL_ONE, GL_ONE);
2039 for (int i=0 ; i<nbFrames ; i++) {
2040 float x, y, w, h;
2041 const float vr = itr(i);
2042 const float vg = itg(i);
2043 const float vb = itb(i);
2044
2045 // clear screen
2046 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002047 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002048 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002049
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002050 // draw the red plane
2051 vverts(vtx, vr);
2052 glColorMask(1,0,0,1);
2053 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002054
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002055 // draw the green plane
2056 vverts(vtx, vg);
2057 glColorMask(0,1,0,1);
2058 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002059
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002060 // draw the blue plane
2061 vverts(vtx, vb);
2062 glColorMask(0,0,1,1);
2063 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002064
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002065 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002066 glDisable(GL_TEXTURE_2D);
2067 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002068 glColor4f(vg, vg, vg, 1);
2069 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2070 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002071 }
2072
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002073 h_stretch hverts(hw_w, hw_h);
2074 glDisable(GL_BLEND);
2075 glDisable(GL_TEXTURE_2D);
2076 glColorMask(1,1,1,1);
2077 for (int i=0 ; i<nbFrames ; i++) {
2078 const float v = itg(i);
2079 hverts(vtx, v);
2080 glClear(GL_COLOR_BUFFER_BIT);
2081 glColor4f(1-v, 1-v, 1-v, 1);
2082 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2083 hw.flip(screenBounds);
2084 }
2085
2086 glColorMask(1,1,1,1);
2087 glEnable(GL_SCISSOR_TEST);
2088 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2089 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002090 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002091 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002092 return NO_ERROR;
2093}
2094
2095status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2096{
2097 status_t result = PERMISSION_DENIED;
2098
2099 if (!GLExtensions::getInstance().haveFramebufferObject())
2100 return INVALID_OPERATION;
2101
2102
2103 // get screen geometry
2104 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2105 const uint32_t hw_w = hw.getWidth();
2106 const uint32_t hw_h = hw.getHeight();
2107 const Region screenBounds(hw.bounds());
2108
2109 GLfloat u, v;
2110 GLuint tname;
2111 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2112 if (result != NO_ERROR) {
2113 return result;
2114 }
2115
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002116 GLfloat vtx[8];
2117 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118 glBindTexture(GL_TEXTURE_2D, tname);
2119 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2120 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2121 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002122 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2123 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002124 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2125 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2126 glVertexPointer(2, GL_FLOAT, 0, vtx);
2127
2128 class s_curve_interpolator {
2129 const float nbFrames, s, v;
2130 public:
2131 s_curve_interpolator(int nbFrames, float s)
2132 : nbFrames(1.0f / (nbFrames-1)), s(s),
2133 v(1.0f + expf(-s + 0.5f*s)) {
2134 }
2135 float operator()(int f) {
2136 const float x = f * nbFrames;
2137 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2138 }
2139 };
2140
2141 class v_stretch {
2142 const GLfloat hw_w, hw_h;
2143 public:
2144 v_stretch(uint32_t hw_w, uint32_t hw_h)
2145 : hw_w(hw_w), hw_h(hw_h) {
2146 }
2147 void operator()(GLfloat* vtx, float v) {
2148 const GLfloat w = hw_w + (hw_w * v);
2149 const GLfloat h = hw_h - (hw_h * v);
2150 const GLfloat x = (hw_w - w) * 0.5f;
2151 const GLfloat y = (hw_h - h) * 0.5f;
2152 vtx[0] = x; vtx[1] = y;
2153 vtx[2] = x; vtx[3] = y + h;
2154 vtx[4] = x + w; vtx[5] = y + h;
2155 vtx[6] = x + w; vtx[7] = y;
2156 }
2157 };
2158
2159 class h_stretch {
2160 const GLfloat hw_w, hw_h;
2161 public:
2162 h_stretch(uint32_t hw_w, uint32_t hw_h)
2163 : hw_w(hw_w), hw_h(hw_h) {
2164 }
2165 void operator()(GLfloat* vtx, float v) {
2166 const GLfloat w = hw_w - (hw_w * v);
2167 const GLfloat h = 1.0f;
2168 const GLfloat x = (hw_w - w) * 0.5f;
2169 const GLfloat y = (hw_h - h) * 0.5f;
2170 vtx[0] = x; vtx[1] = y;
2171 vtx[2] = x; vtx[3] = y + h;
2172 vtx[4] = x + w; vtx[5] = y + h;
2173 vtx[6] = x + w; vtx[7] = y;
2174 }
2175 };
2176
Mathias Agopiana6546e52010-10-14 12:33:07 -07002177 // the full animation is 12 frames
2178 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179 s_curve_interpolator itr(nbFrames, 7.5f);
2180 s_curve_interpolator itg(nbFrames, 8.0f);
2181 s_curve_interpolator itb(nbFrames, 8.5f);
2182
2183 h_stretch hverts(hw_w, hw_h);
2184 glDisable(GL_BLEND);
2185 glDisable(GL_TEXTURE_2D);
2186 glColorMask(1,1,1,1);
2187 for (int i=nbFrames-1 ; i>=0 ; i--) {
2188 const float v = itg(i);
2189 hverts(vtx, v);
2190 glClear(GL_COLOR_BUFFER_BIT);
2191 glColor4f(1-v, 1-v, 1-v, 1);
2192 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2193 hw.flip(screenBounds);
2194 }
2195
Mathias Agopiana6546e52010-10-14 12:33:07 -07002196 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002197 v_stretch vverts(hw_w, hw_h);
2198 glEnable(GL_BLEND);
2199 glBlendFunc(GL_ONE, GL_ONE);
2200 for (int i=nbFrames-1 ; i>=0 ; i--) {
2201 float x, y, w, h;
2202 const float vr = itr(i);
2203 const float vg = itg(i);
2204 const float vb = itb(i);
2205
2206 // clear screen
2207 glColorMask(1,1,1,1);
2208 glClear(GL_COLOR_BUFFER_BIT);
2209 glEnable(GL_TEXTURE_2D);
2210
2211 // draw the red plane
2212 vverts(vtx, vr);
2213 glColorMask(1,0,0,1);
2214 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2215
2216 // draw the green plane
2217 vverts(vtx, vg);
2218 glColorMask(0,1,0,1);
2219 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2220
2221 // draw the blue plane
2222 vverts(vtx, vb);
2223 glColorMask(0,0,1,1);
2224 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2225
2226 hw.flip(screenBounds);
2227 }
2228
2229 glColorMask(1,1,1,1);
2230 glEnable(GL_SCISSOR_TEST);
2231 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002232 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002233 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002234 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002235
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002236 return NO_ERROR;
2237}
2238
2239// ---------------------------------------------------------------------------
2240
Mathias Agopianabd671a2010-10-14 14:54:06 -07002241status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002242{
2243 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2244 if (!hw.canDraw()) {
2245 // we're already off
2246 return NO_ERROR;
2247 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002248
2249 // turn off hwc while we're doing the animation
2250 hw.getHwComposer().disable();
2251 // and make sure to turn it back on (if needed) next time we compose
2252 invalidateHwcGeometry();
2253
Mathias Agopianabd671a2010-10-14 14:54:06 -07002254 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2255 electronBeamOffAnimationImplLocked();
2256 }
2257
2258 // always clear the whole screen at the end of the animation
2259 glClearColor(0,0,0,1);
2260 glDisable(GL_SCISSOR_TEST);
2261 glClear(GL_COLOR_BUFFER_BIT);
2262 glEnable(GL_SCISSOR_TEST);
2263 hw.flip( Region(hw.bounds()) );
2264
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002265 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002266}
2267
2268status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2269{
Mathias Agopian59119e62010-10-11 12:37:43 -07002270 class MessageTurnElectronBeamOff : public MessageBase {
2271 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002272 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002273 status_t result;
2274 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002275 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2276 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002277 }
2278 status_t getResult() const {
2279 return result;
2280 }
2281 virtual bool handler() {
2282 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002283 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002284 return true;
2285 }
2286 };
2287
Mathias Agopianabd671a2010-10-14 14:54:06 -07002288 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002289 status_t res = postMessageSync(msg);
2290 if (res == NO_ERROR) {
2291 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002292
2293 // work-around: when the power-manager calls us we activate the
2294 // animation. eventually, the "on" animation will be called
2295 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002296 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002297 }
2298 return res;
2299}
2300
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002301// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002302
Mathias Agopianabd671a2010-10-14 14:54:06 -07002303status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002304{
2305 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2306 if (hw.canDraw()) {
2307 // we're already on
2308 return NO_ERROR;
2309 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002310 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2311 electronBeamOnAnimationImplLocked();
2312 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002313
2314 // make sure to redraw the whole screen when the animation is done
2315 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002316 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002317
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002318 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002319}
2320
2321status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2322{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002323 class MessageTurnElectronBeamOn : public MessageBase {
2324 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002325 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002326 status_t result;
2327 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002328 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2329 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002330 }
2331 status_t getResult() const {
2332 return result;
2333 }
2334 virtual bool handler() {
2335 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002336 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002337 return true;
2338 }
2339 };
2340
Mathias Agopianabd671a2010-10-14 14:54:06 -07002341 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002342 return NO_ERROR;
2343}
2344
2345// ---------------------------------------------------------------------------
2346
Mathias Agopian74c40c02010-09-29 13:02:36 -07002347status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2348 sp<IMemoryHeap>* heap,
2349 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002350 uint32_t sw, uint32_t sh,
2351 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002353 ATRACE_CALL();
2354
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355 status_t result = PERMISSION_DENIED;
2356
2357 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002358 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002359 return BAD_VALUE;
2360
2361 if (!GLExtensions::getInstance().haveFramebufferObject())
2362 return INVALID_OPERATION;
2363
2364 // get screen geometry
2365 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2366 const uint32_t hw_w = hw.getWidth();
2367 const uint32_t hw_h = hw.getHeight();
2368
2369 if ((sw > hw_w) || (sh > hw_h))
2370 return BAD_VALUE;
2371
2372 sw = (!sw) ? hw_w : sw;
2373 sh = (!sh) ? hw_h : sh;
2374 const size_t size = sw * sh * 4;
2375
Steve Block9d453682011-12-20 16:23:08 +00002376 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002377 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002378
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 // make sure to clear all GL error flags
2380 while ( glGetError() != GL_NO_ERROR ) ;
2381
2382 // create a FBO
2383 GLuint name, tname;
2384 glGenRenderbuffersOES(1, &tname);
2385 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2386 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002387
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 glGenFramebuffersOES(1, &name);
2389 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2390 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2391 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2392
2393 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002394
Mathias Agopian74c40c02010-09-29 13:02:36 -07002395 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2396
2397 // invert everything, b/c glReadPixel() below will invert the FB
2398 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002399 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002400 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 glMatrixMode(GL_PROJECTION);
2402 glPushMatrix();
2403 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002404 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 glMatrixMode(GL_MODELVIEW);
2406
2407 // redraw the screen entirely...
2408 glClearColor(0,0,0,1);
2409 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002410
Jamie Gennis9575f602011-10-07 14:51:16 -07002411 const LayerVector& layers(mDrawingState.layersSortedByZ);
2412 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002413 for (size_t i=0 ; i<count ; ++i) {
2414 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002415 const uint32_t flags = layer->drawingState().flags;
2416 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2417 const uint32_t z = layer->drawingState().z;
2418 if (z >= minLayerZ && z <= maxLayerZ) {
2419 layer->drawForSreenShot();
2420 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002421 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002422 }
2423
2424 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002425 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002426 glScissor(0, 0, sw, sh);
2427
2428 // check for errors and return screen capture
2429 if (glGetError() != GL_NO_ERROR) {
2430 // error while rendering
2431 result = INVALID_OPERATION;
2432 } else {
2433 // allocate shared memory large enough to hold the
2434 // screen capture
2435 sp<MemoryHeapBase> base(
2436 new MemoryHeapBase(size, 0, "screen-capture") );
2437 void* const ptr = base->getBase();
2438 if (ptr) {
2439 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002440 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002441 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2442 if (glGetError() == GL_NO_ERROR) {
2443 *heap = base;
2444 *w = sw;
2445 *h = sh;
2446 *f = PIXEL_FORMAT_RGBA_8888;
2447 result = NO_ERROR;
2448 }
2449 } else {
2450 result = NO_MEMORY;
2451 }
2452 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002453 glEnable(GL_SCISSOR_TEST);
2454 glViewport(0, 0, hw_w, hw_h);
2455 glMatrixMode(GL_PROJECTION);
2456 glPopMatrix();
2457 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002458 } else {
2459 result = BAD_VALUE;
2460 }
2461
2462 // release FBO resources
2463 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2464 glDeleteRenderbuffersOES(1, &tname);
2465 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002466
2467 hw.compositionComplete();
2468
Steve Block9d453682011-12-20 16:23:08 +00002469 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002470
Mathias Agopian74c40c02010-09-29 13:02:36 -07002471 return result;
2472}
2473
2474
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002475status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2476 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002477 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002478 uint32_t sw, uint32_t sh,
2479 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002480{
2481 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002482 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483 return BAD_VALUE;
2484
2485 if (!GLExtensions::getInstance().haveFramebufferObject())
2486 return INVALID_OPERATION;
2487
2488 class MessageCaptureScreen : public MessageBase {
2489 SurfaceFlinger* flinger;
2490 DisplayID dpy;
2491 sp<IMemoryHeap>* heap;
2492 uint32_t* w;
2493 uint32_t* h;
2494 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002495 uint32_t sw;
2496 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002497 uint32_t minLayerZ;
2498 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002499 status_t result;
2500 public:
2501 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002502 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002503 uint32_t sw, uint32_t sh,
2504 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002505 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002506 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2507 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2508 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002509 {
2510 }
2511 status_t getResult() const {
2512 return result;
2513 }
2514 virtual bool handler() {
2515 Mutex::Autolock _l(flinger->mStateLock);
2516
2517 // if we have secure windows, never allow the screen capture
2518 if (flinger->mSecureFrameBuffer)
2519 return true;
2520
Mathias Agopian74c40c02010-09-29 13:02:36 -07002521 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002522 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002523
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002524 return true;
2525 }
2526 };
2527
2528 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002529 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002530 status_t res = postMessageSync(msg);
2531 if (res == NO_ERROR) {
2532 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2533 }
2534 return res;
2535}
2536
2537// ---------------------------------------------------------------------------
2538
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002539sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2540{
2541 sp<Layer> result;
2542 Mutex::Autolock _l(mStateLock);
2543 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2544 return result;
2545}
2546
2547// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002548
Mathias Agopian96f08192010-06-02 23:28:45 -07002549Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002550 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002552}
2553
Mathias Agopian96f08192010-06-02 23:28:45 -07002554Client::~Client()
2555{
Mathias Agopian96f08192010-06-02 23:28:45 -07002556 const size_t count = mLayers.size();
2557 for (size_t i=0 ; i<count ; i++) {
2558 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2559 if (layer != 0) {
2560 mFlinger->removeLayer(layer);
2561 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562 }
2563}
2564
Mathias Agopian96f08192010-06-02 23:28:45 -07002565status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002566 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002568
Mathias Agopian4f113742011-05-03 16:21:41 -07002569size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002570{
Mathias Agopian4f113742011-05-03 16:21:41 -07002571 Mutex::Autolock _l(mLock);
2572 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002573 mLayers.add(name, layer);
2574 return name;
2575}
2576
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002577void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002578{
Mathias Agopian4f113742011-05-03 16:21:41 -07002579 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002580 // we do a linear search here, because this doesn't happen often
2581 const size_t count = mLayers.size();
2582 for (size_t i=0 ; i<count ; i++) {
2583 if (mLayers.valueAt(i) == layer) {
2584 mLayers.removeItemsAt(i, 1);
2585 break;
2586 }
2587 }
2588}
Mathias Agopian4f113742011-05-03 16:21:41 -07002589sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2590{
2591 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002592 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002593 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002594 if (layer != 0) {
2595 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002596 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002597 }
2598 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002599}
2600
Mathias Agopiana67932f2011-04-20 14:20:59 -07002601
2602status_t Client::onTransact(
2603 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2604{
2605 // these must be checked
2606 IPCThreadState* ipc = IPCThreadState::self();
2607 const int pid = ipc->getCallingPid();
2608 const int uid = ipc->getCallingUid();
2609 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002610 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002611 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002612 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002613 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002614 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002615 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2616 return PERMISSION_DENIED;
2617 }
2618 }
2619 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002620}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002621
2622
Mathias Agopian96f08192010-06-02 23:28:45 -07002623sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002624 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002625 const String8& name,
2626 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627 uint32_t flags)
2628{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002629 /*
2630 * createSurface must be called from the GL thread so that it can
2631 * have access to the GL context.
2632 */
2633
2634 class MessageCreateSurface : public MessageBase {
2635 sp<ISurface> result;
2636 SurfaceFlinger* flinger;
2637 ISurfaceComposerClient::surface_data_t* params;
2638 Client* client;
2639 const String8& name;
2640 DisplayID display;
2641 uint32_t w, h;
2642 PixelFormat format;
2643 uint32_t flags;
2644 public:
2645 MessageCreateSurface(SurfaceFlinger* flinger,
2646 ISurfaceComposerClient::surface_data_t* params,
2647 const String8& name, Client* client,
2648 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2649 uint32_t flags)
2650 : flinger(flinger), params(params), client(client), name(name),
2651 display(display), w(w), h(h), format(format), flags(flags)
2652 {
2653 }
2654 sp<ISurface> getResult() const { return result; }
2655 virtual bool handler() {
2656 result = flinger->createSurface(params, name, client,
2657 display, w, h, format, flags);
2658 return true;
2659 }
2660 };
2661
2662 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2663 params, name, this, display, w, h, format, flags);
2664 mFlinger->postMessageSync(msg);
2665 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002666}
Mathias Agopian96f08192010-06-02 23:28:45 -07002667status_t Client::destroySurface(SurfaceID sid) {
2668 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002669}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002670
2671// ---------------------------------------------------------------------------
2672
Jamie Gennis9a78c902011-01-12 18:30:40 -08002673GraphicBufferAlloc::GraphicBufferAlloc() {}
2674
2675GraphicBufferAlloc::~GraphicBufferAlloc() {}
2676
2677sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002678 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002679 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2680 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002681 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002682 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002683 if (err == NO_MEMORY) {
2684 GraphicBuffer::dumpAllocationsToSystemLog();
2685 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002686 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002687 "failed (%s), handle=%p",
2688 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002689 return 0;
2690 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002691 return graphicBuffer;
2692}
2693
Jamie Gennis9a78c902011-01-12 18:30:40 -08002694// ---------------------------------------------------------------------------
2695
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696GraphicPlane::GraphicPlane()
2697 : mHw(0)
2698{
2699}
2700
2701GraphicPlane::~GraphicPlane() {
2702 delete mHw;
2703}
2704
2705bool GraphicPlane::initialized() const {
2706 return mHw ? true : false;
2707}
2708
Mathias Agopian2b92d892010-02-08 15:49:35 -08002709int GraphicPlane::getWidth() const {
2710 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002711}
2712
Mathias Agopian2b92d892010-02-08 15:49:35 -08002713int GraphicPlane::getHeight() const {
2714 return mHeight;
2715}
2716
2717void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2718{
2719 mHw = hw;
2720
2721 // initialize the display orientation transform.
2722 // it's a constant that should come from the display driver.
2723 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2724 char property[PROPERTY_VALUE_MAX];
2725 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2726 //displayOrientation
2727 switch (atoi(property)) {
2728 case 90:
2729 displayOrientation = ISurfaceComposer::eOrientation90;
2730 break;
2731 case 270:
2732 displayOrientation = ISurfaceComposer::eOrientation270;
2733 break;
2734 }
2735 }
2736
2737 const float w = hw->getWidth();
2738 const float h = hw->getHeight();
2739 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2740 &mDisplayTransform);
2741 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2742 mDisplayWidth = h;
2743 mDisplayHeight = w;
2744 } else {
2745 mDisplayWidth = w;
2746 mDisplayHeight = h;
2747 }
2748
2749 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002750}
2751
2752status_t GraphicPlane::orientationToTransfrom(
2753 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002754{
2755 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002756 switch (orientation) {
2757 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002758 flags = Transform::ROT_0;
2759 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002760 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002761 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002762 break;
2763 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002764 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002765 break;
2766 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002767 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002768 break;
2769 default:
2770 return BAD_VALUE;
2771 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002772 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002773 return NO_ERROR;
2774}
2775
2776status_t GraphicPlane::setOrientation(int orientation)
2777{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002778 // If the rotation can be handled in hardware, this is where
2779 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002780
2781 const DisplayHardware& hw(displayHardware());
2782 const float w = mDisplayWidth;
2783 const float h = mDisplayHeight;
2784 mWidth = int(w);
2785 mHeight = int(h);
2786
2787 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002788 GraphicPlane::orientationToTransfrom(orientation, w, h,
2789 &orientationTransform);
2790 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2791 mWidth = int(h);
2792 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002793 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002794
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002795 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002796 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002797 return NO_ERROR;
2798}
2799
2800const DisplayHardware& GraphicPlane::displayHardware() const {
2801 return *mHw;
2802}
2803
Mathias Agopian59119e62010-10-11 12:37:43 -07002804DisplayHardware& GraphicPlane::editDisplayHardware() {
2805 return *mHw;
2806}
2807
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002808const Transform& GraphicPlane::transform() const {
2809 return mGlobalTransform;
2810}
2811
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002812EGLDisplay GraphicPlane::getEGLDisplay() const {
2813 return mHw->getEGLDisplay();
2814}
2815
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002816// ---------------------------------------------------------------------------
2817
2818}; // namespace android