blob: 014c7e24af9dce3aadafd605f16a12f6c511662e [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
Mathias Agopiancadd3252011-11-29 17:55:46 -080072#define EGL_VERSION_HW_ANDROID 0x3143
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
76namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077// ---------------------------------------------------------------------------
78
Mathias Agopian0dd593f2011-06-27 16:05:52 -070079const String16 sHardwareTest("android.permission.HARDWARE_TEST");
80const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
81const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
82const String16 sDump("android.permission.DUMP");
83
84// ---------------------------------------------------------------------------
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086SurfaceFlinger::SurfaceFlinger()
87 : BnSurfaceComposer(), Thread(false),
88 mTransactionFlags(0),
Jamie Gennis122aa6b2011-10-12 17:39:00 -070089 mTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070090 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 mBootTime(systemTime()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070093 mHwWorkListDirty(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070094 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 mDebugBackground(0),
Mathias Agopian93d75ec2011-08-15 20:44:40 -070097 mDebugDDMS(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070098 mDebugDisableHWC(0),
Mathias Agopian2143fe02011-08-23 18:03:18 -070099 mDebugDisableTransformHint(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -0700100 mDebugInSwapBuffers(0),
101 mLastSwapBufferTime(0),
102 mDebugInTransaction(0),
103 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -0700104 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 mConsoleSignals(0),
106 mSecureFrameBuffer(0)
107{
108 init();
109}
110
111void SurfaceFlinger::init()
112{
113 LOGI("SurfaceFlinger is starting");
114
115 // debugging stuff...
116 char value[PROPERTY_VALUE_MAX];
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 property_get("debug.sf.showupdates", value, "0");
119 mDebugRegion = atoi(value);
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 property_get("debug.sf.showbackground", value, "0");
122 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700124 property_get("debug.sf.ddms", value, "0");
125 mDebugDDMS = atoi(value);
126 if (mDebugDDMS) {
127 DdmConnection::start(getServiceName());
128 }
129
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700130 LOGI_IF(mDebugRegion, "showupdates enabled");
131 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700132 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133}
134
135SurfaceFlinger::~SurfaceFlinger()
136{
137 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700140sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700142 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143}
144
Mathias Agopian770492c2010-05-28 14:22:23 -0700145sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700147 sp<ISurfaceComposerClient> bclient;
148 sp<Client> client(new Client(this));
149 status_t err = client->initCheck();
150 if (err == NO_ERROR) {
151 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 return bclient;
154}
155
Jamie Gennisf7acf162011-01-12 18:30:40 -0800156sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
157{
158 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
159 return gba;
160}
Mathias Agopian7623da42010-06-01 15:12:58 -0700161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
163{
164 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
165 const GraphicPlane& plane(mGraphicPlanes[dpy]);
166 return plane;
167}
168
169GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
170{
171 return const_cast<GraphicPlane&>(
172 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
173}
174
175void SurfaceFlinger::bootFinished()
176{
177 const nsecs_t now = systemTime();
178 const nsecs_t duration = now - mBootTime;
Andreas Hubere3c01832010-08-16 08:49:37 -0700179 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700180 mBootFinished = true;
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700181
182 // wait patiently for the window manager death
183 const String16 name("window");
184 sp<IBinder> window(defaultServiceManager()->getService(name));
185 if (window != 0) {
186 window->linkToDeath(this);
187 }
188
189 // stop boot animation
Mathias Agopian627e7b52009-05-21 19:21:59 -0700190 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191}
192
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700193void SurfaceFlinger::binderDied(const wp<IBinder>& who)
194{
195 // the window manager died on us. prepare its eulogy.
196
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700197 // reset screen orientation
198 setOrientation(0, eOrientationDefault, 0);
199
200 // restart the boot-animation
201 property_set("ctl.start", "bootanim");
202}
203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204void SurfaceFlinger::onFirstRef()
205{
206 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
207
208 // Wait for the main thread to be done with its initialization
209 mReadyToRunBarrier.wait();
210}
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212static inline uint16_t pack565(int r, int g, int b) {
213 return (r<<11)|(g<<5)|b;
214}
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216status_t SurfaceFlinger::readyToRun()
217{
218 LOGI( "SurfaceFlinger's main thread ready to run. "
219 "Initializing graphics H/W...");
220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 // we only support one display currently
222 int dpy = 0;
223
224 {
225 // initialize the main display
226 GraphicPlane& plane(graphicPlane(dpy));
227 DisplayHardware* const hw = new DisplayHardware(this, dpy);
228 plane.setDisplayHardware(hw);
229 }
230
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700231 // create the shared control-block
232 mServerHeap = new MemoryHeapBase(4096,
233 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
234 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700235
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700236 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
237 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700238
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700239 new(mServerCblk) surface_flinger_cblk_t;
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 // initialize primary screen
242 // (other display should be initialized in the same manner, but
243 // asynchronously, as they could come and go. None of this is supported
244 // yet).
245 const GraphicPlane& plane(graphicPlane(dpy));
246 const DisplayHardware& hw = plane.displayHardware();
247 const uint32_t w = hw.getWidth();
248 const uint32_t h = hw.getHeight();
249 const uint32_t f = hw.getFormat();
250 hw.makeCurrent();
251
252 // initialize the shared control block
253 mServerCblk->connected |= 1<<dpy;
254 display_cblk_t* dcblk = mServerCblk->displays + dpy;
255 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800256 dcblk->w = plane.getWidth();
257 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 dcblk->format = f;
259 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
260 dcblk->xdpi = hw.getDpiX();
261 dcblk->ydpi = hw.getDpiY();
262 dcblk->fps = hw.getRefreshRate();
263 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264
265 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700267 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 glEnableClientState(GL_VERTEX_ARRAY);
269 glEnable(GL_SCISSOR_TEST);
270 glShadeModel(GL_FLAT);
271 glDisable(GL_DITHER);
272 glDisable(GL_CULL_FACE);
273
274 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
275 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis830d0832011-10-07 14:51:16 -0700276 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 glGenTextures(1, &mWormholeTexName);
278 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
283 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis830d0832011-10-07 14:51:16 -0700284 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
285
286 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
287 glGenTextures(1, &mProtectedTexName);
288 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
293 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
294 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
296 glViewport(0, 0, w, h);
297 glMatrixMode(GL_PROJECTION);
298 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -0700299 // put the origin in the left-bottom corner
300 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 -0800301
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800302
303 // start the EventThread
304 mEventThread = new EventThread(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800310 mReadyToRunBarrier.open();
311
Mathias Agopian627e7b52009-05-21 19:21:59 -0700312 // start boot animation
313 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 return NO_ERROR;
316}
317
318// ----------------------------------------------------------------------------
319#if 0
320#pragma mark -
321#pragma mark Events Handler
322#endif
323
Mathias Agopian16aca512011-11-23 16:49:10 -0800324void SurfaceFlinger::waitForEvent() {
325 mEventQueue.waitMessage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326}
327
328void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700329 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330}
331
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800332status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
333 nsecs_t reltime, uint32_t flags) {
334 return mEventQueue.postMessage(msg, reltime);
335}
336
337status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
338 nsecs_t reltime, uint32_t flags) {
339 status_t res = mEventQueue.postMessage(msg, reltime);
340 if (res == NO_ERROR) {
341 msg->wait();
342 }
343 return res;
344}
345
346// ----------------------------------------------------------------------------
347
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700348bool SurfaceFlinger::authenticateSurfaceTexture(
349 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800350 Mutex::Autolock _l(mStateLock);
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700351 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800352
353 // Check the visible layer list for the ISurface
354 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
355 size_t count = currentLayers.size();
356 for (size_t i=0 ; i<count ; i++) {
357 const sp<LayerBase>& layer(currentLayers[i]);
358 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700359 if (lbc != NULL) {
360 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
361 if (lbcBinder == surfaceTextureBinder) {
362 return true;
363 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800364 }
365 }
366
367 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700368 // SurfaceTexture gets destroyed before all the clients are done using it,
369 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800370 // will instead fail later on when the client tries to use the surface,
371 // which should be reported as "surface XYZ returned an -ENODEV". The
372 // purgatorized layers are no less authentic than the visible ones, so this
373 // should not cause any harm.
374 size_t purgatorySize = mLayerPurgatory.size();
375 for (size_t i=0 ; i<purgatorySize ; i++) {
376 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
377 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700378 if (lbc != NULL) {
379 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
380 if (lbcBinder == surfaceTextureBinder) {
381 return true;
382 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800383 }
384 }
385
386 return false;
387}
388
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800389// ----------------------------------------------------------------------------
390
391sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian6779df22011-12-06 17:22:19 -0800392 sp<DisplayEventConnection> result(new DisplayEventConnection(mEventThread));
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800393 return result;
Mathias Agopian898c4c92010-05-18 17:06:55 -0700394}
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396// ----------------------------------------------------------------------------
397#if 0
398#pragma mark -
399#pragma mark Main loop
400#endif
401
402bool SurfaceFlinger::threadLoop()
403{
404 waitForEvent();
405
406 // check for transactions
407 if (UNLIKELY(mConsoleSignals)) {
408 handleConsoleEvents();
409 }
410
Mathias Agopian439863f2011-06-28 19:09:31 -0700411 // if we're in a global transaction, don't do anything.
412 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
413 uint32_t transactionFlags = peekTransactionFlags(mask);
414 if (UNLIKELY(transactionFlags)) {
415 handleTransaction(transactionFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417
418 // post surfaces (if needed)
419 handlePageFlip();
420
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700421 if (mDirtyRegion.isEmpty()) {
422 // nothing new to do.
423 return true;
424 }
425
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700426 if (UNLIKELY(mHwWorkListDirty)) {
427 // build the h/w work list
428 handleWorkList();
429 }
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Jamie Gennisde14eca2011-10-14 16:44:08 -0700432 if (LIKELY(hw.canDraw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700434
435 const int index = hw.getCurrentBufferIndex();
436 GraphicLog& logger(GraphicLog::getInstance());
437
438 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 handleRepaint();
440
Mathias Agopianb1a18742009-09-17 16:18:16 -0700441 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700442 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700443 hw.compositionComplete();
444
Mathias Agopian04262e92010-09-13 22:57:58 -0700445 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700446 postFramebuffer();
447
Mathias Agopian04262e92010-09-13 22:57:58 -0700448 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 } else {
450 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800451 hw.compositionComplete();
Mathias Agopian79f39eb2011-11-17 17:49:17 -0800452 hw.waitForVSync();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454 return true;
455}
456
457void SurfaceFlinger::postFramebuffer()
458{
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700459 // this should never happen. we do the flip anyways so we don't
460 // risk to cause a deadlock with hwc
461 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiane757a872011-10-16 18:46:35 -0700462 const DisplayHardware& hw(graphicPlane(0).displayHardware());
463 const nsecs_t now = systemTime();
464 mDebugInSwapBuffers = now;
465 hw.flip(mSwapRegion);
466 mLastSwapBufferTime = systemTime() - now;
467 mDebugInSwapBuffers = 0;
468 mSwapRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469}
470
471void SurfaceFlinger::handleConsoleEvents()
472{
473 // something to do with the console
474 const DisplayHardware& hw = graphicPlane(0).displayHardware();
475
476 int what = android_atomic_and(0, &mConsoleSignals);
477 if (what & eConsoleAcquired) {
478 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700479 // this is a temporary work-around, eventually this should be called
480 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700481 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700485 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 hw.releaseScreen();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
489
490 mDirtyRegion.set(hw.bounds());
491}
492
493void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
494{
Mathias Agopian69608112011-05-19 15:38:14 -0700495 Mutex::Autolock _l(mStateLock);
496 const nsecs_t now = systemTime();
497 mDebugInTransaction = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
Mathias Agopian69608112011-05-19 15:38:14 -0700499 // Here we're guaranteed that some transaction flags are set
500 // so we can call handleTransactionLocked() unconditionally.
501 // We call getTransactionFlags(), which will also clear the flags,
502 // with mStateLock held to guarantee that mCurrentState won't change
503 // until the transaction is committed.
Mathias Agopianff1d4102010-08-10 17:19:56 -0700504
Mathias Agopian69608112011-05-19 15:38:14 -0700505 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
506 transactionFlags = getTransactionFlags(mask);
507 handleTransactionLocked(transactionFlags);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700508
Mathias Agopian69608112011-05-19 15:38:14 -0700509 mLastTransactionTime = systemTime() - now;
510 mDebugInTransaction = 0;
511 invalidateHwcGeometry();
512 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700513}
514
Mathias Agopian69608112011-05-19 15:38:14 -0700515void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700516{
517 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 const size_t count = currentLayers.size();
519
520 /*
521 * Traversal of the children
522 * (perform the transaction for each of them if needed)
523 */
524
525 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
526 if (layersNeedTransaction) {
527 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700528 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
530 if (!trFlags) continue;
531
532 const uint32_t flags = layer->doTransaction(0);
533 if (flags & Layer::eVisibleRegion)
534 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536 }
537
538 /*
539 * Perform our own transaction if needed
540 */
541
542 if (transactionFlags & eTransactionNeeded) {
543 if (mCurrentState.orientation != mDrawingState.orientation) {
544 // the orientation has changed, recompute all visible regions
545 // and invalidate everything.
546
547 const int dpy = 0;
548 const int orientation = mCurrentState.orientation;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700549 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 GraphicPlane& plane(graphicPlane(dpy));
551 plane.setOrientation(orientation);
552
553 // update the shared control block
554 const DisplayHardware& hw(plane.displayHardware());
555 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
556 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800557 dcblk->w = plane.getWidth();
558 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
560 mVisibleRegionsDirty = true;
561 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700564 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
565 // layers have been added
566 mVisibleRegionsDirty = true;
567 }
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 // some layers might have been removed, so
570 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700571 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700572 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700574 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700575 const size_t count = previousLayers.size();
576 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700577 const sp<LayerBase>& layer(previousLayers[i]);
578 if (currentLayers.indexOf( layer ) < 0) {
579 // this layer is not visible anymore
Mathias Agopian33863dd2009-07-28 14:20:21 -0700580 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700581 }
582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585
586 commitTransaction();
587}
588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800590 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591{
592 const GraphicPlane& plane(graphicPlane(0));
593 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700594 const DisplayHardware& hw(plane.displayHardware());
595 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596
597 Region aboveOpaqueLayers;
598 Region aboveCoveredLayers;
599 Region dirty;
600
601 bool secureFrameBuffer = false;
602
603 size_t i = currentLayers.size();
604 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700605 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 layer->validateVisibility(planeTransform);
607
608 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700609 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700611 /*
612 * opaqueRegion: area of a surface that is fully opaque.
613 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700615
616 /*
617 * visibleRegion: area of a surface that is visible on screen
618 * and not fully transparent. This is essentially the layer's
619 * footprint minus the opaque regions above it.
620 * Areas covered by a translucent surface are considered visible.
621 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700623
624 /*
625 * coveredRegion: area of a surface that is covered by all
626 * visible regions above it (which includes the translucent areas).
627 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700629
630
631 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700632 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700633 const bool translucent = !layer->isOpaque();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700634 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700636 visibleRegion.andSelf(screenRegion);
637 if (!visibleRegion.isEmpty()) {
638 // Remove the transparent area from the visible region
639 if (translucent) {
640 visibleRegion.subtractSelf(layer->transparentRegionScreen);
641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700643 // compute the opaque region
644 const int32_t layerOrientation = layer->getOrientation();
645 if (s.alpha==255 && !translucent &&
646 ((layerOrientation & Transform::ROT_INVALID) == false)) {
647 // the opaque region is the layer's footprint
648 opaqueRegion = visibleRegion;
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651 }
652
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700653 // Clip the covered region to the visible region
654 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
655
656 // Update aboveCoveredLayers for next (lower) layer
657 aboveCoveredLayers.orSelf(visibleRegion);
658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 // subtract the opaque region covered by the layers above us
660 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
662 // compute this layer's dirty region
663 if (layer->contentDirty) {
664 // we need to invalidate the whole region
665 dirty = visibleRegion;
666 // as well, as the old visible region
667 dirty.orSelf(layer->visibleRegionScreen);
668 layer->contentDirty = false;
669 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700670 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700671 * the exposed region consists of two components:
672 * 1) what's VISIBLE now and was COVERED before
673 * 2) what's EXPOSED now less what was EXPOSED before
674 *
675 * note that (1) is conservative, we start with the whole
676 * visible region but only keep what used to be covered by
677 * something -- which mean it may have been exposed.
678 *
679 * (2) handles areas that were not covered by anything but got
680 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700681 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700682 const Region newExposed = visibleRegion - coveredRegion;
683 const Region oldVisibleRegion = layer->visibleRegionScreen;
684 const Region oldCoveredRegion = layer->coveredRegionScreen;
685 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
686 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688 dirty.subtractSelf(aboveOpaqueLayers);
689
690 // accumulate to the screen dirty region
691 dirtyRegion.orSelf(dirty);
692
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700693 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 // Store the visible region is screen space
697 layer->setVisibleRegion(visibleRegion);
698 layer->setCoveredRegion(coveredRegion);
699
Mathias Agopian12cedff2009-07-28 10:57:27 -0700700 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (layer->isSecure() && !visibleRegion.isEmpty()) {
702 secureFrameBuffer = true;
703 }
704 }
705
Mathias Agopian12cedff2009-07-28 10:57:27 -0700706 // invalidate the areas where a layer was removed
707 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
708 mDirtyRegionRemovedLayer.clear();
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 mSecureFrameBuffer = secureFrameBuffer;
711 opaqueRegion = aboveOpaqueLayers;
712}
713
714
715void SurfaceFlinger::commitTransaction()
716{
Jesse Hall8a7c9402011-12-02 10:00:00 -0800717 if (!mLayersPendingRemoval.isEmpty()) {
718 // Notify removed layers now that they can't be drawn from
719 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
720 mLayersPendingRemoval[i]->onRemoved();
721 }
722 mLayersPendingRemoval.clear();
723 }
724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 mDrawingState = mCurrentState;
Jamie Gennis122aa6b2011-10-12 17:39:00 -0700726 mTransationPending = false;
Mathias Agopian9779b222009-09-07 16:32:45 -0700727 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728}
729
730void SurfaceFlinger::handlePageFlip()
731{
732 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800733 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 visibleRegions |= lockPageFlip(currentLayers);
735
736 const DisplayHardware& hw = graphicPlane(0).displayHardware();
737 const Region screenRegion(hw.bounds());
738 if (visibleRegions) {
739 Region opaqueRegion;
740 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700741
742 /*
743 * rebuild the visible layer list
744 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800745 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700746 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700747 mVisibleLayersSortedByZ.setCapacity(count);
748 for (size_t i=0 ; i<count ; i++) {
749 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
750 mVisibleLayersSortedByZ.add(currentLayers[i]);
751 }
752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 mWormholeRegion = screenRegion.subtract(opaqueRegion);
754 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800755 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 unlockPageFlip(currentLayers);
Mathias Agopian6497eab2011-10-21 15:18:28 -0700759
760 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 mDirtyRegion.andSelf(screenRegion);
762}
763
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800764void SurfaceFlinger::invalidateHwcGeometry()
765{
766 mHwWorkListDirty = true;
767}
768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
770{
771 bool recomputeVisibleRegions = false;
772 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700773 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700775 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 layer->lockPageFlip(recomputeVisibleRegions);
777 }
778 return recomputeVisibleRegions;
779}
780
781void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
782{
783 const GraphicPlane& plane(graphicPlane(0));
784 const Transform& planeTransform(plane.transform());
785 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700786 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700788 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 layer->unlockPageFlip(planeTransform, mDirtyRegion);
790 }
791}
792
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700793void SurfaceFlinger::handleWorkList()
794{
795 mHwWorkListDirty = false;
796 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
797 if (hwc.initCheck() == NO_ERROR) {
798 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
799 const size_t count = currentLayers.size();
800 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700801 hwc_layer_t* const cur(hwc.getLayers());
802 for (size_t i=0 ; cur && i<count ; i++) {
803 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700804 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian6a969242010-09-22 18:58:01 -0700805 cur[i].compositionType = HWC_FRAMEBUFFER;
806 cur[i].flags |= HWC_SKIP_LAYER;
807 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700808 }
809 }
810}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812void SurfaceFlinger::handleRepaint()
813{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700814 // compute the invalid region
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700815 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816
817 if (UNLIKELY(mDebugRegion)) {
818 debugFlashRegions();
819 }
820
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700821 // set the frame buffer
822 const DisplayHardware& hw(graphicPlane(0).displayHardware());
823 glMatrixMode(GL_MODELVIEW);
824 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825
826 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700827 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
828 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700829 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700830 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
831 // takes a rectangle, we must make sure to update that whole
832 // rectangle in that case
833 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700834 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700835 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700836 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700838 // in the BUFFER_PRESERVED case, obviously, we can update only
839 // what's needed and nothing more.
840 // NOTE: this is NOT a common case, as preserving the backbuffer
841 // is costly and usually involves copying the whole update back.
842 }
843 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700844 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700845 // We need to redraw the rectangle that will be updated
846 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700847 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700848 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700849 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700850 } else {
851 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 mDirtyRegion.set(hw.bounds());
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700853 mSwapRegion = mDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855 }
856
Mathias Agopian88cde072011-09-20 17:21:56 -0700857 setupHardwareComposer(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 composeSurfaces(mDirtyRegion);
859
Mathias Agopian88cde072011-09-20 17:21:56 -0700860 // update the swap region and clear the dirty region
861 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 mDirtyRegion.clear();
863}
864
Mathias Agopian88cde072011-09-20 17:21:56 -0700865void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866{
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700867 const DisplayHardware& hw(graphicPlane(0).displayHardware());
868 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700869 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700870 if (!cur) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700871 return;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700872 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700873
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700874 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
875 size_t count = layers.size();
876
877 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700878 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
879 hwc.getNumLayers(), count);
880
881 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700882 if (hwc.initCheck() == NO_ERROR) {
883 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
884 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700885
886 /*
887 * update the per-frame h/w composer data for each layer
888 * and build the transparent region of the FB
889 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700890 for (size_t i=0 ; i<count ; i++) {
891 const sp<LayerBase>& layer(layers[i]);
892 layer->setPerFrameData(&cur[i]);
893 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700894 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700895 status_t err = hwc.prepare();
896 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700897
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700898 if (err == NO_ERROR) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700899 // what's happening here is tricky.
900 // we want to clear all the layers with the CLEAR_FB flags
901 // that are opaque.
902 // however, since some GPU are efficient at preserving
903 // the backbuffer, we want to take advantage of that so we do the
904 // clear only in the dirty region (other areas will be preserved
905 // on those GPUs).
906 // NOTE: on non backbuffer preserving GPU, the dirty region
907 // has already been expanded as needed, so the code is correct
908 // there too.
909 //
910 // However, the content of the framebuffer cannot be trusted when
911 // we switch to/from FB/OVERLAY, in which case we need to
912 // expand the dirty region to those areas too.
913 //
914 // Note also that there is a special case when switching from
915 // "no layers in FB" to "some layers in FB", where we need to redraw
916 // the entire FB, since some areas might contain uninitialized
917 // data.
918 //
919 // Also we want to make sure to not clear areas that belong to
Mathias Agopian0d0fba42011-10-18 17:36:28 -0700920 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian88cde072011-09-20 17:21:56 -0700921 // that is, we can't erase anything outside the dirty region.
922
Mathias Agopian0a5abdb2011-09-09 01:47:48 -0700923 Region transparent;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700924
Mathias Agopian88cde072011-09-20 17:21:56 -0700925 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
926 transparent.set(hw.getBounds());
927 dirtyInOut = transparent;
928 } else {
929 for (size_t i=0 ; i<count ; i++) {
930 const sp<LayerBase>& layer(layers[i]);
931 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
932 transparent.orSelf(layer->visibleRegionScreen);
933 }
934 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
935 if (isOverlay != layer->isOverlay()) {
936 // we transitioned to/from overlay, so add this layer
937 // to the dirty region so the framebuffer can be either
938 // cleared or redrawn.
939 dirtyInOut.orSelf(layer->visibleRegionScreen);
940 }
941 layer->setOverlay(isOverlay);
Mathias Agopian45491692011-01-19 15:24:23 -0800942 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700943 // don't erase stuff outside the dirty region
944 transparent.andSelf(dirtyInOut);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700945 }
946
947 /*
948 * clear the area of the FB that need to be transparent
949 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700950 if (!transparent.isEmpty()) {
951 glClearColor(0,0,0,0);
952 Region::const_iterator it = transparent.begin();
953 Region::const_iterator const end = transparent.end();
954 const int32_t height = hw.getHeight();
955 while (it != end) {
956 const Rect& r(*it++);
957 const GLint sy = height - (r.top + r.height());
958 glScissor(r.left, sy, r.width(), r.height());
959 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian45491692011-01-19 15:24:23 -0800960 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700961 }
962 }
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700963}
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700964
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700965void SurfaceFlinger::composeSurfaces(const Region& dirty)
966{
Mathias Agopian9fe96542011-09-22 20:57:04 -0700967 const DisplayHardware& hw(graphicPlane(0).displayHardware());
968 HWComposer& hwc(hw.getHwComposer());
969
970 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
971 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700972 // should never happen unless the window manager has a bug
973 // draw something...
974 drawWormhole();
975 }
976
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700977 /*
978 * and then, render the layers targeted at the framebuffer
979 */
Mathias Agopian9fe96542011-09-22 20:57:04 -0700980 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700981 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
982 size_t count = layers.size();
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700983 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700984 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700985 continue;
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700986 }
987 const sp<LayerBase>& layer(layers[i]);
988 const Region clip(dirty.intersect(layer->visibleRegionScreen));
989 if (!clip.isEmpty()) {
990 layer->draw(clip);
991 }
992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993}
994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995void SurfaceFlinger::debugFlashRegions()
996{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700997 const DisplayHardware& hw(graphicPlane(0).displayHardware());
998 const uint32_t flags = hw.getFlags();
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700999 const int32_t height = hw.getHeight();
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001000 if (mSwapRegion.isEmpty()) {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001001 return;
1002 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -07001003
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001004 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1005 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1006 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1007 mDirtyRegion.bounds() : hw.bounds());
1008 composeSurfaces(repaint);
1009 }
1010
Mathias Agopian9044ef02011-10-18 14:49:27 -07001011 glDisable(GL_TEXTURE_EXTERNAL_OES);
1012 glDisable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 glDisable(GL_SCISSOR_TEST);
1015
Mathias Agopiandff8e582009-05-04 14:17:04 -07001016 static int toggle = 0;
1017 toggle = 1 - toggle;
1018 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001019 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001020 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001021 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001024 Region::const_iterator it = mDirtyRegion.begin();
1025 Region::const_iterator const end = mDirtyRegion.end();
1026 while (it != end) {
1027 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 GLfloat vertices[][2] = {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001029 { r.left, height - r.top },
1030 { r.left, height - r.bottom },
1031 { r.right, height - r.bottom },
1032 { r.right, height - r.top }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 };
1034 glVertexPointer(2, GL_FLOAT, 0, vertices);
1035 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1036 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001037
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001038 hw.flip(mSwapRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039
1040 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001041 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042
1043 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044}
1045
1046void SurfaceFlinger::drawWormhole() const
1047{
1048 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1049 if (region.isEmpty())
1050 return;
1051
1052 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1053 const int32_t width = hw.getWidth();
1054 const int32_t height = hw.getHeight();
1055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001057 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001058 Region::const_iterator it = region.begin();
1059 Region::const_iterator const end = region.end();
1060 while (it != end) {
1061 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 const GLint sy = height - (r.top + r.height());
1063 glScissor(r.left, sy, r.width(), r.height());
1064 glClear(GL_COLOR_BUFFER_BIT);
1065 }
1066 } else {
1067 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1068 { width, height }, { 0, height } };
1069 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopian9044ef02011-10-18 14:49:27 -07001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 glVertexPointer(2, GL_SHORT, 0, vertices);
1072 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1073 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian9044ef02011-10-18 14:49:27 -07001074
1075 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 glEnable(GL_TEXTURE_2D);
1077 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1078 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1079 glMatrixMode(GL_TEXTURE);
1080 glLoadIdentity();
Mathias Agopian9044ef02011-10-18 14:49:27 -07001081
1082 glDisable(GL_BLEND);
1083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001085 Region::const_iterator it = region.begin();
1086 Region::const_iterator const end = region.end();
1087 while (it != end) {
1088 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 const GLint sy = height - (r.top + r.height());
1090 glScissor(r.left, sy, r.width(), r.height());
1091 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1092 }
1093 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001094 glDisable(GL_TEXTURE_2D);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001095 glLoadIdentity();
1096 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098}
1099
1100void SurfaceFlinger::debugShowFPS() const
1101{
1102 static int mFrameCount;
1103 static int mLastFrameCount = 0;
1104 static nsecs_t mLastFpsTime = 0;
1105 static float mFps = 0;
1106 mFrameCount++;
1107 nsecs_t now = systemTime();
1108 nsecs_t diff = now - mLastFpsTime;
1109 if (diff > ms2ns(250)) {
1110 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1111 mLastFpsTime = now;
1112 mLastFrameCount = mFrameCount;
1113 }
1114 // XXX: mFPS has the value we want
1115 }
1116
Mathias Agopian1473f462009-04-10 14:24:30 -07001117status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118{
1119 Mutex::Autolock _l(mStateLock);
1120 addLayer_l(layer);
1121 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1122 return NO_ERROR;
1123}
1124
Mathias Agopian593c05c2010-06-02 23:28:45 -07001125status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1126{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001127 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001128 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1129}
1130
1131ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1132 const sp<LayerBaseClient>& lbc)
1133{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001134 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001135 size_t name = client->attachLayer(lbc);
1136
1137 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001138
1139 // add this layer to the current state list
1140 addLayer_l(lbc);
1141
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001142 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001143}
1144
Mathias Agopian1473f462009-04-10 14:24:30 -07001145status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146{
1147 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001148 status_t err = purgatorizeLayer_l(layer);
1149 if (err == NO_ERROR)
1150 setTransactionFlags(eTransactionNeeded);
1151 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152}
1153
Mathias Agopian1473f462009-04-10 14:24:30 -07001154status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155{
Mathias Agopian7623da42010-06-01 15:12:58 -07001156 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1157 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001158 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1161 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001162 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 return NO_ERROR;
1164 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001165 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166}
1167
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001168status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1169{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001170 // First add the layer to the purgatory list, which makes sure it won't
1171 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001172 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001173 if (err >= 0) {
1174 mLayerPurgatory.add(layerBase);
1175 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001176
Jesse Hall8a7c9402011-12-02 10:00:00 -08001177 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001178
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001179 // it's possible that we don't find a layer, because it might
1180 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001181 // from the user because there is a race between Client::destroySurface(),
1182 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001183 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1184}
1185
Mathias Agopian593c05c2010-06-02 23:28:45 -07001186status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001188 layer->forceVisibilityTransaction();
1189 setTransactionFlags(eTraversalNeeded);
1190 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191}
1192
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001193uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1194{
1195 return android_atomic_release_load(&mTransactionFlags);
1196}
1197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1199{
1200 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1201}
1202
Mathias Agopian898c4c92010-05-18 17:06:55 -07001203uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204{
1205 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1206 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001207 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
1209 return old;
1210}
1211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212
Jamie Gennise2909e12011-10-10 15:48:06 -07001213void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001214 int orientation, uint32_t flags) {
Mathias Agopian439863f2011-06-28 19:09:31 -07001215 Mutex::Autolock _l(mStateLock);
Mathias Agopian9779b222009-09-07 16:32:45 -07001216
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001217 uint32_t transactionFlags = 0;
Jamie Gennise2909e12011-10-10 15:48:06 -07001218 if (mCurrentState.orientation != orientation) {
1219 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1220 mCurrentState.orientation = orientation;
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001221 transactionFlags |= eTransactionNeeded;
Jamie Gennise2909e12011-10-10 15:48:06 -07001222 } else if (orientation != eOrientationUnchanged) {
1223 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1224 orientation);
1225 }
1226 }
1227
Mathias Agopian439863f2011-06-28 19:09:31 -07001228 const size_t count = state.size();
1229 for (size_t i=0 ; i<count ; i++) {
1230 const ComposerState& s(state[i]);
1231 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis122aa6b2011-10-12 17:39:00 -07001232 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian439863f2011-06-28 19:09:31 -07001233 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001234
Mathias Agopiana06c06b2011-11-07 21:58:03 -08001235 if (transactionFlags) {
1236 // this triggers the transaction
1237 setTransactionFlags(transactionFlags);
1238
1239 // if this is a synchronous transaction, wait for it to take effect
1240 // before returning.
1241 if (flags & eSynchronous) {
1242 mTransationPending = true;
1243 }
1244 while (mTransationPending) {
1245 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1246 if (CC_UNLIKELY(err != NO_ERROR)) {
1247 // just in case something goes wrong in SF, return to the
1248 // called after a few seconds.
1249 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1250 mTransationPending = false;
1251 break;
1252 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255}
1256
Andreas Hubere3c01832010-08-16 08:49:37 -07001257int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001258 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259{
1260 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1261 return BAD_VALUE;
1262
1263 Mutex::Autolock _l(mStateLock);
1264 if (mCurrentState.orientation != orientation) {
1265 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown01a98dd2011-09-20 15:08:29 -07001266 mCurrentState.orientationFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mCurrentState.orientation = orientation;
1268 setTransactionFlags(eTransactionNeeded);
1269 mTransactionCV.wait(mStateLock);
1270 } else {
1271 orientation = BAD_VALUE;
1272 }
1273 }
1274 return orientation;
1275}
1276
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001277sp<ISurface> SurfaceFlinger::createSurface(
1278 ISurfaceComposerClient::surface_data_t* params,
1279 const String8& name,
1280 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1282 uint32_t flags)
1283{
Mathias Agopian1473f462009-04-10 14:24:30 -07001284 sp<LayerBaseClient> layer;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001285 sp<ISurface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001286
1287 if (int32_t(w|h) < 0) {
1288 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1289 int(w), int(h));
1290 return surfaceHandle;
1291 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001294 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 switch (flags & eFXSurfaceMask) {
1296 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001297 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1298 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
1300 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001301 // for now we treat Blur as Dim, until we can implement it
1302 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001304 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 break;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001306 case eFXSurfaceScreenshot:
1307 layer = createScreenshotSurface(client, d, w, h, flags);
1308 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310
Mathias Agopian1473f462009-04-10 14:24:30 -07001311 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001312 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001313 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001314 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001317 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001318 params->token = token;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001319 params->identity = layer->getIdentity();
Mathias Agopian7623da42010-06-01 15:12:58 -07001320 if (normalLayer != 0) {
1321 Mutex::Autolock _l(mStateLock);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001322 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001323 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001324 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001325
Mathias Agopian593c05c2010-06-02 23:28:45 -07001326 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328
1329 return surfaceHandle;
1330}
1331
Mathias Agopian7623da42010-06-01 15:12:58 -07001332sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001333 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001334 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001335 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336{
1337 // initialize the surfaces
1338 switch (format) { // TODO: take h/w into account
1339 case PIXEL_FORMAT_TRANSPARENT:
1340 case PIXEL_FORMAT_TRANSLUCENT:
1341 format = PIXEL_FORMAT_RGBA_8888;
1342 break;
1343 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001344#ifdef NO_RGBX_8888
1345 format = PIXEL_FORMAT_RGB_565;
1346#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001347 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001348#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 break;
1350 }
1351
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001352#ifdef NO_RGBX_8888
1353 if (format == PIXEL_FORMAT_RGBX_8888)
1354 format = PIXEL_FORMAT_RGBA_8888;
1355#endif
1356
Mathias Agopian593c05c2010-06-02 23:28:45 -07001357 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001358 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001359 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001361 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363 return layer;
1364}
1365
Mathias Agopian7623da42010-06-01 15:12:58 -07001366sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001367 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001368 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001370 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001371 return layer;
1372}
1373
1374sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1375 const sp<Client>& client, DisplayID display,
1376 uint32_t w, uint32_t h, uint32_t flags)
1377{
1378 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 return layer;
1380}
1381
Mathias Agopian593c05c2010-06-02 23:28:45 -07001382status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001384 /*
1385 * called by the window manager, when a surface should be marked for
1386 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001387 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001388 * The surface is removed from the current and drawing lists, but placed
1389 * in the purgatory queue, so it's not destroyed right-away (we need
1390 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001391 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001392
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001393 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001394 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001395 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001396 if (layer != 0) {
1397 err = purgatorizeLayer_l(layer);
1398 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001399 setTransactionFlags(eTransactionNeeded);
1400 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001401 }
1402 return err;
1403}
1404
Mathias Agopian69608112011-05-19 15:38:14 -07001405status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001406{
Mathias Agopian359140c2009-07-02 17:33:40 -07001407 // called by ~ISurface() when all references are gone
Mathias Agopian69608112011-05-19 15:38:14 -07001408 status_t err = NO_ERROR;
1409 sp<LayerBaseClient> l(layer.promote());
1410 if (l != NULL) {
1411 Mutex::Autolock _l(mStateLock);
1412 err = removeLayer_l(l);
1413 if (err == NAME_NOT_FOUND) {
1414 // The surface wasn't in the current list, which means it was
1415 // removed already, which means it is in the purgatory,
1416 // and need to be removed from there.
1417 ssize_t idx = mLayerPurgatory.remove(l);
1418 LOGE_IF(idx < 0,
1419 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001420 }
Mathias Agopian69608112011-05-19 15:38:14 -07001421 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1422 "error removing layer=%p (%s)", l.get(), strerror(-err));
1423 }
1424 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425}
1426
Mathias Agopian439863f2011-06-28 19:09:31 -07001427uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001428 const sp<Client>& client,
Mathias Agopian439863f2011-06-28 19:09:31 -07001429 const layer_state_t& s)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 uint32_t flags = 0;
Mathias Agopian439863f2011-06-28 19:09:31 -07001432 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1433 if (layer != 0) {
1434 const uint32_t what = s.what;
1435 if (what & ePositionChanged) {
1436 if (layer->setPosition(s.x, s.y))
1437 flags |= eTraversalNeeded;
1438 }
1439 if (what & eLayerChanged) {
1440 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1441 if (layer->setLayer(s.z)) {
1442 mCurrentState.layersSortedByZ.removeAt(idx);
1443 mCurrentState.layersSortedByZ.add(layer);
1444 // we need traversal (state changed)
1445 // AND transaction (list changed)
1446 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001449 if (what & eSizeChanged) {
1450 if (layer->setSize(s.w, s.h)) {
1451 flags |= eTraversalNeeded;
Mathias Agopian439863f2011-06-28 19:09:31 -07001452 }
1453 }
1454 if (what & eAlphaChanged) {
1455 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1456 flags |= eTraversalNeeded;
1457 }
1458 if (what & eMatrixChanged) {
1459 if (layer->setMatrix(s.matrix))
1460 flags |= eTraversalNeeded;
1461 }
1462 if (what & eTransparentRegionChanged) {
1463 if (layer->setTransparentRegionHint(s.transparentRegion))
1464 flags |= eTraversalNeeded;
1465 }
1466 if (what & eVisibilityChanged) {
1467 if (layer->setFlags(s.flags, s.mask))
1468 flags |= eTraversalNeeded;
1469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001471 return flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472}
1473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474void SurfaceFlinger::screenReleased(int dpy)
1475{
1476 // this may be called by a signal handler, we can't do too much in here
1477 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1478 signalEvent();
1479}
1480
1481void SurfaceFlinger::screenAcquired(int dpy)
1482{
1483 // this may be called by a signal handler, we can't do too much in here
1484 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1485 signalEvent();
1486}
1487
1488status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1489{
Erik Gilling94720d72010-12-01 16:38:01 -08001490 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 char buffer[SIZE];
1492 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001493
1494 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 snprintf(buffer, SIZE, "Permission Denial: "
1496 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1497 IPCThreadState::self()->getCallingPid(),
1498 IPCThreadState::self()->getCallingUid());
1499 result.append(buffer);
1500 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001501
1502 // figure out if we're stuck somewhere
1503 const nsecs_t now = systemTime();
1504 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1505 const nsecs_t inTransaction(mDebugInTransaction);
1506 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1507 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1508
1509 // Try to get the main lock, but don't insist if we can't
1510 // (this would indicate SF is stuck, but we want to be able to
1511 // print something in dumpsys).
1512 int retry = 3;
1513 while (mStateLock.tryLock()<0 && --retry>=0) {
1514 usleep(1000000);
1515 }
1516 const bool locked(retry >= 0);
1517 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001518 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001519 "SurfaceFlinger appears to be unresponsive, "
1520 "dumping anyways (no locks held)\n");
1521 result.append(buffer);
1522 }
1523
Mathias Agopian06a61e22011-01-19 16:15:53 -08001524 /*
1525 * Dump the visible layer list
1526 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1528 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001529 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1530 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001532 const sp<LayerBase>& layer(currentLayers[i]);
1533 layer->dump(result, buffer, SIZE);
1534 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 s.transparentRegion.dump(result, "transparentRegion");
1536 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1537 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1538 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001539
Mathias Agopian06a61e22011-01-19 16:15:53 -08001540 /*
1541 * Dump the layers in the purgatory
1542 */
1543
Mathias Agopiancadd3252011-11-29 17:55:46 -08001544 const size_t purgatorySize = mLayerPurgatory.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001545 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1546 result.append(buffer);
1547 for (size_t i=0 ; i<purgatorySize ; i++) {
1548 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1549 layer->shortDump(result, buffer, SIZE);
1550 }
1551
1552 /*
1553 * Dump SurfaceFlinger global state
1554 */
1555
Mathias Agopianab951172011-07-14 18:01:49 -07001556 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian06a61e22011-01-19 16:15:53 -08001557 result.append(buffer);
Mathias Agopianab951172011-07-14 18:01:49 -07001558
1559 const GLExtensions& extensions(GLExtensions::getInstance());
1560 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1561 extensions.getVendor(),
1562 extensions.getRenderer(),
1563 extensions.getVersion());
1564 result.append(buffer);
Mathias Agopiancadd3252011-11-29 17:55:46 -08001565
1566 snprintf(buffer, SIZE, "EGL : %s\n",
1567 eglQueryString(graphicPlane(0).getEGLDisplay(),
1568 EGL_VERSION_HW_ANDROID));
1569 result.append(buffer);
1570
Mathias Agopianab951172011-07-14 18:01:49 -07001571 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1572 result.append(buffer);
1573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 mWormholeRegion.dump(result, "WormholeRegion");
1575 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1576 snprintf(buffer, SIZE,
Jamie Gennisde14eca2011-10-14 16:44:08 -07001577 " orientation=%d, canDraw=%d\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 mCurrentState.orientation, hw.canDraw());
1579 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001580 snprintf(buffer, SIZE,
1581 " last eglSwapBuffers() time: %f us\n"
Mathias Agopianabf88be2011-10-20 17:22:38 -07001582 " last transaction time : %f us\n"
1583 " refresh-rate : %f fps\n"
1584 " x-dpi : %f\n"
1585 " y-dpi : %f\n",
1586 mLastSwapBufferTime/1000.0,
1587 mLastTransactionTime/1000.0,
1588 hw.getRefreshRate(),
1589 hw.getDpiX(),
1590 hw.getDpiY());
Mathias Agopiana8d49172009-08-26 16:36:26 -07001591 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001592
Mathias Agopiana8d49172009-08-26 16:36:26 -07001593 if (inSwapBuffersDuration || !locked) {
1594 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1595 inSwapBuffersDuration/1000.0);
1596 result.append(buffer);
1597 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001598
Mathias Agopiana8d49172009-08-26 16:36:26 -07001599 if (inTransactionDuration || !locked) {
1600 snprintf(buffer, SIZE, " transaction time: %f us\n",
1601 inTransactionDuration/1000.0);
1602 result.append(buffer);
1603 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001604
Mathias Agopian06a61e22011-01-19 16:15:53 -08001605 /*
Mathias Agopian79f39eb2011-11-17 17:49:17 -08001606 * VSYNC state
1607 */
1608 mEventThread->dump(result, buffer, SIZE);
1609
1610 /*
Mathias Agopian06a61e22011-01-19 16:15:53 -08001611 * Dump HWComposer state
1612 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001613 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian79f39eb2011-11-17 17:49:17 -08001614 snprintf(buffer, SIZE, "h/w composer state:\n");
1615 result.append(buffer);
Mathias Agopian6a969242010-09-22 18:58:01 -07001616 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1617 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001618 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian6a969242010-09-22 18:58:01 -07001619 result.append(buffer);
Mathias Agopian5bd1b272011-09-09 00:49:11 -07001620 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian6a969242010-09-22 18:58:01 -07001621
Mathias Agopian06a61e22011-01-19 16:15:53 -08001622 /*
1623 * Dump gralloc state
1624 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001625 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001626 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001627 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001628
1629 if (locked) {
1630 mStateLock.unlock();
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 write(fd, result.string(), result.size());
1634 return NO_ERROR;
1635}
1636
1637status_t SurfaceFlinger::onTransact(
1638 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1639{
1640 switch (code) {
1641 case CREATE_CONNECTION:
Mathias Agopian439863f2011-06-28 19:09:31 -07001642 case SET_TRANSACTION_STATE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 case SET_ORIENTATION:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001645 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001646 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 {
1648 // codes that require permission check
1649 IPCThreadState* ipc = IPCThreadState::self();
1650 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001651 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001652 if ((uid != AID_GRAPHICS) &&
1653 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001654 LOGE("Permission Denial: "
1655 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1656 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001658 break;
1659 }
1660 case CAPTURE_SCREEN:
1661 {
1662 // codes that require permission check
1663 IPCThreadState* ipc = IPCThreadState::self();
1664 const int pid = ipc->getCallingPid();
1665 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001666 if ((uid != AID_GRAPHICS) &&
1667 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001668 LOGE("Permission Denial: "
1669 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1670 return PERMISSION_DENIED;
1671 }
1672 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1677 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001678 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001679 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001680 IPCThreadState* ipc = IPCThreadState::self();
1681 const int pid = ipc->getCallingPid();
1682 const int uid = ipc->getCallingUid();
1683 LOGE("Permission Denial: "
1684 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 return PERMISSION_DENIED;
1686 }
1687 int n;
1688 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001689 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001690 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 return NO_ERROR;
1692 case 1002: // SHOW_UPDATES
1693 n = data.readInt32();
1694 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001695 invalidateHwcGeometry();
1696 repaintEverything();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 return NO_ERROR;
1698 case 1003: // SHOW_BACKGROUND
1699 n = data.readInt32();
1700 mDebugBackground = n ? 1 : 0;
1701 return NO_ERROR;
1702 case 1004:{ // repaint everything
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001703 repaintEverything();
Mathias Agopian9779b222009-09-07 16:32:45 -07001704 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001706 case 1005:{ // force transaction
1707 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1708 return NO_ERROR;
1709 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001710 case 1006:{ // enable/disable GraphicLog
1711 int enabled = data.readInt32();
1712 GraphicLog::getInstance().setEnabled(enabled);
1713 return NO_ERROR;
1714 }
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001715 case 1008: // toggle use of hw composer
1716 n = data.readInt32();
1717 mDebugDisableHWC = n ? 1 : 0;
1718 invalidateHwcGeometry();
1719 repaintEverything();
1720 return NO_ERROR;
Mathias Agopian2143fe02011-08-23 18:03:18 -07001721 case 1009: // toggle use of transform hint
1722 n = data.readInt32();
1723 mDebugDisableTransformHint = n ? 1 : 0;
1724 invalidateHwcGeometry();
1725 repaintEverything();
1726 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001728 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 reply->writeInt32(0);
1730 reply->writeInt32(mDebugRegion);
1731 reply->writeInt32(mDebugBackground);
1732 return NO_ERROR;
1733 case 1013: {
1734 Mutex::Autolock _l(mStateLock);
1735 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1736 reply->writeInt32(hw.getPageFlipCount());
1737 }
1738 return NO_ERROR;
1739 }
1740 }
1741 return err;
1742}
1743
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001744void SurfaceFlinger::repaintEverything() {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001745 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian6497eab2011-10-21 15:18:28 -07001746 const Rect bounds(hw.getBounds());
1747 setInvalidateRegion(Region(bounds));
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001748 signalEvent();
1749}
1750
Mathias Agopian6497eab2011-10-21 15:18:28 -07001751void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1752 Mutex::Autolock _l(mInvalidateLock);
1753 mInvalidateRegion = reg;
1754}
1755
1756Region SurfaceFlinger::getAndClearInvalidateRegion() {
1757 Mutex::Autolock _l(mInvalidateLock);
1758 Region reg(mInvalidateRegion);
1759 mInvalidateRegion.clear();
1760 return reg;
1761}
1762
Mathias Agopianaab758e2010-10-11 12:37:43 -07001763// ---------------------------------------------------------------------------
1764
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001765status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1766 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1767{
1768 Mutex::Autolock _l(mStateLock);
1769 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1770}
1771
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001772status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1773 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001774{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001775 if (!GLExtensions::getInstance().haveFramebufferObject())
1776 return INVALID_OPERATION;
1777
1778 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001779 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001780 const uint32_t hw_w = hw.getWidth();
1781 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001782 GLfloat u = 1;
1783 GLfloat v = 1;
1784
1785 // make sure to clear all GL error flags
1786 while ( glGetError() != GL_NO_ERROR ) ;
1787
1788 // create a FBO
1789 GLuint name, tname;
1790 glGenTextures(1, &tname);
1791 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001792 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1793 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001794 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001795 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001796 GLint tw = (2 << (31 - clz(hw_w)));
1797 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001798 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1799 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001800 u = GLfloat(hw_w) / tw;
1801 v = GLfloat(hw_h) / th;
1802 }
1803 glGenFramebuffersOES(1, &name);
1804 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001805 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1806 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001807
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001808 // redraw the screen entirely...
Mathias Agopian9044ef02011-10-18 14:49:27 -07001809 glDisable(GL_TEXTURE_EXTERNAL_OES);
1810 glDisable(GL_TEXTURE_2D);
Mathias Agopian01d650e2011-10-26 15:11:59 -07001811 glDisable(GL_SCISSOR_TEST);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001812 glClearColor(0,0,0,1);
1813 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian01d650e2011-10-26 15:11:59 -07001814 glEnable(GL_SCISSOR_TEST);
Mathias Agopianb946a562011-10-10 19:02:07 -07001815 glMatrixMode(GL_MODELVIEW);
1816 glLoadIdentity();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001817 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1818 const size_t count = layers.size();
1819 for (size_t i=0 ; i<count ; ++i) {
1820 const sp<LayerBase>& layer(layers[i]);
1821 layer->drawForSreenShot();
1822 }
1823
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001824 hw.compositionComplete();
1825
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001826 // back to main framebuffer
1827 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1828 glDisable(GL_SCISSOR_TEST);
1829 glDeleteFramebuffersOES(1, &name);
1830
1831 *textureName = tname;
1832 *uOut = u;
1833 *vOut = v;
1834 return NO_ERROR;
1835}
1836
1837// ---------------------------------------------------------------------------
1838
1839status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1840{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001841 // get screen geometry
1842 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1843 const uint32_t hw_w = hw.getWidth();
1844 const uint32_t hw_h = hw.getHeight();
Mathias Agopianb946a562011-10-10 19:02:07 -07001845 const Region screenBounds(hw.getBounds());
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001846
1847 GLfloat u, v;
1848 GLuint tname;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -07001849 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001850 if (result != NO_ERROR) {
1851 return result;
1852 }
1853
1854 GLfloat vtx[8];
Mathias Agopianb946a562011-10-10 19:02:07 -07001855 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001856 glBindTexture(GL_TEXTURE_2D, tname);
1857 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1858 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1859 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1860 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1861 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1862 glVertexPointer(2, GL_FLOAT, 0, vtx);
1863
Mathias Agopian3a831d22011-07-07 17:30:31 -07001864 /*
1865 * Texture coordinate mapping
1866 *
1867 * u
1868 * 1 +----------+---+
1869 * | | | | image is inverted
1870 * | V | | w.r.t. the texture
1871 * 1-v +----------+ | coordinates
1872 * | |
1873 * | |
1874 * | |
1875 * 0 +--------------+
1876 * 0 1
1877 *
1878 */
1879
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001880 class s_curve_interpolator {
1881 const float nbFrames, s, v;
1882 public:
1883 s_curve_interpolator(int nbFrames, float s)
1884 : nbFrames(1.0f / (nbFrames-1)), s(s),
1885 v(1.0f + expf(-s + 0.5f*s)) {
1886 }
1887 float operator()(int f) {
1888 const float x = f * nbFrames;
1889 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1890 }
1891 };
1892
1893 class v_stretch {
1894 const GLfloat hw_w, hw_h;
1895 public:
1896 v_stretch(uint32_t hw_w, uint32_t hw_h)
1897 : hw_w(hw_w), hw_h(hw_h) {
1898 }
1899 void operator()(GLfloat* vtx, float v) {
1900 const GLfloat w = hw_w + (hw_w * v);
1901 const GLfloat h = hw_h - (hw_h * v);
1902 const GLfloat x = (hw_w - w) * 0.5f;
1903 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001904 vtx[0] = x; vtx[1] = y;
1905 vtx[2] = x; vtx[3] = y + h;
1906 vtx[4] = x + w; vtx[5] = y + h;
1907 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001908 }
1909 };
1910
1911 class h_stretch {
1912 const GLfloat hw_w, hw_h;
1913 public:
1914 h_stretch(uint32_t hw_w, uint32_t hw_h)
1915 : hw_w(hw_w), hw_h(hw_h) {
1916 }
1917 void operator()(GLfloat* vtx, float v) {
1918 const GLfloat w = hw_w - (hw_w * v);
1919 const GLfloat h = 1.0f;
1920 const GLfloat x = (hw_w - w) * 0.5f;
1921 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001922 vtx[0] = x; vtx[1] = y;
1923 vtx[2] = x; vtx[3] = y + h;
1924 vtx[4] = x + w; vtx[5] = y + h;
1925 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001926 }
1927 };
1928
1929 // the full animation is 24 frames
Mathias Agopian3a831d22011-07-07 17:30:31 -07001930 char value[PROPERTY_VALUE_MAX];
1931 property_get("debug.sf.electron_frames", value, "24");
1932 int nbFrames = (atoi(value) + 1) >> 1;
1933 if (nbFrames <= 0) // just in case
1934 nbFrames = 24;
1935
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001936 s_curve_interpolator itr(nbFrames, 7.5f);
1937 s_curve_interpolator itg(nbFrames, 8.0f);
1938 s_curve_interpolator itb(nbFrames, 8.5f);
1939
1940 v_stretch vverts(hw_w, hw_h);
Mathias Agopianb946a562011-10-10 19:02:07 -07001941
1942 glMatrixMode(GL_TEXTURE);
1943 glLoadIdentity();
1944 glMatrixMode(GL_MODELVIEW);
1945 glLoadIdentity();
1946
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001947 glEnable(GL_BLEND);
1948 glBlendFunc(GL_ONE, GL_ONE);
1949 for (int i=0 ; i<nbFrames ; i++) {
1950 float x, y, w, h;
1951 const float vr = itr(i);
1952 const float vg = itg(i);
1953 const float vb = itb(i);
1954
1955 // clear screen
1956 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001957 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001958 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001959
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001960 // draw the red plane
1961 vverts(vtx, vr);
1962 glColorMask(1,0,0,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 green plane
1966 vverts(vtx, vg);
1967 glColorMask(0,1,0,1);
1968 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001969
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001970 // draw the blue plane
1971 vverts(vtx, vb);
1972 glColorMask(0,0,1,1);
1973 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001974
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001975 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001976 glDisable(GL_TEXTURE_2D);
1977 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001978 glColor4f(vg, vg, vg, 1);
1979 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1980 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001981 }
1982
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001983 h_stretch hverts(hw_w, hw_h);
1984 glDisable(GL_BLEND);
1985 glDisable(GL_TEXTURE_2D);
1986 glColorMask(1,1,1,1);
1987 for (int i=0 ; i<nbFrames ; i++) {
1988 const float v = itg(i);
1989 hverts(vtx, v);
1990 glClear(GL_COLOR_BUFFER_BIT);
1991 glColor4f(1-v, 1-v, 1-v, 1);
1992 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1993 hw.flip(screenBounds);
1994 }
1995
1996 glColorMask(1,1,1,1);
1997 glEnable(GL_SCISSOR_TEST);
1998 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1999 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002000 glDisable(GL_TEXTURE_2D);
Mathias Agopian9044ef02011-10-18 14:49:27 -07002001 glDisable(GL_BLEND);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002002 return NO_ERROR;
2003}
2004
2005status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2006{
2007 status_t result = PERMISSION_DENIED;
2008
2009 if (!GLExtensions::getInstance().haveFramebufferObject())
2010 return INVALID_OPERATION;
2011
2012
2013 // get screen geometry
2014 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2015 const uint32_t hw_w = hw.getWidth();
2016 const uint32_t hw_h = hw.getHeight();
2017 const Region screenBounds(hw.bounds());
2018
2019 GLfloat u, v;
2020 GLuint tname;
2021 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2022 if (result != NO_ERROR) {
2023 return result;
2024 }
2025
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002026 GLfloat vtx[8];
2027 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002028 glBindTexture(GL_TEXTURE_2D, tname);
2029 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2030 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2031 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2032 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2033 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2034 glVertexPointer(2, GL_FLOAT, 0, vtx);
2035
2036 class s_curve_interpolator {
2037 const float nbFrames, s, v;
2038 public:
2039 s_curve_interpolator(int nbFrames, float s)
2040 : nbFrames(1.0f / (nbFrames-1)), s(s),
2041 v(1.0f + expf(-s + 0.5f*s)) {
2042 }
2043 float operator()(int f) {
2044 const float x = f * nbFrames;
2045 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2046 }
2047 };
2048
2049 class v_stretch {
2050 const GLfloat hw_w, hw_h;
2051 public:
2052 v_stretch(uint32_t hw_w, uint32_t hw_h)
2053 : hw_w(hw_w), hw_h(hw_h) {
2054 }
2055 void operator()(GLfloat* vtx, float v) {
2056 const GLfloat w = hw_w + (hw_w * v);
2057 const GLfloat h = hw_h - (hw_h * v);
2058 const GLfloat x = (hw_w - w) * 0.5f;
2059 const GLfloat y = (hw_h - h) * 0.5f;
2060 vtx[0] = x; vtx[1] = y;
2061 vtx[2] = x; vtx[3] = y + h;
2062 vtx[4] = x + w; vtx[5] = y + h;
2063 vtx[6] = x + w; vtx[7] = y;
2064 }
2065 };
2066
2067 class h_stretch {
2068 const GLfloat hw_w, hw_h;
2069 public:
2070 h_stretch(uint32_t hw_w, uint32_t hw_h)
2071 : hw_w(hw_w), hw_h(hw_h) {
2072 }
2073 void operator()(GLfloat* vtx, float v) {
2074 const GLfloat w = hw_w - (hw_w * v);
2075 const GLfloat h = 1.0f;
2076 const GLfloat x = (hw_w - w) * 0.5f;
2077 const GLfloat y = (hw_h - h) * 0.5f;
2078 vtx[0] = x; vtx[1] = y;
2079 vtx[2] = x; vtx[3] = y + h;
2080 vtx[4] = x + w; vtx[5] = y + h;
2081 vtx[6] = x + w; vtx[7] = y;
2082 }
2083 };
2084
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002085 // the full animation is 12 frames
2086 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002087 s_curve_interpolator itr(nbFrames, 7.5f);
2088 s_curve_interpolator itg(nbFrames, 8.0f);
2089 s_curve_interpolator itb(nbFrames, 8.5f);
2090
2091 h_stretch hverts(hw_w, hw_h);
2092 glDisable(GL_BLEND);
2093 glDisable(GL_TEXTURE_2D);
2094 glColorMask(1,1,1,1);
2095 for (int i=nbFrames-1 ; i>=0 ; i--) {
2096 const float v = itg(i);
2097 hverts(vtx, v);
2098 glClear(GL_COLOR_BUFFER_BIT);
2099 glColor4f(1-v, 1-v, 1-v, 1);
2100 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2101 hw.flip(screenBounds);
2102 }
2103
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002104 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002105 v_stretch vverts(hw_w, hw_h);
2106 glEnable(GL_BLEND);
2107 glBlendFunc(GL_ONE, GL_ONE);
2108 for (int i=nbFrames-1 ; i>=0 ; i--) {
2109 float x, y, w, h;
2110 const float vr = itr(i);
2111 const float vg = itg(i);
2112 const float vb = itb(i);
2113
2114 // clear screen
2115 glColorMask(1,1,1,1);
2116 glClear(GL_COLOR_BUFFER_BIT);
2117 glEnable(GL_TEXTURE_2D);
2118
2119 // draw the red plane
2120 vverts(vtx, vr);
2121 glColorMask(1,0,0,1);
2122 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2123
2124 // draw the green plane
2125 vverts(vtx, vg);
2126 glColorMask(0,1,0,1);
2127 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2128
2129 // draw the blue plane
2130 vverts(vtx, vb);
2131 glColorMask(0,0,1,1);
2132 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2133
2134 hw.flip(screenBounds);
2135 }
2136
2137 glColorMask(1,1,1,1);
2138 glEnable(GL_SCISSOR_TEST);
2139 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002140 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002141 glDisable(GL_TEXTURE_2D);
Mathias Agopian9044ef02011-10-18 14:49:27 -07002142 glDisable(GL_BLEND);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002143
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002144 return NO_ERROR;
2145}
2146
2147// ---------------------------------------------------------------------------
2148
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002149status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002150{
2151 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2152 if (!hw.canDraw()) {
2153 // we're already off
2154 return NO_ERROR;
2155 }
Mathias Agopian7f972582011-09-02 12:22:39 -07002156
2157 // turn off hwc while we're doing the animation
2158 hw.getHwComposer().disable();
2159 // and make sure to turn it back on (if needed) next time we compose
2160 invalidateHwcGeometry();
2161
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002162 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2163 electronBeamOffAnimationImplLocked();
2164 }
2165
2166 // always clear the whole screen at the end of the animation
2167 glClearColor(0,0,0,1);
2168 glDisable(GL_SCISSOR_TEST);
2169 glClear(GL_COLOR_BUFFER_BIT);
2170 glEnable(GL_SCISSOR_TEST);
2171 hw.flip( Region(hw.bounds()) );
2172
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002173 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002174}
2175
2176status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2177{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002178 class MessageTurnElectronBeamOff : public MessageBase {
2179 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002180 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002181 status_t result;
2182 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002183 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2184 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002185 }
2186 status_t getResult() const {
2187 return result;
2188 }
2189 virtual bool handler() {
2190 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002191 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002192 return true;
2193 }
2194 };
2195
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002196 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002197 status_t res = postMessageSync(msg);
2198 if (res == NO_ERROR) {
2199 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002200
2201 // work-around: when the power-manager calls us we activate the
2202 // animation. eventually, the "on" animation will be called
2203 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002204 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002205 }
2206 return res;
2207}
2208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002210
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002211status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002212{
2213 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2214 if (hw.canDraw()) {
2215 // we're already on
2216 return NO_ERROR;
2217 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002218 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2219 electronBeamOnAnimationImplLocked();
2220 }
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002221
2222 // make sure to redraw the whole screen when the animation is done
2223 mDirtyRegion.set(hw.bounds());
2224 signalEvent();
2225
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002226 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002227}
2228
2229status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2230{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002231 class MessageTurnElectronBeamOn : public MessageBase {
2232 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002233 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002234 status_t result;
2235 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002236 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2237 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002238 }
2239 status_t getResult() const {
2240 return result;
2241 }
2242 virtual bool handler() {
2243 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002244 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002245 return true;
2246 }
2247 };
2248
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002249 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002250 return NO_ERROR;
2251}
2252
2253// ---------------------------------------------------------------------------
2254
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002255status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2256 sp<IMemoryHeap>* heap,
2257 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002258 uint32_t sw, uint32_t sh,
2259 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002260{
2261 status_t result = PERMISSION_DENIED;
2262
2263 // only one display supported for now
2264 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2265 return BAD_VALUE;
2266
2267 if (!GLExtensions::getInstance().haveFramebufferObject())
2268 return INVALID_OPERATION;
2269
2270 // get screen geometry
2271 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2272 const uint32_t hw_w = hw.getWidth();
2273 const uint32_t hw_h = hw.getHeight();
2274
2275 if ((sw > hw_w) || (sh > hw_h))
2276 return BAD_VALUE;
2277
2278 sw = (!sw) ? hw_w : sw;
2279 sh = (!sh) ? hw_h : sh;
2280 const size_t size = sw * sh * 4;
2281
Mathias Agopian32ae0942011-03-02 18:45:50 -08002282 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2283 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002284
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002285 // make sure to clear all GL error flags
2286 while ( glGetError() != GL_NO_ERROR ) ;
2287
2288 // create a FBO
2289 GLuint name, tname;
2290 glGenRenderbuffersOES(1, &tname);
2291 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2292 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2293 glGenFramebuffersOES(1, &name);
2294 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2295 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2296 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2297
2298 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002299
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002300 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2301
2302 // invert everything, b/c glReadPixel() below will invert the FB
2303 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002304 glScissor(0, 0, sw, sh);
Mathias Agopian3a831d22011-07-07 17:30:31 -07002305 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002306 glMatrixMode(GL_PROJECTION);
2307 glPushMatrix();
2308 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -07002309 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002310 glMatrixMode(GL_MODELVIEW);
2311
2312 // redraw the screen entirely...
2313 glClearColor(0,0,0,1);
2314 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002315
Jamie Gennis830d0832011-10-07 14:51:16 -07002316 const LayerVector& layers(mDrawingState.layersSortedByZ);
2317 const size_t count = layers.size();
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002318 for (size_t i=0 ; i<count ; ++i) {
2319 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianec49d892011-08-25 14:36:43 -07002320 const uint32_t flags = layer->drawingState().flags;
2321 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2322 const uint32_t z = layer->drawingState().z;
2323 if (z >= minLayerZ && z <= maxLayerZ) {
2324 layer->drawForSreenShot();
2325 }
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002326 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002327 }
2328
2329 // XXX: this is needed on tegra
Mathias Agopian3a831d22011-07-07 17:30:31 -07002330 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002331 glScissor(0, 0, sw, sh);
2332
2333 // check for errors and return screen capture
2334 if (glGetError() != GL_NO_ERROR) {
2335 // error while rendering
2336 result = INVALID_OPERATION;
2337 } else {
2338 // allocate shared memory large enough to hold the
2339 // screen capture
2340 sp<MemoryHeapBase> base(
2341 new MemoryHeapBase(size, 0, "screen-capture") );
2342 void* const ptr = base->getBase();
2343 if (ptr) {
2344 // capture the screen with glReadPixels()
2345 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2346 if (glGetError() == GL_NO_ERROR) {
2347 *heap = base;
2348 *w = sw;
2349 *h = sh;
2350 *f = PIXEL_FORMAT_RGBA_8888;
2351 result = NO_ERROR;
2352 }
2353 } else {
2354 result = NO_MEMORY;
2355 }
2356 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002357 glEnable(GL_SCISSOR_TEST);
2358 glViewport(0, 0, hw_w, hw_h);
2359 glMatrixMode(GL_PROJECTION);
2360 glPopMatrix();
2361 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002362 } else {
2363 result = BAD_VALUE;
2364 }
2365
2366 // release FBO resources
2367 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2368 glDeleteRenderbuffersOES(1, &tname);
2369 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002370
2371 hw.compositionComplete();
2372
Mathias Agopian32ae0942011-03-02 18:45:50 -08002373 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002374
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002375 return result;
2376}
2377
2378
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002379status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2380 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002381 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002382 uint32_t sw, uint32_t sh,
2383 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002384{
2385 // only one display supported for now
2386 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2387 return BAD_VALUE;
2388
2389 if (!GLExtensions::getInstance().haveFramebufferObject())
2390 return INVALID_OPERATION;
2391
2392 class MessageCaptureScreen : public MessageBase {
2393 SurfaceFlinger* flinger;
2394 DisplayID dpy;
2395 sp<IMemoryHeap>* heap;
2396 uint32_t* w;
2397 uint32_t* h;
2398 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002399 uint32_t sw;
2400 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002401 uint32_t minLayerZ;
2402 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002403 status_t result;
2404 public:
2405 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002406 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002407 uint32_t sw, uint32_t sh,
2408 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002409 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002410 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2411 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2412 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002413 {
2414 }
2415 status_t getResult() const {
2416 return result;
2417 }
2418 virtual bool handler() {
2419 Mutex::Autolock _l(flinger->mStateLock);
2420
2421 // if we have secure windows, never allow the screen capture
2422 if (flinger->mSecureFrameBuffer)
2423 return true;
2424
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002425 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002426 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002427
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002428 return true;
2429 }
2430 };
2431
2432 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002433 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002434 status_t res = postMessageSync(msg);
2435 if (res == NO_ERROR) {
2436 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2437 }
2438 return res;
2439}
2440
2441// ---------------------------------------------------------------------------
2442
Mathias Agopian7623da42010-06-01 15:12:58 -07002443sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2444{
2445 sp<Layer> result;
2446 Mutex::Autolock _l(mStateLock);
2447 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2448 return result;
2449}
2450
2451// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452
Mathias Agopian593c05c2010-06-02 23:28:45 -07002453Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002454 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456}
2457
Mathias Agopian593c05c2010-06-02 23:28:45 -07002458Client::~Client()
2459{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002460 const size_t count = mLayers.size();
2461 for (size_t i=0 ; i<count ; i++) {
2462 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2463 if (layer != 0) {
2464 mFlinger->removeLayer(layer);
2465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
2467}
2468
Mathias Agopian593c05c2010-06-02 23:28:45 -07002469status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002470 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471}
Mathias Agopian1473f462009-04-10 14:24:30 -07002472
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002473size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002474{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002475 Mutex::Autolock _l(mLock);
2476 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002477 mLayers.add(name, layer);
2478 return name;
2479}
2480
Mathias Agopian7623da42010-06-01 15:12:58 -07002481void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002482{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002483 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002484 // we do a linear search here, because this doesn't happen often
2485 const size_t count = mLayers.size();
2486 for (size_t i=0 ; i<count ; i++) {
2487 if (mLayers.valueAt(i) == layer) {
2488 mLayers.removeItemsAt(i, 1);
2489 break;
2490 }
2491 }
2492}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002493sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2494{
2495 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002496 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002497 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002498 if (layer != 0) {
2499 lbc = layer.promote();
2500 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002501 }
2502 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503}
2504
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002505
2506status_t Client::onTransact(
2507 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2508{
2509 // these must be checked
2510 IPCThreadState* ipc = IPCThreadState::self();
2511 const int pid = ipc->getCallingPid();
2512 const int uid = ipc->getCallingUid();
2513 const int self_pid = getpid();
2514 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2515 // we're called from a different process, do the real check
Mathias Agopian0dd593f2011-06-27 16:05:52 -07002516 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002517 {
2518 LOGE("Permission Denial: "
2519 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2520 return PERMISSION_DENIED;
2521 }
2522 }
2523 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopian7623da42010-06-01 15:12:58 -07002524}
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002525
2526
Mathias Agopian593c05c2010-06-02 23:28:45 -07002527sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002528 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002529 const String8& name,
2530 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 uint32_t flags)
2532{
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002533 /*
2534 * createSurface must be called from the GL thread so that it can
2535 * have access to the GL context.
2536 */
2537
2538 class MessageCreateSurface : public MessageBase {
2539 sp<ISurface> result;
2540 SurfaceFlinger* flinger;
2541 ISurfaceComposerClient::surface_data_t* params;
2542 Client* client;
2543 const String8& name;
2544 DisplayID display;
2545 uint32_t w, h;
2546 PixelFormat format;
2547 uint32_t flags;
2548 public:
2549 MessageCreateSurface(SurfaceFlinger* flinger,
2550 ISurfaceComposerClient::surface_data_t* params,
2551 const String8& name, Client* client,
2552 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2553 uint32_t flags)
2554 : flinger(flinger), params(params), client(client), name(name),
2555 display(display), w(w), h(h), format(format), flags(flags)
2556 {
2557 }
2558 sp<ISurface> getResult() const { return result; }
2559 virtual bool handler() {
2560 result = flinger->createSurface(params, name, client,
2561 display, w, h, format, flags);
2562 return true;
2563 }
2564 };
2565
2566 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2567 params, name, this, display, w, h, format, flags);
2568 mFlinger->postMessageSync(msg);
2569 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002571status_t Client::destroySurface(SurfaceID sid) {
2572 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574
2575// ---------------------------------------------------------------------------
2576
Jamie Gennisf7acf162011-01-12 18:30:40 -08002577GraphicBufferAlloc::GraphicBufferAlloc() {}
2578
2579GraphicBufferAlloc::~GraphicBufferAlloc() {}
2580
2581sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002582 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennisf7acf162011-01-12 18:30:40 -08002583 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2584 status_t err = graphicBuffer->initCheck();
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002585 *error = err;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002586 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002587 if (err == NO_MEMORY) {
2588 GraphicBuffer::dumpAllocationsToSystemLog();
2589 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002590 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2591 "failed (%s), handle=%p",
2592 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennisf7acf162011-01-12 18:30:40 -08002593 return 0;
2594 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002595 return graphicBuffer;
2596}
2597
Jamie Gennisf7acf162011-01-12 18:30:40 -08002598// ---------------------------------------------------------------------------
2599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600GraphicPlane::GraphicPlane()
2601 : mHw(0)
2602{
2603}
2604
2605GraphicPlane::~GraphicPlane() {
2606 delete mHw;
2607}
2608
2609bool GraphicPlane::initialized() const {
2610 return mHw ? true : false;
2611}
2612
Mathias Agopian66c77a52010-02-08 15:49:35 -08002613int GraphicPlane::getWidth() const {
2614 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615}
2616
Mathias Agopian66c77a52010-02-08 15:49:35 -08002617int GraphicPlane::getHeight() const {
2618 return mHeight;
2619}
2620
2621void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2622{
2623 mHw = hw;
2624
2625 // initialize the display orientation transform.
2626 // it's a constant that should come from the display driver.
2627 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2628 char property[PROPERTY_VALUE_MAX];
2629 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2630 //displayOrientation
2631 switch (atoi(property)) {
2632 case 90:
2633 displayOrientation = ISurfaceComposer::eOrientation90;
2634 break;
2635 case 270:
2636 displayOrientation = ISurfaceComposer::eOrientation270;
2637 break;
2638 }
2639 }
2640
2641 const float w = hw->getWidth();
2642 const float h = hw->getHeight();
2643 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2644 &mDisplayTransform);
2645 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2646 mDisplayWidth = h;
2647 mDisplayHeight = w;
2648 } else {
2649 mDisplayWidth = w;
2650 mDisplayHeight = h;
2651 }
2652
2653 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654}
2655
2656status_t GraphicPlane::orientationToTransfrom(
2657 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002658{
2659 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 switch (orientation) {
2661 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002662 flags = Transform::ROT_0;
2663 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002665 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 break;
2667 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002668 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 break;
2670 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002671 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 break;
2673 default:
2674 return BAD_VALUE;
2675 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002676 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 return NO_ERROR;
2678}
2679
2680status_t GraphicPlane::setOrientation(int orientation)
2681{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 // If the rotation can be handled in hardware, this is where
2683 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002684
2685 const DisplayHardware& hw(displayHardware());
2686 const float w = mDisplayWidth;
2687 const float h = mDisplayHeight;
2688 mWidth = int(w);
2689 mHeight = int(h);
2690
2691 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002692 GraphicPlane::orientationToTransfrom(orientation, w, h,
2693 &orientationTransform);
2694 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2695 mWidth = int(h);
2696 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002698
Mathias Agopian3552f532009-03-27 17:58:20 -07002699 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002700 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 return NO_ERROR;
2702}
2703
2704const DisplayHardware& GraphicPlane::displayHardware() const {
2705 return *mHw;
2706}
2707
Mathias Agopianaab758e2010-10-11 12:37:43 -07002708DisplayHardware& GraphicPlane::editDisplayHardware() {
2709 return *mHw;
2710}
2711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712const Transform& GraphicPlane::transform() const {
2713 return mGlobalTransform;
2714}
2715
Mathias Agopian1473f462009-04-10 14:24:30 -07002716EGLDisplay GraphicPlane::getEGLDisplay() const {
2717 return mHw->getEGLDisplay();
2718}
2719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720// ---------------------------------------------------------------------------
2721
2722}; // namespace android