blob: b01a6a34d16bf235f4c15b5d2f9b48e7bc451171 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian0dd593f2011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian6950e422009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian93d75ec2011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070056#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Mathias Agopian7bb843c2011-04-20 14:20:59 -070058#include <private/surfaceflinger/SharedBufferStack.h>
59
Mathias Agopian627e7b52009-05-21 19:21:59 -070060/* ideally AID_GRAPHICS would be in a semi-public header
61 * or there would be a way to map a user/group name to its id
62 */
63#ifndef AID_GRAPHICS
64#define AID_GRAPHICS 1003
65#endif
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian0dd593f2011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Mathias Agopian9779b222009-09-07 16:32:45 -070082 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDeferReleaseConsole(false),
88 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070091 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 mDebugBackground(0),
Mathias Agopian93d75ec2011-08-15 20:44:40 -070094 mDebugDDMS(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070095 mDebugDisableHWC(0),
Mathias Agopian2143fe02011-08-23 18:03:18 -070096 mDebugDisableTransformHint(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070097 mDebugInSwapBuffers(0),
98 mLastSwapBufferTime(0),
99 mDebugInTransaction(0),
100 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -0700101 mBootFinished(false),
The Android Open Source Project9066cfe2009-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 Agopian93d75ec2011-08-15 20:44:40 -0700114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 property_get("debug.sf.showupdates", value, "0");
116 mDebugRegion = atoi(value);
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 property_get("debug.sf.showbackground", value, "0");
119 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Mathias Agopian93d75ec2011-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 Agopiane5c0a7b2010-04-20 14:51:04 -0700127 LOGI_IF(mDebugRegion, "showupdates enabled");
128 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700129 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130}
131
132SurfaceFlinger::~SurfaceFlinger()
133{
134 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135}
136
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700137sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700139 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140}
141
Mathias Agopian770492c2010-05-28 14:22:23 -0700142sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143{
Mathias Agopian593c05c2010-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 Project9066cfe2009-03-03 19:31:44 -0800149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 return bclient;
151}
152
Jamie Gennisf7acf162011-01-12 18:30:40 -0800153sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
154{
155 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
156 return gba;
157}
Mathias Agopian7623da42010-06-01 15:12:58 -0700158
The Android Open Source Project9066cfe2009-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 Hubere3c01832010-08-16 08:49:37 -0700176 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700177 mBootFinished = true;
Mathias Agopian0c63ef52011-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 Agopian627e7b52009-05-21 19:21:59 -0700187 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188}
189
Mathias Agopian0c63ef52011-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 Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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 Project9066cfe2009-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 Agopiand763b5d2009-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 Hubere3c01832010-08-16 08:49:37 -0700237
Mathias Agopiand763b5d2009-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 Hubere3c01832010-08-16 08:49:37 -0700240
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700241 new(mServerCblk) surface_flinger_cblk_t;
242
The Android Open Source Project9066cfe2009-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 Agopian66c77a52010-02-08 15:49:35 -0800258 dcblk->w = plane.getWidth();
259 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -0800266
267 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700269 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-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 Gennis830d0832011-10-07 14:51:16 -0700278 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Project9066cfe2009-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 Gennis830d0832011-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 Project9066cfe2009-03-03 19:31:44 -0800297
298 glViewport(0, 0, w, h);
299 glMatrixMode(GL_PROJECTION);
300 glLoadIdentity();
Mathias Agopian3a831d22011-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 Project9066cfe2009-03-03 19:31:44 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 mReadyToRunBarrier.open();
305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopian627e7b52009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700312
The Android Open Source Project9066cfe2009-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 Agopian6ead5d92009-04-20 19:39:12 -0700324 while (true) {
325 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800326 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700327 if (UNLIKELY(isFrozen())) {
328 // wait 5 seconds
Mathias Agopian6ead5d92009-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 Projectc39a6e02009-03-11 12:11:56 -0700335 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700336
Mathias Agopian898c4c92010-05-18 17:06:55 -0700337 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-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 Agopian6ead5d92009-04-20 19:39:12 -0700353 if (msg != 0) {
Mathias Agopian6ead5d92009-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 Project9066cfe2009-03-03 19:31:44 -0800359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
361}
362
363void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700364 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365}
366
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700367bool SurfaceFlinger::authenticateSurfaceTexture(
368 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800369 Mutex::Autolock _l(mStateLock);
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700370 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennisd2acedf2011-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 Gennis9b8fc652011-08-17 18:19:00 -0700378 if (lbc != NULL) {
379 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
380 if (lbcBinder == surfaceTextureBinder) {
381 return true;
382 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800383 }
384 }
385
386 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis9b8fc652011-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 Gennisd2acedf2011-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 Gennis9b8fc652011-08-17 18:19:00 -0700397 if (lbc != NULL) {
398 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
399 if (lbcBinder == surfaceTextureBinder) {
400 return true;
401 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800402 }
403 }
404
405 return false;
406}
407
Mathias Agopian898c4c92010-05-18 17:06:55 -0700408status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
409 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410{
Mathias Agopian898c4c92010-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 Project9066cfe2009-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 Agopian439863f2011-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 Project9066cfe2009-03-03 19:31:44 -0800444 }
445
446 // post surfaces (if needed)
447 handlePageFlip();
448
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700449 if (UNLIKELY(mHwWorkListDirty)) {
450 // build the h/w work list
451 handleWorkList();
452 }
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700455 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-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 Project9066cfe2009-03-03 19:31:44 -0800462 handleRepaint();
463
Mathias Agopianb1a18742009-09-17 16:18:16 -0700464 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700465 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700466 hw.compositionComplete();
467
Mathias Agopian04262e92010-09-13 22:57:58 -0700468 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700469 postFramebuffer();
470
Mathias Agopian04262e92010-09-13 22:57:58 -0700471 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 } else {
473 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800474 hw.compositionComplete();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 usleep(16667); // 60 fps period
476 }
477 return true;
478}
479
480void SurfaceFlinger::postFramebuffer()
481{
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700482 if (!mSwapRegion.isEmpty()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700484 const nsecs_t now = systemTime();
485 mDebugInSwapBuffers = now;
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700486 hw.flip(mSwapRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700487 mLastSwapBufferTime = systemTime() - now;
488 mDebugInSwapBuffers = 0;
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700489 mSwapRegion.clear();
The Android Open Source Project9066cfe2009-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 Agopian2d2b8032010-10-12 16:05:48 -0700501 // this is a temporary work-around, eventually this should be called
502 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700503 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
Mathias Agopianaab758e2010-10-11 12:37:43 -0700506 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700507 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 hw.releaseScreen();
510 }
511
512 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700513 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 hw.releaseScreen();
515 } else {
516 mDeferReleaseConsole = true;
517 }
518 }
519
520 mDirtyRegion.set(hw.bounds());
521}
522
523void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
524{
Mathias Agopian69608112011-05-19 15:38:14 -0700525 Mutex::Autolock _l(mStateLock);
526 const nsecs_t now = systemTime();
527 mDebugInTransaction = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
Mathias Agopian69608112011-05-19 15:38:14 -0700529 // Here we're guaranteed that some transaction flags are set
530 // so we can call handleTransactionLocked() unconditionally.
531 // We call getTransactionFlags(), which will also clear the flags,
532 // with mStateLock held to guarantee that mCurrentState won't change
533 // until the transaction is committed.
Mathias Agopianff1d4102010-08-10 17:19:56 -0700534
Mathias Agopian69608112011-05-19 15:38:14 -0700535 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
536 transactionFlags = getTransactionFlags(mask);
537 handleTransactionLocked(transactionFlags);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700538
Mathias Agopian69608112011-05-19 15:38:14 -0700539 mLastTransactionTime = systemTime() - now;
540 mDebugInTransaction = 0;
541 invalidateHwcGeometry();
542 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700543}
544
Mathias Agopian69608112011-05-19 15:38:14 -0700545void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700546{
547 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 const size_t count = currentLayers.size();
549
550 /*
551 * Traversal of the children
552 * (perform the transaction for each of them if needed)
553 */
554
555 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
556 if (layersNeedTransaction) {
557 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700558 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
560 if (!trFlags) continue;
561
562 const uint32_t flags = layer->doTransaction(0);
563 if (flags & Layer::eVisibleRegion)
564 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
567
568 /*
569 * Perform our own transaction if needed
570 */
571
572 if (transactionFlags & eTransactionNeeded) {
573 if (mCurrentState.orientation != mDrawingState.orientation) {
574 // the orientation has changed, recompute all visible regions
575 // and invalidate everything.
576
577 const int dpy = 0;
578 const int orientation = mCurrentState.orientation;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700579 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 GraphicPlane& plane(graphicPlane(dpy));
581 plane.setOrientation(orientation);
582
583 // update the shared control block
584 const DisplayHardware& hw(plane.displayHardware());
585 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
586 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800587 dcblk->w = plane.getWidth();
588 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589
590 mVisibleRegionsDirty = true;
591 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593
594 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
595 // freezing or unfreezing the display -> trigger animation if needed
596 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800597 if (mFreezeDisplay)
598 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700601 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
602 // layers have been added
603 mVisibleRegionsDirty = true;
604 }
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 // some layers might have been removed, so
607 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700608 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700609 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700611 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700612 const size_t count = previousLayers.size();
613 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700614 const sp<LayerBase>& layer(previousLayers[i]);
615 if (currentLayers.indexOf( layer ) < 0) {
616 // this layer is not visible anymore
Mathias Agopian33863dd2009-07-28 14:20:21 -0700617 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700618 }
619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
623 commitTransaction();
624}
625
626sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
627{
628 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
629}
630
631void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800632 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633{
634 const GraphicPlane& plane(graphicPlane(0));
635 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700636 const DisplayHardware& hw(plane.displayHardware());
637 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638
639 Region aboveOpaqueLayers;
640 Region aboveCoveredLayers;
641 Region dirty;
642
643 bool secureFrameBuffer = false;
644
645 size_t i = currentLayers.size();
646 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700647 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 layer->validateVisibility(planeTransform);
649
650 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700651 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700653 /*
654 * opaqueRegion: area of a surface that is fully opaque.
655 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700657
658 /*
659 * visibleRegion: area of a surface that is visible on screen
660 * and not fully transparent. This is essentially the layer's
661 * footprint minus the opaque regions above it.
662 * Areas covered by a translucent surface are considered visible.
663 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700665
666 /*
667 * coveredRegion: area of a surface that is covered by all
668 * visible regions above it (which includes the translucent areas).
669 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700671
672
673 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700674 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700675 const bool translucent = !layer->isOpaque();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700676 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700678 visibleRegion.andSelf(screenRegion);
679 if (!visibleRegion.isEmpty()) {
680 // Remove the transparent area from the visible region
681 if (translucent) {
682 visibleRegion.subtractSelf(layer->transparentRegionScreen);
683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700685 // compute the opaque region
686 const int32_t layerOrientation = layer->getOrientation();
687 if (s.alpha==255 && !translucent &&
688 ((layerOrientation & Transform::ROT_INVALID) == false)) {
689 // the opaque region is the layer's footprint
690 opaqueRegion = visibleRegion;
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 }
694
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700695 // Clip the covered region to the visible region
696 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
697
698 // Update aboveCoveredLayers for next (lower) layer
699 aboveCoveredLayers.orSelf(visibleRegion);
700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 // subtract the opaque region covered by the layers above us
702 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703
704 // compute this layer's dirty region
705 if (layer->contentDirty) {
706 // we need to invalidate the whole region
707 dirty = visibleRegion;
708 // as well, as the old visible region
709 dirty.orSelf(layer->visibleRegionScreen);
710 layer->contentDirty = false;
711 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700712 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700713 * the exposed region consists of two components:
714 * 1) what's VISIBLE now and was COVERED before
715 * 2) what's EXPOSED now less what was EXPOSED before
716 *
717 * note that (1) is conservative, we start with the whole
718 * visible region but only keep what used to be covered by
719 * something -- which mean it may have been exposed.
720 *
721 * (2) handles areas that were not covered by anything but got
722 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700723 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700724 const Region newExposed = visibleRegion - coveredRegion;
725 const Region oldVisibleRegion = layer->visibleRegionScreen;
726 const Region oldCoveredRegion = layer->coveredRegionScreen;
727 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
728 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730 dirty.subtractSelf(aboveOpaqueLayers);
731
732 // accumulate to the screen dirty region
733 dirtyRegion.orSelf(dirty);
734
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700735 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 // Store the visible region is screen space
739 layer->setVisibleRegion(visibleRegion);
740 layer->setCoveredRegion(coveredRegion);
741
Mathias Agopian12cedff2009-07-28 10:57:27 -0700742 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 if (layer->isSecure() && !visibleRegion.isEmpty()) {
744 secureFrameBuffer = true;
745 }
746 }
747
Mathias Agopian12cedff2009-07-28 10:57:27 -0700748 // invalidate the areas where a layer was removed
749 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
750 mDirtyRegionRemovedLayer.clear();
751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 mSecureFrameBuffer = secureFrameBuffer;
753 opaqueRegion = aboveOpaqueLayers;
754}
755
756
757void SurfaceFlinger::commitTransaction()
758{
759 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700760 mResizeTransationPending = false;
761 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762}
763
764void SurfaceFlinger::handlePageFlip()
765{
766 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800767 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 visibleRegions |= lockPageFlip(currentLayers);
769
770 const DisplayHardware& hw = graphicPlane(0).displayHardware();
771 const Region screenRegion(hw.bounds());
772 if (visibleRegions) {
773 Region opaqueRegion;
774 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700775
776 /*
777 * rebuild the visible layer list
778 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800779 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700780 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700781 mVisibleLayersSortedByZ.setCapacity(count);
782 for (size_t i=0 ; i<count ; i++) {
783 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
784 mVisibleLayersSortedByZ.add(currentLayers[i]);
785 }
786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 mWormholeRegion = screenRegion.subtract(opaqueRegion);
788 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800789 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791
792 unlockPageFlip(currentLayers);
793 mDirtyRegion.andSelf(screenRegion);
794}
795
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800796void SurfaceFlinger::invalidateHwcGeometry()
797{
798 mHwWorkListDirty = true;
799}
800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
802{
803 bool recomputeVisibleRegions = false;
804 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700805 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700807 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 layer->lockPageFlip(recomputeVisibleRegions);
809 }
810 return recomputeVisibleRegions;
811}
812
813void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
814{
815 const GraphicPlane& plane(graphicPlane(0));
816 const Transform& planeTransform(plane.transform());
817 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700818 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700820 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 layer->unlockPageFlip(planeTransform, mDirtyRegion);
822 }
823}
824
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700825void SurfaceFlinger::handleWorkList()
826{
827 mHwWorkListDirty = false;
828 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
829 if (hwc.initCheck() == NO_ERROR) {
830 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
831 const size_t count = currentLayers.size();
832 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700833 hwc_layer_t* const cur(hwc.getLayers());
834 for (size_t i=0 ; cur && i<count ; i++) {
835 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700836 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian6a969242010-09-22 18:58:01 -0700837 cur[i].compositionType = HWC_FRAMEBUFFER;
838 cur[i].flags |= HWC_SKIP_LAYER;
839 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700840 }
841 }
842}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844void SurfaceFlinger::handleRepaint()
845{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700846 // compute the invalid region
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700847 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848
849 if (UNLIKELY(mDebugRegion)) {
850 debugFlashRegions();
851 }
852
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700853 // set the frame buffer
854 const DisplayHardware& hw(graphicPlane(0).displayHardware());
855 glMatrixMode(GL_MODELVIEW);
856 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857
858 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700859 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
860 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700861 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700862 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
863 // takes a rectangle, we must make sure to update that whole
864 // rectangle in that case
865 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700866 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700867 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700868 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700870 // in the BUFFER_PRESERVED case, obviously, we can update only
871 // what's needed and nothing more.
872 // NOTE: this is NOT a common case, as preserving the backbuffer
873 // is costly and usually involves copying the whole update back.
874 }
875 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700876 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700877 // We need to redraw the rectangle that will be updated
878 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700879 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700880 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700881 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700882 } else {
883 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 mDirtyRegion.set(hw.bounds());
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -0700885 mSwapRegion = mDirtyRegion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887 }
888
Mathias Agopian88cde072011-09-20 17:21:56 -0700889 setupHardwareComposer(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 composeSurfaces(mDirtyRegion);
891
Mathias Agopian88cde072011-09-20 17:21:56 -0700892 // update the swap region and clear the dirty region
893 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 mDirtyRegion.clear();
895}
896
Mathias Agopian88cde072011-09-20 17:21:56 -0700897void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898{
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700899 const DisplayHardware& hw(graphicPlane(0).displayHardware());
900 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700901 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700902 if (!cur) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700903 return;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700904 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700905
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700906 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
907 size_t count = layers.size();
908
909 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700910 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
911 hwc.getNumLayers(), count);
912
913 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700914 if (hwc.initCheck() == NO_ERROR) {
915 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
916 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700917
918 /*
919 * update the per-frame h/w composer data for each layer
920 * and build the transparent region of the FB
921 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700922 for (size_t i=0 ; i<count ; i++) {
923 const sp<LayerBase>& layer(layers[i]);
924 layer->setPerFrameData(&cur[i]);
925 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700926 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700927 status_t err = hwc.prepare();
928 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700929
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700930 if (err == NO_ERROR) {
Mathias Agopian88cde072011-09-20 17:21:56 -0700931 // what's happening here is tricky.
932 // we want to clear all the layers with the CLEAR_FB flags
933 // that are opaque.
934 // however, since some GPU are efficient at preserving
935 // the backbuffer, we want to take advantage of that so we do the
936 // clear only in the dirty region (other areas will be preserved
937 // on those GPUs).
938 // NOTE: on non backbuffer preserving GPU, the dirty region
939 // has already been expanded as needed, so the code is correct
940 // there too.
941 //
942 // However, the content of the framebuffer cannot be trusted when
943 // we switch to/from FB/OVERLAY, in which case we need to
944 // expand the dirty region to those areas too.
945 //
946 // Note also that there is a special case when switching from
947 // "no layers in FB" to "some layers in FB", where we need to redraw
948 // the entire FB, since some areas might contain uninitialized
949 // data.
950 //
951 // Also we want to make sure to not clear areas that belong to
952 // layers above that won't redraw (we would just erasing them),
953 // that is, we can't erase anything outside the dirty region.
954
Mathias Agopian0a5abdb2011-09-09 01:47:48 -0700955 Region transparent;
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700956
Mathias Agopian88cde072011-09-20 17:21:56 -0700957 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
958 transparent.set(hw.getBounds());
959 dirtyInOut = transparent;
960 } else {
961 for (size_t i=0 ; i<count ; i++) {
962 const sp<LayerBase>& layer(layers[i]);
963 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
964 transparent.orSelf(layer->visibleRegionScreen);
965 }
966 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
967 if (isOverlay != layer->isOverlay()) {
968 // we transitioned to/from overlay, so add this layer
969 // to the dirty region so the framebuffer can be either
970 // cleared or redrawn.
971 dirtyInOut.orSelf(layer->visibleRegionScreen);
972 }
973 layer->setOverlay(isOverlay);
Mathias Agopian45491692011-01-19 15:24:23 -0800974 }
Mathias Agopian88cde072011-09-20 17:21:56 -0700975 // don't erase stuff outside the dirty region
976 transparent.andSelf(dirtyInOut);
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700977 }
978
979 /*
980 * clear the area of the FB that need to be transparent
981 */
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700982 if (!transparent.isEmpty()) {
983 glClearColor(0,0,0,0);
984 Region::const_iterator it = transparent.begin();
985 Region::const_iterator const end = transparent.end();
986 const int32_t height = hw.getHeight();
987 while (it != end) {
988 const Rect& r(*it++);
989 const GLint sy = height - (r.top + r.height());
990 glScissor(r.left, sy, r.width(), r.height());
991 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian45491692011-01-19 15:24:23 -0800992 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700993 }
994 }
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700995}
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700996
Mathias Agopian4bacc9d2011-09-08 18:31:55 -0700997void SurfaceFlinger::composeSurfaces(const Region& dirty)
998{
Mathias Agopian9fe96542011-09-22 20:57:04 -0700999 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1000 HWComposer& hwc(hw.getHwComposer());
1001
1002 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
1003 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -07001004 // should never happen unless the window manager has a bug
1005 // draw something...
1006 drawWormhole();
1007 }
1008
Mathias Agopianf8e705d2010-08-12 15:03:26 -07001009 /*
1010 * and then, render the layers targeted at the framebuffer
1011 */
Mathias Agopian9fe96542011-09-22 20:57:04 -07001012 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopian4bacc9d2011-09-08 18:31:55 -07001013 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1014 size_t count = layers.size();
Mathias Agopianf8e705d2010-08-12 15:03:26 -07001015 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian88cde072011-09-20 17:21:56 -07001016 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopian4bacc9d2011-09-08 18:31:55 -07001017 continue;
Mathias Agopianf8e705d2010-08-12 15:03:26 -07001018 }
1019 const sp<LayerBase>& layer(layers[i]);
1020 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1021 if (!clip.isEmpty()) {
1022 layer->draw(clip);
1023 }
1024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025}
1026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027void SurfaceFlinger::debugFlashRegions()
1028{
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001029 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1030 const uint32_t flags = hw.getFlags();
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001031 const int32_t height = hw.getHeight();
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001032 if (mSwapRegion.isEmpty()) {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001033 return;
1034 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -07001035
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001036 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1037 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1038 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1039 mDirtyRegion.bounds() : hw.bounds());
1040 composeSurfaces(repaint);
1041 }
1042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 glDisable(GL_BLEND);
1044 glDisable(GL_DITHER);
1045 glDisable(GL_SCISSOR_TEST);
1046
Mathias Agopiandff8e582009-05-04 14:17:04 -07001047 static int toggle = 0;
1048 toggle = 1 - toggle;
1049 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001050 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001051 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001052 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -07001053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001055 Region::const_iterator it = mDirtyRegion.begin();
1056 Region::const_iterator const end = mDirtyRegion.end();
1057 while (it != end) {
1058 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 GLfloat vertices[][2] = {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001060 { r.left, height - r.top },
1061 { r.left, height - r.bottom },
1062 { r.right, height - r.bottom },
1063 { r.right, height - r.top }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 };
1065 glVertexPointer(2, GL_FLOAT, 0, vertices);
1066 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1067 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001068
Mathias Agopiand0f2f0d2011-09-20 17:22:44 -07001069 hw.flip(mSwapRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070
1071 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001072 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073
1074 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075}
1076
1077void SurfaceFlinger::drawWormhole() const
1078{
1079 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1080 if (region.isEmpty())
1081 return;
1082
1083 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1084 const int32_t width = hw.getWidth();
1085 const int32_t height = hw.getHeight();
1086
1087 glDisable(GL_BLEND);
1088 glDisable(GL_DITHER);
1089
1090 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001091 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001092 Region::const_iterator it = region.begin();
1093 Region::const_iterator const end = region.end();
1094 while (it != end) {
1095 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 const GLint sy = height - (r.top + r.height());
1097 glScissor(r.left, sy, r.width(), r.height());
1098 glClear(GL_COLOR_BUFFER_BIT);
1099 }
1100 } else {
1101 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1102 { width, height }, { 0, height } };
1103 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1104 glVertexPointer(2, GL_SHORT, 0, vertices);
1105 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1106 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001107#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001108 if (GLExtensions::getInstance().haveTextureExternal()) {
1109 glDisable(GL_TEXTURE_EXTERNAL_OES);
1110 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001111#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 glEnable(GL_TEXTURE_2D);
1113 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1114 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1115 glMatrixMode(GL_TEXTURE);
1116 glLoadIdentity();
1117 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001118 Region::const_iterator it = region.begin();
1119 Region::const_iterator const end = region.end();
1120 while (it != end) {
1121 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 const GLint sy = height - (r.top + r.height());
1123 glScissor(r.left, sy, r.width(), r.height());
1124 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1125 }
1126 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001127 glDisable(GL_TEXTURE_2D);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001128 glLoadIdentity();
1129 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131}
1132
1133void SurfaceFlinger::debugShowFPS() const
1134{
1135 static int mFrameCount;
1136 static int mLastFrameCount = 0;
1137 static nsecs_t mLastFpsTime = 0;
1138 static float mFps = 0;
1139 mFrameCount++;
1140 nsecs_t now = systemTime();
1141 nsecs_t diff = now - mLastFpsTime;
1142 if (diff > ms2ns(250)) {
1143 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1144 mLastFpsTime = now;
1145 mLastFrameCount = mFrameCount;
1146 }
1147 // XXX: mFPS has the value we want
1148 }
1149
Mathias Agopian1473f462009-04-10 14:24:30 -07001150status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151{
1152 Mutex::Autolock _l(mStateLock);
1153 addLayer_l(layer);
1154 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1155 return NO_ERROR;
1156}
1157
Mathias Agopian593c05c2010-06-02 23:28:45 -07001158status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1159{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001160 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001161 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1162}
1163
1164ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1165 const sp<LayerBaseClient>& lbc)
1166{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001167 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001168 size_t name = client->attachLayer(lbc);
1169
1170 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001171
1172 // add this layer to the current state list
1173 addLayer_l(lbc);
1174
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001175 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001176}
1177
Mathias Agopian1473f462009-04-10 14:24:30 -07001178status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179{
1180 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001181 status_t err = purgatorizeLayer_l(layer);
1182 if (err == NO_ERROR)
1183 setTransactionFlags(eTransactionNeeded);
1184 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185}
1186
Mathias Agopian1473f462009-04-10 14:24:30 -07001187status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188{
Mathias Agopian7623da42010-06-01 15:12:58 -07001189 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1190 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001191 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1194 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001195 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 return NO_ERROR;
1197 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001198 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199}
1200
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001201status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1202{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001203 // First add the layer to the purgatory list, which makes sure it won't
1204 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001205 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001206 if (err >= 0) {
1207 mLayerPurgatory.add(layerBase);
1208 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001209
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001210 layerBase->onRemoved();
1211
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001212 // it's possible that we don't find a layer, because it might
1213 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001214 // from the user because there is a race between Client::destroySurface(),
1215 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001216 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1217}
1218
Mathias Agopian593c05c2010-06-02 23:28:45 -07001219status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001221 layer->forceVisibilityTransaction();
1222 setTransactionFlags(eTraversalNeeded);
1223 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224}
1225
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001226uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1227{
1228 return android_atomic_release_load(&mTransactionFlags);
1229}
1230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1232{
1233 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1234}
1235
Mathias Agopian898c4c92010-05-18 17:06:55 -07001236uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237{
1238 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1239 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001240 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 return old;
1243}
1244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245
Mathias Agopian439863f2011-06-28 19:09:31 -07001246void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state) {
1247 Mutex::Autolock _l(mStateLock);
Mathias Agopian9779b222009-09-07 16:32:45 -07001248
Mathias Agopian439863f2011-06-28 19:09:31 -07001249 uint32_t flags = 0;
1250 const size_t count = state.size();
1251 for (size_t i=0 ; i<count ; i++) {
1252 const ComposerState& s(state[i]);
1253 sp<Client> client( static_cast<Client *>(s.client.get()) );
1254 flags |= setClientStateLocked(client, s.state);
1255 }
1256 if (flags) {
1257 setTransactionFlags(flags);
1258 }
1259
1260 signalEvent();
1261
1262 // if there is a transaction with a resize, wait for it to
1263 // take effect before returning.
1264 while (mResizeTransationPending) {
1265 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1266 if (CC_UNLIKELY(err != NO_ERROR)) {
1267 // just in case something goes wrong in SF, return to the
1268 // called after a few seconds.
1269 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1270 mResizeTransationPending = false;
1271 break;
Mathias Agopian9779b222009-09-07 16:32:45 -07001272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274}
1275
1276status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1277{
1278 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1279 return BAD_VALUE;
1280
1281 Mutex::Autolock _l(mStateLock);
1282 mCurrentState.freezeDisplay = 1;
1283 setTransactionFlags(eTransactionNeeded);
1284
1285 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001286 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 return NO_ERROR;
1288}
1289
1290status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1291{
1292 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1293 return BAD_VALUE;
1294
1295 Mutex::Autolock _l(mStateLock);
1296 mCurrentState.freezeDisplay = 0;
1297 setTransactionFlags(eTransactionNeeded);
1298
1299 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001300 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 return NO_ERROR;
1302}
1303
Andreas Hubere3c01832010-08-16 08:49:37 -07001304int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001305 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306{
1307 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1308 return BAD_VALUE;
1309
1310 Mutex::Autolock _l(mStateLock);
1311 if (mCurrentState.orientation != orientation) {
1312 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown01a98dd2011-09-20 15:08:29 -07001313 mCurrentState.orientationFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 mCurrentState.orientation = orientation;
1315 setTransactionFlags(eTransactionNeeded);
1316 mTransactionCV.wait(mStateLock);
1317 } else {
1318 orientation = BAD_VALUE;
1319 }
1320 }
1321 return orientation;
1322}
1323
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001324sp<ISurface> SurfaceFlinger::createSurface(
1325 ISurfaceComposerClient::surface_data_t* params,
1326 const String8& name,
1327 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1329 uint32_t flags)
1330{
Mathias Agopian1473f462009-04-10 14:24:30 -07001331 sp<LayerBaseClient> layer;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001332 sp<ISurface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001333
1334 if (int32_t(w|h) < 0) {
1335 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1336 int(w), int(h));
1337 return surfaceHandle;
1338 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001341 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 switch (flags & eFXSurfaceMask) {
1343 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001344 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1345 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 break;
1347 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001348 // for now we treat Blur as Dim, until we can implement it
1349 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001351 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 break;
1353 }
1354
Mathias Agopian1473f462009-04-10 14:24:30 -07001355 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001356 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001357 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001358 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001361 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001362 params->token = token;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001363 params->identity = layer->getIdentity();
Mathias Agopian7623da42010-06-01 15:12:58 -07001364 if (normalLayer != 0) {
1365 Mutex::Autolock _l(mStateLock);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001366 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001367 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001368 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001369
Mathias Agopian593c05c2010-06-02 23:28:45 -07001370 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372
1373 return surfaceHandle;
1374}
1375
Mathias Agopian7623da42010-06-01 15:12:58 -07001376sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001377 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001378 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001379 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380{
1381 // initialize the surfaces
1382 switch (format) { // TODO: take h/w into account
1383 case PIXEL_FORMAT_TRANSPARENT:
1384 case PIXEL_FORMAT_TRANSLUCENT:
1385 format = PIXEL_FORMAT_RGBA_8888;
1386 break;
1387 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001388#ifdef NO_RGBX_8888
1389 format = PIXEL_FORMAT_RGB_565;
1390#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001391 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001392#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 break;
1394 }
1395
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001396#ifdef NO_RGBX_8888
1397 if (format == PIXEL_FORMAT_RGBX_8888)
1398 format = PIXEL_FORMAT_RGBA_8888;
1399#endif
1400
Mathias Agopian593c05c2010-06-02 23:28:45 -07001401 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001402 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001403 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001405 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 return layer;
1408}
1409
Mathias Agopian7623da42010-06-01 15:12:58 -07001410sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001411 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001412 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001414 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 return layer;
1417}
1418
Mathias Agopian593c05c2010-06-02 23:28:45 -07001419status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001421 /*
1422 * called by the window manager, when a surface should be marked for
1423 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001424 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001425 * The surface is removed from the current and drawing lists, but placed
1426 * in the purgatory queue, so it's not destroyed right-away (we need
1427 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001428 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001429
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001430 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001431 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001432 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001433 if (layer != 0) {
1434 err = purgatorizeLayer_l(layer);
1435 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001436 setTransactionFlags(eTransactionNeeded);
1437 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001438 }
1439 return err;
1440}
1441
Mathias Agopian69608112011-05-19 15:38:14 -07001442status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001443{
Mathias Agopian359140c2009-07-02 17:33:40 -07001444 // called by ~ISurface() when all references are gone
Mathias Agopian69608112011-05-19 15:38:14 -07001445 status_t err = NO_ERROR;
1446 sp<LayerBaseClient> l(layer.promote());
1447 if (l != NULL) {
1448 Mutex::Autolock _l(mStateLock);
1449 err = removeLayer_l(l);
1450 if (err == NAME_NOT_FOUND) {
1451 // The surface wasn't in the current list, which means it was
1452 // removed already, which means it is in the purgatory,
1453 // and need to be removed from there.
1454 ssize_t idx = mLayerPurgatory.remove(l);
1455 LOGE_IF(idx < 0,
1456 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001457 }
Mathias Agopian69608112011-05-19 15:38:14 -07001458 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1459 "error removing layer=%p (%s)", l.get(), strerror(-err));
1460 }
1461 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462}
1463
Mathias Agopian439863f2011-06-28 19:09:31 -07001464uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001465 const sp<Client>& client,
Mathias Agopian439863f2011-06-28 19:09:31 -07001466 const layer_state_t& s)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 uint32_t flags = 0;
Mathias Agopian439863f2011-06-28 19:09:31 -07001469 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1470 if (layer != 0) {
1471 const uint32_t what = s.what;
1472 if (what & ePositionChanged) {
1473 if (layer->setPosition(s.x, s.y))
1474 flags |= eTraversalNeeded;
1475 }
1476 if (what & eLayerChanged) {
1477 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1478 if (layer->setLayer(s.z)) {
1479 mCurrentState.layersSortedByZ.removeAt(idx);
1480 mCurrentState.layersSortedByZ.add(layer);
1481 // we need traversal (state changed)
1482 // AND transaction (list changed)
1483 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001486 if (what & eSizeChanged) {
1487 if (layer->setSize(s.w, s.h)) {
1488 flags |= eTraversalNeeded;
1489 mResizeTransationPending = true;
1490 }
1491 }
1492 if (what & eAlphaChanged) {
1493 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1494 flags |= eTraversalNeeded;
1495 }
1496 if (what & eMatrixChanged) {
1497 if (layer->setMatrix(s.matrix))
1498 flags |= eTraversalNeeded;
1499 }
1500 if (what & eTransparentRegionChanged) {
1501 if (layer->setTransparentRegionHint(s.transparentRegion))
1502 flags |= eTraversalNeeded;
1503 }
1504 if (what & eVisibilityChanged) {
1505 if (layer->setFlags(s.flags, s.mask))
1506 flags |= eTraversalNeeded;
1507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001509 return flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510}
1511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512void SurfaceFlinger::screenReleased(int dpy)
1513{
1514 // this may be called by a signal handler, we can't do too much in here
1515 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1516 signalEvent();
1517}
1518
1519void SurfaceFlinger::screenAcquired(int dpy)
1520{
1521 // this may be called by a signal handler, we can't do too much in here
1522 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1523 signalEvent();
1524}
1525
1526status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1527{
Erik Gilling94720d72010-12-01 16:38:01 -08001528 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 char buffer[SIZE];
1530 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001531
1532 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 snprintf(buffer, SIZE, "Permission Denial: "
1534 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1535 IPCThreadState::self()->getCallingPid(),
1536 IPCThreadState::self()->getCallingUid());
1537 result.append(buffer);
1538 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001539
1540 // figure out if we're stuck somewhere
1541 const nsecs_t now = systemTime();
1542 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1543 const nsecs_t inTransaction(mDebugInTransaction);
1544 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1545 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1546
1547 // Try to get the main lock, but don't insist if we can't
1548 // (this would indicate SF is stuck, but we want to be able to
1549 // print something in dumpsys).
1550 int retry = 3;
1551 while (mStateLock.tryLock()<0 && --retry>=0) {
1552 usleep(1000000);
1553 }
1554 const bool locked(retry >= 0);
1555 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001556 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001557 "SurfaceFlinger appears to be unresponsive, "
1558 "dumping anyways (no locks held)\n");
1559 result.append(buffer);
1560 }
1561
Mathias Agopian06a61e22011-01-19 16:15:53 -08001562 /*
1563 * Dump the visible layer list
1564 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1566 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001567 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1568 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001570 const sp<LayerBase>& layer(currentLayers[i]);
1571 layer->dump(result, buffer, SIZE);
1572 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 s.transparentRegion.dump(result, "transparentRegion");
1574 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1575 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1576 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001577
Mathias Agopian06a61e22011-01-19 16:15:53 -08001578 /*
1579 * Dump the layers in the purgatory
1580 */
1581
1582 const size_t purgatorySize = mLayerPurgatory.size();
1583 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1584 result.append(buffer);
1585 for (size_t i=0 ; i<purgatorySize ; i++) {
1586 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1587 layer->shortDump(result, buffer, SIZE);
1588 }
1589
1590 /*
1591 * Dump SurfaceFlinger global state
1592 */
1593
Mathias Agopianab951172011-07-14 18:01:49 -07001594 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian06a61e22011-01-19 16:15:53 -08001595 result.append(buffer);
Mathias Agopianab951172011-07-14 18:01:49 -07001596
1597 const GLExtensions& extensions(GLExtensions::getInstance());
1598 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1599 extensions.getVendor(),
1600 extensions.getRenderer(),
1601 extensions.getVersion());
1602 result.append(buffer);
1603 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1604 result.append(buffer);
1605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 mWormholeRegion.dump(result, "WormholeRegion");
1607 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1608 snprintf(buffer, SIZE,
1609 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1610 mFreezeDisplay?"yes":"no", mFreezeCount,
1611 mCurrentState.orientation, hw.canDraw());
1612 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001613 snprintf(buffer, SIZE,
1614 " last eglSwapBuffers() time: %f us\n"
1615 " last transaction time : %f us\n",
1616 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1617 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001618
Mathias Agopiana8d49172009-08-26 16:36:26 -07001619 if (inSwapBuffersDuration || !locked) {
1620 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1621 inSwapBuffersDuration/1000.0);
1622 result.append(buffer);
1623 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001624
Mathias Agopiana8d49172009-08-26 16:36:26 -07001625 if (inTransactionDuration || !locked) {
1626 snprintf(buffer, SIZE, " transaction time: %f us\n",
1627 inTransactionDuration/1000.0);
1628 result.append(buffer);
1629 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001630
Mathias Agopian06a61e22011-01-19 16:15:53 -08001631 /*
1632 * Dump HWComposer state
1633 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001634 HWComposer& hwc(hw.getHwComposer());
1635 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1636 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001637 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian6a969242010-09-22 18:58:01 -07001638 result.append(buffer);
Mathias Agopian5bd1b272011-09-09 00:49:11 -07001639 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian6a969242010-09-22 18:58:01 -07001640
Mathias Agopian06a61e22011-01-19 16:15:53 -08001641 /*
1642 * Dump gralloc state
1643 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001644 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001645 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001646 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001647
1648 if (locked) {
1649 mStateLock.unlock();
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652 write(fd, result.string(), result.size());
1653 return NO_ERROR;
1654}
1655
1656status_t SurfaceFlinger::onTransact(
1657 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1658{
1659 switch (code) {
1660 case CREATE_CONNECTION:
Mathias Agopian439863f2011-06-28 19:09:31 -07001661 case SET_TRANSACTION_STATE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 case SET_ORIENTATION:
1663 case FREEZE_DISPLAY:
1664 case UNFREEZE_DISPLAY:
1665 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001666 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001667 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 {
1669 // codes that require permission check
1670 IPCThreadState* ipc = IPCThreadState::self();
1671 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001672 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001673 if ((uid != AID_GRAPHICS) &&
1674 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001675 LOGE("Permission Denial: "
1676 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1677 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001679 break;
1680 }
1681 case CAPTURE_SCREEN:
1682 {
1683 // codes that require permission check
1684 IPCThreadState* ipc = IPCThreadState::self();
1685 const int pid = ipc->getCallingPid();
1686 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001687 if ((uid != AID_GRAPHICS) &&
1688 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001689 LOGE("Permission Denial: "
1690 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1691 return PERMISSION_DENIED;
1692 }
1693 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
1695 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1698 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001699 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001700 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001701 IPCThreadState* ipc = IPCThreadState::self();
1702 const int pid = ipc->getCallingPid();
1703 const int uid = ipc->getCallingUid();
1704 LOGE("Permission Denial: "
1705 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 return PERMISSION_DENIED;
1707 }
1708 int n;
1709 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001710 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001711 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 return NO_ERROR;
1713 case 1002: // SHOW_UPDATES
1714 n = data.readInt32();
1715 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001716 invalidateHwcGeometry();
1717 repaintEverything();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 return NO_ERROR;
1719 case 1003: // SHOW_BACKGROUND
1720 n = data.readInt32();
1721 mDebugBackground = n ? 1 : 0;
1722 return NO_ERROR;
1723 case 1004:{ // repaint everything
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001724 repaintEverything();
Mathias Agopian9779b222009-09-07 16:32:45 -07001725 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001727 case 1005:{ // force transaction
1728 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1729 return NO_ERROR;
1730 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001731 case 1006:{ // enable/disable GraphicLog
1732 int enabled = data.readInt32();
1733 GraphicLog::getInstance().setEnabled(enabled);
1734 return NO_ERROR;
1735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 case 1007: // set mFreezeCount
1737 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001738 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 return NO_ERROR;
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001740 case 1008: // toggle use of hw composer
1741 n = data.readInt32();
1742 mDebugDisableHWC = n ? 1 : 0;
1743 invalidateHwcGeometry();
1744 repaintEverything();
1745 return NO_ERROR;
Mathias Agopian2143fe02011-08-23 18:03:18 -07001746 case 1009: // toggle use of transform hint
1747 n = data.readInt32();
1748 mDebugDisableTransformHint = n ? 1 : 0;
1749 invalidateHwcGeometry();
1750 repaintEverything();
1751 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001753 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 reply->writeInt32(0);
1755 reply->writeInt32(mDebugRegion);
1756 reply->writeInt32(mDebugBackground);
1757 return NO_ERROR;
1758 case 1013: {
1759 Mutex::Autolock _l(mStateLock);
1760 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1761 reply->writeInt32(hw.getPageFlipCount());
1762 }
1763 return NO_ERROR;
1764 }
1765 }
1766 return err;
1767}
1768
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001769void SurfaceFlinger::repaintEverything() {
1770 Mutex::Autolock _l(mStateLock);
1771 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1772 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1773 signalEvent();
1774}
1775
Mathias Agopianaab758e2010-10-11 12:37:43 -07001776// ---------------------------------------------------------------------------
1777
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001778status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1779 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001780{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001781 if (!GLExtensions::getInstance().haveFramebufferObject())
1782 return INVALID_OPERATION;
1783
1784 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001785 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001786 const uint32_t hw_w = hw.getWidth();
1787 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001788 GLfloat u = 1;
1789 GLfloat v = 1;
1790
1791 // make sure to clear all GL error flags
1792 while ( glGetError() != GL_NO_ERROR ) ;
1793
1794 // create a FBO
1795 GLuint name, tname;
1796 glGenTextures(1, &tname);
1797 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001798 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1799 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001800 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001801 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001802 GLint tw = (2 << (31 - clz(hw_w)));
1803 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001804 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1805 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001806 u = GLfloat(hw_w) / tw;
1807 v = GLfloat(hw_h) / th;
1808 }
1809 glGenFramebuffersOES(1, &name);
1810 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001811 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1812 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001813
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001814 // redraw the screen entirely...
1815 glClearColor(0,0,0,1);
1816 glClear(GL_COLOR_BUFFER_BIT);
1817 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1818 const size_t count = layers.size();
1819 for (size_t i=0 ; i<count ; ++i) {
1820 const sp<LayerBase>& layer(layers[i]);
1821 layer->drawForSreenShot();
1822 }
1823
1824 // back to main framebuffer
1825 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1826 glDisable(GL_SCISSOR_TEST);
1827 glDeleteFramebuffersOES(1, &name);
1828
1829 *textureName = tname;
1830 *uOut = u;
1831 *vOut = v;
1832 return NO_ERROR;
1833}
1834
1835// ---------------------------------------------------------------------------
1836
1837status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1838{
1839 status_t result = PERMISSION_DENIED;
1840
1841 if (!GLExtensions::getInstance().haveFramebufferObject())
1842 return INVALID_OPERATION;
1843
1844 // get screen geometry
1845 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1846 const uint32_t hw_w = hw.getWidth();
1847 const uint32_t hw_h = hw.getHeight();
1848 const Region screenBounds(hw.bounds());
1849
1850 GLfloat u, v;
1851 GLuint tname;
1852 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1853 if (result != NO_ERROR) {
1854 return result;
1855 }
1856
1857 GLfloat vtx[8];
Mathias Agopian3a831d22011-07-07 17:30:31 -07001858 const GLfloat texCoords[4][2] = { {0,1}, {0,1-v}, {u,1-v}, {u,1} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001859 glBindTexture(GL_TEXTURE_2D, tname);
1860 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1861 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1862 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1863 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1864 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1865 glVertexPointer(2, GL_FLOAT, 0, vtx);
1866
Mathias Agopian3a831d22011-07-07 17:30:31 -07001867 /*
1868 * Texture coordinate mapping
1869 *
1870 * u
1871 * 1 +----------+---+
1872 * | | | | image is inverted
1873 * | V | | w.r.t. the texture
1874 * 1-v +----------+ | coordinates
1875 * | |
1876 * | |
1877 * | |
1878 * 0 +--------------+
1879 * 0 1
1880 *
1881 */
1882
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001883 class s_curve_interpolator {
1884 const float nbFrames, s, v;
1885 public:
1886 s_curve_interpolator(int nbFrames, float s)
1887 : nbFrames(1.0f / (nbFrames-1)), s(s),
1888 v(1.0f + expf(-s + 0.5f*s)) {
1889 }
1890 float operator()(int f) {
1891 const float x = f * nbFrames;
1892 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1893 }
1894 };
1895
1896 class v_stretch {
1897 const GLfloat hw_w, hw_h;
1898 public:
1899 v_stretch(uint32_t hw_w, uint32_t hw_h)
1900 : hw_w(hw_w), hw_h(hw_h) {
1901 }
1902 void operator()(GLfloat* vtx, float v) {
1903 const GLfloat w = hw_w + (hw_w * v);
1904 const GLfloat h = hw_h - (hw_h * v);
1905 const GLfloat x = (hw_w - w) * 0.5f;
1906 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001907 vtx[0] = x; vtx[1] = y;
1908 vtx[2] = x; vtx[3] = y + h;
1909 vtx[4] = x + w; vtx[5] = y + h;
1910 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001911 }
1912 };
1913
1914 class h_stretch {
1915 const GLfloat hw_w, hw_h;
1916 public:
1917 h_stretch(uint32_t hw_w, uint32_t hw_h)
1918 : hw_w(hw_w), hw_h(hw_h) {
1919 }
1920 void operator()(GLfloat* vtx, float v) {
1921 const GLfloat w = hw_w - (hw_w * v);
1922 const GLfloat h = 1.0f;
1923 const GLfloat x = (hw_w - w) * 0.5f;
1924 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001925 vtx[0] = x; vtx[1] = y;
1926 vtx[2] = x; vtx[3] = y + h;
1927 vtx[4] = x + w; vtx[5] = y + h;
1928 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001929 }
1930 };
1931
1932 // the full animation is 24 frames
Mathias Agopian3a831d22011-07-07 17:30:31 -07001933 char value[PROPERTY_VALUE_MAX];
1934 property_get("debug.sf.electron_frames", value, "24");
1935 int nbFrames = (atoi(value) + 1) >> 1;
1936 if (nbFrames <= 0) // just in case
1937 nbFrames = 24;
1938
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001939 s_curve_interpolator itr(nbFrames, 7.5f);
1940 s_curve_interpolator itg(nbFrames, 8.0f);
1941 s_curve_interpolator itb(nbFrames, 8.5f);
1942
1943 v_stretch vverts(hw_w, hw_h);
1944 glEnable(GL_BLEND);
1945 glBlendFunc(GL_ONE, GL_ONE);
1946 for (int i=0 ; i<nbFrames ; i++) {
1947 float x, y, w, h;
1948 const float vr = itr(i);
1949 const float vg = itg(i);
1950 const float vb = itb(i);
1951
1952 // clear screen
1953 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001954 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001955 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001956
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001957 // draw the red plane
1958 vverts(vtx, vr);
1959 glColorMask(1,0,0,1);
1960 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001961
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001962 // draw the green plane
1963 vverts(vtx, vg);
1964 glColorMask(0,1,0,1);
1965 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001966
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001967 // draw the blue plane
1968 vverts(vtx, vb);
1969 glColorMask(0,0,1,1);
1970 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001971
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001972 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001973 glDisable(GL_TEXTURE_2D);
1974 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001975 glColor4f(vg, vg, vg, 1);
1976 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1977 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001978 }
1979
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001980 h_stretch hverts(hw_w, hw_h);
1981 glDisable(GL_BLEND);
1982 glDisable(GL_TEXTURE_2D);
1983 glColorMask(1,1,1,1);
1984 for (int i=0 ; i<nbFrames ; i++) {
1985 const float v = itg(i);
1986 hverts(vtx, v);
1987 glClear(GL_COLOR_BUFFER_BIT);
1988 glColor4f(1-v, 1-v, 1-v, 1);
1989 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1990 hw.flip(screenBounds);
1991 }
1992
1993 glColorMask(1,1,1,1);
1994 glEnable(GL_SCISSOR_TEST);
1995 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1996 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001997 glDisable(GL_TEXTURE_2D);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001998 return NO_ERROR;
1999}
2000
2001status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2002{
2003 status_t result = PERMISSION_DENIED;
2004
2005 if (!GLExtensions::getInstance().haveFramebufferObject())
2006 return INVALID_OPERATION;
2007
2008
2009 // get screen geometry
2010 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2011 const uint32_t hw_w = hw.getWidth();
2012 const uint32_t hw_h = hw.getHeight();
2013 const Region screenBounds(hw.bounds());
2014
2015 GLfloat u, v;
2016 GLuint tname;
2017 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2018 if (result != NO_ERROR) {
2019 return result;
2020 }
2021
2022 // back to main framebuffer
2023 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2024 glDisable(GL_SCISSOR_TEST);
2025
2026 GLfloat vtx[8];
2027 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002028 glBindTexture(GL_TEXTURE_2D, tname);
2029 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2030 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2031 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2032 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2033 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2034 glVertexPointer(2, GL_FLOAT, 0, vtx);
2035
2036 class s_curve_interpolator {
2037 const float nbFrames, s, v;
2038 public:
2039 s_curve_interpolator(int nbFrames, float s)
2040 : nbFrames(1.0f / (nbFrames-1)), s(s),
2041 v(1.0f + expf(-s + 0.5f*s)) {
2042 }
2043 float operator()(int f) {
2044 const float x = f * nbFrames;
2045 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2046 }
2047 };
2048
2049 class v_stretch {
2050 const GLfloat hw_w, hw_h;
2051 public:
2052 v_stretch(uint32_t hw_w, uint32_t hw_h)
2053 : hw_w(hw_w), hw_h(hw_h) {
2054 }
2055 void operator()(GLfloat* vtx, float v) {
2056 const GLfloat w = hw_w + (hw_w * v);
2057 const GLfloat h = hw_h - (hw_h * v);
2058 const GLfloat x = (hw_w - w) * 0.5f;
2059 const GLfloat y = (hw_h - h) * 0.5f;
2060 vtx[0] = x; vtx[1] = y;
2061 vtx[2] = x; vtx[3] = y + h;
2062 vtx[4] = x + w; vtx[5] = y + h;
2063 vtx[6] = x + w; vtx[7] = y;
2064 }
2065 };
2066
2067 class h_stretch {
2068 const GLfloat hw_w, hw_h;
2069 public:
2070 h_stretch(uint32_t hw_w, uint32_t hw_h)
2071 : hw_w(hw_w), hw_h(hw_h) {
2072 }
2073 void operator()(GLfloat* vtx, float v) {
2074 const GLfloat w = hw_w - (hw_w * v);
2075 const GLfloat h = 1.0f;
2076 const GLfloat x = (hw_w - w) * 0.5f;
2077 const GLfloat y = (hw_h - h) * 0.5f;
2078 vtx[0] = x; vtx[1] = y;
2079 vtx[2] = x; vtx[3] = y + h;
2080 vtx[4] = x + w; vtx[5] = y + h;
2081 vtx[6] = x + w; vtx[7] = y;
2082 }
2083 };
2084
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002085 // the full animation is 12 frames
2086 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002087 s_curve_interpolator itr(nbFrames, 7.5f);
2088 s_curve_interpolator itg(nbFrames, 8.0f);
2089 s_curve_interpolator itb(nbFrames, 8.5f);
2090
2091 h_stretch hverts(hw_w, hw_h);
2092 glDisable(GL_BLEND);
2093 glDisable(GL_TEXTURE_2D);
2094 glColorMask(1,1,1,1);
2095 for (int i=nbFrames-1 ; i>=0 ; i--) {
2096 const float v = itg(i);
2097 hverts(vtx, v);
2098 glClear(GL_COLOR_BUFFER_BIT);
2099 glColor4f(1-v, 1-v, 1-v, 1);
2100 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2101 hw.flip(screenBounds);
2102 }
2103
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002104 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002105 v_stretch vverts(hw_w, hw_h);
2106 glEnable(GL_BLEND);
2107 glBlendFunc(GL_ONE, GL_ONE);
2108 for (int i=nbFrames-1 ; i>=0 ; i--) {
2109 float x, y, w, h;
2110 const float vr = itr(i);
2111 const float vg = itg(i);
2112 const float vb = itb(i);
2113
2114 // clear screen
2115 glColorMask(1,1,1,1);
2116 glClear(GL_COLOR_BUFFER_BIT);
2117 glEnable(GL_TEXTURE_2D);
2118
2119 // draw the red plane
2120 vverts(vtx, vr);
2121 glColorMask(1,0,0,1);
2122 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2123
2124 // draw the green plane
2125 vverts(vtx, vg);
2126 glColorMask(0,1,0,1);
2127 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2128
2129 // draw the blue plane
2130 vverts(vtx, vb);
2131 glColorMask(0,0,1,1);
2132 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2133
2134 hw.flip(screenBounds);
2135 }
2136
2137 glColorMask(1,1,1,1);
2138 glEnable(GL_SCISSOR_TEST);
2139 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002140 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002141 glDisable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002142
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002143 return NO_ERROR;
2144}
2145
2146// ---------------------------------------------------------------------------
2147
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002148status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002149{
2150 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2151 if (!hw.canDraw()) {
2152 // we're already off
2153 return NO_ERROR;
2154 }
Mathias Agopian7f972582011-09-02 12:22:39 -07002155
2156 // turn off hwc while we're doing the animation
2157 hw.getHwComposer().disable();
2158 // and make sure to turn it back on (if needed) next time we compose
2159 invalidateHwcGeometry();
2160
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002161 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2162 electronBeamOffAnimationImplLocked();
2163 }
2164
2165 // always clear the whole screen at the end of the animation
2166 glClearColor(0,0,0,1);
2167 glDisable(GL_SCISSOR_TEST);
2168 glClear(GL_COLOR_BUFFER_BIT);
2169 glEnable(GL_SCISSOR_TEST);
2170 hw.flip( Region(hw.bounds()) );
2171
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002172 hw.setCanDraw(false);
2173 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002174}
2175
2176status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2177{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002178 class MessageTurnElectronBeamOff : public MessageBase {
2179 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002180 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002181 status_t result;
2182 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002183 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2184 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002185 }
2186 status_t getResult() const {
2187 return result;
2188 }
2189 virtual bool handler() {
2190 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002191 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002192 return true;
2193 }
2194 };
2195
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002196 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002197 status_t res = postMessageSync(msg);
2198 if (res == NO_ERROR) {
2199 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002200
2201 // work-around: when the power-manager calls us we activate the
2202 // animation. eventually, the "on" animation will be called
2203 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002204 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002205 }
2206 return res;
2207}
2208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002210
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002211status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002212{
2213 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2214 if (hw.canDraw()) {
2215 // we're already on
2216 return NO_ERROR;
2217 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002218 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2219 electronBeamOnAnimationImplLocked();
2220 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002221 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002222
2223 // make sure to redraw the whole screen when the animation is done
2224 mDirtyRegion.set(hw.bounds());
2225 signalEvent();
2226
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002227 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002228}
2229
2230status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2231{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002232 class MessageTurnElectronBeamOn : public MessageBase {
2233 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002234 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002235 status_t result;
2236 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002237 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2238 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002239 }
2240 status_t getResult() const {
2241 return result;
2242 }
2243 virtual bool handler() {
2244 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002245 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002246 return true;
2247 }
2248 };
2249
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002250 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002251 return NO_ERROR;
2252}
2253
2254// ---------------------------------------------------------------------------
2255
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002256status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2257 sp<IMemoryHeap>* heap,
2258 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002259 uint32_t sw, uint32_t sh,
2260 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002261{
2262 status_t result = PERMISSION_DENIED;
2263
2264 // only one display supported for now
2265 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2266 return BAD_VALUE;
2267
2268 if (!GLExtensions::getInstance().haveFramebufferObject())
2269 return INVALID_OPERATION;
2270
2271 // get screen geometry
2272 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2273 const uint32_t hw_w = hw.getWidth();
2274 const uint32_t hw_h = hw.getHeight();
2275
2276 if ((sw > hw_w) || (sh > hw_h))
2277 return BAD_VALUE;
2278
2279 sw = (!sw) ? hw_w : sw;
2280 sh = (!sh) ? hw_h : sh;
2281 const size_t size = sw * sh * 4;
2282
Mathias Agopian32ae0942011-03-02 18:45:50 -08002283 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2284 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002285
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002286 // make sure to clear all GL error flags
2287 while ( glGetError() != GL_NO_ERROR ) ;
2288
2289 // create a FBO
2290 GLuint name, tname;
2291 glGenRenderbuffersOES(1, &tname);
2292 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2293 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2294 glGenFramebuffersOES(1, &name);
2295 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2296 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2297 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2298
2299 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002300
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002301 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2302
2303 // invert everything, b/c glReadPixel() below will invert the FB
2304 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002305 glScissor(0, 0, sw, sh);
Mathias Agopian3a831d22011-07-07 17:30:31 -07002306 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002307 glMatrixMode(GL_PROJECTION);
2308 glPushMatrix();
2309 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -07002310 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002311 glMatrixMode(GL_MODELVIEW);
2312
2313 // redraw the screen entirely...
2314 glClearColor(0,0,0,1);
2315 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002316
Jamie Gennis830d0832011-10-07 14:51:16 -07002317 const LayerVector& layers(mDrawingState.layersSortedByZ);
2318 const size_t count = layers.size();
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002319 for (size_t i=0 ; i<count ; ++i) {
2320 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianec49d892011-08-25 14:36:43 -07002321 const uint32_t flags = layer->drawingState().flags;
2322 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2323 const uint32_t z = layer->drawingState().z;
2324 if (z >= minLayerZ && z <= maxLayerZ) {
2325 layer->drawForSreenShot();
2326 }
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002327 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002328 }
2329
2330 // XXX: this is needed on tegra
Mathias Agopian3a831d22011-07-07 17:30:31 -07002331 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002332 glScissor(0, 0, sw, sh);
2333
2334 // check for errors and return screen capture
2335 if (glGetError() != GL_NO_ERROR) {
2336 // error while rendering
2337 result = INVALID_OPERATION;
2338 } else {
2339 // allocate shared memory large enough to hold the
2340 // screen capture
2341 sp<MemoryHeapBase> base(
2342 new MemoryHeapBase(size, 0, "screen-capture") );
2343 void* const ptr = base->getBase();
2344 if (ptr) {
2345 // capture the screen with glReadPixels()
2346 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2347 if (glGetError() == GL_NO_ERROR) {
2348 *heap = base;
2349 *w = sw;
2350 *h = sh;
2351 *f = PIXEL_FORMAT_RGBA_8888;
2352 result = NO_ERROR;
2353 }
2354 } else {
2355 result = NO_MEMORY;
2356 }
2357 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002358 glEnable(GL_SCISSOR_TEST);
2359 glViewport(0, 0, hw_w, hw_h);
2360 glMatrixMode(GL_PROJECTION);
2361 glPopMatrix();
2362 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002363 } else {
2364 result = BAD_VALUE;
2365 }
2366
2367 // release FBO resources
2368 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2369 glDeleteRenderbuffersOES(1, &tname);
2370 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002371
2372 hw.compositionComplete();
2373
Mathias Agopian32ae0942011-03-02 18:45:50 -08002374 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002375
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002376 return result;
2377}
2378
2379
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002380status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2381 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002382 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002383 uint32_t sw, uint32_t sh,
2384 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002385{
2386 // only one display supported for now
2387 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2388 return BAD_VALUE;
2389
2390 if (!GLExtensions::getInstance().haveFramebufferObject())
2391 return INVALID_OPERATION;
2392
2393 class MessageCaptureScreen : public MessageBase {
2394 SurfaceFlinger* flinger;
2395 DisplayID dpy;
2396 sp<IMemoryHeap>* heap;
2397 uint32_t* w;
2398 uint32_t* h;
2399 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002400 uint32_t sw;
2401 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002402 uint32_t minLayerZ;
2403 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002404 status_t result;
2405 public:
2406 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002407 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002408 uint32_t sw, uint32_t sh,
2409 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002410 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002411 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2412 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2413 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002414 {
2415 }
2416 status_t getResult() const {
2417 return result;
2418 }
2419 virtual bool handler() {
2420 Mutex::Autolock _l(flinger->mStateLock);
2421
2422 // if we have secure windows, never allow the screen capture
2423 if (flinger->mSecureFrameBuffer)
2424 return true;
2425
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002426 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002427 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002428
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002429 return true;
2430 }
2431 };
2432
2433 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002434 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002435 status_t res = postMessageSync(msg);
2436 if (res == NO_ERROR) {
2437 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2438 }
2439 return res;
2440}
2441
2442// ---------------------------------------------------------------------------
2443
Mathias Agopian7623da42010-06-01 15:12:58 -07002444sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2445{
2446 sp<Layer> result;
2447 Mutex::Autolock _l(mStateLock);
2448 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2449 return result;
2450}
2451
2452// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453
Mathias Agopian593c05c2010-06-02 23:28:45 -07002454Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002455 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457}
2458
Mathias Agopian593c05c2010-06-02 23:28:45 -07002459Client::~Client()
2460{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002461 const size_t count = mLayers.size();
2462 for (size_t i=0 ; i<count ; i++) {
2463 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2464 if (layer != 0) {
2465 mFlinger->removeLayer(layer);
2466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468}
2469
Mathias Agopian593c05c2010-06-02 23:28:45 -07002470status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002471 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472}
Mathias Agopian1473f462009-04-10 14:24:30 -07002473
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002474size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002475{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002476 Mutex::Autolock _l(mLock);
2477 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002478 mLayers.add(name, layer);
2479 return name;
2480}
2481
Mathias Agopian7623da42010-06-01 15:12:58 -07002482void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002483{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002484 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002485 // we do a linear search here, because this doesn't happen often
2486 const size_t count = mLayers.size();
2487 for (size_t i=0 ; i<count ; i++) {
2488 if (mLayers.valueAt(i) == layer) {
2489 mLayers.removeItemsAt(i, 1);
2490 break;
2491 }
2492 }
2493}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002494sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2495{
2496 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002497 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002498 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002499 if (layer != 0) {
2500 lbc = layer.promote();
2501 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002502 }
2503 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504}
2505
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002506
2507status_t Client::onTransact(
2508 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2509{
2510 // these must be checked
2511 IPCThreadState* ipc = IPCThreadState::self();
2512 const int pid = ipc->getCallingPid();
2513 const int uid = ipc->getCallingUid();
2514 const int self_pid = getpid();
2515 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2516 // we're called from a different process, do the real check
Mathias Agopian0dd593f2011-06-27 16:05:52 -07002517 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002518 {
2519 LOGE("Permission Denial: "
2520 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2521 return PERMISSION_DENIED;
2522 }
2523 }
2524 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopian7623da42010-06-01 15:12:58 -07002525}
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002526
2527
Mathias Agopian593c05c2010-06-02 23:28:45 -07002528sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002529 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002530 const String8& name,
2531 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 uint32_t flags)
2533{
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002534 /*
2535 * createSurface must be called from the GL thread so that it can
2536 * have access to the GL context.
2537 */
2538
2539 class MessageCreateSurface : public MessageBase {
2540 sp<ISurface> result;
2541 SurfaceFlinger* flinger;
2542 ISurfaceComposerClient::surface_data_t* params;
2543 Client* client;
2544 const String8& name;
2545 DisplayID display;
2546 uint32_t w, h;
2547 PixelFormat format;
2548 uint32_t flags;
2549 public:
2550 MessageCreateSurface(SurfaceFlinger* flinger,
2551 ISurfaceComposerClient::surface_data_t* params,
2552 const String8& name, Client* client,
2553 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2554 uint32_t flags)
2555 : flinger(flinger), params(params), client(client), name(name),
2556 display(display), w(w), h(h), format(format), flags(flags)
2557 {
2558 }
2559 sp<ISurface> getResult() const { return result; }
2560 virtual bool handler() {
2561 result = flinger->createSurface(params, name, client,
2562 display, w, h, format, flags);
2563 return true;
2564 }
2565 };
2566
2567 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2568 params, name, this, display, w, h, format, flags);
2569 mFlinger->postMessageSync(msg);
2570 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002572status_t Client::destroySurface(SurfaceID sid) {
2573 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575
2576// ---------------------------------------------------------------------------
2577
Jamie Gennisf7acf162011-01-12 18:30:40 -08002578GraphicBufferAlloc::GraphicBufferAlloc() {}
2579
2580GraphicBufferAlloc::~GraphicBufferAlloc() {}
2581
2582sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002583 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennisf7acf162011-01-12 18:30:40 -08002584 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2585 status_t err = graphicBuffer->initCheck();
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002586 *error = err;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002587 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002588 if (err == NO_MEMORY) {
2589 GraphicBuffer::dumpAllocationsToSystemLog();
2590 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002591 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2592 "failed (%s), handle=%p",
2593 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennisf7acf162011-01-12 18:30:40 -08002594 return 0;
2595 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002596 return graphicBuffer;
2597}
2598
Jamie Gennisf7acf162011-01-12 18:30:40 -08002599// ---------------------------------------------------------------------------
2600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601GraphicPlane::GraphicPlane()
2602 : mHw(0)
2603{
2604}
2605
2606GraphicPlane::~GraphicPlane() {
2607 delete mHw;
2608}
2609
2610bool GraphicPlane::initialized() const {
2611 return mHw ? true : false;
2612}
2613
Mathias Agopian66c77a52010-02-08 15:49:35 -08002614int GraphicPlane::getWidth() const {
2615 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616}
2617
Mathias Agopian66c77a52010-02-08 15:49:35 -08002618int GraphicPlane::getHeight() const {
2619 return mHeight;
2620}
2621
2622void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2623{
2624 mHw = hw;
2625
2626 // initialize the display orientation transform.
2627 // it's a constant that should come from the display driver.
2628 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2629 char property[PROPERTY_VALUE_MAX];
2630 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2631 //displayOrientation
2632 switch (atoi(property)) {
2633 case 90:
2634 displayOrientation = ISurfaceComposer::eOrientation90;
2635 break;
2636 case 270:
2637 displayOrientation = ISurfaceComposer::eOrientation270;
2638 break;
2639 }
2640 }
2641
2642 const float w = hw->getWidth();
2643 const float h = hw->getHeight();
2644 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2645 &mDisplayTransform);
2646 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2647 mDisplayWidth = h;
2648 mDisplayHeight = w;
2649 } else {
2650 mDisplayWidth = w;
2651 mDisplayHeight = h;
2652 }
2653
2654 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655}
2656
2657status_t GraphicPlane::orientationToTransfrom(
2658 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002659{
2660 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 switch (orientation) {
2662 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002663 flags = Transform::ROT_0;
2664 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002666 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 break;
2668 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002669 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 break;
2671 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002672 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 break;
2674 default:
2675 return BAD_VALUE;
2676 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002677 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 return NO_ERROR;
2679}
2680
2681status_t GraphicPlane::setOrientation(int orientation)
2682{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 // If the rotation can be handled in hardware, this is where
2684 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002685
2686 const DisplayHardware& hw(displayHardware());
2687 const float w = mDisplayWidth;
2688 const float h = mDisplayHeight;
2689 mWidth = int(w);
2690 mHeight = int(h);
2691
2692 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002693 GraphicPlane::orientationToTransfrom(orientation, w, h,
2694 &orientationTransform);
2695 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2696 mWidth = int(h);
2697 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002699
Mathias Agopian3552f532009-03-27 17:58:20 -07002700 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002701 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 return NO_ERROR;
2703}
2704
2705const DisplayHardware& GraphicPlane::displayHardware() const {
2706 return *mHw;
2707}
2708
Mathias Agopianaab758e2010-10-11 12:37:43 -07002709DisplayHardware& GraphicPlane::editDisplayHardware() {
2710 return *mHw;
2711}
2712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713const Transform& GraphicPlane::transform() const {
2714 return mGlobalTransform;
2715}
2716
Mathias Agopian1473f462009-04-10 14:24:30 -07002717EGLDisplay GraphicPlane::getEGLDisplay() const {
2718 return mHw->getEGLDisplay();
2719}
2720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721// ---------------------------------------------------------------------------
2722
2723}; // namespace android