blob: bfb5cf4a9de87efb45db18fc66fddecb458751fa [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),
Jamie Gennis28378392011-10-12 17:39:00 -070082 mTransationPending(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 Agopian8a77baa2009-09-27 22:47:27 -0700454 if (LIKELY(hw.canDraw() && !isFrozen())) {
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 Agopian0656a682011-09-20 17:22:44 -0700481 if (!mSwapRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700483 const nsecs_t now = systemTime();
484 mDebugInSwapBuffers = now;
Mathias Agopian0656a682011-09-20 17:22:44 -0700485 hw.flip(mSwapRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700486 mLastSwapBufferTime = systemTime() - now;
487 mDebugInSwapBuffers = 0;
Mathias Agopian0656a682011-09-20 17:22:44 -0700488 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 }
490}
491
492void SurfaceFlinger::handleConsoleEvents()
493{
494 // something to do with the console
495 const DisplayHardware& hw = graphicPlane(0).displayHardware();
496
497 int what = android_atomic_and(0, &mConsoleSignals);
498 if (what & eConsoleAcquired) {
499 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700500 // this is a temporary work-around, eventually this should be called
501 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700502 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 }
504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700506 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 }
509 }
510
511 mDirtyRegion.set(hw.bounds());
512}
513
514void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
515{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700516 Mutex::Autolock _l(mStateLock);
517 const nsecs_t now = systemTime();
518 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519
Mathias Agopianca4d3602011-05-19 15:38:14 -0700520 // Here we're guaranteed that some transaction flags are set
521 // so we can call handleTransactionLocked() unconditionally.
522 // We call getTransactionFlags(), which will also clear the flags,
523 // with mStateLock held to guarantee that mCurrentState won't change
524 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700525
Mathias Agopianca4d3602011-05-19 15:38:14 -0700526 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
527 transactionFlags = getTransactionFlags(mask);
528 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700529
Mathias Agopianca4d3602011-05-19 15:38:14 -0700530 mLastTransactionTime = systemTime() - now;
531 mDebugInTransaction = 0;
532 invalidateHwcGeometry();
533 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700534}
535
Mathias Agopianca4d3602011-05-19 15:38:14 -0700536void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700537{
538 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800539 const size_t count = currentLayers.size();
540
541 /*
542 * Traversal of the children
543 * (perform the transaction for each of them if needed)
544 */
545
546 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
547 if (layersNeedTransaction) {
548 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700549 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
551 if (!trFlags) continue;
552
553 const uint32_t flags = layer->doTransaction(0);
554 if (flags & Layer::eVisibleRegion)
555 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
557 }
558
559 /*
560 * Perform our own transaction if needed
561 */
562
563 if (transactionFlags & eTransactionNeeded) {
564 if (mCurrentState.orientation != mDrawingState.orientation) {
565 // the orientation has changed, recompute all visible regions
566 // and invalidate everything.
567
568 const int dpy = 0;
569 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700570 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 GraphicPlane& plane(graphicPlane(dpy));
572 plane.setOrientation(orientation);
573
574 // update the shared control block
575 const DisplayHardware& hw(plane.displayHardware());
576 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
577 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800578 dcblk->w = plane.getWidth();
579 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580
581 mVisibleRegionsDirty = true;
582 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 }
584
585 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
586 // freezing or unfreezing the display -> trigger animation if needed
587 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800588 if (mFreezeDisplay)
589 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 }
591
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700592 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
593 // layers have been added
594 mVisibleRegionsDirty = true;
595 }
596
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 // some layers might have been removed, so
598 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700600 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700602 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700603 const size_t count = previousLayers.size();
604 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700605 const sp<LayerBase>& layer(previousLayers[i]);
606 if (currentLayers.indexOf( layer ) < 0) {
607 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700608 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700609 }
610 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 }
613
614 commitTransaction();
615}
616
617sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
618{
619 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
620}
621
622void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800623 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624{
625 const GraphicPlane& plane(graphicPlane(0));
626 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700627 const DisplayHardware& hw(plane.displayHardware());
628 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629
630 Region aboveOpaqueLayers;
631 Region aboveCoveredLayers;
632 Region dirty;
633
634 bool secureFrameBuffer = false;
635
636 size_t i = currentLayers.size();
637 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700638 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 layer->validateVisibility(planeTransform);
640
641 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700642 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643
Mathias Agopianab028732010-03-16 16:41:46 -0700644 /*
645 * opaqueRegion: area of a surface that is fully opaque.
646 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700648
649 /*
650 * visibleRegion: area of a surface that is visible on screen
651 * and not fully transparent. This is essentially the layer's
652 * footprint minus the opaque regions above it.
653 * Areas covered by a translucent surface are considered visible.
654 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700656
657 /*
658 * coveredRegion: area of a surface that is covered by all
659 * visible regions above it (which includes the translucent areas).
660 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700662
663
664 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700665 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700666 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700667 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700669 visibleRegion.andSelf(screenRegion);
670 if (!visibleRegion.isEmpty()) {
671 // Remove the transparent area from the visible region
672 if (translucent) {
673 visibleRegion.subtractSelf(layer->transparentRegionScreen);
674 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675
Mathias Agopianab028732010-03-16 16:41:46 -0700676 // compute the opaque region
677 const int32_t layerOrientation = layer->getOrientation();
678 if (s.alpha==255 && !translucent &&
679 ((layerOrientation & Transform::ROT_INVALID) == false)) {
680 // the opaque region is the layer's footprint
681 opaqueRegion = visibleRegion;
682 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 }
684 }
685
Mathias Agopianab028732010-03-16 16:41:46 -0700686 // Clip the covered region to the visible region
687 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
688
689 // Update aboveCoveredLayers for next (lower) layer
690 aboveCoveredLayers.orSelf(visibleRegion);
691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 // subtract the opaque region covered by the layers above us
693 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694
695 // compute this layer's dirty region
696 if (layer->contentDirty) {
697 // we need to invalidate the whole region
698 dirty = visibleRegion;
699 // as well, as the old visible region
700 dirty.orSelf(layer->visibleRegionScreen);
701 layer->contentDirty = false;
702 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700703 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700704 * the exposed region consists of two components:
705 * 1) what's VISIBLE now and was COVERED before
706 * 2) what's EXPOSED now less what was EXPOSED before
707 *
708 * note that (1) is conservative, we start with the whole
709 * visible region but only keep what used to be covered by
710 * something -- which mean it may have been exposed.
711 *
712 * (2) handles areas that were not covered by anything but got
713 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700714 */
Mathias Agopianab028732010-03-16 16:41:46 -0700715 const Region newExposed = visibleRegion - coveredRegion;
716 const Region oldVisibleRegion = layer->visibleRegionScreen;
717 const Region oldCoveredRegion = layer->coveredRegionScreen;
718 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
719 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 }
721 dirty.subtractSelf(aboveOpaqueLayers);
722
723 // accumulate to the screen dirty region
724 dirtyRegion.orSelf(dirty);
725
Mathias Agopianab028732010-03-16 16:41:46 -0700726 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 // Store the visible region is screen space
730 layer->setVisibleRegion(visibleRegion);
731 layer->setCoveredRegion(coveredRegion);
732
Mathias Agopian97011222009-07-28 10:57:27 -0700733 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 if (layer->isSecure() && !visibleRegion.isEmpty()) {
735 secureFrameBuffer = true;
736 }
737 }
738
Mathias Agopian97011222009-07-28 10:57:27 -0700739 // invalidate the areas where a layer was removed
740 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
741 mDirtyRegionRemovedLayer.clear();
742
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 mSecureFrameBuffer = secureFrameBuffer;
744 opaqueRegion = aboveOpaqueLayers;
745}
746
747
748void SurfaceFlinger::commitTransaction()
749{
750 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700751 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700752 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753}
754
755void SurfaceFlinger::handlePageFlip()
756{
757 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800758 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 visibleRegions |= lockPageFlip(currentLayers);
760
761 const DisplayHardware& hw = graphicPlane(0).displayHardware();
762 const Region screenRegion(hw.bounds());
763 if (visibleRegions) {
764 Region opaqueRegion;
765 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700766
767 /*
768 * rebuild the visible layer list
769 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800770 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700771 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700772 mVisibleLayersSortedByZ.setCapacity(count);
773 for (size_t i=0 ; i<count ; i++) {
774 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
775 mVisibleLayersSortedByZ.add(currentLayers[i]);
776 }
777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 mWormholeRegion = screenRegion.subtract(opaqueRegion);
779 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800780 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 }
782
783 unlockPageFlip(currentLayers);
784 mDirtyRegion.andSelf(screenRegion);
785}
786
Mathias Agopianad456f92011-01-13 17:53:01 -0800787void SurfaceFlinger::invalidateHwcGeometry()
788{
789 mHwWorkListDirty = true;
790}
791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
793{
794 bool recomputeVisibleRegions = false;
795 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700796 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800797 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700798 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 layer->lockPageFlip(recomputeVisibleRegions);
800 }
801 return recomputeVisibleRegions;
802}
803
804void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
805{
806 const GraphicPlane& plane(graphicPlane(0));
807 const Transform& planeTransform(plane.transform());
808 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700809 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700811 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 layer->unlockPageFlip(planeTransform, mDirtyRegion);
813 }
814}
815
Mathias Agopiana350ff92010-08-10 17:14:02 -0700816void SurfaceFlinger::handleWorkList()
817{
818 mHwWorkListDirty = false;
819 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
820 if (hwc.initCheck() == NO_ERROR) {
821 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
822 const size_t count = currentLayers.size();
823 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700824 hwc_layer_t* const cur(hwc.getLayers());
825 for (size_t i=0 ; cur && i<count ; i++) {
826 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700827 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700828 cur[i].compositionType = HWC_FRAMEBUFFER;
829 cur[i].flags |= HWC_SKIP_LAYER;
830 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700831 }
832 }
833}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835void SurfaceFlinger::handleRepaint()
836{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700837 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700838 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839
840 if (UNLIKELY(mDebugRegion)) {
841 debugFlashRegions();
842 }
843
Mathias Agopianb8a55602009-06-26 19:06:36 -0700844 // set the frame buffer
845 const DisplayHardware& hw(graphicPlane(0).displayHardware());
846 glMatrixMode(GL_MODELVIEW);
847 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848
849 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700850 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
851 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700852 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700853 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
854 // takes a rectangle, we must make sure to update that whole
855 // rectangle in that case
856 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700857 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700858 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700859 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700861 // in the BUFFER_PRESERVED case, obviously, we can update only
862 // what's needed and nothing more.
863 // NOTE: this is NOT a common case, as preserving the backbuffer
864 // is costly and usually involves copying the whole update back.
865 }
866 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700867 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700868 // We need to redraw the rectangle that will be updated
869 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700870 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700871 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700872 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700873 } else {
874 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700876 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800877 }
878 }
879
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700880 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 composeSurfaces(mDirtyRegion);
882
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700883 // update the swap region and clear the dirty region
884 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 mDirtyRegion.clear();
886}
887
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700888void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700890 const DisplayHardware& hw(graphicPlane(0).displayHardware());
891 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700892 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700893 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700894 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700896
Mathias Agopianf384cc32011-09-08 18:31:55 -0700897 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
898 size_t count = layers.size();
899
900 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700901 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
902 hwc.getNumLayers(), count);
903
904 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700905 if (hwc.initCheck() == NO_ERROR) {
906 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
907 }
Mathias Agopian45721772010-08-12 15:03:26 -0700908
909 /*
910 * update the per-frame h/w composer data for each layer
911 * and build the transparent region of the FB
912 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700913 for (size_t i=0 ; i<count ; i++) {
914 const sp<LayerBase>& layer(layers[i]);
915 layer->setPerFrameData(&cur[i]);
916 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700917 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700918 status_t err = hwc.prepare();
919 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700920
Mathias Agopianf384cc32011-09-08 18:31:55 -0700921 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700922 // what's happening here is tricky.
923 // we want to clear all the layers with the CLEAR_FB flags
924 // that are opaque.
925 // however, since some GPU are efficient at preserving
926 // the backbuffer, we want to take advantage of that so we do the
927 // clear only in the dirty region (other areas will be preserved
928 // on those GPUs).
929 // NOTE: on non backbuffer preserving GPU, the dirty region
930 // has already been expanded as needed, so the code is correct
931 // there too.
932 //
933 // However, the content of the framebuffer cannot be trusted when
934 // we switch to/from FB/OVERLAY, in which case we need to
935 // expand the dirty region to those areas too.
936 //
937 // Note also that there is a special case when switching from
938 // "no layers in FB" to "some layers in FB", where we need to redraw
939 // the entire FB, since some areas might contain uninitialized
940 // data.
941 //
942 // Also we want to make sure to not clear areas that belong to
943 // layers above that won't redraw (we would just erasing them),
944 // that is, we can't erase anything outside the dirty region.
945
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700946 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700947
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700948 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
949 transparent.set(hw.getBounds());
950 dirtyInOut = transparent;
951 } else {
952 for (size_t i=0 ; i<count ; i++) {
953 const sp<LayerBase>& layer(layers[i]);
954 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
955 transparent.orSelf(layer->visibleRegionScreen);
956 }
957 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
958 if (isOverlay != layer->isOverlay()) {
959 // we transitioned to/from overlay, so add this layer
960 // to the dirty region so the framebuffer can be either
961 // cleared or redrawn.
962 dirtyInOut.orSelf(layer->visibleRegionScreen);
963 }
964 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800965 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700966 // don't erase stuff outside the dirty region
967 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700968 }
969
970 /*
971 * clear the area of the FB that need to be transparent
972 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700973 if (!transparent.isEmpty()) {
974 glClearColor(0,0,0,0);
975 Region::const_iterator it = transparent.begin();
976 Region::const_iterator const end = transparent.end();
977 const int32_t height = hw.getHeight();
978 while (it != end) {
979 const Rect& r(*it++);
980 const GLint sy = height - (r.top + r.height());
981 glScissor(r.left, sy, r.width(), r.height());
982 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800983 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700984 }
985 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700986}
Mathias Agopian45721772010-08-12 15:03:26 -0700987
Mathias Agopianf384cc32011-09-08 18:31:55 -0700988void SurfaceFlinger::composeSurfaces(const Region& dirty)
989{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700990 const DisplayHardware& hw(graphicPlane(0).displayHardware());
991 HWComposer& hwc(hw.getHwComposer());
992
993 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
994 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700995 // should never happen unless the window manager has a bug
996 // draw something...
997 drawWormhole();
998 }
999
Mathias Agopian45721772010-08-12 15:03:26 -07001000 /*
1001 * and then, render the layers targeted at the framebuffer
1002 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001003 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001004 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1005 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001006 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001007 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001008 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001009 }
1010 const sp<LayerBase>& layer(layers[i]);
1011 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1012 if (!clip.isEmpty()) {
1013 layer->draw(clip);
1014 }
1015 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016}
1017
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018void SurfaceFlinger::debugFlashRegions()
1019{
Mathias Agopian0a917752010-06-14 21:20:00 -07001020 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1021 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001022 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001023 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001024 return;
1025 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001026
Mathias Agopian0a917752010-06-14 21:20:00 -07001027 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1028 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1029 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1030 mDirtyRegion.bounds() : hw.bounds());
1031 composeSurfaces(repaint);
1032 }
1033
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 glDisable(GL_BLEND);
1035 glDisable(GL_DITHER);
1036 glDisable(GL_SCISSOR_TEST);
1037
Mathias Agopian0926f502009-05-04 14:17:04 -07001038 static int toggle = 0;
1039 toggle = 1 - toggle;
1040 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001041 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001042 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001043 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001044 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045
Mathias Agopian20f68782009-05-11 00:03:41 -07001046 Region::const_iterator it = mDirtyRegion.begin();
1047 Region::const_iterator const end = mDirtyRegion.end();
1048 while (it != end) {
1049 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001051 { r.left, height - r.top },
1052 { r.left, height - r.bottom },
1053 { r.right, height - r.bottom },
1054 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 };
1056 glVertexPointer(2, GL_FLOAT, 0, vertices);
1057 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1058 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001059
Mathias Agopian0656a682011-09-20 17:22:44 -07001060 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061
1062 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001063 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064
1065 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066}
1067
1068void SurfaceFlinger::drawWormhole() const
1069{
1070 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1071 if (region.isEmpty())
1072 return;
1073
1074 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1075 const int32_t width = hw.getWidth();
1076 const int32_t height = hw.getHeight();
1077
1078 glDisable(GL_BLEND);
1079 glDisable(GL_DITHER);
1080
1081 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001082 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001083 Region::const_iterator it = region.begin();
1084 Region::const_iterator const end = region.end();
1085 while (it != end) {
1086 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 const GLint sy = height - (r.top + r.height());
1088 glScissor(r.left, sy, r.width(), r.height());
1089 glClear(GL_COLOR_BUFFER_BIT);
1090 }
1091 } else {
1092 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1093 { width, height }, { 0, height } };
1094 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1095 glVertexPointer(2, GL_SHORT, 0, vertices);
1096 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1097 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001098#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001099 if (GLExtensions::getInstance().haveTextureExternal()) {
1100 glDisable(GL_TEXTURE_EXTERNAL_OES);
1101 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001102#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 glEnable(GL_TEXTURE_2D);
1104 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1105 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1106 glMatrixMode(GL_TEXTURE);
1107 glLoadIdentity();
1108 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001109 Region::const_iterator it = region.begin();
1110 Region::const_iterator const end = region.end();
1111 while (it != end) {
1112 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 const GLint sy = height - (r.top + r.height());
1114 glScissor(r.left, sy, r.width(), r.height());
1115 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1116 }
1117 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001118 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001119 glLoadIdentity();
1120 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 }
1122}
1123
1124void SurfaceFlinger::debugShowFPS() const
1125{
1126 static int mFrameCount;
1127 static int mLastFrameCount = 0;
1128 static nsecs_t mLastFpsTime = 0;
1129 static float mFps = 0;
1130 mFrameCount++;
1131 nsecs_t now = systemTime();
1132 nsecs_t diff = now - mLastFpsTime;
1133 if (diff > ms2ns(250)) {
1134 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1135 mLastFpsTime = now;
1136 mLastFrameCount = mFrameCount;
1137 }
1138 // XXX: mFPS has the value we want
1139 }
1140
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001141status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142{
1143 Mutex::Autolock _l(mStateLock);
1144 addLayer_l(layer);
1145 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1146 return NO_ERROR;
1147}
1148
Mathias Agopian96f08192010-06-02 23:28:45 -07001149status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1150{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001151 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001152 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1153}
1154
1155ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1156 const sp<LayerBaseClient>& lbc)
1157{
Mathias Agopian96f08192010-06-02 23:28:45 -07001158 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001159 size_t name = client->attachLayer(lbc);
1160
1161 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001162
1163 // add this layer to the current state list
1164 addLayer_l(lbc);
1165
Mathias Agopian4f113742011-05-03 16:21:41 -07001166 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001167}
1168
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001169status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170{
1171 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001172 status_t err = purgatorizeLayer_l(layer);
1173 if (err == NO_ERROR)
1174 setTransactionFlags(eTransactionNeeded);
1175 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176}
1177
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001178status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001180 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1181 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001182 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001183 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1185 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001186 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 return NO_ERROR;
1188 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001189 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190}
1191
Mathias Agopian9a112062009-04-17 19:36:26 -07001192status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1193{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001194 // First add the layer to the purgatory list, which makes sure it won't
1195 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001196 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001197 if (err >= 0) {
1198 mLayerPurgatory.add(layerBase);
1199 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001200
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001201 layerBase->onRemoved();
1202
Mathias Agopian3d579642009-06-04 18:46:21 -07001203 // it's possible that we don't find a layer, because it might
1204 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001205 // from the user because there is a race between Client::destroySurface(),
1206 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001207 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1208}
1209
Mathias Agopian96f08192010-06-02 23:28:45 -07001210status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211{
Mathias Agopian96f08192010-06-02 23:28:45 -07001212 layer->forceVisibilityTransaction();
1213 setTransactionFlags(eTraversalNeeded);
1214 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215}
1216
Mathias Agopiandea20b12011-05-03 17:04:02 -07001217uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1218{
1219 return android_atomic_release_load(&mTransactionFlags);
1220}
1221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1223{
1224 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1225}
1226
Mathias Agopianbb641242010-05-18 17:06:55 -07001227uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228{
1229 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1230 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001231 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 }
1233 return old;
1234}
1235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001237void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001238 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001239 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001240
Jamie Gennis28378392011-10-12 17:39:00 -07001241 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001242 if (mCurrentState.orientation != orientation) {
1243 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1244 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001245 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001246 } else if (orientation != eOrientationUnchanged) {
1247 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1248 orientation);
1249 }
1250 }
1251
Mathias Agopian698c0872011-06-28 19:09:31 -07001252 const size_t count = state.size();
1253 for (size_t i=0 ; i<count ; i++) {
1254 const ComposerState& s(state[i]);
1255 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001256 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001257 }
Jamie Gennis28378392011-10-12 17:39:00 -07001258 if (transactionFlags) {
1259 setTransactionFlags(transactionFlags);
Mathias Agopian698c0872011-06-28 19:09:31 -07001260 }
1261
Jamie Gennis28378392011-10-12 17:39:00 -07001262 // if this is a synchronous transaction, wait for it to take effect before
1263 // returning.
1264 if (flags & eSynchronous) {
1265 mTransationPending = true;
1266 }
1267 while (mTransationPending) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001268 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1269 if (CC_UNLIKELY(err != NO_ERROR)) {
1270 // just in case something goes wrong in SF, return to the
1271 // called after a few seconds.
1272 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Jamie Gennis28378392011-10-12 17:39:00 -07001273 mTransationPending = false;
Mathias Agopian698c0872011-06-28 19:09:31 -07001274 break;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001275 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 }
1277}
1278
1279status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1280{
1281 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1282 return BAD_VALUE;
1283
1284 Mutex::Autolock _l(mStateLock);
1285 mCurrentState.freezeDisplay = 1;
1286 setTransactionFlags(eTransactionNeeded);
1287
1288 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001289 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 return NO_ERROR;
1291}
1292
1293status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1294{
1295 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1296 return BAD_VALUE;
1297
1298 Mutex::Autolock _l(mStateLock);
1299 mCurrentState.freezeDisplay = 0;
1300 setTransactionFlags(eTransactionNeeded);
1301
1302 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001303 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 return NO_ERROR;
1305}
1306
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001307int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001308 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309{
1310 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1311 return BAD_VALUE;
1312
1313 Mutex::Autolock _l(mStateLock);
1314 if (mCurrentState.orientation != orientation) {
1315 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001316 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 mCurrentState.orientation = orientation;
1318 setTransactionFlags(eTransactionNeeded);
1319 mTransactionCV.wait(mStateLock);
1320 } else {
1321 orientation = BAD_VALUE;
1322 }
1323 }
1324 return orientation;
1325}
1326
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001327sp<ISurface> SurfaceFlinger::createSurface(
1328 ISurfaceComposerClient::surface_data_t* params,
1329 const String8& name,
1330 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1332 uint32_t flags)
1333{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001334 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001335 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001336
1337 if (int32_t(w|h) < 0) {
1338 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1339 int(w), int(h));
1340 return surfaceHandle;
1341 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001344 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345 switch (flags & eFXSurfaceMask) {
1346 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001347 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1348 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 break;
1350 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001351 // for now we treat Blur as Dim, until we can implement it
1352 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001354 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 break;
1356 }
1357
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001358 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001359 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001360 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001361 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001362
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001364 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001365 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001366 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001367 if (normalLayer != 0) {
1368 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001369 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001370 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001371 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001372
Mathias Agopian96f08192010-06-02 23:28:45 -07001373 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 }
1375
1376 return surfaceHandle;
1377}
1378
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001379sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001380 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001381 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001382 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383{
1384 // initialize the surfaces
1385 switch (format) { // TODO: take h/w into account
1386 case PIXEL_FORMAT_TRANSPARENT:
1387 case PIXEL_FORMAT_TRANSLUCENT:
1388 format = PIXEL_FORMAT_RGBA_8888;
1389 break;
1390 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001391#ifdef NO_RGBX_8888
1392 format = PIXEL_FORMAT_RGB_565;
1393#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001394 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001395#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 break;
1397 }
1398
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001399#ifdef NO_RGBX_8888
1400 if (format == PIXEL_FORMAT_RGBX_8888)
1401 format = PIXEL_FORMAT_RGBA_8888;
1402#endif
1403
Mathias Agopian96f08192010-06-02 23:28:45 -07001404 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001405 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001406 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001408 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 }
1410 return layer;
1411}
1412
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001413sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001414 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001415 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416{
Mathias Agopian96f08192010-06-02 23:28:45 -07001417 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 return layer;
1420}
1421
Mathias Agopian96f08192010-06-02 23:28:45 -07001422status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423{
Mathias Agopian9a112062009-04-17 19:36:26 -07001424 /*
1425 * called by the window manager, when a surface should be marked for
1426 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001427 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001428 * The surface is removed from the current and drawing lists, but placed
1429 * in the purgatory queue, so it's not destroyed right-away (we need
1430 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001431 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001432
Mathias Agopian48d819a2009-09-10 19:41:18 -07001433 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001434 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001435 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001436 if (layer != 0) {
1437 err = purgatorizeLayer_l(layer);
1438 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001439 setTransactionFlags(eTransactionNeeded);
1440 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001441 }
1442 return err;
1443}
1444
Mathias Agopianca4d3602011-05-19 15:38:14 -07001445status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001446{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001447 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001448 status_t err = NO_ERROR;
1449 sp<LayerBaseClient> l(layer.promote());
1450 if (l != NULL) {
1451 Mutex::Autolock _l(mStateLock);
1452 err = removeLayer_l(l);
1453 if (err == NAME_NOT_FOUND) {
1454 // The surface wasn't in the current list, which means it was
1455 // removed already, which means it is in the purgatory,
1456 // and need to be removed from there.
1457 ssize_t idx = mLayerPurgatory.remove(l);
1458 LOGE_IF(idx < 0,
1459 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001460 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001461 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1462 "error removing layer=%p (%s)", l.get(), strerror(-err));
1463 }
1464 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465}
1466
Mathias Agopian698c0872011-06-28 19:09:31 -07001467uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001468 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001469 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001472 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1473 if (layer != 0) {
1474 const uint32_t what = s.what;
1475 if (what & ePositionChanged) {
1476 if (layer->setPosition(s.x, s.y))
1477 flags |= eTraversalNeeded;
1478 }
1479 if (what & eLayerChanged) {
1480 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1481 if (layer->setLayer(s.z)) {
1482 mCurrentState.layersSortedByZ.removeAt(idx);
1483 mCurrentState.layersSortedByZ.add(layer);
1484 // we need traversal (state changed)
1485 // AND transaction (list changed)
1486 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
1488 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001489 if (what & eSizeChanged) {
1490 if (layer->setSize(s.w, s.h)) {
1491 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001492 }
1493 }
1494 if (what & eAlphaChanged) {
1495 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1496 flags |= eTraversalNeeded;
1497 }
1498 if (what & eMatrixChanged) {
1499 if (layer->setMatrix(s.matrix))
1500 flags |= eTraversalNeeded;
1501 }
1502 if (what & eTransparentRegionChanged) {
1503 if (layer->setTransparentRegionHint(s.transparentRegion))
1504 flags |= eTraversalNeeded;
1505 }
1506 if (what & eVisibilityChanged) {
1507 if (layer->setFlags(s.flags, s.mask))
1508 flags |= eTraversalNeeded;
1509 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001511 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512}
1513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514void SurfaceFlinger::screenReleased(int dpy)
1515{
1516 // this may be called by a signal handler, we can't do too much in here
1517 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1518 signalEvent();
1519}
1520
1521void SurfaceFlinger::screenAcquired(int dpy)
1522{
1523 // this may be called by a signal handler, we can't do too much in here
1524 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1525 signalEvent();
1526}
1527
1528status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1529{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001530 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 char buffer[SIZE];
1532 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001533
1534 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 snprintf(buffer, SIZE, "Permission Denial: "
1536 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1537 IPCThreadState::self()->getCallingPid(),
1538 IPCThreadState::self()->getCallingUid());
1539 result.append(buffer);
1540 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001541
1542 // figure out if we're stuck somewhere
1543 const nsecs_t now = systemTime();
1544 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1545 const nsecs_t inTransaction(mDebugInTransaction);
1546 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1547 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1548
1549 // Try to get the main lock, but don't insist if we can't
1550 // (this would indicate SF is stuck, but we want to be able to
1551 // print something in dumpsys).
1552 int retry = 3;
1553 while (mStateLock.tryLock()<0 && --retry>=0) {
1554 usleep(1000000);
1555 }
1556 const bool locked(retry >= 0);
1557 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001558 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001559 "SurfaceFlinger appears to be unresponsive, "
1560 "dumping anyways (no locks held)\n");
1561 result.append(buffer);
1562 }
1563
Mathias Agopian48b888a2011-01-19 16:15:53 -08001564 /*
1565 * Dump the visible layer list
1566 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1568 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001569 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1570 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001572 const sp<LayerBase>& layer(currentLayers[i]);
1573 layer->dump(result, buffer, SIZE);
1574 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575 s.transparentRegion.dump(result, "transparentRegion");
1576 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1577 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1578 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001579
Mathias Agopian48b888a2011-01-19 16:15:53 -08001580 /*
1581 * Dump the layers in the purgatory
1582 */
1583
1584 const size_t purgatorySize = mLayerPurgatory.size();
1585 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1586 result.append(buffer);
1587 for (size_t i=0 ; i<purgatorySize ; i++) {
1588 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1589 layer->shortDump(result, buffer, SIZE);
1590 }
1591
1592 /*
1593 * Dump SurfaceFlinger global state
1594 */
1595
Mathias Agopianad701862011-07-14 18:01:49 -07001596 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001597 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001598
1599 const GLExtensions& extensions(GLExtensions::getInstance());
1600 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1601 extensions.getVendor(),
1602 extensions.getRenderer(),
1603 extensions.getVersion());
1604 result.append(buffer);
1605 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1606 result.append(buffer);
1607
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 mWormholeRegion.dump(result, "WormholeRegion");
1609 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1610 snprintf(buffer, SIZE,
1611 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1612 mFreezeDisplay?"yes":"no", mFreezeCount,
1613 mCurrentState.orientation, hw.canDraw());
1614 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001615 snprintf(buffer, SIZE,
1616 " last eglSwapBuffers() time: %f us\n"
1617 " last transaction time : %f us\n",
1618 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1619 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001620
Mathias Agopian9795c422009-08-26 16:36:26 -07001621 if (inSwapBuffersDuration || !locked) {
1622 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1623 inSwapBuffersDuration/1000.0);
1624 result.append(buffer);
1625 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001626
Mathias Agopian9795c422009-08-26 16:36:26 -07001627 if (inTransactionDuration || !locked) {
1628 snprintf(buffer, SIZE, " transaction time: %f us\n",
1629 inTransactionDuration/1000.0);
1630 result.append(buffer);
1631 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001632
Mathias Agopian48b888a2011-01-19 16:15:53 -08001633 /*
1634 * Dump HWComposer state
1635 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001636 HWComposer& hwc(hw.getHwComposer());
1637 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1638 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001639 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001640 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001641 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001642
Mathias Agopian48b888a2011-01-19 16:15:53 -08001643 /*
1644 * Dump gralloc state
1645 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001646 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001647 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001648 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001649
1650 if (locked) {
1651 mStateLock.unlock();
1652 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 }
1654 write(fd, result.string(), result.size());
1655 return NO_ERROR;
1656}
1657
1658status_t SurfaceFlinger::onTransact(
1659 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1660{
1661 switch (code) {
1662 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001663 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 case SET_ORIENTATION:
1665 case FREEZE_DISPLAY:
1666 case UNFREEZE_DISPLAY:
1667 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001668 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001669 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 {
1671 // codes that require permission check
1672 IPCThreadState* ipc = IPCThreadState::self();
1673 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001674 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001675 if ((uid != AID_GRAPHICS) &&
1676 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001677 LOGE("Permission Denial: "
1678 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1679 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001681 break;
1682 }
1683 case CAPTURE_SCREEN:
1684 {
1685 // codes that require permission check
1686 IPCThreadState* ipc = IPCThreadState::self();
1687 const int pid = ipc->getCallingPid();
1688 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001689 if ((uid != AID_GRAPHICS) &&
1690 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001691 LOGE("Permission Denial: "
1692 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1693 return PERMISSION_DENIED;
1694 }
1695 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696 }
1697 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1700 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001701 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001702 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001703 IPCThreadState* ipc = IPCThreadState::self();
1704 const int pid = ipc->getCallingPid();
1705 const int uid = ipc->getCallingUid();
1706 LOGE("Permission Denial: "
1707 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 return PERMISSION_DENIED;
1709 }
1710 int n;
1711 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001712 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001713 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 return NO_ERROR;
1715 case 1002: // SHOW_UPDATES
1716 n = data.readInt32();
1717 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001718 invalidateHwcGeometry();
1719 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 return NO_ERROR;
1721 case 1003: // SHOW_BACKGROUND
1722 n = data.readInt32();
1723 mDebugBackground = n ? 1 : 0;
1724 return NO_ERROR;
1725 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001726 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001727 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001729 case 1005:{ // force transaction
1730 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1731 return NO_ERROR;
1732 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001733 case 1006:{ // enable/disable GraphicLog
1734 int enabled = data.readInt32();
1735 GraphicLog::getInstance().setEnabled(enabled);
1736 return NO_ERROR;
1737 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 case 1007: // set mFreezeCount
1739 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001740 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 return NO_ERROR;
Mathias Agopian53331da2011-08-22 21:44:41 -07001742 case 1008: // toggle use of hw composer
1743 n = data.readInt32();
1744 mDebugDisableHWC = n ? 1 : 0;
1745 invalidateHwcGeometry();
1746 repaintEverything();
1747 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001748 case 1009: // toggle use of transform hint
1749 n = data.readInt32();
1750 mDebugDisableTransformHint = n ? 1 : 0;
1751 invalidateHwcGeometry();
1752 repaintEverything();
1753 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001755 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756 reply->writeInt32(0);
1757 reply->writeInt32(mDebugRegion);
1758 reply->writeInt32(mDebugBackground);
1759 return NO_ERROR;
1760 case 1013: {
1761 Mutex::Autolock _l(mStateLock);
1762 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1763 reply->writeInt32(hw.getPageFlipCount());
1764 }
1765 return NO_ERROR;
1766 }
1767 }
1768 return err;
1769}
1770
Mathias Agopian53331da2011-08-22 21:44:41 -07001771void SurfaceFlinger::repaintEverything() {
1772 Mutex::Autolock _l(mStateLock);
1773 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1774 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1775 signalEvent();
1776}
1777
Mathias Agopian59119e62010-10-11 12:37:43 -07001778// ---------------------------------------------------------------------------
1779
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001780status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1781 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001782{
Mathias Agopian59119e62010-10-11 12:37:43 -07001783 if (!GLExtensions::getInstance().haveFramebufferObject())
1784 return INVALID_OPERATION;
1785
1786 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001787 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001788 const uint32_t hw_w = hw.getWidth();
1789 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001790 GLfloat u = 1;
1791 GLfloat v = 1;
1792
1793 // make sure to clear all GL error flags
1794 while ( glGetError() != GL_NO_ERROR ) ;
1795
1796 // create a FBO
1797 GLuint name, tname;
1798 glGenTextures(1, &tname);
1799 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001800 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1801 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001802 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001803 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001804 GLint tw = (2 << (31 - clz(hw_w)));
1805 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001806 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1807 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001808 u = GLfloat(hw_w) / tw;
1809 v = GLfloat(hw_h) / th;
1810 }
1811 glGenFramebuffersOES(1, &name);
1812 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001813 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1814 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001815
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001816 // redraw the screen entirely...
1817 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{
1843 status_t result = PERMISSION_DENIED;
1844
1845 if (!GLExtensions::getInstance().haveFramebufferObject())
1846 return INVALID_OPERATION;
1847
1848 // 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;
1856 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1857 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 Agopian9daa5c92010-10-12 16:05:48 -07002008 return NO_ERROR;
2009}
2010
2011status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2012{
2013 status_t result = PERMISSION_DENIED;
2014
2015 if (!GLExtensions::getInstance().haveFramebufferObject())
2016 return INVALID_OPERATION;
2017
2018
2019 // get screen geometry
2020 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2021 const uint32_t hw_w = hw.getWidth();
2022 const uint32_t hw_h = hw.getHeight();
2023 const Region screenBounds(hw.bounds());
2024
2025 GLfloat u, v;
2026 GLuint tname;
2027 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2028 if (result != NO_ERROR) {
2029 return result;
2030 }
2031
2032 // back to main framebuffer
2033 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2034 glDisable(GL_SCISSOR_TEST);
2035
2036 GLfloat vtx[8];
2037 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002038 glBindTexture(GL_TEXTURE_2D, tname);
2039 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2040 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2041 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2042 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2043 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2044 glVertexPointer(2, GL_FLOAT, 0, vtx);
2045
2046 class s_curve_interpolator {
2047 const float nbFrames, s, v;
2048 public:
2049 s_curve_interpolator(int nbFrames, float s)
2050 : nbFrames(1.0f / (nbFrames-1)), s(s),
2051 v(1.0f + expf(-s + 0.5f*s)) {
2052 }
2053 float operator()(int f) {
2054 const float x = f * nbFrames;
2055 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2056 }
2057 };
2058
2059 class v_stretch {
2060 const GLfloat hw_w, hw_h;
2061 public:
2062 v_stretch(uint32_t hw_w, uint32_t hw_h)
2063 : hw_w(hw_w), hw_h(hw_h) {
2064 }
2065 void operator()(GLfloat* vtx, float v) {
2066 const GLfloat w = hw_w + (hw_w * v);
2067 const GLfloat h = hw_h - (hw_h * v);
2068 const GLfloat x = (hw_w - w) * 0.5f;
2069 const GLfloat y = (hw_h - h) * 0.5f;
2070 vtx[0] = x; vtx[1] = y;
2071 vtx[2] = x; vtx[3] = y + h;
2072 vtx[4] = x + w; vtx[5] = y + h;
2073 vtx[6] = x + w; vtx[7] = y;
2074 }
2075 };
2076
2077 class h_stretch {
2078 const GLfloat hw_w, hw_h;
2079 public:
2080 h_stretch(uint32_t hw_w, uint32_t hw_h)
2081 : hw_w(hw_w), hw_h(hw_h) {
2082 }
2083 void operator()(GLfloat* vtx, float v) {
2084 const GLfloat w = hw_w - (hw_w * v);
2085 const GLfloat h = 1.0f;
2086 const GLfloat x = (hw_w - w) * 0.5f;
2087 const GLfloat y = (hw_h - h) * 0.5f;
2088 vtx[0] = x; vtx[1] = y;
2089 vtx[2] = x; vtx[3] = y + h;
2090 vtx[4] = x + w; vtx[5] = y + h;
2091 vtx[6] = x + w; vtx[7] = y;
2092 }
2093 };
2094
Mathias Agopiana6546e52010-10-14 12:33:07 -07002095 // the full animation is 12 frames
2096 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002097 s_curve_interpolator itr(nbFrames, 7.5f);
2098 s_curve_interpolator itg(nbFrames, 8.0f);
2099 s_curve_interpolator itb(nbFrames, 8.5f);
2100
2101 h_stretch hverts(hw_w, hw_h);
2102 glDisable(GL_BLEND);
2103 glDisable(GL_TEXTURE_2D);
2104 glColorMask(1,1,1,1);
2105 for (int i=nbFrames-1 ; i>=0 ; i--) {
2106 const float v = itg(i);
2107 hverts(vtx, v);
2108 glClear(GL_COLOR_BUFFER_BIT);
2109 glColor4f(1-v, 1-v, 1-v, 1);
2110 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2111 hw.flip(screenBounds);
2112 }
2113
Mathias Agopiana6546e52010-10-14 12:33:07 -07002114 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002115 v_stretch vverts(hw_w, hw_h);
2116 glEnable(GL_BLEND);
2117 glBlendFunc(GL_ONE, GL_ONE);
2118 for (int i=nbFrames-1 ; i>=0 ; i--) {
2119 float x, y, w, h;
2120 const float vr = itr(i);
2121 const float vg = itg(i);
2122 const float vb = itb(i);
2123
2124 // clear screen
2125 glColorMask(1,1,1,1);
2126 glClear(GL_COLOR_BUFFER_BIT);
2127 glEnable(GL_TEXTURE_2D);
2128
2129 // draw the red plane
2130 vverts(vtx, vr);
2131 glColorMask(1,0,0,1);
2132 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2133
2134 // draw the green plane
2135 vverts(vtx, vg);
2136 glColorMask(0,1,0,1);
2137 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2138
2139 // draw the blue plane
2140 vverts(vtx, vb);
2141 glColorMask(0,0,1,1);
2142 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2143
2144 hw.flip(screenBounds);
2145 }
2146
2147 glColorMask(1,1,1,1);
2148 glEnable(GL_SCISSOR_TEST);
2149 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002150 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002151 glDisable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002152
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002153 return NO_ERROR;
2154}
2155
2156// ---------------------------------------------------------------------------
2157
Mathias Agopianabd671a2010-10-14 14:54:06 -07002158status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002159{
2160 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2161 if (!hw.canDraw()) {
2162 // we're already off
2163 return NO_ERROR;
2164 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002165
2166 // turn off hwc while we're doing the animation
2167 hw.getHwComposer().disable();
2168 // and make sure to turn it back on (if needed) next time we compose
2169 invalidateHwcGeometry();
2170
Mathias Agopianabd671a2010-10-14 14:54:06 -07002171 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2172 electronBeamOffAnimationImplLocked();
2173 }
2174
2175 // always clear the whole screen at the end of the animation
2176 glClearColor(0,0,0,1);
2177 glDisable(GL_SCISSOR_TEST);
2178 glClear(GL_COLOR_BUFFER_BIT);
2179 glEnable(GL_SCISSOR_TEST);
2180 hw.flip( Region(hw.bounds()) );
2181
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002182 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002183}
2184
2185status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2186{
Mathias Agopian59119e62010-10-11 12:37:43 -07002187 class MessageTurnElectronBeamOff : public MessageBase {
2188 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002189 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002190 status_t result;
2191 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002192 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2193 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002194 }
2195 status_t getResult() const {
2196 return result;
2197 }
2198 virtual bool handler() {
2199 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002200 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002201 return true;
2202 }
2203 };
2204
Mathias Agopianabd671a2010-10-14 14:54:06 -07002205 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002206 status_t res = postMessageSync(msg);
2207 if (res == NO_ERROR) {
2208 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002209
2210 // work-around: when the power-manager calls us we activate the
2211 // animation. eventually, the "on" animation will be called
2212 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002213 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002214 }
2215 return res;
2216}
2217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002218// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002219
Mathias Agopianabd671a2010-10-14 14:54:06 -07002220status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002221{
2222 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2223 if (hw.canDraw()) {
2224 // we're already on
2225 return NO_ERROR;
2226 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002227 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2228 electronBeamOnAnimationImplLocked();
2229 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002230
2231 // make sure to redraw the whole screen when the animation is done
2232 mDirtyRegion.set(hw.bounds());
2233 signalEvent();
2234
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002235 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002236}
2237
2238status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2239{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002240 class MessageTurnElectronBeamOn : public MessageBase {
2241 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002242 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002243 status_t result;
2244 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002245 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2246 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002247 }
2248 status_t getResult() const {
2249 return result;
2250 }
2251 virtual bool handler() {
2252 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002253 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002254 return true;
2255 }
2256 };
2257
Mathias Agopianabd671a2010-10-14 14:54:06 -07002258 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 return NO_ERROR;
2260}
2261
2262// ---------------------------------------------------------------------------
2263
Mathias Agopian74c40c02010-09-29 13:02:36 -07002264status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2265 sp<IMemoryHeap>* heap,
2266 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002267 uint32_t sw, uint32_t sh,
2268 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002269{
2270 status_t result = PERMISSION_DENIED;
2271
2272 // only one display supported for now
2273 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2274 return BAD_VALUE;
2275
2276 if (!GLExtensions::getInstance().haveFramebufferObject())
2277 return INVALID_OPERATION;
2278
2279 // get screen geometry
2280 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2281 const uint32_t hw_w = hw.getWidth();
2282 const uint32_t hw_h = hw.getHeight();
2283
2284 if ((sw > hw_w) || (sh > hw_h))
2285 return BAD_VALUE;
2286
2287 sw = (!sw) ? hw_w : sw;
2288 sh = (!sh) ? hw_h : sh;
2289 const size_t size = sw * sh * 4;
2290
Mathias Agopian1c71a472011-03-02 18:45:50 -08002291 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2292 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002293
Mathias Agopian74c40c02010-09-29 13:02:36 -07002294 // make sure to clear all GL error flags
2295 while ( glGetError() != GL_NO_ERROR ) ;
2296
2297 // create a FBO
2298 GLuint name, tname;
2299 glGenRenderbuffersOES(1, &tname);
2300 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2301 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2302 glGenFramebuffersOES(1, &name);
2303 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2304 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2305 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2306
2307 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002308
Mathias Agopian74c40c02010-09-29 13:02:36 -07002309 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2310
2311 // invert everything, b/c glReadPixel() below will invert the FB
2312 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002313 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002314 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002315 glMatrixMode(GL_PROJECTION);
2316 glPushMatrix();
2317 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002318 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319 glMatrixMode(GL_MODELVIEW);
2320
2321 // redraw the screen entirely...
2322 glClearColor(0,0,0,1);
2323 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002324
Jamie Gennis9575f602011-10-07 14:51:16 -07002325 const LayerVector& layers(mDrawingState.layersSortedByZ);
2326 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002327 for (size_t i=0 ; i<count ; ++i) {
2328 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002329 const uint32_t flags = layer->drawingState().flags;
2330 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2331 const uint32_t z = layer->drawingState().z;
2332 if (z >= minLayerZ && z <= maxLayerZ) {
2333 layer->drawForSreenShot();
2334 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002335 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002336 }
2337
2338 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002339 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002340 glScissor(0, 0, sw, sh);
2341
2342 // check for errors and return screen capture
2343 if (glGetError() != GL_NO_ERROR) {
2344 // error while rendering
2345 result = INVALID_OPERATION;
2346 } else {
2347 // allocate shared memory large enough to hold the
2348 // screen capture
2349 sp<MemoryHeapBase> base(
2350 new MemoryHeapBase(size, 0, "screen-capture") );
2351 void* const ptr = base->getBase();
2352 if (ptr) {
2353 // capture the screen with glReadPixels()
2354 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2355 if (glGetError() == GL_NO_ERROR) {
2356 *heap = base;
2357 *w = sw;
2358 *h = sh;
2359 *f = PIXEL_FORMAT_RGBA_8888;
2360 result = NO_ERROR;
2361 }
2362 } else {
2363 result = NO_MEMORY;
2364 }
2365 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002366 glEnable(GL_SCISSOR_TEST);
2367 glViewport(0, 0, hw_w, hw_h);
2368 glMatrixMode(GL_PROJECTION);
2369 glPopMatrix();
2370 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002371 } else {
2372 result = BAD_VALUE;
2373 }
2374
2375 // release FBO resources
2376 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2377 glDeleteRenderbuffersOES(1, &tname);
2378 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002379
2380 hw.compositionComplete();
2381
Mathias Agopian1c71a472011-03-02 18:45:50 -08002382 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002383
Mathias Agopian74c40c02010-09-29 13:02:36 -07002384 return result;
2385}
2386
2387
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002388status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2389 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002390 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002391 uint32_t sw, uint32_t sh,
2392 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002393{
2394 // only one display supported for now
2395 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2396 return BAD_VALUE;
2397
2398 if (!GLExtensions::getInstance().haveFramebufferObject())
2399 return INVALID_OPERATION;
2400
2401 class MessageCaptureScreen : public MessageBase {
2402 SurfaceFlinger* flinger;
2403 DisplayID dpy;
2404 sp<IMemoryHeap>* heap;
2405 uint32_t* w;
2406 uint32_t* h;
2407 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002408 uint32_t sw;
2409 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002410 uint32_t minLayerZ;
2411 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002412 status_t result;
2413 public:
2414 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002415 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002416 uint32_t sw, uint32_t sh,
2417 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002418 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002419 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2420 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2421 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002422 {
2423 }
2424 status_t getResult() const {
2425 return result;
2426 }
2427 virtual bool handler() {
2428 Mutex::Autolock _l(flinger->mStateLock);
2429
2430 // if we have secure windows, never allow the screen capture
2431 if (flinger->mSecureFrameBuffer)
2432 return true;
2433
Mathias Agopian74c40c02010-09-29 13:02:36 -07002434 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002435 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002436
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002437 return true;
2438 }
2439 };
2440
2441 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002442 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443 status_t res = postMessageSync(msg);
2444 if (res == NO_ERROR) {
2445 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2446 }
2447 return res;
2448}
2449
2450// ---------------------------------------------------------------------------
2451
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002452sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2453{
2454 sp<Layer> result;
2455 Mutex::Autolock _l(mStateLock);
2456 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2457 return result;
2458}
2459
2460// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002461
Mathias Agopian96f08192010-06-02 23:28:45 -07002462Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002463 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002465}
2466
Mathias Agopian96f08192010-06-02 23:28:45 -07002467Client::~Client()
2468{
Mathias Agopian96f08192010-06-02 23:28:45 -07002469 const size_t count = mLayers.size();
2470 for (size_t i=0 ; i<count ; i++) {
2471 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2472 if (layer != 0) {
2473 mFlinger->removeLayer(layer);
2474 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002475 }
2476}
2477
Mathias Agopian96f08192010-06-02 23:28:45 -07002478status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002479 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002481
Mathias Agopian4f113742011-05-03 16:21:41 -07002482size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002483{
Mathias Agopian4f113742011-05-03 16:21:41 -07002484 Mutex::Autolock _l(mLock);
2485 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002486 mLayers.add(name, layer);
2487 return name;
2488}
2489
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002490void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002491{
Mathias Agopian4f113742011-05-03 16:21:41 -07002492 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002493 // we do a linear search here, because this doesn't happen often
2494 const size_t count = mLayers.size();
2495 for (size_t i=0 ; i<count ; i++) {
2496 if (mLayers.valueAt(i) == layer) {
2497 mLayers.removeItemsAt(i, 1);
2498 break;
2499 }
2500 }
2501}
Mathias Agopian4f113742011-05-03 16:21:41 -07002502sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2503{
2504 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002505 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002506 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002507 if (layer != 0) {
2508 lbc = layer.promote();
2509 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002510 }
2511 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512}
2513
Mathias Agopiana67932f2011-04-20 14:20:59 -07002514
2515status_t Client::onTransact(
2516 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2517{
2518 // these must be checked
2519 IPCThreadState* ipc = IPCThreadState::self();
2520 const int pid = ipc->getCallingPid();
2521 const int uid = ipc->getCallingUid();
2522 const int self_pid = getpid();
2523 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2524 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002525 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002526 {
2527 LOGE("Permission Denial: "
2528 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2529 return PERMISSION_DENIED;
2530 }
2531 }
2532 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002533}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002534
2535
Mathias Agopian96f08192010-06-02 23:28:45 -07002536sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002537 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002538 const String8& name,
2539 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002540 uint32_t flags)
2541{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002542 /*
2543 * createSurface must be called from the GL thread so that it can
2544 * have access to the GL context.
2545 */
2546
2547 class MessageCreateSurface : public MessageBase {
2548 sp<ISurface> result;
2549 SurfaceFlinger* flinger;
2550 ISurfaceComposerClient::surface_data_t* params;
2551 Client* client;
2552 const String8& name;
2553 DisplayID display;
2554 uint32_t w, h;
2555 PixelFormat format;
2556 uint32_t flags;
2557 public:
2558 MessageCreateSurface(SurfaceFlinger* flinger,
2559 ISurfaceComposerClient::surface_data_t* params,
2560 const String8& name, Client* client,
2561 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2562 uint32_t flags)
2563 : flinger(flinger), params(params), client(client), name(name),
2564 display(display), w(w), h(h), format(format), flags(flags)
2565 {
2566 }
2567 sp<ISurface> getResult() const { return result; }
2568 virtual bool handler() {
2569 result = flinger->createSurface(params, name, client,
2570 display, w, h, format, flags);
2571 return true;
2572 }
2573 };
2574
2575 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2576 params, name, this, display, w, h, format, flags);
2577 mFlinger->postMessageSync(msg);
2578 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002579}
Mathias Agopian96f08192010-06-02 23:28:45 -07002580status_t Client::destroySurface(SurfaceID sid) {
2581 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002582}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002583
2584// ---------------------------------------------------------------------------
2585
Jamie Gennis9a78c902011-01-12 18:30:40 -08002586GraphicBufferAlloc::GraphicBufferAlloc() {}
2587
2588GraphicBufferAlloc::~GraphicBufferAlloc() {}
2589
2590sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002591 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002592 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2593 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002594 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002595 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002596 if (err == NO_MEMORY) {
2597 GraphicBuffer::dumpAllocationsToSystemLog();
2598 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002599 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2600 "failed (%s), handle=%p",
2601 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002602 return 0;
2603 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002604 return graphicBuffer;
2605}
2606
Jamie Gennis9a78c902011-01-12 18:30:40 -08002607// ---------------------------------------------------------------------------
2608
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609GraphicPlane::GraphicPlane()
2610 : mHw(0)
2611{
2612}
2613
2614GraphicPlane::~GraphicPlane() {
2615 delete mHw;
2616}
2617
2618bool GraphicPlane::initialized() const {
2619 return mHw ? true : false;
2620}
2621
Mathias Agopian2b92d892010-02-08 15:49:35 -08002622int GraphicPlane::getWidth() const {
2623 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002624}
2625
Mathias Agopian2b92d892010-02-08 15:49:35 -08002626int GraphicPlane::getHeight() const {
2627 return mHeight;
2628}
2629
2630void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2631{
2632 mHw = hw;
2633
2634 // initialize the display orientation transform.
2635 // it's a constant that should come from the display driver.
2636 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2637 char property[PROPERTY_VALUE_MAX];
2638 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2639 //displayOrientation
2640 switch (atoi(property)) {
2641 case 90:
2642 displayOrientation = ISurfaceComposer::eOrientation90;
2643 break;
2644 case 270:
2645 displayOrientation = ISurfaceComposer::eOrientation270;
2646 break;
2647 }
2648 }
2649
2650 const float w = hw->getWidth();
2651 const float h = hw->getHeight();
2652 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2653 &mDisplayTransform);
2654 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2655 mDisplayWidth = h;
2656 mDisplayHeight = w;
2657 } else {
2658 mDisplayWidth = w;
2659 mDisplayHeight = h;
2660 }
2661
2662 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002663}
2664
2665status_t GraphicPlane::orientationToTransfrom(
2666 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002667{
2668 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002669 switch (orientation) {
2670 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002671 flags = Transform::ROT_0;
2672 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002674 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675 break;
2676 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002677 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678 break;
2679 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002680 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002681 break;
2682 default:
2683 return BAD_VALUE;
2684 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002685 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002686 return NO_ERROR;
2687}
2688
2689status_t GraphicPlane::setOrientation(int orientation)
2690{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002691 // If the rotation can be handled in hardware, this is where
2692 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002693
2694 const DisplayHardware& hw(displayHardware());
2695 const float w = mDisplayWidth;
2696 const float h = mDisplayHeight;
2697 mWidth = int(w);
2698 mHeight = int(h);
2699
2700 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002701 GraphicPlane::orientationToTransfrom(orientation, w, h,
2702 &orientationTransform);
2703 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2704 mWidth = int(h);
2705 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002706 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002707
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002708 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002709 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710 return NO_ERROR;
2711}
2712
2713const DisplayHardware& GraphicPlane::displayHardware() const {
2714 return *mHw;
2715}
2716
Mathias Agopian59119e62010-10-11 12:37:43 -07002717DisplayHardware& GraphicPlane::editDisplayHardware() {
2718 return *mHw;
2719}
2720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002721const Transform& GraphicPlane::transform() const {
2722 return mGlobalTransform;
2723}
2724
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002725EGLDisplay GraphicPlane::getEGLDisplay() const {
2726 return mHw->getEGLDisplay();
2727}
2728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002729// ---------------------------------------------------------------------------
2730
2731}; // namespace android