blob: d5a8d083e8c265d09cd0e36b5b9543b24129b1ac [file] [log] [blame]
The Android Open Source Project9066cfe2009-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian0dd593f2011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070036
Mathias Agopian79f39eb2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include <utils/String8.h>
40#include <utils/String16.h>
41#include <utils/StopWatch.h>
42
Mathias Agopian6950e422009-10-05 17:07:12 -070043#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070044#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47#include <pixelflinger/pixelflinger.h>
48#include <GLES/gl.h>
49
50#include "clz.h"
Mathias Agopian79f39eb2011-11-17 17:49:17 -080051#include "DisplayEventConnection.h"
52#include "EventThread.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070053#include "GLExtensions.h"
Mathias Agopian93d75ec2011-08-15 20:44:40 -070054#include "DdmConnection.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056#include "LayerDim.h"
Mathias Agopian0ab84ef2011-10-13 16:02:48 -070057#include "LayerScreenshot.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070061#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Mathias Agopian7bb843c2011-04-20 14:20:59 -070063#include <private/surfaceflinger/SharedBufferStack.h>
64
Mathias Agopian627e7b52009-05-21 19:21:59 -070065/* ideally AID_GRAPHICS would be in a semi-public header
66 * or there would be a way to map a user/group name to its id
67 */
68#ifndef AID_GRAPHICS
69#define AID_GRAPHICS 1003
70#endif
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072#define DISPLAY_COUNT 1
73
74namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075// ---------------------------------------------------------------------------
76
Mathias Agopian0dd593f2011-06-27 16:05:52 -070077const String16 sHardwareTest("android.permission.HARDWARE_TEST");
78const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
79const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
80const String16 sDump("android.permission.DUMP");
81
82// ---------------------------------------------------------------------------
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084SurfaceFlinger::SurfaceFlinger()
85 : BnSurfaceComposer(), Thread(false),
86 mTransactionFlags(0),
Jamie Gennis122aa6b2011-10-12 17:39:00 -070087 mTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070088 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 mBootTime(systemTime()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070091 mHwWorkListDirty(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070092 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 mDebugBackground(0),
Mathias Agopian93d75ec2011-08-15 20:44:40 -070095 mDebugDDMS(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070096 mDebugDisableHWC(0),
Mathias Agopian2143fe02011-08-23 18:03:18 -070097 mDebugDisableTransformHint(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070098 mDebugInSwapBuffers(0),
99 mLastSwapBufferTime(0),
100 mDebugInTransaction(0),
101 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -0700102 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 mConsoleSignals(0),
104 mSecureFrameBuffer(0)
105{
106 init();
107}
108
109void SurfaceFlinger::init()
110{
111 LOGI("SurfaceFlinger is starting");
112
113 // debugging stuff...
114 char value[PROPERTY_VALUE_MAX];
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 property_get("debug.sf.showupdates", value, "0");
117 mDebugRegion = atoi(value);
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 property_get("debug.sf.showbackground", value, "0");
120 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700122 property_get("debug.sf.ddms", value, "0");
123 mDebugDDMS = atoi(value);
124 if (mDebugDDMS) {
125 DdmConnection::start(getServiceName());
126 }
127
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700128 LOGI_IF(mDebugRegion, "showupdates enabled");
129 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700130 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131}
132
133SurfaceFlinger::~SurfaceFlinger()
134{
135 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136}
137
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700138sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700140 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141}
142
Mathias Agopian770492c2010-05-28 14:22:23 -0700143sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700145 sp<ISurfaceComposerClient> bclient;
146 sp<Client> client(new Client(this));
147 status_t err = client->initCheck();
148 if (err == NO_ERROR) {
149 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 return bclient;
152}
153
Jamie Gennisf7acf162011-01-12 18:30:40 -0800154sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
155{
156 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
157 return gba;
158}
Mathias Agopian7623da42010-06-01 15:12:58 -0700159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
161{
162 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
163 const GraphicPlane& plane(mGraphicPlanes[dpy]);
164 return plane;
165}
166
167GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
168{
169 return const_cast<GraphicPlane&>(
170 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
171}
172
173void SurfaceFlinger::bootFinished()
174{
175 const nsecs_t now = systemTime();
176 const nsecs_t duration = now - mBootTime;
Andreas Hubere3c01832010-08-16 08:49:37 -0700177 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700178 mBootFinished = true;
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700179
180 // wait patiently for the window manager death
181 const String16 name("window");
182 sp<IBinder> window(defaultServiceManager()->getService(name));
183 if (window != 0) {
184 window->linkToDeath(this);
185 }
186
187 // stop boot animation
Mathias Agopian627e7b52009-05-21 19:21:59 -0700188 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189}
190
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700191void SurfaceFlinger::binderDied(const wp<IBinder>& who)
192{
193 // the window manager died on us. prepare its eulogy.
194
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700195 // reset screen orientation
196 setOrientation(0, eOrientationDefault, 0);
197
198 // restart the boot-animation
199 property_set("ctl.start", "bootanim");
200}
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202void SurfaceFlinger::onFirstRef()
203{
204 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
205
206 // Wait for the main thread to be done with its initialization
207 mReadyToRunBarrier.wait();
208}
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210static inline uint16_t pack565(int r, int g, int b) {
211 return (r<<11)|(g<<5)|b;
212}
213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214status_t SurfaceFlinger::readyToRun()
215{
216 LOGI( "SurfaceFlinger's main thread ready to run. "
217 "Initializing graphics H/W...");
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 // we only support one display currently
220 int dpy = 0;
221
222 {
223 // initialize the main display
224 GraphicPlane& plane(graphicPlane(dpy));
225 DisplayHardware* const hw = new DisplayHardware(this, dpy);
226 plane.setDisplayHardware(hw);
227 }
228
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700229 // create the shared control-block
230 mServerHeap = new MemoryHeapBase(4096,
231 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
232 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700233
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700234 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
235 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700236
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700237 new(mServerCblk) surface_flinger_cblk_t;
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 // initialize primary screen
240 // (other display should be initialized in the same manner, but
241 // asynchronously, as they could come and go. None of this is supported
242 // yet).
243 const GraphicPlane& plane(graphicPlane(dpy));
244 const DisplayHardware& hw = plane.displayHardware();
245 const uint32_t w = hw.getWidth();
246 const uint32_t h = hw.getHeight();
247 const uint32_t f = hw.getFormat();
248 hw.makeCurrent();
249
250 // initialize the shared control block
251 mServerCblk->connected |= 1<<dpy;
252 display_cblk_t* dcblk = mServerCblk->displays + dpy;
253 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800254 dcblk->w = plane.getWidth();
255 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 dcblk->format = f;
257 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
258 dcblk->xdpi = hw.getDpiX();
259 dcblk->ydpi = hw.getDpiY();
260 dcblk->fps = hw.getRefreshRate();
261 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700265 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 glEnableClientState(GL_VERTEX_ARRAY);
267 glEnable(GL_SCISSOR_TEST);
268 glShadeModel(GL_FLAT);
269 glDisable(GL_DITHER);
270 glDisable(GL_CULL_FACE);
271
272 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
273 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis830d0832011-10-07 14:51:16 -0700274 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 glGenTextures(1, &mWormholeTexName);
276 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
281 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis830d0832011-10-07 14:51:16 -0700282 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
283
284 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
285 glGenTextures(1, &mProtectedTexName);
286 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
291 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
292 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
294 glViewport(0, 0, w, h);
295 glMatrixMode(GL_PROJECTION);
296 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -0700297 // put the origin in the left-bottom corner
298 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800300
301 // start the EventThread
302 mEventThread = new EventThread(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
304 /*
305 * We're now ready to accept clients...
306 */
307
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800308 mReadyToRunBarrier.open();
309
Mathias Agopian627e7b52009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
317#if 0
318#pragma mark -
319#pragma mark Events Handler
320#endif
321
Mathias Agopian16aca512011-11-23 16:49:10 -0800322void SurfaceFlinger::waitForEvent() {
323 mEventQueue.waitMessage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324}
325
326void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700327 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}
329
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800330status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
331 nsecs_t reltime, uint32_t flags) {
332 return mEventQueue.postMessage(msg, reltime);
333}
334
335status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
336 nsecs_t reltime, uint32_t flags) {
337 status_t res = mEventQueue.postMessage(msg, reltime);
338 if (res == NO_ERROR) {
339 msg->wait();
340 }
341 return res;
342}
343
344// ----------------------------------------------------------------------------
345
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700346bool SurfaceFlinger::authenticateSurfaceTexture(
347 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800348 Mutex::Autolock _l(mStateLock);
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700349 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800350
351 // Check the visible layer list for the ISurface
352 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
353 size_t count = currentLayers.size();
354 for (size_t i=0 ; i<count ; i++) {
355 const sp<LayerBase>& layer(currentLayers[i]);
356 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700357 if (lbc != NULL) {
358 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
359 if (lbcBinder == surfaceTextureBinder) {
360 return true;
361 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800362 }
363 }
364
365 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700366 // SurfaceTexture gets destroyed before all the clients are done using it,
367 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800368 // will instead fail later on when the client tries to use the surface,
369 // which should be reported as "surface XYZ returned an -ENODEV". The
370 // purgatorized layers are no less authentic than the visible ones, so this
371 // should not cause any harm.
372 size_t purgatorySize = mLayerPurgatory.size();
373 for (size_t i=0 ; i<purgatorySize ; i++) {
374 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
375 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700376 if (lbc != NULL) {
377 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
378 if (lbcBinder == surfaceTextureBinder) {
379 return true;
380 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800381 }
382 }
383
384 return false;
385}
386
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800387// ----------------------------------------------------------------------------
388
389sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
390 sp<DisplayEventConnection> result(new DisplayEventConnection(this));
391 mEventThread->registerDisplayEventConnection(result);
392 return result;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700393}
394
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800395void SurfaceFlinger::cleanupDisplayEventConnection(
396 const wp<DisplayEventConnection>& connection) {
397 mEventThread->unregisterDisplayEventConnection(connection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398}
399
400// ----------------------------------------------------------------------------
401#if 0
402#pragma mark -
403#pragma mark Main loop
404#endif
405
406bool SurfaceFlinger::threadLoop()
407{
408 waitForEvent();
409
410 // check for transactions
411 if (UNLIKELY(mConsoleSignals)) {
412 handleConsoleEvents();
413 }
414
Mathias Agopian439863f2011-06-28 19:09:31 -0700415 // if we're in a global transaction, don't do anything.
416 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
417 uint32_t transactionFlags = peekTransactionFlags(mask);
418 if (UNLIKELY(transactionFlags)) {
419 handleTransaction(transactionFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421
422 // post surfaces (if needed)
423 handlePageFlip();
424
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700425 if (mDirtyRegion.isEmpty()) {
426 // nothing new to do.
427 return true;
428 }
429
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700430 if (UNLIKELY(mHwWorkListDirty)) {
431 // build the h/w work list
432 handleWorkList();
433 }
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Jamie Gennisde14eca2011-10-14 16:44:08 -0700436 if (LIKELY(hw.canDraw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700438
439 const int index = hw.getCurrentBufferIndex();
440 GraphicLog& logger(GraphicLog::getInstance());
441
442 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 handleRepaint();
444
Mathias Agopianb1a18742009-09-17 16:18:16 -0700445 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700446 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700447 hw.compositionComplete();
448
Mathias Agopian04262e92010-09-13 22:57:58 -0700449 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700450 postFramebuffer();
451
Mathias Agopian04262e92010-09-13 22:57:58 -0700452 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 } else {
454 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800455 hw.compositionComplete();
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800456 hw.waitForVSync();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458 return true;
459}
460
461void SurfaceFlinger::postFramebuffer()
462{
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700463 // this should never happen. we do the flip anyways so we don't
464 // risk to cause a deadlock with hwc
465 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiane757a872011-10-16 18:46:35 -0700466 const DisplayHardware& hw(graphicPlane(0).displayHardware());
467 const nsecs_t now = systemTime();
468 mDebugInSwapBuffers = now;
469 hw.flip(mSwapRegion);
470 mLastSwapBufferTime = systemTime() - now;
471 mDebugInSwapBuffers = 0;
472 mSwapRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473}
474
475void SurfaceFlinger::handleConsoleEvents()
476{
477 // something to do with the console
478 const DisplayHardware& hw = graphicPlane(0).displayHardware();
479
480 int what = android_atomic_and(0, &mConsoleSignals);
481 if (what & eConsoleAcquired) {
482 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700483 // this is a temporary work-around, eventually this should be called
484 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700485 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700489 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 hw.releaseScreen();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493
494 mDirtyRegion.set(hw.bounds());
495}
496
497void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
498{
Mathias Agopian69608112011-05-19 15:38:14 -0700499 Mutex::Autolock _l(mStateLock);
500 const nsecs_t now = systemTime();
501 mDebugInTransaction = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
Mathias Agopian69608112011-05-19 15:38:14 -0700503 // Here we're guaranteed that some transaction flags are set
504 // so we can call handleTransactionLocked() unconditionally.
505 // We call getTransactionFlags(), which will also clear the flags,
506 // with mStateLock held to guarantee that mCurrentState won't change
507 // until the transaction is committed.
Mathias Agopianff1d4102010-08-10 17:19:56 -0700508
Mathias Agopian69608112011-05-19 15:38:14 -0700509 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
510 transactionFlags = getTransactionFlags(mask);
511 handleTransactionLocked(transactionFlags);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700512
Mathias Agopian69608112011-05-19 15:38:14 -0700513 mLastTransactionTime = systemTime() - now;
514 mDebugInTransaction = 0;
515 invalidateHwcGeometry();
516 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700517}
518
Mathias Agopian69608112011-05-19 15:38:14 -0700519void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700520{
521 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 const size_t count = currentLayers.size();
523
524 /*
525 * Traversal of the children
526 * (perform the transaction for each of them if needed)
527 */
528
529 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
530 if (layersNeedTransaction) {
531 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700532 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
534 if (!trFlags) continue;
535
536 const uint32_t flags = layer->doTransaction(0);
537 if (flags & Layer::eVisibleRegion)
538 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 }
541
542 /*
543 * Perform our own transaction if needed
544 */
545
546 if (transactionFlags & eTransactionNeeded) {
547 if (mCurrentState.orientation != mDrawingState.orientation) {
548 // the orientation has changed, recompute all visible regions
549 // and invalidate everything.
550
551 const int dpy = 0;
552 const int orientation = mCurrentState.orientation;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700553 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 GraphicPlane& plane(graphicPlane(dpy));
555 plane.setOrientation(orientation);
556
557 // update the shared control block
558 const DisplayHardware& hw(plane.displayHardware());
559 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
560 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800561 dcblk->w = plane.getWidth();
562 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
564 mVisibleRegionsDirty = true;
565 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700568 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
569 // layers have been added
570 mVisibleRegionsDirty = true;
571 }
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 // some layers might have been removed, so
574 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700575 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700576 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700578 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700579 const size_t count = previousLayers.size();
580 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700581 const sp<LayerBase>& layer(previousLayers[i]);
582 if (currentLayers.indexOf( layer ) < 0) {
583 // this layer is not visible anymore
Mathias Agopian33863dd2009-07-28 14:20:21 -0700584 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700585 }
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
589
590 commitTransaction();
591}
592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800594 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595{
596 const GraphicPlane& plane(graphicPlane(0));
597 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700598 const DisplayHardware& hw(plane.displayHardware());
599 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600
601 Region aboveOpaqueLayers;
602 Region aboveCoveredLayers;
603 Region dirty;
604
605 bool secureFrameBuffer = false;
606
607 size_t i = currentLayers.size();
608 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700609 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 layer->validateVisibility(planeTransform);
611
612 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700613 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700615 /*
616 * opaqueRegion: area of a surface that is fully opaque.
617 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700619
620 /*
621 * visibleRegion: area of a surface that is visible on screen
622 * and not fully transparent. This is essentially the layer's
623 * footprint minus the opaque regions above it.
624 * Areas covered by a translucent surface are considered visible.
625 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700627
628 /*
629 * coveredRegion: area of a surface that is covered by all
630 * visible regions above it (which includes the translucent areas).
631 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700633
634
635 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700636 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700637 const bool translucent = !layer->isOpaque();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700638 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700640 visibleRegion.andSelf(screenRegion);
641 if (!visibleRegion.isEmpty()) {
642 // Remove the transparent area from the visible region
643 if (translucent) {
644 visibleRegion.subtractSelf(layer->transparentRegionScreen);
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700647 // compute the opaque region
648 const int32_t layerOrientation = layer->getOrientation();
649 if (s.alpha==255 && !translucent &&
650 ((layerOrientation & Transform::ROT_INVALID) == false)) {
651 // the opaque region is the layer's footprint
652 opaqueRegion = visibleRegion;
653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655 }
656
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700657 // Clip the covered region to the visible region
658 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
659
660 // Update aboveCoveredLayers for next (lower) layer
661 aboveCoveredLayers.orSelf(visibleRegion);
662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 // subtract the opaque region covered by the layers above us
664 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665
666 // compute this layer's dirty region
667 if (layer->contentDirty) {
668 // we need to invalidate the whole region
669 dirty = visibleRegion;
670 // as well, as the old visible region
671 dirty.orSelf(layer->visibleRegionScreen);
672 layer->contentDirty = false;
673 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700674 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700675 * the exposed region consists of two components:
676 * 1) what's VISIBLE now and was COVERED before
677 * 2) what's EXPOSED now less what was EXPOSED before
678 *
679 * note that (1) is conservative, we start with the whole
680 * visible region but only keep what used to be covered by
681 * something -- which mean it may have been exposed.
682 *
683 * (2) handles areas that were not covered by anything but got
684 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700685 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700686 const Region newExposed = visibleRegion - coveredRegion;
687 const Region oldVisibleRegion = layer->visibleRegionScreen;
688 const Region oldCoveredRegion = layer->coveredRegionScreen;
689 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
690 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692 dirty.subtractSelf(aboveOpaqueLayers);
693
694 // accumulate to the screen dirty region
695 dirtyRegion.orSelf(dirty);
696
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700697 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 // Store the visible region is screen space
701 layer->setVisibleRegion(visibleRegion);
702 layer->setCoveredRegion(coveredRegion);
703
Mathias Agopian12cedff2009-07-28 10:57:27 -0700704 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 if (layer->isSecure() && !visibleRegion.isEmpty()) {
706 secureFrameBuffer = true;
707 }
708 }
709
Mathias Agopian12cedff2009-07-28 10:57:27 -0700710 // invalidate the areas where a layer was removed
711 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
712 mDirtyRegionRemovedLayer.clear();
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 mSecureFrameBuffer = secureFrameBuffer;
715 opaqueRegion = aboveOpaqueLayers;
716}
717
718
719void SurfaceFlinger::commitTransaction()
720{
721 mDrawingState = mCurrentState;
Jamie Gennis122aa6b2011-10-12 17:39:00 -0700722 mTransationPending = false;
Mathias Agopian9779b2212009-09-07 16:32:45 -0700723 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724}
725
726void SurfaceFlinger::handlePageFlip()
727{
728 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800729 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 visibleRegions |= lockPageFlip(currentLayers);
731
732 const DisplayHardware& hw = graphicPlane(0).displayHardware();
733 const Region screenRegion(hw.bounds());
734 if (visibleRegions) {
735 Region opaqueRegion;
736 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700737
738 /*
739 * rebuild the visible layer list
740 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800741 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700742 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700743 mVisibleLayersSortedByZ.setCapacity(count);
744 for (size_t i=0 ; i<count ; i++) {
745 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
746 mVisibleLayersSortedByZ.add(currentLayers[i]);
747 }
748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 mWormholeRegion = screenRegion.subtract(opaqueRegion);
750 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800751 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753
754 unlockPageFlip(currentLayers);
Mathias Agopian6497eab2011-10-21 15:18:28 -0700755
756 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 mDirtyRegion.andSelf(screenRegion);
758}
759
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800760void SurfaceFlinger::invalidateHwcGeometry()
761{
762 mHwWorkListDirty = true;
763}
764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
766{
767 bool recomputeVisibleRegions = false;
768 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700769 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700771 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 layer->lockPageFlip(recomputeVisibleRegions);
773 }
774 return recomputeVisibleRegions;
775}
776
777void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
778{
779 const GraphicPlane& plane(graphicPlane(0));
780 const Transform& planeTransform(plane.transform());
781 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700782 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700784 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 layer->unlockPageFlip(planeTransform, mDirtyRegion);
786 }
787}
788
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700789void SurfaceFlinger::handleWorkList()
790{
791 mHwWorkListDirty = false;
792 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
793 if (hwc.initCheck() == NO_ERROR) {
794 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
795 const size_t count = currentLayers.size();
796 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700797 hwc_layer_t* const cur(hwc.getLayers());
798 for (size_t i=0 ; cur && i<count ; i++) {
799 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700800 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian6a969242010-09-22 18:58:01 -0700801 cur[i].compositionType = HWC_FRAMEBUFFER;
802 cur[i].flags |= HWC_SKIP_LAYER;
803 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700804 }
805 }
806}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808void SurfaceFlinger::handleRepaint()
809{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700810 // compute the invalid region
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700811 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812
813 if (UNLIKELY(mDebugRegion)) {
814 debugFlashRegions();
815 }
816
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700817 // set the frame buffer
818 const DisplayHardware& hw(graphicPlane(0).displayHardware());
819 glMatrixMode(GL_MODELVIEW);
820 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821
822 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700823 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
824 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700825 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700826 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
827 // takes a rectangle, we must make sure to update that whole
828 // rectangle in that case
829 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700830 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700831 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700832 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700834 // in the BUFFER_PRESERVED case, obviously, we can update only
835 // what's needed and nothing more.
836 // NOTE: this is NOT a common case, as preserving the backbuffer
837 // is costly and usually involves copying the whole update back.
838 }
839 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700840 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700841 // We need to redraw the rectangle that will be updated
842 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700843 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700844 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700845 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700846 } else {
847 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 mDirtyRegion.set(hw.bounds());
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700849 mSwapRegion = mDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851 }
852
Mathias Agopian88cde072011-09-20 17:21:56 -0700853 setupHardwareComposer(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 composeSurfaces(mDirtyRegion);
855
Mathias Agopian88cde072011-09-20 17:21:56 -0700856 // update the swap region and clear the dirty region
857 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 mDirtyRegion.clear();
859}
860
Mathias Agopian88cde072011-09-20 17:21:56 -0700861void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862{
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700863 const DisplayHardware& hw(graphicPlane(0).displayHardware());
864 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700865 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700866 if (!cur) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700867 return;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700868 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700869
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700870 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
871 size_t count = layers.size();
872
873 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700874 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
875 hwc.getNumLayers(), count);
876
877 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700878 if (hwc.initCheck() == NO_ERROR) {
879 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
880 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700881
882 /*
883 * update the per-frame h/w composer data for each layer
884 * and build the transparent region of the FB
885 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700886 for (size_t i=0 ; i<count ; i++) {
887 const sp<LayerBase>& layer(layers[i]);
888 layer->setPerFrameData(&cur[i]);
889 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700890 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700891 status_t err = hwc.prepare();
892 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700893
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700894 if (err == NO_ERROR) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700895 // what's happening here is tricky.
896 // we want to clear all the layers with the CLEAR_FB flags
897 // that are opaque.
898 // however, since some GPU are efficient at preserving
899 // the backbuffer, we want to take advantage of that so we do the
900 // clear only in the dirty region (other areas will be preserved
901 // on those GPUs).
902 // NOTE: on non backbuffer preserving GPU, the dirty region
903 // has already been expanded as needed, so the code is correct
904 // there too.
905 //
906 // However, the content of the framebuffer cannot be trusted when
907 // we switch to/from FB/OVERLAY, in which case we need to
908 // expand the dirty region to those areas too.
909 //
910 // Note also that there is a special case when switching from
911 // "no layers in FB" to "some layers in FB", where we need to redraw
912 // the entire FB, since some areas might contain uninitialized
913 // data.
914 //
915 // Also we want to make sure to not clear areas that belong to
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700916 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian88cde072011-09-20 17:21:56 -0700917 // that is, we can't erase anything outside the dirty region.
918
Mathias Agopian0a5abdb2011-09-09 01:47:48 -0700919 Region transparent;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700920
Mathias Agopian88cde072011-09-20 17:21:56 -0700921 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
922 transparent.set(hw.getBounds());
923 dirtyInOut = transparent;
924 } else {
925 for (size_t i=0 ; i<count ; i++) {
926 const sp<LayerBase>& layer(layers[i]);
927 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
928 transparent.orSelf(layer->visibleRegionScreen);
929 }
930 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
931 if (isOverlay != layer->isOverlay()) {
932 // we transitioned to/from overlay, so add this layer
933 // to the dirty region so the framebuffer can be either
934 // cleared or redrawn.
935 dirtyInOut.orSelf(layer->visibleRegionScreen);
936 }
937 layer->setOverlay(isOverlay);
Mathias Agopian45491692011-01-19 15:24:23 -0800938 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700939 // don't erase stuff outside the dirty region
940 transparent.andSelf(dirtyInOut);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700941 }
942
943 /*
944 * clear the area of the FB that need to be transparent
945 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700946 if (!transparent.isEmpty()) {
947 glClearColor(0,0,0,0);
948 Region::const_iterator it = transparent.begin();
949 Region::const_iterator const end = transparent.end();
950 const int32_t height = hw.getHeight();
951 while (it != end) {
952 const Rect& r(*it++);
953 const GLint sy = height - (r.top + r.height());
954 glScissor(r.left, sy, r.width(), r.height());
955 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian45491692011-01-19 15:24:23 -0800956 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700957 }
958 }
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700959}
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700960
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700961void SurfaceFlinger::composeSurfaces(const Region& dirty)
962{
Mathias Agopian9fe96542011-09-22 20:57:04 -0700963 const DisplayHardware& hw(graphicPlane(0).displayHardware());
964 HWComposer& hwc(hw.getHwComposer());
965
966 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
967 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700968 // should never happen unless the window manager has a bug
969 // draw something...
970 drawWormhole();
971 }
972
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700973 /*
974 * and then, render the layers targeted at the framebuffer
975 */
Mathias Agopian9fe96542011-09-22 20:57:04 -0700976 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700977 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
978 size_t count = layers.size();
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700979 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700980 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700981 continue;
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700982 }
983 const sp<LayerBase>& layer(layers[i]);
984 const Region clip(dirty.intersect(layer->visibleRegionScreen));
985 if (!clip.isEmpty()) {
986 layer->draw(clip);
987 }
988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989}
990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991void SurfaceFlinger::debugFlashRegions()
992{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700993 const DisplayHardware& hw(graphicPlane(0).displayHardware());
994 const uint32_t flags = hw.getFlags();
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700995 const int32_t height = hw.getHeight();
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700996 if (mSwapRegion.isEmpty()) {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700997 return;
998 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700999
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001000 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1001 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1002 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1003 mDirtyRegion.bounds() : hw.bounds());
1004 composeSurfaces(repaint);
1005 }
1006
Mathias Agopian9044ef02011-10-18 14:49:27 -07001007 glDisable(GL_TEXTURE_EXTERNAL_OES);
1008 glDisable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 glDisable(GL_SCISSOR_TEST);
1011
Mathias Agopiandff8e582009-05-04 14:17:04 -07001012 static int toggle = 0;
1013 toggle = 1 - toggle;
1014 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001015 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001016 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001017 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001020 Region::const_iterator it = mDirtyRegion.begin();
1021 Region::const_iterator const end = mDirtyRegion.end();
1022 while (it != end) {
1023 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 GLfloat vertices[][2] = {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001025 { r.left, height - r.top },
1026 { r.left, height - r.bottom },
1027 { r.right, height - r.bottom },
1028 { r.right, height - r.top }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 };
1030 glVertexPointer(2, GL_FLOAT, 0, vertices);
1031 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1032 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001033
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001034 hw.flip(mSwapRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035
1036 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001037 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038
1039 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040}
1041
1042void SurfaceFlinger::drawWormhole() const
1043{
1044 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1045 if (region.isEmpty())
1046 return;
1047
1048 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1049 const int32_t width = hw.getWidth();
1050 const int32_t height = hw.getHeight();
1051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001053 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001054 Region::const_iterator it = region.begin();
1055 Region::const_iterator const end = region.end();
1056 while (it != end) {
1057 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 const GLint sy = height - (r.top + r.height());
1059 glScissor(r.left, sy, r.width(), r.height());
1060 glClear(GL_COLOR_BUFFER_BIT);
1061 }
1062 } else {
1063 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1064 { width, height }, { 0, height } };
1065 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopian9044ef02011-10-18 14:49:27 -07001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 glVertexPointer(2, GL_SHORT, 0, vertices);
1068 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1069 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian9044ef02011-10-18 14:49:27 -07001070
1071 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 glEnable(GL_TEXTURE_2D);
1073 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1074 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1075 glMatrixMode(GL_TEXTURE);
1076 glLoadIdentity();
Mathias Agopian9044ef02011-10-18 14:49:27 -07001077
1078 glDisable(GL_BLEND);
1079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001081 Region::const_iterator it = region.begin();
1082 Region::const_iterator const end = region.end();
1083 while (it != end) {
1084 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 const GLint sy = height - (r.top + r.height());
1086 glScissor(r.left, sy, r.width(), r.height());
1087 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1088 }
1089 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001090 glDisable(GL_TEXTURE_2D);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001091 glLoadIdentity();
1092 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094}
1095
1096void SurfaceFlinger::debugShowFPS() const
1097{
1098 static int mFrameCount;
1099 static int mLastFrameCount = 0;
1100 static nsecs_t mLastFpsTime = 0;
1101 static float mFps = 0;
1102 mFrameCount++;
1103 nsecs_t now = systemTime();
1104 nsecs_t diff = now - mLastFpsTime;
1105 if (diff > ms2ns(250)) {
1106 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1107 mLastFpsTime = now;
1108 mLastFrameCount = mFrameCount;
1109 }
1110 // XXX: mFPS has the value we want
1111 }
1112
Mathias Agopian1473f462009-04-10 14:24:30 -07001113status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114{
1115 Mutex::Autolock _l(mStateLock);
1116 addLayer_l(layer);
1117 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1118 return NO_ERROR;
1119}
1120
Mathias Agopian593c05c2010-06-02 23:28:45 -07001121status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1122{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001123 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001124 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1125}
1126
1127ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1128 const sp<LayerBaseClient>& lbc)
1129{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001130 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001131 size_t name = client->attachLayer(lbc);
1132
1133 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001134
1135 // add this layer to the current state list
1136 addLayer_l(lbc);
1137
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001138 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001139}
1140
Mathias Agopian1473f462009-04-10 14:24:30 -07001141status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142{
1143 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001144 status_t err = purgatorizeLayer_l(layer);
1145 if (err == NO_ERROR)
1146 setTransactionFlags(eTransactionNeeded);
1147 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148}
1149
Mathias Agopian1473f462009-04-10 14:24:30 -07001150status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151{
Mathias Agopian7623da42010-06-01 15:12:58 -07001152 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1153 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001154 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1157 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001158 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 return NO_ERROR;
1160 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001161 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162}
1163
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001164status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1165{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001166 // First add the layer to the purgatory list, which makes sure it won't
1167 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001168 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001169 if (err >= 0) {
1170 mLayerPurgatory.add(layerBase);
1171 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001172
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001173 layerBase->onRemoved();
1174
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001175 // it's possible that we don't find a layer, because it might
1176 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001177 // from the user because there is a race between Client::destroySurface(),
1178 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001179 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1180}
1181
Mathias Agopian593c05c2010-06-02 23:28:45 -07001182status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001184 layer->forceVisibilityTransaction();
1185 setTransactionFlags(eTraversalNeeded);
1186 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187}
1188
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001189uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1190{
1191 return android_atomic_release_load(&mTransactionFlags);
1192}
1193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1195{
1196 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1197}
1198
Mathias Agopian898c4c92010-05-18 17:06:55 -07001199uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200{
1201 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1202 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001203 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205 return old;
1206}
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208
Jamie Gennise2909e12011-10-10 15:48:06 -07001209void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001210 int orientation, uint32_t flags) {
Mathias Agopian439863f2011-06-28 19:09:31 -07001211 Mutex::Autolock _l(mStateLock);
Mathias Agopian9779b2212009-09-07 16:32:45 -07001212
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001213 uint32_t transactionFlags = 0;
Jamie Gennise2909e12011-10-10 15:48:06 -07001214 if (mCurrentState.orientation != orientation) {
1215 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1216 mCurrentState.orientation = orientation;
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001217 transactionFlags |= eTransactionNeeded;
Jamie Gennise2909e12011-10-10 15:48:06 -07001218 } else if (orientation != eOrientationUnchanged) {
1219 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1220 orientation);
1221 }
1222 }
1223
Mathias Agopian439863f2011-06-28 19:09:31 -07001224 const size_t count = state.size();
1225 for (size_t i=0 ; i<count ; i++) {
1226 const ComposerState& s(state[i]);
1227 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001228 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian439863f2011-06-28 19:09:31 -07001229 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001230
Mathias Agopiana06c06b2011-11-07 21:58:03 -08001231 if (transactionFlags) {
1232 // this triggers the transaction
1233 setTransactionFlags(transactionFlags);
1234
1235 // if this is a synchronous transaction, wait for it to take effect
1236 // before returning.
1237 if (flags & eSynchronous) {
1238 mTransationPending = true;
1239 }
1240 while (mTransationPending) {
1241 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1242 if (CC_UNLIKELY(err != NO_ERROR)) {
1243 // just in case something goes wrong in SF, return to the
1244 // called after a few seconds.
1245 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1246 mTransationPending = false;
1247 break;
1248 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 }
1251}
1252
Andreas Hubere3c01832010-08-16 08:49:37 -07001253int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001254 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255{
1256 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1257 return BAD_VALUE;
1258
1259 Mutex::Autolock _l(mStateLock);
1260 if (mCurrentState.orientation != orientation) {
1261 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown01a98dd2011-09-20 15:08:29 -07001262 mCurrentState.orientationFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 mCurrentState.orientation = orientation;
1264 setTransactionFlags(eTransactionNeeded);
1265 mTransactionCV.wait(mStateLock);
1266 } else {
1267 orientation = BAD_VALUE;
1268 }
1269 }
1270 return orientation;
1271}
1272
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001273sp<ISurface> SurfaceFlinger::createSurface(
1274 ISurfaceComposerClient::surface_data_t* params,
1275 const String8& name,
1276 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1278 uint32_t flags)
1279{
Mathias Agopian1473f462009-04-10 14:24:30 -07001280 sp<LayerBaseClient> layer;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001281 sp<ISurface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001282
1283 if (int32_t(w|h) < 0) {
1284 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1285 int(w), int(h));
1286 return surfaceHandle;
1287 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001290 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 switch (flags & eFXSurfaceMask) {
1292 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001293 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1294 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 break;
1296 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001297 // for now we treat Blur as Dim, until we can implement it
1298 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001300 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 break;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001302 case eFXSurfaceScreenshot:
1303 layer = createScreenshotSurface(client, d, w, h, flags);
1304 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306
Mathias Agopian1473f462009-04-10 14:24:30 -07001307 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001308 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001309 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001310 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001313 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001314 params->token = token;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001315 params->identity = layer->getIdentity();
Mathias Agopian7623da42010-06-01 15:12:58 -07001316 if (normalLayer != 0) {
1317 Mutex::Autolock _l(mStateLock);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001318 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001319 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001320 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001321
Mathias Agopian593c05c2010-06-02 23:28:45 -07001322 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
1324
1325 return surfaceHandle;
1326}
1327
Mathias Agopian7623da42010-06-01 15:12:58 -07001328sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001329 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001330 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001331 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332{
1333 // initialize the surfaces
1334 switch (format) { // TODO: take h/w into account
1335 case PIXEL_FORMAT_TRANSPARENT:
1336 case PIXEL_FORMAT_TRANSLUCENT:
1337 format = PIXEL_FORMAT_RGBA_8888;
1338 break;
1339 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001340#ifdef NO_RGBX_8888
1341 format = PIXEL_FORMAT_RGB_565;
1342#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001343 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001344#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 break;
1346 }
1347
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001348#ifdef NO_RGBX_8888
1349 if (format == PIXEL_FORMAT_RGBX_8888)
1350 format = PIXEL_FORMAT_RGBA_8888;
1351#endif
1352
Mathias Agopian593c05c2010-06-02 23:28:45 -07001353 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001354 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001355 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001357 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 return layer;
1360}
1361
Mathias Agopian7623da42010-06-01 15:12:58 -07001362sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001363 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001364 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001366 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001367 return layer;
1368}
1369
1370sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1371 const sp<Client>& client, DisplayID display,
1372 uint32_t w, uint32_t h, uint32_t flags)
1373{
1374 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 return layer;
1376}
1377
Mathias Agopian593c05c2010-06-02 23:28:45 -07001378status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001380 /*
1381 * called by the window manager, when a surface should be marked for
1382 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001383 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001384 * The surface is removed from the current and drawing lists, but placed
1385 * in the purgatory queue, so it's not destroyed right-away (we need
1386 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001387 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001388
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001389 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001390 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001391 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001392 if (layer != 0) {
1393 err = purgatorizeLayer_l(layer);
1394 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001395 setTransactionFlags(eTransactionNeeded);
1396 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001397 }
1398 return err;
1399}
1400
Mathias Agopian69608112011-05-19 15:38:14 -07001401status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001402{
Mathias Agopian359140c2009-07-02 17:33:40 -07001403 // called by ~ISurface() when all references are gone
Mathias Agopian69608112011-05-19 15:38:14 -07001404 status_t err = NO_ERROR;
1405 sp<LayerBaseClient> l(layer.promote());
1406 if (l != NULL) {
1407 Mutex::Autolock _l(mStateLock);
1408 err = removeLayer_l(l);
1409 if (err == NAME_NOT_FOUND) {
1410 // The surface wasn't in the current list, which means it was
1411 // removed already, which means it is in the purgatory,
1412 // and need to be removed from there.
1413 ssize_t idx = mLayerPurgatory.remove(l);
1414 LOGE_IF(idx < 0,
1415 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001416 }
Mathias Agopian69608112011-05-19 15:38:14 -07001417 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1418 "error removing layer=%p (%s)", l.get(), strerror(-err));
1419 }
1420 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421}
1422
Mathias Agopian439863f2011-06-28 19:09:31 -07001423uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001424 const sp<Client>& client,
Mathias Agopian439863f2011-06-28 19:09:31 -07001425 const layer_state_t& s)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 uint32_t flags = 0;
Mathias Agopian439863f2011-06-28 19:09:31 -07001428 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1429 if (layer != 0) {
1430 const uint32_t what = s.what;
1431 if (what & ePositionChanged) {
1432 if (layer->setPosition(s.x, s.y))
1433 flags |= eTraversalNeeded;
1434 }
1435 if (what & eLayerChanged) {
1436 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1437 if (layer->setLayer(s.z)) {
1438 mCurrentState.layersSortedByZ.removeAt(idx);
1439 mCurrentState.layersSortedByZ.add(layer);
1440 // we need traversal (state changed)
1441 // AND transaction (list changed)
1442 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001445 if (what & eSizeChanged) {
1446 if (layer->setSize(s.w, s.h)) {
1447 flags |= eTraversalNeeded;
Mathias Agopian439863f2011-06-28 19:09:31 -07001448 }
1449 }
1450 if (what & eAlphaChanged) {
1451 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1452 flags |= eTraversalNeeded;
1453 }
1454 if (what & eMatrixChanged) {
1455 if (layer->setMatrix(s.matrix))
1456 flags |= eTraversalNeeded;
1457 }
1458 if (what & eTransparentRegionChanged) {
1459 if (layer->setTransparentRegionHint(s.transparentRegion))
1460 flags |= eTraversalNeeded;
1461 }
1462 if (what & eVisibilityChanged) {
1463 if (layer->setFlags(s.flags, s.mask))
1464 flags |= eTraversalNeeded;
1465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001467 return flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468}
1469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470void SurfaceFlinger::screenReleased(int dpy)
1471{
1472 // this may be called by a signal handler, we can't do too much in here
1473 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1474 signalEvent();
1475}
1476
1477void SurfaceFlinger::screenAcquired(int dpy)
1478{
1479 // this may be called by a signal handler, we can't do too much in here
1480 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1481 signalEvent();
1482}
1483
1484status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1485{
Erik Gilling94720d72010-12-01 16:38:01 -08001486 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 char buffer[SIZE];
1488 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001489
1490 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 snprintf(buffer, SIZE, "Permission Denial: "
1492 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1493 IPCThreadState::self()->getCallingPid(),
1494 IPCThreadState::self()->getCallingUid());
1495 result.append(buffer);
1496 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001497
1498 // figure out if we're stuck somewhere
1499 const nsecs_t now = systemTime();
1500 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1501 const nsecs_t inTransaction(mDebugInTransaction);
1502 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1503 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1504
1505 // Try to get the main lock, but don't insist if we can't
1506 // (this would indicate SF is stuck, but we want to be able to
1507 // print something in dumpsys).
1508 int retry = 3;
1509 while (mStateLock.tryLock()<0 && --retry>=0) {
1510 usleep(1000000);
1511 }
1512 const bool locked(retry >= 0);
1513 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001514 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001515 "SurfaceFlinger appears to be unresponsive, "
1516 "dumping anyways (no locks held)\n");
1517 result.append(buffer);
1518 }
1519
Mathias Agopian06a61e22011-01-19 16:15:53 -08001520 /*
1521 * Dump the visible layer list
1522 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1524 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001525 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1526 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001528 const sp<LayerBase>& layer(currentLayers[i]);
1529 layer->dump(result, buffer, SIZE);
1530 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 s.transparentRegion.dump(result, "transparentRegion");
1532 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1533 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1534 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001535
Mathias Agopian06a61e22011-01-19 16:15:53 -08001536 /*
1537 * Dump the layers in the purgatory
1538 */
1539
1540 const size_t purgatorySize = mLayerPurgatory.size();
1541 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1542 result.append(buffer);
1543 for (size_t i=0 ; i<purgatorySize ; i++) {
1544 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1545 layer->shortDump(result, buffer, SIZE);
1546 }
1547
1548 /*
1549 * Dump SurfaceFlinger global state
1550 */
1551
Mathias Agopianab951172011-07-14 18:01:49 -07001552 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian06a61e22011-01-19 16:15:53 -08001553 result.append(buffer);
Mathias Agopianab951172011-07-14 18:01:49 -07001554
1555 const GLExtensions& extensions(GLExtensions::getInstance());
1556 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1557 extensions.getVendor(),
1558 extensions.getRenderer(),
1559 extensions.getVersion());
1560 result.append(buffer);
1561 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1562 result.append(buffer);
1563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 mWormholeRegion.dump(result, "WormholeRegion");
1565 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1566 snprintf(buffer, SIZE,
Jamie Gennisde14eca2011-10-14 16:44:08 -07001567 " orientation=%d, canDraw=%d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 mCurrentState.orientation, hw.canDraw());
1569 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001570 snprintf(buffer, SIZE,
1571 " last eglSwapBuffers() time: %f us\n"
Mathias Agopianabf88be2011-10-20 17:22:38 -07001572 " last transaction time : %f us\n"
1573 " refresh-rate : %f fps\n"
1574 " x-dpi : %f\n"
1575 " y-dpi : %f\n",
1576 mLastSwapBufferTime/1000.0,
1577 mLastTransactionTime/1000.0,
1578 hw.getRefreshRate(),
1579 hw.getDpiX(),
1580 hw.getDpiY());
Mathias Agopiana8d49172009-08-26 16:36:26 -07001581 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001582
Mathias Agopiana8d49172009-08-26 16:36:26 -07001583 if (inSwapBuffersDuration || !locked) {
1584 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1585 inSwapBuffersDuration/1000.0);
1586 result.append(buffer);
1587 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001588
Mathias Agopiana8d49172009-08-26 16:36:26 -07001589 if (inTransactionDuration || !locked) {
1590 snprintf(buffer, SIZE, " transaction time: %f us\n",
1591 inTransactionDuration/1000.0);
1592 result.append(buffer);
1593 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001594
Mathias Agopian06a61e22011-01-19 16:15:53 -08001595 /*
Mathias Agopian79f39eb2011-11-17 17:49:17 -08001596 * VSYNC state
1597 */
1598 mEventThread->dump(result, buffer, SIZE);
1599
1600 /*
Mathias Agopian06a61e22011-01-19 16:15:53 -08001601 * Dump HWComposer state
1602 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001603 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian79f39eb2011-11-17 17:49:17 -08001604 snprintf(buffer, SIZE, "h/w composer state:\n");
1605 result.append(buffer);
Mathias Agopian6a969242010-09-22 18:58:01 -07001606 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1607 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001608 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian6a969242010-09-22 18:58:01 -07001609 result.append(buffer);
Mathias Agopian5bd1b272011-09-09 00:49:11 -07001610 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian6a969242010-09-22 18:58:01 -07001611
Mathias Agopian06a61e22011-01-19 16:15:53 -08001612 /*
1613 * Dump gralloc state
1614 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001615 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001616 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001617 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001618
1619 if (locked) {
1620 mStateLock.unlock();
1621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 }
1623 write(fd, result.string(), result.size());
1624 return NO_ERROR;
1625}
1626
1627status_t SurfaceFlinger::onTransact(
1628 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1629{
1630 switch (code) {
1631 case CREATE_CONNECTION:
Mathias Agopian439863f2011-06-28 19:09:31 -07001632 case SET_TRANSACTION_STATE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 case SET_ORIENTATION:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001635 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001636 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 {
1638 // codes that require permission check
1639 IPCThreadState* ipc = IPCThreadState::self();
1640 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001641 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001642 if ((uid != AID_GRAPHICS) &&
1643 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001644 LOGE("Permission Denial: "
1645 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1646 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001648 break;
1649 }
1650 case CAPTURE_SCREEN:
1651 {
1652 // codes that require permission check
1653 IPCThreadState* ipc = IPCThreadState::self();
1654 const int pid = ipc->getCallingPid();
1655 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001656 if ((uid != AID_GRAPHICS) &&
1657 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001658 LOGE("Permission Denial: "
1659 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1660 return PERMISSION_DENIED;
1661 }
1662 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1667 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001668 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001669 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001670 IPCThreadState* ipc = IPCThreadState::self();
1671 const int pid = ipc->getCallingPid();
1672 const int uid = ipc->getCallingUid();
1673 LOGE("Permission Denial: "
1674 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 return PERMISSION_DENIED;
1676 }
1677 int n;
1678 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001679 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001680 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 return NO_ERROR;
1682 case 1002: // SHOW_UPDATES
1683 n = data.readInt32();
1684 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001685 invalidateHwcGeometry();
1686 repaintEverything();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 return NO_ERROR;
1688 case 1003: // SHOW_BACKGROUND
1689 n = data.readInt32();
1690 mDebugBackground = n ? 1 : 0;
1691 return NO_ERROR;
1692 case 1004:{ // repaint everything
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001693 repaintEverything();
Mathias Agopian9779b2212009-09-07 16:32:45 -07001694 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Mathias Agopian9779b2212009-09-07 16:32:45 -07001696 case 1005:{ // force transaction
1697 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1698 return NO_ERROR;
1699 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001700 case 1006:{ // enable/disable GraphicLog
1701 int enabled = data.readInt32();
1702 GraphicLog::getInstance().setEnabled(enabled);
1703 return NO_ERROR;
1704 }
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001705 case 1008: // toggle use of hw composer
1706 n = data.readInt32();
1707 mDebugDisableHWC = n ? 1 : 0;
1708 invalidateHwcGeometry();
1709 repaintEverything();
1710 return NO_ERROR;
Mathias Agopian2143fe02011-08-23 18:03:18 -07001711 case 1009: // toggle use of transform hint
1712 n = data.readInt32();
1713 mDebugDisableTransformHint = n ? 1 : 0;
1714 invalidateHwcGeometry();
1715 repaintEverything();
1716 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001718 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 reply->writeInt32(0);
1720 reply->writeInt32(mDebugRegion);
1721 reply->writeInt32(mDebugBackground);
1722 return NO_ERROR;
1723 case 1013: {
1724 Mutex::Autolock _l(mStateLock);
1725 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1726 reply->writeInt32(hw.getPageFlipCount());
1727 }
1728 return NO_ERROR;
1729 }
1730 }
1731 return err;
1732}
1733
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001734void SurfaceFlinger::repaintEverything() {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001735 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian6497eab2011-10-21 15:18:28 -07001736 const Rect bounds(hw.getBounds());
1737 setInvalidateRegion(Region(bounds));
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001738 signalEvent();
1739}
1740
Mathias Agopian6497eab2011-10-21 15:18:28 -07001741void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1742 Mutex::Autolock _l(mInvalidateLock);
1743 mInvalidateRegion = reg;
1744}
1745
1746Region SurfaceFlinger::getAndClearInvalidateRegion() {
1747 Mutex::Autolock _l(mInvalidateLock);
1748 Region reg(mInvalidateRegion);
1749 mInvalidateRegion.clear();
1750 return reg;
1751}
1752
Mathias Agopianaab758e2010-10-11 12:37:43 -07001753// ---------------------------------------------------------------------------
1754
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001755status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1756 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1757{
1758 Mutex::Autolock _l(mStateLock);
1759 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1760}
1761
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001762status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1763 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001764{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001765 if (!GLExtensions::getInstance().haveFramebufferObject())
1766 return INVALID_OPERATION;
1767
1768 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001769 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001770 const uint32_t hw_w = hw.getWidth();
1771 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001772 GLfloat u = 1;
1773 GLfloat v = 1;
1774
1775 // make sure to clear all GL error flags
1776 while ( glGetError() != GL_NO_ERROR ) ;
1777
1778 // create a FBO
1779 GLuint name, tname;
1780 glGenTextures(1, &tname);
1781 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001782 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1783 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001784 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001785 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001786 GLint tw = (2 << (31 - clz(hw_w)));
1787 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001788 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1789 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001790 u = GLfloat(hw_w) / tw;
1791 v = GLfloat(hw_h) / th;
1792 }
1793 glGenFramebuffersOES(1, &name);
1794 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001795 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1796 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001797
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001798 // redraw the screen entirely...
Mathias Agopian9044ef02011-10-18 14:49:27 -07001799 glDisable(GL_TEXTURE_EXTERNAL_OES);
1800 glDisable(GL_TEXTURE_2D);
Mathias Agopian01d650e2011-10-26 15:11:59 -07001801 glDisable(GL_SCISSOR_TEST);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001802 glClearColor(0,0,0,1);
1803 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian01d650e2011-10-26 15:11:59 -07001804 glEnable(GL_SCISSOR_TEST);
Mathias Agopianb946a562011-10-10 19:02:07 -07001805 glMatrixMode(GL_MODELVIEW);
1806 glLoadIdentity();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001807 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1808 const size_t count = layers.size();
1809 for (size_t i=0 ; i<count ; ++i) {
1810 const sp<LayerBase>& layer(layers[i]);
1811 layer->drawForSreenShot();
1812 }
1813
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001814 hw.compositionComplete();
1815
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001816 // back to main framebuffer
1817 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1818 glDisable(GL_SCISSOR_TEST);
1819 glDeleteFramebuffersOES(1, &name);
1820
1821 *textureName = tname;
1822 *uOut = u;
1823 *vOut = v;
1824 return NO_ERROR;
1825}
1826
1827// ---------------------------------------------------------------------------
1828
1829status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1830{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001831 // get screen geometry
1832 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1833 const uint32_t hw_w = hw.getWidth();
1834 const uint32_t hw_h = hw.getHeight();
Mathias Agopianb946a562011-10-10 19:02:07 -07001835 const Region screenBounds(hw.getBounds());
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001836
1837 GLfloat u, v;
1838 GLuint tname;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001839 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001840 if (result != NO_ERROR) {
1841 return result;
1842 }
1843
1844 GLfloat vtx[8];
Mathias Agopianb946a562011-10-10 19:02:07 -07001845 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001846 glBindTexture(GL_TEXTURE_2D, tname);
1847 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1848 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1849 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1850 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1851 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1852 glVertexPointer(2, GL_FLOAT, 0, vtx);
1853
Mathias Agopian3a831d22011-07-07 17:30:31 -07001854 /*
1855 * Texture coordinate mapping
1856 *
1857 * u
1858 * 1 +----------+---+
1859 * | | | | image is inverted
1860 * | V | | w.r.t. the texture
1861 * 1-v +----------+ | coordinates
1862 * | |
1863 * | |
1864 * | |
1865 * 0 +--------------+
1866 * 0 1
1867 *
1868 */
1869
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001870 class s_curve_interpolator {
1871 const float nbFrames, s, v;
1872 public:
1873 s_curve_interpolator(int nbFrames, float s)
1874 : nbFrames(1.0f / (nbFrames-1)), s(s),
1875 v(1.0f + expf(-s + 0.5f*s)) {
1876 }
1877 float operator()(int f) {
1878 const float x = f * nbFrames;
1879 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1880 }
1881 };
1882
1883 class v_stretch {
1884 const GLfloat hw_w, hw_h;
1885 public:
1886 v_stretch(uint32_t hw_w, uint32_t hw_h)
1887 : hw_w(hw_w), hw_h(hw_h) {
1888 }
1889 void operator()(GLfloat* vtx, float v) {
1890 const GLfloat w = hw_w + (hw_w * v);
1891 const GLfloat h = hw_h - (hw_h * v);
1892 const GLfloat x = (hw_w - w) * 0.5f;
1893 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001894 vtx[0] = x; vtx[1] = y;
1895 vtx[2] = x; vtx[3] = y + h;
1896 vtx[4] = x + w; vtx[5] = y + h;
1897 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001898 }
1899 };
1900
1901 class h_stretch {
1902 const GLfloat hw_w, hw_h;
1903 public:
1904 h_stretch(uint32_t hw_w, uint32_t hw_h)
1905 : hw_w(hw_w), hw_h(hw_h) {
1906 }
1907 void operator()(GLfloat* vtx, float v) {
1908 const GLfloat w = hw_w - (hw_w * v);
1909 const GLfloat h = 1.0f;
1910 const GLfloat x = (hw_w - w) * 0.5f;
1911 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001912 vtx[0] = x; vtx[1] = y;
1913 vtx[2] = x; vtx[3] = y + h;
1914 vtx[4] = x + w; vtx[5] = y + h;
1915 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001916 }
1917 };
1918
1919 // the full animation is 24 frames
Mathias Agopian3a831d22011-07-07 17:30:31 -07001920 char value[PROPERTY_VALUE_MAX];
1921 property_get("debug.sf.electron_frames", value, "24");
1922 int nbFrames = (atoi(value) + 1) >> 1;
1923 if (nbFrames <= 0) // just in case
1924 nbFrames = 24;
1925
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001926 s_curve_interpolator itr(nbFrames, 7.5f);
1927 s_curve_interpolator itg(nbFrames, 8.0f);
1928 s_curve_interpolator itb(nbFrames, 8.5f);
1929
1930 v_stretch vverts(hw_w, hw_h);
Mathias Agopianb946a562011-10-10 19:02:07 -07001931
1932 glMatrixMode(GL_TEXTURE);
1933 glLoadIdentity();
1934 glMatrixMode(GL_MODELVIEW);
1935 glLoadIdentity();
1936
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001937 glEnable(GL_BLEND);
1938 glBlendFunc(GL_ONE, GL_ONE);
1939 for (int i=0 ; i<nbFrames ; i++) {
1940 float x, y, w, h;
1941 const float vr = itr(i);
1942 const float vg = itg(i);
1943 const float vb = itb(i);
1944
1945 // clear screen
1946 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001947 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001948 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001949
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001950 // draw the red plane
1951 vverts(vtx, vr);
1952 glColorMask(1,0,0,1);
1953 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001954
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001955 // draw the green plane
1956 vverts(vtx, vg);
1957 glColorMask(0,1,0,1);
1958 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001959
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001960 // draw the blue plane
1961 vverts(vtx, vb);
1962 glColorMask(0,0,1,1);
1963 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001964
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001965 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001966 glDisable(GL_TEXTURE_2D);
1967 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001968 glColor4f(vg, vg, vg, 1);
1969 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1970 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001971 }
1972
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001973 h_stretch hverts(hw_w, hw_h);
1974 glDisable(GL_BLEND);
1975 glDisable(GL_TEXTURE_2D);
1976 glColorMask(1,1,1,1);
1977 for (int i=0 ; i<nbFrames ; i++) {
1978 const float v = itg(i);
1979 hverts(vtx, v);
1980 glClear(GL_COLOR_BUFFER_BIT);
1981 glColor4f(1-v, 1-v, 1-v, 1);
1982 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1983 hw.flip(screenBounds);
1984 }
1985
1986 glColorMask(1,1,1,1);
1987 glEnable(GL_SCISSOR_TEST);
1988 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1989 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001990 glDisable(GL_TEXTURE_2D);
Mathias Agopian9044ef02011-10-18 14:49:27 -07001991 glDisable(GL_BLEND);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001992 return NO_ERROR;
1993}
1994
1995status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1996{
1997 status_t result = PERMISSION_DENIED;
1998
1999 if (!GLExtensions::getInstance().haveFramebufferObject())
2000 return INVALID_OPERATION;
2001
2002
2003 // get screen geometry
2004 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2005 const uint32_t hw_w = hw.getWidth();
2006 const uint32_t hw_h = hw.getHeight();
2007 const Region screenBounds(hw.bounds());
2008
2009 GLfloat u, v;
2010 GLuint tname;
2011 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2012 if (result != NO_ERROR) {
2013 return result;
2014 }
2015
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002016 GLfloat vtx[8];
2017 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002018 glBindTexture(GL_TEXTURE_2D, tname);
2019 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2020 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2021 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2022 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2023 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2024 glVertexPointer(2, GL_FLOAT, 0, vtx);
2025
2026 class s_curve_interpolator {
2027 const float nbFrames, s, v;
2028 public:
2029 s_curve_interpolator(int nbFrames, float s)
2030 : nbFrames(1.0f / (nbFrames-1)), s(s),
2031 v(1.0f + expf(-s + 0.5f*s)) {
2032 }
2033 float operator()(int f) {
2034 const float x = f * nbFrames;
2035 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2036 }
2037 };
2038
2039 class v_stretch {
2040 const GLfloat hw_w, hw_h;
2041 public:
2042 v_stretch(uint32_t hw_w, uint32_t hw_h)
2043 : hw_w(hw_w), hw_h(hw_h) {
2044 }
2045 void operator()(GLfloat* vtx, float v) {
2046 const GLfloat w = hw_w + (hw_w * v);
2047 const GLfloat h = hw_h - (hw_h * v);
2048 const GLfloat x = (hw_w - w) * 0.5f;
2049 const GLfloat y = (hw_h - h) * 0.5f;
2050 vtx[0] = x; vtx[1] = y;
2051 vtx[2] = x; vtx[3] = y + h;
2052 vtx[4] = x + w; vtx[5] = y + h;
2053 vtx[6] = x + w; vtx[7] = y;
2054 }
2055 };
2056
2057 class h_stretch {
2058 const GLfloat hw_w, hw_h;
2059 public:
2060 h_stretch(uint32_t hw_w, uint32_t hw_h)
2061 : hw_w(hw_w), hw_h(hw_h) {
2062 }
2063 void operator()(GLfloat* vtx, float v) {
2064 const GLfloat w = hw_w - (hw_w * v);
2065 const GLfloat h = 1.0f;
2066 const GLfloat x = (hw_w - w) * 0.5f;
2067 const GLfloat y = (hw_h - h) * 0.5f;
2068 vtx[0] = x; vtx[1] = y;
2069 vtx[2] = x; vtx[3] = y + h;
2070 vtx[4] = x + w; vtx[5] = y + h;
2071 vtx[6] = x + w; vtx[7] = y;
2072 }
2073 };
2074
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002075 // the full animation is 12 frames
2076 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002077 s_curve_interpolator itr(nbFrames, 7.5f);
2078 s_curve_interpolator itg(nbFrames, 8.0f);
2079 s_curve_interpolator itb(nbFrames, 8.5f);
2080
2081 h_stretch hverts(hw_w, hw_h);
2082 glDisable(GL_BLEND);
2083 glDisable(GL_TEXTURE_2D);
2084 glColorMask(1,1,1,1);
2085 for (int i=nbFrames-1 ; i>=0 ; i--) {
2086 const float v = itg(i);
2087 hverts(vtx, v);
2088 glClear(GL_COLOR_BUFFER_BIT);
2089 glColor4f(1-v, 1-v, 1-v, 1);
2090 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2091 hw.flip(screenBounds);
2092 }
2093
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002094 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002095 v_stretch vverts(hw_w, hw_h);
2096 glEnable(GL_BLEND);
2097 glBlendFunc(GL_ONE, GL_ONE);
2098 for (int i=nbFrames-1 ; i>=0 ; i--) {
2099 float x, y, w, h;
2100 const float vr = itr(i);
2101 const float vg = itg(i);
2102 const float vb = itb(i);
2103
2104 // clear screen
2105 glColorMask(1,1,1,1);
2106 glClear(GL_COLOR_BUFFER_BIT);
2107 glEnable(GL_TEXTURE_2D);
2108
2109 // draw the red plane
2110 vverts(vtx, vr);
2111 glColorMask(1,0,0,1);
2112 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2113
2114 // draw the green plane
2115 vverts(vtx, vg);
2116 glColorMask(0,1,0,1);
2117 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2118
2119 // draw the blue plane
2120 vverts(vtx, vb);
2121 glColorMask(0,0,1,1);
2122 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2123
2124 hw.flip(screenBounds);
2125 }
2126
2127 glColorMask(1,1,1,1);
2128 glEnable(GL_SCISSOR_TEST);
2129 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002130 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002131 glDisable(GL_TEXTURE_2D);
Mathias Agopian9044ef02011-10-18 14:49:27 -07002132 glDisable(GL_BLEND);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002133
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002134 return NO_ERROR;
2135}
2136
2137// ---------------------------------------------------------------------------
2138
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002139status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002140{
2141 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2142 if (!hw.canDraw()) {
2143 // we're already off
2144 return NO_ERROR;
2145 }
Mathias Agopian7f972582011-09-02 12:22:39 -07002146
2147 // turn off hwc while we're doing the animation
2148 hw.getHwComposer().disable();
2149 // and make sure to turn it back on (if needed) next time we compose
2150 invalidateHwcGeometry();
2151
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002152 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2153 electronBeamOffAnimationImplLocked();
2154 }
2155
2156 // always clear the whole screen at the end of the animation
2157 glClearColor(0,0,0,1);
2158 glDisable(GL_SCISSOR_TEST);
2159 glClear(GL_COLOR_BUFFER_BIT);
2160 glEnable(GL_SCISSOR_TEST);
2161 hw.flip( Region(hw.bounds()) );
2162
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002163 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002164}
2165
2166status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2167{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002168 class MessageTurnElectronBeamOff : public MessageBase {
2169 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002170 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002171 status_t result;
2172 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002173 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2174 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002175 }
2176 status_t getResult() const {
2177 return result;
2178 }
2179 virtual bool handler() {
2180 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002181 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002182 return true;
2183 }
2184 };
2185
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002186 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002187 status_t res = postMessageSync(msg);
2188 if (res == NO_ERROR) {
2189 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002190
2191 // work-around: when the power-manager calls us we activate the
2192 // animation. eventually, the "on" animation will be called
2193 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002194 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002195 }
2196 return res;
2197}
2198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002200
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002201status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002202{
2203 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2204 if (hw.canDraw()) {
2205 // we're already on
2206 return NO_ERROR;
2207 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002208 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2209 electronBeamOnAnimationImplLocked();
2210 }
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002211
2212 // make sure to redraw the whole screen when the animation is done
2213 mDirtyRegion.set(hw.bounds());
2214 signalEvent();
2215
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002216 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002217}
2218
2219status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2220{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002221 class MessageTurnElectronBeamOn : public MessageBase {
2222 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002223 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002224 status_t result;
2225 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002226 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2227 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002228 }
2229 status_t getResult() const {
2230 return result;
2231 }
2232 virtual bool handler() {
2233 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002234 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002235 return true;
2236 }
2237 };
2238
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002239 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002240 return NO_ERROR;
2241}
2242
2243// ---------------------------------------------------------------------------
2244
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002245status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2246 sp<IMemoryHeap>* heap,
2247 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002248 uint32_t sw, uint32_t sh,
2249 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002250{
2251 status_t result = PERMISSION_DENIED;
2252
2253 // only one display supported for now
2254 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2255 return BAD_VALUE;
2256
2257 if (!GLExtensions::getInstance().haveFramebufferObject())
2258 return INVALID_OPERATION;
2259
2260 // get screen geometry
2261 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2262 const uint32_t hw_w = hw.getWidth();
2263 const uint32_t hw_h = hw.getHeight();
2264
2265 if ((sw > hw_w) || (sh > hw_h))
2266 return BAD_VALUE;
2267
2268 sw = (!sw) ? hw_w : sw;
2269 sh = (!sh) ? hw_h : sh;
2270 const size_t size = sw * sh * 4;
2271
Mathias Agopian32ae0942011-03-02 18:45:50 -08002272 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2273 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002274
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002275 // make sure to clear all GL error flags
2276 while ( glGetError() != GL_NO_ERROR ) ;
2277
2278 // create a FBO
2279 GLuint name, tname;
2280 glGenRenderbuffersOES(1, &tname);
2281 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2282 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2283 glGenFramebuffersOES(1, &name);
2284 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2285 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2286 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2287
2288 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002289
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002290 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2291
2292 // invert everything, b/c glReadPixel() below will invert the FB
2293 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002294 glScissor(0, 0, sw, sh);
Mathias Agopian3a831d22011-07-07 17:30:31 -07002295 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002296 glMatrixMode(GL_PROJECTION);
2297 glPushMatrix();
2298 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -07002299 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002300 glMatrixMode(GL_MODELVIEW);
2301
2302 // redraw the screen entirely...
2303 glClearColor(0,0,0,1);
2304 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002305
Jamie Gennis830d0832011-10-07 14:51:16 -07002306 const LayerVector& layers(mDrawingState.layersSortedByZ);
2307 const size_t count = layers.size();
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002308 for (size_t i=0 ; i<count ; ++i) {
2309 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianec49d892011-08-25 14:36:43 -07002310 const uint32_t flags = layer->drawingState().flags;
2311 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2312 const uint32_t z = layer->drawingState().z;
2313 if (z >= minLayerZ && z <= maxLayerZ) {
2314 layer->drawForSreenShot();
2315 }
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002316 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002317 }
2318
2319 // XXX: this is needed on tegra
Mathias Agopian3a831d22011-07-07 17:30:31 -07002320 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002321 glScissor(0, 0, sw, sh);
2322
2323 // check for errors and return screen capture
2324 if (glGetError() != GL_NO_ERROR) {
2325 // error while rendering
2326 result = INVALID_OPERATION;
2327 } else {
2328 // allocate shared memory large enough to hold the
2329 // screen capture
2330 sp<MemoryHeapBase> base(
2331 new MemoryHeapBase(size, 0, "screen-capture") );
2332 void* const ptr = base->getBase();
2333 if (ptr) {
2334 // capture the screen with glReadPixels()
2335 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2336 if (glGetError() == GL_NO_ERROR) {
2337 *heap = base;
2338 *w = sw;
2339 *h = sh;
2340 *f = PIXEL_FORMAT_RGBA_8888;
2341 result = NO_ERROR;
2342 }
2343 } else {
2344 result = NO_MEMORY;
2345 }
2346 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002347 glEnable(GL_SCISSOR_TEST);
2348 glViewport(0, 0, hw_w, hw_h);
2349 glMatrixMode(GL_PROJECTION);
2350 glPopMatrix();
2351 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002352 } else {
2353 result = BAD_VALUE;
2354 }
2355
2356 // release FBO resources
2357 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2358 glDeleteRenderbuffersOES(1, &tname);
2359 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002360
2361 hw.compositionComplete();
2362
Mathias Agopian32ae0942011-03-02 18:45:50 -08002363 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002364
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002365 return result;
2366}
2367
2368
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002369status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2370 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002371 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002372 uint32_t sw, uint32_t sh,
2373 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002374{
2375 // only one display supported for now
2376 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2377 return BAD_VALUE;
2378
2379 if (!GLExtensions::getInstance().haveFramebufferObject())
2380 return INVALID_OPERATION;
2381
2382 class MessageCaptureScreen : public MessageBase {
2383 SurfaceFlinger* flinger;
2384 DisplayID dpy;
2385 sp<IMemoryHeap>* heap;
2386 uint32_t* w;
2387 uint32_t* h;
2388 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002389 uint32_t sw;
2390 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002391 uint32_t minLayerZ;
2392 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002393 status_t result;
2394 public:
2395 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002396 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002397 uint32_t sw, uint32_t sh,
2398 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002399 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002400 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2401 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2402 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002403 {
2404 }
2405 status_t getResult() const {
2406 return result;
2407 }
2408 virtual bool handler() {
2409 Mutex::Autolock _l(flinger->mStateLock);
2410
2411 // if we have secure windows, never allow the screen capture
2412 if (flinger->mSecureFrameBuffer)
2413 return true;
2414
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002415 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002416 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002417
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002418 return true;
2419 }
2420 };
2421
2422 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002423 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002424 status_t res = postMessageSync(msg);
2425 if (res == NO_ERROR) {
2426 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2427 }
2428 return res;
2429}
2430
2431// ---------------------------------------------------------------------------
2432
Mathias Agopian7623da42010-06-01 15:12:58 -07002433sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2434{
2435 sp<Layer> result;
2436 Mutex::Autolock _l(mStateLock);
2437 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2438 return result;
2439}
2440
2441// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442
Mathias Agopian593c05c2010-06-02 23:28:45 -07002443Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002444 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446}
2447
Mathias Agopian593c05c2010-06-02 23:28:45 -07002448Client::~Client()
2449{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002450 const size_t count = mLayers.size();
2451 for (size_t i=0 ; i<count ; i++) {
2452 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2453 if (layer != 0) {
2454 mFlinger->removeLayer(layer);
2455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 }
2457}
2458
Mathias Agopian593c05c2010-06-02 23:28:45 -07002459status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002460 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461}
Mathias Agopian1473f462009-04-10 14:24:30 -07002462
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002463size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002464{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002465 Mutex::Autolock _l(mLock);
2466 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002467 mLayers.add(name, layer);
2468 return name;
2469}
2470
Mathias Agopian7623da42010-06-01 15:12:58 -07002471void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002472{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002473 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002474 // we do a linear search here, because this doesn't happen often
2475 const size_t count = mLayers.size();
2476 for (size_t i=0 ; i<count ; i++) {
2477 if (mLayers.valueAt(i) == layer) {
2478 mLayers.removeItemsAt(i, 1);
2479 break;
2480 }
2481 }
2482}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002483sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2484{
2485 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002486 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002487 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002488 if (layer != 0) {
2489 lbc = layer.promote();
2490 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002491 }
2492 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493}
2494
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002495
2496status_t Client::onTransact(
2497 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2498{
2499 // these must be checked
2500 IPCThreadState* ipc = IPCThreadState::self();
2501 const int pid = ipc->getCallingPid();
2502 const int uid = ipc->getCallingUid();
2503 const int self_pid = getpid();
2504 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2505 // we're called from a different process, do the real check
Mathias Agopian0dd593f2011-06-27 16:05:52 -07002506 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002507 {
2508 LOGE("Permission Denial: "
2509 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2510 return PERMISSION_DENIED;
2511 }
2512 }
2513 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopian7623da42010-06-01 15:12:58 -07002514}
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002515
2516
Mathias Agopian593c05c2010-06-02 23:28:45 -07002517sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002518 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002519 const String8& name,
2520 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 uint32_t flags)
2522{
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002523 /*
2524 * createSurface must be called from the GL thread so that it can
2525 * have access to the GL context.
2526 */
2527
2528 class MessageCreateSurface : public MessageBase {
2529 sp<ISurface> result;
2530 SurfaceFlinger* flinger;
2531 ISurfaceComposerClient::surface_data_t* params;
2532 Client* client;
2533 const String8& name;
2534 DisplayID display;
2535 uint32_t w, h;
2536 PixelFormat format;
2537 uint32_t flags;
2538 public:
2539 MessageCreateSurface(SurfaceFlinger* flinger,
2540 ISurfaceComposerClient::surface_data_t* params,
2541 const String8& name, Client* client,
2542 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2543 uint32_t flags)
2544 : flinger(flinger), params(params), client(client), name(name),
2545 display(display), w(w), h(h), format(format), flags(flags)
2546 {
2547 }
2548 sp<ISurface> getResult() const { return result; }
2549 virtual bool handler() {
2550 result = flinger->createSurface(params, name, client,
2551 display, w, h, format, flags);
2552 return true;
2553 }
2554 };
2555
2556 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2557 params, name, this, display, w, h, format, flags);
2558 mFlinger->postMessageSync(msg);
2559 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002561status_t Client::destroySurface(SurfaceID sid) {
2562 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564
2565// ---------------------------------------------------------------------------
2566
Jamie Gennisf7acf162011-01-12 18:30:40 -08002567GraphicBufferAlloc::GraphicBufferAlloc() {}
2568
2569GraphicBufferAlloc::~GraphicBufferAlloc() {}
2570
2571sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002572 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennisf7acf162011-01-12 18:30:40 -08002573 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2574 status_t err = graphicBuffer->initCheck();
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002575 *error = err;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002576 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002577 if (err == NO_MEMORY) {
2578 GraphicBuffer::dumpAllocationsToSystemLog();
2579 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002580 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2581 "failed (%s), handle=%p",
2582 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennisf7acf162011-01-12 18:30:40 -08002583 return 0;
2584 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002585 return graphicBuffer;
2586}
2587
Jamie Gennisf7acf162011-01-12 18:30:40 -08002588// ---------------------------------------------------------------------------
2589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590GraphicPlane::GraphicPlane()
2591 : mHw(0)
2592{
2593}
2594
2595GraphicPlane::~GraphicPlane() {
2596 delete mHw;
2597}
2598
2599bool GraphicPlane::initialized() const {
2600 return mHw ? true : false;
2601}
2602
Mathias Agopian66c77a52010-02-08 15:49:35 -08002603int GraphicPlane::getWidth() const {
2604 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605}
2606
Mathias Agopian66c77a52010-02-08 15:49:35 -08002607int GraphicPlane::getHeight() const {
2608 return mHeight;
2609}
2610
2611void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2612{
2613 mHw = hw;
2614
2615 // initialize the display orientation transform.
2616 // it's a constant that should come from the display driver.
2617 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2618 char property[PROPERTY_VALUE_MAX];
2619 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2620 //displayOrientation
2621 switch (atoi(property)) {
2622 case 90:
2623 displayOrientation = ISurfaceComposer::eOrientation90;
2624 break;
2625 case 270:
2626 displayOrientation = ISurfaceComposer::eOrientation270;
2627 break;
2628 }
2629 }
2630
2631 const float w = hw->getWidth();
2632 const float h = hw->getHeight();
2633 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2634 &mDisplayTransform);
2635 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2636 mDisplayWidth = h;
2637 mDisplayHeight = w;
2638 } else {
2639 mDisplayWidth = w;
2640 mDisplayHeight = h;
2641 }
2642
2643 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644}
2645
2646status_t GraphicPlane::orientationToTransfrom(
2647 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002648{
2649 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 switch (orientation) {
2651 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002652 flags = Transform::ROT_0;
2653 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002655 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 break;
2657 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002658 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 break;
2660 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002661 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 break;
2663 default:
2664 return BAD_VALUE;
2665 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002666 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 return NO_ERROR;
2668}
2669
2670status_t GraphicPlane::setOrientation(int orientation)
2671{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 // If the rotation can be handled in hardware, this is where
2673 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002674
2675 const DisplayHardware& hw(displayHardware());
2676 const float w = mDisplayWidth;
2677 const float h = mDisplayHeight;
2678 mWidth = int(w);
2679 mHeight = int(h);
2680
2681 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002682 GraphicPlane::orientationToTransfrom(orientation, w, h,
2683 &orientationTransform);
2684 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2685 mWidth = int(h);
2686 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002688
Mathias Agopian3552f532009-03-27 17:58:20 -07002689 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002690 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 return NO_ERROR;
2692}
2693
2694const DisplayHardware& GraphicPlane::displayHardware() const {
2695 return *mHw;
2696}
2697
Mathias Agopianaab758e2010-10-11 12:37:43 -07002698DisplayHardware& GraphicPlane::editDisplayHardware() {
2699 return *mHw;
2700}
2701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702const Transform& GraphicPlane::transform() const {
2703 return mGlobalTransform;
2704}
2705
Mathias Agopian1473f462009-04-10 14:24:30 -07002706EGLDisplay GraphicPlane::getEGLDisplay() const {
2707 return mHw->getEGLDisplay();
2708}
2709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710// ---------------------------------------------------------------------------
2711
2712}; // namespace android