blob: 595ec1e121c270907a96aa8ac6b7b4eca474dc8c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopiana67932f2011-04-20 14:20:59 -070058#include <private/surfaceflinger/SharedBufferStack.h>
59
Mathias Agopiana1ecca92009-05-21 19:21:59 -070060/* ideally AID_GRAPHICS would be in a semi-public header
61 * or there would be a way to map a user/group name to its id
62 */
63#ifndef AID_GRAPHICS
64#define AID_GRAPHICS 1003
65#endif
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian99b49842011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070082 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070090 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700100 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mConsoleSignals(0),
102 mSecureFrameBuffer(0)
103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
109 LOGI("SurfaceFlinger is starting");
110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 property_get("debug.sf.showbackground", value, "0");
118 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120 property_get("debug.sf.ddms", value, "0");
121 mDebugDDMS = atoi(value);
122 if (mDebugDDMS) {
123 DdmConnection::start(getServiceName());
124 }
125
Mathias Agopian78fd5012010-04-20 14:51:04 -0700126 LOGI_IF(mDebugRegion, "showupdates enabled");
127 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
131SurfaceFlinger::~SurfaceFlinger()
132{
133 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700136sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700138 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian7e27f052010-05-28 14:22:23 -0700141sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142{
Mathias Agopian96f08192010-06-02 23:28:45 -0700143 sp<ISurfaceComposerClient> bclient;
144 sp<Client> client(new Client(this));
145 status_t err = client->initCheck();
146 if (err == NO_ERROR) {
147 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 return bclient;
150}
151
Jamie Gennis9a78c902011-01-12 18:30:40 -0800152sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
153{
154 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
155 return gba;
156}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
159{
160 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
161 const GraphicPlane& plane(mGraphicPlanes[dpy]);
162 return plane;
163}
164
165GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
166{
167 return const_cast<GraphicPlane&>(
168 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
169}
170
171void SurfaceFlinger::bootFinished()
172{
173 const nsecs_t now = systemTime();
174 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700175 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700176 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700177
178 // wait patiently for the window manager death
179 const String16 name("window");
180 sp<IBinder> window(defaultServiceManager()->getService(name));
181 if (window != 0) {
182 window->linkToDeath(this);
183 }
184
185 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700186 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700189void SurfaceFlinger::binderDied(const wp<IBinder>& who)
190{
191 // the window manager died on us. prepare its eulogy.
192
193 // unfreeze the screen in case it was... frozen
194 mFreezeDisplayTime = 0;
195 mFreezeCount = 0;
196 mFreezeDisplay = false;
197
198 // reset screen orientation
199 setOrientation(0, eOrientationDefault, 0);
200
201 // restart the boot-animation
202 property_set("ctl.start", "bootanim");
203}
204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205void SurfaceFlinger::onFirstRef()
206{
207 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
208
209 // Wait for the main thread to be done with its initialization
210 mReadyToRunBarrier.wait();
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213static inline uint16_t pack565(int r, int g, int b) {
214 return (r<<11)|(g<<5)|b;
215}
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217status_t SurfaceFlinger::readyToRun()
218{
219 LOGI( "SurfaceFlinger's main thread ready to run. "
220 "Initializing graphics H/W...");
221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 // we only support one display currently
223 int dpy = 0;
224
225 {
226 // initialize the main display
227 GraphicPlane& plane(graphicPlane(dpy));
228 DisplayHardware* const hw = new DisplayHardware(this, dpy);
229 plane.setDisplayHardware(hw);
230 }
231
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700232 // create the shared control-block
233 mServerHeap = new MemoryHeapBase(4096,
234 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
235 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700236
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700237 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
238 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700239
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700240 new(mServerCblk) surface_flinger_cblk_t;
241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 // initialize primary screen
243 // (other display should be initialized in the same manner, but
244 // asynchronously, as they could come and go. None of this is supported
245 // yet).
246 const GraphicPlane& plane(graphicPlane(dpy));
247 const DisplayHardware& hw = plane.displayHardware();
248 const uint32_t w = hw.getWidth();
249 const uint32_t h = hw.getHeight();
250 const uint32_t f = hw.getFormat();
251 hw.makeCurrent();
252
253 // initialize the shared control block
254 mServerCblk->connected |= 1<<dpy;
255 display_cblk_t* dcblk = mServerCblk->displays + dpy;
256 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800257 dcblk->w = plane.getWidth();
258 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 dcblk->format = f;
260 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
261 dcblk->xdpi = hw.getDpiX();
262 dcblk->ydpi = hw.getDpiY();
263 dcblk->fps = hw.getRefreshRate();
264 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265
266 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700268 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 glEnableClientState(GL_VERTEX_ARRAY);
270 glEnable(GL_SCISSOR_TEST);
271 glShadeModel(GL_FLAT);
272 glDisable(GL_DITHER);
273 glDisable(GL_CULL_FACE);
274
275 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
276 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 glGenTextures(1, &mWormholeTexName);
279 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700285 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
286
287 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
288 glGenTextures(1, &mProtectedTexName);
289 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
293 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
295 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
297 glViewport(0, 0, w, h);
298 glMatrixMode(GL_PROJECTION);
299 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700300 // put the origin in the left-bottom corner
301 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 mReadyToRunBarrier.open();
304
305 /*
306 * We're now ready to accept clients...
307 */
308
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700309 // start boot animation
310 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
315// ----------------------------------------------------------------------------
316#if 0
317#pragma mark -
318#pragma mark Events Handler
319#endif
320
321void SurfaceFlinger::waitForEvent()
322{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700323 while (true) {
324 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800325 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700326 if (UNLIKELY(isFrozen())) {
327 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700328 const nsecs_t now = systemTime();
329 if (mFreezeDisplayTime == 0) {
330 mFreezeDisplayTime = now;
331 }
332 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
333 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700334 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700335
Mathias Agopianbb641242010-05-18 17:06:55 -0700336 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800337
338 // see if we timed out
339 if (isFrozen()) {
340 const nsecs_t now = systemTime();
341 nsecs_t frozenTime = (now - mFreezeDisplayTime);
342 if (frozenTime >= freezeDisplayTimeout) {
343 // we timed out and are still frozen
344 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
345 mFreezeDisplay, mFreezeCount);
346 mFreezeDisplayTime = 0;
347 mFreezeCount = 0;
348 mFreezeDisplay = false;
349 }
350 }
351
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700352 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700353 switch (msg->what) {
354 case MessageQueue::INVALIDATE:
355 // invalidate message, just return to the main loop
356 return;
357 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 }
360}
361
362void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700363 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364}
365
Jamie Gennis582270d2011-08-17 18:19:00 -0700366bool SurfaceFlinger::authenticateSurfaceTexture(
367 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800368 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700369 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800370
371 // Check the visible layer list for the ISurface
372 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
373 size_t count = currentLayers.size();
374 for (size_t i=0 ; i<count ; i++) {
375 const sp<LayerBase>& layer(currentLayers[i]);
376 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700377 if (lbc != NULL) {
378 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
379 if (lbcBinder == surfaceTextureBinder) {
380 return true;
381 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800382 }
383 }
384
385 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700386 // SurfaceTexture gets destroyed before all the clients are done using it,
387 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800388 // will instead fail later on when the client tries to use the surface,
389 // which should be reported as "surface XYZ returned an -ENODEV". The
390 // purgatorized layers are no less authentic than the visible ones, so this
391 // should not cause any harm.
392 size_t purgatorySize = mLayerPurgatory.size();
393 for (size_t i=0 ; i<purgatorySize ; i++) {
394 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
395 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700396 if (lbc != NULL) {
397 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
398 if (lbcBinder == surfaceTextureBinder) {
399 return true;
400 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800401 }
402 }
403
404 return false;
405}
406
Mathias Agopianbb641242010-05-18 17:06:55 -0700407status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
408 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409{
Mathias Agopianbb641242010-05-18 17:06:55 -0700410 return mEventQueue.postMessage(msg, reltime, flags);
411}
412
413status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
414 nsecs_t reltime, uint32_t flags)
415{
416 status_t res = mEventQueue.postMessage(msg, reltime, flags);
417 if (res == NO_ERROR) {
418 msg->wait();
419 }
420 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421}
422
423// ----------------------------------------------------------------------------
424#if 0
425#pragma mark -
426#pragma mark Main loop
427#endif
428
429bool SurfaceFlinger::threadLoop()
430{
431 waitForEvent();
432
433 // check for transactions
434 if (UNLIKELY(mConsoleSignals)) {
435 handleConsoleEvents();
436 }
437
Mathias Agopian698c0872011-06-28 19:09:31 -0700438 // if we're in a global transaction, don't do anything.
439 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
440 uint32_t transactionFlags = peekTransactionFlags(mask);
441 if (UNLIKELY(transactionFlags)) {
442 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 }
444
445 // post surfaces (if needed)
446 handlePageFlip();
447
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700448 if (mDirtyRegion.isEmpty()) {
449 // nothing new to do.
450 return true;
451 }
452
Mathias Agopiana350ff92010-08-10 17:14:02 -0700453 if (UNLIKELY(mHwWorkListDirty)) {
454 // build the h/w work list
455 handleWorkList();
456 }
457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana44b0412011-10-16 18:46:35 -0700459 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700461
462 const int index = hw.getCurrentBufferIndex();
463 GraphicLog& logger(GraphicLog::getInstance());
464
465 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 handleRepaint();
467
Mathias Agopian74faca22009-09-17 16:18:16 -0700468 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700469 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700470 hw.compositionComplete();
471
Mathias Agopian35b48d12010-09-13 22:57:58 -0700472 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700473 postFramebuffer();
474
Mathias Agopian35b48d12010-09-13 22:57:58 -0700475 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 } else {
477 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800478 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 usleep(16667); // 60 fps period
480 }
481 return true;
482}
483
484void SurfaceFlinger::postFramebuffer()
485{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700486 // this should never happen. we do the flip anyways so we don't
487 // risk to cause a deadlock with hwc
488 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700489 const DisplayHardware& hw(graphicPlane(0).displayHardware());
490 const nsecs_t now = systemTime();
491 mDebugInSwapBuffers = now;
492 hw.flip(mSwapRegion);
493 mLastSwapBufferTime = systemTime() - now;
494 mDebugInSwapBuffers = 0;
495 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496}
497
498void SurfaceFlinger::handleConsoleEvents()
499{
500 // something to do with the console
501 const DisplayHardware& hw = graphicPlane(0).displayHardware();
502
503 int what = android_atomic_and(0, &mConsoleSignals);
504 if (what & eConsoleAcquired) {
505 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700506 // this is a temporary work-around, eventually this should be called
507 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700508 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 }
510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700512 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 }
515 }
516
517 mDirtyRegion.set(hw.bounds());
518}
519
520void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
521{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700522 Mutex::Autolock _l(mStateLock);
523 const nsecs_t now = systemTime();
524 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525
Mathias Agopianca4d3602011-05-19 15:38:14 -0700526 // Here we're guaranteed that some transaction flags are set
527 // so we can call handleTransactionLocked() unconditionally.
528 // We call getTransactionFlags(), which will also clear the flags,
529 // with mStateLock held to guarantee that mCurrentState won't change
530 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700531
Mathias Agopianca4d3602011-05-19 15:38:14 -0700532 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
533 transactionFlags = getTransactionFlags(mask);
534 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700535
Mathias Agopianca4d3602011-05-19 15:38:14 -0700536 mLastTransactionTime = systemTime() - now;
537 mDebugInTransaction = 0;
538 invalidateHwcGeometry();
539 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700540}
541
Mathias Agopianca4d3602011-05-19 15:38:14 -0700542void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700543{
544 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 const size_t count = currentLayers.size();
546
547 /*
548 * Traversal of the children
549 * (perform the transaction for each of them if needed)
550 */
551
552 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
553 if (layersNeedTransaction) {
554 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700555 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
557 if (!trFlags) continue;
558
559 const uint32_t flags = layer->doTransaction(0);
560 if (flags & Layer::eVisibleRegion)
561 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 }
563 }
564
565 /*
566 * Perform our own transaction if needed
567 */
568
569 if (transactionFlags & eTransactionNeeded) {
570 if (mCurrentState.orientation != mDrawingState.orientation) {
571 // the orientation has changed, recompute all visible regions
572 // and invalidate everything.
573
574 const int dpy = 0;
575 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700576 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 GraphicPlane& plane(graphicPlane(dpy));
578 plane.setOrientation(orientation);
579
580 // update the shared control block
581 const DisplayHardware& hw(plane.displayHardware());
582 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
583 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800584 dcblk->w = plane.getWidth();
585 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586
587 mVisibleRegionsDirty = true;
588 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 }
590
591 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
592 // freezing or unfreezing the display -> trigger animation if needed
593 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800594 if (mFreezeDisplay)
595 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 }
597
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700598 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
599 // layers have been added
600 mVisibleRegionsDirty = true;
601 }
602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 // some layers might have been removed, so
604 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700606 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700608 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700609 const size_t count = previousLayers.size();
610 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700611 const sp<LayerBase>& layer(previousLayers[i]);
612 if (currentLayers.indexOf( layer ) < 0) {
613 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700614 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700615 }
616 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 }
619
620 commitTransaction();
621}
622
623sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
624{
625 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
626}
627
628void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800629 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630{
631 const GraphicPlane& plane(graphicPlane(0));
632 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700633 const DisplayHardware& hw(plane.displayHardware());
634 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635
636 Region aboveOpaqueLayers;
637 Region aboveCoveredLayers;
638 Region dirty;
639
640 bool secureFrameBuffer = false;
641
642 size_t i = currentLayers.size();
643 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700644 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 layer->validateVisibility(planeTransform);
646
647 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700648 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649
Mathias Agopianab028732010-03-16 16:41:46 -0700650 /*
651 * opaqueRegion: area of a surface that is fully opaque.
652 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700654
655 /*
656 * visibleRegion: area of a surface that is visible on screen
657 * and not fully transparent. This is essentially the layer's
658 * footprint minus the opaque regions above it.
659 * Areas covered by a translucent surface are considered visible.
660 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700662
663 /*
664 * coveredRegion: area of a surface that is covered by all
665 * visible regions above it (which includes the translucent areas).
666 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700668
669
670 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700671 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700672 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700673 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700675 visibleRegion.andSelf(screenRegion);
676 if (!visibleRegion.isEmpty()) {
677 // Remove the transparent area from the visible region
678 if (translucent) {
679 visibleRegion.subtractSelf(layer->transparentRegionScreen);
680 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681
Mathias Agopianab028732010-03-16 16:41:46 -0700682 // compute the opaque region
683 const int32_t layerOrientation = layer->getOrientation();
684 if (s.alpha==255 && !translucent &&
685 ((layerOrientation & Transform::ROT_INVALID) == false)) {
686 // the opaque region is the layer's footprint
687 opaqueRegion = visibleRegion;
688 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 }
690 }
691
Mathias Agopianab028732010-03-16 16:41:46 -0700692 // Clip the covered region to the visible region
693 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
694
695 // Update aboveCoveredLayers for next (lower) layer
696 aboveCoveredLayers.orSelf(visibleRegion);
697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 // subtract the opaque region covered by the layers above us
699 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700
701 // compute this layer's dirty region
702 if (layer->contentDirty) {
703 // we need to invalidate the whole region
704 dirty = visibleRegion;
705 // as well, as the old visible region
706 dirty.orSelf(layer->visibleRegionScreen);
707 layer->contentDirty = false;
708 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700709 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700710 * the exposed region consists of two components:
711 * 1) what's VISIBLE now and was COVERED before
712 * 2) what's EXPOSED now less what was EXPOSED before
713 *
714 * note that (1) is conservative, we start with the whole
715 * visible region but only keep what used to be covered by
716 * something -- which mean it may have been exposed.
717 *
718 * (2) handles areas that were not covered by anything but got
719 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700720 */
Mathias Agopianab028732010-03-16 16:41:46 -0700721 const Region newExposed = visibleRegion - coveredRegion;
722 const Region oldVisibleRegion = layer->visibleRegionScreen;
723 const Region oldCoveredRegion = layer->coveredRegionScreen;
724 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
725 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 }
727 dirty.subtractSelf(aboveOpaqueLayers);
728
729 // accumulate to the screen dirty region
730 dirtyRegion.orSelf(dirty);
731
Mathias Agopianab028732010-03-16 16:41:46 -0700732 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700734
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 // Store the visible region is screen space
736 layer->setVisibleRegion(visibleRegion);
737 layer->setCoveredRegion(coveredRegion);
738
Mathias Agopian97011222009-07-28 10:57:27 -0700739 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 if (layer->isSecure() && !visibleRegion.isEmpty()) {
741 secureFrameBuffer = true;
742 }
743 }
744
Mathias Agopian97011222009-07-28 10:57:27 -0700745 // invalidate the areas where a layer was removed
746 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
747 mDirtyRegionRemovedLayer.clear();
748
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 mSecureFrameBuffer = secureFrameBuffer;
750 opaqueRegion = aboveOpaqueLayers;
751}
752
753
754void SurfaceFlinger::commitTransaction()
755{
756 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700757 mResizeTransationPending = false;
758 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759}
760
761void SurfaceFlinger::handlePageFlip()
762{
763 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800764 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 visibleRegions |= lockPageFlip(currentLayers);
766
767 const DisplayHardware& hw = graphicPlane(0).displayHardware();
768 const Region screenRegion(hw.bounds());
769 if (visibleRegions) {
770 Region opaqueRegion;
771 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700772
773 /*
774 * rebuild the visible layer list
775 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800776 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700777 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700778 mVisibleLayersSortedByZ.setCapacity(count);
779 for (size_t i=0 ; i<count ; i++) {
780 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
781 mVisibleLayersSortedByZ.add(currentLayers[i]);
782 }
783
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 mWormholeRegion = screenRegion.subtract(opaqueRegion);
785 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800786 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 }
788
789 unlockPageFlip(currentLayers);
790 mDirtyRegion.andSelf(screenRegion);
791}
792
Mathias Agopianad456f92011-01-13 17:53:01 -0800793void SurfaceFlinger::invalidateHwcGeometry()
794{
795 mHwWorkListDirty = true;
796}
797
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
799{
800 bool recomputeVisibleRegions = false;
801 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700802 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700804 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 layer->lockPageFlip(recomputeVisibleRegions);
806 }
807 return recomputeVisibleRegions;
808}
809
810void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
811{
812 const GraphicPlane& plane(graphicPlane(0));
813 const Transform& planeTransform(plane.transform());
814 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700815 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700817 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 layer->unlockPageFlip(planeTransform, mDirtyRegion);
819 }
820}
821
Mathias Agopiana350ff92010-08-10 17:14:02 -0700822void SurfaceFlinger::handleWorkList()
823{
824 mHwWorkListDirty = false;
825 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
826 if (hwc.initCheck() == NO_ERROR) {
827 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
828 const size_t count = currentLayers.size();
829 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700830 hwc_layer_t* const cur(hwc.getLayers());
831 for (size_t i=0 ; cur && i<count ; i++) {
832 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700833 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700834 cur[i].compositionType = HWC_FRAMEBUFFER;
835 cur[i].flags |= HWC_SKIP_LAYER;
836 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700837 }
838 }
839}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700840
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841void SurfaceFlinger::handleRepaint()
842{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700843 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700844 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845
846 if (UNLIKELY(mDebugRegion)) {
847 debugFlashRegions();
848 }
849
Mathias Agopianb8a55602009-06-26 19:06:36 -0700850 // set the frame buffer
851 const DisplayHardware& hw(graphicPlane(0).displayHardware());
852 glMatrixMode(GL_MODELVIEW);
853 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854
855 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700856 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
857 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700858 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
860 // takes a rectangle, we must make sure to update that whole
861 // rectangle in that case
862 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700863 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700864 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700865 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700867 // in the BUFFER_PRESERVED case, obviously, we can update only
868 // what's needed and nothing more.
869 // NOTE: this is NOT a common case, as preserving the backbuffer
870 // is costly and usually involves copying the whole update back.
871 }
872 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700873 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700874 // We need to redraw the rectangle that will be updated
875 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700876 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700877 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700878 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700879 } else {
880 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700882 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 }
884 }
885
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700886 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887 composeSurfaces(mDirtyRegion);
888
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700889 // update the swap region and clear the dirty region
890 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 mDirtyRegion.clear();
892}
893
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700894void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700896 const DisplayHardware& hw(graphicPlane(0).displayHardware());
897 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700898 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700899 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700900 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700901 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700902
Mathias Agopianf384cc32011-09-08 18:31:55 -0700903 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
904 size_t count = layers.size();
905
906 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700907 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
908 hwc.getNumLayers(), count);
909
910 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700911 if (hwc.initCheck() == NO_ERROR) {
912 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
913 }
Mathias Agopian45721772010-08-12 15:03:26 -0700914
915 /*
916 * update the per-frame h/w composer data for each layer
917 * and build the transparent region of the FB
918 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700919 for (size_t i=0 ; i<count ; i++) {
920 const sp<LayerBase>& layer(layers[i]);
921 layer->setPerFrameData(&cur[i]);
922 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700923 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700924 status_t err = hwc.prepare();
925 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700926
Mathias Agopianf384cc32011-09-08 18:31:55 -0700927 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700928 // what's happening here is tricky.
929 // we want to clear all the layers with the CLEAR_FB flags
930 // that are opaque.
931 // however, since some GPU are efficient at preserving
932 // the backbuffer, we want to take advantage of that so we do the
933 // clear only in the dirty region (other areas will be preserved
934 // on those GPUs).
935 // NOTE: on non backbuffer preserving GPU, the dirty region
936 // has already been expanded as needed, so the code is correct
937 // there too.
938 //
939 // However, the content of the framebuffer cannot be trusted when
940 // we switch to/from FB/OVERLAY, in which case we need to
941 // expand the dirty region to those areas too.
942 //
943 // Note also that there is a special case when switching from
944 // "no layers in FB" to "some layers in FB", where we need to redraw
945 // the entire FB, since some areas might contain uninitialized
946 // data.
947 //
948 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700949 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700950 // that is, we can't erase anything outside the dirty region.
951
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700952 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700953
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700954 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
955 transparent.set(hw.getBounds());
956 dirtyInOut = transparent;
957 } else {
958 for (size_t i=0 ; i<count ; i++) {
959 const sp<LayerBase>& layer(layers[i]);
960 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
961 transparent.orSelf(layer->visibleRegionScreen);
962 }
963 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
964 if (isOverlay != layer->isOverlay()) {
965 // we transitioned to/from overlay, so add this layer
966 // to the dirty region so the framebuffer can be either
967 // cleared or redrawn.
968 dirtyInOut.orSelf(layer->visibleRegionScreen);
969 }
970 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800971 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700972 // don't erase stuff outside the dirty region
973 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700974 }
975
976 /*
977 * clear the area of the FB that need to be transparent
978 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700979 if (!transparent.isEmpty()) {
980 glClearColor(0,0,0,0);
981 Region::const_iterator it = transparent.begin();
982 Region::const_iterator const end = transparent.end();
983 const int32_t height = hw.getHeight();
984 while (it != end) {
985 const Rect& r(*it++);
986 const GLint sy = height - (r.top + r.height());
987 glScissor(r.left, sy, r.width(), r.height());
988 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800989 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700990 }
991 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700992}
Mathias Agopian45721772010-08-12 15:03:26 -0700993
Mathias Agopianf384cc32011-09-08 18:31:55 -0700994void SurfaceFlinger::composeSurfaces(const Region& dirty)
995{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700996 const DisplayHardware& hw(graphicPlane(0).displayHardware());
997 HWComposer& hwc(hw.getHwComposer());
998
999 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
1000 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001001 // should never happen unless the window manager has a bug
1002 // draw something...
1003 drawWormhole();
1004 }
1005
Mathias Agopian45721772010-08-12 15:03:26 -07001006 /*
1007 * and then, render the layers targeted at the framebuffer
1008 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001009 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001010 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1011 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001012 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001013 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001014 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001015 }
1016 const sp<LayerBase>& layer(layers[i]);
1017 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1018 if (!clip.isEmpty()) {
1019 layer->draw(clip);
1020 }
1021 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022}
1023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024void SurfaceFlinger::debugFlashRegions()
1025{
Mathias Agopian0a917752010-06-14 21:20:00 -07001026 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1027 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001028 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001029 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001030 return;
1031 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001032
Mathias Agopian0a917752010-06-14 21:20:00 -07001033 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1034 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1035 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1036 mDirtyRegion.bounds() : hw.bounds());
1037 composeSurfaces(repaint);
1038 }
1039
Mathias Agopianc492e672011-10-18 14:49:27 -07001040 glDisable(GL_TEXTURE_EXTERNAL_OES);
1041 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 glDisable(GL_SCISSOR_TEST);
1044
Mathias Agopian0926f502009-05-04 14:17:04 -07001045 static int toggle = 0;
1046 toggle = 1 - toggle;
1047 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001048 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001049 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001050 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001051 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052
Mathias Agopian20f68782009-05-11 00:03:41 -07001053 Region::const_iterator it = mDirtyRegion.begin();
1054 Region::const_iterator const end = mDirtyRegion.end();
1055 while (it != end) {
1056 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001058 { r.left, height - r.top },
1059 { r.left, height - r.bottom },
1060 { r.right, height - r.bottom },
1061 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 };
1063 glVertexPointer(2, GL_FLOAT, 0, vertices);
1064 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1065 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001066
Mathias Agopian0656a682011-09-20 17:22:44 -07001067 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068
1069 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001070 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071
1072 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073}
1074
1075void SurfaceFlinger::drawWormhole() const
1076{
1077 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1078 if (region.isEmpty())
1079 return;
1080
1081 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1082 const int32_t width = hw.getWidth();
1083 const int32_t height = hw.getHeight();
1084
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001086 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001087 Region::const_iterator it = region.begin();
1088 Region::const_iterator const end = region.end();
1089 while (it != end) {
1090 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 const GLint sy = height - (r.top + r.height());
1092 glScissor(r.left, sy, r.width(), r.height());
1093 glClear(GL_COLOR_BUFFER_BIT);
1094 }
1095 } else {
1096 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1097 { width, height }, { 0, height } };
1098 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001099
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 glVertexPointer(2, GL_SHORT, 0, vertices);
1101 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1102 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001103
1104 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 glEnable(GL_TEXTURE_2D);
1106 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1107 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1108 glMatrixMode(GL_TEXTURE);
1109 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001110
1111 glDisable(GL_BLEND);
1112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001114 Region::const_iterator it = region.begin();
1115 Region::const_iterator const end = region.end();
1116 while (it != end) {
1117 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 const GLint sy = height - (r.top + r.height());
1119 glScissor(r.left, sy, r.width(), r.height());
1120 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1121 }
1122 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001123 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001124 glLoadIdentity();
1125 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 }
1127}
1128
1129void SurfaceFlinger::debugShowFPS() const
1130{
1131 static int mFrameCount;
1132 static int mLastFrameCount = 0;
1133 static nsecs_t mLastFpsTime = 0;
1134 static float mFps = 0;
1135 mFrameCount++;
1136 nsecs_t now = systemTime();
1137 nsecs_t diff = now - mLastFpsTime;
1138 if (diff > ms2ns(250)) {
1139 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1140 mLastFpsTime = now;
1141 mLastFrameCount = mFrameCount;
1142 }
1143 // XXX: mFPS has the value we want
1144 }
1145
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001146status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147{
1148 Mutex::Autolock _l(mStateLock);
1149 addLayer_l(layer);
1150 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1151 return NO_ERROR;
1152}
1153
Mathias Agopian96f08192010-06-02 23:28:45 -07001154status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1155{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001156 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001157 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1158}
1159
1160ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1161 const sp<LayerBaseClient>& lbc)
1162{
Mathias Agopian96f08192010-06-02 23:28:45 -07001163 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001164 size_t name = client->attachLayer(lbc);
1165
1166 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001167
1168 // add this layer to the current state list
1169 addLayer_l(lbc);
1170
Mathias Agopian4f113742011-05-03 16:21:41 -07001171 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001172}
1173
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001174status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175{
1176 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001177 status_t err = purgatorizeLayer_l(layer);
1178 if (err == NO_ERROR)
1179 setTransactionFlags(eTransactionNeeded);
1180 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181}
1182
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001183status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001185 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1186 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001187 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1190 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001191 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 return NO_ERROR;
1193 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001194 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195}
1196
Mathias Agopian9a112062009-04-17 19:36:26 -07001197status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1198{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001199 // First add the layer to the purgatory list, which makes sure it won't
1200 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001201 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001202 if (err >= 0) {
1203 mLayerPurgatory.add(layerBase);
1204 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001205
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001206 layerBase->onRemoved();
1207
Mathias Agopian3d579642009-06-04 18:46:21 -07001208 // it's possible that we don't find a layer, because it might
1209 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 // from the user because there is a race between Client::destroySurface(),
1211 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001212 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1213}
1214
Mathias Agopian96f08192010-06-02 23:28:45 -07001215status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216{
Mathias Agopian96f08192010-06-02 23:28:45 -07001217 layer->forceVisibilityTransaction();
1218 setTransactionFlags(eTraversalNeeded);
1219 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220}
1221
Mathias Agopiandea20b12011-05-03 17:04:02 -07001222uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1223{
1224 return android_atomic_release_load(&mTransactionFlags);
1225}
1226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1228{
1229 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1230}
1231
Mathias Agopianbb641242010-05-18 17:06:55 -07001232uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
1234 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1235 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001236 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 }
1238 return old;
1239}
1240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001242void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
1243 int orientation) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001244 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001245
Mathias Agopian698c0872011-06-28 19:09:31 -07001246 uint32_t flags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001247 if (mCurrentState.orientation != orientation) {
1248 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1249 mCurrentState.orientation = orientation;
1250 flags |= eTransactionNeeded;
1251 mResizeTransationPending = true;
1252 } else if (orientation != eOrientationUnchanged) {
1253 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1254 orientation);
1255 }
1256 }
1257
Mathias Agopian698c0872011-06-28 19:09:31 -07001258 const size_t count = state.size();
1259 for (size_t i=0 ; i<count ; i++) {
1260 const ComposerState& s(state[i]);
1261 sp<Client> client( static_cast<Client *>(s.client.get()) );
1262 flags |= setClientStateLocked(client, s.state);
1263 }
1264 if (flags) {
1265 setTransactionFlags(flags);
1266 }
1267
1268 signalEvent();
1269
1270 // if there is a transaction with a resize, wait for it to
1271 // take effect before returning.
1272 while (mResizeTransationPending) {
1273 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1274 if (CC_UNLIKELY(err != NO_ERROR)) {
1275 // just in case something goes wrong in SF, return to the
1276 // called after a few seconds.
1277 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1278 mResizeTransationPending = false;
1279 break;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001280 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 }
1282}
1283
1284status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1285{
1286 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1287 return BAD_VALUE;
1288
1289 Mutex::Autolock _l(mStateLock);
1290 mCurrentState.freezeDisplay = 1;
1291 setTransactionFlags(eTransactionNeeded);
1292
1293 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001294 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 return NO_ERROR;
1296}
1297
1298status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1299{
1300 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1301 return BAD_VALUE;
1302
1303 Mutex::Autolock _l(mStateLock);
1304 mCurrentState.freezeDisplay = 0;
1305 setTransactionFlags(eTransactionNeeded);
1306
1307 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001308 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 return NO_ERROR;
1310}
1311
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001312int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001313 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314{
1315 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1316 return BAD_VALUE;
1317
1318 Mutex::Autolock _l(mStateLock);
1319 if (mCurrentState.orientation != orientation) {
1320 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001321 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 mCurrentState.orientation = orientation;
1323 setTransactionFlags(eTransactionNeeded);
1324 mTransactionCV.wait(mStateLock);
1325 } else {
1326 orientation = BAD_VALUE;
1327 }
1328 }
1329 return orientation;
1330}
1331
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001332sp<ISurface> SurfaceFlinger::createSurface(
1333 ISurfaceComposerClient::surface_data_t* params,
1334 const String8& name,
1335 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1337 uint32_t flags)
1338{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001339 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001340 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001341
1342 if (int32_t(w|h) < 0) {
1343 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1344 int(w), int(h));
1345 return surfaceHandle;
1346 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001349 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 switch (flags & eFXSurfaceMask) {
1351 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001352 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1353 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 break;
1355 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001356 // for now we treat Blur as Dim, until we can implement it
1357 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001359 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 break;
1361 }
1362
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001363 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001364 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001365 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001366 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001369 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001371 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001372 if (normalLayer != 0) {
1373 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001374 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001375 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001376 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001377
Mathias Agopian96f08192010-06-02 23:28:45 -07001378 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 }
1380
1381 return surfaceHandle;
1382}
1383
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001384sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001385 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001386 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001387 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388{
1389 // initialize the surfaces
1390 switch (format) { // TODO: take h/w into account
1391 case PIXEL_FORMAT_TRANSPARENT:
1392 case PIXEL_FORMAT_TRANSLUCENT:
1393 format = PIXEL_FORMAT_RGBA_8888;
1394 break;
1395 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001396#ifdef NO_RGBX_8888
1397 format = PIXEL_FORMAT_RGB_565;
1398#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001399 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001400#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 break;
1402 }
1403
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001404#ifdef NO_RGBX_8888
1405 if (format == PIXEL_FORMAT_RGBX_8888)
1406 format = PIXEL_FORMAT_RGBA_8888;
1407#endif
1408
Mathias Agopian96f08192010-06-02 23:28:45 -07001409 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001410 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001411 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001413 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 }
1415 return layer;
1416}
1417
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001418sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001419 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001420 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421{
Mathias Agopian96f08192010-06-02 23:28:45 -07001422 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopiane9800c82011-10-16 23:54:25 -07001423 layer->initStates(w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 return layer;
1425}
1426
Mathias Agopian96f08192010-06-02 23:28:45 -07001427status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428{
Mathias Agopian9a112062009-04-17 19:36:26 -07001429 /*
1430 * called by the window manager, when a surface should be marked for
1431 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001432 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001433 * The surface is removed from the current and drawing lists, but placed
1434 * in the purgatory queue, so it's not destroyed right-away (we need
1435 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001436 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001437
Mathias Agopian48d819a2009-09-10 19:41:18 -07001438 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001439 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001440 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001441 if (layer != 0) {
1442 err = purgatorizeLayer_l(layer);
1443 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001444 setTransactionFlags(eTransactionNeeded);
1445 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001446 }
1447 return err;
1448}
1449
Mathias Agopianca4d3602011-05-19 15:38:14 -07001450status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001451{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001452 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001453 status_t err = NO_ERROR;
1454 sp<LayerBaseClient> l(layer.promote());
1455 if (l != NULL) {
1456 Mutex::Autolock _l(mStateLock);
1457 err = removeLayer_l(l);
1458 if (err == NAME_NOT_FOUND) {
1459 // The surface wasn't in the current list, which means it was
1460 // removed already, which means it is in the purgatory,
1461 // and need to be removed from there.
1462 ssize_t idx = mLayerPurgatory.remove(l);
1463 LOGE_IF(idx < 0,
1464 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001465 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001466 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1467 "error removing layer=%p (%s)", l.get(), strerror(-err));
1468 }
1469 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470}
1471
Mathias Agopian698c0872011-06-28 19:09:31 -07001472uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001473 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001474 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001477 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1478 if (layer != 0) {
1479 const uint32_t what = s.what;
1480 if (what & ePositionChanged) {
1481 if (layer->setPosition(s.x, s.y))
1482 flags |= eTraversalNeeded;
1483 }
1484 if (what & eLayerChanged) {
1485 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1486 if (layer->setLayer(s.z)) {
1487 mCurrentState.layersSortedByZ.removeAt(idx);
1488 mCurrentState.layersSortedByZ.add(layer);
1489 // we need traversal (state changed)
1490 // AND transaction (list changed)
1491 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 }
1493 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001494 if (what & eSizeChanged) {
1495 if (layer->setSize(s.w, s.h)) {
1496 flags |= eTraversalNeeded;
1497 mResizeTransationPending = true;
1498 }
1499 }
1500 if (what & eAlphaChanged) {
1501 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1502 flags |= eTraversalNeeded;
1503 }
1504 if (what & eMatrixChanged) {
1505 if (layer->setMatrix(s.matrix))
1506 flags |= eTraversalNeeded;
1507 }
1508 if (what & eTransparentRegionChanged) {
1509 if (layer->setTransparentRegionHint(s.transparentRegion))
1510 flags |= eTraversalNeeded;
1511 }
1512 if (what & eVisibilityChanged) {
1513 if (layer->setFlags(s.flags, s.mask))
1514 flags |= eTraversalNeeded;
1515 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001517 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518}
1519
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520void SurfaceFlinger::screenReleased(int dpy)
1521{
1522 // this may be called by a signal handler, we can't do too much in here
1523 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1524 signalEvent();
1525}
1526
1527void SurfaceFlinger::screenAcquired(int dpy)
1528{
1529 // this may be called by a signal handler, we can't do too much in here
1530 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1531 signalEvent();
1532}
1533
1534status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1535{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001536 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 char buffer[SIZE];
1538 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001539
1540 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 snprintf(buffer, SIZE, "Permission Denial: "
1542 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1543 IPCThreadState::self()->getCallingPid(),
1544 IPCThreadState::self()->getCallingUid());
1545 result.append(buffer);
1546 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001547
1548 // figure out if we're stuck somewhere
1549 const nsecs_t now = systemTime();
1550 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1551 const nsecs_t inTransaction(mDebugInTransaction);
1552 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1553 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1554
1555 // Try to get the main lock, but don't insist if we can't
1556 // (this would indicate SF is stuck, but we want to be able to
1557 // print something in dumpsys).
1558 int retry = 3;
1559 while (mStateLock.tryLock()<0 && --retry>=0) {
1560 usleep(1000000);
1561 }
1562 const bool locked(retry >= 0);
1563 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001564 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001565 "SurfaceFlinger appears to be unresponsive, "
1566 "dumping anyways (no locks held)\n");
1567 result.append(buffer);
1568 }
1569
Mathias Agopian48b888a2011-01-19 16:15:53 -08001570 /*
1571 * Dump the visible layer list
1572 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1574 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001575 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1576 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001578 const sp<LayerBase>& layer(currentLayers[i]);
1579 layer->dump(result, buffer, SIZE);
1580 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 s.transparentRegion.dump(result, "transparentRegion");
1582 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1583 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1584 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001585
Mathias Agopian48b888a2011-01-19 16:15:53 -08001586 /*
1587 * Dump the layers in the purgatory
1588 */
1589
1590 const size_t purgatorySize = mLayerPurgatory.size();
1591 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1592 result.append(buffer);
1593 for (size_t i=0 ; i<purgatorySize ; i++) {
1594 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1595 layer->shortDump(result, buffer, SIZE);
1596 }
1597
1598 /*
1599 * Dump SurfaceFlinger global state
1600 */
1601
Mathias Agopianad701862011-07-14 18:01:49 -07001602 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001603 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001604
1605 const GLExtensions& extensions(GLExtensions::getInstance());
1606 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1607 extensions.getVendor(),
1608 extensions.getRenderer(),
1609 extensions.getVersion());
1610 result.append(buffer);
1611 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1612 result.append(buffer);
1613
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 mWormholeRegion.dump(result, "WormholeRegion");
1615 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1616 snprintf(buffer, SIZE,
1617 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1618 mFreezeDisplay?"yes":"no", mFreezeCount,
1619 mCurrentState.orientation, hw.canDraw());
1620 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001621 snprintf(buffer, SIZE,
1622 " last eglSwapBuffers() time: %f us\n"
1623 " last transaction time : %f us\n",
1624 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1625 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001626
Mathias Agopian9795c422009-08-26 16:36:26 -07001627 if (inSwapBuffersDuration || !locked) {
1628 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1629 inSwapBuffersDuration/1000.0);
1630 result.append(buffer);
1631 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001632
Mathias Agopian9795c422009-08-26 16:36:26 -07001633 if (inTransactionDuration || !locked) {
1634 snprintf(buffer, SIZE, " transaction time: %f us\n",
1635 inTransactionDuration/1000.0);
1636 result.append(buffer);
1637 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001638
Mathias Agopian48b888a2011-01-19 16:15:53 -08001639 /*
1640 * Dump HWComposer state
1641 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001642 HWComposer& hwc(hw.getHwComposer());
1643 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1644 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001645 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001646 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001647 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001648
Mathias Agopian48b888a2011-01-19 16:15:53 -08001649 /*
1650 * Dump gralloc state
1651 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001652 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001653 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001654 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001655
1656 if (locked) {
1657 mStateLock.unlock();
1658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 }
1660 write(fd, result.string(), result.size());
1661 return NO_ERROR;
1662}
1663
1664status_t SurfaceFlinger::onTransact(
1665 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1666{
1667 switch (code) {
1668 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001669 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 case SET_ORIENTATION:
1671 case FREEZE_DISPLAY:
1672 case UNFREEZE_DISPLAY:
1673 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001674 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001675 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 {
1677 // codes that require permission check
1678 IPCThreadState* ipc = IPCThreadState::self();
1679 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001680 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001681 if ((uid != AID_GRAPHICS) &&
1682 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001683 LOGE("Permission Denial: "
1684 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1685 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001687 break;
1688 }
1689 case CAPTURE_SCREEN:
1690 {
1691 // codes that require permission check
1692 IPCThreadState* ipc = IPCThreadState::self();
1693 const int pid = ipc->getCallingPid();
1694 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001695 if ((uid != AID_GRAPHICS) &&
1696 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001697 LOGE("Permission Denial: "
1698 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1699 return PERMISSION_DENIED;
1700 }
1701 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702 }
1703 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1706 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001707 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001708 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001709 IPCThreadState* ipc = IPCThreadState::self();
1710 const int pid = ipc->getCallingPid();
1711 const int uid = ipc->getCallingUid();
1712 LOGE("Permission Denial: "
1713 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 return PERMISSION_DENIED;
1715 }
1716 int n;
1717 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001718 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001719 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 return NO_ERROR;
1721 case 1002: // SHOW_UPDATES
1722 n = data.readInt32();
1723 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001724 invalidateHwcGeometry();
1725 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 return NO_ERROR;
1727 case 1003: // SHOW_BACKGROUND
1728 n = data.readInt32();
1729 mDebugBackground = n ? 1 : 0;
1730 return NO_ERROR;
1731 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001732 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001733 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001735 case 1005:{ // force transaction
1736 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1737 return NO_ERROR;
1738 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001739 case 1006:{ // enable/disable GraphicLog
1740 int enabled = data.readInt32();
1741 GraphicLog::getInstance().setEnabled(enabled);
1742 return NO_ERROR;
1743 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 case 1007: // set mFreezeCount
1745 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001746 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 return NO_ERROR;
Mathias Agopian53331da2011-08-22 21:44:41 -07001748 case 1008: // toggle use of hw composer
1749 n = data.readInt32();
1750 mDebugDisableHWC = n ? 1 : 0;
1751 invalidateHwcGeometry();
1752 repaintEverything();
1753 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001754 case 1009: // toggle use of transform hint
1755 n = data.readInt32();
1756 mDebugDisableTransformHint = n ? 1 : 0;
1757 invalidateHwcGeometry();
1758 repaintEverything();
1759 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001761 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762 reply->writeInt32(0);
1763 reply->writeInt32(mDebugRegion);
1764 reply->writeInt32(mDebugBackground);
1765 return NO_ERROR;
1766 case 1013: {
1767 Mutex::Autolock _l(mStateLock);
1768 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1769 reply->writeInt32(hw.getPageFlipCount());
1770 }
1771 return NO_ERROR;
1772 }
1773 }
1774 return err;
1775}
1776
Mathias Agopian53331da2011-08-22 21:44:41 -07001777void SurfaceFlinger::repaintEverything() {
1778 Mutex::Autolock _l(mStateLock);
1779 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian3a3cad32011-10-18 17:36:28 -07001780 mDirtyRegion.set(hw.bounds());
Mathias Agopian53331da2011-08-22 21:44:41 -07001781 signalEvent();
1782}
1783
Mathias Agopian59119e62010-10-11 12:37:43 -07001784// ---------------------------------------------------------------------------
1785
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001786status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1787 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001788{
Mathias Agopian59119e62010-10-11 12:37:43 -07001789 if (!GLExtensions::getInstance().haveFramebufferObject())
1790 return INVALID_OPERATION;
1791
1792 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001793 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001794 const uint32_t hw_w = hw.getWidth();
1795 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 GLfloat u = 1;
1797 GLfloat v = 1;
1798
1799 // make sure to clear all GL error flags
1800 while ( glGetError() != GL_NO_ERROR ) ;
1801
1802 // create a FBO
1803 GLuint name, tname;
1804 glGenTextures(1, &tname);
1805 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001806 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1807 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001808 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001809 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001810 GLint tw = (2 << (31 - clz(hw_w)));
1811 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001812 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1813 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001814 u = GLfloat(hw_w) / tw;
1815 v = GLfloat(hw_h) / th;
1816 }
1817 glGenFramebuffersOES(1, &name);
1818 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001819 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1820 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001821
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001822 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001823 glDisable(GL_TEXTURE_EXTERNAL_OES);
1824 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001825 glClearColor(0,0,0,1);
1826 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001827 glMatrixMode(GL_MODELVIEW);
1828 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001829 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1830 const size_t count = layers.size();
1831 for (size_t i=0 ; i<count ; ++i) {
1832 const sp<LayerBase>& layer(layers[i]);
1833 layer->drawForSreenShot();
1834 }
1835
1836 // back to main framebuffer
1837 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1838 glDisable(GL_SCISSOR_TEST);
1839 glDeleteFramebuffersOES(1, &name);
1840
1841 *textureName = tname;
1842 *uOut = u;
1843 *vOut = v;
1844 return NO_ERROR;
1845}
1846
1847// ---------------------------------------------------------------------------
1848
1849status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1850{
Mathias Agopiane9800c82011-10-16 23:54:25 -07001851 status_t result = PERMISSION_DENIED;
1852
1853 if (!GLExtensions::getInstance().haveFramebufferObject())
1854 return INVALID_OPERATION;
1855
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001856 // get screen geometry
1857 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1858 const uint32_t hw_w = hw.getWidth();
1859 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001860 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861
1862 GLfloat u, v;
1863 GLuint tname;
Mathias Agopiane9800c82011-10-16 23:54:25 -07001864 result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001865 if (result != NO_ERROR) {
1866 return result;
1867 }
1868
1869 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001870 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001871 glBindTexture(GL_TEXTURE_2D, tname);
1872 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1873 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1874 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1875 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1876 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1877 glVertexPointer(2, GL_FLOAT, 0, vtx);
1878
Mathias Agopianffcf4652011-07-07 17:30:31 -07001879 /*
1880 * Texture coordinate mapping
1881 *
1882 * u
1883 * 1 +----------+---+
1884 * | | | | image is inverted
1885 * | V | | w.r.t. the texture
1886 * 1-v +----------+ | coordinates
1887 * | |
1888 * | |
1889 * | |
1890 * 0 +--------------+
1891 * 0 1
1892 *
1893 */
1894
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001895 class s_curve_interpolator {
1896 const float nbFrames, s, v;
1897 public:
1898 s_curve_interpolator(int nbFrames, float s)
1899 : nbFrames(1.0f / (nbFrames-1)), s(s),
1900 v(1.0f + expf(-s + 0.5f*s)) {
1901 }
1902 float operator()(int f) {
1903 const float x = f * nbFrames;
1904 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1905 }
1906 };
1907
1908 class v_stretch {
1909 const GLfloat hw_w, hw_h;
1910 public:
1911 v_stretch(uint32_t hw_w, uint32_t hw_h)
1912 : hw_w(hw_w), hw_h(hw_h) {
1913 }
1914 void operator()(GLfloat* vtx, float v) {
1915 const GLfloat w = hw_w + (hw_w * v);
1916 const GLfloat h = hw_h - (hw_h * v);
1917 const GLfloat x = (hw_w - w) * 0.5f;
1918 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001919 vtx[0] = x; vtx[1] = y;
1920 vtx[2] = x; vtx[3] = y + h;
1921 vtx[4] = x + w; vtx[5] = y + h;
1922 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001923 }
1924 };
1925
1926 class h_stretch {
1927 const GLfloat hw_w, hw_h;
1928 public:
1929 h_stretch(uint32_t hw_w, uint32_t hw_h)
1930 : hw_w(hw_w), hw_h(hw_h) {
1931 }
1932 void operator()(GLfloat* vtx, float v) {
1933 const GLfloat w = hw_w - (hw_w * v);
1934 const GLfloat h = 1.0f;
1935 const GLfloat x = (hw_w - w) * 0.5f;
1936 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001937 vtx[0] = x; vtx[1] = y;
1938 vtx[2] = x; vtx[3] = y + h;
1939 vtx[4] = x + w; vtx[5] = y + h;
1940 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001941 }
1942 };
1943
1944 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001945 char value[PROPERTY_VALUE_MAX];
1946 property_get("debug.sf.electron_frames", value, "24");
1947 int nbFrames = (atoi(value) + 1) >> 1;
1948 if (nbFrames <= 0) // just in case
1949 nbFrames = 24;
1950
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001951 s_curve_interpolator itr(nbFrames, 7.5f);
1952 s_curve_interpolator itg(nbFrames, 8.0f);
1953 s_curve_interpolator itb(nbFrames, 8.5f);
1954
1955 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001956
1957 glMatrixMode(GL_TEXTURE);
1958 glLoadIdentity();
1959 glMatrixMode(GL_MODELVIEW);
1960 glLoadIdentity();
1961
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001962 glEnable(GL_BLEND);
1963 glBlendFunc(GL_ONE, GL_ONE);
1964 for (int i=0 ; i<nbFrames ; i++) {
1965 float x, y, w, h;
1966 const float vr = itr(i);
1967 const float vg = itg(i);
1968 const float vb = itb(i);
1969
1970 // clear screen
1971 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001972 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001973 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001974
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001975 // draw the red plane
1976 vverts(vtx, vr);
1977 glColorMask(1,0,0,1);
1978 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001979
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001980 // draw the green plane
1981 vverts(vtx, vg);
1982 glColorMask(0,1,0,1);
1983 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001984
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 // draw the blue plane
1986 vverts(vtx, vb);
1987 glColorMask(0,0,1,1);
1988 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001989
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001991 glDisable(GL_TEXTURE_2D);
1992 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001993 glColor4f(vg, vg, vg, 1);
1994 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1995 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001996 }
1997
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001998 h_stretch hverts(hw_w, hw_h);
1999 glDisable(GL_BLEND);
2000 glDisable(GL_TEXTURE_2D);
2001 glColorMask(1,1,1,1);
2002 for (int i=0 ; i<nbFrames ; i++) {
2003 const float v = itg(i);
2004 hverts(vtx, v);
2005 glClear(GL_COLOR_BUFFER_BIT);
2006 glColor4f(1-v, 1-v, 1-v, 1);
2007 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2008 hw.flip(screenBounds);
2009 }
2010
2011 glColorMask(1,1,1,1);
2012 glEnable(GL_SCISSOR_TEST);
2013 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2014 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002015 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002016 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002017 return NO_ERROR;
2018}
2019
2020status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2021{
2022 status_t result = PERMISSION_DENIED;
2023
2024 if (!GLExtensions::getInstance().haveFramebufferObject())
2025 return INVALID_OPERATION;
2026
2027
2028 // get screen geometry
2029 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2030 const uint32_t hw_w = hw.getWidth();
2031 const uint32_t hw_h = hw.getHeight();
2032 const Region screenBounds(hw.bounds());
2033
2034 GLfloat u, v;
2035 GLuint tname;
2036 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2037 if (result != NO_ERROR) {
2038 return result;
2039 }
2040
Mathias Agopiane9800c82011-10-16 23:54:25 -07002041 // back to main framebuffer
2042 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2043 glDisable(GL_SCISSOR_TEST);
2044
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 GLfloat vtx[8];
2046 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 glBindTexture(GL_TEXTURE_2D, tname);
2048 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2049 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2050 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2051 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2052 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2053 glVertexPointer(2, GL_FLOAT, 0, vtx);
2054
2055 class s_curve_interpolator {
2056 const float nbFrames, s, v;
2057 public:
2058 s_curve_interpolator(int nbFrames, float s)
2059 : nbFrames(1.0f / (nbFrames-1)), s(s),
2060 v(1.0f + expf(-s + 0.5f*s)) {
2061 }
2062 float operator()(int f) {
2063 const float x = f * nbFrames;
2064 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2065 }
2066 };
2067
2068 class v_stretch {
2069 const GLfloat hw_w, hw_h;
2070 public:
2071 v_stretch(uint32_t hw_w, uint32_t hw_h)
2072 : hw_w(hw_w), hw_h(hw_h) {
2073 }
2074 void operator()(GLfloat* vtx, float v) {
2075 const GLfloat w = hw_w + (hw_w * v);
2076 const GLfloat h = hw_h - (hw_h * v);
2077 const GLfloat x = (hw_w - w) * 0.5f;
2078 const GLfloat y = (hw_h - h) * 0.5f;
2079 vtx[0] = x; vtx[1] = y;
2080 vtx[2] = x; vtx[3] = y + h;
2081 vtx[4] = x + w; vtx[5] = y + h;
2082 vtx[6] = x + w; vtx[7] = y;
2083 }
2084 };
2085
2086 class h_stretch {
2087 const GLfloat hw_w, hw_h;
2088 public:
2089 h_stretch(uint32_t hw_w, uint32_t hw_h)
2090 : hw_w(hw_w), hw_h(hw_h) {
2091 }
2092 void operator()(GLfloat* vtx, float v) {
2093 const GLfloat w = hw_w - (hw_w * v);
2094 const GLfloat h = 1.0f;
2095 const GLfloat x = (hw_w - w) * 0.5f;
2096 const GLfloat y = (hw_h - h) * 0.5f;
2097 vtx[0] = x; vtx[1] = y;
2098 vtx[2] = x; vtx[3] = y + h;
2099 vtx[4] = x + w; vtx[5] = y + h;
2100 vtx[6] = x + w; vtx[7] = y;
2101 }
2102 };
2103
Mathias Agopiana6546e52010-10-14 12:33:07 -07002104 // the full animation is 12 frames
2105 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 s_curve_interpolator itr(nbFrames, 7.5f);
2107 s_curve_interpolator itg(nbFrames, 8.0f);
2108 s_curve_interpolator itb(nbFrames, 8.5f);
2109
2110 h_stretch hverts(hw_w, hw_h);
2111 glDisable(GL_BLEND);
2112 glDisable(GL_TEXTURE_2D);
2113 glColorMask(1,1,1,1);
2114 for (int i=nbFrames-1 ; i>=0 ; i--) {
2115 const float v = itg(i);
2116 hverts(vtx, v);
2117 glClear(GL_COLOR_BUFFER_BIT);
2118 glColor4f(1-v, 1-v, 1-v, 1);
2119 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2120 hw.flip(screenBounds);
2121 }
2122
Mathias Agopiana6546e52010-10-14 12:33:07 -07002123 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002124 v_stretch vverts(hw_w, hw_h);
2125 glEnable(GL_BLEND);
2126 glBlendFunc(GL_ONE, GL_ONE);
2127 for (int i=nbFrames-1 ; i>=0 ; i--) {
2128 float x, y, w, h;
2129 const float vr = itr(i);
2130 const float vg = itg(i);
2131 const float vb = itb(i);
2132
2133 // clear screen
2134 glColorMask(1,1,1,1);
2135 glClear(GL_COLOR_BUFFER_BIT);
2136 glEnable(GL_TEXTURE_2D);
2137
2138 // draw the red plane
2139 vverts(vtx, vr);
2140 glColorMask(1,0,0,1);
2141 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2142
2143 // draw the green plane
2144 vverts(vtx, vg);
2145 glColorMask(0,1,0,1);
2146 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2147
2148 // draw the blue plane
2149 vverts(vtx, vb);
2150 glColorMask(0,0,1,1);
2151 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2152
2153 hw.flip(screenBounds);
2154 }
2155
2156 glColorMask(1,1,1,1);
2157 glEnable(GL_SCISSOR_TEST);
2158 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002159 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002160 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002161 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002162
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002163 return NO_ERROR;
2164}
2165
2166// ---------------------------------------------------------------------------
2167
Mathias Agopianabd671a2010-10-14 14:54:06 -07002168status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002169{
2170 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2171 if (!hw.canDraw()) {
2172 // we're already off
2173 return NO_ERROR;
2174 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002175
2176 // turn off hwc while we're doing the animation
2177 hw.getHwComposer().disable();
2178 // and make sure to turn it back on (if needed) next time we compose
2179 invalidateHwcGeometry();
2180
Mathias Agopianabd671a2010-10-14 14:54:06 -07002181 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2182 electronBeamOffAnimationImplLocked();
2183 }
2184
2185 // always clear the whole screen at the end of the animation
2186 glClearColor(0,0,0,1);
2187 glDisable(GL_SCISSOR_TEST);
2188 glClear(GL_COLOR_BUFFER_BIT);
2189 glEnable(GL_SCISSOR_TEST);
2190 hw.flip( Region(hw.bounds()) );
2191
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002192 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002193}
2194
2195status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2196{
Mathias Agopian59119e62010-10-11 12:37:43 -07002197 class MessageTurnElectronBeamOff : public MessageBase {
2198 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002199 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002200 status_t result;
2201 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002202 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2203 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002204 }
2205 status_t getResult() const {
2206 return result;
2207 }
2208 virtual bool handler() {
2209 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002210 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002211 return true;
2212 }
2213 };
2214
Mathias Agopianabd671a2010-10-14 14:54:06 -07002215 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002216 status_t res = postMessageSync(msg);
2217 if (res == NO_ERROR) {
2218 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002219
2220 // work-around: when the power-manager calls us we activate the
2221 // animation. eventually, the "on" animation will be called
2222 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002223 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002224 }
2225 return res;
2226}
2227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002228// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002229
Mathias Agopianabd671a2010-10-14 14:54:06 -07002230status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002231{
2232 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2233 if (hw.canDraw()) {
2234 // we're already on
2235 return NO_ERROR;
2236 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002237 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2238 electronBeamOnAnimationImplLocked();
2239 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002240
2241 // make sure to redraw the whole screen when the animation is done
2242 mDirtyRegion.set(hw.bounds());
2243 signalEvent();
2244
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002245 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002246}
2247
2248status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2249{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002250 class MessageTurnElectronBeamOn : public MessageBase {
2251 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002252 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002253 status_t result;
2254 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2256 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002257 }
2258 status_t getResult() const {
2259 return result;
2260 }
2261 virtual bool handler() {
2262 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002263 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264 return true;
2265 }
2266 };
2267
Mathias Agopianabd671a2010-10-14 14:54:06 -07002268 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002269 return NO_ERROR;
2270}
2271
2272// ---------------------------------------------------------------------------
2273
Mathias Agopian74c40c02010-09-29 13:02:36 -07002274status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2275 sp<IMemoryHeap>* heap,
2276 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002277 uint32_t sw, uint32_t sh,
2278 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002279{
2280 status_t result = PERMISSION_DENIED;
2281
2282 // only one display supported for now
2283 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2284 return BAD_VALUE;
2285
2286 if (!GLExtensions::getInstance().haveFramebufferObject())
2287 return INVALID_OPERATION;
2288
2289 // get screen geometry
2290 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2291 const uint32_t hw_w = hw.getWidth();
2292 const uint32_t hw_h = hw.getHeight();
2293
2294 if ((sw > hw_w) || (sh > hw_h))
2295 return BAD_VALUE;
2296
2297 sw = (!sw) ? hw_w : sw;
2298 sh = (!sh) ? hw_h : sh;
2299 const size_t size = sw * sh * 4;
2300
Mathias Agopian1c71a472011-03-02 18:45:50 -08002301 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2302 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002303
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 // make sure to clear all GL error flags
2305 while ( glGetError() != GL_NO_ERROR ) ;
2306
2307 // create a FBO
2308 GLuint name, tname;
2309 glGenRenderbuffersOES(1, &tname);
2310 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2311 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2312 glGenFramebuffersOES(1, &name);
2313 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2314 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2315 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2316
2317 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002318
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2320
2321 // invert everything, b/c glReadPixel() below will invert the FB
2322 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002323 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002324 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002325 glMatrixMode(GL_PROJECTION);
2326 glPushMatrix();
2327 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002328 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 glMatrixMode(GL_MODELVIEW);
2330
2331 // redraw the screen entirely...
2332 glClearColor(0,0,0,1);
2333 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002334
Jamie Gennis9575f602011-10-07 14:51:16 -07002335 const LayerVector& layers(mDrawingState.layersSortedByZ);
2336 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 for (size_t i=0 ; i<count ; ++i) {
2338 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002339 const uint32_t flags = layer->drawingState().flags;
2340 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2341 const uint32_t z = layer->drawingState().z;
2342 if (z >= minLayerZ && z <= maxLayerZ) {
2343 layer->drawForSreenShot();
2344 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002345 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002346 }
2347
2348 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002349 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002350 glScissor(0, 0, sw, sh);
2351
2352 // check for errors and return screen capture
2353 if (glGetError() != GL_NO_ERROR) {
2354 // error while rendering
2355 result = INVALID_OPERATION;
2356 } else {
2357 // allocate shared memory large enough to hold the
2358 // screen capture
2359 sp<MemoryHeapBase> base(
2360 new MemoryHeapBase(size, 0, "screen-capture") );
2361 void* const ptr = base->getBase();
2362 if (ptr) {
2363 // capture the screen with glReadPixels()
2364 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2365 if (glGetError() == GL_NO_ERROR) {
2366 *heap = base;
2367 *w = sw;
2368 *h = sh;
2369 *f = PIXEL_FORMAT_RGBA_8888;
2370 result = NO_ERROR;
2371 }
2372 } else {
2373 result = NO_MEMORY;
2374 }
2375 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002376 glEnable(GL_SCISSOR_TEST);
2377 glViewport(0, 0, hw_w, hw_h);
2378 glMatrixMode(GL_PROJECTION);
2379 glPopMatrix();
2380 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 } else {
2382 result = BAD_VALUE;
2383 }
2384
2385 // release FBO resources
2386 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2387 glDeleteRenderbuffersOES(1, &tname);
2388 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002389
2390 hw.compositionComplete();
2391
Mathias Agopian1c71a472011-03-02 18:45:50 -08002392 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002393
Mathias Agopian74c40c02010-09-29 13:02:36 -07002394 return result;
2395}
2396
2397
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002398status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2399 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002400 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002401 uint32_t sw, uint32_t sh,
2402 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002403{
2404 // only one display supported for now
2405 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2406 return BAD_VALUE;
2407
2408 if (!GLExtensions::getInstance().haveFramebufferObject())
2409 return INVALID_OPERATION;
2410
2411 class MessageCaptureScreen : public MessageBase {
2412 SurfaceFlinger* flinger;
2413 DisplayID dpy;
2414 sp<IMemoryHeap>* heap;
2415 uint32_t* w;
2416 uint32_t* h;
2417 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 uint32_t sw;
2419 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002420 uint32_t minLayerZ;
2421 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002422 status_t result;
2423 public:
2424 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002425 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002426 uint32_t sw, uint32_t sh,
2427 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002428 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002429 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2430 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2431 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002432 {
2433 }
2434 status_t getResult() const {
2435 return result;
2436 }
2437 virtual bool handler() {
2438 Mutex::Autolock _l(flinger->mStateLock);
2439
2440 // if we have secure windows, never allow the screen capture
2441 if (flinger->mSecureFrameBuffer)
2442 return true;
2443
Mathias Agopian74c40c02010-09-29 13:02:36 -07002444 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002445 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002446
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447 return true;
2448 }
2449 };
2450
2451 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002452 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002453 status_t res = postMessageSync(msg);
2454 if (res == NO_ERROR) {
2455 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2456 }
2457 return res;
2458}
2459
2460// ---------------------------------------------------------------------------
2461
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002462sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2463{
2464 sp<Layer> result;
2465 Mutex::Autolock _l(mStateLock);
2466 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2467 return result;
2468}
2469
2470// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002471
Mathias Agopian96f08192010-06-02 23:28:45 -07002472Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002473 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002474{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002475}
2476
Mathias Agopian96f08192010-06-02 23:28:45 -07002477Client::~Client()
2478{
Mathias Agopian96f08192010-06-02 23:28:45 -07002479 const size_t count = mLayers.size();
2480 for (size_t i=0 ; i<count ; i++) {
2481 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2482 if (layer != 0) {
2483 mFlinger->removeLayer(layer);
2484 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 }
2486}
2487
Mathias Agopian96f08192010-06-02 23:28:45 -07002488status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002489 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002491
Mathias Agopian4f113742011-05-03 16:21:41 -07002492size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002493{
Mathias Agopian4f113742011-05-03 16:21:41 -07002494 Mutex::Autolock _l(mLock);
2495 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002496 mLayers.add(name, layer);
2497 return name;
2498}
2499
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002500void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002501{
Mathias Agopian4f113742011-05-03 16:21:41 -07002502 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002503 // we do a linear search here, because this doesn't happen often
2504 const size_t count = mLayers.size();
2505 for (size_t i=0 ; i<count ; i++) {
2506 if (mLayers.valueAt(i) == layer) {
2507 mLayers.removeItemsAt(i, 1);
2508 break;
2509 }
2510 }
2511}
Mathias Agopian4f113742011-05-03 16:21:41 -07002512sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2513{
2514 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002515 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002516 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002517 if (layer != 0) {
2518 lbc = layer.promote();
2519 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002520 }
2521 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522}
2523
Mathias Agopiana67932f2011-04-20 14:20:59 -07002524
2525status_t Client::onTransact(
2526 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2527{
2528 // these must be checked
2529 IPCThreadState* ipc = IPCThreadState::self();
2530 const int pid = ipc->getCallingPid();
2531 const int uid = ipc->getCallingUid();
2532 const int self_pid = getpid();
2533 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2534 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002535 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002536 {
2537 LOGE("Permission Denial: "
2538 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2539 return PERMISSION_DENIED;
2540 }
2541 }
2542 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002543}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002544
2545
Mathias Agopian96f08192010-06-02 23:28:45 -07002546sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002547 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002548 const String8& name,
2549 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550 uint32_t flags)
2551{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002552 /*
2553 * createSurface must be called from the GL thread so that it can
2554 * have access to the GL context.
2555 */
2556
2557 class MessageCreateSurface : public MessageBase {
2558 sp<ISurface> result;
2559 SurfaceFlinger* flinger;
2560 ISurfaceComposerClient::surface_data_t* params;
2561 Client* client;
2562 const String8& name;
2563 DisplayID display;
2564 uint32_t w, h;
2565 PixelFormat format;
2566 uint32_t flags;
2567 public:
2568 MessageCreateSurface(SurfaceFlinger* flinger,
2569 ISurfaceComposerClient::surface_data_t* params,
2570 const String8& name, Client* client,
2571 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2572 uint32_t flags)
2573 : flinger(flinger), params(params), client(client), name(name),
2574 display(display), w(w), h(h), format(format), flags(flags)
2575 {
2576 }
2577 sp<ISurface> getResult() const { return result; }
2578 virtual bool handler() {
2579 result = flinger->createSurface(params, name, client,
2580 display, w, h, format, flags);
2581 return true;
2582 }
2583 };
2584
2585 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2586 params, name, this, display, w, h, format, flags);
2587 mFlinger->postMessageSync(msg);
2588 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002589}
Mathias Agopian96f08192010-06-02 23:28:45 -07002590status_t Client::destroySurface(SurfaceID sid) {
2591 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002592}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002593
2594// ---------------------------------------------------------------------------
2595
Jamie Gennis9a78c902011-01-12 18:30:40 -08002596GraphicBufferAlloc::GraphicBufferAlloc() {}
2597
2598GraphicBufferAlloc::~GraphicBufferAlloc() {}
2599
2600sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002601 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002602 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2603 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002604 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002605 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002606 if (err == NO_MEMORY) {
2607 GraphicBuffer::dumpAllocationsToSystemLog();
2608 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002609 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2610 "failed (%s), handle=%p",
2611 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002612 return 0;
2613 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002614 return graphicBuffer;
2615}
2616
Jamie Gennis9a78c902011-01-12 18:30:40 -08002617// ---------------------------------------------------------------------------
2618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619GraphicPlane::GraphicPlane()
2620 : mHw(0)
2621{
2622}
2623
2624GraphicPlane::~GraphicPlane() {
2625 delete mHw;
2626}
2627
2628bool GraphicPlane::initialized() const {
2629 return mHw ? true : false;
2630}
2631
Mathias Agopian2b92d892010-02-08 15:49:35 -08002632int GraphicPlane::getWidth() const {
2633 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634}
2635
Mathias Agopian2b92d892010-02-08 15:49:35 -08002636int GraphicPlane::getHeight() const {
2637 return mHeight;
2638}
2639
2640void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2641{
2642 mHw = hw;
2643
2644 // initialize the display orientation transform.
2645 // it's a constant that should come from the display driver.
2646 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2647 char property[PROPERTY_VALUE_MAX];
2648 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2649 //displayOrientation
2650 switch (atoi(property)) {
2651 case 90:
2652 displayOrientation = ISurfaceComposer::eOrientation90;
2653 break;
2654 case 270:
2655 displayOrientation = ISurfaceComposer::eOrientation270;
2656 break;
2657 }
2658 }
2659
2660 const float w = hw->getWidth();
2661 const float h = hw->getHeight();
2662 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2663 &mDisplayTransform);
2664 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2665 mDisplayWidth = h;
2666 mDisplayHeight = w;
2667 } else {
2668 mDisplayWidth = w;
2669 mDisplayHeight = h;
2670 }
2671
2672 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673}
2674
2675status_t GraphicPlane::orientationToTransfrom(
2676 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002677{
2678 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002679 switch (orientation) {
2680 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002681 flags = Transform::ROT_0;
2682 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002683 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002684 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002685 break;
2686 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002687 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688 break;
2689 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002690 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002691 break;
2692 default:
2693 return BAD_VALUE;
2694 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002695 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696 return NO_ERROR;
2697}
2698
2699status_t GraphicPlane::setOrientation(int orientation)
2700{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002701 // If the rotation can be handled in hardware, this is where
2702 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002703
2704 const DisplayHardware& hw(displayHardware());
2705 const float w = mDisplayWidth;
2706 const float h = mDisplayHeight;
2707 mWidth = int(w);
2708 mHeight = int(h);
2709
2710 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002711 GraphicPlane::orientationToTransfrom(orientation, w, h,
2712 &orientationTransform);
2713 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2714 mWidth = int(h);
2715 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002716 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002717
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002718 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002719 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002720 return NO_ERROR;
2721}
2722
2723const DisplayHardware& GraphicPlane::displayHardware() const {
2724 return *mHw;
2725}
2726
Mathias Agopian59119e62010-10-11 12:37:43 -07002727DisplayHardware& GraphicPlane::editDisplayHardware() {
2728 return *mHw;
2729}
2730
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002731const Transform& GraphicPlane::transform() const {
2732 return mGlobalTransform;
2733}
2734
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002735EGLDisplay GraphicPlane::getEGLDisplay() const {
2736 return mHw->getEGLDisplay();
2737}
2738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002739// ---------------------------------------------------------------------------
2740
2741}; // namespace android