blob: 3dccc1130ea23f37c0e4a06c5292e3c847c3adcf [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"
Mathias Agopianf171ab62011-10-13 16:02:48 -070053#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
56#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070057#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopiana67932f2011-04-20 14:20:59 -070059#include <private/surfaceflinger/SharedBufferStack.h>
60
Mathias Agopiana1ecca92009-05-21 19:21:59 -070061/* ideally AID_GRAPHICS would be in a semi-public header
62 * or there would be a way to map a user/group name to its id
63 */
64#ifndef AID_GRAPHICS
65#define AID_GRAPHICS 1003
66#endif
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070083 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070091 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070094 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070095 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070096 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070097 mDebugInSwapBuffers(0),
98 mLastSwapBufferTime(0),
99 mDebugInTransaction(0),
100 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700101 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mConsoleSignals(0),
103 mSecureFrameBuffer(0)
104{
105 init();
106}
107
108void SurfaceFlinger::init()
109{
110 LOGI("SurfaceFlinger is starting");
111
112 // debugging stuff...
113 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 property_get("debug.sf.showupdates", value, "0");
116 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 property_get("debug.sf.showbackground", value, "0");
119 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121 property_get("debug.sf.ddms", value, "0");
122 mDebugDDMS = atoi(value);
123 if (mDebugDDMS) {
124 DdmConnection::start(getServiceName());
125 }
126
Mathias Agopian78fd5012010-04-20 14:51:04 -0700127 LOGI_IF(mDebugRegion, "showupdates enabled");
128 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700129 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
132SurfaceFlinger::~SurfaceFlinger()
133{
134 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700137sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700139 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian7e27f052010-05-28 14:22:23 -0700142sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143{
Mathias Agopian96f08192010-06-02 23:28:45 -0700144 sp<ISurfaceComposerClient> bclient;
145 sp<Client> client(new Client(this));
146 status_t err = client->initCheck();
147 if (err == NO_ERROR) {
148 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 return bclient;
151}
152
Jamie Gennis9a78c902011-01-12 18:30:40 -0800153sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
154{
155 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
156 return gba;
157}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
160{
161 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
162 const GraphicPlane& plane(mGraphicPlanes[dpy]);
163 return plane;
164}
165
166GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
167{
168 return const_cast<GraphicPlane&>(
169 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
170}
171
172void SurfaceFlinger::bootFinished()
173{
174 const nsecs_t now = systemTime();
175 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700176 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700177 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700178
179 // wait patiently for the window manager death
180 const String16 name("window");
181 sp<IBinder> window(defaultServiceManager()->getService(name));
182 if (window != 0) {
183 window->linkToDeath(this);
184 }
185
186 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700187 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188}
189
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700190void SurfaceFlinger::binderDied(const wp<IBinder>& who)
191{
192 // the window manager died on us. prepare its eulogy.
193
194 // unfreeze the screen in case it was... frozen
195 mFreezeDisplayTime = 0;
196 mFreezeCount = 0;
197 mFreezeDisplay = false;
198
199 // reset screen orientation
200 setOrientation(0, eOrientationDefault, 0);
201
202 // restart the boot-animation
203 property_set("ctl.start", "bootanim");
204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206void SurfaceFlinger::onFirstRef()
207{
208 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
209
210 // Wait for the main thread to be done with its initialization
211 mReadyToRunBarrier.wait();
212}
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214static inline uint16_t pack565(int r, int g, int b) {
215 return (r<<11)|(g<<5)|b;
216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218status_t SurfaceFlinger::readyToRun()
219{
220 LOGI( "SurfaceFlinger's main thread ready to run. "
221 "Initializing graphics H/W...");
222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 // we only support one display currently
224 int dpy = 0;
225
226 {
227 // initialize the main display
228 GraphicPlane& plane(graphicPlane(dpy));
229 DisplayHardware* const hw = new DisplayHardware(this, dpy);
230 plane.setDisplayHardware(hw);
231 }
232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 // create the shared control-block
234 mServerHeap = new MemoryHeapBase(4096,
235 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
236 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700238 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
239 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700240
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700241 new(mServerCblk) surface_flinger_cblk_t;
242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 // initialize primary screen
244 // (other display should be initialized in the same manner, but
245 // asynchronously, as they could come and go. None of this is supported
246 // yet).
247 const GraphicPlane& plane(graphicPlane(dpy));
248 const DisplayHardware& hw = plane.displayHardware();
249 const uint32_t w = hw.getWidth();
250 const uint32_t h = hw.getHeight();
251 const uint32_t f = hw.getFormat();
252 hw.makeCurrent();
253
254 // initialize the shared control block
255 mServerCblk->connected |= 1<<dpy;
256 display_cblk_t* dcblk = mServerCblk->displays + dpy;
257 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800258 dcblk->w = plane.getWidth();
259 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 dcblk->format = f;
261 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
262 dcblk->xdpi = hw.getDpiX();
263 dcblk->ydpi = hw.getDpiY();
264 dcblk->fps = hw.getRefreshRate();
265 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266
267 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700269 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 glEnableClientState(GL_VERTEX_ARRAY);
271 glEnable(GL_SCISSOR_TEST);
272 glShadeModel(GL_FLAT);
273 glDisable(GL_DITHER);
274 glDisable(GL_CULL_FACE);
275
276 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
277 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 glGenTextures(1, &mWormholeTexName);
280 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700286 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
287
288 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
289 glGenTextures(1, &mProtectedTexName);
290 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
293 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
294 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
296 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
298 glViewport(0, 0, w, h);
299 glMatrixMode(GL_PROJECTION);
300 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700301 // put the origin in the left-bottom corner
302 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 -0800303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 mReadyToRunBarrier.open();
305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
317#if 0
318#pragma mark -
319#pragma mark Events Handler
320#endif
321
322void SurfaceFlinger::waitForEvent()
323{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700324 while (true) {
325 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800326 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700327 if (UNLIKELY(isFrozen())) {
328 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700329 const nsecs_t now = systemTime();
330 if (mFreezeDisplayTime == 0) {
331 mFreezeDisplayTime = now;
332 }
333 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
334 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700335 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700336
Mathias Agopianbb641242010-05-18 17:06:55 -0700337 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800338
339 // see if we timed out
340 if (isFrozen()) {
341 const nsecs_t now = systemTime();
342 nsecs_t frozenTime = (now - mFreezeDisplayTime);
343 if (frozenTime >= freezeDisplayTimeout) {
344 // we timed out and are still frozen
345 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
346 mFreezeDisplay, mFreezeCount);
347 mFreezeDisplayTime = 0;
348 mFreezeCount = 0;
349 mFreezeDisplay = false;
350 }
351 }
352
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700353 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700354 switch (msg->what) {
355 case MessageQueue::INVALIDATE:
356 // invalidate message, just return to the main loop
357 return;
358 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 }
361}
362
363void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700364 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}
366
Jamie Gennis582270d2011-08-17 18:19:00 -0700367bool SurfaceFlinger::authenticateSurfaceTexture(
368 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800369 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700370 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800371
372 // Check the visible layer list for the ISurface
373 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
374 size_t count = currentLayers.size();
375 for (size_t i=0 ; i<count ; i++) {
376 const sp<LayerBase>& layer(currentLayers[i]);
377 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700378 if (lbc != NULL) {
379 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
380 if (lbcBinder == surfaceTextureBinder) {
381 return true;
382 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800383 }
384 }
385
386 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700387 // SurfaceTexture gets destroyed before all the clients are done using it,
388 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800389 // will instead fail later on when the client tries to use the surface,
390 // which should be reported as "surface XYZ returned an -ENODEV". The
391 // purgatorized layers are no less authentic than the visible ones, so this
392 // should not cause any harm.
393 size_t purgatorySize = mLayerPurgatory.size();
394 for (size_t i=0 ; i<purgatorySize ; i++) {
395 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
396 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700397 if (lbc != NULL) {
398 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
399 if (lbcBinder == surfaceTextureBinder) {
400 return true;
401 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800402 }
403 }
404
405 return false;
406}
407
Mathias Agopianbb641242010-05-18 17:06:55 -0700408status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
409 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410{
Mathias Agopianbb641242010-05-18 17:06:55 -0700411 return mEventQueue.postMessage(msg, reltime, flags);
412}
413
414status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
415 nsecs_t reltime, uint32_t flags)
416{
417 status_t res = mEventQueue.postMessage(msg, reltime, flags);
418 if (res == NO_ERROR) {
419 msg->wait();
420 }
421 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
423
424// ----------------------------------------------------------------------------
425#if 0
426#pragma mark -
427#pragma mark Main loop
428#endif
429
430bool SurfaceFlinger::threadLoop()
431{
432 waitForEvent();
433
434 // check for transactions
435 if (UNLIKELY(mConsoleSignals)) {
436 handleConsoleEvents();
437 }
438
Mathias Agopian698c0872011-06-28 19:09:31 -0700439 // if we're in a global transaction, don't do anything.
440 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
441 uint32_t transactionFlags = peekTransactionFlags(mask);
442 if (UNLIKELY(transactionFlags)) {
443 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444 }
445
446 // post surfaces (if needed)
447 handlePageFlip();
448
Mathias Agopiana350ff92010-08-10 17:14:02 -0700449 if (UNLIKELY(mHwWorkListDirty)) {
450 // build the h/w work list
451 handleWorkList();
452 }
453
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian8a77baa2009-09-27 22:47:27 -0700455 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700457
458 const int index = hw.getCurrentBufferIndex();
459 GraphicLog& logger(GraphicLog::getInstance());
460
461 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462 handleRepaint();
463
Mathias Agopian74faca22009-09-17 16:18:16 -0700464 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700465 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700466 hw.compositionComplete();
467
Mathias Agopian35b48d12010-09-13 22:57:58 -0700468 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700469 postFramebuffer();
470
Mathias Agopian35b48d12010-09-13 22:57:58 -0700471 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472 } else {
473 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800474 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 usleep(16667); // 60 fps period
476 }
477 return true;
478}
479
480void SurfaceFlinger::postFramebuffer()
481{
Mathias Agopian0656a682011-09-20 17:22:44 -0700482 if (!mSwapRegion.isEmpty()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian9795c422009-08-26 16:36:26 -0700484 const nsecs_t now = systemTime();
485 mDebugInSwapBuffers = now;
Mathias Agopian0656a682011-09-20 17:22:44 -0700486 hw.flip(mSwapRegion);
Mathias Agopian9795c422009-08-26 16:36:26 -0700487 mLastSwapBufferTime = systemTime() - now;
488 mDebugInSwapBuffers = 0;
Mathias Agopian0656a682011-09-20 17:22:44 -0700489 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490 }
491}
492
493void SurfaceFlinger::handleConsoleEvents()
494{
495 // something to do with the console
496 const DisplayHardware& hw = graphicPlane(0).displayHardware();
497
498 int what = android_atomic_and(0, &mConsoleSignals);
499 if (what & eConsoleAcquired) {
500 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700501 // this is a temporary work-around, eventually this should be called
502 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700503 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 }
505
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800506 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700507 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 }
510 }
511
512 mDirtyRegion.set(hw.bounds());
513}
514
515void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
516{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700517 Mutex::Autolock _l(mStateLock);
518 const nsecs_t now = systemTime();
519 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520
Mathias Agopianca4d3602011-05-19 15:38:14 -0700521 // Here we're guaranteed that some transaction flags are set
522 // so we can call handleTransactionLocked() unconditionally.
523 // We call getTransactionFlags(), which will also clear the flags,
524 // with mStateLock held to guarantee that mCurrentState won't change
525 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700526
Mathias Agopianca4d3602011-05-19 15:38:14 -0700527 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
528 transactionFlags = getTransactionFlags(mask);
529 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700530
Mathias Agopianca4d3602011-05-19 15:38:14 -0700531 mLastTransactionTime = systemTime() - now;
532 mDebugInTransaction = 0;
533 invalidateHwcGeometry();
534 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700535}
536
Mathias Agopianca4d3602011-05-19 15:38:14 -0700537void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700538{
539 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 const size_t count = currentLayers.size();
541
542 /*
543 * Traversal of the children
544 * (perform the transaction for each of them if needed)
545 */
546
547 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
548 if (layersNeedTransaction) {
549 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700550 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
552 if (!trFlags) continue;
553
554 const uint32_t flags = layer->doTransaction(0);
555 if (flags & Layer::eVisibleRegion)
556 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 }
558 }
559
560 /*
561 * Perform our own transaction if needed
562 */
563
564 if (transactionFlags & eTransactionNeeded) {
565 if (mCurrentState.orientation != mDrawingState.orientation) {
566 // the orientation has changed, recompute all visible regions
567 // and invalidate everything.
568
569 const int dpy = 0;
570 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700571 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 GraphicPlane& plane(graphicPlane(dpy));
573 plane.setOrientation(orientation);
574
575 // update the shared control block
576 const DisplayHardware& hw(plane.displayHardware());
577 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
578 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800579 dcblk->w = plane.getWidth();
580 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581
582 mVisibleRegionsDirty = true;
583 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 }
585
586 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
587 // freezing or unfreezing the display -> trigger animation if needed
588 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800589 if (mFreezeDisplay)
590 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 }
592
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700593 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
594 // layers have been added
595 mVisibleRegionsDirty = true;
596 }
597
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 // some layers might have been removed, so
599 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700601 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700603 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700604 const size_t count = previousLayers.size();
605 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700606 const sp<LayerBase>& layer(previousLayers[i]);
607 if (currentLayers.indexOf( layer ) < 0) {
608 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700609 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700610 }
611 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613 }
614
615 commitTransaction();
616}
617
618sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
619{
620 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
621}
622
623void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800624 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625{
626 const GraphicPlane& plane(graphicPlane(0));
627 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700628 const DisplayHardware& hw(plane.displayHardware());
629 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630
631 Region aboveOpaqueLayers;
632 Region aboveCoveredLayers;
633 Region dirty;
634
635 bool secureFrameBuffer = false;
636
637 size_t i = currentLayers.size();
638 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700639 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 layer->validateVisibility(planeTransform);
641
642 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700643 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644
Mathias Agopianab028732010-03-16 16:41:46 -0700645 /*
646 * opaqueRegion: area of a surface that is fully opaque.
647 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700649
650 /*
651 * visibleRegion: area of a surface that is visible on screen
652 * and not fully transparent. This is essentially the layer's
653 * footprint minus the opaque regions above it.
654 * Areas covered by a translucent surface are considered visible.
655 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700657
658 /*
659 * coveredRegion: area of a surface that is covered by all
660 * visible regions above it (which includes the translucent areas).
661 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700663
664
665 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700666 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700667 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700668 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700670 visibleRegion.andSelf(screenRegion);
671 if (!visibleRegion.isEmpty()) {
672 // Remove the transparent area from the visible region
673 if (translucent) {
674 visibleRegion.subtractSelf(layer->transparentRegionScreen);
675 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676
Mathias Agopianab028732010-03-16 16:41:46 -0700677 // compute the opaque region
678 const int32_t layerOrientation = layer->getOrientation();
679 if (s.alpha==255 && !translucent &&
680 ((layerOrientation & Transform::ROT_INVALID) == false)) {
681 // the opaque region is the layer's footprint
682 opaqueRegion = visibleRegion;
683 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 }
685 }
686
Mathias Agopianab028732010-03-16 16:41:46 -0700687 // Clip the covered region to the visible region
688 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
689
690 // Update aboveCoveredLayers for next (lower) layer
691 aboveCoveredLayers.orSelf(visibleRegion);
692
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 // subtract the opaque region covered by the layers above us
694 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695
696 // compute this layer's dirty region
697 if (layer->contentDirty) {
698 // we need to invalidate the whole region
699 dirty = visibleRegion;
700 // as well, as the old visible region
701 dirty.orSelf(layer->visibleRegionScreen);
702 layer->contentDirty = false;
703 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700704 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700705 * the exposed region consists of two components:
706 * 1) what's VISIBLE now and was COVERED before
707 * 2) what's EXPOSED now less what was EXPOSED before
708 *
709 * note that (1) is conservative, we start with the whole
710 * visible region but only keep what used to be covered by
711 * something -- which mean it may have been exposed.
712 *
713 * (2) handles areas that were not covered by anything but got
714 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700715 */
Mathias Agopianab028732010-03-16 16:41:46 -0700716 const Region newExposed = visibleRegion - coveredRegion;
717 const Region oldVisibleRegion = layer->visibleRegionScreen;
718 const Region oldCoveredRegion = layer->coveredRegionScreen;
719 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
720 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 }
722 dirty.subtractSelf(aboveOpaqueLayers);
723
724 // accumulate to the screen dirty region
725 dirtyRegion.orSelf(dirty);
726
Mathias Agopianab028732010-03-16 16:41:46 -0700727 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700729
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 // Store the visible region is screen space
731 layer->setVisibleRegion(visibleRegion);
732 layer->setCoveredRegion(coveredRegion);
733
Mathias Agopian97011222009-07-28 10:57:27 -0700734 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 if (layer->isSecure() && !visibleRegion.isEmpty()) {
736 secureFrameBuffer = true;
737 }
738 }
739
Mathias Agopian97011222009-07-28 10:57:27 -0700740 // invalidate the areas where a layer was removed
741 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
742 mDirtyRegionRemovedLayer.clear();
743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 mSecureFrameBuffer = secureFrameBuffer;
745 opaqueRegion = aboveOpaqueLayers;
746}
747
748
749void SurfaceFlinger::commitTransaction()
750{
751 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700752 mResizeTransationPending = false;
753 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754}
755
756void SurfaceFlinger::handlePageFlip()
757{
758 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800759 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 visibleRegions |= lockPageFlip(currentLayers);
761
762 const DisplayHardware& hw = graphicPlane(0).displayHardware();
763 const Region screenRegion(hw.bounds());
764 if (visibleRegions) {
765 Region opaqueRegion;
766 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700767
768 /*
769 * rebuild the visible layer list
770 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800771 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700772 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700773 mVisibleLayersSortedByZ.setCapacity(count);
774 for (size_t i=0 ; i<count ; i++) {
775 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
776 mVisibleLayersSortedByZ.add(currentLayers[i]);
777 }
778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 mWormholeRegion = screenRegion.subtract(opaqueRegion);
780 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800781 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 }
783
784 unlockPageFlip(currentLayers);
785 mDirtyRegion.andSelf(screenRegion);
786}
787
Mathias Agopianad456f92011-01-13 17:53:01 -0800788void SurfaceFlinger::invalidateHwcGeometry()
789{
790 mHwWorkListDirty = true;
791}
792
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
794{
795 bool recomputeVisibleRegions = false;
796 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700797 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700799 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 layer->lockPageFlip(recomputeVisibleRegions);
801 }
802 return recomputeVisibleRegions;
803}
804
805void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
806{
807 const GraphicPlane& plane(graphicPlane(0));
808 const Transform& planeTransform(plane.transform());
809 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700810 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700812 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813 layer->unlockPageFlip(planeTransform, mDirtyRegion);
814 }
815}
816
Mathias Agopiana350ff92010-08-10 17:14:02 -0700817void SurfaceFlinger::handleWorkList()
818{
819 mHwWorkListDirty = false;
820 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
821 if (hwc.initCheck() == NO_ERROR) {
822 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
823 const size_t count = currentLayers.size();
824 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700825 hwc_layer_t* const cur(hwc.getLayers());
826 for (size_t i=0 ; cur && i<count ; i++) {
827 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700828 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700829 cur[i].compositionType = HWC_FRAMEBUFFER;
830 cur[i].flags |= HWC_SKIP_LAYER;
831 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700832 }
833 }
834}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700835
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836void SurfaceFlinger::handleRepaint()
837{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700838 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700839 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840
841 if (UNLIKELY(mDebugRegion)) {
842 debugFlashRegions();
843 }
844
Mathias Agopianb8a55602009-06-26 19:06:36 -0700845 // set the frame buffer
846 const DisplayHardware& hw(graphicPlane(0).displayHardware());
847 glMatrixMode(GL_MODELVIEW);
848 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849
850 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700851 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
852 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700853 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700854 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
855 // takes a rectangle, we must make sure to update that whole
856 // rectangle in that case
857 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700858 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700859 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700860 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700862 // in the BUFFER_PRESERVED case, obviously, we can update only
863 // what's needed and nothing more.
864 // NOTE: this is NOT a common case, as preserving the backbuffer
865 // is costly and usually involves copying the whole update back.
866 }
867 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700868 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700869 // We need to redraw the rectangle that will be updated
870 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700871 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700872 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700873 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700874 } else {
875 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700877 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 }
879 }
880
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700881 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 composeSurfaces(mDirtyRegion);
883
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700884 // update the swap region and clear the dirty region
885 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 mDirtyRegion.clear();
887}
888
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700889void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700891 const DisplayHardware& hw(graphicPlane(0).displayHardware());
892 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700893 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700894 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700895 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700896 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700897
Mathias Agopianf384cc32011-09-08 18:31:55 -0700898 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
899 size_t count = layers.size();
900
901 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700902 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
903 hwc.getNumLayers(), count);
904
905 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700906 if (hwc.initCheck() == NO_ERROR) {
907 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
908 }
Mathias Agopian45721772010-08-12 15:03:26 -0700909
910 /*
911 * update the per-frame h/w composer data for each layer
912 * and build the transparent region of the FB
913 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700914 for (size_t i=0 ; i<count ; i++) {
915 const sp<LayerBase>& layer(layers[i]);
916 layer->setPerFrameData(&cur[i]);
917 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700918 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700919 status_t err = hwc.prepare();
920 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700921
Mathias Agopianf384cc32011-09-08 18:31:55 -0700922 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700923 // what's happening here is tricky.
924 // we want to clear all the layers with the CLEAR_FB flags
925 // that are opaque.
926 // however, since some GPU are efficient at preserving
927 // the backbuffer, we want to take advantage of that so we do the
928 // clear only in the dirty region (other areas will be preserved
929 // on those GPUs).
930 // NOTE: on non backbuffer preserving GPU, the dirty region
931 // has already been expanded as needed, so the code is correct
932 // there too.
933 //
934 // However, the content of the framebuffer cannot be trusted when
935 // we switch to/from FB/OVERLAY, in which case we need to
936 // expand the dirty region to those areas too.
937 //
938 // Note also that there is a special case when switching from
939 // "no layers in FB" to "some layers in FB", where we need to redraw
940 // the entire FB, since some areas might contain uninitialized
941 // data.
942 //
943 // Also we want to make sure to not clear areas that belong to
944 // layers above that won't redraw (we would just erasing them),
945 // that is, we can't erase anything outside the dirty region.
946
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700947 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700948
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700949 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
950 transparent.set(hw.getBounds());
951 dirtyInOut = transparent;
952 } else {
953 for (size_t i=0 ; i<count ; i++) {
954 const sp<LayerBase>& layer(layers[i]);
955 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
956 transparent.orSelf(layer->visibleRegionScreen);
957 }
958 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
959 if (isOverlay != layer->isOverlay()) {
960 // we transitioned to/from overlay, so add this layer
961 // to the dirty region so the framebuffer can be either
962 // cleared or redrawn.
963 dirtyInOut.orSelf(layer->visibleRegionScreen);
964 }
965 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800966 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700967 // don't erase stuff outside the dirty region
968 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700969 }
970
971 /*
972 * clear the area of the FB that need to be transparent
973 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700974 if (!transparent.isEmpty()) {
975 glClearColor(0,0,0,0);
976 Region::const_iterator it = transparent.begin();
977 Region::const_iterator const end = transparent.end();
978 const int32_t height = hw.getHeight();
979 while (it != end) {
980 const Rect& r(*it++);
981 const GLint sy = height - (r.top + r.height());
982 glScissor(r.left, sy, r.width(), r.height());
983 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800984 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700985 }
986 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700987}
Mathias Agopian45721772010-08-12 15:03:26 -0700988
Mathias Agopianf384cc32011-09-08 18:31:55 -0700989void SurfaceFlinger::composeSurfaces(const Region& dirty)
990{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700991 const DisplayHardware& hw(graphicPlane(0).displayHardware());
992 HWComposer& hwc(hw.getHwComposer());
993
994 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
995 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700996 // should never happen unless the window manager has a bug
997 // draw something...
998 drawWormhole();
999 }
1000
Mathias Agopian45721772010-08-12 15:03:26 -07001001 /*
1002 * and then, render the layers targeted at the framebuffer
1003 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001004 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001005 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1006 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001007 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001008 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001009 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001010 }
1011 const sp<LayerBase>& layer(layers[i]);
1012 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1013 if (!clip.isEmpty()) {
1014 layer->draw(clip);
1015 }
1016 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017}
1018
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019void SurfaceFlinger::debugFlashRegions()
1020{
Mathias Agopian0a917752010-06-14 21:20:00 -07001021 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1022 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001023 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001024 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001025 return;
1026 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001027
Mathias Agopian0a917752010-06-14 21:20:00 -07001028 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1029 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1030 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1031 mDirtyRegion.bounds() : hw.bounds());
1032 composeSurfaces(repaint);
1033 }
1034
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 glDisable(GL_BLEND);
1036 glDisable(GL_DITHER);
1037 glDisable(GL_SCISSOR_TEST);
1038
Mathias Agopian0926f502009-05-04 14:17:04 -07001039 static int toggle = 0;
1040 toggle = 1 - toggle;
1041 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001042 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001043 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001044 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001045 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046
Mathias Agopian20f68782009-05-11 00:03:41 -07001047 Region::const_iterator it = mDirtyRegion.begin();
1048 Region::const_iterator const end = mDirtyRegion.end();
1049 while (it != end) {
1050 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001052 { r.left, height - r.top },
1053 { r.left, height - r.bottom },
1054 { r.right, height - r.bottom },
1055 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 };
1057 glVertexPointer(2, GL_FLOAT, 0, vertices);
1058 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1059 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001060
Mathias Agopian0656a682011-09-20 17:22:44 -07001061 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062
1063 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001064 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065
1066 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067}
1068
1069void SurfaceFlinger::drawWormhole() const
1070{
1071 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1072 if (region.isEmpty())
1073 return;
1074
1075 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1076 const int32_t width = hw.getWidth();
1077 const int32_t height = hw.getHeight();
1078
1079 glDisable(GL_BLEND);
1080 glDisable(GL_DITHER);
1081
1082 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001083 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001084 Region::const_iterator it = region.begin();
1085 Region::const_iterator const end = region.end();
1086 while (it != end) {
1087 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 const GLint sy = height - (r.top + r.height());
1089 glScissor(r.left, sy, r.width(), r.height());
1090 glClear(GL_COLOR_BUFFER_BIT);
1091 }
1092 } else {
1093 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1094 { width, height }, { 0, height } };
1095 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1096 glVertexPointer(2, GL_SHORT, 0, vertices);
1097 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1098 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Gold7f198b62010-09-15 15:46:24 -07001099#if defined(GL_OES_EGL_image_external)
Mathias Agopian1f7bec62010-06-25 18:02:21 -07001100 if (GLExtensions::getInstance().haveTextureExternal()) {
1101 glDisable(GL_TEXTURE_EXTERNAL_OES);
1102 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001103#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 glEnable(GL_TEXTURE_2D);
1105 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1106 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1107 glMatrixMode(GL_TEXTURE);
1108 glLoadIdentity();
1109 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001110 Region::const_iterator it = region.begin();
1111 Region::const_iterator const end = region.end();
1112 while (it != end) {
1113 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 const GLint sy = height - (r.top + r.height());
1115 glScissor(r.left, sy, r.width(), r.height());
1116 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1117 }
1118 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001119 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001120 glLoadIdentity();
1121 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 }
1123}
1124
1125void SurfaceFlinger::debugShowFPS() const
1126{
1127 static int mFrameCount;
1128 static int mLastFrameCount = 0;
1129 static nsecs_t mLastFpsTime = 0;
1130 static float mFps = 0;
1131 mFrameCount++;
1132 nsecs_t now = systemTime();
1133 nsecs_t diff = now - mLastFpsTime;
1134 if (diff > ms2ns(250)) {
1135 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1136 mLastFpsTime = now;
1137 mLastFrameCount = mFrameCount;
1138 }
1139 // XXX: mFPS has the value we want
1140 }
1141
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001142status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143{
1144 Mutex::Autolock _l(mStateLock);
1145 addLayer_l(layer);
1146 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1147 return NO_ERROR;
1148}
1149
Mathias Agopian96f08192010-06-02 23:28:45 -07001150status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1151{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001152 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001153 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1154}
1155
1156ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1157 const sp<LayerBaseClient>& lbc)
1158{
Mathias Agopian96f08192010-06-02 23:28:45 -07001159 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001160 size_t name = client->attachLayer(lbc);
1161
1162 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001163
1164 // add this layer to the current state list
1165 addLayer_l(lbc);
1166
Mathias Agopian4f113742011-05-03 16:21:41 -07001167 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001168}
1169
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001170status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171{
1172 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001173 status_t err = purgatorizeLayer_l(layer);
1174 if (err == NO_ERROR)
1175 setTransactionFlags(eTransactionNeeded);
1176 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177}
1178
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001179status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001181 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1182 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001183 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1186 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001187 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 return NO_ERROR;
1189 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001190 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191}
1192
Mathias Agopian9a112062009-04-17 19:36:26 -07001193status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1194{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001195 // First add the layer to the purgatory list, which makes sure it won't
1196 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001197 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001198 if (err >= 0) {
1199 mLayerPurgatory.add(layerBase);
1200 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001201
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001202 layerBase->onRemoved();
1203
Mathias Agopian3d579642009-06-04 18:46:21 -07001204 // it's possible that we don't find a layer, because it might
1205 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001206 // from the user because there is a race between Client::destroySurface(),
1207 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001208 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1209}
1210
Mathias Agopian96f08192010-06-02 23:28:45 -07001211status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212{
Mathias Agopian96f08192010-06-02 23:28:45 -07001213 layer->forceVisibilityTransaction();
1214 setTransactionFlags(eTraversalNeeded);
1215 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216}
1217
Mathias Agopiandea20b12011-05-03 17:04:02 -07001218uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1219{
1220 return android_atomic_release_load(&mTransactionFlags);
1221}
1222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1224{
1225 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1226}
1227
Mathias Agopianbb641242010-05-18 17:06:55 -07001228uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229{
1230 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1231 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001232 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 }
1234 return old;
1235}
1236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001238void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
1239 int orientation) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001240 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001241
Mathias Agopian698c0872011-06-28 19:09:31 -07001242 uint32_t flags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001243 if (mCurrentState.orientation != orientation) {
1244 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1245 mCurrentState.orientation = orientation;
1246 flags |= eTransactionNeeded;
1247 mResizeTransationPending = true;
1248 } else if (orientation != eOrientationUnchanged) {
1249 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1250 orientation);
1251 }
1252 }
1253
Mathias Agopian698c0872011-06-28 19:09:31 -07001254 const size_t count = state.size();
1255 for (size_t i=0 ; i<count ; i++) {
1256 const ComposerState& s(state[i]);
1257 sp<Client> client( static_cast<Client *>(s.client.get()) );
1258 flags |= setClientStateLocked(client, s.state);
1259 }
1260 if (flags) {
1261 setTransactionFlags(flags);
1262 }
1263
1264 signalEvent();
1265
1266 // if there is a transaction with a resize, wait for it to
1267 // take effect before returning.
1268 while (mResizeTransationPending) {
1269 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1270 if (CC_UNLIKELY(err != NO_ERROR)) {
1271 // just in case something goes wrong in SF, return to the
1272 // called after a few seconds.
1273 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1274 mResizeTransationPending = false;
1275 break;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001276 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 }
1278}
1279
1280status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1281{
1282 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1283 return BAD_VALUE;
1284
1285 Mutex::Autolock _l(mStateLock);
1286 mCurrentState.freezeDisplay = 1;
1287 setTransactionFlags(eTransactionNeeded);
1288
1289 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001290 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 return NO_ERROR;
1292}
1293
1294status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1295{
1296 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1297 return BAD_VALUE;
1298
1299 Mutex::Autolock _l(mStateLock);
1300 mCurrentState.freezeDisplay = 0;
1301 setTransactionFlags(eTransactionNeeded);
1302
1303 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001304 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 return NO_ERROR;
1306}
1307
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001308int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001309 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310{
1311 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1312 return BAD_VALUE;
1313
1314 Mutex::Autolock _l(mStateLock);
1315 if (mCurrentState.orientation != orientation) {
1316 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001317 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 mCurrentState.orientation = orientation;
1319 setTransactionFlags(eTransactionNeeded);
1320 mTransactionCV.wait(mStateLock);
1321 } else {
1322 orientation = BAD_VALUE;
1323 }
1324 }
1325 return orientation;
1326}
1327
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001328sp<ISurface> SurfaceFlinger::createSurface(
1329 ISurfaceComposerClient::surface_data_t* params,
1330 const String8& name,
1331 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1333 uint32_t flags)
1334{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001335 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001336 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001337
1338 if (int32_t(w|h) < 0) {
1339 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1340 int(w), int(h));
1341 return surfaceHandle;
1342 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001343
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001345 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 switch (flags & eFXSurfaceMask) {
1347 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001348 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1349 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 break;
1351 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001352 // for now we treat Blur as Dim, until we can implement it
1353 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001355 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356 break;
Mathias Agopianf171ab62011-10-13 16:02:48 -07001357 case eFXSurfaceScreenshot:
1358 layer = createScreenshotSurface(client, d, w, h, flags);
1359 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 }
1361
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001362 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001363 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001364 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001365 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001368 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001369 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001370 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001371 if (normalLayer != 0) {
1372 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001373 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001374 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001375 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001376
Mathias Agopian96f08192010-06-02 23:28:45 -07001377 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 }
1379
1380 return surfaceHandle;
1381}
1382
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001383sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001384 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001385 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001386 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387{
1388 // initialize the surfaces
1389 switch (format) { // TODO: take h/w into account
1390 case PIXEL_FORMAT_TRANSPARENT:
1391 case PIXEL_FORMAT_TRANSLUCENT:
1392 format = PIXEL_FORMAT_RGBA_8888;
1393 break;
1394 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001395#ifdef NO_RGBX_8888
1396 format = PIXEL_FORMAT_RGB_565;
1397#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001398 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001399#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400 break;
1401 }
1402
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001403#ifdef NO_RGBX_8888
1404 if (format == PIXEL_FORMAT_RGBX_8888)
1405 format = PIXEL_FORMAT_RGBA_8888;
1406#endif
1407
Mathias Agopian96f08192010-06-02 23:28:45 -07001408 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001409 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001410 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001412 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 }
1414 return layer;
1415}
1416
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001417sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001418 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001419 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420{
Mathias Agopian96f08192010-06-02 23:28:45 -07001421 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopianf171ab62011-10-13 16:02:48 -07001422 return layer;
1423}
1424
1425sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1426 const sp<Client>& client, DisplayID display,
1427 uint32_t w, uint32_t h, uint32_t flags)
1428{
1429 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
1430 status_t err = layer->capture();
1431 if (err != NO_ERROR) {
1432 layer.clear();
1433 LOGW("createScreenshotSurface failed (%s)", strerror(-err));
1434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 return layer;
1436}
1437
Mathias Agopian96f08192010-06-02 23:28:45 -07001438status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
Mathias Agopian9a112062009-04-17 19:36:26 -07001440 /*
1441 * called by the window manager, when a surface should be marked for
1442 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001443 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001444 * The surface is removed from the current and drawing lists, but placed
1445 * in the purgatory queue, so it's not destroyed right-away (we need
1446 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001447 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001448
Mathias Agopian48d819a2009-09-10 19:41:18 -07001449 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001450 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001451 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001452 if (layer != 0) {
1453 err = purgatorizeLayer_l(layer);
1454 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001455 setTransactionFlags(eTransactionNeeded);
1456 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001457 }
1458 return err;
1459}
1460
Mathias Agopianca4d3602011-05-19 15:38:14 -07001461status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001462{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001463 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001464 status_t err = NO_ERROR;
1465 sp<LayerBaseClient> l(layer.promote());
1466 if (l != NULL) {
1467 Mutex::Autolock _l(mStateLock);
1468 err = removeLayer_l(l);
1469 if (err == NAME_NOT_FOUND) {
1470 // The surface wasn't in the current list, which means it was
1471 // removed already, which means it is in the purgatory,
1472 // and need to be removed from there.
1473 ssize_t idx = mLayerPurgatory.remove(l);
1474 LOGE_IF(idx < 0,
1475 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001476 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001477 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1478 "error removing layer=%p (%s)", l.get(), strerror(-err));
1479 }
1480 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481}
1482
Mathias Agopian698c0872011-06-28 19:09:31 -07001483uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001484 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001485 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001488 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1489 if (layer != 0) {
1490 const uint32_t what = s.what;
1491 if (what & ePositionChanged) {
1492 if (layer->setPosition(s.x, s.y))
1493 flags |= eTraversalNeeded;
1494 }
1495 if (what & eLayerChanged) {
1496 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1497 if (layer->setLayer(s.z)) {
1498 mCurrentState.layersSortedByZ.removeAt(idx);
1499 mCurrentState.layersSortedByZ.add(layer);
1500 // we need traversal (state changed)
1501 // AND transaction (list changed)
1502 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 }
1504 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001505 if (what & eSizeChanged) {
1506 if (layer->setSize(s.w, s.h)) {
1507 flags |= eTraversalNeeded;
1508 mResizeTransationPending = true;
1509 }
1510 }
1511 if (what & eAlphaChanged) {
1512 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1513 flags |= eTraversalNeeded;
1514 }
1515 if (what & eMatrixChanged) {
1516 if (layer->setMatrix(s.matrix))
1517 flags |= eTraversalNeeded;
1518 }
1519 if (what & eTransparentRegionChanged) {
1520 if (layer->setTransparentRegionHint(s.transparentRegion))
1521 flags |= eTraversalNeeded;
1522 }
1523 if (what & eVisibilityChanged) {
1524 if (layer->setFlags(s.flags, s.mask))
1525 flags |= eTraversalNeeded;
1526 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001528 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529}
1530
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531void SurfaceFlinger::screenReleased(int dpy)
1532{
1533 // this may be called by a signal handler, we can't do too much in here
1534 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1535 signalEvent();
1536}
1537
1538void SurfaceFlinger::screenAcquired(int dpy)
1539{
1540 // this may be called by a signal handler, we can't do too much in here
1541 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1542 signalEvent();
1543}
1544
1545status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1546{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001547 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 char buffer[SIZE];
1549 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001550
1551 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 snprintf(buffer, SIZE, "Permission Denial: "
1553 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1554 IPCThreadState::self()->getCallingPid(),
1555 IPCThreadState::self()->getCallingUid());
1556 result.append(buffer);
1557 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001558
1559 // figure out if we're stuck somewhere
1560 const nsecs_t now = systemTime();
1561 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1562 const nsecs_t inTransaction(mDebugInTransaction);
1563 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1564 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1565
1566 // Try to get the main lock, but don't insist if we can't
1567 // (this would indicate SF is stuck, but we want to be able to
1568 // print something in dumpsys).
1569 int retry = 3;
1570 while (mStateLock.tryLock()<0 && --retry>=0) {
1571 usleep(1000000);
1572 }
1573 const bool locked(retry >= 0);
1574 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001575 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001576 "SurfaceFlinger appears to be unresponsive, "
1577 "dumping anyways (no locks held)\n");
1578 result.append(buffer);
1579 }
1580
Mathias Agopian48b888a2011-01-19 16:15:53 -08001581 /*
1582 * Dump the visible layer list
1583 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1585 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001586 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1587 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001589 const sp<LayerBase>& layer(currentLayers[i]);
1590 layer->dump(result, buffer, SIZE);
1591 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 s.transparentRegion.dump(result, "transparentRegion");
1593 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1594 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1595 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001596
Mathias Agopian48b888a2011-01-19 16:15:53 -08001597 /*
1598 * Dump the layers in the purgatory
1599 */
1600
1601 const size_t purgatorySize = mLayerPurgatory.size();
1602 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1603 result.append(buffer);
1604 for (size_t i=0 ; i<purgatorySize ; i++) {
1605 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1606 layer->shortDump(result, buffer, SIZE);
1607 }
1608
1609 /*
1610 * Dump SurfaceFlinger global state
1611 */
1612
Mathias Agopianad701862011-07-14 18:01:49 -07001613 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001614 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001615
1616 const GLExtensions& extensions(GLExtensions::getInstance());
1617 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1618 extensions.getVendor(),
1619 extensions.getRenderer(),
1620 extensions.getVersion());
1621 result.append(buffer);
1622 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1623 result.append(buffer);
1624
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 mWormholeRegion.dump(result, "WormholeRegion");
1626 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1627 snprintf(buffer, SIZE,
1628 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1629 mFreezeDisplay?"yes":"no", mFreezeCount,
1630 mCurrentState.orientation, hw.canDraw());
1631 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001632 snprintf(buffer, SIZE,
1633 " last eglSwapBuffers() time: %f us\n"
1634 " last transaction time : %f us\n",
1635 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1636 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001637
Mathias Agopian9795c422009-08-26 16:36:26 -07001638 if (inSwapBuffersDuration || !locked) {
1639 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1640 inSwapBuffersDuration/1000.0);
1641 result.append(buffer);
1642 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001643
Mathias Agopian9795c422009-08-26 16:36:26 -07001644 if (inTransactionDuration || !locked) {
1645 snprintf(buffer, SIZE, " transaction time: %f us\n",
1646 inTransactionDuration/1000.0);
1647 result.append(buffer);
1648 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001649
Mathias Agopian48b888a2011-01-19 16:15:53 -08001650 /*
1651 * Dump HWComposer state
1652 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001653 HWComposer& hwc(hw.getHwComposer());
1654 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1655 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001656 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001657 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001658 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001659
Mathias Agopian48b888a2011-01-19 16:15:53 -08001660 /*
1661 * Dump gralloc state
1662 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001663 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001664 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001665 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001666
1667 if (locked) {
1668 mStateLock.unlock();
1669 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 }
1671 write(fd, result.string(), result.size());
1672 return NO_ERROR;
1673}
1674
1675status_t SurfaceFlinger::onTransact(
1676 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1677{
1678 switch (code) {
1679 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001680 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 case SET_ORIENTATION:
1682 case FREEZE_DISPLAY:
1683 case UNFREEZE_DISPLAY:
1684 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001685 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001686 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 {
1688 // codes that require permission check
1689 IPCThreadState* ipc = IPCThreadState::self();
1690 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001691 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001692 if ((uid != AID_GRAPHICS) &&
1693 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001694 LOGE("Permission Denial: "
1695 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1696 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001698 break;
1699 }
1700 case CAPTURE_SCREEN:
1701 {
1702 // codes that require permission check
1703 IPCThreadState* ipc = IPCThreadState::self();
1704 const int pid = ipc->getCallingPid();
1705 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001706 if ((uid != AID_GRAPHICS) &&
1707 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001708 LOGE("Permission Denial: "
1709 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1710 return PERMISSION_DENIED;
1711 }
1712 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713 }
1714 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001715
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1717 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001718 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001719 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001720 IPCThreadState* ipc = IPCThreadState::self();
1721 const int pid = ipc->getCallingPid();
1722 const int uid = ipc->getCallingUid();
1723 LOGE("Permission Denial: "
1724 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 return PERMISSION_DENIED;
1726 }
1727 int n;
1728 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001729 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001730 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 return NO_ERROR;
1732 case 1002: // SHOW_UPDATES
1733 n = data.readInt32();
1734 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001735 invalidateHwcGeometry();
1736 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 return NO_ERROR;
1738 case 1003: // SHOW_BACKGROUND
1739 n = data.readInt32();
1740 mDebugBackground = n ? 1 : 0;
1741 return NO_ERROR;
1742 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001743 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001744 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001746 case 1005:{ // force transaction
1747 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1748 return NO_ERROR;
1749 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001750 case 1006:{ // enable/disable GraphicLog
1751 int enabled = data.readInt32();
1752 GraphicLog::getInstance().setEnabled(enabled);
1753 return NO_ERROR;
1754 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 case 1007: // set mFreezeCount
1756 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001757 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 return NO_ERROR;
Mathias Agopian53331da2011-08-22 21:44:41 -07001759 case 1008: // toggle use of hw composer
1760 n = data.readInt32();
1761 mDebugDisableHWC = n ? 1 : 0;
1762 invalidateHwcGeometry();
1763 repaintEverything();
1764 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001765 case 1009: // toggle use of transform hint
1766 n = data.readInt32();
1767 mDebugDisableTransformHint = n ? 1 : 0;
1768 invalidateHwcGeometry();
1769 repaintEverything();
1770 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001772 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773 reply->writeInt32(0);
1774 reply->writeInt32(mDebugRegion);
1775 reply->writeInt32(mDebugBackground);
1776 return NO_ERROR;
1777 case 1013: {
1778 Mutex::Autolock _l(mStateLock);
1779 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1780 reply->writeInt32(hw.getPageFlipCount());
1781 }
1782 return NO_ERROR;
1783 }
1784 }
1785 return err;
1786}
1787
Mathias Agopian53331da2011-08-22 21:44:41 -07001788void SurfaceFlinger::repaintEverything() {
1789 Mutex::Autolock _l(mStateLock);
1790 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1791 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1792 signalEvent();
1793}
1794
Mathias Agopian59119e62010-10-11 12:37:43 -07001795// ---------------------------------------------------------------------------
1796
Mathias Agopianf171ab62011-10-13 16:02:48 -07001797status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1798 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1799{
1800 Mutex::Autolock _l(mStateLock);
1801 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1802}
1803
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001804status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1805 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001806{
Mathias Agopian59119e62010-10-11 12:37:43 -07001807 if (!GLExtensions::getInstance().haveFramebufferObject())
1808 return INVALID_OPERATION;
1809
1810 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001811 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001812 const uint32_t hw_w = hw.getWidth();
1813 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001814 GLfloat u = 1;
1815 GLfloat v = 1;
1816
1817 // make sure to clear all GL error flags
1818 while ( glGetError() != GL_NO_ERROR ) ;
1819
1820 // create a FBO
1821 GLuint name, tname;
1822 glGenTextures(1, &tname);
1823 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001824 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1825 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001826 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001827 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001828 GLint tw = (2 << (31 - clz(hw_w)));
1829 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001830 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1831 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001832 u = GLfloat(hw_w) / tw;
1833 v = GLfloat(hw_h) / th;
1834 }
1835 glGenFramebuffersOES(1, &name);
1836 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001837 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1838 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001839
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001840 // redraw the screen entirely...
1841 glClearColor(0,0,0,1);
1842 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001843 glMatrixMode(GL_MODELVIEW);
1844 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001845 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1846 const size_t count = layers.size();
1847 for (size_t i=0 ; i<count ; ++i) {
1848 const sp<LayerBase>& layer(layers[i]);
1849 layer->drawForSreenShot();
1850 }
1851
1852 // back to main framebuffer
1853 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1854 glDisable(GL_SCISSOR_TEST);
1855 glDeleteFramebuffersOES(1, &name);
1856
1857 *textureName = tname;
1858 *uOut = u;
1859 *vOut = v;
1860 return NO_ERROR;
1861}
1862
1863// ---------------------------------------------------------------------------
1864
1865status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1866{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001867 // get screen geometry
1868 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1869 const uint32_t hw_w = hw.getWidth();
1870 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001871 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001872
1873 GLfloat u, v;
1874 GLuint tname;
Mathias Agopianf171ab62011-10-13 16:02:48 -07001875 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001876 if (result != NO_ERROR) {
1877 return result;
1878 }
1879
1880 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001881 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001882 glBindTexture(GL_TEXTURE_2D, tname);
1883 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1884 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1885 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1886 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1887 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1888 glVertexPointer(2, GL_FLOAT, 0, vtx);
1889
Mathias Agopianffcf4652011-07-07 17:30:31 -07001890 /*
1891 * Texture coordinate mapping
1892 *
1893 * u
1894 * 1 +----------+---+
1895 * | | | | image is inverted
1896 * | V | | w.r.t. the texture
1897 * 1-v +----------+ | coordinates
1898 * | |
1899 * | |
1900 * | |
1901 * 0 +--------------+
1902 * 0 1
1903 *
1904 */
1905
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001906 class s_curve_interpolator {
1907 const float nbFrames, s, v;
1908 public:
1909 s_curve_interpolator(int nbFrames, float s)
1910 : nbFrames(1.0f / (nbFrames-1)), s(s),
1911 v(1.0f + expf(-s + 0.5f*s)) {
1912 }
1913 float operator()(int f) {
1914 const float x = f * nbFrames;
1915 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1916 }
1917 };
1918
1919 class v_stretch {
1920 const GLfloat hw_w, hw_h;
1921 public:
1922 v_stretch(uint32_t hw_w, uint32_t hw_h)
1923 : hw_w(hw_w), hw_h(hw_h) {
1924 }
1925 void operator()(GLfloat* vtx, float v) {
1926 const GLfloat w = hw_w + (hw_w * v);
1927 const GLfloat h = hw_h - (hw_h * v);
1928 const GLfloat x = (hw_w - w) * 0.5f;
1929 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001930 vtx[0] = x; vtx[1] = y;
1931 vtx[2] = x; vtx[3] = y + h;
1932 vtx[4] = x + w; vtx[5] = y + h;
1933 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001934 }
1935 };
1936
1937 class h_stretch {
1938 const GLfloat hw_w, hw_h;
1939 public:
1940 h_stretch(uint32_t hw_w, uint32_t hw_h)
1941 : hw_w(hw_w), hw_h(hw_h) {
1942 }
1943 void operator()(GLfloat* vtx, float v) {
1944 const GLfloat w = hw_w - (hw_w * v);
1945 const GLfloat h = 1.0f;
1946 const GLfloat x = (hw_w - w) * 0.5f;
1947 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001948 vtx[0] = x; vtx[1] = y;
1949 vtx[2] = x; vtx[3] = y + h;
1950 vtx[4] = x + w; vtx[5] = y + h;
1951 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001952 }
1953 };
1954
1955 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001956 char value[PROPERTY_VALUE_MAX];
1957 property_get("debug.sf.electron_frames", value, "24");
1958 int nbFrames = (atoi(value) + 1) >> 1;
1959 if (nbFrames <= 0) // just in case
1960 nbFrames = 24;
1961
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001962 s_curve_interpolator itr(nbFrames, 7.5f);
1963 s_curve_interpolator itg(nbFrames, 8.0f);
1964 s_curve_interpolator itb(nbFrames, 8.5f);
1965
1966 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001967
1968 glMatrixMode(GL_TEXTURE);
1969 glLoadIdentity();
1970 glMatrixMode(GL_MODELVIEW);
1971 glLoadIdentity();
1972
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001973 glEnable(GL_BLEND);
1974 glBlendFunc(GL_ONE, GL_ONE);
1975 for (int i=0 ; i<nbFrames ; i++) {
1976 float x, y, w, h;
1977 const float vr = itr(i);
1978 const float vg = itg(i);
1979 const float vb = itb(i);
1980
1981 // clear screen
1982 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001983 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001984 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001985
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001986 // draw the red plane
1987 vverts(vtx, vr);
1988 glColorMask(1,0,0,1);
1989 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001990
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001991 // draw the green plane
1992 vverts(vtx, vg);
1993 glColorMask(0,1,0,1);
1994 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001995
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001996 // draw the blue plane
1997 vverts(vtx, vb);
1998 glColorMask(0,0,1,1);
1999 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002000
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002001 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002002 glDisable(GL_TEXTURE_2D);
2003 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 glColor4f(vg, vg, vg, 1);
2005 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2006 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002007 }
2008
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 h_stretch hverts(hw_w, hw_h);
2010 glDisable(GL_BLEND);
2011 glDisable(GL_TEXTURE_2D);
2012 glColorMask(1,1,1,1);
2013 for (int i=0 ; i<nbFrames ; i++) {
2014 const float v = itg(i);
2015 hverts(vtx, v);
2016 glClear(GL_COLOR_BUFFER_BIT);
2017 glColor4f(1-v, 1-v, 1-v, 1);
2018 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2019 hw.flip(screenBounds);
2020 }
2021
2022 glColorMask(1,1,1,1);
2023 glEnable(GL_SCISSOR_TEST);
2024 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2025 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002026 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002027 return NO_ERROR;
2028}
2029
2030status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2031{
2032 status_t result = PERMISSION_DENIED;
2033
2034 if (!GLExtensions::getInstance().haveFramebufferObject())
2035 return INVALID_OPERATION;
2036
2037
2038 // get screen geometry
2039 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2040 const uint32_t hw_w = hw.getWidth();
2041 const uint32_t hw_h = hw.getHeight();
2042 const Region screenBounds(hw.bounds());
2043
2044 GLfloat u, v;
2045 GLuint tname;
2046 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2047 if (result != NO_ERROR) {
2048 return result;
2049 }
2050
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 GLfloat vtx[8];
2052 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002053 glBindTexture(GL_TEXTURE_2D, tname);
2054 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2055 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2056 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2057 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2058 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2059 glVertexPointer(2, GL_FLOAT, 0, vtx);
2060
2061 class s_curve_interpolator {
2062 const float nbFrames, s, v;
2063 public:
2064 s_curve_interpolator(int nbFrames, float s)
2065 : nbFrames(1.0f / (nbFrames-1)), s(s),
2066 v(1.0f + expf(-s + 0.5f*s)) {
2067 }
2068 float operator()(int f) {
2069 const float x = f * nbFrames;
2070 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2071 }
2072 };
2073
2074 class v_stretch {
2075 const GLfloat hw_w, hw_h;
2076 public:
2077 v_stretch(uint32_t hw_w, uint32_t hw_h)
2078 : hw_w(hw_w), hw_h(hw_h) {
2079 }
2080 void operator()(GLfloat* vtx, float v) {
2081 const GLfloat w = hw_w + (hw_w * v);
2082 const GLfloat h = hw_h - (hw_h * v);
2083 const GLfloat x = (hw_w - w) * 0.5f;
2084 const GLfloat y = (hw_h - h) * 0.5f;
2085 vtx[0] = x; vtx[1] = y;
2086 vtx[2] = x; vtx[3] = y + h;
2087 vtx[4] = x + w; vtx[5] = y + h;
2088 vtx[6] = x + w; vtx[7] = y;
2089 }
2090 };
2091
2092 class h_stretch {
2093 const GLfloat hw_w, hw_h;
2094 public:
2095 h_stretch(uint32_t hw_w, uint32_t hw_h)
2096 : hw_w(hw_w), hw_h(hw_h) {
2097 }
2098 void operator()(GLfloat* vtx, float v) {
2099 const GLfloat w = hw_w - (hw_w * v);
2100 const GLfloat h = 1.0f;
2101 const GLfloat x = (hw_w - w) * 0.5f;
2102 const GLfloat y = (hw_h - h) * 0.5f;
2103 vtx[0] = x; vtx[1] = y;
2104 vtx[2] = x; vtx[3] = y + h;
2105 vtx[4] = x + w; vtx[5] = y + h;
2106 vtx[6] = x + w; vtx[7] = y;
2107 }
2108 };
2109
Mathias Agopiana6546e52010-10-14 12:33:07 -07002110 // the full animation is 12 frames
2111 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002112 s_curve_interpolator itr(nbFrames, 7.5f);
2113 s_curve_interpolator itg(nbFrames, 8.0f);
2114 s_curve_interpolator itb(nbFrames, 8.5f);
2115
2116 h_stretch hverts(hw_w, hw_h);
2117 glDisable(GL_BLEND);
2118 glDisable(GL_TEXTURE_2D);
2119 glColorMask(1,1,1,1);
2120 for (int i=nbFrames-1 ; i>=0 ; i--) {
2121 const float v = itg(i);
2122 hverts(vtx, v);
2123 glClear(GL_COLOR_BUFFER_BIT);
2124 glColor4f(1-v, 1-v, 1-v, 1);
2125 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2126 hw.flip(screenBounds);
2127 }
2128
Mathias Agopiana6546e52010-10-14 12:33:07 -07002129 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002130 v_stretch vverts(hw_w, hw_h);
2131 glEnable(GL_BLEND);
2132 glBlendFunc(GL_ONE, GL_ONE);
2133 for (int i=nbFrames-1 ; i>=0 ; i--) {
2134 float x, y, w, h;
2135 const float vr = itr(i);
2136 const float vg = itg(i);
2137 const float vb = itb(i);
2138
2139 // clear screen
2140 glColorMask(1,1,1,1);
2141 glClear(GL_COLOR_BUFFER_BIT);
2142 glEnable(GL_TEXTURE_2D);
2143
2144 // draw the red plane
2145 vverts(vtx, vr);
2146 glColorMask(1,0,0,1);
2147 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2148
2149 // draw the green plane
2150 vverts(vtx, vg);
2151 glColorMask(0,1,0,1);
2152 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2153
2154 // draw the blue plane
2155 vverts(vtx, vb);
2156 glColorMask(0,0,1,1);
2157 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2158
2159 hw.flip(screenBounds);
2160 }
2161
2162 glColorMask(1,1,1,1);
2163 glEnable(GL_SCISSOR_TEST);
2164 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002165 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002166 glDisable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002167
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002168 return NO_ERROR;
2169}
2170
2171// ---------------------------------------------------------------------------
2172
Mathias Agopianabd671a2010-10-14 14:54:06 -07002173status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002174{
2175 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2176 if (!hw.canDraw()) {
2177 // we're already off
2178 return NO_ERROR;
2179 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002180
2181 // turn off hwc while we're doing the animation
2182 hw.getHwComposer().disable();
2183 // and make sure to turn it back on (if needed) next time we compose
2184 invalidateHwcGeometry();
2185
Mathias Agopianabd671a2010-10-14 14:54:06 -07002186 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2187 electronBeamOffAnimationImplLocked();
2188 }
2189
2190 // always clear the whole screen at the end of the animation
2191 glClearColor(0,0,0,1);
2192 glDisable(GL_SCISSOR_TEST);
2193 glClear(GL_COLOR_BUFFER_BIT);
2194 glEnable(GL_SCISSOR_TEST);
2195 hw.flip( Region(hw.bounds()) );
2196
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002197 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002198}
2199
2200status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2201{
Mathias Agopian59119e62010-10-11 12:37:43 -07002202 class MessageTurnElectronBeamOff : public MessageBase {
2203 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002204 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002205 status_t result;
2206 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002207 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2208 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002209 }
2210 status_t getResult() const {
2211 return result;
2212 }
2213 virtual bool handler() {
2214 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002215 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002216 return true;
2217 }
2218 };
2219
Mathias Agopianabd671a2010-10-14 14:54:06 -07002220 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002221 status_t res = postMessageSync(msg);
2222 if (res == NO_ERROR) {
2223 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002224
2225 // work-around: when the power-manager calls us we activate the
2226 // animation. eventually, the "on" animation will be called
2227 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002228 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002229 }
2230 return res;
2231}
2232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002233// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002234
Mathias Agopianabd671a2010-10-14 14:54:06 -07002235status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002236{
2237 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2238 if (hw.canDraw()) {
2239 // we're already on
2240 return NO_ERROR;
2241 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002242 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2243 electronBeamOnAnimationImplLocked();
2244 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002245
2246 // make sure to redraw the whole screen when the animation is done
2247 mDirtyRegion.set(hw.bounds());
2248 signalEvent();
2249
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002250 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002251}
2252
2253status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2254{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002255 class MessageTurnElectronBeamOn : public MessageBase {
2256 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002257 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002258 status_t result;
2259 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2261 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002262 }
2263 status_t getResult() const {
2264 return result;
2265 }
2266 virtual bool handler() {
2267 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002268 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002269 return true;
2270 }
2271 };
2272
Mathias Agopianabd671a2010-10-14 14:54:06 -07002273 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002274 return NO_ERROR;
2275}
2276
2277// ---------------------------------------------------------------------------
2278
Mathias Agopian74c40c02010-09-29 13:02:36 -07002279status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2280 sp<IMemoryHeap>* heap,
2281 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002282 uint32_t sw, uint32_t sh,
2283 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002284{
2285 status_t result = PERMISSION_DENIED;
2286
2287 // only one display supported for now
2288 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2289 return BAD_VALUE;
2290
2291 if (!GLExtensions::getInstance().haveFramebufferObject())
2292 return INVALID_OPERATION;
2293
2294 // get screen geometry
2295 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2296 const uint32_t hw_w = hw.getWidth();
2297 const uint32_t hw_h = hw.getHeight();
2298
2299 if ((sw > hw_w) || (sh > hw_h))
2300 return BAD_VALUE;
2301
2302 sw = (!sw) ? hw_w : sw;
2303 sh = (!sh) ? hw_h : sh;
2304 const size_t size = sw * sh * 4;
2305
Mathias Agopian1c71a472011-03-02 18:45:50 -08002306 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2307 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002308
Mathias Agopian74c40c02010-09-29 13:02:36 -07002309 // make sure to clear all GL error flags
2310 while ( glGetError() != GL_NO_ERROR ) ;
2311
2312 // create a FBO
2313 GLuint name, tname;
2314 glGenRenderbuffersOES(1, &tname);
2315 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2316 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2317 glGenFramebuffersOES(1, &name);
2318 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2319 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2320 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2321
2322 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002323
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2325
2326 // invert everything, b/c glReadPixel() below will invert the FB
2327 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002328 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002329 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002330 glMatrixMode(GL_PROJECTION);
2331 glPushMatrix();
2332 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002333 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002334 glMatrixMode(GL_MODELVIEW);
2335
2336 // redraw the screen entirely...
2337 glClearColor(0,0,0,1);
2338 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002339
Jamie Gennis9575f602011-10-07 14:51:16 -07002340 const LayerVector& layers(mDrawingState.layersSortedByZ);
2341 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002342 for (size_t i=0 ; i<count ; ++i) {
2343 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002344 const uint32_t flags = layer->drawingState().flags;
2345 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2346 const uint32_t z = layer->drawingState().z;
2347 if (z >= minLayerZ && z <= maxLayerZ) {
2348 layer->drawForSreenShot();
2349 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002350 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002351 }
2352
2353 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002354 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355 glScissor(0, 0, sw, sh);
2356
2357 // check for errors and return screen capture
2358 if (glGetError() != GL_NO_ERROR) {
2359 // error while rendering
2360 result = INVALID_OPERATION;
2361 } else {
2362 // allocate shared memory large enough to hold the
2363 // screen capture
2364 sp<MemoryHeapBase> base(
2365 new MemoryHeapBase(size, 0, "screen-capture") );
2366 void* const ptr = base->getBase();
2367 if (ptr) {
2368 // capture the screen with glReadPixels()
2369 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2370 if (glGetError() == GL_NO_ERROR) {
2371 *heap = base;
2372 *w = sw;
2373 *h = sh;
2374 *f = PIXEL_FORMAT_RGBA_8888;
2375 result = NO_ERROR;
2376 }
2377 } else {
2378 result = NO_MEMORY;
2379 }
2380 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 glEnable(GL_SCISSOR_TEST);
2382 glViewport(0, 0, hw_w, hw_h);
2383 glMatrixMode(GL_PROJECTION);
2384 glPopMatrix();
2385 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002386 } else {
2387 result = BAD_VALUE;
2388 }
2389
2390 // release FBO resources
2391 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2392 glDeleteRenderbuffersOES(1, &tname);
2393 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002394
2395 hw.compositionComplete();
2396
Mathias Agopian1c71a472011-03-02 18:45:50 -08002397 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002398
Mathias Agopian74c40c02010-09-29 13:02:36 -07002399 return result;
2400}
2401
2402
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002403status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2404 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002406 uint32_t sw, uint32_t sh,
2407 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002408{
2409 // only one display supported for now
2410 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2411 return BAD_VALUE;
2412
2413 if (!GLExtensions::getInstance().haveFramebufferObject())
2414 return INVALID_OPERATION;
2415
2416 class MessageCaptureScreen : public MessageBase {
2417 SurfaceFlinger* flinger;
2418 DisplayID dpy;
2419 sp<IMemoryHeap>* heap;
2420 uint32_t* w;
2421 uint32_t* h;
2422 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 uint32_t sw;
2424 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002425 uint32_t minLayerZ;
2426 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002427 status_t result;
2428 public:
2429 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002430 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002431 uint32_t sw, uint32_t sh,
2432 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002433 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002434 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2435 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2436 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002437 {
2438 }
2439 status_t getResult() const {
2440 return result;
2441 }
2442 virtual bool handler() {
2443 Mutex::Autolock _l(flinger->mStateLock);
2444
2445 // if we have secure windows, never allow the screen capture
2446 if (flinger->mSecureFrameBuffer)
2447 return true;
2448
Mathias Agopian74c40c02010-09-29 13:02:36 -07002449 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002450 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002451
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002452 return true;
2453 }
2454 };
2455
2456 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002457 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458 status_t res = postMessageSync(msg);
2459 if (res == NO_ERROR) {
2460 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2461 }
2462 return res;
2463}
2464
2465// ---------------------------------------------------------------------------
2466
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002467sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2468{
2469 sp<Layer> result;
2470 Mutex::Autolock _l(mStateLock);
2471 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2472 return result;
2473}
2474
2475// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476
Mathias Agopian96f08192010-06-02 23:28:45 -07002477Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002478 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480}
2481
Mathias Agopian96f08192010-06-02 23:28:45 -07002482Client::~Client()
2483{
Mathias Agopian96f08192010-06-02 23:28:45 -07002484 const size_t count = mLayers.size();
2485 for (size_t i=0 ; i<count ; i++) {
2486 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2487 if (layer != 0) {
2488 mFlinger->removeLayer(layer);
2489 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490 }
2491}
2492
Mathias Agopian96f08192010-06-02 23:28:45 -07002493status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002494 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002496
Mathias Agopian4f113742011-05-03 16:21:41 -07002497size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002498{
Mathias Agopian4f113742011-05-03 16:21:41 -07002499 Mutex::Autolock _l(mLock);
2500 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002501 mLayers.add(name, layer);
2502 return name;
2503}
2504
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002505void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002506{
Mathias Agopian4f113742011-05-03 16:21:41 -07002507 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002508 // we do a linear search here, because this doesn't happen often
2509 const size_t count = mLayers.size();
2510 for (size_t i=0 ; i<count ; i++) {
2511 if (mLayers.valueAt(i) == layer) {
2512 mLayers.removeItemsAt(i, 1);
2513 break;
2514 }
2515 }
2516}
Mathias Agopian4f113742011-05-03 16:21:41 -07002517sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2518{
2519 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002520 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002521 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002522 if (layer != 0) {
2523 lbc = layer.promote();
2524 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002525 }
2526 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527}
2528
Mathias Agopiana67932f2011-04-20 14:20:59 -07002529
2530status_t Client::onTransact(
2531 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2532{
2533 // these must be checked
2534 IPCThreadState* ipc = IPCThreadState::self();
2535 const int pid = ipc->getCallingPid();
2536 const int uid = ipc->getCallingUid();
2537 const int self_pid = getpid();
2538 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2539 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002540 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002541 {
2542 LOGE("Permission Denial: "
2543 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2544 return PERMISSION_DENIED;
2545 }
2546 }
2547 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002548}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002549
2550
Mathias Agopian96f08192010-06-02 23:28:45 -07002551sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002552 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002553 const String8& name,
2554 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002555 uint32_t flags)
2556{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002557 /*
2558 * createSurface must be called from the GL thread so that it can
2559 * have access to the GL context.
2560 */
2561
2562 class MessageCreateSurface : public MessageBase {
2563 sp<ISurface> result;
2564 SurfaceFlinger* flinger;
2565 ISurfaceComposerClient::surface_data_t* params;
2566 Client* client;
2567 const String8& name;
2568 DisplayID display;
2569 uint32_t w, h;
2570 PixelFormat format;
2571 uint32_t flags;
2572 public:
2573 MessageCreateSurface(SurfaceFlinger* flinger,
2574 ISurfaceComposerClient::surface_data_t* params,
2575 const String8& name, Client* client,
2576 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2577 uint32_t flags)
2578 : flinger(flinger), params(params), client(client), name(name),
2579 display(display), w(w), h(h), format(format), flags(flags)
2580 {
2581 }
2582 sp<ISurface> getResult() const { return result; }
2583 virtual bool handler() {
2584 result = flinger->createSurface(params, name, client,
2585 display, w, h, format, flags);
2586 return true;
2587 }
2588 };
2589
2590 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2591 params, name, this, display, w, h, format, flags);
2592 mFlinger->postMessageSync(msg);
2593 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002594}
Mathias Agopian96f08192010-06-02 23:28:45 -07002595status_t Client::destroySurface(SurfaceID sid) {
2596 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002597}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598
2599// ---------------------------------------------------------------------------
2600
Jamie Gennis9a78c902011-01-12 18:30:40 -08002601GraphicBufferAlloc::GraphicBufferAlloc() {}
2602
2603GraphicBufferAlloc::~GraphicBufferAlloc() {}
2604
2605sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002606 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002607 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2608 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002609 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002610 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002611 if (err == NO_MEMORY) {
2612 GraphicBuffer::dumpAllocationsToSystemLog();
2613 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002614 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2615 "failed (%s), handle=%p",
2616 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002617 return 0;
2618 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002619 return graphicBuffer;
2620}
2621
Jamie Gennis9a78c902011-01-12 18:30:40 -08002622// ---------------------------------------------------------------------------
2623
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002624GraphicPlane::GraphicPlane()
2625 : mHw(0)
2626{
2627}
2628
2629GraphicPlane::~GraphicPlane() {
2630 delete mHw;
2631}
2632
2633bool GraphicPlane::initialized() const {
2634 return mHw ? true : false;
2635}
2636
Mathias Agopian2b92d892010-02-08 15:49:35 -08002637int GraphicPlane::getWidth() const {
2638 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002639}
2640
Mathias Agopian2b92d892010-02-08 15:49:35 -08002641int GraphicPlane::getHeight() const {
2642 return mHeight;
2643}
2644
2645void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2646{
2647 mHw = hw;
2648
2649 // initialize the display orientation transform.
2650 // it's a constant that should come from the display driver.
2651 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2652 char property[PROPERTY_VALUE_MAX];
2653 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2654 //displayOrientation
2655 switch (atoi(property)) {
2656 case 90:
2657 displayOrientation = ISurfaceComposer::eOrientation90;
2658 break;
2659 case 270:
2660 displayOrientation = ISurfaceComposer::eOrientation270;
2661 break;
2662 }
2663 }
2664
2665 const float w = hw->getWidth();
2666 const float h = hw->getHeight();
2667 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2668 &mDisplayTransform);
2669 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2670 mDisplayWidth = h;
2671 mDisplayHeight = w;
2672 } else {
2673 mDisplayWidth = w;
2674 mDisplayHeight = h;
2675 }
2676
2677 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678}
2679
2680status_t GraphicPlane::orientationToTransfrom(
2681 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002682{
2683 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684 switch (orientation) {
2685 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002686 flags = Transform::ROT_0;
2687 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002689 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690 break;
2691 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002692 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002693 break;
2694 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002695 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696 break;
2697 default:
2698 return BAD_VALUE;
2699 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002700 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002701 return NO_ERROR;
2702}
2703
2704status_t GraphicPlane::setOrientation(int orientation)
2705{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002706 // If the rotation can be handled in hardware, this is where
2707 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002708
2709 const DisplayHardware& hw(displayHardware());
2710 const float w = mDisplayWidth;
2711 const float h = mDisplayHeight;
2712 mWidth = int(w);
2713 mHeight = int(h);
2714
2715 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002716 GraphicPlane::orientationToTransfrom(orientation, w, h,
2717 &orientationTransform);
2718 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2719 mWidth = int(h);
2720 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002721 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002722
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002723 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002724 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 return NO_ERROR;
2726}
2727
2728const DisplayHardware& GraphicPlane::displayHardware() const {
2729 return *mHw;
2730}
2731
Mathias Agopian59119e62010-10-11 12:37:43 -07002732DisplayHardware& GraphicPlane::editDisplayHardware() {
2733 return *mHw;
2734}
2735
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002736const Transform& GraphicPlane::transform() const {
2737 return mGlobalTransform;
2738}
2739
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002740EGLDisplay GraphicPlane::getEGLDisplay() const {
2741 return mHw->getEGLDisplay();
2742}
2743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002744// ---------------------------------------------------------------------------
2745
2746}; // namespace android