blob: 58196d8f34ad94588cfad81b6d753210eaf6139e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-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 Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-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 Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <utils/String8.h>
40#include <utils/String16.h>
41#include <utils/StopWatch.h>
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070044#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47#include <pixelflinger/pixelflinger.h>
48#include <GLES/gl.h>
49
50#include "clz.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051#include "DisplayEventConnection.h"
52#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070053#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070054#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070057#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070061#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana67932f2011-04-20 14:20:59 -070063#include <private/surfaceflinger/SharedBufferStack.h>
64
Mathias Agopiana1ecca92009-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 Agopianbc2d79e2011-11-29 17:55:46 -080072#define EGL_VERSION_HW_ANDROID 0x3143
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
76namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ---------------------------------------------------------------------------
78
Mathias Agopian99b49842011-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 Projectedbf3b62009-03-03 19:31:44 -080086SurfaceFlinger::SurfaceFlinger()
87 : BnSurfaceComposer(), Thread(false),
88 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070089 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070093 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070094 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070097 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070098 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070099 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700100 mDebugInSwapBuffers(0),
101 mLastSwapBufferTime(0),
102 mDebugInTransaction(0),
103 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700104 mBootFinished(false),
The Android Open Source Projectedbf3b62009-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 Agopian8afb7e32011-08-15 20:44:40 -0700117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 property_get("debug.sf.showupdates", value, "0");
119 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 property_get("debug.sf.showbackground", value, "0");
122 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
Mathias Agopian8afb7e32011-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 Agopian78fd5012010-04-20 14:51:04 -0700130 LOGI_IF(mDebugRegion, "showupdates enabled");
131 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700132 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133}
134
135SurfaceFlinger::~SurfaceFlinger()
136{
137 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700140sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700142 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Mathias Agopian7e27f052010-05-28 14:22:23 -0700145sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146{
Mathias Agopian96f08192010-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 Projectedbf3b62009-03-03 19:31:44 -0800152 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 return bclient;
154}
155
Jamie Gennis9a78c902011-01-12 18:30:40 -0800156sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
157{
158 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
159 return gba;
160}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700161
The Android Open Source Projectedbf3b62009-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 Huber8b42e8a2010-08-16 08:49:37 -0700179 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700180 mBootFinished = true;
Mathias Agopian1f339ff2011-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 Agopiana1ecca92009-05-21 19:21:59 -0700190 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian1f339ff2011-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 Agopian1f339ff2011-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 Projectedbf3b62009-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 Projectedbf3b62009-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 Projectedbf3b62009-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 Projectedbf3b62009-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 Agopian7303c6b2009-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 Huber8b42e8a2010-08-16 08:49:37 -0700235
Mathias Agopian7303c6b2009-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 Huber8b42e8a2010-08-16 08:49:37 -0700238
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700239 new(mServerCblk) surface_flinger_cblk_t;
240
The Android Open Source Projectedbf3b62009-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 Agopian2b92d892010-02-08 15:49:35 -0800256 dcblk->w = plane.getWidth();
257 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -0800264
265 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700267 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-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 Gennis9575f602011-10-07 14:51:16 -0700276 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-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 Gennis9575f602011-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 Projectedbf3b62009-03-03 19:31:44 -0800295
296 glViewport(0, 0, w, h);
297 glMatrixMode(GL_PROJECTION);
298 glLoadIdentity();
Mathias Agopianffcf4652011-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 Projectedbf3b62009-03-03 19:31:44 -0800301
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800302
303 // start the EventThread
304 mEventThread = new EventThread(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800310 mReadyToRunBarrier.open();
311
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700312 // start boot animation
313 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700314
The Android Open Source Projectedbf3b62009-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 Agopianf61c57f2011-11-23 16:49:10 -0800324void SurfaceFlinger::waitForEvent() {
325 mEventQueue.waitMessage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326}
327
328void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700329 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Mathias Agopiand0566bc2011-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 Gennis582270d2011-08-17 18:19:00 -0700348bool SurfaceFlinger::authenticateSurfaceTexture(
349 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800350 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700351 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-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 Gennis582270d2011-08-17 18:19:00 -0700359 if (lbc != NULL) {
360 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
361 if (lbcBinder == surfaceTextureBinder) {
362 return true;
363 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800364 }
365 }
366
367 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-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 Gennis134f0422011-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 Gennis582270d2011-08-17 18:19:00 -0700378 if (lbc != NULL) {
379 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
380 if (lbcBinder == surfaceTextureBinder) {
381 return true;
382 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800383 }
384 }
385
386 return false;
387}
388
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800389// ----------------------------------------------------------------------------
390
391sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
392 sp<DisplayEventConnection> result(new DisplayEventConnection(this));
393 mEventThread->registerDisplayEventConnection(result);
394 return result;
Mathias Agopianbb641242010-05-18 17:06:55 -0700395}
396
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800397void SurfaceFlinger::cleanupDisplayEventConnection(
398 const wp<DisplayEventConnection>& connection) {
399 mEventThread->unregisterDisplayEventConnection(connection);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400}
401
402// ----------------------------------------------------------------------------
403#if 0
404#pragma mark -
405#pragma mark Main loop
406#endif
407
408bool SurfaceFlinger::threadLoop()
409{
410 waitForEvent();
411
412 // check for transactions
413 if (UNLIKELY(mConsoleSignals)) {
414 handleConsoleEvents();
415 }
416
Mathias Agopian698c0872011-06-28 19:09:31 -0700417 // if we're in a global transaction, don't do anything.
418 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
419 uint32_t transactionFlags = peekTransactionFlags(mask);
420 if (UNLIKELY(transactionFlags)) {
421 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 }
423
424 // post surfaces (if needed)
425 handlePageFlip();
426
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700427 if (mDirtyRegion.isEmpty()) {
428 // nothing new to do.
429 return true;
430 }
431
Mathias Agopiana350ff92010-08-10 17:14:02 -0700432 if (UNLIKELY(mHwWorkListDirty)) {
433 // build the h/w work list
434 handleWorkList();
435 }
436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700438 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700440
441 const int index = hw.getCurrentBufferIndex();
442 GraphicLog& logger(GraphicLog::getInstance());
443
444 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 handleRepaint();
446
Mathias Agopian74faca22009-09-17 16:18:16 -0700447 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700448 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700449 hw.compositionComplete();
450
Mathias Agopian35b48d12010-09-13 22:57:58 -0700451 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700452 postFramebuffer();
453
Mathias Agopian35b48d12010-09-13 22:57:58 -0700454 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 } else {
456 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800457 hw.compositionComplete();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800458 hw.waitForVSync();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459 }
460 return true;
461}
462
463void SurfaceFlinger::postFramebuffer()
464{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700465 // this should never happen. we do the flip anyways so we don't
466 // risk to cause a deadlock with hwc
467 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700468 const DisplayHardware& hw(graphicPlane(0).displayHardware());
469 const nsecs_t now = systemTime();
470 mDebugInSwapBuffers = now;
471 hw.flip(mSwapRegion);
472 mLastSwapBufferTime = systemTime() - now;
473 mDebugInSwapBuffers = 0;
474 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475}
476
477void SurfaceFlinger::handleConsoleEvents()
478{
479 // something to do with the console
480 const DisplayHardware& hw = graphicPlane(0).displayHardware();
481
482 int what = android_atomic_and(0, &mConsoleSignals);
483 if (what & eConsoleAcquired) {
484 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700485 // this is a temporary work-around, eventually this should be called
486 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700487 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 }
489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700491 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493 }
494 }
495
496 mDirtyRegion.set(hw.bounds());
497}
498
499void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
500{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700501 Mutex::Autolock _l(mStateLock);
502 const nsecs_t now = systemTime();
503 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504
Mathias Agopianca4d3602011-05-19 15:38:14 -0700505 // Here we're guaranteed that some transaction flags are set
506 // so we can call handleTransactionLocked() unconditionally.
507 // We call getTransactionFlags(), which will also clear the flags,
508 // with mStateLock held to guarantee that mCurrentState won't change
509 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700510
Mathias Agopianca4d3602011-05-19 15:38:14 -0700511 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
512 transactionFlags = getTransactionFlags(mask);
513 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700514
Mathias Agopianca4d3602011-05-19 15:38:14 -0700515 mLastTransactionTime = systemTime() - now;
516 mDebugInTransaction = 0;
517 invalidateHwcGeometry();
518 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700519}
520
Mathias Agopianca4d3602011-05-19 15:38:14 -0700521void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700522{
523 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524 const size_t count = currentLayers.size();
525
526 /*
527 * Traversal of the children
528 * (perform the transaction for each of them if needed)
529 */
530
531 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
532 if (layersNeedTransaction) {
533 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
536 if (!trFlags) continue;
537
538 const uint32_t flags = layer->doTransaction(0);
539 if (flags & Layer::eVisibleRegion)
540 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541 }
542 }
543
544 /*
545 * Perform our own transaction if needed
546 */
547
548 if (transactionFlags & eTransactionNeeded) {
549 if (mCurrentState.orientation != mDrawingState.orientation) {
550 // the orientation has changed, recompute all visible regions
551 // and invalidate everything.
552
553 const int dpy = 0;
554 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700555 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 GraphicPlane& plane(graphicPlane(dpy));
557 plane.setOrientation(orientation);
558
559 // update the shared control block
560 const DisplayHardware& hw(plane.displayHardware());
561 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
562 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800563 dcblk->w = plane.getWidth();
564 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565
566 mVisibleRegionsDirty = true;
567 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 }
569
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700570 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
571 // layers have been added
572 mVisibleRegionsDirty = true;
573 }
574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 // some layers might have been removed, so
576 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700577 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700578 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700580 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700581 const size_t count = previousLayers.size();
582 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700583 const sp<LayerBase>& layer(previousLayers[i]);
584 if (currentLayers.indexOf( layer ) < 0) {
585 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700586 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700587 }
588 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 }
591
592 commitTransaction();
593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800596 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597{
598 const GraphicPlane& plane(graphicPlane(0));
599 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700600 const DisplayHardware& hw(plane.displayHardware());
601 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602
603 Region aboveOpaqueLayers;
604 Region aboveCoveredLayers;
605 Region dirty;
606
607 bool secureFrameBuffer = false;
608
609 size_t i = currentLayers.size();
610 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700611 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 layer->validateVisibility(planeTransform);
613
614 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700615 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616
Mathias Agopianab028732010-03-16 16:41:46 -0700617 /*
618 * opaqueRegion: area of a surface that is fully opaque.
619 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700621
622 /*
623 * visibleRegion: area of a surface that is visible on screen
624 * and not fully transparent. This is essentially the layer's
625 * footprint minus the opaque regions above it.
626 * Areas covered by a translucent surface are considered visible.
627 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700629
630 /*
631 * coveredRegion: area of a surface that is covered by all
632 * visible regions above it (which includes the translucent areas).
633 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700635
636
637 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700638 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700639 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700640 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700642 visibleRegion.andSelf(screenRegion);
643 if (!visibleRegion.isEmpty()) {
644 // Remove the transparent area from the visible region
645 if (translucent) {
646 visibleRegion.subtractSelf(layer->transparentRegionScreen);
647 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648
Mathias Agopianab028732010-03-16 16:41:46 -0700649 // compute the opaque region
650 const int32_t layerOrientation = layer->getOrientation();
651 if (s.alpha==255 && !translucent &&
652 ((layerOrientation & Transform::ROT_INVALID) == false)) {
653 // the opaque region is the layer's footprint
654 opaqueRegion = visibleRegion;
655 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 }
657 }
658
Mathias Agopianab028732010-03-16 16:41:46 -0700659 // Clip the covered region to the visible region
660 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
661
662 // Update aboveCoveredLayers for next (lower) layer
663 aboveCoveredLayers.orSelf(visibleRegion);
664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665 // subtract the opaque region covered by the layers above us
666 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667
668 // compute this layer's dirty region
669 if (layer->contentDirty) {
670 // we need to invalidate the whole region
671 dirty = visibleRegion;
672 // as well, as the old visible region
673 dirty.orSelf(layer->visibleRegionScreen);
674 layer->contentDirty = false;
675 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700676 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700677 * the exposed region consists of two components:
678 * 1) what's VISIBLE now and was COVERED before
679 * 2) what's EXPOSED now less what was EXPOSED before
680 *
681 * note that (1) is conservative, we start with the whole
682 * visible region but only keep what used to be covered by
683 * something -- which mean it may have been exposed.
684 *
685 * (2) handles areas that were not covered by anything but got
686 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700687 */
Mathias Agopianab028732010-03-16 16:41:46 -0700688 const Region newExposed = visibleRegion - coveredRegion;
689 const Region oldVisibleRegion = layer->visibleRegionScreen;
690 const Region oldCoveredRegion = layer->coveredRegionScreen;
691 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
692 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 }
694 dirty.subtractSelf(aboveOpaqueLayers);
695
696 // accumulate to the screen dirty region
697 dirtyRegion.orSelf(dirty);
698
Mathias Agopianab028732010-03-16 16:41:46 -0700699 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700701
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702 // Store the visible region is screen space
703 layer->setVisibleRegion(visibleRegion);
704 layer->setCoveredRegion(coveredRegion);
705
Mathias Agopian97011222009-07-28 10:57:27 -0700706 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 if (layer->isSecure() && !visibleRegion.isEmpty()) {
708 secureFrameBuffer = true;
709 }
710 }
711
Mathias Agopian97011222009-07-28 10:57:27 -0700712 // invalidate the areas where a layer was removed
713 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
714 mDirtyRegionRemovedLayer.clear();
715
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716 mSecureFrameBuffer = secureFrameBuffer;
717 opaqueRegion = aboveOpaqueLayers;
718}
719
720
721void SurfaceFlinger::commitTransaction()
722{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800723 if (!mLayersPendingRemoval.isEmpty()) {
724 // Notify removed layers now that they can't be drawn from
725 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
726 mLayersPendingRemoval[i]->onRemoved();
727 }
728 mLayersPendingRemoval.clear();
729 }
730
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700732 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700733 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734}
735
736void SurfaceFlinger::handlePageFlip()
737{
738 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800739 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 visibleRegions |= lockPageFlip(currentLayers);
741
742 const DisplayHardware& hw = graphicPlane(0).displayHardware();
743 const Region screenRegion(hw.bounds());
744 if (visibleRegions) {
745 Region opaqueRegion;
746 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700747
748 /*
749 * rebuild the visible layer list
750 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800751 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700752 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700753 mVisibleLayersSortedByZ.setCapacity(count);
754 for (size_t i=0 ; i<count ; i++) {
755 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
756 mVisibleLayersSortedByZ.add(currentLayers[i]);
757 }
758
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 mWormholeRegion = screenRegion.subtract(opaqueRegion);
760 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800761 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 }
763
764 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700765
766 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767 mDirtyRegion.andSelf(screenRegion);
768}
769
Mathias Agopianad456f92011-01-13 17:53:01 -0800770void SurfaceFlinger::invalidateHwcGeometry()
771{
772 mHwWorkListDirty = true;
773}
774
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
776{
777 bool recomputeVisibleRegions = false;
778 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700779 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700781 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 layer->lockPageFlip(recomputeVisibleRegions);
783 }
784 return recomputeVisibleRegions;
785}
786
787void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
788{
789 const GraphicPlane& plane(graphicPlane(0));
790 const Transform& planeTransform(plane.transform());
791 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700792 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700794 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 layer->unlockPageFlip(planeTransform, mDirtyRegion);
796 }
797}
798
Mathias Agopiana350ff92010-08-10 17:14:02 -0700799void SurfaceFlinger::handleWorkList()
800{
801 mHwWorkListDirty = false;
802 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
803 if (hwc.initCheck() == NO_ERROR) {
804 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
805 const size_t count = currentLayers.size();
806 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700807 hwc_layer_t* const cur(hwc.getLayers());
808 for (size_t i=0 ; cur && i<count ; i++) {
809 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700810 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700811 cur[i].compositionType = HWC_FRAMEBUFFER;
812 cur[i].flags |= HWC_SKIP_LAYER;
813 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700814 }
815 }
816}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700817
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818void SurfaceFlinger::handleRepaint()
819{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700820 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700821 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822
823 if (UNLIKELY(mDebugRegion)) {
824 debugFlashRegions();
825 }
826
Mathias Agopianb8a55602009-06-26 19:06:36 -0700827 // set the frame buffer
828 const DisplayHardware& hw(graphicPlane(0).displayHardware());
829 glMatrixMode(GL_MODELVIEW);
830 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831
832 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700833 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
834 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700835 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
837 // takes a rectangle, we must make sure to update that whole
838 // rectangle in that case
839 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700840 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700842 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700844 // in the BUFFER_PRESERVED case, obviously, we can update only
845 // what's needed and nothing more.
846 // NOTE: this is NOT a common case, as preserving the backbuffer
847 // is costly and usually involves copying the whole update back.
848 }
849 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700850 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700851 // We need to redraw the rectangle that will be updated
852 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700853 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700854 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700855 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 } else {
857 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700859 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 }
861 }
862
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700863 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 composeSurfaces(mDirtyRegion);
865
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700866 // update the swap region and clear the dirty region
867 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800868 mDirtyRegion.clear();
869}
870
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700871void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700873 const DisplayHardware& hw(graphicPlane(0).displayHardware());
874 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700875 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700876 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700877 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700878 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700879
Mathias Agopianf384cc32011-09-08 18:31:55 -0700880 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
881 size_t count = layers.size();
882
883 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700884 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
885 hwc.getNumLayers(), count);
886
887 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700888 if (hwc.initCheck() == NO_ERROR) {
889 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
890 }
Mathias Agopian45721772010-08-12 15:03:26 -0700891
892 /*
893 * update the per-frame h/w composer data for each layer
894 * and build the transparent region of the FB
895 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700896 for (size_t i=0 ; i<count ; i++) {
897 const sp<LayerBase>& layer(layers[i]);
898 layer->setPerFrameData(&cur[i]);
899 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700900 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700901 status_t err = hwc.prepare();
902 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700903
Mathias Agopianf384cc32011-09-08 18:31:55 -0700904 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700905 // what's happening here is tricky.
906 // we want to clear all the layers with the CLEAR_FB flags
907 // that are opaque.
908 // however, since some GPU are efficient at preserving
909 // the backbuffer, we want to take advantage of that so we do the
910 // clear only in the dirty region (other areas will be preserved
911 // on those GPUs).
912 // NOTE: on non backbuffer preserving GPU, the dirty region
913 // has already been expanded as needed, so the code is correct
914 // there too.
915 //
916 // However, the content of the framebuffer cannot be trusted when
917 // we switch to/from FB/OVERLAY, in which case we need to
918 // expand the dirty region to those areas too.
919 //
920 // Note also that there is a special case when switching from
921 // "no layers in FB" to "some layers in FB", where we need to redraw
922 // the entire FB, since some areas might contain uninitialized
923 // data.
924 //
925 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700926 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700927 // that is, we can't erase anything outside the dirty region.
928
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700929 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700930
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700931 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
932 transparent.set(hw.getBounds());
933 dirtyInOut = transparent;
934 } else {
935 for (size_t i=0 ; i<count ; i++) {
936 const sp<LayerBase>& layer(layers[i]);
937 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
938 transparent.orSelf(layer->visibleRegionScreen);
939 }
940 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
941 if (isOverlay != layer->isOverlay()) {
942 // we transitioned to/from overlay, so add this layer
943 // to the dirty region so the framebuffer can be either
944 // cleared or redrawn.
945 dirtyInOut.orSelf(layer->visibleRegionScreen);
946 }
947 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800948 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700949 // don't erase stuff outside the dirty region
950 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700951 }
952
953 /*
954 * clear the area of the FB that need to be transparent
955 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700956 if (!transparent.isEmpty()) {
957 glClearColor(0,0,0,0);
958 Region::const_iterator it = transparent.begin();
959 Region::const_iterator const end = transparent.end();
960 const int32_t height = hw.getHeight();
961 while (it != end) {
962 const Rect& r(*it++);
963 const GLint sy = height - (r.top + r.height());
964 glScissor(r.left, sy, r.width(), r.height());
965 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800966 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700967 }
968 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700969}
Mathias Agopian45721772010-08-12 15:03:26 -0700970
Mathias Agopianf384cc32011-09-08 18:31:55 -0700971void SurfaceFlinger::composeSurfaces(const Region& dirty)
972{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700973 const DisplayHardware& hw(graphicPlane(0).displayHardware());
974 HWComposer& hwc(hw.getHwComposer());
975
976 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
977 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700978 // should never happen unless the window manager has a bug
979 // draw something...
980 drawWormhole();
981 }
982
Mathias Agopian45721772010-08-12 15:03:26 -0700983 /*
984 * and then, render the layers targeted at the framebuffer
985 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700986 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700987 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
988 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700989 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700990 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700991 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700992 }
993 const sp<LayerBase>& layer(layers[i]);
994 const Region clip(dirty.intersect(layer->visibleRegionScreen));
995 if (!clip.isEmpty()) {
996 layer->draw(clip);
997 }
998 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999}
1000
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001void SurfaceFlinger::debugFlashRegions()
1002{
Mathias Agopian0a917752010-06-14 21:20:00 -07001003 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1004 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001005 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001006 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001007 return;
1008 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001009
Mathias Agopian0a917752010-06-14 21:20:00 -07001010 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1011 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1012 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1013 mDirtyRegion.bounds() : hw.bounds());
1014 composeSurfaces(repaint);
1015 }
1016
Mathias Agopianc492e672011-10-18 14:49:27 -07001017 glDisable(GL_TEXTURE_EXTERNAL_OES);
1018 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 glDisable(GL_SCISSOR_TEST);
1021
Mathias Agopian0926f502009-05-04 14:17:04 -07001022 static int toggle = 0;
1023 toggle = 1 - toggle;
1024 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001025 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001026 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001027 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001028 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029
Mathias Agopian20f68782009-05-11 00:03:41 -07001030 Region::const_iterator it = mDirtyRegion.begin();
1031 Region::const_iterator const end = mDirtyRegion.end();
1032 while (it != end) {
1033 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001035 { r.left, height - r.top },
1036 { r.left, height - r.bottom },
1037 { r.right, height - r.bottom },
1038 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 };
1040 glVertexPointer(2, GL_FLOAT, 0, vertices);
1041 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1042 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001043
Mathias Agopian0656a682011-09-20 17:22:44 -07001044 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045
1046 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001047 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048
1049 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050}
1051
1052void SurfaceFlinger::drawWormhole() const
1053{
1054 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1055 if (region.isEmpty())
1056 return;
1057
1058 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1059 const int32_t width = hw.getWidth();
1060 const int32_t height = hw.getHeight();
1061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001063 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001064 Region::const_iterator it = region.begin();
1065 Region::const_iterator const end = region.end();
1066 while (it != end) {
1067 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 const GLint sy = height - (r.top + r.height());
1069 glScissor(r.left, sy, r.width(), r.height());
1070 glClear(GL_COLOR_BUFFER_BIT);
1071 }
1072 } else {
1073 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1074 { width, height }, { 0, height } };
1075 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 glVertexPointer(2, GL_SHORT, 0, vertices);
1078 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1079 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001080
1081 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 glEnable(GL_TEXTURE_2D);
1083 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1084 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1085 glMatrixMode(GL_TEXTURE);
1086 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001087
1088 glDisable(GL_BLEND);
1089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001091 Region::const_iterator it = region.begin();
1092 Region::const_iterator const end = region.end();
1093 while (it != end) {
1094 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 const GLint sy = height - (r.top + r.height());
1096 glScissor(r.left, sy, r.width(), r.height());
1097 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1098 }
1099 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001100 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001101 glLoadIdentity();
1102 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 }
1104}
1105
1106void SurfaceFlinger::debugShowFPS() const
1107{
1108 static int mFrameCount;
1109 static int mLastFrameCount = 0;
1110 static nsecs_t mLastFpsTime = 0;
1111 static float mFps = 0;
1112 mFrameCount++;
1113 nsecs_t now = systemTime();
1114 nsecs_t diff = now - mLastFpsTime;
1115 if (diff > ms2ns(250)) {
1116 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1117 mLastFpsTime = now;
1118 mLastFrameCount = mFrameCount;
1119 }
1120 // XXX: mFPS has the value we want
1121 }
1122
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001123status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124{
1125 Mutex::Autolock _l(mStateLock);
1126 addLayer_l(layer);
1127 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1128 return NO_ERROR;
1129}
1130
Mathias Agopian96f08192010-06-02 23:28:45 -07001131status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1132{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001133 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001134 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1135}
1136
1137ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1138 const sp<LayerBaseClient>& lbc)
1139{
Mathias Agopian96f08192010-06-02 23:28:45 -07001140 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001141 size_t name = client->attachLayer(lbc);
1142
1143 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001144
1145 // add this layer to the current state list
1146 addLayer_l(lbc);
1147
Mathias Agopian4f113742011-05-03 16:21:41 -07001148 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001149}
1150
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001151status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152{
1153 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001154 status_t err = purgatorizeLayer_l(layer);
1155 if (err == NO_ERROR)
1156 setTransactionFlags(eTransactionNeeded);
1157 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158}
1159
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001160status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001162 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1163 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001164 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001165 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1167 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001168 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 return NO_ERROR;
1170 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001171 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172}
1173
Mathias Agopian9a112062009-04-17 19:36:26 -07001174status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1175{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001176 // First add the layer to the purgatory list, which makes sure it won't
1177 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001178 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001179 if (err >= 0) {
1180 mLayerPurgatory.add(layerBase);
1181 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001182
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001183 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001184
Mathias Agopian3d579642009-06-04 18:46:21 -07001185 // it's possible that we don't find a layer, because it might
1186 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001187 // from the user because there is a race between Client::destroySurface(),
1188 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001189 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1190}
1191
Mathias Agopian96f08192010-06-02 23:28:45 -07001192status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193{
Mathias Agopian96f08192010-06-02 23:28:45 -07001194 layer->forceVisibilityTransaction();
1195 setTransactionFlags(eTraversalNeeded);
1196 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197}
1198
Mathias Agopiandea20b12011-05-03 17:04:02 -07001199uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1200{
1201 return android_atomic_release_load(&mTransactionFlags);
1202}
1203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1205{
1206 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1207}
1208
Mathias Agopianbb641242010-05-18 17:06:55 -07001209uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210{
1211 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1212 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001213 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 }
1215 return old;
1216}
1217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001219void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001220 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001221 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001222
Jamie Gennis28378392011-10-12 17:39:00 -07001223 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001224 if (mCurrentState.orientation != orientation) {
1225 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1226 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001227 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001228 } else if (orientation != eOrientationUnchanged) {
1229 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1230 orientation);
1231 }
1232 }
1233
Mathias Agopian698c0872011-06-28 19:09:31 -07001234 const size_t count = state.size();
1235 for (size_t i=0 ; i<count ; i++) {
1236 const ComposerState& s(state[i]);
1237 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001238 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001239 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001240
Mathias Agopian386aa982011-11-07 21:58:03 -08001241 if (transactionFlags) {
1242 // this triggers the transaction
1243 setTransactionFlags(transactionFlags);
1244
1245 // if this is a synchronous transaction, wait for it to take effect
1246 // before returning.
1247 if (flags & eSynchronous) {
1248 mTransationPending = true;
1249 }
1250 while (mTransationPending) {
1251 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1252 if (CC_UNLIKELY(err != NO_ERROR)) {
1253 // just in case something goes wrong in SF, return to the
1254 // called after a few seconds.
1255 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1256 mTransationPending = false;
1257 break;
1258 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 }
1261}
1262
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001263int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001264 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265{
1266 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1267 return BAD_VALUE;
1268
1269 Mutex::Autolock _l(mStateLock);
1270 if (mCurrentState.orientation != orientation) {
1271 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001272 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 mCurrentState.orientation = orientation;
1274 setTransactionFlags(eTransactionNeeded);
1275 mTransactionCV.wait(mStateLock);
1276 } else {
1277 orientation = BAD_VALUE;
1278 }
1279 }
1280 return orientation;
1281}
1282
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001283sp<ISurface> SurfaceFlinger::createSurface(
1284 ISurfaceComposerClient::surface_data_t* params,
1285 const String8& name,
1286 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1288 uint32_t flags)
1289{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001290 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001291 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001292
1293 if (int32_t(w|h) < 0) {
1294 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1295 int(w), int(h));
1296 return surfaceHandle;
1297 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001298
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 switch (flags & eFXSurfaceMask) {
1302 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001303 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1304 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 break;
1306 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001307 // for now we treat Blur as Dim, until we can implement it
1308 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001310 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001312 case eFXSurfaceScreenshot:
1313 layer = createScreenshotSurface(client, d, w, h, flags);
1314 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 }
1316
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001317 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001319 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001320 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001323 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001324 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001325 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001326 if (normalLayer != 0) {
1327 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001328 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001329 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001330 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001331
Mathias Agopian96f08192010-06-02 23:28:45 -07001332 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 }
1334
1335 return surfaceHandle;
1336}
1337
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001338sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001339 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001340 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001341 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342{
1343 // initialize the surfaces
1344 switch (format) { // TODO: take h/w into account
1345 case PIXEL_FORMAT_TRANSPARENT:
1346 case PIXEL_FORMAT_TRANSLUCENT:
1347 format = PIXEL_FORMAT_RGBA_8888;
1348 break;
1349 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001350#ifdef NO_RGBX_8888
1351 format = PIXEL_FORMAT_RGB_565;
1352#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001353 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001354#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 break;
1356 }
1357
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001358#ifdef NO_RGBX_8888
1359 if (format == PIXEL_FORMAT_RGBX_8888)
1360 format = PIXEL_FORMAT_RGBA_8888;
1361#endif
1362
Mathias Agopian96f08192010-06-02 23:28:45 -07001363 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001364 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001365 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001367 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 }
1369 return layer;
1370}
1371
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001372sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001373 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001374 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375{
Mathias Agopian96f08192010-06-02 23:28:45 -07001376 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001377 return layer;
1378}
1379
1380sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1381 const sp<Client>& client, DisplayID display,
1382 uint32_t w, uint32_t h, uint32_t flags)
1383{
1384 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 return layer;
1386}
1387
Mathias Agopian96f08192010-06-02 23:28:45 -07001388status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389{
Mathias Agopian9a112062009-04-17 19:36:26 -07001390 /*
1391 * called by the window manager, when a surface should be marked for
1392 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001393 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001394 * The surface is removed from the current and drawing lists, but placed
1395 * in the purgatory queue, so it's not destroyed right-away (we need
1396 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001397 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001398
Mathias Agopian48d819a2009-09-10 19:41:18 -07001399 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001400 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001401 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001402 if (layer != 0) {
1403 err = purgatorizeLayer_l(layer);
1404 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001405 setTransactionFlags(eTransactionNeeded);
1406 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001407 }
1408 return err;
1409}
1410
Mathias Agopianca4d3602011-05-19 15:38:14 -07001411status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001412{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001413 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001414 status_t err = NO_ERROR;
1415 sp<LayerBaseClient> l(layer.promote());
1416 if (l != NULL) {
1417 Mutex::Autolock _l(mStateLock);
1418 err = removeLayer_l(l);
1419 if (err == NAME_NOT_FOUND) {
1420 // The surface wasn't in the current list, which means it was
1421 // removed already, which means it is in the purgatory,
1422 // and need to be removed from there.
1423 ssize_t idx = mLayerPurgatory.remove(l);
1424 LOGE_IF(idx < 0,
1425 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001426 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001427 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1428 "error removing layer=%p (%s)", l.get(), strerror(-err));
1429 }
1430 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431}
1432
Mathias Agopian698c0872011-06-28 19:09:31 -07001433uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001434 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001435 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001438 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1439 if (layer != 0) {
1440 const uint32_t what = s.what;
1441 if (what & ePositionChanged) {
1442 if (layer->setPosition(s.x, s.y))
1443 flags |= eTraversalNeeded;
1444 }
1445 if (what & eLayerChanged) {
1446 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1447 if (layer->setLayer(s.z)) {
1448 mCurrentState.layersSortedByZ.removeAt(idx);
1449 mCurrentState.layersSortedByZ.add(layer);
1450 // we need traversal (state changed)
1451 // AND transaction (list changed)
1452 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 }
1454 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001455 if (what & eSizeChanged) {
1456 if (layer->setSize(s.w, s.h)) {
1457 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001458 }
1459 }
1460 if (what & eAlphaChanged) {
1461 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1462 flags |= eTraversalNeeded;
1463 }
1464 if (what & eMatrixChanged) {
1465 if (layer->setMatrix(s.matrix))
1466 flags |= eTraversalNeeded;
1467 }
1468 if (what & eTransparentRegionChanged) {
1469 if (layer->setTransparentRegionHint(s.transparentRegion))
1470 flags |= eTraversalNeeded;
1471 }
1472 if (what & eVisibilityChanged) {
1473 if (layer->setFlags(s.flags, s.mask))
1474 flags |= eTraversalNeeded;
1475 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001477 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478}
1479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480void SurfaceFlinger::screenReleased(int dpy)
1481{
1482 // this may be called by a signal handler, we can't do too much in here
1483 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1484 signalEvent();
1485}
1486
1487void SurfaceFlinger::screenAcquired(int dpy)
1488{
1489 // this may be called by a signal handler, we can't do too much in here
1490 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1491 signalEvent();
1492}
1493
1494status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1495{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001496 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 char buffer[SIZE];
1498 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001499
1500 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 snprintf(buffer, SIZE, "Permission Denial: "
1502 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1503 IPCThreadState::self()->getCallingPid(),
1504 IPCThreadState::self()->getCallingUid());
1505 result.append(buffer);
1506 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001507
1508 // figure out if we're stuck somewhere
1509 const nsecs_t now = systemTime();
1510 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1511 const nsecs_t inTransaction(mDebugInTransaction);
1512 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1513 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1514
1515 // Try to get the main lock, but don't insist if we can't
1516 // (this would indicate SF is stuck, but we want to be able to
1517 // print something in dumpsys).
1518 int retry = 3;
1519 while (mStateLock.tryLock()<0 && --retry>=0) {
1520 usleep(1000000);
1521 }
1522 const bool locked(retry >= 0);
1523 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001524 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001525 "SurfaceFlinger appears to be unresponsive, "
1526 "dumping anyways (no locks held)\n");
1527 result.append(buffer);
1528 }
1529
Mathias Agopian48b888a2011-01-19 16:15:53 -08001530 /*
1531 * Dump the visible layer list
1532 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1534 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001535 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1536 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001538 const sp<LayerBase>& layer(currentLayers[i]);
1539 layer->dump(result, buffer, SIZE);
1540 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 s.transparentRegion.dump(result, "transparentRegion");
1542 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1543 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1544 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001545
Mathias Agopian48b888a2011-01-19 16:15:53 -08001546 /*
1547 * Dump the layers in the purgatory
1548 */
1549
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001550 const size_t purgatorySize = mLayerPurgatory.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001551 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1552 result.append(buffer);
1553 for (size_t i=0 ; i<purgatorySize ; i++) {
1554 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1555 layer->shortDump(result, buffer, SIZE);
1556 }
1557
1558 /*
1559 * Dump SurfaceFlinger global state
1560 */
1561
Mathias Agopianad701862011-07-14 18:01:49 -07001562 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001563 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001564
1565 const GLExtensions& extensions(GLExtensions::getInstance());
1566 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1567 extensions.getVendor(),
1568 extensions.getRenderer(),
1569 extensions.getVersion());
1570 result.append(buffer);
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001571
1572 snprintf(buffer, SIZE, "EGL : %s\n",
1573 eglQueryString(graphicPlane(0).getEGLDisplay(),
1574 EGL_VERSION_HW_ANDROID));
1575 result.append(buffer);
1576
Mathias Agopianad701862011-07-14 18:01:49 -07001577 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1578 result.append(buffer);
1579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 mWormholeRegion.dump(result, "WormholeRegion");
1581 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1582 snprintf(buffer, SIZE,
Jamie Gennisa402c4c2011-10-14 16:44:08 -07001583 " orientation=%d, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 mCurrentState.orientation, hw.canDraw());
1585 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001586 snprintf(buffer, SIZE,
1587 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001588 " last transaction time : %f us\n"
1589 " refresh-rate : %f fps\n"
1590 " x-dpi : %f\n"
1591 " y-dpi : %f\n",
1592 mLastSwapBufferTime/1000.0,
1593 mLastTransactionTime/1000.0,
1594 hw.getRefreshRate(),
1595 hw.getDpiX(),
1596 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001597 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001598
Mathias Agopian9795c422009-08-26 16:36:26 -07001599 if (inSwapBuffersDuration || !locked) {
1600 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1601 inSwapBuffersDuration/1000.0);
1602 result.append(buffer);
1603 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001604
Mathias Agopian9795c422009-08-26 16:36:26 -07001605 if (inTransactionDuration || !locked) {
1606 snprintf(buffer, SIZE, " transaction time: %f us\n",
1607 inTransactionDuration/1000.0);
1608 result.append(buffer);
1609 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001610
Mathias Agopian48b888a2011-01-19 16:15:53 -08001611 /*
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001612 * VSYNC state
1613 */
1614 mEventThread->dump(result, buffer, SIZE);
1615
1616 /*
Mathias Agopian48b888a2011-01-19 16:15:53 -08001617 * Dump HWComposer state
1618 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001619 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001620 snprintf(buffer, SIZE, "h/w composer state:\n");
1621 result.append(buffer);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001622 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1623 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001624 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001625 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001626 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001627
Mathias Agopian48b888a2011-01-19 16:15:53 -08001628 /*
1629 * Dump gralloc state
1630 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001631 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001632 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001633 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001634
1635 if (locked) {
1636 mStateLock.unlock();
1637 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
1639 write(fd, result.string(), result.size());
1640 return NO_ERROR;
1641}
1642
1643status_t SurfaceFlinger::onTransact(
1644 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1645{
1646 switch (code) {
1647 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001648 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001651 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001652 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 {
1654 // codes that require permission check
1655 IPCThreadState* ipc = IPCThreadState::self();
1656 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001657 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001658 if ((uid != AID_GRAPHICS) &&
1659 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001660 LOGE("Permission Denial: "
1661 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1662 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001664 break;
1665 }
1666 case CAPTURE_SCREEN:
1667 {
1668 // codes that require permission check
1669 IPCThreadState* ipc = IPCThreadState::self();
1670 const int pid = ipc->getCallingPid();
1671 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001672 if ((uid != AID_GRAPHICS) &&
1673 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001674 LOGE("Permission Denial: "
1675 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1676 return PERMISSION_DENIED;
1677 }
1678 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 }
1680 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1683 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001684 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001685 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001686 IPCThreadState* ipc = IPCThreadState::self();
1687 const int pid = ipc->getCallingPid();
1688 const int uid = ipc->getCallingUid();
1689 LOGE("Permission Denial: "
1690 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 return PERMISSION_DENIED;
1692 }
1693 int n;
1694 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001695 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001696 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 return NO_ERROR;
1698 case 1002: // SHOW_UPDATES
1699 n = data.readInt32();
1700 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001701 invalidateHwcGeometry();
1702 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 return NO_ERROR;
1704 case 1003: // SHOW_BACKGROUND
1705 n = data.readInt32();
1706 mDebugBackground = n ? 1 : 0;
1707 return NO_ERROR;
1708 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001709 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001710 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001712 case 1005:{ // force transaction
1713 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1714 return NO_ERROR;
1715 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001716 case 1006:{ // enable/disable GraphicLog
1717 int enabled = data.readInt32();
1718 GraphicLog::getInstance().setEnabled(enabled);
1719 return NO_ERROR;
1720 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001721 case 1008: // toggle use of hw composer
1722 n = data.readInt32();
1723 mDebugDisableHWC = n ? 1 : 0;
1724 invalidateHwcGeometry();
1725 repaintEverything();
1726 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001727 case 1009: // toggle use of transform hint
1728 n = data.readInt32();
1729 mDebugDisableTransformHint = n ? 1 : 0;
1730 invalidateHwcGeometry();
1731 repaintEverything();
1732 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001734 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 reply->writeInt32(0);
1736 reply->writeInt32(mDebugRegion);
1737 reply->writeInt32(mDebugBackground);
1738 return NO_ERROR;
1739 case 1013: {
1740 Mutex::Autolock _l(mStateLock);
1741 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1742 reply->writeInt32(hw.getPageFlipCount());
1743 }
1744 return NO_ERROR;
1745 }
1746 }
1747 return err;
1748}
1749
Mathias Agopian53331da2011-08-22 21:44:41 -07001750void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001751 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001752 const Rect bounds(hw.getBounds());
1753 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001754 signalEvent();
1755}
1756
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001757void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1758 Mutex::Autolock _l(mInvalidateLock);
1759 mInvalidateRegion = reg;
1760}
1761
1762Region SurfaceFlinger::getAndClearInvalidateRegion() {
1763 Mutex::Autolock _l(mInvalidateLock);
1764 Region reg(mInvalidateRegion);
1765 mInvalidateRegion.clear();
1766 return reg;
1767}
1768
Mathias Agopian59119e62010-10-11 12:37:43 -07001769// ---------------------------------------------------------------------------
1770
Mathias Agopian118d0242011-10-13 16:02:48 -07001771status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1772 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1773{
1774 Mutex::Autolock _l(mStateLock);
1775 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1776}
1777
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001778status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1779 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001780{
Mathias Agopian59119e62010-10-11 12:37:43 -07001781 if (!GLExtensions::getInstance().haveFramebufferObject())
1782 return INVALID_OPERATION;
1783
1784 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001785 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001786 const uint32_t hw_w = hw.getWidth();
1787 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001788 GLfloat u = 1;
1789 GLfloat v = 1;
1790
1791 // make sure to clear all GL error flags
1792 while ( glGetError() != GL_NO_ERROR ) ;
1793
1794 // create a FBO
1795 GLuint name, tname;
1796 glGenTextures(1, &tname);
1797 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001798 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1799 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001800 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001801 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001802 GLint tw = (2 << (31 - clz(hw_w)));
1803 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001804 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1805 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001806 u = GLfloat(hw_w) / tw;
1807 v = GLfloat(hw_h) / th;
1808 }
1809 glGenFramebuffersOES(1, &name);
1810 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001811 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1812 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001813
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001814 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001815 glDisable(GL_TEXTURE_EXTERNAL_OES);
1816 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001817 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 glClearColor(0,0,0,1);
1819 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001820 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001821 glMatrixMode(GL_MODELVIEW);
1822 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001823 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1824 const size_t count = layers.size();
1825 for (size_t i=0 ; i<count ; ++i) {
1826 const sp<LayerBase>& layer(layers[i]);
1827 layer->drawForSreenShot();
1828 }
1829
Mathias Agopian118d0242011-10-13 16:02:48 -07001830 hw.compositionComplete();
1831
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001832 // back to main framebuffer
1833 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1834 glDisable(GL_SCISSOR_TEST);
1835 glDeleteFramebuffersOES(1, &name);
1836
1837 *textureName = tname;
1838 *uOut = u;
1839 *vOut = v;
1840 return NO_ERROR;
1841}
1842
1843// ---------------------------------------------------------------------------
1844
1845status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1846{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001847 // get screen geometry
1848 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1849 const uint32_t hw_w = hw.getWidth();
1850 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001851 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001852
1853 GLfloat u, v;
1854 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001855 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001856 if (result != NO_ERROR) {
1857 return result;
1858 }
1859
1860 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001861 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001862 glBindTexture(GL_TEXTURE_2D, tname);
1863 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1864 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1865 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1866 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1867 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1868 glVertexPointer(2, GL_FLOAT, 0, vtx);
1869
Mathias Agopianffcf4652011-07-07 17:30:31 -07001870 /*
1871 * Texture coordinate mapping
1872 *
1873 * u
1874 * 1 +----------+---+
1875 * | | | | image is inverted
1876 * | V | | w.r.t. the texture
1877 * 1-v +----------+ | coordinates
1878 * | |
1879 * | |
1880 * | |
1881 * 0 +--------------+
1882 * 0 1
1883 *
1884 */
1885
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001886 class s_curve_interpolator {
1887 const float nbFrames, s, v;
1888 public:
1889 s_curve_interpolator(int nbFrames, float s)
1890 : nbFrames(1.0f / (nbFrames-1)), s(s),
1891 v(1.0f + expf(-s + 0.5f*s)) {
1892 }
1893 float operator()(int f) {
1894 const float x = f * nbFrames;
1895 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1896 }
1897 };
1898
1899 class v_stretch {
1900 const GLfloat hw_w, hw_h;
1901 public:
1902 v_stretch(uint32_t hw_w, uint32_t hw_h)
1903 : hw_w(hw_w), hw_h(hw_h) {
1904 }
1905 void operator()(GLfloat* vtx, float v) {
1906 const GLfloat w = hw_w + (hw_w * v);
1907 const GLfloat h = hw_h - (hw_h * v);
1908 const GLfloat x = (hw_w - w) * 0.5f;
1909 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001910 vtx[0] = x; vtx[1] = y;
1911 vtx[2] = x; vtx[3] = y + h;
1912 vtx[4] = x + w; vtx[5] = y + h;
1913 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001914 }
1915 };
1916
1917 class h_stretch {
1918 const GLfloat hw_w, hw_h;
1919 public:
1920 h_stretch(uint32_t hw_w, uint32_t hw_h)
1921 : hw_w(hw_w), hw_h(hw_h) {
1922 }
1923 void operator()(GLfloat* vtx, float v) {
1924 const GLfloat w = hw_w - (hw_w * v);
1925 const GLfloat h = 1.0f;
1926 const GLfloat x = (hw_w - w) * 0.5f;
1927 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001928 vtx[0] = x; vtx[1] = y;
1929 vtx[2] = x; vtx[3] = y + h;
1930 vtx[4] = x + w; vtx[5] = y + h;
1931 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001932 }
1933 };
1934
1935 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001936 char value[PROPERTY_VALUE_MAX];
1937 property_get("debug.sf.electron_frames", value, "24");
1938 int nbFrames = (atoi(value) + 1) >> 1;
1939 if (nbFrames <= 0) // just in case
1940 nbFrames = 24;
1941
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001942 s_curve_interpolator itr(nbFrames, 7.5f);
1943 s_curve_interpolator itg(nbFrames, 8.0f);
1944 s_curve_interpolator itb(nbFrames, 8.5f);
1945
1946 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001947
1948 glMatrixMode(GL_TEXTURE);
1949 glLoadIdentity();
1950 glMatrixMode(GL_MODELVIEW);
1951 glLoadIdentity();
1952
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001953 glEnable(GL_BLEND);
1954 glBlendFunc(GL_ONE, GL_ONE);
1955 for (int i=0 ; i<nbFrames ; i++) {
1956 float x, y, w, h;
1957 const float vr = itr(i);
1958 const float vg = itg(i);
1959 const float vb = itb(i);
1960
1961 // clear screen
1962 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001963 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001964 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001965
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001966 // draw the red plane
1967 vverts(vtx, vr);
1968 glColorMask(1,0,0,1);
1969 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001970
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 // draw the green plane
1972 vverts(vtx, vg);
1973 glColorMask(0,1,0,1);
1974 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001975
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001976 // draw the blue plane
1977 vverts(vtx, vb);
1978 glColorMask(0,0,1,1);
1979 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001980
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001981 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001982 glDisable(GL_TEXTURE_2D);
1983 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001984 glColor4f(vg, vg, vg, 1);
1985 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1986 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001987 }
1988
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001989 h_stretch hverts(hw_w, hw_h);
1990 glDisable(GL_BLEND);
1991 glDisable(GL_TEXTURE_2D);
1992 glColorMask(1,1,1,1);
1993 for (int i=0 ; i<nbFrames ; i++) {
1994 const float v = itg(i);
1995 hverts(vtx, v);
1996 glClear(GL_COLOR_BUFFER_BIT);
1997 glColor4f(1-v, 1-v, 1-v, 1);
1998 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1999 hw.flip(screenBounds);
2000 }
2001
2002 glColorMask(1,1,1,1);
2003 glEnable(GL_SCISSOR_TEST);
2004 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2005 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002006 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002007 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002008 return NO_ERROR;
2009}
2010
2011status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2012{
2013 status_t result = PERMISSION_DENIED;
2014
2015 if (!GLExtensions::getInstance().haveFramebufferObject())
2016 return INVALID_OPERATION;
2017
2018
2019 // get screen geometry
2020 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2021 const uint32_t hw_w = hw.getWidth();
2022 const uint32_t hw_h = hw.getHeight();
2023 const Region screenBounds(hw.bounds());
2024
2025 GLfloat u, v;
2026 GLuint tname;
2027 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2028 if (result != NO_ERROR) {
2029 return result;
2030 }
2031
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002032 GLfloat vtx[8];
2033 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002034 glBindTexture(GL_TEXTURE_2D, tname);
2035 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2036 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2037 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2038 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2039 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2040 glVertexPointer(2, GL_FLOAT, 0, vtx);
2041
2042 class s_curve_interpolator {
2043 const float nbFrames, s, v;
2044 public:
2045 s_curve_interpolator(int nbFrames, float s)
2046 : nbFrames(1.0f / (nbFrames-1)), s(s),
2047 v(1.0f + expf(-s + 0.5f*s)) {
2048 }
2049 float operator()(int f) {
2050 const float x = f * nbFrames;
2051 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2052 }
2053 };
2054
2055 class v_stretch {
2056 const GLfloat hw_w, hw_h;
2057 public:
2058 v_stretch(uint32_t hw_w, uint32_t hw_h)
2059 : hw_w(hw_w), hw_h(hw_h) {
2060 }
2061 void operator()(GLfloat* vtx, float v) {
2062 const GLfloat w = hw_w + (hw_w * v);
2063 const GLfloat h = hw_h - (hw_h * v);
2064 const GLfloat x = (hw_w - w) * 0.5f;
2065 const GLfloat y = (hw_h - h) * 0.5f;
2066 vtx[0] = x; vtx[1] = y;
2067 vtx[2] = x; vtx[3] = y + h;
2068 vtx[4] = x + w; vtx[5] = y + h;
2069 vtx[6] = x + w; vtx[7] = y;
2070 }
2071 };
2072
2073 class h_stretch {
2074 const GLfloat hw_w, hw_h;
2075 public:
2076 h_stretch(uint32_t hw_w, uint32_t hw_h)
2077 : hw_w(hw_w), hw_h(hw_h) {
2078 }
2079 void operator()(GLfloat* vtx, float v) {
2080 const GLfloat w = hw_w - (hw_w * v);
2081 const GLfloat h = 1.0f;
2082 const GLfloat x = (hw_w - w) * 0.5f;
2083 const GLfloat y = (hw_h - h) * 0.5f;
2084 vtx[0] = x; vtx[1] = y;
2085 vtx[2] = x; vtx[3] = y + h;
2086 vtx[4] = x + w; vtx[5] = y + h;
2087 vtx[6] = x + w; vtx[7] = y;
2088 }
2089 };
2090
Mathias Agopiana6546e52010-10-14 12:33:07 -07002091 // the full animation is 12 frames
2092 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002093 s_curve_interpolator itr(nbFrames, 7.5f);
2094 s_curve_interpolator itg(nbFrames, 8.0f);
2095 s_curve_interpolator itb(nbFrames, 8.5f);
2096
2097 h_stretch hverts(hw_w, hw_h);
2098 glDisable(GL_BLEND);
2099 glDisable(GL_TEXTURE_2D);
2100 glColorMask(1,1,1,1);
2101 for (int i=nbFrames-1 ; i>=0 ; i--) {
2102 const float v = itg(i);
2103 hverts(vtx, v);
2104 glClear(GL_COLOR_BUFFER_BIT);
2105 glColor4f(1-v, 1-v, 1-v, 1);
2106 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2107 hw.flip(screenBounds);
2108 }
2109
Mathias Agopiana6546e52010-10-14 12:33:07 -07002110 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002111 v_stretch vverts(hw_w, hw_h);
2112 glEnable(GL_BLEND);
2113 glBlendFunc(GL_ONE, GL_ONE);
2114 for (int i=nbFrames-1 ; i>=0 ; i--) {
2115 float x, y, w, h;
2116 const float vr = itr(i);
2117 const float vg = itg(i);
2118 const float vb = itb(i);
2119
2120 // clear screen
2121 glColorMask(1,1,1,1);
2122 glClear(GL_COLOR_BUFFER_BIT);
2123 glEnable(GL_TEXTURE_2D);
2124
2125 // draw the red plane
2126 vverts(vtx, vr);
2127 glColorMask(1,0,0,1);
2128 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2129
2130 // draw the green plane
2131 vverts(vtx, vg);
2132 glColorMask(0,1,0,1);
2133 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2134
2135 // draw the blue plane
2136 vverts(vtx, vb);
2137 glColorMask(0,0,1,1);
2138 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2139
2140 hw.flip(screenBounds);
2141 }
2142
2143 glColorMask(1,1,1,1);
2144 glEnable(GL_SCISSOR_TEST);
2145 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002146 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002147 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002148 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002149
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002150 return NO_ERROR;
2151}
2152
2153// ---------------------------------------------------------------------------
2154
Mathias Agopianabd671a2010-10-14 14:54:06 -07002155status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002156{
2157 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2158 if (!hw.canDraw()) {
2159 // we're already off
2160 return NO_ERROR;
2161 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002162
2163 // turn off hwc while we're doing the animation
2164 hw.getHwComposer().disable();
2165 // and make sure to turn it back on (if needed) next time we compose
2166 invalidateHwcGeometry();
2167
Mathias Agopianabd671a2010-10-14 14:54:06 -07002168 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2169 electronBeamOffAnimationImplLocked();
2170 }
2171
2172 // always clear the whole screen at the end of the animation
2173 glClearColor(0,0,0,1);
2174 glDisable(GL_SCISSOR_TEST);
2175 glClear(GL_COLOR_BUFFER_BIT);
2176 glEnable(GL_SCISSOR_TEST);
2177 hw.flip( Region(hw.bounds()) );
2178
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002179 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002180}
2181
2182status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2183{
Mathias Agopian59119e62010-10-11 12:37:43 -07002184 class MessageTurnElectronBeamOff : public MessageBase {
2185 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002186 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002187 status_t result;
2188 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002189 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2190 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002191 }
2192 status_t getResult() const {
2193 return result;
2194 }
2195 virtual bool handler() {
2196 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002197 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002198 return true;
2199 }
2200 };
2201
Mathias Agopianabd671a2010-10-14 14:54:06 -07002202 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002203 status_t res = postMessageSync(msg);
2204 if (res == NO_ERROR) {
2205 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206
2207 // work-around: when the power-manager calls us we activate the
2208 // animation. eventually, the "on" animation will be called
2209 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002210 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002211 }
2212 return res;
2213}
2214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002215// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002216
Mathias Agopianabd671a2010-10-14 14:54:06 -07002217status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002218{
2219 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2220 if (hw.canDraw()) {
2221 // we're already on
2222 return NO_ERROR;
2223 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002224 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2225 electronBeamOnAnimationImplLocked();
2226 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002227
2228 // make sure to redraw the whole screen when the animation is done
2229 mDirtyRegion.set(hw.bounds());
2230 signalEvent();
2231
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002232 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002233}
2234
2235status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2236{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237 class MessageTurnElectronBeamOn : public MessageBase {
2238 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002239 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002240 status_t result;
2241 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002242 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2243 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244 }
2245 status_t getResult() const {
2246 return result;
2247 }
2248 virtual bool handler() {
2249 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002250 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002251 return true;
2252 }
2253 };
2254
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002256 return NO_ERROR;
2257}
2258
2259// ---------------------------------------------------------------------------
2260
Mathias Agopian74c40c02010-09-29 13:02:36 -07002261status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2262 sp<IMemoryHeap>* heap,
2263 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002264 uint32_t sw, uint32_t sh,
2265 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002266{
2267 status_t result = PERMISSION_DENIED;
2268
2269 // only one display supported for now
2270 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2271 return BAD_VALUE;
2272
2273 if (!GLExtensions::getInstance().haveFramebufferObject())
2274 return INVALID_OPERATION;
2275
2276 // get screen geometry
2277 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2278 const uint32_t hw_w = hw.getWidth();
2279 const uint32_t hw_h = hw.getHeight();
2280
2281 if ((sw > hw_w) || (sh > hw_h))
2282 return BAD_VALUE;
2283
2284 sw = (!sw) ? hw_w : sw;
2285 sh = (!sh) ? hw_h : sh;
2286 const size_t size = sw * sh * 4;
2287
Mathias Agopian1c71a472011-03-02 18:45:50 -08002288 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2289 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002290
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291 // make sure to clear all GL error flags
2292 while ( glGetError() != GL_NO_ERROR ) ;
2293
2294 // create a FBO
2295 GLuint name, tname;
2296 glGenRenderbuffersOES(1, &tname);
2297 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2298 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2299 glGenFramebuffersOES(1, &name);
2300 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2301 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2302 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2303
2304 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002305
Mathias Agopian74c40c02010-09-29 13:02:36 -07002306 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2307
2308 // invert everything, b/c glReadPixel() below will invert the FB
2309 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002310 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002311 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002312 glMatrixMode(GL_PROJECTION);
2313 glPushMatrix();
2314 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002315 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002316 glMatrixMode(GL_MODELVIEW);
2317
2318 // redraw the screen entirely...
2319 glClearColor(0,0,0,1);
2320 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002321
Jamie Gennis9575f602011-10-07 14:51:16 -07002322 const LayerVector& layers(mDrawingState.layersSortedByZ);
2323 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324 for (size_t i=0 ; i<count ; ++i) {
2325 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002326 const uint32_t flags = layer->drawingState().flags;
2327 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2328 const uint32_t z = layer->drawingState().z;
2329 if (z >= minLayerZ && z <= maxLayerZ) {
2330 layer->drawForSreenShot();
2331 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002332 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002333 }
2334
2335 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002336 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 glScissor(0, 0, sw, sh);
2338
2339 // check for errors and return screen capture
2340 if (glGetError() != GL_NO_ERROR) {
2341 // error while rendering
2342 result = INVALID_OPERATION;
2343 } else {
2344 // allocate shared memory large enough to hold the
2345 // screen capture
2346 sp<MemoryHeapBase> base(
2347 new MemoryHeapBase(size, 0, "screen-capture") );
2348 void* const ptr = base->getBase();
2349 if (ptr) {
2350 // capture the screen with glReadPixels()
2351 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2352 if (glGetError() == GL_NO_ERROR) {
2353 *heap = base;
2354 *w = sw;
2355 *h = sh;
2356 *f = PIXEL_FORMAT_RGBA_8888;
2357 result = NO_ERROR;
2358 }
2359 } else {
2360 result = NO_MEMORY;
2361 }
2362 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002363 glEnable(GL_SCISSOR_TEST);
2364 glViewport(0, 0, hw_w, hw_h);
2365 glMatrixMode(GL_PROJECTION);
2366 glPopMatrix();
2367 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002368 } else {
2369 result = BAD_VALUE;
2370 }
2371
2372 // release FBO resources
2373 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2374 glDeleteRenderbuffersOES(1, &tname);
2375 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002376
2377 hw.compositionComplete();
2378
Mathias Agopian1c71a472011-03-02 18:45:50 -08002379 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002380
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 return result;
2382}
2383
2384
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002385status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2386 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002387 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002388 uint32_t sw, uint32_t sh,
2389 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002390{
2391 // only one display supported for now
2392 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2393 return BAD_VALUE;
2394
2395 if (!GLExtensions::getInstance().haveFramebufferObject())
2396 return INVALID_OPERATION;
2397
2398 class MessageCaptureScreen : public MessageBase {
2399 SurfaceFlinger* flinger;
2400 DisplayID dpy;
2401 sp<IMemoryHeap>* heap;
2402 uint32_t* w;
2403 uint32_t* h;
2404 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 uint32_t sw;
2406 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 uint32_t minLayerZ;
2408 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002409 status_t result;
2410 public:
2411 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002413 uint32_t sw, uint32_t sh,
2414 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002415 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002416 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2417 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2418 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002419 {
2420 }
2421 status_t getResult() const {
2422 return result;
2423 }
2424 virtual bool handler() {
2425 Mutex::Autolock _l(flinger->mStateLock);
2426
2427 // if we have secure windows, never allow the screen capture
2428 if (flinger->mSecureFrameBuffer)
2429 return true;
2430
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002432 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002433
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002434 return true;
2435 }
2436 };
2437
2438 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002439 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440 status_t res = postMessageSync(msg);
2441 if (res == NO_ERROR) {
2442 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2443 }
2444 return res;
2445}
2446
2447// ---------------------------------------------------------------------------
2448
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002449sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2450{
2451 sp<Layer> result;
2452 Mutex::Autolock _l(mStateLock);
2453 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2454 return result;
2455}
2456
2457// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002458
Mathias Agopian96f08192010-06-02 23:28:45 -07002459Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002460 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002461{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002462}
2463
Mathias Agopian96f08192010-06-02 23:28:45 -07002464Client::~Client()
2465{
Mathias Agopian96f08192010-06-02 23:28:45 -07002466 const size_t count = mLayers.size();
2467 for (size_t i=0 ; i<count ; i++) {
2468 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2469 if (layer != 0) {
2470 mFlinger->removeLayer(layer);
2471 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472 }
2473}
2474
Mathias Agopian96f08192010-06-02 23:28:45 -07002475status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002476 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002478
Mathias Agopian4f113742011-05-03 16:21:41 -07002479size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002480{
Mathias Agopian4f113742011-05-03 16:21:41 -07002481 Mutex::Autolock _l(mLock);
2482 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002483 mLayers.add(name, layer);
2484 return name;
2485}
2486
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002487void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002488{
Mathias Agopian4f113742011-05-03 16:21:41 -07002489 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002490 // we do a linear search here, because this doesn't happen often
2491 const size_t count = mLayers.size();
2492 for (size_t i=0 ; i<count ; i++) {
2493 if (mLayers.valueAt(i) == layer) {
2494 mLayers.removeItemsAt(i, 1);
2495 break;
2496 }
2497 }
2498}
Mathias Agopian4f113742011-05-03 16:21:41 -07002499sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2500{
2501 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002502 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002503 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002504 if (layer != 0) {
2505 lbc = layer.promote();
2506 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002507 }
2508 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002509}
2510
Mathias Agopiana67932f2011-04-20 14:20:59 -07002511
2512status_t Client::onTransact(
2513 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2514{
2515 // these must be checked
2516 IPCThreadState* ipc = IPCThreadState::self();
2517 const int pid = ipc->getCallingPid();
2518 const int uid = ipc->getCallingUid();
2519 const int self_pid = getpid();
2520 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2521 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002522 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002523 {
2524 LOGE("Permission Denial: "
2525 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2526 return PERMISSION_DENIED;
2527 }
2528 }
2529 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002530}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002531
2532
Mathias Agopian96f08192010-06-02 23:28:45 -07002533sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002534 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002535 const String8& name,
2536 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537 uint32_t flags)
2538{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002539 /*
2540 * createSurface must be called from the GL thread so that it can
2541 * have access to the GL context.
2542 */
2543
2544 class MessageCreateSurface : public MessageBase {
2545 sp<ISurface> result;
2546 SurfaceFlinger* flinger;
2547 ISurfaceComposerClient::surface_data_t* params;
2548 Client* client;
2549 const String8& name;
2550 DisplayID display;
2551 uint32_t w, h;
2552 PixelFormat format;
2553 uint32_t flags;
2554 public:
2555 MessageCreateSurface(SurfaceFlinger* flinger,
2556 ISurfaceComposerClient::surface_data_t* params,
2557 const String8& name, Client* client,
2558 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2559 uint32_t flags)
2560 : flinger(flinger), params(params), client(client), name(name),
2561 display(display), w(w), h(h), format(format), flags(flags)
2562 {
2563 }
2564 sp<ISurface> getResult() const { return result; }
2565 virtual bool handler() {
2566 result = flinger->createSurface(params, name, client,
2567 display, w, h, format, flags);
2568 return true;
2569 }
2570 };
2571
2572 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2573 params, name, this, display, w, h, format, flags);
2574 mFlinger->postMessageSync(msg);
2575 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002576}
Mathias Agopian96f08192010-06-02 23:28:45 -07002577status_t Client::destroySurface(SurfaceID sid) {
2578 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002579}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002580
2581// ---------------------------------------------------------------------------
2582
Jamie Gennis9a78c902011-01-12 18:30:40 -08002583GraphicBufferAlloc::GraphicBufferAlloc() {}
2584
2585GraphicBufferAlloc::~GraphicBufferAlloc() {}
2586
2587sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002588 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002589 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2590 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002591 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002592 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002593 if (err == NO_MEMORY) {
2594 GraphicBuffer::dumpAllocationsToSystemLog();
2595 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002596 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2597 "failed (%s), handle=%p",
2598 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002599 return 0;
2600 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002601 return graphicBuffer;
2602}
2603
Jamie Gennis9a78c902011-01-12 18:30:40 -08002604// ---------------------------------------------------------------------------
2605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002606GraphicPlane::GraphicPlane()
2607 : mHw(0)
2608{
2609}
2610
2611GraphicPlane::~GraphicPlane() {
2612 delete mHw;
2613}
2614
2615bool GraphicPlane::initialized() const {
2616 return mHw ? true : false;
2617}
2618
Mathias Agopian2b92d892010-02-08 15:49:35 -08002619int GraphicPlane::getWidth() const {
2620 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002621}
2622
Mathias Agopian2b92d892010-02-08 15:49:35 -08002623int GraphicPlane::getHeight() const {
2624 return mHeight;
2625}
2626
2627void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2628{
2629 mHw = hw;
2630
2631 // initialize the display orientation transform.
2632 // it's a constant that should come from the display driver.
2633 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2634 char property[PROPERTY_VALUE_MAX];
2635 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2636 //displayOrientation
2637 switch (atoi(property)) {
2638 case 90:
2639 displayOrientation = ISurfaceComposer::eOrientation90;
2640 break;
2641 case 270:
2642 displayOrientation = ISurfaceComposer::eOrientation270;
2643 break;
2644 }
2645 }
2646
2647 const float w = hw->getWidth();
2648 const float h = hw->getHeight();
2649 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2650 &mDisplayTransform);
2651 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2652 mDisplayWidth = h;
2653 mDisplayHeight = w;
2654 } else {
2655 mDisplayWidth = w;
2656 mDisplayHeight = h;
2657 }
2658
2659 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002660}
2661
2662status_t GraphicPlane::orientationToTransfrom(
2663 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002664{
2665 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002666 switch (orientation) {
2667 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002668 flags = Transform::ROT_0;
2669 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002670 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002671 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002672 break;
2673 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002674 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675 break;
2676 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002677 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678 break;
2679 default:
2680 return BAD_VALUE;
2681 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002682 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002683 return NO_ERROR;
2684}
2685
2686status_t GraphicPlane::setOrientation(int orientation)
2687{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688 // If the rotation can be handled in hardware, this is where
2689 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002690
2691 const DisplayHardware& hw(displayHardware());
2692 const float w = mDisplayWidth;
2693 const float h = mDisplayHeight;
2694 mWidth = int(w);
2695 mHeight = int(h);
2696
2697 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002698 GraphicPlane::orientationToTransfrom(orientation, w, h,
2699 &orientationTransform);
2700 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2701 mWidth = int(h);
2702 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002703 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002704
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002705 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002706 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002707 return NO_ERROR;
2708}
2709
2710const DisplayHardware& GraphicPlane::displayHardware() const {
2711 return *mHw;
2712}
2713
Mathias Agopian59119e62010-10-11 12:37:43 -07002714DisplayHardware& GraphicPlane::editDisplayHardware() {
2715 return *mHw;
2716}
2717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002718const Transform& GraphicPlane::transform() const {
2719 return mGlobalTransform;
2720}
2721
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002722EGLDisplay GraphicPlane::getEGLDisplay() const {
2723 return mHw->getEGLDisplay();
2724}
2725
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726// ---------------------------------------------------------------------------
2727
2728}; // namespace android