blob: 3c48b7e270cb4ee7aa26444ee6bebc685b4face6 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopiana67932f2011-04-20 14:20:59 -070058#include <private/surfaceflinger/SharedBufferStack.h>
59
Mathias Agopiana1ecca92009-05-21 19:21:59 -070060/* ideally AID_GRAPHICS would be in a semi-public header
61 * or there would be a way to map a user/group name to its id
62 */
63#ifndef AID_GRAPHICS
64#define AID_GRAPHICS 1003
65#endif
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian99b49842011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070082 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070090 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700100 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mConsoleSignals(0),
102 mSecureFrameBuffer(0)
103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
109 LOGI("SurfaceFlinger is starting");
110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 property_get("debug.sf.showbackground", value, "0");
118 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120 property_get("debug.sf.ddms", value, "0");
121 mDebugDDMS = atoi(value);
122 if (mDebugDDMS) {
123 DdmConnection::start(getServiceName());
124 }
125
Mathias Agopian78fd5012010-04-20 14:51:04 -0700126 LOGI_IF(mDebugRegion, "showupdates enabled");
127 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
131SurfaceFlinger::~SurfaceFlinger()
132{
133 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700136sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700138 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian7e27f052010-05-28 14:22:23 -0700141sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142{
Mathias Agopian96f08192010-06-02 23:28:45 -0700143 sp<ISurfaceComposerClient> bclient;
144 sp<Client> client(new Client(this));
145 status_t err = client->initCheck();
146 if (err == NO_ERROR) {
147 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 return bclient;
150}
151
Jamie Gennis9a78c902011-01-12 18:30:40 -0800152sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
153{
154 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
155 return gba;
156}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
159{
160 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
161 const GraphicPlane& plane(mGraphicPlanes[dpy]);
162 return plane;
163}
164
165GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
166{
167 return const_cast<GraphicPlane&>(
168 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
169}
170
171void SurfaceFlinger::bootFinished()
172{
173 const nsecs_t now = systemTime();
174 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700175 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700176 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700177
178 // wait patiently for the window manager death
179 const String16 name("window");
180 sp<IBinder> window(defaultServiceManager()->getService(name));
181 if (window != 0) {
182 window->linkToDeath(this);
183 }
184
185 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700186 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700189void SurfaceFlinger::binderDied(const wp<IBinder>& who)
190{
191 // the window manager died on us. prepare its eulogy.
192
193 // unfreeze the screen in case it was... frozen
194 mFreezeDisplayTime = 0;
195 mFreezeCount = 0;
196 mFreezeDisplay = false;
197
198 // reset screen orientation
199 setOrientation(0, eOrientationDefault, 0);
200
201 // restart the boot-animation
202 property_set("ctl.start", "bootanim");
203}
204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205void SurfaceFlinger::onFirstRef()
206{
207 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
208
209 // Wait for the main thread to be done with its initialization
210 mReadyToRunBarrier.wait();
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213static inline uint16_t pack565(int r, int g, int b) {
214 return (r<<11)|(g<<5)|b;
215}
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217status_t SurfaceFlinger::readyToRun()
218{
219 LOGI( "SurfaceFlinger's main thread ready to run. "
220 "Initializing graphics H/W...");
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 // we only support one display currently
223 int dpy = 0;
224
225 {
226 // initialize the main display
227 GraphicPlane& plane(graphicPlane(dpy));
228 DisplayHardware* const hw = new DisplayHardware(this, dpy);
229 plane.setDisplayHardware(hw);
230 }
231
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700232 // create the shared control-block
233 mServerHeap = new MemoryHeapBase(4096,
234 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
235 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700236
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700237 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
238 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700239
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700240 new(mServerCblk) surface_flinger_cblk_t;
241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 // initialize primary screen
243 // (other display should be initialized in the same manner, but
244 // asynchronously, as they could come and go. None of this is supported
245 // yet).
246 const GraphicPlane& plane(graphicPlane(dpy));
247 const DisplayHardware& hw = plane.displayHardware();
248 const uint32_t w = hw.getWidth();
249 const uint32_t h = hw.getHeight();
250 const uint32_t f = hw.getFormat();
251 hw.makeCurrent();
252
253 // initialize the shared control block
254 mServerCblk->connected |= 1<<dpy;
255 display_cblk_t* dcblk = mServerCblk->displays + dpy;
256 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800257 dcblk->w = plane.getWidth();
258 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 dcblk->format = f;
260 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
261 dcblk->xdpi = hw.getDpiX();
262 dcblk->ydpi = hw.getDpiY();
263 dcblk->fps = hw.getRefreshRate();
264 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265
266 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700268 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 glEnableClientState(GL_VERTEX_ARRAY);
270 glEnable(GL_SCISSOR_TEST);
271 glShadeModel(GL_FLAT);
272 glDisable(GL_DITHER);
273 glDisable(GL_CULL_FACE);
274
275 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
276 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 glGenTextures(1, &mWormholeTexName);
279 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700285 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
286
287 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
288 glGenTextures(1, &mProtectedTexName);
289 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
293 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
295 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
297 glViewport(0, 0, w, h);
298 glMatrixMode(GL_PROJECTION);
299 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700300 // put the origin in the left-bottom corner
301 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 -0800302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 mReadyToRunBarrier.open();
304
305 /*
306 * We're now ready to accept clients...
307 */
308
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700309 // start boot animation
310 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
315// ----------------------------------------------------------------------------
316#if 0
317#pragma mark -
318#pragma mark Events Handler
319#endif
320
321void SurfaceFlinger::waitForEvent()
322{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700323 while (true) {
324 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800325 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700326 if (UNLIKELY(isFrozen())) {
327 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700328 const nsecs_t now = systemTime();
329 if (mFreezeDisplayTime == 0) {
330 mFreezeDisplayTime = now;
331 }
332 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
333 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700334 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700335
Mathias Agopianbb641242010-05-18 17:06:55 -0700336 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800337
338 // see if we timed out
339 if (isFrozen()) {
340 const nsecs_t now = systemTime();
341 nsecs_t frozenTime = (now - mFreezeDisplayTime);
342 if (frozenTime >= freezeDisplayTimeout) {
343 // we timed out and are still frozen
344 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
345 mFreezeDisplay, mFreezeCount);
346 mFreezeDisplayTime = 0;
347 mFreezeCount = 0;
348 mFreezeDisplay = false;
349 }
350 }
351
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700352 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700353 switch (msg->what) {
354 case MessageQueue::INVALIDATE:
355 // invalidate message, just return to the main loop
356 return;
357 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 }
360}
361
362void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700363 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364}
365
Jamie Gennis582270d2011-08-17 18:19:00 -0700366bool SurfaceFlinger::authenticateSurfaceTexture(
367 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800368 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700369 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800370
371 // Check the visible layer list for the ISurface
372 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
373 size_t count = currentLayers.size();
374 for (size_t i=0 ; i<count ; i++) {
375 const sp<LayerBase>& layer(currentLayers[i]);
376 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700377 if (lbc != NULL) {
378 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
379 if (lbcBinder == surfaceTextureBinder) {
380 return true;
381 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800382 }
383 }
384
385 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700386 // SurfaceTexture gets destroyed before all the clients are done using it,
387 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800388 // will instead fail later on when the client tries to use the surface,
389 // which should be reported as "surface XYZ returned an -ENODEV". The
390 // purgatorized layers are no less authentic than the visible ones, so this
391 // should not cause any harm.
392 size_t purgatorySize = mLayerPurgatory.size();
393 for (size_t i=0 ; i<purgatorySize ; i++) {
394 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
395 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700396 if (lbc != NULL) {
397 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
398 if (lbcBinder == surfaceTextureBinder) {
399 return true;
400 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800401 }
402 }
403
404 return false;
405}
406
Mathias Agopianbb641242010-05-18 17:06:55 -0700407status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
408 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409{
Mathias Agopianbb641242010-05-18 17:06:55 -0700410 return mEventQueue.postMessage(msg, reltime, flags);
411}
412
413status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
414 nsecs_t reltime, uint32_t flags)
415{
416 status_t res = mEventQueue.postMessage(msg, reltime, flags);
417 if (res == NO_ERROR) {
418 msg->wait();
419 }
420 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421}
422
423// ----------------------------------------------------------------------------
424#if 0
425#pragma mark -
426#pragma mark Main loop
427#endif
428
429bool SurfaceFlinger::threadLoop()
430{
431 waitForEvent();
432
433 // check for transactions
434 if (UNLIKELY(mConsoleSignals)) {
435 handleConsoleEvents();
436 }
437
Mathias Agopian698c0872011-06-28 19:09:31 -0700438 // if we're in a global transaction, don't do anything.
439 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
440 uint32_t transactionFlags = peekTransactionFlags(mask);
441 if (UNLIKELY(transactionFlags)) {
442 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 }
444
445 // post surfaces (if needed)
446 handlePageFlip();
447
Mathias Agopiana350ff92010-08-10 17:14:02 -0700448 if (UNLIKELY(mHwWorkListDirty)) {
449 // build the h/w work list
450 handleWorkList();
451 }
452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana44b0412011-10-16 18:46:35 -0700454 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700456
457 const int index = hw.getCurrentBufferIndex();
458 GraphicLog& logger(GraphicLog::getInstance());
459
460 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 handleRepaint();
462
Mathias Agopian74faca22009-09-17 16:18:16 -0700463 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700464 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700465 hw.compositionComplete();
466
Mathias Agopian35b48d12010-09-13 22:57:58 -0700467 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700468 postFramebuffer();
469
Mathias Agopian35b48d12010-09-13 22:57:58 -0700470 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 } else {
472 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800473 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474 usleep(16667); // 60 fps period
475 }
476 return true;
477}
478
479void SurfaceFlinger::postFramebuffer()
480{
Mathias Agopiana44b0412011-10-16 18:46:35 -0700481 const DisplayHardware& hw(graphicPlane(0).displayHardware());
482 const nsecs_t now = systemTime();
483 mDebugInSwapBuffers = now;
484 hw.flip(mSwapRegion);
485 mLastSwapBufferTime = systemTime() - now;
486 mDebugInSwapBuffers = 0;
487 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488}
489
490void SurfaceFlinger::handleConsoleEvents()
491{
492 // something to do with the console
493 const DisplayHardware& hw = graphicPlane(0).displayHardware();
494
495 int what = android_atomic_and(0, &mConsoleSignals);
496 if (what & eConsoleAcquired) {
497 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700498 // this is a temporary work-around, eventually this should be called
499 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700500 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 }
502
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700504 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 }
507 }
508
509 mDirtyRegion.set(hw.bounds());
510}
511
512void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
513{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700514 Mutex::Autolock _l(mStateLock);
515 const nsecs_t now = systemTime();
516 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517
Mathias Agopianca4d3602011-05-19 15:38:14 -0700518 // Here we're guaranteed that some transaction flags are set
519 // so we can call handleTransactionLocked() unconditionally.
520 // We call getTransactionFlags(), which will also clear the flags,
521 // with mStateLock held to guarantee that mCurrentState won't change
522 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700523
Mathias Agopianca4d3602011-05-19 15:38:14 -0700524 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
525 transactionFlags = getTransactionFlags(mask);
526 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700527
Mathias Agopianca4d3602011-05-19 15:38:14 -0700528 mLastTransactionTime = systemTime() - now;
529 mDebugInTransaction = 0;
530 invalidateHwcGeometry();
531 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700532}
533
Mathias Agopianca4d3602011-05-19 15:38:14 -0700534void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700535{
536 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 const size_t count = currentLayers.size();
538
539 /*
540 * Traversal of the children
541 * (perform the transaction for each of them if needed)
542 */
543
544 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
545 if (layersNeedTransaction) {
546 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700547 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
549 if (!trFlags) continue;
550
551 const uint32_t flags = layer->doTransaction(0);
552 if (flags & Layer::eVisibleRegion)
553 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 }
555 }
556
557 /*
558 * Perform our own transaction if needed
559 */
560
561 if (transactionFlags & eTransactionNeeded) {
562 if (mCurrentState.orientation != mDrawingState.orientation) {
563 // the orientation has changed, recompute all visible regions
564 // and invalidate everything.
565
566 const int dpy = 0;
567 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700568 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 GraphicPlane& plane(graphicPlane(dpy));
570 plane.setOrientation(orientation);
571
572 // update the shared control block
573 const DisplayHardware& hw(plane.displayHardware());
574 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
575 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800576 dcblk->w = plane.getWidth();
577 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578
579 mVisibleRegionsDirty = true;
580 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 }
582
583 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
584 // freezing or unfreezing the display -> trigger animation if needed
585 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800586 if (mFreezeDisplay)
587 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 }
589
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700590 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
591 // layers have been added
592 mVisibleRegionsDirty = true;
593 }
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 // some layers might have been removed, so
596 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700598 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700600 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700601 const size_t count = previousLayers.size();
602 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700603 const sp<LayerBase>& layer(previousLayers[i]);
604 if (currentLayers.indexOf( layer ) < 0) {
605 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700606 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700607 }
608 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 }
611
612 commitTransaction();
613}
614
615sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
616{
617 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
618}
619
620void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800621 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622{
623 const GraphicPlane& plane(graphicPlane(0));
624 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700625 const DisplayHardware& hw(plane.displayHardware());
626 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627
628 Region aboveOpaqueLayers;
629 Region aboveCoveredLayers;
630 Region dirty;
631
632 bool secureFrameBuffer = false;
633
634 size_t i = currentLayers.size();
635 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700636 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 layer->validateVisibility(planeTransform);
638
639 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700640 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
Mathias Agopianab028732010-03-16 16:41:46 -0700642 /*
643 * opaqueRegion: area of a surface that is fully opaque.
644 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700646
647 /*
648 * visibleRegion: area of a surface that is visible on screen
649 * and not fully transparent. This is essentially the layer's
650 * footprint minus the opaque regions above it.
651 * Areas covered by a translucent surface are considered visible.
652 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700654
655 /*
656 * coveredRegion: area of a surface that is covered by all
657 * visible regions above it (which includes the translucent areas).
658 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700660
661
662 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700663 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700664 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700665 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700667 visibleRegion.andSelf(screenRegion);
668 if (!visibleRegion.isEmpty()) {
669 // Remove the transparent area from the visible region
670 if (translucent) {
671 visibleRegion.subtractSelf(layer->transparentRegionScreen);
672 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673
Mathias Agopianab028732010-03-16 16:41:46 -0700674 // compute the opaque region
675 const int32_t layerOrientation = layer->getOrientation();
676 if (s.alpha==255 && !translucent &&
677 ((layerOrientation & Transform::ROT_INVALID) == false)) {
678 // the opaque region is the layer's footprint
679 opaqueRegion = visibleRegion;
680 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 }
682 }
683
Mathias Agopianab028732010-03-16 16:41:46 -0700684 // Clip the covered region to the visible region
685 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
686
687 // Update aboveCoveredLayers for next (lower) layer
688 aboveCoveredLayers.orSelf(visibleRegion);
689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 // subtract the opaque region covered by the layers above us
691 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
693 // compute this layer's dirty region
694 if (layer->contentDirty) {
695 // we need to invalidate the whole region
696 dirty = visibleRegion;
697 // as well, as the old visible region
698 dirty.orSelf(layer->visibleRegionScreen);
699 layer->contentDirty = false;
700 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700701 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700702 * the exposed region consists of two components:
703 * 1) what's VISIBLE now and was COVERED before
704 * 2) what's EXPOSED now less what was EXPOSED before
705 *
706 * note that (1) is conservative, we start with the whole
707 * visible region but only keep what used to be covered by
708 * something -- which mean it may have been exposed.
709 *
710 * (2) handles areas that were not covered by anything but got
711 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700712 */
Mathias Agopianab028732010-03-16 16:41:46 -0700713 const Region newExposed = visibleRegion - coveredRegion;
714 const Region oldVisibleRegion = layer->visibleRegionScreen;
715 const Region oldCoveredRegion = layer->coveredRegionScreen;
716 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
717 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 }
719 dirty.subtractSelf(aboveOpaqueLayers);
720
721 // accumulate to the screen dirty region
722 dirtyRegion.orSelf(dirty);
723
Mathias Agopianab028732010-03-16 16:41:46 -0700724 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700726
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 // Store the visible region is screen space
728 layer->setVisibleRegion(visibleRegion);
729 layer->setCoveredRegion(coveredRegion);
730
Mathias Agopian97011222009-07-28 10:57:27 -0700731 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 if (layer->isSecure() && !visibleRegion.isEmpty()) {
733 secureFrameBuffer = true;
734 }
735 }
736
Mathias Agopian97011222009-07-28 10:57:27 -0700737 // invalidate the areas where a layer was removed
738 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
739 mDirtyRegionRemovedLayer.clear();
740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 mSecureFrameBuffer = secureFrameBuffer;
742 opaqueRegion = aboveOpaqueLayers;
743}
744
745
746void SurfaceFlinger::commitTransaction()
747{
748 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700749 mResizeTransationPending = false;
750 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751}
752
753void SurfaceFlinger::handlePageFlip()
754{
755 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800756 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 visibleRegions |= lockPageFlip(currentLayers);
758
759 const DisplayHardware& hw = graphicPlane(0).displayHardware();
760 const Region screenRegion(hw.bounds());
761 if (visibleRegions) {
762 Region opaqueRegion;
763 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700764
765 /*
766 * rebuild the visible layer list
767 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800768 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700769 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700770 mVisibleLayersSortedByZ.setCapacity(count);
771 for (size_t i=0 ; i<count ; i++) {
772 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
773 mVisibleLayersSortedByZ.add(currentLayers[i]);
774 }
775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 mWormholeRegion = screenRegion.subtract(opaqueRegion);
777 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800778 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 }
780
781 unlockPageFlip(currentLayers);
782 mDirtyRegion.andSelf(screenRegion);
783}
784
Mathias Agopianad456f92011-01-13 17:53:01 -0800785void SurfaceFlinger::invalidateHwcGeometry()
786{
787 mHwWorkListDirty = true;
788}
789
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
791{
792 bool recomputeVisibleRegions = false;
793 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700794 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700796 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797 layer->lockPageFlip(recomputeVisibleRegions);
798 }
799 return recomputeVisibleRegions;
800}
801
802void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
803{
804 const GraphicPlane& plane(graphicPlane(0));
805 const Transform& planeTransform(plane.transform());
806 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700807 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700809 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 layer->unlockPageFlip(planeTransform, mDirtyRegion);
811 }
812}
813
Mathias Agopiana350ff92010-08-10 17:14:02 -0700814void SurfaceFlinger::handleWorkList()
815{
816 mHwWorkListDirty = false;
817 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
818 if (hwc.initCheck() == NO_ERROR) {
819 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
820 const size_t count = currentLayers.size();
821 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700822 hwc_layer_t* const cur(hwc.getLayers());
823 for (size_t i=0 ; cur && i<count ; i++) {
824 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700825 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700826 cur[i].compositionType = HWC_FRAMEBUFFER;
827 cur[i].flags |= HWC_SKIP_LAYER;
828 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700829 }
830 }
831}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833void SurfaceFlinger::handleRepaint()
834{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700835 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700836 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837
838 if (UNLIKELY(mDebugRegion)) {
839 debugFlashRegions();
840 }
841
Mathias Agopianb8a55602009-06-26 19:06:36 -0700842 // set the frame buffer
843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
844 glMatrixMode(GL_MODELVIEW);
845 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846
847 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700848 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
849 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700850 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700851 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
852 // takes a rectangle, we must make sure to update that whole
853 // rectangle in that case
854 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700855 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700856 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700857 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // in the BUFFER_PRESERVED case, obviously, we can update only
860 // what's needed and nothing more.
861 // NOTE: this is NOT a common case, as preserving the backbuffer
862 // is costly and usually involves copying the whole update back.
863 }
864 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700865 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700866 // We need to redraw the rectangle that will be updated
867 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700868 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700869 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700870 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700871 } else {
872 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700874 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 }
876 }
877
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700878 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 composeSurfaces(mDirtyRegion);
880
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700881 // update the swap region and clear the dirty region
882 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 mDirtyRegion.clear();
884}
885
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700886void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700888 const DisplayHardware& hw(graphicPlane(0).displayHardware());
889 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700890 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700891 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700892 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700893 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700894
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
896 size_t count = layers.size();
897
898 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700899 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
900 hwc.getNumLayers(), count);
901
902 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700903 if (hwc.initCheck() == NO_ERROR) {
904 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
905 }
Mathias Agopian45721772010-08-12 15:03:26 -0700906
907 /*
908 * update the per-frame h/w composer data for each layer
909 * and build the transparent region of the FB
910 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700911 for (size_t i=0 ; i<count ; i++) {
912 const sp<LayerBase>& layer(layers[i]);
913 layer->setPerFrameData(&cur[i]);
914 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700915 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700916 status_t err = hwc.prepare();
917 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700918
Mathias Agopianf384cc32011-09-08 18:31:55 -0700919 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700920 // what's happening here is tricky.
921 // we want to clear all the layers with the CLEAR_FB flags
922 // that are opaque.
923 // however, since some GPU are efficient at preserving
924 // the backbuffer, we want to take advantage of that so we do the
925 // clear only in the dirty region (other areas will be preserved
926 // on those GPUs).
927 // NOTE: on non backbuffer preserving GPU, the dirty region
928 // has already been expanded as needed, so the code is correct
929 // there too.
930 //
931 // However, the content of the framebuffer cannot be trusted when
932 // we switch to/from FB/OVERLAY, in which case we need to
933 // expand the dirty region to those areas too.
934 //
935 // Note also that there is a special case when switching from
936 // "no layers in FB" to "some layers in FB", where we need to redraw
937 // the entire FB, since some areas might contain uninitialized
938 // data.
939 //
940 // Also we want to make sure to not clear areas that belong to
941 // layers above that won't redraw (we would just erasing them),
942 // that is, we can't erase anything outside the dirty region.
943
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700944 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700945
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700946 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
947 transparent.set(hw.getBounds());
948 dirtyInOut = transparent;
949 } else {
950 for (size_t i=0 ; i<count ; i++) {
951 const sp<LayerBase>& layer(layers[i]);
952 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
953 transparent.orSelf(layer->visibleRegionScreen);
954 }
955 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
956 if (isOverlay != layer->isOverlay()) {
957 // we transitioned to/from overlay, so add this layer
958 // to the dirty region so the framebuffer can be either
959 // cleared or redrawn.
960 dirtyInOut.orSelf(layer->visibleRegionScreen);
961 }
962 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800963 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700964 // don't erase stuff outside the dirty region
965 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700966 }
967
968 /*
969 * clear the area of the FB that need to be transparent
970 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700971 if (!transparent.isEmpty()) {
972 glClearColor(0,0,0,0);
973 Region::const_iterator it = transparent.begin();
974 Region::const_iterator const end = transparent.end();
975 const int32_t height = hw.getHeight();
976 while (it != end) {
977 const Rect& r(*it++);
978 const GLint sy = height - (r.top + r.height());
979 glScissor(r.left, sy, r.width(), r.height());
980 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800981 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700982 }
983 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700984}
Mathias Agopian45721772010-08-12 15:03:26 -0700985
Mathias Agopianf384cc32011-09-08 18:31:55 -0700986void SurfaceFlinger::composeSurfaces(const Region& dirty)
987{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700988 const DisplayHardware& hw(graphicPlane(0).displayHardware());
989 HWComposer& hwc(hw.getHwComposer());
990
991 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
992 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700993 // should never happen unless the window manager has a bug
994 // draw something...
995 drawWormhole();
996 }
997
Mathias Agopian45721772010-08-12 15:03:26 -0700998 /*
999 * and then, render the layers targeted at the framebuffer
1000 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001001 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001002 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1003 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001004 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001005 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001006 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001007 }
1008 const sp<LayerBase>& layer(layers[i]);
1009 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1010 if (!clip.isEmpty()) {
1011 layer->draw(clip);
1012 }
1013 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014}
1015
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016void SurfaceFlinger::debugFlashRegions()
1017{
Mathias Agopian0a917752010-06-14 21:20:00 -07001018 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1019 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001020 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001021 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001022 return;
1023 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001024
Mathias Agopian0a917752010-06-14 21:20:00 -07001025 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1026 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1027 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1028 mDirtyRegion.bounds() : hw.bounds());
1029 composeSurfaces(repaint);
1030 }
1031
Mathias Agopianc492e672011-10-18 14:49:27 -07001032 glDisable(GL_TEXTURE_EXTERNAL_OES);
1033 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 glDisable(GL_SCISSOR_TEST);
1036
Mathias Agopian0926f502009-05-04 14:17:04 -07001037 static int toggle = 0;
1038 toggle = 1 - toggle;
1039 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001040 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001041 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001042 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001043 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044
Mathias Agopian20f68782009-05-11 00:03:41 -07001045 Region::const_iterator it = mDirtyRegion.begin();
1046 Region::const_iterator const end = mDirtyRegion.end();
1047 while (it != end) {
1048 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001050 { r.left, height - r.top },
1051 { r.left, height - r.bottom },
1052 { r.right, height - r.bottom },
1053 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 };
1055 glVertexPointer(2, GL_FLOAT, 0, vertices);
1056 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1057 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001058
Mathias Agopian0656a682011-09-20 17:22:44 -07001059 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060
1061 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001062 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063
1064 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065}
1066
1067void SurfaceFlinger::drawWormhole() const
1068{
1069 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1070 if (region.isEmpty())
1071 return;
1072
1073 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1074 const int32_t width = hw.getWidth();
1075 const int32_t height = hw.getHeight();
1076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001078 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001079 Region::const_iterator it = region.begin();
1080 Region::const_iterator const end = region.end();
1081 while (it != end) {
1082 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 const GLint sy = height - (r.top + r.height());
1084 glScissor(r.left, sy, r.width(), r.height());
1085 glClear(GL_COLOR_BUFFER_BIT);
1086 }
1087 } else {
1088 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1089 { width, height }, { 0, height } };
1090 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 glVertexPointer(2, GL_SHORT, 0, vertices);
1093 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1094 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001095
1096 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 glEnable(GL_TEXTURE_2D);
1098 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1099 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1100 glMatrixMode(GL_TEXTURE);
1101 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001102
1103 glDisable(GL_BLEND);
1104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001106 Region::const_iterator it = region.begin();
1107 Region::const_iterator const end = region.end();
1108 while (it != end) {
1109 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 const GLint sy = height - (r.top + r.height());
1111 glScissor(r.left, sy, r.width(), r.height());
1112 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1113 }
1114 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001115 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001116 glLoadIdentity();
1117 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 }
1119}
1120
1121void SurfaceFlinger::debugShowFPS() const
1122{
1123 static int mFrameCount;
1124 static int mLastFrameCount = 0;
1125 static nsecs_t mLastFpsTime = 0;
1126 static float mFps = 0;
1127 mFrameCount++;
1128 nsecs_t now = systemTime();
1129 nsecs_t diff = now - mLastFpsTime;
1130 if (diff > ms2ns(250)) {
1131 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1132 mLastFpsTime = now;
1133 mLastFrameCount = mFrameCount;
1134 }
1135 // XXX: mFPS has the value we want
1136 }
1137
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001138status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139{
1140 Mutex::Autolock _l(mStateLock);
1141 addLayer_l(layer);
1142 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1143 return NO_ERROR;
1144}
1145
Mathias Agopian96f08192010-06-02 23:28:45 -07001146status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1147{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001148 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001149 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1150}
1151
1152ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1153 const sp<LayerBaseClient>& lbc)
1154{
Mathias Agopian96f08192010-06-02 23:28:45 -07001155 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001156 size_t name = client->attachLayer(lbc);
1157
1158 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001159
1160 // add this layer to the current state list
1161 addLayer_l(lbc);
1162
Mathias Agopian4f113742011-05-03 16:21:41 -07001163 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001164}
1165
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001166status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167{
1168 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001169 status_t err = purgatorizeLayer_l(layer);
1170 if (err == NO_ERROR)
1171 setTransactionFlags(eTransactionNeeded);
1172 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173}
1174
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001175status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001177 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1178 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001179 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001180 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1182 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001183 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 return NO_ERROR;
1185 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001186 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187}
1188
Mathias Agopian9a112062009-04-17 19:36:26 -07001189status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1190{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001191 // First add the layer to the purgatory list, which makes sure it won't
1192 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001193 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001194 if (err >= 0) {
1195 mLayerPurgatory.add(layerBase);
1196 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001197
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001198 layerBase->onRemoved();
1199
Mathias Agopian3d579642009-06-04 18:46:21 -07001200 // it's possible that we don't find a layer, because it might
1201 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001202 // from the user because there is a race between Client::destroySurface(),
1203 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001204 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1205}
1206
Mathias Agopian96f08192010-06-02 23:28:45 -07001207status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208{
Mathias Agopian96f08192010-06-02 23:28:45 -07001209 layer->forceVisibilityTransaction();
1210 setTransactionFlags(eTraversalNeeded);
1211 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212}
1213
Mathias Agopiandea20b12011-05-03 17:04:02 -07001214uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1215{
1216 return android_atomic_release_load(&mTransactionFlags);
1217}
1218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1220{
1221 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1222}
1223
Mathias Agopianbb641242010-05-18 17:06:55 -07001224uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225{
1226 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1227 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001228 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 }
1230 return old;
1231}
1232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001234void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
1235 int orientation) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001236 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001237
Mathias Agopian698c0872011-06-28 19:09:31 -07001238 uint32_t flags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001239 if (mCurrentState.orientation != orientation) {
1240 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1241 mCurrentState.orientation = orientation;
1242 flags |= eTransactionNeeded;
1243 mResizeTransationPending = true;
1244 } else if (orientation != eOrientationUnchanged) {
1245 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1246 orientation);
1247 }
1248 }
1249
Mathias Agopian698c0872011-06-28 19:09:31 -07001250 const size_t count = state.size();
1251 for (size_t i=0 ; i<count ; i++) {
1252 const ComposerState& s(state[i]);
1253 sp<Client> client( static_cast<Client *>(s.client.get()) );
1254 flags |= setClientStateLocked(client, s.state);
1255 }
1256 if (flags) {
1257 setTransactionFlags(flags);
1258 }
1259
1260 signalEvent();
1261
1262 // if there is a transaction with a resize, wait for it to
1263 // take effect before returning.
1264 while (mResizeTransationPending) {
1265 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1266 if (CC_UNLIKELY(err != NO_ERROR)) {
1267 // just in case something goes wrong in SF, return to the
1268 // called after a few seconds.
1269 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1270 mResizeTransationPending = false;
1271 break;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001272 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273 }
1274}
1275
1276status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1277{
1278 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1279 return BAD_VALUE;
1280
1281 Mutex::Autolock _l(mStateLock);
1282 mCurrentState.freezeDisplay = 1;
1283 setTransactionFlags(eTransactionNeeded);
1284
1285 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001286 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 return NO_ERROR;
1288}
1289
1290status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1291{
1292 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1293 return BAD_VALUE;
1294
1295 Mutex::Autolock _l(mStateLock);
1296 mCurrentState.freezeDisplay = 0;
1297 setTransactionFlags(eTransactionNeeded);
1298
1299 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001300 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 return NO_ERROR;
1302}
1303
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001304int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001305 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
1307 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1308 return BAD_VALUE;
1309
1310 Mutex::Autolock _l(mStateLock);
1311 if (mCurrentState.orientation != orientation) {
1312 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001313 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 mCurrentState.orientation = orientation;
1315 setTransactionFlags(eTransactionNeeded);
1316 mTransactionCV.wait(mStateLock);
1317 } else {
1318 orientation = BAD_VALUE;
1319 }
1320 }
1321 return orientation;
1322}
1323
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001324sp<ISurface> SurfaceFlinger::createSurface(
1325 ISurfaceComposerClient::surface_data_t* params,
1326 const String8& name,
1327 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1329 uint32_t flags)
1330{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001331 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001332 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001333
1334 if (int32_t(w|h) < 0) {
1335 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1336 int(w), int(h));
1337 return surfaceHandle;
1338 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001341 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 switch (flags & eFXSurfaceMask) {
1343 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001344 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1345 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 break;
1347 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001348 // for now we treat Blur as Dim, until we can implement it
1349 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001351 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 break;
1353 }
1354
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001355 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001356 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001357 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001359
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001361 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001362 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001363 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001364 if (normalLayer != 0) {
1365 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001366 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001367 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001368 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001369
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 }
1372
1373 return surfaceHandle;
1374}
1375
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001376sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001377 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001378 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001379 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380{
1381 // initialize the surfaces
1382 switch (format) { // TODO: take h/w into account
1383 case PIXEL_FORMAT_TRANSPARENT:
1384 case PIXEL_FORMAT_TRANSLUCENT:
1385 format = PIXEL_FORMAT_RGBA_8888;
1386 break;
1387 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001388#ifdef NO_RGBX_8888
1389 format = PIXEL_FORMAT_RGB_565;
1390#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001391 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001392#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 break;
1394 }
1395
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001396#ifdef NO_RGBX_8888
1397 if (format == PIXEL_FORMAT_RGBX_8888)
1398 format = PIXEL_FORMAT_RGBA_8888;
1399#endif
1400
Mathias Agopian96f08192010-06-02 23:28:45 -07001401 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001402 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001403 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001405 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 }
1407 return layer;
1408}
1409
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001410sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001411 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001412 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413{
Mathias Agopian96f08192010-06-02 23:28:45 -07001414 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopiane9800c82011-10-16 23:54:25 -07001415 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 return layer;
1417}
1418
Mathias Agopian96f08192010-06-02 23:28:45 -07001419status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420{
Mathias Agopian9a112062009-04-17 19:36:26 -07001421 /*
1422 * called by the window manager, when a surface should be marked for
1423 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001424 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001425 * The surface is removed from the current and drawing lists, but placed
1426 * in the purgatory queue, so it's not destroyed right-away (we need
1427 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001428 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001429
Mathias Agopian48d819a2009-09-10 19:41:18 -07001430 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001431 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001432 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001433 if (layer != 0) {
1434 err = purgatorizeLayer_l(layer);
1435 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001436 setTransactionFlags(eTransactionNeeded);
1437 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001438 }
1439 return err;
1440}
1441
Mathias Agopianca4d3602011-05-19 15:38:14 -07001442status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001443{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001444 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001445 status_t err = NO_ERROR;
1446 sp<LayerBaseClient> l(layer.promote());
1447 if (l != NULL) {
1448 Mutex::Autolock _l(mStateLock);
1449 err = removeLayer_l(l);
1450 if (err == NAME_NOT_FOUND) {
1451 // The surface wasn't in the current list, which means it was
1452 // removed already, which means it is in the purgatory,
1453 // and need to be removed from there.
1454 ssize_t idx = mLayerPurgatory.remove(l);
1455 LOGE_IF(idx < 0,
1456 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001457 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001458 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1459 "error removing layer=%p (%s)", l.get(), strerror(-err));
1460 }
1461 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462}
1463
Mathias Agopian698c0872011-06-28 19:09:31 -07001464uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001465 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001466 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001469 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1470 if (layer != 0) {
1471 const uint32_t what = s.what;
1472 if (what & ePositionChanged) {
1473 if (layer->setPosition(s.x, s.y))
1474 flags |= eTraversalNeeded;
1475 }
1476 if (what & eLayerChanged) {
1477 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1478 if (layer->setLayer(s.z)) {
1479 mCurrentState.layersSortedByZ.removeAt(idx);
1480 mCurrentState.layersSortedByZ.add(layer);
1481 // we need traversal (state changed)
1482 // AND transaction (list changed)
1483 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 }
1485 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001486 if (what & eSizeChanged) {
1487 if (layer->setSize(s.w, s.h)) {
1488 flags |= eTraversalNeeded;
1489 mResizeTransationPending = true;
1490 }
1491 }
1492 if (what & eAlphaChanged) {
1493 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1494 flags |= eTraversalNeeded;
1495 }
1496 if (what & eMatrixChanged) {
1497 if (layer->setMatrix(s.matrix))
1498 flags |= eTraversalNeeded;
1499 }
1500 if (what & eTransparentRegionChanged) {
1501 if (layer->setTransparentRegionHint(s.transparentRegion))
1502 flags |= eTraversalNeeded;
1503 }
1504 if (what & eVisibilityChanged) {
1505 if (layer->setFlags(s.flags, s.mask))
1506 flags |= eTraversalNeeded;
1507 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001509 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510}
1511
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512void SurfaceFlinger::screenReleased(int dpy)
1513{
1514 // this may be called by a signal handler, we can't do too much in here
1515 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1516 signalEvent();
1517}
1518
1519void SurfaceFlinger::screenAcquired(int dpy)
1520{
1521 // this may be called by a signal handler, we can't do too much in here
1522 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1523 signalEvent();
1524}
1525
1526status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1527{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001528 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 char buffer[SIZE];
1530 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001531
1532 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 snprintf(buffer, SIZE, "Permission Denial: "
1534 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1535 IPCThreadState::self()->getCallingPid(),
1536 IPCThreadState::self()->getCallingUid());
1537 result.append(buffer);
1538 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001539
1540 // figure out if we're stuck somewhere
1541 const nsecs_t now = systemTime();
1542 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1543 const nsecs_t inTransaction(mDebugInTransaction);
1544 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1545 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1546
1547 // Try to get the main lock, but don't insist if we can't
1548 // (this would indicate SF is stuck, but we want to be able to
1549 // print something in dumpsys).
1550 int retry = 3;
1551 while (mStateLock.tryLock()<0 && --retry>=0) {
1552 usleep(1000000);
1553 }
1554 const bool locked(retry >= 0);
1555 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001556 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001557 "SurfaceFlinger appears to be unresponsive, "
1558 "dumping anyways (no locks held)\n");
1559 result.append(buffer);
1560 }
1561
Mathias Agopian48b888a2011-01-19 16:15:53 -08001562 /*
1563 * Dump the visible layer list
1564 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1566 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001567 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1568 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001570 const sp<LayerBase>& layer(currentLayers[i]);
1571 layer->dump(result, buffer, SIZE);
1572 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 s.transparentRegion.dump(result, "transparentRegion");
1574 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1575 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1576 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001577
Mathias Agopian48b888a2011-01-19 16:15:53 -08001578 /*
1579 * Dump the layers in the purgatory
1580 */
1581
1582 const size_t purgatorySize = mLayerPurgatory.size();
1583 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1584 result.append(buffer);
1585 for (size_t i=0 ; i<purgatorySize ; i++) {
1586 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1587 layer->shortDump(result, buffer, SIZE);
1588 }
1589
1590 /*
1591 * Dump SurfaceFlinger global state
1592 */
1593
Mathias Agopianad701862011-07-14 18:01:49 -07001594 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001595 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001596
1597 const GLExtensions& extensions(GLExtensions::getInstance());
1598 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1599 extensions.getVendor(),
1600 extensions.getRenderer(),
1601 extensions.getVersion());
1602 result.append(buffer);
1603 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1604 result.append(buffer);
1605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 mWormholeRegion.dump(result, "WormholeRegion");
1607 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1608 snprintf(buffer, SIZE,
1609 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1610 mFreezeDisplay?"yes":"no", mFreezeCount,
1611 mCurrentState.orientation, hw.canDraw());
1612 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001613 snprintf(buffer, SIZE,
1614 " last eglSwapBuffers() time: %f us\n"
1615 " last transaction time : %f us\n",
1616 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1617 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001618
Mathias Agopian9795c422009-08-26 16:36:26 -07001619 if (inSwapBuffersDuration || !locked) {
1620 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1621 inSwapBuffersDuration/1000.0);
1622 result.append(buffer);
1623 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001624
Mathias Agopian9795c422009-08-26 16:36:26 -07001625 if (inTransactionDuration || !locked) {
1626 snprintf(buffer, SIZE, " transaction time: %f us\n",
1627 inTransactionDuration/1000.0);
1628 result.append(buffer);
1629 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001630
Mathias Agopian48b888a2011-01-19 16:15:53 -08001631 /*
1632 * Dump HWComposer state
1633 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001634 HWComposer& hwc(hw.getHwComposer());
1635 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1636 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001637 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001638 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001639 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001640
Mathias Agopian48b888a2011-01-19 16:15:53 -08001641 /*
1642 * Dump gralloc state
1643 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001644 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001645 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001646 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001647
1648 if (locked) {
1649 mStateLock.unlock();
1650 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 }
1652 write(fd, result.string(), result.size());
1653 return NO_ERROR;
1654}
1655
1656status_t SurfaceFlinger::onTransact(
1657 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1658{
1659 switch (code) {
1660 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001661 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 case SET_ORIENTATION:
1663 case FREEZE_DISPLAY:
1664 case UNFREEZE_DISPLAY:
1665 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001666 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001667 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 {
1669 // codes that require permission check
1670 IPCThreadState* ipc = IPCThreadState::self();
1671 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001672 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001673 if ((uid != AID_GRAPHICS) &&
1674 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001675 LOGE("Permission Denial: "
1676 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1677 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001679 break;
1680 }
1681 case CAPTURE_SCREEN:
1682 {
1683 // codes that require permission check
1684 IPCThreadState* ipc = IPCThreadState::self();
1685 const int pid = ipc->getCallingPid();
1686 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001687 if ((uid != AID_GRAPHICS) &&
1688 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001689 LOGE("Permission Denial: "
1690 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1691 return PERMISSION_DENIED;
1692 }
1693 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 }
1695 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1698 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001699 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001700 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001701 IPCThreadState* ipc = IPCThreadState::self();
1702 const int pid = ipc->getCallingPid();
1703 const int uid = ipc->getCallingUid();
1704 LOGE("Permission Denial: "
1705 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706 return PERMISSION_DENIED;
1707 }
1708 int n;
1709 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001710 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001711 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 return NO_ERROR;
1713 case 1002: // SHOW_UPDATES
1714 n = data.readInt32();
1715 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001716 invalidateHwcGeometry();
1717 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 return NO_ERROR;
1719 case 1003: // SHOW_BACKGROUND
1720 n = data.readInt32();
1721 mDebugBackground = n ? 1 : 0;
1722 return NO_ERROR;
1723 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001724 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001725 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001727 case 1005:{ // force transaction
1728 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1729 return NO_ERROR;
1730 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001731 case 1006:{ // enable/disable GraphicLog
1732 int enabled = data.readInt32();
1733 GraphicLog::getInstance().setEnabled(enabled);
1734 return NO_ERROR;
1735 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736 case 1007: // set mFreezeCount
1737 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001738 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 return NO_ERROR;
Mathias Agopian53331da2011-08-22 21:44:41 -07001740 case 1008: // toggle use of hw composer
1741 n = data.readInt32();
1742 mDebugDisableHWC = n ? 1 : 0;
1743 invalidateHwcGeometry();
1744 repaintEverything();
1745 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001746 case 1009: // toggle use of transform hint
1747 n = data.readInt32();
1748 mDebugDisableTransformHint = n ? 1 : 0;
1749 invalidateHwcGeometry();
1750 repaintEverything();
1751 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001753 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 reply->writeInt32(0);
1755 reply->writeInt32(mDebugRegion);
1756 reply->writeInt32(mDebugBackground);
1757 return NO_ERROR;
1758 case 1013: {
1759 Mutex::Autolock _l(mStateLock);
1760 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1761 reply->writeInt32(hw.getPageFlipCount());
1762 }
1763 return NO_ERROR;
1764 }
1765 }
1766 return err;
1767}
1768
Mathias Agopian53331da2011-08-22 21:44:41 -07001769void SurfaceFlinger::repaintEverything() {
1770 Mutex::Autolock _l(mStateLock);
1771 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1772 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1773 signalEvent();
1774}
1775
Mathias Agopian59119e62010-10-11 12:37:43 -07001776// ---------------------------------------------------------------------------
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 Agopian9daa5c92010-10-12 16:05:48 -07001817 glClearColor(0,0,0,1);
1818 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001819 glMatrixMode(GL_MODELVIEW);
1820 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001821 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1822 const size_t count = layers.size();
1823 for (size_t i=0 ; i<count ; ++i) {
1824 const sp<LayerBase>& layer(layers[i]);
1825 layer->drawForSreenShot();
1826 }
1827
1828 // back to main framebuffer
1829 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1830 glDisable(GL_SCISSOR_TEST);
1831 glDeleteFramebuffersOES(1, &name);
1832
1833 *textureName = tname;
1834 *uOut = u;
1835 *vOut = v;
1836 return NO_ERROR;
1837}
1838
1839// ---------------------------------------------------------------------------
1840
1841status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1842{
Mathias Agopiane9800c82011-10-16 23:54:25 -07001843 status_t result = PERMISSION_DENIED;
1844
1845 if (!GLExtensions::getInstance().haveFramebufferObject())
1846 return INVALID_OPERATION;
1847
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001848 // get screen geometry
1849 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1850 const uint32_t hw_w = hw.getWidth();
1851 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001852 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001853
1854 GLfloat u, v;
1855 GLuint tname;
Mathias Agopiane9800c82011-10-16 23:54:25 -07001856 result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001857 if (result != NO_ERROR) {
1858 return result;
1859 }
1860
1861 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001862 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001863 glBindTexture(GL_TEXTURE_2D, tname);
1864 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1865 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1866 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1867 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1868 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1869 glVertexPointer(2, GL_FLOAT, 0, vtx);
1870
Mathias Agopianffcf4652011-07-07 17:30:31 -07001871 /*
1872 * Texture coordinate mapping
1873 *
1874 * u
1875 * 1 +----------+---+
1876 * | | | | image is inverted
1877 * | V | | w.r.t. the texture
1878 * 1-v +----------+ | coordinates
1879 * | |
1880 * | |
1881 * | |
1882 * 0 +--------------+
1883 * 0 1
1884 *
1885 */
1886
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001887 class s_curve_interpolator {
1888 const float nbFrames, s, v;
1889 public:
1890 s_curve_interpolator(int nbFrames, float s)
1891 : nbFrames(1.0f / (nbFrames-1)), s(s),
1892 v(1.0f + expf(-s + 0.5f*s)) {
1893 }
1894 float operator()(int f) {
1895 const float x = f * nbFrames;
1896 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1897 }
1898 };
1899
1900 class v_stretch {
1901 const GLfloat hw_w, hw_h;
1902 public:
1903 v_stretch(uint32_t hw_w, uint32_t hw_h)
1904 : hw_w(hw_w), hw_h(hw_h) {
1905 }
1906 void operator()(GLfloat* vtx, float v) {
1907 const GLfloat w = hw_w + (hw_w * v);
1908 const GLfloat h = hw_h - (hw_h * v);
1909 const GLfloat x = (hw_w - w) * 0.5f;
1910 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001911 vtx[0] = x; vtx[1] = y;
1912 vtx[2] = x; vtx[3] = y + h;
1913 vtx[4] = x + w; vtx[5] = y + h;
1914 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001915 }
1916 };
1917
1918 class h_stretch {
1919 const GLfloat hw_w, hw_h;
1920 public:
1921 h_stretch(uint32_t hw_w, uint32_t hw_h)
1922 : hw_w(hw_w), hw_h(hw_h) {
1923 }
1924 void operator()(GLfloat* vtx, float v) {
1925 const GLfloat w = hw_w - (hw_w * v);
1926 const GLfloat h = 1.0f;
1927 const GLfloat x = (hw_w - w) * 0.5f;
1928 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001929 vtx[0] = x; vtx[1] = y;
1930 vtx[2] = x; vtx[3] = y + h;
1931 vtx[4] = x + w; vtx[5] = y + h;
1932 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001933 }
1934 };
1935
1936 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001937 char value[PROPERTY_VALUE_MAX];
1938 property_get("debug.sf.electron_frames", value, "24");
1939 int nbFrames = (atoi(value) + 1) >> 1;
1940 if (nbFrames <= 0) // just in case
1941 nbFrames = 24;
1942
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001943 s_curve_interpolator itr(nbFrames, 7.5f);
1944 s_curve_interpolator itg(nbFrames, 8.0f);
1945 s_curve_interpolator itb(nbFrames, 8.5f);
1946
1947 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001948
1949 glMatrixMode(GL_TEXTURE);
1950 glLoadIdentity();
1951 glMatrixMode(GL_MODELVIEW);
1952 glLoadIdentity();
1953
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001954 glEnable(GL_BLEND);
1955 glBlendFunc(GL_ONE, GL_ONE);
1956 for (int i=0 ; i<nbFrames ; i++) {
1957 float x, y, w, h;
1958 const float vr = itr(i);
1959 const float vg = itg(i);
1960 const float vb = itb(i);
1961
1962 // clear screen
1963 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001964 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001965 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001966
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001967 // draw the red plane
1968 vverts(vtx, vr);
1969 glColorMask(1,0,0,1);
1970 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001971
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 // draw the green plane
1973 vverts(vtx, vg);
1974 glColorMask(0,1,0,1);
1975 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001976
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977 // draw the blue plane
1978 vverts(vtx, vb);
1979 glColorMask(0,0,1,1);
1980 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001981
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001982 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001983 glDisable(GL_TEXTURE_2D);
1984 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 glColor4f(vg, vg, vg, 1);
1986 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1987 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001988 }
1989
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 h_stretch hverts(hw_w, hw_h);
1991 glDisable(GL_BLEND);
1992 glDisable(GL_TEXTURE_2D);
1993 glColorMask(1,1,1,1);
1994 for (int i=0 ; i<nbFrames ; i++) {
1995 const float v = itg(i);
1996 hverts(vtx, v);
1997 glClear(GL_COLOR_BUFFER_BIT);
1998 glColor4f(1-v, 1-v, 1-v, 1);
1999 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2000 hw.flip(screenBounds);
2001 }
2002
2003 glColorMask(1,1,1,1);
2004 glEnable(GL_SCISSOR_TEST);
2005 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2006 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002007 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002008 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 return NO_ERROR;
2010}
2011
2012status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2013{
2014 status_t result = PERMISSION_DENIED;
2015
2016 if (!GLExtensions::getInstance().haveFramebufferObject())
2017 return INVALID_OPERATION;
2018
2019
2020 // get screen geometry
2021 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2022 const uint32_t hw_w = hw.getWidth();
2023 const uint32_t hw_h = hw.getHeight();
2024 const Region screenBounds(hw.bounds());
2025
2026 GLfloat u, v;
2027 GLuint tname;
2028 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2029 if (result != NO_ERROR) {
2030 return result;
2031 }
2032
Mathias Agopiane9800c82011-10-16 23:54:25 -07002033 // back to main framebuffer
2034 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2035 glDisable(GL_SCISSOR_TEST);
2036
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002037 GLfloat vtx[8];
2038 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 glBindTexture(GL_TEXTURE_2D, tname);
2040 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2041 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2042 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2043 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2044 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2045 glVertexPointer(2, GL_FLOAT, 0, vtx);
2046
2047 class s_curve_interpolator {
2048 const float nbFrames, s, v;
2049 public:
2050 s_curve_interpolator(int nbFrames, float s)
2051 : nbFrames(1.0f / (nbFrames-1)), s(s),
2052 v(1.0f + expf(-s + 0.5f*s)) {
2053 }
2054 float operator()(int f) {
2055 const float x = f * nbFrames;
2056 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2057 }
2058 };
2059
2060 class v_stretch {
2061 const GLfloat hw_w, hw_h;
2062 public:
2063 v_stretch(uint32_t hw_w, uint32_t hw_h)
2064 : hw_w(hw_w), hw_h(hw_h) {
2065 }
2066 void operator()(GLfloat* vtx, float v) {
2067 const GLfloat w = hw_w + (hw_w * v);
2068 const GLfloat h = hw_h - (hw_h * v);
2069 const GLfloat x = (hw_w - w) * 0.5f;
2070 const GLfloat y = (hw_h - h) * 0.5f;
2071 vtx[0] = x; vtx[1] = y;
2072 vtx[2] = x; vtx[3] = y + h;
2073 vtx[4] = x + w; vtx[5] = y + h;
2074 vtx[6] = x + w; vtx[7] = y;
2075 }
2076 };
2077
2078 class h_stretch {
2079 const GLfloat hw_w, hw_h;
2080 public:
2081 h_stretch(uint32_t hw_w, uint32_t hw_h)
2082 : hw_w(hw_w), hw_h(hw_h) {
2083 }
2084 void operator()(GLfloat* vtx, float v) {
2085 const GLfloat w = hw_w - (hw_w * v);
2086 const GLfloat h = 1.0f;
2087 const GLfloat x = (hw_w - w) * 0.5f;
2088 const GLfloat y = (hw_h - h) * 0.5f;
2089 vtx[0] = x; vtx[1] = y;
2090 vtx[2] = x; vtx[3] = y + h;
2091 vtx[4] = x + w; vtx[5] = y + h;
2092 vtx[6] = x + w; vtx[7] = y;
2093 }
2094 };
2095
Mathias Agopiana6546e52010-10-14 12:33:07 -07002096 // the full animation is 12 frames
2097 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002098 s_curve_interpolator itr(nbFrames, 7.5f);
2099 s_curve_interpolator itg(nbFrames, 8.0f);
2100 s_curve_interpolator itb(nbFrames, 8.5f);
2101
2102 h_stretch hverts(hw_w, hw_h);
2103 glDisable(GL_BLEND);
2104 glDisable(GL_TEXTURE_2D);
2105 glColorMask(1,1,1,1);
2106 for (int i=nbFrames-1 ; i>=0 ; i--) {
2107 const float v = itg(i);
2108 hverts(vtx, v);
2109 glClear(GL_COLOR_BUFFER_BIT);
2110 glColor4f(1-v, 1-v, 1-v, 1);
2111 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2112 hw.flip(screenBounds);
2113 }
2114
Mathias Agopiana6546e52010-10-14 12:33:07 -07002115 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002116 v_stretch vverts(hw_w, hw_h);
2117 glEnable(GL_BLEND);
2118 glBlendFunc(GL_ONE, GL_ONE);
2119 for (int i=nbFrames-1 ; i>=0 ; i--) {
2120 float x, y, w, h;
2121 const float vr = itr(i);
2122 const float vg = itg(i);
2123 const float vb = itb(i);
2124
2125 // clear screen
2126 glColorMask(1,1,1,1);
2127 glClear(GL_COLOR_BUFFER_BIT);
2128 glEnable(GL_TEXTURE_2D);
2129
2130 // draw the red plane
2131 vverts(vtx, vr);
2132 glColorMask(1,0,0,1);
2133 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2134
2135 // draw the green plane
2136 vverts(vtx, vg);
2137 glColorMask(0,1,0,1);
2138 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2139
2140 // draw the blue plane
2141 vverts(vtx, vb);
2142 glColorMask(0,0,1,1);
2143 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2144
2145 hw.flip(screenBounds);
2146 }
2147
2148 glColorMask(1,1,1,1);
2149 glEnable(GL_SCISSOR_TEST);
2150 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002151 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002152 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002153 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002154
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002155 return NO_ERROR;
2156}
2157
2158// ---------------------------------------------------------------------------
2159
Mathias Agopianabd671a2010-10-14 14:54:06 -07002160status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002161{
2162 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2163 if (!hw.canDraw()) {
2164 // we're already off
2165 return NO_ERROR;
2166 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002167
2168 // turn off hwc while we're doing the animation
2169 hw.getHwComposer().disable();
2170 // and make sure to turn it back on (if needed) next time we compose
2171 invalidateHwcGeometry();
2172
Mathias Agopianabd671a2010-10-14 14:54:06 -07002173 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2174 electronBeamOffAnimationImplLocked();
2175 }
2176
2177 // always clear the whole screen at the end of the animation
2178 glClearColor(0,0,0,1);
2179 glDisable(GL_SCISSOR_TEST);
2180 glClear(GL_COLOR_BUFFER_BIT);
2181 glEnable(GL_SCISSOR_TEST);
2182 hw.flip( Region(hw.bounds()) );
2183
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002184 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002185}
2186
2187status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2188{
Mathias Agopian59119e62010-10-11 12:37:43 -07002189 class MessageTurnElectronBeamOff : public MessageBase {
2190 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002191 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002192 status_t result;
2193 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002194 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2195 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002196 }
2197 status_t getResult() const {
2198 return result;
2199 }
2200 virtual bool handler() {
2201 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002202 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002203 return true;
2204 }
2205 };
2206
Mathias Agopianabd671a2010-10-14 14:54:06 -07002207 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002208 status_t res = postMessageSync(msg);
2209 if (res == NO_ERROR) {
2210 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002211
2212 // work-around: when the power-manager calls us we activate the
2213 // animation. eventually, the "on" animation will be called
2214 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002215 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002216 }
2217 return res;
2218}
2219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002220// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002221
Mathias Agopianabd671a2010-10-14 14:54:06 -07002222status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002223{
2224 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2225 if (hw.canDraw()) {
2226 // we're already on
2227 return NO_ERROR;
2228 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002229 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2230 electronBeamOnAnimationImplLocked();
2231 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002232
2233 // make sure to redraw the whole screen when the animation is done
2234 mDirtyRegion.set(hw.bounds());
2235 signalEvent();
2236
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002237 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002238}
2239
2240status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2241{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002242 class MessageTurnElectronBeamOn : public MessageBase {
2243 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002244 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002245 status_t result;
2246 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002247 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2248 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002249 }
2250 status_t getResult() const {
2251 return result;
2252 }
2253 virtual bool handler() {
2254 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002256 return true;
2257 }
2258 };
2259
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002261 return NO_ERROR;
2262}
2263
2264// ---------------------------------------------------------------------------
2265
Mathias Agopian74c40c02010-09-29 13:02:36 -07002266status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2267 sp<IMemoryHeap>* heap,
2268 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002269 uint32_t sw, uint32_t sh,
2270 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002271{
2272 status_t result = PERMISSION_DENIED;
2273
2274 // only one display supported for now
2275 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2276 return BAD_VALUE;
2277
2278 if (!GLExtensions::getInstance().haveFramebufferObject())
2279 return INVALID_OPERATION;
2280
2281 // get screen geometry
2282 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2283 const uint32_t hw_w = hw.getWidth();
2284 const uint32_t hw_h = hw.getHeight();
2285
2286 if ((sw > hw_w) || (sh > hw_h))
2287 return BAD_VALUE;
2288
2289 sw = (!sw) ? hw_w : sw;
2290 sh = (!sh) ? hw_h : sh;
2291 const size_t size = sw * sh * 4;
2292
Mathias Agopian1c71a472011-03-02 18:45:50 -08002293 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2294 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002295
Mathias Agopian74c40c02010-09-29 13:02:36 -07002296 // make sure to clear all GL error flags
2297 while ( glGetError() != GL_NO_ERROR ) ;
2298
2299 // create a FBO
2300 GLuint name, tname;
2301 glGenRenderbuffersOES(1, &tname);
2302 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2303 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2304 glGenFramebuffersOES(1, &name);
2305 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2306 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2307 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2308
2309 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002310
Mathias Agopian74c40c02010-09-29 13:02:36 -07002311 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2312
2313 // invert everything, b/c glReadPixel() below will invert the FB
2314 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002315 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002316 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002317 glMatrixMode(GL_PROJECTION);
2318 glPushMatrix();
2319 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002320 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002321 glMatrixMode(GL_MODELVIEW);
2322
2323 // redraw the screen entirely...
2324 glClearColor(0,0,0,1);
2325 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002326
Jamie Gennis9575f602011-10-07 14:51:16 -07002327 const LayerVector& layers(mDrawingState.layersSortedByZ);
2328 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 for (size_t i=0 ; i<count ; ++i) {
2330 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002331 const uint32_t flags = layer->drawingState().flags;
2332 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2333 const uint32_t z = layer->drawingState().z;
2334 if (z >= minLayerZ && z <= maxLayerZ) {
2335 layer->drawForSreenShot();
2336 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002337 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002338 }
2339
2340 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002341 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002342 glScissor(0, 0, sw, sh);
2343
2344 // check for errors and return screen capture
2345 if (glGetError() != GL_NO_ERROR) {
2346 // error while rendering
2347 result = INVALID_OPERATION;
2348 } else {
2349 // allocate shared memory large enough to hold the
2350 // screen capture
2351 sp<MemoryHeapBase> base(
2352 new MemoryHeapBase(size, 0, "screen-capture") );
2353 void* const ptr = base->getBase();
2354 if (ptr) {
2355 // capture the screen with glReadPixels()
2356 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2357 if (glGetError() == GL_NO_ERROR) {
2358 *heap = base;
2359 *w = sw;
2360 *h = sh;
2361 *f = PIXEL_FORMAT_RGBA_8888;
2362 result = NO_ERROR;
2363 }
2364 } else {
2365 result = NO_MEMORY;
2366 }
2367 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002368 glEnable(GL_SCISSOR_TEST);
2369 glViewport(0, 0, hw_w, hw_h);
2370 glMatrixMode(GL_PROJECTION);
2371 glPopMatrix();
2372 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 } else {
2374 result = BAD_VALUE;
2375 }
2376
2377 // release FBO resources
2378 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2379 glDeleteRenderbuffersOES(1, &tname);
2380 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002381
2382 hw.compositionComplete();
2383
Mathias Agopian1c71a472011-03-02 18:45:50 -08002384 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002385
Mathias Agopian74c40c02010-09-29 13:02:36 -07002386 return result;
2387}
2388
2389
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002390status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2391 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002393 uint32_t sw, uint32_t sh,
2394 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002395{
2396 // only one display supported for now
2397 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2398 return BAD_VALUE;
2399
2400 if (!GLExtensions::getInstance().haveFramebufferObject())
2401 return INVALID_OPERATION;
2402
2403 class MessageCaptureScreen : public MessageBase {
2404 SurfaceFlinger* flinger;
2405 DisplayID dpy;
2406 sp<IMemoryHeap>* heap;
2407 uint32_t* w;
2408 uint32_t* h;
2409 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002410 uint32_t sw;
2411 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002412 uint32_t minLayerZ;
2413 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002414 status_t result;
2415 public:
2416 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002418 uint32_t sw, uint32_t sh,
2419 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002420 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002421 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2422 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2423 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002424 {
2425 }
2426 status_t getResult() const {
2427 return result;
2428 }
2429 virtual bool handler() {
2430 Mutex::Autolock _l(flinger->mStateLock);
2431
2432 // if we have secure windows, never allow the screen capture
2433 if (flinger->mSecureFrameBuffer)
2434 return true;
2435
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002437 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002438
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002439 return true;
2440 }
2441 };
2442
2443 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002444 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002445 status_t res = postMessageSync(msg);
2446 if (res == NO_ERROR) {
2447 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2448 }
2449 return res;
2450}
2451
2452// ---------------------------------------------------------------------------
2453
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002454sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2455{
2456 sp<Layer> result;
2457 Mutex::Autolock _l(mStateLock);
2458 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2459 return result;
2460}
2461
2462// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002463
Mathias Agopian96f08192010-06-02 23:28:45 -07002464Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002465 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002466{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002467}
2468
Mathias Agopian96f08192010-06-02 23:28:45 -07002469Client::~Client()
2470{
Mathias Agopian96f08192010-06-02 23:28:45 -07002471 const size_t count = mLayers.size();
2472 for (size_t i=0 ; i<count ; i++) {
2473 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2474 if (layer != 0) {
2475 mFlinger->removeLayer(layer);
2476 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 }
2478}
2479
Mathias Agopian96f08192010-06-02 23:28:45 -07002480status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002481 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002483
Mathias Agopian4f113742011-05-03 16:21:41 -07002484size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002485{
Mathias Agopian4f113742011-05-03 16:21:41 -07002486 Mutex::Autolock _l(mLock);
2487 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002488 mLayers.add(name, layer);
2489 return name;
2490}
2491
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002492void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002493{
Mathias Agopian4f113742011-05-03 16:21:41 -07002494 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002495 // we do a linear search here, because this doesn't happen often
2496 const size_t count = mLayers.size();
2497 for (size_t i=0 ; i<count ; i++) {
2498 if (mLayers.valueAt(i) == layer) {
2499 mLayers.removeItemsAt(i, 1);
2500 break;
2501 }
2502 }
2503}
Mathias Agopian4f113742011-05-03 16:21:41 -07002504sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2505{
2506 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002507 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002508 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002509 if (layer != 0) {
2510 lbc = layer.promote();
2511 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002512 }
2513 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514}
2515
Mathias Agopiana67932f2011-04-20 14:20:59 -07002516
2517status_t Client::onTransact(
2518 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2519{
2520 // these must be checked
2521 IPCThreadState* ipc = IPCThreadState::self();
2522 const int pid = ipc->getCallingPid();
2523 const int uid = ipc->getCallingUid();
2524 const int self_pid = getpid();
2525 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2526 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002527 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002528 {
2529 LOGE("Permission Denial: "
2530 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2531 return PERMISSION_DENIED;
2532 }
2533 }
2534 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002535}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002536
2537
Mathias Agopian96f08192010-06-02 23:28:45 -07002538sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002539 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002540 const String8& name,
2541 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002542 uint32_t flags)
2543{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002544 /*
2545 * createSurface must be called from the GL thread so that it can
2546 * have access to the GL context.
2547 */
2548
2549 class MessageCreateSurface : public MessageBase {
2550 sp<ISurface> result;
2551 SurfaceFlinger* flinger;
2552 ISurfaceComposerClient::surface_data_t* params;
2553 Client* client;
2554 const String8& name;
2555 DisplayID display;
2556 uint32_t w, h;
2557 PixelFormat format;
2558 uint32_t flags;
2559 public:
2560 MessageCreateSurface(SurfaceFlinger* flinger,
2561 ISurfaceComposerClient::surface_data_t* params,
2562 const String8& name, Client* client,
2563 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2564 uint32_t flags)
2565 : flinger(flinger), params(params), client(client), name(name),
2566 display(display), w(w), h(h), format(format), flags(flags)
2567 {
2568 }
2569 sp<ISurface> getResult() const { return result; }
2570 virtual bool handler() {
2571 result = flinger->createSurface(params, name, client,
2572 display, w, h, format, flags);
2573 return true;
2574 }
2575 };
2576
2577 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2578 params, name, this, display, w, h, format, flags);
2579 mFlinger->postMessageSync(msg);
2580 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581}
Mathias Agopian96f08192010-06-02 23:28:45 -07002582status_t Client::destroySurface(SurfaceID sid) {
2583 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002584}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002585
2586// ---------------------------------------------------------------------------
2587
Jamie Gennis9a78c902011-01-12 18:30:40 -08002588GraphicBufferAlloc::GraphicBufferAlloc() {}
2589
2590GraphicBufferAlloc::~GraphicBufferAlloc() {}
2591
2592sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002593 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002594 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2595 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002596 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002597 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002598 if (err == NO_MEMORY) {
2599 GraphicBuffer::dumpAllocationsToSystemLog();
2600 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002601 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2602 "failed (%s), handle=%p",
2603 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002604 return 0;
2605 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002606 return graphicBuffer;
2607}
2608
Jamie Gennis9a78c902011-01-12 18:30:40 -08002609// ---------------------------------------------------------------------------
2610
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002611GraphicPlane::GraphicPlane()
2612 : mHw(0)
2613{
2614}
2615
2616GraphicPlane::~GraphicPlane() {
2617 delete mHw;
2618}
2619
2620bool GraphicPlane::initialized() const {
2621 return mHw ? true : false;
2622}
2623
Mathias Agopian2b92d892010-02-08 15:49:35 -08002624int GraphicPlane::getWidth() const {
2625 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626}
2627
Mathias Agopian2b92d892010-02-08 15:49:35 -08002628int GraphicPlane::getHeight() const {
2629 return mHeight;
2630}
2631
2632void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2633{
2634 mHw = hw;
2635
2636 // initialize the display orientation transform.
2637 // it's a constant that should come from the display driver.
2638 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2639 char property[PROPERTY_VALUE_MAX];
2640 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2641 //displayOrientation
2642 switch (atoi(property)) {
2643 case 90:
2644 displayOrientation = ISurfaceComposer::eOrientation90;
2645 break;
2646 case 270:
2647 displayOrientation = ISurfaceComposer::eOrientation270;
2648 break;
2649 }
2650 }
2651
2652 const float w = hw->getWidth();
2653 const float h = hw->getHeight();
2654 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2655 &mDisplayTransform);
2656 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2657 mDisplayWidth = h;
2658 mDisplayHeight = w;
2659 } else {
2660 mDisplayWidth = w;
2661 mDisplayHeight = h;
2662 }
2663
2664 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002665}
2666
2667status_t GraphicPlane::orientationToTransfrom(
2668 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002669{
2670 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002671 switch (orientation) {
2672 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002673 flags = Transform::ROT_0;
2674 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002676 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002677 break;
2678 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002679 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002680 break;
2681 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002682 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002683 break;
2684 default:
2685 return BAD_VALUE;
2686 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002687 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688 return NO_ERROR;
2689}
2690
2691status_t GraphicPlane::setOrientation(int orientation)
2692{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002693 // If the rotation can be handled in hardware, this is where
2694 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002695
2696 const DisplayHardware& hw(displayHardware());
2697 const float w = mDisplayWidth;
2698 const float h = mDisplayHeight;
2699 mWidth = int(w);
2700 mHeight = int(h);
2701
2702 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002703 GraphicPlane::orientationToTransfrom(orientation, w, h,
2704 &orientationTransform);
2705 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2706 mWidth = int(h);
2707 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002708 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002709
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002710 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002711 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002712 return NO_ERROR;
2713}
2714
2715const DisplayHardware& GraphicPlane::displayHardware() const {
2716 return *mHw;
2717}
2718
Mathias Agopian59119e62010-10-11 12:37:43 -07002719DisplayHardware& GraphicPlane::editDisplayHardware() {
2720 return *mHw;
2721}
2722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002723const Transform& GraphicPlane::transform() const {
2724 return mGlobalTransform;
2725}
2726
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002727EGLDisplay GraphicPlane::getEGLDisplay() const {
2728 return mHw->getEGLDisplay();
2729}
2730
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002731// ---------------------------------------------------------------------------
2732
2733}; // namespace android